-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-type-checker.py
executable file
·69 lines (51 loc) · 1.54 KB
/
test-type-checker.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
#!/usr/bin/env python3
from lib.parser.lexer import tokenize
from lib.parser.parser import *
from semantic_analysis import *
from lib.analysis.typechecker import *
def main():
from io import StringIO
module_name = "test"
testprog = StringIO('''
Bool x = -5;
prefix - (x) :: Int -> Bool {
return True;
}
prefix ! (x) :: Int -> Bool {
return False;
}
/*infixl 7 ++ (a, b) :: Int Int -> Int {
return a + b + 2;
}*/
main() :: -> Int {
Bool x = !(-(5));
x = True;
}
''')
# Tokenize / parse
tokenstream = tokenize(testprog)
tokenlist = list(tokenstream)
ast = SPL.parse_strict(tokenlist, testprog)
# Build symbol table
ERROR_HANDLER.setSourceMapping(testprog)
symbol_table = buildSymbolTable(ast)
ERROR_HANDLER.checkpoint()
# Normalize table
normalizeAllTypes(symbol_table)
ERROR_HANDLER.checkpoint()
# Resolve Expr names
resolveNames(symbol_table)
ERROR_HANDLER.checkpoint()
# Build operator table
op_table = buildOperatorTable()
mergeCustomOps(op_table, symbol_table, module_name)
builtin_funcs = generateBuiltinFuncs()
# Parse expressions
fixExpression(ast, op_table)
ERROR_HANDLER.checkpoint()
# Type check
typecheck_globals(symbol_table, op_table)
typecheck_functions(symbol_table, op_table, builtin_funcs)
ERROR_HANDLER.checkpoint()
if __name__ == "__main__":
main()