-
Notifications
You must be signed in to change notification settings - Fork 78
Fix handling of do-blocks in the SAWScript interpreter #2473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or 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
RyanGlScott
reviewed
Jul 8, 2025
and extend it to all five SAWScript monads. Will be wanted in a moment or two. Don't export it from Interpreter.hs; it isn't used elsewhere. This in turn revealed that the withTopLevel element isn't used, so drop it.
(not just TopLevel) Has no effect yet. Coming right up...
The prior behavior was to execute into do-blocks until the first bind,
then defer execution using a chain of VBind value thunks with copies
of the interpreter closed into them. This caused a lot of unnecessary
consing; however, worse than that it caused strange eval behavior.
For example,
let f = do { let x = undefined; return 3; };
crashes immediately, but
let f = do { _ <- return (); let x = undefined; return 3; };
crashes only when the resulting monadic value is executed.
To fix this, introduce a VDo value, containing abstract syntax rather
than values, and when we hit a do-block when executing purely, return it
immediately as a VDo value. Then, in the same places that process VBind,
process VDo by executing the statements in it.
This changes the internals of the interpreter slightly such that
expressions are always evaluated purely and statements are always
evaluated in a monad context; it is thus necessary when executing
statements to force any VDo values that appear. (The typechecker
ensures that they'll be do-blocks in the correct monad.)
Unfortunately, we can't get rid of VBind yet. There is one thing that
still needs it: the "for" builtin. It can't use VDo: as a builtin, it
is handed only values, not abstract syntax, and for the time being we
can't lift arbitrary values to abstract syntax. The best bet is
probably to rewrite it in SAWScript and stick it in the prelude we
don't yet have (see #253). Rename VBind to VBindOnce and note all
this.
One further caution: this commit removes part of the stack trace in
the reference output for test_sawscript_builtins/fail2. This is
because the current scheme for doing stack traces can't cope with
the new VDo, and I'm not inclined to garbage things up to make it.
I'm going to add some chewing gum and baling wire to bring the stack
trace entry back in the next commit, but I expect to be able to revert
the mess in the near future. :-)
bccc844 to
be73b4b
Compare
This restores the stack trace compromised in the previous commit, at the cost of making a small mess. This is its own commit in order to be able to revert it easily later. This works by stuffing a Maybe String into VDo, populating it from the stack trace goop, and feeding it to a wrapper around interpretStmts. As an instance of chewing gum, it could be worse...
be73b4b to
65ba586
Compare
Contributor
Author
RyanGlScott
approved these changes
Jul 9, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is not stable yet, since it needs to move on top of #2471, but the content should be stable and ready for an initial review.
...provided the tests pass. If they don't, it may need to be thrashed at some length and it's probably not worth reviewing until that happens.
Given how many bizarre things I've visited in the past few days, I give that about 50/50.