From 18cb7203d3ce92a5212713c97e4faddf11e7bf1a Mon Sep 17 00:00:00 2001 From: "E. Anders Lannerback" Date: Fri, 19 Apr 2013 08:40:03 +0200 Subject: [PATCH] Added global. Not sure if this is a good idea ... (global foo) => global foo --- hy/compiler.py | 9 +++++++++ tests/compilers/test_ast.py | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/hy/compiler.py b/hy/compiler.py index 362e9c8..bd4d13a 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -468,6 +468,15 @@ class HyASTCompiler(object): lineno=e.start_line, col_offset=e.start_column) + @builds("global") + @checkargs(1) + def compile_global_expression(self,expr): + expr.pop(0) # global + e = expr.pop(0) + return ast.Global(names=[ast_str(e)], + lineno=e.start_line, + col_offset=e.start_column) + @builds("lambda") @checkargs(min=2) def compile_lambda_expression(self, expr): diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 3b34e14..70459b5 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -185,6 +185,14 @@ def test_ast_bad_assert(): cant_compile("(assert)") cant_compile("(assert 1 2)") +def test_ast_good_global(): + "Make sure AST can compile valid global" + hy_compile(tokenize("(global a)")) + +def test_ast_bad_global(): + "Make sure AST can't compile invalid global" + cant_compile("(global)") + cant_compile("(global foo bar)") def test_ast_good_lambda(): "Make sure AST can compile valid lambda"