Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[oil-language] A bunch of things about typed arrays working.
- Loading branch information
Andy Chu
committed
Sep 26, 2019
1 parent
f22e0b1
commit 045a5fc
Showing
9 changed files
with
247 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Typed arrays | ||
|
||
#### integer array | ||
var x = @[1 2 3] | ||
echo len=$len(x) | ||
## STDOUT: | ||
len=3 | ||
## END | ||
|
||
#### string array with command sub, varsub, etc. | ||
shopt -s oil:basic | ||
|
||
var x = 1 | ||
var a = @[$x $(echo hi)] | ||
echo len=$len(a) | ||
echo @a | ||
## STDOUT: | ||
len=2 | ||
1 | ||
hi | ||
## END | ||
|
||
#### arrays with expressions | ||
shopt -s oil:basic | ||
|
||
# Does () make makes sense? | ||
|
||
var x = 5 | ||
var y = 6 | ||
var a = @[(x+1) (y*2)] | ||
|
||
echo len=$len(a) | ||
echo @a | ||
|
||
## STDOUT: | ||
len=2 | ||
6 | ||
12 | ||
## END | ||
|
||
#### Empty arrays and using Array[T] | ||
shopt -s oil:basic | ||
|
||
var b = Array[Bool]() | ||
var i = Array[Int]() | ||
|
||
#var f = Array[Float]() | ||
echo len=$len(b) | ||
echo len=$len(i) | ||
|
||
var b2 = Array[Bool]([true, false]) | ||
echo @b2 | ||
|
||
#echo len=$len(f) | ||
## STDOUT: | ||
len=0 | ||
len=0 | ||
True | ||
False | ||
## END | ||
|
||
|
||
#### Arrays from generator expressions | ||
shopt -s oil:basic | ||
|
||
var b = Array[Bool](true for _ in 1:3) | ||
var i = Array[Int](i+1 for i in 1:3) | ||
var f = Array[Float](i * 2.5 for i in 1:3) | ||
echo @b | ||
echo @i | ||
echo @f | ||
## STDOUT: | ||
len=0 | ||
len=0 | ||
len=0 | ||
## END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Test var / setvar / etc. | ||
|
||
# TODO: GetVar needs a mode where Obj[str] gets translated to value.Str? | ||
# Then all code will work. | ||
# | ||
# word_eval: | ||
# | ||
# val = self.mem.GetVar(var_name) -> | ||
# val = GetWordVar(self.mem, var_name) | ||
# | ||
# Conversely, in oil_lang/expr_eval.py: | ||
# LookupVar gives you a plain Python object. I don't think there's any | ||
# downside here. | ||
# | ||
# repr exposes the differences. | ||
# | ||
# Notes: | ||
# | ||
# - osh/cmd_exec.py handles OilAssign, which gets wrapped in value.Obj() | ||
# - osh/word_eval.py _ValueToPartValue handles 3 value types. Used in: | ||
# - _EvalBracedVarSub | ||
# - SimpleVarSub in _EvalWordPart | ||
# - osh/expr_eval.py: _LookupVar wrapper should disallow using Oil values | ||
# - this is legacy stuff. Both (( )) and [[ ]] | ||
# - LhsIndexedName should not reference Oil vars either | ||
|
||
|
||
#### integers expression and augmented assignment | ||
var x = 1 + 2 * 3 | ||
echo x=$x | ||
|
||
setvar x += 4 | ||
echo x=$x | ||
## STDOUT: | ||
x=7 | ||
x=11 | ||
## END | ||
|
||
#### setvar when variable isn't declared results in fatal error | ||
var x = 1 | ||
f() { | ||
# setting global is OK | ||
setvar x = 2 | ||
echo x=$x | ||
|
||
setvar y = 3 # NOT DECLARED | ||
echo y=$y | ||
} | ||
f | ||
## status: 1 | ||
## STDOUT: | ||
x=2 | ||
## END | ||
|
||
#### var/setvar x, y = 1, 2 | ||
|
||
# Python doesn't allow you to have annotation on each variable! | ||
# https://www.python.org/dev/peps/pep-0526/#where-annotations-aren-t-allowed | ||
#var x Int, y Int = 3, 4 | ||
setvar x, y = 1, 2 | ||
echo $x $y | ||
## STDOUT: | ||
1 2 | ||
## END | ||
|
||
#### setvar x[1] = 42 | ||
shopt -s oil:basic | ||
var mylist = [1,2,3] | ||
setvar x[1] = 42 | ||
echo -sep ' ' @x | ||
## STDOUT: | ||
1 42 3 | ||
## END | ||
|
||
|
||
#### duplicate var def results in fatal error | ||
var x = "global" | ||
f() { | ||
var x = "local" | ||
echo x=$x | ||
} | ||
f | ||
var x = "redeclaration is an error" | ||
## status: 1 | ||
## STDOUT: | ||
x=local | ||
## END | ||
|
||
#### setvar dynamic scope (TODO: change this?) | ||
modify_with_shell_assignment() { | ||
f=shell | ||
} | ||
|
||
modify_with_setvar() { | ||
setvar f = "setvar" | ||
} | ||
|
||
f() { | ||
var f = 1 | ||
echo f=$f | ||
modify_with_shell_assignment | ||
echo f=$f | ||
modify_with_setvar | ||
echo f=$f | ||
} | ||
f | ||
## STDOUT: | ||
f=1 | ||
f=shell | ||
f=setvar | ||
## END | ||
|
Oops, something went wrong.