-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp3_ast_compiler.hs
74 lines (61 loc) · 1.84 KB
/
interp3_ast_compiler.hs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Interp3_Ast_Translate
import Interp3_Ast_Tests
import Interp2_Parsing as L
import Interp2_Eval
decompileCom :: Com -> String
decompileCom com =
case com of
L.Push const -> "Push " ++ decompileConst const ++ "; "
L.Pop -> "Pop; "
L.Trace -> "Trace; "
L.Add -> "Add; "
L.Sub -> "Sub; "
L.Mul -> "Mul; "
L.Div -> "Div; "
L.And -> "And; "
L.Or -> "Or; "
L.Not -> "Not; "
L.Lt -> "Lt; "
L.Gt -> "Gt; "
L.Swap -> "Swap; "
L.Call -> "Call; "
L.Return -> "Return; "
L.Bind -> "Bind; "
L.Lookup -> "Lookup; "
L.Ifte ifCom elseCom -> "If " ++ decompile ifCom ++ "Else " ++ decompile elseCom ++ "End; "
L.Fun funCom -> "Fun " ++ decompile funCom ++ "Swap; Return; End; "
decompileConst :: Const -> String
decompileConst const =
case const of
IntConst n -> show n
BoolConst b -> show b
UnitConst -> "Unit"
Sym s -> s
decompile :: Prog -> String
decompile = concatMap decompileCom
evalWrap :: String -> IO Trace
evalWrap s = do
result <- parseProgram s
case result of
Left parseError -> return []
Right prog -> return (eval [] [] [] prog)
main :: IO ()
main = do
putStrLn "Test 1:"
evalWrap (decompile (translateAst test1)) >>= print
putStrLn "\nTest 2:"
evalWrap (decompile (translateAst test2)) >>= print
putStrLn "\nTest 3:"
evalWrap (decompile (translateAst test3)) >>= print
putStrLn "\nTest 4:"
evalWrap (decompile (translateAst test4)) >>= print
putStrLn "\nTest 5:"
evalWrap (decompile (translateAst test5)) >>= print
putStrLn "\nTest 6:"
evalWrap (decompile (translateAst test6)) >>= print
putStrLn "\nTest 7:"
evalWrap (decompile (translateAst test7)) >>= print
putStrLn "\nTest 8:"
evalWrap (decompile (translateAst test8)) >>= print
putStrLn "\nTest 9:"
evalWrap (decompile (translateAst test9)) >>= print