Rebuilding.

This commit is contained in:
Paul R. Tagliamonte 2013-02-28 22:37:23 -05:00
parent 84453e75d2
commit 5c6d98a24a
7 changed files with 46 additions and 3 deletions

View File

@ -4,4 +4,4 @@ from hy.lex.states import Idle
def tokenize(buf):
machine = Machine(Idle, 0, 0)
machine.process(buf)
print machine.nodes
return machine.nodes

View File

@ -1,5 +1,8 @@
from hy.models.expression import HyExpression
from hy.models.symbol import HySymbol
from hy.errors import HyError
WHITESPACE = [" ", "\t", "\n", "\r"]
@ -7,6 +10,10 @@ class LexException(HyError):
pass
def _resolve_atom(obj):
return HySymbol(obj)
class State(object):
__slots__ = ("machine",)
@ -26,12 +33,12 @@ class Expression(State):
self.buf = ""
def commit(self):
self.nodes.append(self.buf)
self.nodes.append(_resolve_atom(self.buf))
self.buf = ""
def exit(self):
self.commit()
self.machine.nodes.append(self.nodes)
self.machine.nodes.append(HyExpression(self.nodes))
def process(self, char):
if char == ")":

4
hy/models/__init__.py Normal file
View File

@ -0,0 +1,4 @@
class HyObject(object):
pass

5
hy/models/expression.py Normal file
View File

@ -0,0 +1,5 @@
from hy.models import HyObject
class HyExpression(HyObject, list):
pass

14
hy/models/string.py Normal file
View File

@ -0,0 +1,14 @@
from hy.models import HyObject
import sys
if sys.version_info[0] >= 3:
_str_type = str
else:
_str_type = unicode
class HyString(HyObject, _str_type):
def __new__(cls, value):
obj = _str_type.__new__(cls, value)
return obj

10
hy/models/symbol.py Normal file
View File

@ -0,0 +1,10 @@
from hy.models.string import HyString
class HySymbol(HyString):
def __init__(self, string):
self += string
def eval(self, lns, *args, **kwargs):
obj = self.lookup(lns, self)
return obj

View File

@ -1,5 +1,8 @@
from hy.lex import tokenize
from hy.models.expression import HyExpression
from hy.models.symbol import HySymbol
def test_lex_expression():
objs = tokenize("(foo bar)")
assert objs == [HyExpression([HySymbol("foo"), HySymbol("bar")])]