From e47bac1f9695837bd2022825e9f2ed49d5cde480 Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Wed, 5 Jun 2013 12:19:06 +0200 Subject: [PATCH 1/3] Permit macros to return constants --- hy/macros.py | 21 ++++++++++++++++++++- tests/native_tests/native_macros.hy | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/hy/macros.py b/hy/macros.py index 231d66e..c4652d4 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -21,6 +21,11 @@ from hy.models.expression import HyExpression from hy.models.string import HyString from hy.models.list import HyList +from hy.models.integer import HyInteger +from hy.models.float import HyFloat +from hy.models.complex import HyComplex +from hy.models.dict import HyDict +from hy.util import str_type from collections import defaultdict @@ -44,6 +49,20 @@ def require(source_module_name, target_module_name): refs[name] = macro +def _wrap_value(x): + wrapper = _wrappers.get(type(x)) + if wrapper is None: + return x + else: + return wrapper(x) + +_wrappers = {int: HyInteger, + float: HyFloat, + complex: HyComplex, + str_type: HyString, + dict: lambda d: HyDict(_wrap_value(x) for x in sum(d.items(), ())), + list: lambda l: HyList(_wrap_value(x) for x in l)} + def process(tree, module_name): if isinstance(tree, HyExpression): fn = tree[0] @@ -57,7 +76,7 @@ def process(tree, module_name): if m is None: m = _hy_macros[None].get(fn) if m is not None: - obj = m(*ntree[1:]) + obj = _wrap_value(m(*ntree[1:])) obj.replace(tree) return obj diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index dcb3904..4b3af9f 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -8,3 +8,24 @@ (setv x []) (rev (.append x 1) (.append x 2) (.append x 3)) (assert (= x [3 2 1]))) + + +; Macros returning constants + +(defmacro an-int [] 42) +(assert (= (an-int) 42)) + +(defmacro a-float [] 42.) +(assert (= (a-float) 42.)) + +(defmacro a-complex [] 42j) +(assert (= (a-complex) 42j)) + +(defmacro a-string [] "foo") +(assert (= (a-string) "foo")) + +(defmacro a-list [] [1 2]) +(assert (= (a-list) [1 2])) + +(defmacro a-dict [] {1 2}) +(assert (= (a-dict) {1 2})) From 12abef5ed5273cd2372dcd2358cdce23c31dc3ae Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Fri, 7 Jun 2013 16:35:28 +0200 Subject: [PATCH 2/3] Forgot boolean constants.. --- hy/macros.py | 2 ++ tests/native_tests/native_macros.hy | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/hy/macros.py b/hy/macros.py index c4652d4..5149cc7 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -20,6 +20,7 @@ from hy.models.expression import HyExpression from hy.models.string import HyString +from hy.models.symbol import HySymbol from hy.models.list import HyList from hy.models.integer import HyInteger from hy.models.float import HyFloat @@ -57,6 +58,7 @@ def _wrap_value(x): return wrapper(x) _wrappers = {int: HyInteger, + bool: lambda x: HySymbol("True") if x else HySymbol("False"), float: HyFloat, complex: HyComplex, str_type: HyString, diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index 4b3af9f..276685f 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -15,6 +15,11 @@ (defmacro an-int [] 42) (assert (= (an-int) 42)) +(defmacro a-true [] True) +(assert (= (a-true) True)) +(defmacro a-false [] False) +(assert (= (a-false) False)) + (defmacro a-float [] 42.) (assert (= (a-float) 42.)) From d15aa31a32ce47e993ec4a2ee1ece17711579a3d Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sat, 8 Jun 2013 20:10:27 -0400 Subject: [PATCH 3/3] style fixes --- hy/macros.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/hy/macros.py b/hy/macros.py index 5149cc7..d3530fd 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -57,20 +57,25 @@ def _wrap_value(x): else: return wrapper(x) -_wrappers = {int: HyInteger, - bool: lambda x: HySymbol("True") if x else HySymbol("False"), - float: HyFloat, - complex: HyComplex, - str_type: HyString, - dict: lambda d: HyDict(_wrap_value(x) for x in sum(d.items(), ())), - list: lambda l: HyList(_wrap_value(x) for x in l)} +_wrappers = { + int: HyInteger, + bool: lambda x: HySymbol("True") if x else HySymbol("False"), + float: HyFloat, + complex: HyComplex, + str_type: HyString, + dict: lambda d: HyDict(_wrap_value(x) for x in sum(d.items(), ())), + list: lambda l: HyList(_wrap_value(x) for x in l) +} + def process(tree, module_name): if isinstance(tree, HyExpression): fn = tree[0] if fn in ("quote", "quasiquote"): return tree - ntree = HyExpression([fn] + [process(x, module_name) for x in tree[1:]]) + ntree = HyExpression( + [fn] + [process(x, module_name) for x in tree[1:]] + ) ntree.replace(tree) if isinstance(fn, HyString):