Skip to content

Commit

Permalink
[oil-language] Got lists and subscripts working.
Browse files Browse the repository at this point in the history
Had to hack in an 'any' type to ASDL.
  • Loading branch information
Andy Chu committed Jul 11, 2019
1 parent 5697002 commit b4ddfbd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions asdl/meta.py
Expand Up @@ -109,5 +109,9 @@ def LookupFieldType(self, field_name):
'int': IntType(),
'bool': BoolType(),
'dict': DictType(),

# TODO: This is a hack for the Oil expression evaluator. We're not doing
# any dynamic or static checking now.
'any': StrType(),
}

14 changes: 14 additions & 0 deletions oil_lang/expr_eval.py
Expand Up @@ -55,6 +55,10 @@ def EvalRHS(self, node):
if node.tag == expr_e.Const:
return int(node.c.val)

if node.tag == expr_e.Var:
val = self.mem.GetVar(node.name.val)
return val.obj

if node.tag == expr_e.Binary:
left = self.EvalRHS(node.left)
right = self.EvalRHS(node.right)
Expand All @@ -70,4 +74,14 @@ def EvalRHS(self, node):

raise NotImplementedError(node.op.id)

if node.tag == expr_e.List:
return [self.EvalRHS(e) for e in node.elts]

if node.tag == expr_e.Subscript:
collection = self.EvalRHS(node.collection)

# TODO: handle multiple indices like a[i, j]
index = self.EvalRHS(node.indices[0])
return collection[index]

raise NotImplementedError(node.__class__.__name__)
8 changes: 7 additions & 1 deletion oil_lang/testdata/hello.osh
Expand Up @@ -6,8 +6,14 @@ var x = 1 + 2*3

echo "x: $x"

var mylist = [1, 2, 3]

var y = mylist[1] * 10

echo "y: $y"


# TODO:
# - Mutate variables
# - Demo lists and dicts
# - Demo dicts

2 changes: 1 addition & 1 deletion osh/cmd_exec.py
Expand Up @@ -772,7 +772,7 @@ def _Dispatch(self, node, fork_external):

lval = self.expr_ev.EvalLHS(node.lhs)
py_val = self.expr_ev.EvalRHS(node.rhs)
val = value.Str(str(py_val))
val = value.Obj(py_val)
flags = ()
self.mem.SetVar(lval, val, flags, scope_e.LocalOnly)

Expand Down
2 changes: 2 additions & 0 deletions osh/runtime.asdl
Expand Up @@ -30,6 +30,8 @@ module runtime
| Str(string s)
| StrArray(string* strs)
| AssocArray(dict d)
-- A Python-style object. 'any' is not currently type checked.
| Obj(any obj)

-- For Oil?
-- | ArrayInt(int* array_int)
Expand Down
3 changes: 3 additions & 0 deletions osh/word_eval.py
Expand Up @@ -52,6 +52,9 @@ def _ValueToPartValue(val, quoted):
# TODO: Is this correct?
return part_value.Array(val.d.values())

elif val.tag == value_e.Obj:
return part_value.String(repr(val.obj), quoted, not quoted)

else:
# Undef should be caught by _EmptyStrOrError().
raise AssertionError(val.__class__.__name__)
Expand Down

0 comments on commit b4ddfbd

Please sign in to comment.