-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_compiler.py
93 lines (82 loc) · 2.55 KB
/
test_compiler.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
from compiler import *
def check_output(srcexpr: SrcExpr, input: str, output: str):
received_output = exec_srcexpr(srcexpr, input=input, capture=True)
assert received_output == output
def test_ident():
ident = SrcRoot(
SrcBlock([
SrcApp(SrcVar("print"), SrcVar("input"))
])
)
check_output(ident, "We love Ohad", "We love Ohad")
def test_hello_world():
hello = SrcRoot(
SrcBlock([
SrcApp(SrcVar("print"), SrcStr("Hello world 🌍"))
])
)
check_output(hello, "", "Hello world 🌍")
def test_fib():
fib = SrcAbs("n",
SrcCall(SrcVar("if"), [
SrcCall(SrcVar("<="), [SrcVar("n"), SrcNat(2)]),
SrcNat(1),
SrcCall(SrcVar("+"), [
SrcApp(SrcVar("fib"), SrcApp(SrcVar("--"), SrcVar("n"))),
SrcApp(SrcVar("fib"), SrcCall(SrcVar("-"), [SrcVar("n"), SrcNat(2)])),
])
]
)
)
print_fib = SrcRoot(
SrcBlock([
SrcDefine("fib", fib),
SrcApp(SrcVar("print"),
SrcApp(SrcVar("nat->str"), SrcApp(SrcVar("fib"), SrcNat(8))))
])
)
check_output(print_fib, "", "21")
def test_even_odd():
even = SrcAbs("n",
SrcCall(SrcVar("is-zero"), [
SrcVar("n"),
SrcBool(True),
SrcApp(SrcVar("odd"), SrcApp(SrcVar("--"), SrcVar("n")))
])
)
odd = SrcAbs("n",
SrcCall(SrcVar("is-zero"), [
SrcVar("n"),
SrcBool(False),
SrcApp(SrcVar("even"), SrcApp(SrcVar("--"), SrcVar("n")))
])
)
print_even_odd = lambda n: SrcRoot(
SrcBlock([
SrcDefine("odd", odd),
SrcDefine("even", even),
SrcApp(SrcVar("print"),
SrcCall(SrcApp(SrcVar("even"), SrcNat(n)), [
SrcStr("even"),
SrcStr("odd"),
])
),
SrcApp(SrcVar("print"), SrcStr("\n"))
])
)
check_output(print_even_odd(151), "", "odd\n")
check_output(print_even_odd(21), "", "odd\n")
check_output(print_even_odd(12), "", "even\n")
check_output(print_even_odd(0), "", "even\n")
def test_even_odd_parse():
even_odd = '''
(define odd
(lambda n (if (is-zero n) #f (even (-- n)))))
(define even
(lambda n (if (is-zero n) #t (odd (-- n)))))
(if (even 3)
(print "even")
(print "odd"))
'''
check_output(parse(even_odd), "", "odd")