From abf63fca122feed262e925b262cea27d31c50018 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Fri, 8 Mar 2013 18:46:51 -0500 Subject: [PATCH] fixing up the tests --- hy/importer.py | 2 +- hy/macros.py | 23 +++++++++++++++++++++-- hy/models/expression.py | 1 - hy/models/list.py | 7 ++++++- tests/macros/test_macro_processor.py | 9 ++++----- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/hy/importer.py b/hy/importer.py index 43dcdd3..d1b0350 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -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 diff --git a/hy/macros.py b/hy/macros.py index da1e225..e037dfc 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -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 diff --git a/hy/models/expression.py b/hy/models/expression.py index 7ccac91..6639314 100644 --- a/hy/models/expression.py +++ b/hy/models/expression.py @@ -25,4 +25,3 @@ class HyExpression(HyList): """ Hy S-Expression. Basically just a list. """ - pass diff --git a/hy/models/list.py b/hy/models/list.py index c63e606..ae86c72 100644 --- a/hy/models/list.py +++ b/hy/models/list.py @@ -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) diff --git a/tests/macros/test_macro_processor.py b/tests/macros/test_macro_processor.py index f7416ae..dd457d3 100644 --- a/tests/macros/test_macro_processor.py +++ b/tests/macros/test_macro_processor.py @@ -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)