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"