From 8d8dd621682b95786ba5925691bdf0ebc7c36fb5 Mon Sep 17 00:00:00 2001 From: John Jacobsen Date: Sun, 21 Apr 2013 19:08:30 -0500 Subject: [PATCH] Move take and drop from AST impl. in compiler.py to macros in core/bootstrap.py --- hy/compiler.py | 30 ------------------------------ hy/core/bootstrap.py | 21 +++++++++++++++++++++ tests/compilers/test_ast.py | 11 ----------- 3 files changed, 21 insertions(+), 41 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index d3c8a61..223242e 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -673,36 +673,6 @@ class HyASTCompiler(object): step=None), ctx=ast.Load()) - @builds("take") - @checkargs(2) - def compile_take_expression(self, expr): - expr.pop(0) - n = self.compile(expr.pop(0)) - seq = self.compile(expr.pop(0)) - return ast.Subscript( - lineno=expr.start_line, - col_offset=expr.start_column, - value=seq, - slice=ast.Slice(lower=None, - upper=n, - step=None), - ctx=ast.Load()) - - @builds("drop") - @checkargs(2) - def compile_drop_expression(self, expr): - expr.pop(0) - n = self.compile(expr.pop(0)) - seq = self.compile(expr.pop(0)) - return ast.Subscript( - lineno=expr.start_line, - col_offset=expr.start_column, - value=seq, - slice=ast.Slice(lower=n, - upper=None, - step=None), - ctx=ast.Load()) - @builds("assoc") @checkargs(3) def compile_assoc_expression(self, expr): diff --git a/hy/core/bootstrap.py b/hy/core/bootstrap.py index 4c80a9f..0337a5a 100644 --- a/hy/core/bootstrap.py +++ b/hy/core/bootstrap.py @@ -149,3 +149,24 @@ def let_macro(tree): expr.append(stmt) return HyExpression([expr]) + + +@macro("take") +def take_macro(tree): + tree.pop(0) # "take" + n = tree.pop(0) + ret = tree.pop(0) + return HyExpression([HySymbol('slice'), + ret, + HyInteger(0), + HyInteger(n)]) + + +@macro("drop") +def drop_macro(tree): + tree.pop(0) # "drop" + n = tree.pop(0) + ret = tree.pop(0) + return HyExpression([HySymbol('slice'), + ret, + HyInteger(n)]) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 9ceedf6..4c51e0b 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -242,22 +242,11 @@ def test_ast_good_take(): hy_compile(tokenize("(take 1 [2 3])")) -def test_ast_bad_take(): - "Make sure AST chokes on bad 'take'" - cant_compile("(take)") - cant_compile("(take 2)") - def test_ast_good_drop(): "Make sure AST can compile valid 'drop'" hy_compile(tokenize("(drop 1 [2 3])")) -def test_ast_bad_drop(): - "Make sure AST chokes on bad 'drop'" - cant_compile("(drop)") - cant_compile("(drop 2)") - - def test_ast_good_assoc(): "Make sure AST can compile valid assoc" hy_compile(tokenize("(assoc x y z)"))