diff --git a/hy/compiler.py b/hy/compiler.py index 2022685..4756ad7 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -309,6 +309,33 @@ class HyASTCompiler(object): fn.decorator_list = [self.compile(x) for x in expr] return fn + @builds("with") + def compile_with_expression(self, expr): + expr.pop(0) # with + return ast.With(context_expr=self.compile(expr.pop(0)), + lineno=expr.start_line, + col_offset=expr.start_column, + optional_vars=None, + body=self._mangle_branch([ + self.compile(x) for x in expr])) + + @builds("with_as") + def compile_with_as_expression(self, expr): + expr.pop(0) # with-as + ctx = self.compile(expr.pop(0)) + thing = self.compile(expr.pop(0)) + thing.ctx = ast.Store() + + # Module(body=[With(items=[withitem(context_expr=Name(id='foo', ctx=Load()), optional_vars=Name(id='e', ctx=Store()))], body=[Pass()])]) + # Module(body=[With(context_expr=Name(id='foo', ctx=Load()), optional_vars=Name(id='e', ctx=Store()), body=[Pass()])]) + + return ast.With(context_expr=ctx, + lineno=expr.start_line, + col_offset=expr.start_column, + optional_vars=thing, + body=self._mangle_branch([ + self.compile(x) for x in expr])) + @builds("kwapply") def compile_kwapply_expression(self, expr): expr.pop(0) # kwapply diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index ab7495e..915a4ff 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -194,3 +194,11 @@ (defn test-importas [] "NATIVE: test import as" (assert (!= (len systest.path) 0))) + + +(defn test-context [] + "NATIVE: test with" + (with (open "README.md" "r") + (pass)) + (with-as (open "README.md" "r") fd + (pass)))