-
Notifications
You must be signed in to change notification settings - Fork 43
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
feat: Add while loop support #30
Merged
Merged
Conversation
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
Executing an if statement should never result in a value, nor should a block. Let's make that rule more formal via the type system.
Functions like parser.expressionStatement are only ever going to return an expression statement, so there's no sense in leaving the return values lax.
BrightScript 'while' loops can be exited early with `exit while`, which means we need some way to keep track of why a block stopped exiting. Sometimes there's an error, sometimes it's an `exit while` statement, sometimes it's from a `return` statement! That last one isn't supported yet (because we don't have functions), but the point still stands. This is a big moment, because BrightScript execution is no longer proportional to the length of a `.brs` file.
I should eventually add some invalid cases to these. Right now the entire test/parser/ tree tests valid syntax but has minimal (if any) coverage for invalid syntax.
Hooray, they loop!
sjbarag
added
enhancement
Any addition to this project's existing capabilities
lexer
Affects this project's lexer (aka scanner)
parser
Affects this project's token parser
interpreter
Affects this project's tree-walking interpreter
e2e
Affects this project's end-to-end test cases (the BrightScript sample files executed during testing)
labels
Feb 25, 2018
sjbarag
commented
Feb 25, 2018
@@ -58,6 +58,9 @@ export enum Lexeme { | |||
EndWhile, | |||
Eval, | |||
Exit, | |||
// ExitFor isn't listed as a reserved word, but it seems like it'd match 'exitwhile'. | |||
// TODO: Verify its reservedness on-device. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Experimentally, exitfor
isn't a reserved word, and exit for
is the only way to exit a for
loop:
for i=0 to 10
print i
if i = 2 then exit for
end for
exitfor = "not reserved"
print exitfor ' prints "not reserved"
But exitwhile
is indeed a reserved word, and is substitutable for exit while
:
a = 0
while a < 10
a = a + 1
print a
if a = 3 then exitwhile
end while
exitwhile = "not reserved?" ' ERRNO: 175 - Exit While is not inside a While.
print exitwhile
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
e2e
Affects this project's end-to-end test cases (the BrightScript sample files executed during testing)
enhancement
Any addition to this project's existing capabilities
interpreter
Affects this project's tree-walking interpreter
lexer
Affects this project's lexer (aka scanner)
parser
Affects this project's token parser
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.
While loops are necessary for basically every language, because they allow the execution duration of a program to not be bound to the source length of the program.
Also BrightScript on Rou makes heavy use of
while
loops, so we need them too.