From 8a2ba204078b7e4f4ace398f0640e8fc1813e240 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Sat, 9 Mar 2013 15:57:13 -0500 Subject: [PATCH] Adding in lambdas. --- hy/compiler.py | 22 ++++++++++++++++++++++ tests/native_tests/language.hy | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/hy/compiler.py b/hy/compiler.py index 649e30f..9989ec4 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -116,6 +116,28 @@ class HyASTCompiler(object): lineno=e.start_line, col_offset=e.start_column) + @builds("lambda") + def compile_lambda_expression(self, expr): + expr.pop(0) + sig = expr.pop(0) + body = expr.pop(0) + # assert expr is empty + return ast.Lambda( + lineno=expr.start_line, + col_offset=expr.start_column, + args=ast.arguments(args=[ + ast.Name(arg=str(x), id=str(x), + ctx=ast.Param(), + lineno=x.start_line, + col_offset=x.start_column) + for x in sig], + vararg=None, + kwarg=None, + defaults=[], + kwonlyargs=[], + kw_defaults=[]), + body=self.compile(body)) + @builds("get") def compile_index_expression(self, expr): expr.pop(0) # index diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index c6e84ac..7a47f47 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -63,3 +63,9 @@ "NATIVE: Test that dict access works" (assert (get {"one" "two"} "one") "two") (assert (= (get [1 2 3 4 5] 1) 2))) + + +(defn test_lambda [] + "NATIVE: test lambda operator" + (def square (lambda [x] (* x x))) + (assert (= 4 (square 2))))