mirror of
https://github.com/lloeki/wasp.git
synced 2025-12-06 10:44:39 +01:00
basic ast
This commit is contained in:
parent
5819dba46a
commit
daa57a1f47
3 changed files with 81 additions and 2 deletions
6
repl.py
6
repl.py
|
|
@ -2,7 +2,9 @@ import wasp.parser as parser
|
||||||
|
|
||||||
line = raw_input(">> ")
|
line = raw_input(">> ")
|
||||||
while line != "":
|
while line != "":
|
||||||
tree = parser.parse(line)
|
ptree = parser.parse(line)
|
||||||
print "^^", tree
|
print " ^ %s" % ptree
|
||||||
|
ast = ptree.ast()
|
||||||
|
print " ‡ %r" % ast
|
||||||
#tree.eval()
|
#tree.eval()
|
||||||
line = raw_input(">> ")
|
line = raw_input(">> ")
|
||||||
|
|
|
||||||
52
wasp/ast.py
Normal file
52
wasp/ast.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
class Node(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class List(Node):
|
||||||
|
def __init__(self, car, cdr=None):
|
||||||
|
self.car = car
|
||||||
|
self.cdr = cdr
|
||||||
|
|
||||||
|
def eval(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<List %r %r>" % (self.car, self.cdr)
|
||||||
|
|
||||||
|
|
||||||
|
class Atom(Node):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Operator(Node):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Number(Atom):
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def eval(self):
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Number %s>" % self.value
|
||||||
|
|
||||||
|
|
||||||
|
class Symbol(Atom):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Quote(Node):
|
||||||
|
def __init__(self, sexpr):
|
||||||
|
self.sexpr = sexpr
|
||||||
|
|
||||||
|
def eval(self):
|
||||||
|
return self.sexpr.eval()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Quote %r>" % self.sexpr
|
||||||
|
|
||||||
|
|
||||||
|
class Lambda(Node):
|
||||||
|
pass
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
from rply.token import BaseBox
|
from rply.token import BaseBox
|
||||||
|
from wasp import ast
|
||||||
|
|
||||||
|
|
||||||
class Quote(BaseBox):
|
class Quote(BaseBox):
|
||||||
|
|
@ -11,6 +12,9 @@ class Quote(BaseBox):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "'%s" % self.sexpr
|
return "'%s" % self.sexpr
|
||||||
|
|
||||||
|
def ast(self):
|
||||||
|
return ast.Quote(self.sexpr.ast())
|
||||||
|
|
||||||
|
|
||||||
class Pair(BaseBox):
|
class Pair(BaseBox):
|
||||||
def __init__(self, x, y=None):
|
def __init__(self, x, y=None):
|
||||||
|
|
@ -24,6 +28,24 @@ class Pair(BaseBox):
|
||||||
y = self.y
|
y = self.y
|
||||||
return "(%s . %s)" % (self.x, 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):
|
class Atom(BaseBox):
|
||||||
def __init__(self, atom):
|
def __init__(self, atom):
|
||||||
|
|
@ -34,3 +56,6 @@ class Atom(BaseBox):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s" % (self.atom)
|
return "%s" % (self.atom)
|
||||||
|
|
||||||
|
def ast(self):
|
||||||
|
return ast.Number(int(self.atom))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue