Skip to content

Commit

Permalink
[oil-language] Initial implementation of obj.attr and d->key
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Sep 18, 2019
1 parent c1469ce commit 41f53e9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
24 changes: 24 additions & 0 deletions oil_lang/expr_eval.py
Expand Up @@ -368,4 +368,28 @@ def EvalExpr(self, node):
index = self.EvalExpr(node.indices[0])
return collection[index]

# TODO: obj.method() should be separate
if node.tag == expr_e.Attribute: # obj.attr
o = self.EvalExpr(node.value)
id_ = node.op.id
if id_ == Id.Expr_Dot:
name = node.attr.name.val
# TODO: Does this do the bound method thing we do NOT want?
return getattr(o, name)

if id_ == Id.Expr_RArrow: # d->key is like d['key']
name = node.attr.name.val
return o[name]

if id_ == Id.Expr_DColon: # StaticName::member
raise NotImplementedError(id_)

# TODO: We should prevent virtual lookup here? This is a pure static
# namespace lookup?
# But Python doesn't any hook for this.
# Maybe we can just check that it's a module? And modules don't lookup
# in a supertype or __class__, etc.

raise AssertionError(id_)

raise NotImplementedError(node.__class__.__name__)
30 changes: 30 additions & 0 deletions spec/oil-expr.test.sh
Expand Up @@ -780,3 +780,33 @@ argv.py @m2
['bar', 'ar']
## END
#### obj.attr and obj.method()
var s = 'hi'
# TODO: This does a bound method thing we probably don't want
var s2 = s.upper()
echo $s2
## STDOUT:
HI
## END
#### obj.method does NOT give you a bound method
# TODO: Not sure how to implement this
var s = 'hi'
var method = s.upper
echo $method
## STDOUT:
## END
#### d->key
var d = {name: 'andy'}
var x = d->name
echo $x
## STDOUT:
andy
## END
2 changes: 1 addition & 1 deletion test/spec.sh
Expand Up @@ -250,7 +250,7 @@ oil-options() {
}

oil-expr() {
sh-spec spec/oil-expr.test.sh --cd-tmp --osh-failures-allowed 7 \
sh-spec spec/oil-expr.test.sh --cd-tmp --osh-failures-allowed 8 \
$OSH_LIST "$@"
}

Expand Down

0 comments on commit 41f53e9

Please sign in to comment.