This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_typechecker.py
184 lines (163 loc) · 8.21 KB
/
test_typechecker.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python
import unittest
from lexer import Lexer
from parser import Parser
from typechecker import TypeChecker, TypecheckException
import sys # for stderr printing
# To test the typechecker, we only check code here that should fail.
# Everything else is tested in test_evaluator.py (we just need to check that
# the typechecker returns True which is a task that can be done easily there).
class TestTypechecker(unittest.TestCase):
def test_function(self):
self.check("func f() : int {}")
self.check("func f() { return 5 } f()")
self.check("func f() : int { return 1337.0 } f()")
self.check("func add(a : int, b : int) : int { return a + b } add(21, true)")
self.check("func calc(a:int, b:int, c:int) : float { return a + b * c } calc(3, 5, 7)")
self.check("func faculty(n:int) : int { if n <= 1 { /* return 1 */ } else { return n * faculty(n-1) } } faculty(5)")
def test_return(self):
self.check("return \"no int\"\n")
self.check("return 1337.0")
self.check("return 21.0 ^ 2.0")
self.check("return 21 * 2.0; 13.37")
def test_syscall(self):
# TODO How can program calls fail the typechecking stage? Is there
# another possibility?
self.check("let a : float = ls examples")
self.check("let a : float = grep -nr bong .")
self.check("let a : float = cd")
self.check("let a : float = /usr/bin/true")
self.check("let a : float = /usr/bin/false")
def test_pipe(self):
self.check("ls -la | grep foobar | len(\"mystring\") ")
self.check("ls -la | grep test | nonexistentfunc() | /usr/bin/true")
self.check("ls | grep foobar | sameasabove(1.0, 2.0) ")
def test_advanced_pipe(self):
self.check('let a = 1337 a | grep foo | /usr/bin/true')
self.check('let a = "foo" a | grep bar | len(a)')
# TODO Too lazy now
"""
self.check('func a() : str { return "foo" } a() | grep foo | /usr/bin/true')
self.check('func a() : str { return "foo" } a() | grep bar')
self.check('let a = ""; echo "foo" | a; a')
self.check('let a=""; let b=""; echo "foo\nbar" | grep foo | a,b; a')
self.check('let a=""; let b=""; echo "foo\nbar" | grep foo | grep bar | b,a; b')
self.check('let a = "foo"; a | grep foo | let b; b')
"""
def test_builtin_functions(self):
self.check('len(1337)')
self.check('let a = 1337.5; len(a)')
def test_let(self):
self.check("let a : float = 1337")
self.check("let a = 42 let b : str = a + 1337")
self.check("let a,b : float = 1,0")
def test_if(self):
self.check("let a = 1337 if a == \"no number\" { true } else { false }")
self.check("if false { return 1337 } else { return 42.0 }")
self.check("if \"no_bool\" { 1337 }")
self.check("if 0.0 { 1337 }")
""" No shadowing anymore. Just look for a different name :)
def test_shadowing(self):
tests = [
"let a = 5 { let a = 10 { a } }", 10,
"let a = 5 { let a = 10 } a", 5,
]
test_eval_list(self, tests)
"""
def test_print_arith(self):
self.check("print 2.0 + 4 - 2")
self.check("print 4^2.0")
self.check("print 2^2 + 2^3.0")
self.check("print (1+2)*3.0")
self.check("print (27%5)^2.0")
def test_expression_statements(self):
self.check("let a : str = 1337")
self.check("2 + 4 - 2.0")
self.check("4^2.0")
self.check("2^2 + 2^3 * \"a_string\"")
self.check("2*3 - 12.5")
self.check("12 - 2.0^3" )
self.check("(1+0.2)*3")
self.check("1 + 2.0 *3")
self.check("(1+2.0)^(1+2)")
self.check("27.0%5")
self.check("(27%5)^2.0")
self.check("!true + 1")
self.check("let a = 42 a = 1337.0")
# TODO Do the following or don't do it???? -> also see test_evaluator.py
# Assignments don't return anything anymore.
# Reasons: 1. Consistency with let statement,
# 2. For multiple assignments at once, use ExpressionList syntax: a,b = 0,0
# 3. Assignments don't feel like expressions anymore (which they aren't due to ExpList syntax!)
self.check("let a = 1337 let b = 42.0 a = b = 15")
self.check("let a = 1337 let b = 42.0 a = b = 15")
self.check("let a : [][]int = [1, 2, 3] a[0]")
self.check("[1, 2, 3][0][0]")
self.check("\"1, 2, 3\"[0] + 7")
self.check("let a,b = 1,0.0 a,b=b,a")
def test_let_array(self):
self.check("let a : []float = [1]")
self.check("let a = []")
def test_error_messages(self):
# The following tests fail for various reasons, they are used to
# (manually) test the error reporting capabilities of the typechecker
self.check("123 + 22.0 * 13.1")
self.check("123 + 22.0 * 13")
self.check("let a : int = []")
self.check("let a : []int = [1.0]")
self.check("let a = 5; let b : float = a")
self.check("if 5 == \"asdf\" { }")
self.check("let a = 5; while a {}")
def test_struct_definition(self):
self.check("struct T { x : unknown, y : float }") # inner type unknown
# TODO recursive types, see issue #25
#self.check("struct A { x : B }; struct B { y : A }") # recursive types
def test_struct_value(self):
self.check("T { x : 5 }") # type undefined
self.check("T { foo : A { bar : 5 } }") # inner type undefined
self.check("struct T { x : int } T { y : 5 }") # wrong field name
self.check("struct T { x : int } T { x : 1.0 }") # wrong field type
self.check("struct A { x : int } struct B { x : A } B { x : A }") # A not initialized correctly
self.check("struct A { x : int } struct B { y : A } B { x : A { x : 1.0 } }") # wrong inner field type
def test_dot_access(self):
self.check("a.b") # Variable missing
self.check("struct T { x : int } let a = T { x : 5 }; a.y") # wrong field
self.check("struct T { x : int } let a = T { x : 5 }; a.x = 1.0") # wrong field type
self.check("struct T { x : int } let a = T { x : 5 }; a = 1.0") # type does not match
self.check("struct T { x : int } let a = 1.0; a = T { x : 5 }") # type does not match
self.check("struct T { x : int } struct U { y : int } let a = T { x : 5 }; a = U { y : 7 }") # type does not match
self.check("struct T { x : int } struct U { y : float } let a = T { x : 5 }; let b = U { y : 7.0 }; a.x = b.y") # type does not match
def test_import(self):
self.check("import \"nonexistentfile.bon\" as mod")
self.check("import \"tests/module_buggy.bon\" as mod")
self.check("import \"tests/module_missingtype.bon\" as mod")
self.check("import \"tests/module_missingsubmodule.bon\" as mod")
self.check("import \"tests/module_missingsubsubmodule.bon\" as mod")
self.check("import \"tests/module.bon\" as mod; mod.missingfunc();")
self.check("import \"tests/module.bon\" as mod; let a : mod.missingtype = mod.missingtype { x : 5 };")
self.check("import \"tests/module.bon\" as mod; let s : float = mod.modulefunc(); s") # wrong type
def check(self, code):
worked = typecheck(code)
self.assertFalse(worked, "Expected typechecker to fail.")
def typecheck(code):
l = Lexer(code, "test_typechecker.py input")
p = Parser(l)
tc= TypeChecker()
program = p.compile()
try:
tc.checkprogram_uncaught(program)
except TypecheckException as e:
# DEBUG: Print error messages
if False:
print(f"\nTypecheckError when checking '{code}'", file=sys.stderr)
loc = e.node.get_location()
posstring = f" in {loc[0]}, line {loc[1]} col {loc[2]} to line {loc[3]} col {loc[4]}"
print(f"TypecheckError{posstring}: ", file=sys.stderr, end='')
if loc[5]: # if location is valid
for i, line in enumerate(code.split("\n")):
if i+1 == loc[1]:
print(f"\n{line}", file=sys.stderr)
print(" "*(loc[2]-1) + "^"*(loc[4]-loc[2]+1) , file=sys.stderr)
print(f"{str(e.msg)}\n", file=sys.stderr)
return False
return True