But it can run simple programs without crashing.
Chris Pressey
8 years ago
|
0 |
cons(:hi, cons(:there, :nil))
|
|
0 |
def double(#)
|
|
1 |
cons(#, #)
|
|
2 |
def quadruple(#)
|
|
3 |
double(double(#))
|
|
4 |
|
|
5 |
quadruple(:meow)
|
338 | 338 |
|
339 | 339 |
|
340 | 340 |
class SExpr(object):
|
341 | |
pass
|
|
341 |
def __repr__(self):
|
|
342 |
return "SExpr()"
|
342 | 343 |
|
343 | 344 |
|
344 | 345 |
class Atom(SExpr):
|
|
349 | 350 |
return self.text
|
350 | 351 |
|
351 | 352 |
def __repr__(self):
|
352 | |
return "Atom(%r)" % self.text
|
|
353 |
return "Atom('%s')" % self.text
|
353 | 354 |
|
354 | 355 |
def __eq__(self, other):
|
355 | 356 |
return isinstance(other, Atom) and self.text == other.text
|
|
364 | 365 |
return "(%s %s)" % (self.head, self.tail)
|
365 | 366 |
|
366 | 367 |
def __repr__(self):
|
367 | |
return "Cons(%r, %r)" % (self.head, self.tail)
|
|
368 |
return "Cons(%s, %s)" % (self.head.__repr__(), self.tail.__repr__())
|
368 | 369 |
|
369 | 370 |
def __eq__(self, other):
|
370 | 371 |
return False # isinstance(other, Cons) and self.head == other.head and self.tail == other.tail
|
|
500 | 501 |
os.close(fd)
|
501 | 502 |
return text
|
502 | 503 |
|
503 | |
def rpython_input():
|
504 | |
accum = ''
|
505 | |
done = False
|
506 | |
while not done:
|
507 | |
s = os.read(1, 1)
|
508 | |
if not s:
|
509 | |
done = True
|
510 | |
accum += s
|
511 | |
return accum
|
512 | |
|
513 | 504 |
def rpython_main(argv):
|
514 | |
#inp = rpython_input()
|
515 | |
#if not inp:
|
516 | |
# inp = 'ifeq'
|
517 | 505 |
text = rpython_load(argv[1])
|
518 | 506 |
p = Parser(text)
|
519 | 507 |
prog = p.program()
|
520 | |
print "%r" % prog
|
521 | 508 |
ev = Evaluator(prog)
|
522 | 509 |
result = ev.eval(prog)
|
523 | |
print "%r" % result
|
|
510 |
print result.__repr__()
|
524 | 511 |
return 0
|
525 | 512 |
|
526 | 513 |
return rpython_main, None
|