Skip to content

Commit

Permalink
[ysh language] Remove $f(x) in favor of $[f(x)]
Browse files Browse the repository at this point in the history
It is more explicit, and doesn't conflict with

    json write (x)

e.g. this difference would be confusing:

    json write $f (x)
    json write $f(x)

This is #1274.

Still TODO: remove @split(x) in favor of @[split(x)]
  • Loading branch information
Andy C committed May 27, 2023
1 parent 6335000 commit e60d059
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 93 deletions.
16 changes: 0 additions & 16 deletions osh/word_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,22 +1507,6 @@ def _ReadCompoundWord3(self, lex_mode, eof_type, empty_ok):
vsub_token = self.cur_token

part = SimpleVarSub(vsub_token, lexer.TokenSliceLeft(vsub_token, 1)) # type: word_part_t
if self.token_type == Id.VSub_DollarName:
# Look ahead for $strfunc(x)
# $f(x) or --name=$f(x) is allowed
# but "--name=$f(x)" not allowed? This would BREAK EXISTING CODE.
# It would need a parse option.

next_id = self.lexer.LookAheadOne(lex_mode)
if next_id == Id.Op_LParen:
arglist = ArgList.CreateNull(alloc_lists=True)
self._ParseInlineCallArgs(arglist)
part = word_part.FuncCall(vsub_token, arglist)

# Unlike @arrayfunc(x), it makes sense to allow $f(1)$f(2)
# var a = f(1); var b = f(2); echo $a$b
# It's consistent with other uses of $.

w.parts.append(part)

elif self.token_kind == Kind.ExtGlob:
Expand Down
5 changes: 0 additions & 5 deletions osh/word_parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,6 @@ def testOilSplice(self):
# Can't have trailing chars
_assertReadWordFailure(self, '@func().', oil_at=True)

def testInlineFuncCall(self):
_assertReadWord(self, '--foo=$func()', oil_at=True)
_assertReadWord(self, '$func()trailer', oil_at=True)
_assertReadWord(self, '--foo=$func()trailer', oil_at=True)

def testReadComment(self):
# Test that we get Id.Op_Newline
code = 'foo # comment\nbar #comment\n'
Expand Down
6 changes: 1 addition & 5 deletions spec/bugs.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ echo DONE
## OK mksh/zsh status: 1
## STDOUT:
## END
## OK osh status: 0
## OK osh STDOUT:
DONE
## END

#### Function names
foo$x() {
Expand All @@ -97,7 +93,7 @@ foo $x() {
}

## status: 2
## OK mksh/zsh/osh status: 1
## OK mksh/zsh status: 1
## BUG zsh status: 0
## STDOUT:
## END
Expand Down
2 changes: 1 addition & 1 deletion spec/hay-isolation.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ hay eval :result {
}

const args = result['children'][0]['args']
write --sep ' ' -- $len(result['children']) @args
write --sep ' ' -- $[len(result['children'])] @args

## STDOUT:
outside
Expand Down
28 changes: 14 additions & 14 deletions spec/hay.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,21 @@ Package one
echo status=$?

setvar args = _hay()['children'][0]['args']
write --sep ' ' $len(_hay()['children']) @args
write --sep ' ' $[len(_hay()['children'])] @args

hay eval :result {
Package two
echo status=$?
}

setvar args = result['children'][0]['args']
write --sep ' ' $len(result['children']) @args
write --sep ' ' $[len(result['children'])] @args

Package three
echo status=$?

setvar args = _hay()['children'][0]['args']
write --sep ' ' $len(_hay()['children']) $[_hay()['children'][0]['args'][0]]
write --sep ' ' $[len(_hay()['children'])] $[_hay()['children'][0]['args'][0]]

## STDOUT:
status=0
Expand Down Expand Up @@ -288,17 +288,17 @@ hay eval :result {

user alice
# = _hay()
write -- $len(_hay()['children'])
write -- $[len(_hay()['children'])]

user bob
setvar result = _hay()
write -- $len(_hay()['children'])
write -- $[len(_hay()['children'])]

}

# TODO: Should be cleared here
setvar result = _hay()
write -- $len(_hay()['children'])
write -- $[len(_hay()['children'])]

## STDOUT:
1
Expand Down Expand Up @@ -332,14 +332,14 @@ hay eval :result {
}

#= result
write -- 'level 0 children' $len(result['children'])
write -- 'level 1 children' $len(result['children'][0]['children'])
write -- 'level 0 children' $[len(result['children'])]
write -- 'level 1 children' $[len(result['children'][0]['children'])]

hay eval :result {
haynode parent foo
haynode parent bar
}
write -- 'level 0 children' $len(result['children'])
write -- 'level 0 children' $[len(result['children'])]


## STDOUT:
Expand Down Expand Up @@ -582,11 +582,11 @@ shvar _DIALECT=sourcehut {
}

const children = d['children']
write 'level 0 children' $len(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 All @@ -612,8 +612,8 @@ const block = parse_hay(path)

hay define Package
const d = eval_hay(block)
write 'level 0 children' $len(d['children'])
write 'level 1 children' $len(d['children'][1]['children'])
write 'level 0 children' $[len(d['children'])]
write 'level 1 children' $[len(d['children'][1]['children'])]

## STDOUT:
level 0 children
Expand Down
8 changes: 4 additions & 4 deletions spec/tea-func.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ shopt -s parse_tea
func add(x Int, y=1, z=1+2*3) {
return x + y + z
}
echo $add(3)
echo $add(3,4)
echo $[add(3)]
echo $[add(3,4)]
## STDOUT:
11
14
Expand Down Expand Up @@ -226,7 +226,7 @@ var a = [42, 43]
var n = %{x: 99, y: 100}

echo ____
write string $f(0, 1, ...a, c=2, d=3)
write string $[f(0, 1, ...a, c=2, d=3)]

# Now get a list back
echo ____
Expand Down Expand Up @@ -263,7 +263,7 @@ array
var f = |x| x+1
var y = f(0)
echo $y
echo $f(42)
echo $[f(42)]
## STDOUT:
1
43
Expand Down
4 changes: 2 additions & 2 deletions spec/ysh-array.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#### basic array
var x = %(1 2 3)
write len=$len(x)
write len=$[len(x)]
## STDOUT:
len=3
## END
Expand All @@ -12,7 +12,7 @@ shopt -s oil:all

var x = 1
var a = %($x $(write hi) 'sq' "dq $x")
write len=$len(a)
write len=$[len(a)]
write @a
## STDOUT:
len=4
Expand Down
2 changes: 1 addition & 1 deletion spec/ysh-bugs.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var d = {data: 'foo'}
echo $[d->data]

var e = {enum: 1, class: 2, import: 3, const: 4, var: 5, set: 6}
echo $len(e)
echo $[len(e)]

## STDOUT:
foo
Expand Down
48 changes: 24 additions & 24 deletions spec/ysh-expr.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ echo shell=${#x[@]}

# Oil function call: length is 20. I think that makes sense? It's just a
# different notion of length.
echo oil=$len(x)
echo oil=$[len(x)]

## STDOUT:
shell=5
oil=21
## END

#### $len(x) inside strings
#### $[len(x)] inside strings
var s = "abc"
echo -$len(s)-
echo -$[len(s)]-

# This already has a meaning ...
#echo "-$len(x)-"
Expand All @@ -111,7 +111,7 @@ echo -$len(s)-

#### Func with multiple args in multiple contexts
var x = max(1+2, 3+4)
echo $x $max(1+2, 3+4)
echo $x $[max(1+2, 3+4)]

## STDOUT:
7 7
Expand All @@ -120,7 +120,7 @@ echo $x $max(1+2, 3+4)

#### Trailing Comma in Param list
var x = max(1+2, 3+4,)
echo $x $max(1+2, 3+4,)
echo $x $[max(1+2, 3+4,)]

## STDOUT:
7 7
Expand All @@ -130,7 +130,7 @@ echo $x $max(1+2, 3+4,)
var s = "123"

# lex_mode_e.ShCommand -> Expr -> ShCommand -> Expr
var x = $(echo $'len\n' $len(s))
var x = $(echo $'len\n' $[len(s)])
echo $x
## STDOUT:
len 3
Expand Down Expand Up @@ -398,9 +398,9 @@ great
var zero = ()
var one = tup(42)
var two = (1,2)
echo $len(zero)
echo $len(one)
echo $len(two)
echo $[len(zero)]
echo $[len(one)]
echo $[len(two)]
## STDOUT:
0
1
Expand All @@ -423,11 +423,11 @@ true

#### dict with 'bare word' keys
var d0 = {}
echo len=$len(d0)
echo len=$[len(d0)]
var d1 = {name: "hello"}
echo len=$len(d1)
echo len=$[len(d1)]
var d2 = {name: "hello", other: 2}
echo len=$len(d2)
echo len=$[len(d2)]
## STDOUT:
len=0
len=1
Expand All @@ -436,13 +436,13 @@ len=2

#### dict with expression keys
var d1 = {['name']: "hello"}
echo len=$len(d1)
echo len=$[len(d1)]
var v = d1['name']
echo $v

var key='k'
var d2 = {["$key"]: "bar"}
echo len=$len(d2)
echo len=$[len(d2)]
var v2 = d2['k']
echo $v2

Expand All @@ -457,12 +457,12 @@ bar
#### dict literal with implicit value
var name = 'foo'
var d1 = {name}
echo len=$len(d1)
echo len=$[len(d1)]
var v1 = d1['name']
echo $v1

var d2 = {name, other: 'val'}
echo len=$len(d2)
echo len=$[len(d2)]
var v2 = d2['name']
echo $v2

Expand Down Expand Up @@ -548,24 +548,24 @@ var dq = "
dq
2
"
echo dq=$len(dq)
echo dq=$[len(dq)]
var sq = '
sq
2
'
echo sq=$len(sq)
echo sq=$[len(sq)]
var mylist = [
1,
2,
3,
]
echo mylist=$len(mylist)
echo mylist=$[len(mylist)]
var mytuple = (1,
2, 3)
echo mytuple=$len(mytuple)
echo mytuple=$[len(mytuple)]
## STDOUT:
dq=6
Expand All @@ -582,7 +582,7 @@ mytuple=3
var mydict = { a:1,
b: 2,
}
echo mydict=$len(mydict)
echo mydict=$[len(mydict)]
## STDOUT:
mydict=2
## END
Expand All @@ -593,13 +593,13 @@ var array = %(
two
three
)
echo array=$len(array)
echo array=$[len(array)]
var comsub = $(
echo hi
echo bye
)
echo comsub=$len(comsub)
echo comsub=$[len(comsub)]
## STDOUT:
array=3
Expand Down Expand Up @@ -640,7 +640,7 @@ echo $k
var a = [1, 2]
var b = [3]
var c = a ++ b
echo len=$len(c)
echo len=$[len(c)]
## STDOUT:
abcde
Expand Down

0 comments on commit e60d059

Please sign in to comment.