diff --git a/hy/compiler.py b/hy/compiler.py index bbdf39f..b04a897 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -178,6 +178,23 @@ class HyASTCompiler(object): fn.decorator_list = [self.compile(x) for x in expr] return fn + @builds("kwapply") + def compile_kwapply_expression(self, expr): + expr.pop(0) # kwapply + call = self.compile(expr.pop(0)) + kwargs = expr.pop(0) + + if type(call) != ast.Call: + raise TypeError("kwapplying a non-call") + + call.keywords = [ + ast.keyword( + arg=str(x), + value=self.compile(kwargs[x]) + ) for x in kwargs] + + return call + @builds("=") @builds("!=") @builds("<") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index d5094b2..581e02d 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -1,8 +1,8 @@ ; - -(import sys) +(import-from tests.resources kwtest) (import-from os.path exists isdir isfile) +(import sys) (defn test-sys-argv [] @@ -103,3 +103,7 @@ (defn test-decorators [] (assert (= (tfunction) 2))) + + +(defn test-kwargs [] + (assert (= (kwapply (kwtest) {"one" "two"}) {"one" "two"}))) diff --git a/tests/resources/__init__.py b/tests/resources/__init__.py new file mode 100644 index 0000000..b115480 --- /dev/null +++ b/tests/resources/__init__.py @@ -0,0 +1,4 @@ + + +def kwtest(*args, **kwargs): + return kwargs