-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (54 loc) · 2.23 KB
/
main.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
from bantam_parser import BantamParser
from lexer import Lexer
# some turn-around to avoid creating fixtures for pytest.
def parse_string(source: str) -> str:
lexer = Lexer(source)
lexer.tokenize()
parser = BantamParser(lexer.get_tokens())
return str(parser.parseExpression())
def test_call():
# function call
assert parse_string("a()") == "a()"
assert parse_string("a(b)") == "a(b)"
assert parse_string("a(b, c)") == "a(b, c)"
assert parse_string("a(b)(c)") == "a(b)(c)"
assert parse_string("a(b) + c(d)") == "(a(b) + c(d))"
assert parse_string("a(b ? c : d, e + f)") == "a((b ? c : d), (e + f))"
def test_unary_precedence():
# Unary precedence.
assert parse_string("~!-+a") == "(~(!(-(+a))))"
assert parse_string("a!!!") == "(((a!)!)!)"
def test_unary_and_binary_precedence():
# Unary and binary precedence.
assert parse_string("-a * b") == "((-a) * b)"
assert parse_string("!a + b") == "((!a) + b)"
assert parse_string("~a ^ b") == "((~a) ^ b)"
assert parse_string("-a!") == "(-(a!))"
assert parse_string("!a!") == "(!(a!))"
def test_binary_precedence():
# binary precedence.
assert parse_string("a = b + c * d ^ e - f / g") == "(a = ((b + (c * (d ^ e))) - (f / g)))"
def test_binary_associativity():
# binary associativity.
assert parse_string("a = b = c") == "(a = (b = c))"
assert parse_string("a + b - c") == "((a + b) - c)"
assert parse_string("a * b / c") == "((a * b) / c)"
assert parse_string("a ^ b ^ c") == "(a ^ (b ^ c))"
def test_conditional_operator():
# Conditional operator.
assert parse_string("a ? b : c ? d : e") == "(a ? b : (c ? d : e))"
assert parse_string("a ? b ? c : d : e") == "(a ? (b ? c : d) : e)"
assert parse_string("a + b ? c * d : e / f") == "((a + b) ? (c * d) : (e / f))"
def test_grouping():
# Grouping.
assert parse_string("a + (b + c) + d") == "((a + (b + c)) + d)"
assert parse_string("a ^ (b + c)") == "(a ^ (b + c))"
assert parse_string("(!a)!") == "((!a)!)"
if __name__ == "__main__":
test_call()
test_unary_precedence()
test_unary_and_binary_precedence()
test_binary_precedence()
test_binary_associativity()
test_conditional_operator()
test_grouping()