-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComa.hs
More file actions
56 lines (37 loc) · 1.58 KB
/
Coma.hs
File metadata and controls
56 lines (37 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- Module Coma contains the "eval" function -- the most important function in
-- the codebase. It receives Coma source code as a String and returns another
-- String -- the output of the script. This way, Main simply passes pure data to
-- us. However, our scipts require file-reading to happen in the process and
-- that is why we're returning "IO String" instead of just "String".
module Coma (eval, exec) where
import qualified Data.HashMap as HM
import qualified Lexer
import qualified Ast
import qualified Core
-- EVAL
-- Receive source code of a file and return output. Sometimes, the output is not
-- necessarily CSV, it can be any valid Ast.Coma expression displayed as
-- String.
eval :: String -> IO String
eval code = (exec $ Ast.parse $ Lexer.lex code) >>= (return . show)
-- EXEC
-- Interpreter function. Take Ast.Coma, execute instructions, and return
-- simplified Ast.Coma.
exec :: Ast.Coma -> IO Ast.Coma
exec = Ast.execWithEnv env
-- ENV
-- Contains Coma's standard library of functions.
env :: HM.Map String Ast.Coma
env = HM.fromList
[ ("read", Ast.Lambda 0 HM.empty Core.read)
, ("join", Ast.Lambda 0 HM.empty Core.join)
, ("get", Ast.Lambda 0 HM.empty Core.get)
, ("select", Ast.Lambda 0 HM.empty Core.select)
, ("value", Ast.Lambda 0 HM.empty Core.value)
, ("merge", Ast.Lambda 0 HM.empty Core.merge)
, ("given", Ast.Lambda 0 HM.empty Core.given)
, ("each", Ast.Lambda 0 HM.empty Core.forEach)
, ("csv", Ast.Lambda 0 HM.empty Core.csv)
, ("if", Ast.Lambda 0 HM.empty Core.ifElse)
, ("not", Ast.Lambda 0 HM.empty Core.boolNot)
]