mirror of
https://github.com/lloeki/wasp.git
synced 2025-12-06 10:44:39 +01:00
int, float, symbol and string AST nodes
This commit is contained in:
parent
ba160588a0
commit
5bb94d2dda
2 changed files with 40 additions and 3 deletions
30
wasp/ast.py
30
wasp/ast.py
|
|
@ -30,11 +30,37 @@ class Number(Atom):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Number %s>" % self.value
|
return "<%s %s>" % (type(self).__name__, self.value)
|
||||||
|
|
||||||
|
|
||||||
|
class Integer(Number):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Float(Number):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Symbol(Atom):
|
class Symbol(Atom):
|
||||||
pass
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def eval(self):
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Symbol %s>" % self.value
|
||||||
|
|
||||||
|
|
||||||
|
class String(Atom):
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def eval(self):
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<String %s>" % self.value
|
||||||
|
|
||||||
|
|
||||||
class Quote(Node):
|
class Quote(Node):
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import re
|
||||||
from rply.token import BaseBox
|
from rply.token import BaseBox
|
||||||
from wasp import ast
|
from wasp import ast
|
||||||
|
|
||||||
|
|
@ -58,4 +59,14 @@ class Atom(BaseBox):
|
||||||
return "%s" % (self.atom)
|
return "%s" % (self.atom)
|
||||||
|
|
||||||
def ast(self):
|
def ast(self):
|
||||||
return ast.Number(int(self.atom))
|
if self.atom[0] == '"':
|
||||||
|
if self.atom[-1] == '"':
|
||||||
|
return ast.String(self.atom)
|
||||||
|
else:
|
||||||
|
raise ValueError("missing \"")
|
||||||
|
elif re.match(r'^\d+$', self.atom):
|
||||||
|
return ast.Integer(int(self.atom))
|
||||||
|
elif re.match(r'^\d+\.(\d+)$', self.atom):
|
||||||
|
return ast.Float(float(self.atom))
|
||||||
|
else:
|
||||||
|
return ast.Symbol(self.atom)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue