-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.t
More file actions
97 lines (83 loc) · 3.35 KB
/
test_parser.t
File metadata and controls
97 lines (83 loc) · 3.35 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
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
-- Self-hosting parser: AST construction in Tuk
-- Expected: exit 0
use lexer
use parser
main() i32 {
-- Integer literal
n = make_int_expr(42)
assert(n.kind == EXPR_INT, "int kind")
assert(n.int_val == 42, "int val")
assert(expr_kind_name(n.kind) == "INT", "kind name INT")
-- Identifier
x = make_ident_expr("hello")
assert(x.kind == EXPR_IDENT, "ident kind")
assert(x.name == "hello", "ident name")
assert(expr_kind_name(x.kind) == "IDENT", "kind name IDENT")
-- Boolean
b = make_bool_expr(1)
assert(b.kind == EXPR_BOOL, "bool kind")
assert(b.int_val == 1, "bool val")
-- String
s = make_str_expr("world")
assert(s.kind == EXPR_STRING, "str kind")
assert(s.str_val == "world", "str val")
-- Binary: left + right
left = make_int_expr(3)
right = make_int_expr(4)
add = make_binary_expr(OP_ADD, left, right)
assert(add.kind == EXPR_BINARY, "binary kind")
assert(add.op == OP_ADD, "binary op add")
assert(add.left.int_val == 3, "binary left")
assert(add.right.int_val == 4, "binary right")
assert(expr_kind_name(add.kind) == "BINARY", "kind name BINARY")
-- Nested binary: (left + right) * 2
two = make_int_expr(2)
mul = make_binary_expr(OP_MUL, add, two)
assert(mul.op == OP_MUL, "nested op mul")
assert(mul.left.kind == EXPR_BINARY, "nested left is binary")
assert(mul.left.op == OP_ADD, "nested left op is add")
assert(mul.right.int_val == 2, "nested right is 2")
-- 3-level deep: ((a + b) * c) == d
a = make_ident_expr("a")
b2 = make_ident_expr("b")
c = make_ident_expr("c")
d = make_ident_expr("d")
ab = make_binary_expr(OP_ADD, a, b2)
abc = make_binary_expr(OP_MUL, ab, c)
full = make_binary_expr(OP_EQ, abc, d)
assert(full.kind == EXPR_BINARY, "3-level kind")
assert(full.op == OP_EQ, "3-level op eq")
assert(full.left.op == OP_MUL, "3-level left op mul")
assert(full.left.left.op == OP_ADD, "3-level left.left op add")
assert(full.right.name == "d", "3-level right is d")
-- Call expression
call = make_call_expr("print")
assert(call.kind == EXPR_CALL, "call kind")
assert(call.name == "print", "call name")
-- Unary: -x
neg = make_unary_expr(OP_SUB, x)
assert(neg.kind == EXPR_UNARY, "unary kind")
assert(neg.op == OP_SUB, "unary op")
assert(neg.left.name == "hello", "unary operand")
-- Op name helpers
assert(op_name(OP_ADD) == "+", "op_name +")
assert(op_name(OP_MUL) == "*", "op_name *")
assert(op_name(OP_EQ) == "==", "op_name ==")
assert(op_name(OP_AND) == "&&", "op_name &&")
-- Operator classification
assert(tok_to_binop(TOK_PLUS) == OP_ADD, "tok_to_binop +")
assert(tok_to_binop(TOK_STAR) == OP_MUL, "tok_to_binop *")
assert(tok_to_binop(TOK_EQEQ) == OP_EQ, "tok_to_binop ==")
assert(is_comparison_op(TOK_EQEQ) == true, "is_comparison ==")
assert(is_comparison_op(TOK_PLUS) == false, "is_comparison +")
assert(is_additive_op(TOK_PLUS) == true, "is_additive +")
assert(is_multiplicative_op(TOK_STAR) == true, "is_mult *")
-- Lexer integration
count = lex("x = 42")
assert(count >= 3, "lex count")
assert(keyword_tag("if") == TOK_IF, "kw if")
assert(keyword_tag("for") == TOK_FOR, "kw for")
assert(keyword_tag("match") == TOK_MATCH, "kw match")
print("SELF-HOSTING PARSER ALL PASSED")
> 0
}