Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1913 | jaeger | 1 | # figure out if we're in python or tinypy (tinypy displays "1.0" as "1") |
2 | is_tinypy = (str(1.0) == "1") |
||
3 | if not is_tinypy: |
||
4 | from boot import * |
||
5 | |||
6 | ################################################################################ |
||
7 | RM = 'rm -f ' |
||
8 | VM = './vm ' |
||
9 | TINYPY = './tinypy ' |
||
10 | TMP = 'tmp.txt' |
||
11 | if '-mingw32' in ARGV or "-win" in ARGV: |
||
12 | RM = 'del ' |
||
13 | VM = 'vm ' |
||
14 | TINYPY = 'tinypy ' |
||
15 | TMP = 'tmp.txt' |
||
16 | #TMP = 'stdout.txt' |
||
17 | def system_rm(fname): |
||
18 | system(RM+fname) |
||
19 | |||
20 | ################################################################################ |
||
21 | #if not is_tinypy: |
||
22 | #v = chksize() |
||
23 | #assert (v < 65536) |
||
24 | |||
25 | ################################################################################ |
||
26 | def t_show(t): |
||
27 | if t.type == 'string': return '"'+t.val+'"' |
||
28 | if t.type == 'number': return t.val |
||
29 | if t.type == 'symbol': return t.val |
||
30 | if t.type == 'name': return '$'+t.val |
||
31 | return t.type |
||
32 | def t_tokenize(s,exp=''): |
||
33 | import tokenize |
||
34 | result = tokenize.tokenize(s) |
||
35 | res = ' '.join([t_show(t) for t in result]) |
||
36 | #print(s); print(exp); print(res) |
||
37 | assert(res == exp) |
||
38 | |||
39 | if __name__ == '__main__': |
||
40 | t_tokenize("234",'234') |
||
41 | t_tokenize("234.234",'234.234') |
||
42 | t_tokenize("phil",'$phil') |
||
43 | t_tokenize("_phil234",'$_phil234') |
||
44 | t_tokenize("'phil'",'"phil"') |
||
45 | t_tokenize('"phil"','"phil"') |
||
46 | t_tokenize("'phil' x",'"phil" $x') |
||
47 | t_tokenize("#comment","") |
||
48 | t_tokenize("and","and") |
||
49 | t_tokenize("=","=") |
||
50 | t_tokenize("()","( )") |
||
51 | t_tokenize("(==)","( == )") |
||
52 | t_tokenize("phil=234","$phil = 234") |
||
53 | t_tokenize("a b","$a $b") |
||
54 | t_tokenize("a\nb","$a nl $b") |
||
55 | t_tokenize("a\n b","$a nl indent $b dedent") |
||
56 | t_tokenize("a\n b\n c", "$a nl indent $b nl indent $c dedent dedent") |
||
57 | t_tokenize("a\n b\n c", "$a nl indent $b nl $c dedent") |
||
58 | t_tokenize("a\n b\n \n c", "$a nl indent $b nl nl indent $c dedent dedent") |
||
59 | t_tokenize("a\n b\nc", "$a nl indent $b nl dedent $c") |
||
60 | t_tokenize("a\n b\n c\nd", "$a nl indent $b nl indent $c nl dedent dedent $d") |
||
61 | t_tokenize("(\n )","( )") |
||
62 | t_tokenize(" x","indent $x dedent") |
||
63 | t_tokenize(" #","") |
||
64 | t_tokenize("None","None") |
||
65 | |||
66 | |||
67 | ################################################################################ |
||
68 | |||
69 | def t_lisp(t): |
||
70 | if t.type == 'block': |
||
71 | return """{%s}"""%' '.join([t_lisp(tt) for tt in t.items]) |
||
72 | if t.type == 'statement': |
||
73 | return """%s;"""%' '.join([t_lisp(tt) for tt in t.items]) |
||
74 | if t.items == None: return t.val |
||
75 | args = ''.join([" "+t_lisp(tt) for tt in t.items]) |
||
76 | return "("+t.val+args+")" |
||
77 | |||
78 | def t_parse(s,ex=''): |
||
79 | import tokenize, parse |
||
80 | r = '' |
||
81 | tokens = tokenize.tokenize(s) |
||
82 | tree = parse.parse(s,tokens) |
||
83 | r = t_lisp(tree) |
||
84 | #print(s); print(ex); print(r) |
||
85 | assert(r==ex) |
||
86 | |||
87 | if __name__ == '__main__': |
||
88 | t_parse('2+4*3', '(+ 2 (* 4 3))') |
||
89 | t_parse('4*(2+3)', '(* 4 (+ 2 3))') |
||
90 | t_parse('(2+3)*4', '(* (+ 2 3) 4)') |
||
91 | t_parse('1<2', '(< 1 2)') |
||
92 | t_parse('x=3', '(= x 3)') |
||
93 | t_parse('x = 2*3', '(= x (* 2 3))') |
||
94 | t_parse('x = y', '(= x y)') |
||
95 | t_parse('2,3', '(, 2 3)') |
||
96 | t_parse('2,3,4', '(, 2 3 4)') |
||
97 | t_parse('[]', '([])') |
||
98 | t_parse('[1]', '([] 1)') |
||
99 | t_parse('[2,3,4]', '([] 2 3 4)') |
||
100 | t_parse('print(3)', '($ print 3)') |
||
101 | t_parse('print()', '($ print)') |
||
102 | t_parse('print(2,3)', '($ print 2 3)') |
||
103 | t_parse('def fnc():pass', '(def fnc (():) pass)') |
||
104 | t_parse('def fnc(x):pass', '(def fnc ((): x) pass)') |
||
105 | t_parse('def fnc(x,y):pass', '(def fnc ((): x y) pass)') |
||
106 | t_parse('x\ny\nz', '(; x y z)') |
||
107 | t_parse('return x', '(return x)') |
||
108 | t_parse('print(test(2,3))', '($ print ($ test 2 3))') |
||
109 | t_parse('x.y', '(. x y)') |
||
110 | t_parse('get(2).x', '(. ($ get 2) x)') |
||
111 | t_parse('{}', '({})') |
||
112 | t_parse('True', 'True') |
||
113 | t_parse('False', 'False') |
||
114 | t_parse('None', 'None') |
||
115 | t_parse('while 1:pass', '(while 1 pass)') |
||
116 | t_parse('for x in y:pass', '(for x y pass)') |
||
117 | t_parse('print("x")', '($ print x)') |
||
118 | t_parse('if 1: pass', '(if (elif 1 pass))') |
||
119 | t_parse('x = []', '(= x ([]))') |
||
120 | t_parse('x[1]', '(. x 1)') |
||
121 | t_parse('import sdl', '(import sdl)') |
||
122 | t_parse('2 is 3', '(is 2 3)') |
||
123 | t_parse('2 is not 3', '(isnot 2 3)') |
||
124 | t_parse('x is None', '(is x None)') |
||
125 | t_parse('2 is 3 or 4 is 5', '(or (is 2 3) (is 4 5))') |
||
126 | t_parse('e.x == 3 or e.x == 4', '(or (== (. e x) 3) (== (. e x) 4))') |
||
127 | t_parse('if 1==2: 3\nelif 4:5\nelse:6', '(if (elif (== 1 2) 3) (elif 4 5) (else 6))') |
||
128 | t_parse('x = y(2)*3 + y(4)*5', '(= x (+ (* ($ y 2) 3) (* ($ y 4) 5)))') |
||
129 | t_parse('x(1,2)+y(3,4)', '(+ ($ x 1 2) ($ y 3 4))') |
||
130 | t_parse('x(a,b,c[d])', '($ x a b (. c d))') |
||
131 | t_parse('x(1,2)*j+y(3,4)*k+z(5,6)*l', '(+ (+ (* ($ x 1 2) j) (* ($ y 3 4) k)) (* ($ z 5 6) l))') |
||
132 | t_parse('a = b.x/c * 2 - 1', '(= a (- (* (/ (. b x) c) 2) 1))') |
||
133 | t_parse('for x in y: z', '(for x y z)') |
||
134 | |||
135 | t_parse('min(255,n*2)','($ min 255 (* n 2))') |
||
136 | t_parse('c = pal[i*8]','(= c (. pal (* i 8)))') |
||
137 | t_parse('{x:y,a:b}','({} x y a b)') |
||
138 | t_parse('x[1:2:3]','(. x (: 1 2 3))') |
||
139 | if is_tinypy: t_parse('x - -234','(- x -234)') |
||
140 | else: t_parse('x - -234','(- x -234.0)') |
||
141 | t_parse('x - -y','(- x (- 0 y))') |
||
142 | t_parse('x = ((y*4)-2)','(= x (- (* y 4) 2))') |
||
143 | |||
144 | if is_tinypy: t_parse('print([1,2,"OK",4][-3:3][1])','($ print (. (. ([] 1 2 OK 4) (: -3 3)) 1))') |
||
145 | else: t_parse('print([1,2,"OK",4][-3:3][1])','($ print (. (. ([] 1 2 OK 4) (: -3.0 3)) 1))') |
||
146 | |||
147 | t_parse('a,b = 1,2','(= (, a b) (, 1 2))') |
||
148 | t_parse('class C: pass','(class C (methods pass))') |
||
149 | t_parse('def test(*v): pass','(def test ((): (* v)) pass)') |
||
150 | t_parse('def test(**v): pass','(def test ((): (** v)) pass)') |
||
151 | t_parse('test(*v)','($ test (* v))') |
||
152 | t_parse('test(**v)','($ test (** v))') |
||
153 | t_parse('def test(x=y): pass','(def test ((): (= x y)) pass)') |
||
154 | t_parse('test(x=y)','($ test (= x y))') |
||
155 | t_parse('def test(y="K",x="Z"): pass','(def test ((): (= y K) (= x Z)) pass)') |
||
156 | t_parse('return x+y','(return (+ x y))') |
||
157 | t_parse('if "a" is not "b": pass','(if (elif (isnot a b) pass))') |
||
158 | t_parse('z = 0\nfor x in y: pass','(; (= z 0) (for x y pass))') |
||
159 | t_parse('for k in {"OK":0}: pass','(for k ({} OK 0) pass)') |
||
160 | t_parse('print(test(10,3,z=50000,*[200],**{"x":4000}))','($ print ($ test 10 3 (= z 50000) (* ([] 200)) (** ({} x 4000))))') |
||
161 | t_parse('x="OK";print(x)','(; (= x OK) ($ print x))') |
||
162 | t_parse('[(1,3)]','([] (, 1 3))') |
||
163 | t_parse('x[:]','(. x (: None None))') |
||
164 | t_parse('x[:1]','(. x (: None 1))') |
||
165 | t_parse('x[1:]','(. x (: 1 None))') |
||
166 | t_parse('return\nx','(; return x)') |
||
167 | t_parse('"""test"""','test') |
||
168 | t_parse('return a,b','(return (, a b))') |
||
169 | |||
170 | ################################################################################ |
||
171 | |||
172 | def showerror(cmd, ss, ex, res): |
||
173 | print(cmd) |
||
174 | print("ss : '" + str(ss) + "'") |
||
175 | print("ex : '" + str(ex) + "'") |
||
176 | print("res: '" + str(res) + "'") |
||
177 | |||
178 | def t_render(ss,ex,exact=True): |
||
179 | import tokenize, parse, encode |
||
180 | |||
181 | if not istype(ss,'list'): ss =[ss] |
||
182 | n = 1 |
||
183 | for s in ss: |
||
184 | fname = 'tmp'+str(n)+'.tpc' |
||
185 | system_rm(fname) |
||
186 | tokens = tokenize.tokenize(s) |
||
187 | t = parse.parse(s,tokens) |
||
188 | r = encode.encode(fname,s,t) |
||
189 | f = save(fname,r) |
||
190 | n += 1 |
||
191 | system_rm('tmp.txt') |
||
192 | cmd = VM + fname + " > tmp.txt" |
||
193 | system(cmd) |
||
194 | res = load(TMP).strip() |
||
195 | #print(ss,ex,res) |
||
196 | if exact: |
||
197 | if res != ex: showerror(cmd, ss, ex, res) |
||
198 | assert(res == ex) |
||
199 | else: |
||
200 | if ex not in res: showerror(cmd, ss, ex, res) |
||
201 | assert(ex in res) |
||
202 | |||
203 | def test_range(): |
||
204 | t_render("""print(str(range(4))[:5])"""," |
||
205 | t_render("""print(len(range(4)))""","4") |
||
206 | t_render("""print(range(4)[0])""","0") |
||
207 | t_render("""print(range(4)[1])""","1") |
||
208 | t_render("""print(range(4)[-1])""","3") |
||
209 | |||
210 | t_render("""print(str(range(-4))[:5])"""," |
||
211 | t_render("""print(len(range(-4)))""","0") |
||
212 | |||
213 | t_render("""print(str(range(0,5,3))[:5])"""," |
||
214 | t_render("""print(len(range(0,5,3)))""","2") |
||
215 | t_render("""print(range(0,5,3)[0])""","0") |
||
216 | t_render("""print(range(0,5,3)[1])""","3") |
||
217 | t_render("""print(range(0,5,3)[-1])""","3") |
||
218 | |||
219 | t_render("""print(str(range(5,0,-3))[:5])"""," |
||
220 | t_render("""print(len(range(5,0,-3)))""","2") |
||
221 | t_render("""print(range(5,0,-3)[0])""","5") |
||
222 | t_render("""print(range(5,0,-3)[1])""","2") |
||
223 | t_render("""print(range(5,0,-3)[-1])""","2") |
||
224 | |||
225 | t_render("""print(str(range(-8,-4))[:5])"""," |
||
226 | t_render("""print(len(range(-8,-4)))""","4") |
||
227 | t_render("""print(range(-8,-4)[0])""","-8") |
||
228 | t_render("""print(range(-8,-4)[1])""","-7") |
||
229 | t_render("""print(range(-8,-4)[-1])""","-5") |
||
230 | |||
231 | t_render("""print(str(range(-4,-8,-1))[:5])"""," |
||
232 | t_render("""print(len(range(-4,-8,-1)))""","4") |
||
233 | t_render("""print(range(-4,-8,-1)[0])""","-4") |
||
234 | t_render("""print(range(-4,-8,-1)[1])""","-5") |
||
235 | t_render("""print(range(-4,-8,-1)[-1])""","-7") |
||
236 | |||
237 | t_render("""print(str(range(-4,-8))[:5])"""," |
||
238 | t_render("""print(len(range(-4,-8)))""","0") |
||
239 | |||
240 | t_render("""print(str(range(-8,-4,-1))[:5])"""," |
||
241 | t_render("""print(len(range(-8,-4,-1)))""","0") |
||
242 | |||
243 | t_render("""print(str(range(0,4,0))[:5])"""," |
||
244 | t_render("""print(len(range(0,4,0)))""","0") |
||
245 | |||
246 | |||
247 | if __name__ == '__main__': |
||
248 | t_render('print("hello world")',"hello world") |
||
249 | t_render('print(234)',"234") |
||
250 | t_render('a=3\nprint(a)',"3") |
||
251 | t_render('print(2+3)',"5") |
||
252 | t_render(""" |
||
253 | x = 2 |
||
254 | x += 3 |
||
255 | print(x) |
||
256 | """ |
||
257 | ,"5") |
||
258 | t_render(""" |
||
259 | x = "OK" |
||
260 | print(x) |
||
261 | """ |
||
262 | ,"OK") |
||
263 | t_render(""" |
||
264 | a,b = 1,2 |
||
265 | print(a+b) |
||
266 | """ |
||
267 | ,"3") |
||
268 | t_render(""" |
||
269 | x = 1 |
||
270 | if x == 1: |
||
271 | print("yes") |
||
272 | ""","yes") |
||
273 | t_render(""" |
||
274 | if 0 == 1: |
||
275 | print("X") |
||
276 | else: |
||
277 | print("OK") |
||
278 | """ |
||
279 | ,"OK") |
||
280 | |||
281 | t_render(""" |
||
282 | if 0 == 1: |
||
283 | print("X") |
||
284 | elif 1 == 2: |
||
285 | print("Y") |
||
286 | else: |
||
287 | print("OK") |
||
288 | """ |
||
289 | ,"OK") |
||
290 | |||
291 | t_render(""" |
||
292 | def test(x,y): |
||
293 | return x+y |
||
294 | r = test(3,5) |
||
295 | print(r) |
||
296 | ""","8") |
||
297 | t_render(""" |
||
298 | x = 1 |
||
299 | t = 0 |
||
300 | while x<=5: |
||
301 | t = t+x |
||
302 | x = x+1 |
||
303 | print(t) |
||
304 | ""","15") |
||
305 | t_render(""" |
||
306 | x = {} |
||
307 | x.y = "test" |
||
308 | print(x.y) |
||
309 | ""","test") |
||
310 | |||
311 | t_render(""" |
||
312 | if "a" is "a": |
||
313 | print("OK") |
||
314 | """ |
||
315 | ,"OK") |
||
316 | |||
317 | t_render(""" |
||
318 | if "a" is not "b": |
||
319 | print("OK") |
||
320 | """ |
||
321 | ,"OK") |
||
322 | |||
323 | t_render(""" |
||
324 | if None is None: |
||
325 | print("OK") |
||
326 | """ |
||
327 | ,"OK") |
||
328 | t_render(""" |
||
329 | if "x" is "x" or "y" is "y": |
||
330 | print("OK") |
||
331 | """ |
||
332 | ,"OK") |
||
333 | t_render(""" |
||
334 | x = 1 |
||
335 | while x < 3: |
||
336 | break |
||
337 | x = x + 1 |
||
338 | print(x) |
||
339 | """ |
||
340 | ,"1") |
||
341 | t_render(""" |
||
342 | x = 1 |
||
343 | n = 0 |
||
344 | while x < 10: |
||
345 | x = x + 1 |
||
346 | if n == 2: |
||
347 | continue |
||
348 | n = n + 1 |
||
349 | print(n) |
||
350 | """ |
||
351 | ,"2") |
||
352 | t_render(""" |
||
353 | def test(x): return x |
||
354 | y = test(1)*2 + test(3)*4 + test(5)*6 |
||
355 | print(y) |
||
356 | """ |
||
357 | ,"44") |
||
358 | t_render(""" |
||
359 | def test(a,b): return a+b |
||
360 | print(test(1,1)+test(1,1)) |
||
361 | """ |
||
362 | ,"4") |
||
363 | |||
364 | t_render(""" |
||
365 | def test(): print("OK") |
||
366 | x = test |
||
367 | x() |
||
368 | """ |
||
369 | ,"OK") |
||
370 | |||
371 | t_render(""" |
||
372 | x = [2,4,6] |
||
373 | print(x[1]) |
||
374 | """ |
||
375 | ,"4") |
||
376 | t_render(""" |
||
377 | def test(): print("OK") |
||
378 | x = [1,test,2] |
||
379 | x[1]() |
||
380 | """ |
||
381 | ,"OK") |
||
382 | |||
383 | |||
384 | t_render(""" |
||
385 | z = 0 |
||
386 | for x in [1,2,3]: |
||
387 | z += x |
||
388 | print(z) |
||
389 | """ |
||
390 | ,"6") |
||
391 | |||
392 | t_render(""" |
||
393 | z = 0 |
||
394 | for x in range(1,4): |
||
395 | z += x |
||
396 | print(z) |
||
397 | """ |
||
398 | ,"6") |
||
399 | |||
400 | t_render(""" |
||
401 | x = {'a':'OK'} |
||
402 | print(x.a) |
||
403 | """ |
||
404 | ,"OK") |
||
405 | |||
406 | t_render("""print("1234"[1:3])""","23") |
||
407 | t_render("""print("1234"[-3:3])""","23") |
||
408 | t_render("""print([1,2,"OK",4][-3:3][1])""","OK") |
||
409 | t_render(""" |
||
410 | n = 0 |
||
411 | for x in range(0,10,2): |
||
412 | n += 1 |
||
413 | print(n) |
||
414 | """ |
||
415 | ,"5") |
||
416 | |||
417 | t_render("""print(max(3,8,2,6))""","8") |
||
418 | t_render("""print(min(3,4,2,6))""","2") |
||
419 | t_render("""for k in {'OK':0}: print(k)""","OK") |
||
420 | |||
421 | t_render(""" |
||
422 | X = "OK" |
||
423 | def test(): print(X) |
||
424 | test() |
||
425 | """ |
||
426 | ,"OK") |
||
427 | |||
428 | t_render(""" |
||
429 | a = 4 |
||
430 | def test(z): |
||
431 | for i in range(0,a): |
||
432 | z += i |
||
433 | return z |
||
434 | print(test(1)) |
||
435 | """ |
||
436 | ,"7") |
||
437 | |||
438 | t_render(""" |
||
439 | def test(self): print(self) |
||
440 | fnc = bind(test,"OK") |
||
441 | fnc() |
||
442 | """ |
||
443 | ,"OK") |
||
444 | |||
445 | t_render(""" |
||
446 | class C: |
||
447 | def __init__(self,data): self.data = data |
||
448 | def print(self): print(self.data) |
||
449 | C("OK").print() |
||
450 | """ |
||
451 | ,"OK") |
||
452 | |||
453 | t_render(""" |
||
454 | x = [v*v for v in range(0,5)] |
||
455 | print(x[3]) |
||
456 | """ |
||
457 | ,"9") |
||
458 | t_render(""" |
||
459 | t = [[y*10+x for x in range(0,10)] for y in range(0,10)] |
||
460 | print(t[2][3]) |
||
461 | """ |
||
462 | ,"23") |
||
463 | |||
464 | t_render(""" |
||
465 | x = [1] |
||
466 | x.extend([2,3]) |
||
467 | print(x[1]) |
||
468 | """ |
||
469 | ,"2") |
||
470 | |||
471 | #t_render(""" |
||
472 | #x = {'a':3} |
||
473 | #merge(x,{'b':4}) |
||
474 | #print(x.b) |
||
475 | #""" |
||
476 | #,"4") |
||
477 | |||
478 | t_render(""" |
||
479 | x = [1,2,3] |
||
480 | y = copy(x) |
||
481 | y[0] *= 10 |
||
482 | print(x[0]+y[0]) |
||
483 | """ |
||
484 | ,"11") |
||
485 | t_render(""" |
||
486 | x = {'a':3} |
||
487 | y = copy(x) |
||
488 | y.a *= 10 |
||
489 | print(x.a+y.a) |
||
490 | """ |
||
491 | ,"33") |
||
492 | t_render(""" |
||
493 | x = {} |
||
494 | y = x['x'] |
||
495 | """ |
||
496 | ,'KeyError',0) |
||
497 | t_render(""" |
||
498 | x = [] |
||
499 | y = x[1] |
||
500 | """ |
||
501 | ,'KeyError',0) |
||
502 | t_render("""print("O"+"K")""","OK") |
||
503 | t_render("""print("-".join(["O","K"]))""","O-K") |
||
504 | t_render("""print("OK-OK".split("-")[1])""","OK") |
||
505 | t_render(""" |
||
506 | def test(*v): return max(v[2]) |
||
507 | print(test(*[1,2,"OK"])) |
||
508 | """ |
||
509 | ,"OK") |
||
510 | t_render(""" |
||
511 | def test(**v): return v['x'] |
||
512 | print(test(**{'x':'OK'})) |
||
513 | """ |
||
514 | ,"OK") |
||
515 | #t_render(""" |
||
516 | #def test(y='K',x='Z'): print(x+y) |
||
517 | #test(x='O') |
||
518 | #""" |
||
519 | #,"OK") |
||
520 | t_render(""" |
||
521 | def test(y='K',x='Z'): print(x+y) |
||
522 | test('O') |
||
523 | """ |
||
524 | ,"ZO") |
||
525 | |||
526 | #t_render(""" |
||
527 | #def test(a,b=2,*c,**d): return a+b+c[0]+d['x']+d['z'] |
||
528 | #print(test(10,3,z=50000,*[200],**{'x':4000})) |
||
529 | #""" |
||
530 | #,"54213") |
||
531 | |||
532 | t_render("""print("".join(["O"]+["K"]))""","OK") |
||
533 | t_render("""x="OK";print(x)""","OK") |
||
534 | t_render("""x = [1,2,] ; print(x[1])""","2") |
||
535 | t_render("""a,b,d = [0],0,'OK'; print(d)""","OK") |
||
536 | |||
537 | t_render(""" |
||
538 | def test(): raise |
||
539 | try: |
||
540 | test() |
||
541 | except: |
||
542 | print("OK") |
||
543 | ""","OK") |
||
544 | |||
545 | t_render("""print("OKx"[:-1])""","OK") |
||
546 | t_render("""print("xOK"[1:])""","OK") |
||
547 | t_render("""a,b = "OK"; print(a+b)""","OK") |
||
548 | |||
549 | t_render(""" |
||
550 | def test(a,b): |
||
551 | print a+b[2] |
||
552 | test(1,3) |
||
553 | ""","Exception",False) |
||
554 | |||
555 | t_render(""" |
||
556 | def test(): raise |
||
557 | test() |
||
558 | ""","Exception",False) |
||
559 | |||
560 | t_render(['OK="OK"',"import tmp1\nprint(tmp1.OK)"],"OK") |
||
561 | |||
562 | t_render(['O="O"','K="K"',"import tmp1, tmp2\nprint(tmp1.O+tmp2.K)"],"OK") |
||
563 | |||
564 | t_render(""" |
||
565 | def test(): return |
||
566 | x = 1 |
||
567 | print(test()) |
||
568 | ""","None") |
||
569 | |||
570 | t_render(""" |
||
571 | def test(): pass |
||
572 | x = 1 |
||
573 | print(test()) |
||
574 | ""","None") |
||
575 | |||
576 | t_render(""" |
||
577 | def test(): |
||
578 | global x |
||
579 | x = "OK" |
||
580 | test() |
||
581 | print(x) |
||
582 | ""","OK") |
||
583 | |||
584 | t_render(""" |
||
585 | class X: |
||
586 | pass |
||
587 | y = X() |
||
588 | print("OK") |
||
589 | ""","OK") |
||
590 | |||
591 | t_render(""" |
||
592 | class X: pass |
||
593 | def test(): y = X() |
||
594 | test() |
||
595 | print("OK") |
||
596 | ""","OK") |
||
597 | |||
598 | t_render(["class X: pass\ndef test(): y = X()","import tmp1\ntmp1.test();print('OK')"],"OK") |
||
599 | |||
600 | t_render("print(len([1,2,3]))","3") |
||
601 | |||
602 | t_render('if not "?" in "xyz": print("OK")',"OK") |
||
603 | |||
604 | t_render('print({1:"OK"}[1])',"OK") |
||
605 | |||
606 | t_render('print(len("\0"))',"1") |
||
607 | |||
608 | t_render('print(1 in {1:2})',"1") |
||
609 | |||
610 | t_render('x = {1:2}; del x[1]; print(len(x))','0') |
||
611 | |||
612 | t_render(""" |
||
613 | def test(t): |
||
614 | t = "O"+t |
||
615 | print(t) |
||
616 | test("K") |
||
617 | ""","OK") |
||
618 | |||
619 | t_render("""print([1,2,3].index(3))""","2") |
||
620 | t_render("""print("1,2,3".split(",").index("3"))""","2") |
||
621 | |||
622 | t_render("""v = [3,2,1]; v.sort(); print(v[0])""","1") |
||
623 | |||
624 | t_render("""print(abs(-5))""","5") |
||
625 | t_render("""print(int(1.234))""","1") |
||
626 | |||
627 | t_render("print(int(round(1.5)))","2") |
||
628 | t_render("print(ord('X'))","88") |
||
629 | t_render("print(ord(chr(128)))","128") |
||
630 | #t_render("print(fsize('LICENSE.txt'))","181") |
||
631 | t_render("print(int('ff',16))","255") |
||
632 | t_render(""" |
||
633 | def test(x,y): print(x); return y |
||
634 | test('a',1) or test('b',1) and test('c',0) |
||
635 | ""","a") |
||
636 | |||
637 | t_render("def test(): print('OK')\n{'__call__':test}()","OK") |
||
638 | |||
639 | t_render(""" |
||
640 | class A: |
||
641 | def __init__(self): |
||
642 | self.a = 'O' |
||
643 | self.b = 'x' |
||
644 | def test(self): |
||
645 | print("KO") |
||
646 | class B(A): |
||
647 | def __init__(self): |
||
648 | A.__init__(self) |
||
649 | self.b = 'K' |
||
650 | def test(self): |
||
651 | print(self.a+self.b) |
||
652 | B().test() |
||
653 | ""","OK") |
||
654 | |||
655 | t_render(""" |
||
656 | class A: |
||
657 | def test(self): |
||
658 | print(self) |
||
659 | A.test("OK") |
||
660 | ""","OK") |
||
661 | |||
662 | t_render(""" |
||
663 | def test(): |
||
664 | def fnc(): |
||
665 | print("OK") |
||
666 | fnc() |
||
667 | test() |
||
668 | ""","OK") |
||
669 | |||
670 | t_render("""print("aa..bbb...ccc".replace("..","X"))""","aaXbbbX.ccc") |
||
671 | t_render("""print("..bbb..".replace("..","X"))""","XbbbX") |
||
672 | t_render("""print("234".replace("\r\n","\n"))""","234") |
||
673 | t_render("""print("a\0b".replace("\0","X"))""","aXb") |
||
674 | t_render("""x = "a\0b"; x = x.replace("\0","c"); print(x)""","acb") |
||
675 | |||
676 | t_render("""print(0xff)""","255") |
||
677 | |||
678 | t_render("""x=(1,3);print({x:'OK'}[x])""","OK") |
||
679 | t_render("""x=(1,3);y=(1,3);print({x:'OK'}[y])""","OK") |
||
680 | t_render("""print({(1,3):'OK'}[(1,3)])""","OK") |
||
681 | t_render("def test(): test()\ntest()","Exception",0) |
||
682 | t_render("x = []; x.append(x); print(x |
||
683 | t_render("x = []; x.append(x); print({x:'OK'}[x])","OK") |
||
684 | #t_render("print(float(str(4294967296))==float('4294967296'))","1") |
||
685 | t_render("print(2**3)","8") |
||
686 | #t_render("x = 'OK',\nprint(x[0])","OK") |
||
687 | |||
688 | test_range() |
||
689 | |||
690 | t_render(['v="OK"',"from tmp1 import *\nprint(v)"],"OK") |
||
691 | t_render(['v="OK"',"from tmp1 import v\nprint(v)"],"OK") |
||
692 | t_render(['x="X";y="K"',"x = 'O'\nfrom tmp1 import y\nprint(x+y)"],"OK") |
||
693 | |||
694 | t_render(""" |
||
695 | def test(**e): |
||
696 | print(e['x']) |
||
697 | test(x='OK') |
||
698 | ""","OK") |
||
699 | |||
700 | # test register allocator |
||
701 | s = "def f():pass\n"+("f()\n"*256)+"print('OK')" |
||
702 | t_render(s,"OK") |
||
703 | |||
704 | t_render("print(2**3)","8") |
||
705 | t_render("print(2*3**2)", "18", False) |
||
706 | |||
707 | |||
708 | t_render(""" |
||
709 | def test(**v): return 'OK' |
||
710 | print(test()) |
||
711 | """ |
||
712 | ,"OK") |
||
713 | t_render(""" |
||
714 | def test(**v): |
||
715 | v['x'] = 'OK' |
||
716 | return v |
||
717 | print(test()['x']) |
||
718 | """ |
||
719 | ,"OK") |
||
720 | |||
721 | ################################################################################ |
||
722 | |||
723 | def t_boot(ss,ex,exact=True): |
||
724 | if not istype(ss,'list'): ss =[ss] |
||
725 | n = 1 |
||
726 | for s in ss: |
||
727 | fname = 'tmp'+str(n)+'.tpc' |
||
728 | system_rm(fname) |
||
729 | fname = 'tmp'+str(n)+'.py' |
||
730 | save(fname,s) |
||
731 | n += 1 |
||
732 | system_rm('tmp.txt') |
||
733 | system(TINYPY+fname+' > tmp.txt') |
||
734 | res = load(TMP).strip() |
||
735 | #print(ss,ex,res) |
||
736 | if exact: assert(res == ex) |
||
737 | else: assert(ex in res) |
||
738 | |||
739 | is_boot = False |
||
740 | try: |
||
741 | assert(is_tinypy == True) |
||
742 | x = compile('x=3','') |
||
743 | is_boot = True |
||
744 | except: |
||
745 | pass |
||
746 | |||
747 | if is_boot == True and __name__ == '__main__': |
||
748 | print("# t_boot") |
||
749 | t_boot(["def test(): print('OK')","import tmp1; tmp1.test()"],"OK") |
||
750 |