-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtests.py
executable file
·177 lines (128 loc) · 4.91 KB
/
tests.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
#!/usr/bin/python3
import tdop
import arith_parse
def _assertParseError(make_parser, s, error_substring=''):
p = make_parser(s)
try:
node = p.Parse()
except tdop.ParseError as e:
err = str(e)
if error_substring in err:
print('got expected error for %s: %s' % (s, err))
else:
raise AssertionError('Expected %r to be in %r' % (error_substring, err))
else:
raise AssertionError('%r should have failed' % s)
def TestArith(t_parse):
t_parse('1+2+3', '(+ (+ 1 2) 3)')
t_parse('1+2*3', '(+ 1 (* 2 3))')
t_parse('4*(2+3)', '(* 4 (+ 2 3))')
t_parse('(2+3)*4', '(* (+ 2 3) 4)')
t_parse('1<2', '(< 1 2)')
t_parse('x=3', '(= x 3)')
t_parse('x = 2*3', '(= x (* 2 3))')
t_parse('x = y', '(= x y)')
t_parse('x*y - y*z', '(- (* x y) (* y z))')
t_parse('x/y - y%z', '(- (/ x y) (% y z))')
t_parse("x = y", "(= x y)")
t_parse('2 ** 3 ** 2', '(** 2 (** 3 2))')
t_parse('- 3 ** 2', '(- (** 3 2))')
t_parse('a = b = 10', '(= a (= b 10))')
t_parse('x = ((y*4)-2)', '(= x (- (* y 4) 2))')
t_parse('x - -y', '(- x (- y))')
t_parse("-1 * -2", "(* (- 1) (- 2))")
t_parse("-x * -y", "(* (- x) (- y))")
t_parse('x - -234', '(- x (- 234))')
# Python doesn't allow this
t_parse('x += y += 3', '(+= x (+= y 3))')
# This is sort of nonsensical, but bash allows it. The 1 is discarded as
# the first element of the comma operator.
t_parse('x[1,2]', '(get x (, 1 2))')
# Python doesn't have unary +
t_parse('+1 - +2', '(- (+ 1) (+ 2))')
# LHS
t_parse('f[x] += 1', '(+= (get f x) 1)')
def TestBitwise(t_parse):
t_parse("~1 | ~2", "(| (~ 1) (~ 2))")
t_parse("x & y | a & b", "(| (& x y) (& a b))")
t_parse("~x ^ y", "(^ (~ x) y)")
t_parse("x << y | y << z", "(| (<< x y) (<< y z))")
t_parse("a ^= b-1", "(^= a (- b 1))")
def TestLogical(t_parse):
t_parse("a && b || c && d", "(|| (&& a b) (&& c d))")
t_parse("!a && !b", "(&& (! a) (! b))")
t_parse("a != b && c == d", "(&& (!= a b) (== c d))")
t_parse("a > b ? 0 : 1", "(? (> a b) 0 1)")
t_parse("a > b ? x+1 : y+1", "(? (> a b) (+ x 1) (+ y 1))")
t_parse("1 ? true1 : 2 ? true2 : false", "(? 1 true1 (? 2 true2 false))")
t_parse("1 ? true1 : (2 ? true2 : false)", "(? 1 true1 (? 2 true2 false))")
t_parse("1 ? (2 ? true : false1) : false2", "(? 1 (? 2 true false1) false2)")
t_parse("1 ? 2 ? true : false1 : false2", "(? 1 (? 2 true false1) false2)")
# [cling]$ true ? 1 : 2, true ? 3 : 4
# (int) 3
# Comma expressions can be inside
t_parse("x ? 1 : 2, y ? 3 : 4", "(, (? x 1 2) (? y 3 4))")
t_parse('a , b ? c, d : e, f', '(, a (? b (, c d) e) f)')
def TestUnary(t_parse):
t_parse("!x", "(! x)")
t_parse("x--", "(post-- x)")
t_parse("x[1]--", "(post-- (get x 1))")
t_parse("--x", "(-- x)")
t_parse("++x[1]", "(++ (get x 1))")
t_parse("!x--", "(! (post-- x))")
t_parse("~x++", "(~ (post++ x))")
t_parse("x++ - y++", "(- (post++ x) (post++ y))")
t_parse("++x - ++y", "(- (++ x) (++ y))")
#
# 1. x++ f() x[] left associative
# f(x)[1]++ means
# (++ (get (call f x) 1))
# 2. ++x + - ! ~ right associative
# -++x means (- (++ x))
def TestArrays(t_parse):
"""Shared between shell, oil, and Python."""
t_parse('x[1]', '(get x 1)')
t_parse('x[a+b]', '(get x (+ a b))')
def TestComma(t_parse):
t_parse('x=1,y=2,z=3', '(, (= x 1) (= y 2) (= z 3))')
def TestFuncCalls(t_parse):
t_parse('x = y(2)*3 + y(4)*5', '(= x (+ (* (call y 2) 3) (* (call y 4) 5)))')
t_parse('x(1,2)+y(3,4)', '(+ (call x 1 2) (call y 3 4))')
t_parse('x(a,b,c[d])', '(call x a b (get c d))')
t_parse('x(1,2)*j+y(3,4)*k+z(5,6)*l',
'(+ (+ (* (call x 1 2) j) (* (call y 3 4) k)) (* (call z 5 6) l))')
t_parse('print(test(2,3))', '(call print (call test 2 3))')
t_parse('print("x")', '(call print x)')
t_parse('min(255,n*2)', '(call min 255 (* n 2))')
t_parse('c = pal[i*8]', '(= c (get pal (* i 8)))')
def TestErrors(p):
_assertParseError(p, '}')
_assertParseError(p, ']')
_assertParseError(p, '{') # depends on language
_assertParseError(p, "x+1 = y", "Can't assign")
_assertParseError(p, "(x+1)++", "Can't assign")
# Should be an EOF error
_assertParseError(p, 'foo ? 1 :', 'Unexpected end')
_assertParseError(p, 'foo ? 1 ', 'expected :')
_assertParseError(p, '%', "can't be used in prefix position")
error_str = "can't be used in prefix"
_assertParseError(p, '}')
_assertParseError(p, '{')
_assertParseError(p, ']', error_str)
_assertParseError(p, '1 ( 2', "can't be called")
_assertParseError(p, '(x+1) ( 2 )', "can't be called")
#_assertParseError(p, '1 ) 2')
_assertParseError(p, '1 [ 2 ]', "can't be indexed")
def main():
t_parse = arith_parse.ParseShell
p = arith_parse.MakeParser
TestArith(t_parse)
TestBitwise(t_parse)
TestLogical(t_parse)
TestUnary(t_parse)
TestArrays(t_parse)
TestFuncCalls(t_parse)
TestComma(t_parse)
TestErrors(p)
if __name__ == '__main__':
main()