fixing up the tests

This commit is contained in:
Paul Tagliamonte 2013-03-08 18:46:51 -05:00
parent 67b803b99a
commit abf63fca12
5 changed files with 32 additions and 10 deletions

View File

@ -4,7 +4,7 @@ from hy.lex import tokenize
from hy.macros import process
from hy.compiler import hy_compile
import hy.core.bootstrap # language bits.
import hy.core.bootstrap # NOQA
import imp

View File

@ -20,7 +20,6 @@
from hy.models.expression import HyExpression
from hy.models.list import HyList
from hy.models import HyObject
_hy_macros = {}
@ -32,6 +31,26 @@ def macro(name):
return _
def process(tree):
if isinstance(tree, HyExpression):
fn = tree[0]
ntree = HyExpression([fn] + [process(x) for x in tree[1:]])
if fn in _hy_macros:
m = _hy_macros[fn]
obj = m(ntree)
obj.replace(tree)
return obj
ntree.replace(tree)
return ntree
if isinstance(tree, HyList):
obj = HyList([process(x) for x in tree])
obj.replace(tree)
return obj
if isinstance(tree, list):
return [process(x) for x in tree]
return tree

View File

@ -25,4 +25,3 @@ class HyExpression(HyList):
"""
Hy S-Expression. Basically just a list.
"""
pass

View File

@ -25,4 +25,9 @@ class HyList(HyObject, list):
"""
Hy List. Basically just a list.
"""
pass
def replace(self, other):
for x in self:
x.replace(other)
HyObject.replace(self, other)

View File

@ -1,5 +1,6 @@
from hy.macros import macro, process
from hy.lex import tokenize
from hy.models.expression import HyExpression
from hy.models.string import HyString
@ -15,17 +16,14 @@ def tmac(tree):
def test_preprocessor_simple():
""" Test basic macro expantion """
obj = process(HyExpression(["test", "one", "two"]))
obj = process(tokenize('(test "one" "two")')[0])
assert obj == HyList(["one", "two"])
assert type(obj) == HyList
def test_preprocessor_expression():
""" Test inner macro expantion """
obj = process(HyExpression([HySymbol("test"),
HyExpression([HySymbol("test"),
HyString("one"),
HyString("two")])]))
obj = process(tokenize('(test (test "one" "two"))')[0])
assert type(obj) == HyList
assert type(obj[0]) == HyList
@ -33,4 +31,5 @@ def test_preprocessor_expression():
assert obj[0] == HyList([HyString("one"), HyString("two")])
obj = HyList([HyString("one"), HyString("two")])
obj = tokenize('(shill ["one" "two"])')[0][1]
assert obj == process(obj)