Skip to content

Commit

Permalink
[breaking YSH] Swap . and -> operators
Browse files Browse the repository at this point in the history
New syntax:

- mydict.key
- s->startswith(prefix)

See Zulip:

    #language-design > obj->method(x) Proposal

This is #1636
  • Loading branch information
Andy C committed May 27, 2023
1 parent 36b3769 commit 906acd0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 29 deletions.
4 changes: 2 additions & 2 deletions spec/hay.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ const children = d['children']
write 'level 0 children' $[len(children)] ---

# TODO: Do we need @[] for array expression sub?
write 'child 0' $[children[0]->type] $[join(children[0]->args)] ---
write 'child 1' $[children[1]->type] $[join(children[1]->args)] ---
write 'child 0' $[children[0].type] $[join(children[0].args)] ---
write 'child 1' $[children[1].type] $[join(children[1].args)] ---

## STDOUT:
level 0 children
Expand Down
6 changes: 3 additions & 3 deletions spec/ysh-assign.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,16 @@ x=1 y=9
x=9 y=1
## END

#### setvar d->key = 42 (setitem)
#### setvar d.key = 42 (setitem)
shopt -s oil:all

var d = {}
setvar d['f2'] = 42
setvar d->f3 = 43
setvar d.f3 = 43

# Use the opposite thing to retrieve
var f3 = d['f3']
var f2 = d->f2
var f2 = d.f2
echo f3=$f3
echo f2=$f2
## STDOUT:
Expand Down
6 changes: 3 additions & 3 deletions spec/ysh-expr-sub.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ echo $[d['key']]
42
## END

#### $[d->key]
#### $[d.key]
var d = {}
setvar d['key'] = 42
echo $[d->key]
echo $[d.key]
## STDOUT:
42
## END
Expand All @@ -30,7 +30,7 @@ var d = {}
setvar d['key'] = 42
echo "func $[len(a)]"
echo "key $[d['key']]"
echo "key $[d->key]"
echo "key $[d.key]"
echo "dq $[d["key"]]"
## STDOUT:
func 3
Expand Down
14 changes: 7 additions & 7 deletions spec/ysh-expr.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -606,26 +606,26 @@ array=3
comsub=6
## END
#### obj.attr and obj.method()
#### obj->method()
var s = 'hi'
# TODO: This does a bound method thing we probably don't want
var s2 = s.upper()
var s2 = s->upper()
echo $s2
## STDOUT:
HI
## END
#### obj.method does NOT give you a bound method
#### obj->method does NOT give you a bound method
var s = 'hi'
var method = s.upper
var method = s->upper
echo $method
## status: 2
## status: 3
## stdout-json: ""
#### d->key
#### d.key
var d = {name: 'andy'}
var x = d->name
var x = d.name
echo $x
## STDOUT:
andy
Expand Down
8 changes: 4 additions & 4 deletions spec/ysh-user-feedback.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ EOF
git-branch-merged | while read --line {
# Note: this can't be 'const' because const is dynamic like 'readonly'. And
# we don't have block scope.
var line = _line.strip() # removing leading space
var line = _line->strip() # removing leading space

# with glob: line ~~ '\**' (awkward)
# with regex: line ~ / %start '*' / (not terrible, but somewhat complex)
Expand All @@ -98,7 +98,7 @@ git-branch-merged | while read --line {
# line %startswith 'a'
# line %endswith 'b'

if (line !== 'master' and not line.startswith('*')) {
if (line !== 'master' and not line->startswith('*')) {
echo $line
}
} | readarray -t :branches
Expand All @@ -112,8 +112,8 @@ if (len(branches) === 0) {
# With "append". Hm read --lines isn't bad.
var branches2 = %()
git-branch-merged | while read --line {
var line2 = _line.strip() # removing leading space
if (line2 !== 'master' and not line2.startswith('*')) {
var line2 = _line->strip() # removing leading space
if (line2 !== 'master' and not line2->startswith('*')) {
append :branches2 $line2
}
}
Expand Down
8 changes: 4 additions & 4 deletions ysh/expr_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def EvalPlaceExpr(self, place):
place = cast(Attribute, UP_place)

obj = self.EvalExpr(place.obj, loc.Missing)
if place.op.id == Id.Expr_RArrow:
if place.op.id == Id.Expr_Dot:
attr = place.attr.tval
return lvalue.ObjIndex(obj, attr)
else:
Expand Down Expand Up @@ -1596,12 +1596,12 @@ def _EvalAttribute(self, node):
# type: (Attribute) -> Any # XXX
o = self._EvalExpr(node.obj)
id_ = node.op.id
if id_ == Id.Expr_Dot:
# Used for .startswith()
if id_ == Id.Expr_RArrow:
# Used for s->startswith(x)
name = node.attr.tval
return getattr(o, name)

if id_ == Id.Expr_RArrow: # d->key is like d['key']
if id_ == Id.Expr_Dot: # d.key is like d['key']
name = node.attr.tval
try:
result = o[name]
Expand Down
7 changes: 1 addition & 6 deletions ysh/expr_to_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,17 +580,12 @@ def Expr(self, pnode):
if pnode.NumChildren() == 1: # No trailers
return node

# Support a->startswith(b) and mydict.key
n = pnode.NumChildren()
i = 1
while i < n and ISNONTERMINAL(pnode.GetChild(i).typ):
node = self._Trailer(node, pnode.GetChild(i))
i += 1
if node.tag() == expr_e.Attribute:
# Support a.startswith(b) but not obj.field
attr_node = cast(Attribute, node)
if attr_node.op.id == Id.Expr_Dot:
p_die("obj.field isn't valid, but obj.method() is",
attr_node.op)

if i != n: # ['**' factor]
op_tok = pnode.GetChild(i).tok
Expand Down

0 comments on commit 906acd0

Please sign in to comment.