Move take and drop from AST impl. in compiler.py to macros in core/bootstrap.py

This commit is contained in:
John Jacobsen 2013-04-21 19:08:30 -05:00
parent c65df143b9
commit 8d8dd62168
3 changed files with 21 additions and 41 deletions

View File

@ -673,36 +673,6 @@ class HyASTCompiler(object):
step=None), step=None),
ctx=ast.Load()) 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") @builds("assoc")
@checkargs(3) @checkargs(3)
def compile_assoc_expression(self, expr): def compile_assoc_expression(self, expr):

View File

@ -149,3 +149,24 @@ def let_macro(tree):
expr.append(stmt) expr.append(stmt)
return HyExpression([expr]) 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)])

View File

@ -242,22 +242,11 @@ def test_ast_good_take():
hy_compile(tokenize("(take 1 [2 3])")) 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(): def test_ast_good_drop():
"Make sure AST can compile valid 'drop'" "Make sure AST can compile valid 'drop'"
hy_compile(tokenize("(drop 1 [2 3])")) 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(): def test_ast_good_assoc():
"Make sure AST can compile valid assoc" "Make sure AST can compile valid assoc"
hy_compile(tokenize("(assoc x y z)")) hy_compile(tokenize("(assoc x y z)"))