Fixing (with)'s syntax to be more hyish.

This commit is contained in:
Paul R. Tagliamonte 2013-04-07 18:35:36 -04:00
parent 4debdf63ea
commit 4db9446197
4 changed files with 26 additions and 7 deletions

3
NEWS
View File

@ -11,6 +11,9 @@ Changes from Hy 0.9.4
(catch [Exception] BODY)
(catch [e Exception] BODY)
(catch [e [Exception1 Exception2]] BODY)
* With's syntax was fixed to match the rest of the code. It's now: (PT)
(with [name context-managed-fn] BODY)
(with [context-managed-fn] BODY)
[ Language Changes ]

View File

@ -456,12 +456,21 @@ class HyASTCompiler(object):
fn.decorator_list = [self.compile(x) for x in expr]
return fn
@builds("with_as")
@builds("with")
@checkargs(min=2)
def compile_with_as_expression(self, expr):
expr.pop(0) # with-as
ctx = self.compile(expr.pop(0))
thing = self._storeize(self.compile(expr.pop(0)))
def compile_with_expression(self, expr):
expr.pop(0) # with
args = expr.pop(0)
if len(args) > 2 or len(args) < 1:
raise TypeError("with needs [arg (expr)] or [(expr)]")
args.reverse()
ctx = self.compile(args.pop(0))
thing = None
if args != []:
thing = self._storeize(self.compile(args.pop(0)))
ret = ast.With(context_expr=ctx,
lineno=expr.start_line,

View File

@ -252,6 +252,13 @@ def test_ast_bad_assoc():
cant_compile("(assoc 1 2 3 4)")
def test_ast_bad_with():
"Make sure AST can't compile invalid with"
cant_compile("(with)")
cant_compile("(with [])")
cant_compile("(with [] (pass))")
def test_ast_valid_while():
"Make sure AST can't compile invalid while"
hy_compile(tokenize("(while foo bar)"))

View File

@ -293,8 +293,8 @@
(defn test-context []
"NATIVE: test with"
(with-as (open "README.md" "r") fd
(pass)))
(with [fd (open "README.md" "r")] (pass))
(with [(open "README.md" "r")] (pass)))
(defn test-for-doodle []