adding in array access

This commit is contained in:
Paul R. Tagliamonte 2013-03-09 00:55:27 -05:00
parent 5bdebdf5fc
commit f97cb5e4cb
2 changed files with 33 additions and 0 deletions

View File

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

View File

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