diff --git a/examples/awk/18.awk b/examples/awk/18.awk new file mode 100644 index 0000000..760b9db --- /dev/null +++ b/examples/awk/18.awk @@ -0,0 +1,4 @@ +{ sum = 0 + for (i = 1; i <= NF; i = i + 1) sum = sum + $i + print sum +} diff --git a/examples/awk/18.data b/examples/awk/18.data new file mode 100644 index 0000000..50ecbb5 --- /dev/null +++ b/examples/awk/18.data @@ -0,0 +1,3 @@ +1 2 3 +4 5 6 +7 8 9 diff --git a/examples/awk/18.msh b/examples/awk/18.msh new file mode 100644 index 0000000..1410423 --- /dev/null +++ b/examples/awk/18.msh @@ -0,0 +1 @@ +wt ((toFloat) map sum .s str wl) each diff --git a/examples/awk/test.sh b/examples/awk/test.sh index 761e976..1f5d707 100755 --- a/examples/awk/test.sh +++ b/examples/awk/test.sh @@ -44,5 +44,6 @@ emp_test 14 emp_test 15 emp_test 16 emp_test 17 +data_test 18 exit "$FAIL" diff --git a/lib/std.msh b/lib/std.msh index 540370d..c49afbc 100644 --- a/lib/std.msh +++ b/lib/std.msh @@ -6,9 +6,11 @@ def each 0 each-idx! # index ( [(@each-idx @each-len >=) (break)] if + # "Each idx: " w @each-idx w " Each len: " w @each-len wl over @each-idx nth # Get current item - over x # Copy over quote, execute + over .s x # Copy over quote, execute @each-idx 1 + each-idx! # inc index + # "Each idx: " w @each-idx wl ) loop # Drop list and quote, total length, index diff --git a/mshell/Evaluator.go b/mshell/Evaluator.go index 6896367..2e93e88 100644 --- a/mshell/Evaluator.go +++ b/mshell/Evaluator.go @@ -916,9 +916,20 @@ MainLoop: switch obj2.(type) { case *MShellInt: stack.Push(&MShellInt{obj2.(*MShellInt).Value + obj1.(*MShellInt).Value}) + case *MShellFloat: + stack.Push(&MShellFloat{float64(obj2.(*MShellFloat).Value) + float64(obj1.(*MShellInt).Value)}) default: return FailWithMessage(fmt.Sprintf("%d:%d: Cannot add an integer to a %s.\n", t.Line, t.Column, obj2.TypeName())) } + case *MShellFloat: + switch obj2.(type) { + case *MShellFloat: + stack.Push(&MShellFloat{obj2.(*MShellFloat).Value + obj1.(*MShellFloat).Value}) + case *MShellInt: + stack.Push(&MShellFloat{float64(obj2.(*MShellInt).Value) + obj1.(*MShellFloat).Value}) + default: + return FailWithMessage(fmt.Sprintf("%d:%d: Cannot add a float to a %s.\n", t.Line, t.Column, obj2.TypeName())) + } case *MShellString: switch obj2.(type) { case *MShellString: @@ -946,7 +957,7 @@ MainLoop: stack.Push(newList) } default: - return FailWithMessage(fmt.Sprintf("%d:%d: Cannot apply '+' to a %s to a %s.\n", t.Line, t.Column, obj2.TypeName(), obj1.TypeName())) + return FailWithMessage(fmt.Sprintf("%d:%d: Cannot apply '+' between a %s and a %s.\n", t.Line, t.Column, obj2.TypeName(), obj1.TypeName())) } } else if t.Type == MINUS { obj1, err := stack.Pop() diff --git a/tests/each.msh b/tests/each.msh new file mode 100644 index 0000000..03d3bbb --- /dev/null +++ b/tests/each.msh @@ -0,0 +1 @@ +["a" "b" "c"] (wl) each diff --git a/tests/indexing.msh.stdout b/tests/indexing.msh.stdout index f909fb8..e1b7959 100644 --- a/tests/indexing.msh.stdout +++ b/tests/indexing.msh.stdout @@ -14,3 +14,4 @@ a, b, x, c, d a, b, c, x, d a, b, x, d a, b, c, x +d, c, b, a