basic ast

This commit is contained in:
Loic Nageleisen 2014-01-24 23:11:18 +01:00
parent 5819dba46a
commit daa57a1f47
3 changed files with 81 additions and 2 deletions

View file

@ -1,4 +1,5 @@
from rply.token import BaseBox
from wasp import ast
class Quote(BaseBox):
@ -11,6 +12,9 @@ class Quote(BaseBox):
def __str__(self):
return "'%s" % self.sexpr
def ast(self):
return ast.Quote(self.sexpr.ast())
class Pair(BaseBox):
def __init__(self, x, y=None):
@ -24,6 +28,24 @@ class Pair(BaseBox):
y = self.y
return "(%s . %s)" % (self.x, y)
def r_cdr(self, cdr):
if self.y is None:
return (self.x.ast(), )
else:
return cdr + (self.x.ast(), ) + self.y.r_cdr(cdr)
def ast(self):
car = self.x.ast()
if self.y is None:
cdr = None
elif type(self.y) is Pair:
cdr = list(self.y.r_cdr(()))
else:
cdr = self.y.ast()
return ast.List(car, cdr)
class Atom(BaseBox):
def __init__(self, atom):
@ -34,3 +56,6 @@ class Atom(BaseBox):
def __str__(self):
return "%s" % (self.atom)
def ast(self):
return ast.Number(int(self.atom))