Package up the RPython bits into the target function.
Chris Pressey
10 years ago
6 | 6 | # |
7 | 7 | |
8 | 8 | import sys |
9 | import os | |
10 | 9 | |
11 | 10 | |
12 | 11 | def input(): |
380 | 379 | p.run() |
381 | 380 | |
382 | 381 | |
383 | def rpython_input(): | |
384 | s = os.read(1, 1) | |
385 | if not s: | |
386 | return 0 | |
387 | return ord(s[0]) | |
388 | ||
389 | ||
390 | def rpython_output(code): | |
391 | os.write(0, unichr(code).encode('utf-8')) | |
392 | ||
393 | ||
394 | def rpython_load(filename): | |
395 | fd = os.open(filename, os.O_RDONLY, 0644) | |
396 | text = '' | |
397 | chunk = os.read(fd, 1024) | |
398 | text += chunk | |
399 | while len(chunk) == 1024: | |
382 | def target(*args): | |
383 | global input, output | |
384 | ||
385 | import os | |
386 | ||
387 | def rpython_input(): | |
388 | s = os.read(1, 1) | |
389 | if not s: | |
390 | return 0 | |
391 | return ord(s[0]) | |
392 | ||
393 | ||
394 | def rpython_output(code): | |
395 | os.write(0, unichr(code).encode('utf-8')) | |
396 | ||
397 | ||
398 | def rpython_load(filename): | |
399 | fd = os.open(filename, os.O_RDONLY, 0644) | |
400 | text = '' | |
400 | 401 | chunk = os.read(fd, 1024) |
401 | 402 | text += chunk |
402 | os.close(fd) | |
403 | return text | |
404 | ||
405 | ||
406 | def rpython_main(argv): | |
407 | p = Processor() | |
408 | program = rpython_load(argv[1]) | |
409 | p.load_string(program) | |
410 | p.run() | |
411 | return 0 | |
412 | ||
413 | ||
414 | def target(*args): | |
415 | global input, output | |
403 | while len(chunk) == 1024: | |
404 | chunk = os.read(fd, 1024) | |
405 | text += chunk | |
406 | os.close(fd) | |
407 | return text | |
408 | ||
409 | ||
410 | def rpython_main(argv): | |
411 | p = Processor() | |
412 | program = rpython_load(argv[1]) | |
413 | p.load_string(program) | |
414 | p.run() | |
415 | return 0 | |
416 | ||
416 | 417 | input = rpython_input |
417 | 418 | output = rpython_output |
418 | 419 | return rpython_main, None |