Skip to content

Commit

Permalink
Tmp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Dec 8, 2024
1 parent 1965077 commit 623b700
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/awk/18.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ sum = 0
for (i = 1; i <= NF; i = i + 1) sum = sum + $i
print sum
}
3 changes: 3 additions & 0 deletions examples/awk/18.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 2 3
4 5 6
7 8 9
1 change: 1 addition & 0 deletions examples/awk/18.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wt ((toFloat) map sum .s str wl) each
1 change: 1 addition & 0 deletions examples/awk/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ emp_test 14
emp_test 15
emp_test 16
emp_test 17
data_test 18

exit "$FAIL"
4 changes: 3 additions & 1 deletion lib/std.msh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion mshell/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/each.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a" "b" "c"] (wl) each
1 change: 1 addition & 0 deletions tests/indexing.msh.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 623b700

Please sign in to comment.