diff --git a/hy/compiler.py b/hy/compiler.py index 861d745..649e30f 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -25,6 +25,7 @@ from hy.models.integer import HyInteger from hy.models.string import HyString from hy.models.symbol import HySymbol from hy.models.list import HyList +from hy.models.dict import HyDict import ast @@ -115,6 +116,19 @@ class HyASTCompiler(object): lineno=e.start_line, col_offset=e.start_column) + @builds("get") + def compile_index_expression(self, expr): + expr.pop(0) # index + val = self.compile(expr.pop(0)) # target + sli = self.compile(expr.pop(0)) # slice + + return ast.Subscript( + lineno=expr.start_line, + col_offset=expr.start_column, + value=val, + slice=ast.Index(value=sli), + ctx=ast.Load()) + @builds("=") @builds("<") @builds("<=") @@ -292,6 +306,20 @@ class HyASTCompiler(object): return ast.Str(s=str(string), lineno=string.start_line, col_offset=string.start_column) + @builds(HyDict) + def compile_dict(self, m): + keys = [] + vals = [] + for entry in m: + keys.append(self.compile(entry)) + vals.append(self.compile(m[entry])) + + return ast.Dict( + lineno=m.start_line, + col_offset=m.start_column, + keys=keys, + values=vals) + def hy_compile(tree): " Compile a HyObject tree into a Python AST tree. " diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index a05cd92..5a6ea55 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -57,3 +57,8 @@ (cond (= 1 2) (assert (= true false)) (is null null) (assert (= true true)))) + + +(defn test_index [] + "NATIVE: Test that dict access works" + (assert (get {"one" "two"} "one") "two"))