Adding in lambdas.

This commit is contained in:
Paul R. Tagliamonte 2013-03-09 15:57:13 -05:00
parent e9d22982af
commit 8a2ba20407
2 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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))))