Skip to content

Commit

Permalink
Don't let loops change the size of the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Dec 15, 2024
1 parent be99c34 commit 7b69b87
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ MShellObject

[fish shell built in](https://github.com/fish-shell/fish-shell/tree/master/src/builtins)
[Stroustrop's Rule](https://buttondown.com/hillelwayne/archive/stroustrops-rule/)
[WebAssembly Type Checking](https://binji.github.io/posts/webassembly-type-checking/)
7 changes: 7 additions & 0 deletions mshell/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,9 +1324,16 @@ MainLoop:

breakDiff := 0

initialStackSize := len(*stack)

for loopCount < maxLoops {
result := state.Evaluate(quotation.Tokens, stack, loopContext, definitions)

if len(*stack) != initialStackSize {
// If the stack size changed, we have an error.
return FailWithMessage(fmt.Sprintf("%d:%d: Stack size changed from %d to %d in loop.\n", t.Line, t.Column, initialStackSize, len(*stack)))
}

if !result.Success {
return result
}
Expand Down
11 changes: 6 additions & 5 deletions tests/loop_test.msh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[
echo
(
hello world
[ (10 10 =) (break)] if
) loop
] ;
]
(
hello append world append
[ (10 10 =) (break)] if
) loop
;
2 changes: 1 addition & 1 deletion tests/nested_loop.msh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0 outer-inc!
(
"Outer: " @outer-inc str wl
"Outer: " @outer-inc str + wl
0 inner-inc!
(
" Inner: " w @inner-inc str wl
Expand Down
8 changes: 4 additions & 4 deletions tests/nested_loop.msh.stdout
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
0
Outer: 0
Inner: 0
Inner: 1
Inner: 2
Inner: 3
1
Outer: 1
Inner: 0
Inner: 1
Inner: 2
Inner: 3
2
Outer: 2
Inner: 0
Inner: 1
Inner: 2
Inner: 3
3
Outer: 3
Inner: 0
Inner: 1
Inner: 2
Expand Down
2 changes: 1 addition & 1 deletion tests/while_read.msh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1
(
read [(not) (break)] if
read [(not) (drop break)] if
linetext! num! "Line " @num str ": " + + @linetext + [echo] append ; @num 1 +
) "stdin_for_test.txt" < loop

0 comments on commit 7b69b87

Please sign in to comment.