Skip to content
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 8 commits into from
Feb 25, 2018
Merged

feat: Add while loop support #30

merged 8 commits into from
Feb 25, 2018

Conversation

sjbarag
Copy link
Owner

@sjbarag sjbarag commented Feb 25, 2018

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.

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.
@sjbarag 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
@@ -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.
Copy link
Owner Author

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

@sjbarag sjbarag merged commit 174dfd6 into master Feb 25, 2018
@sjbarag sjbarag deleted the add-while-loops branch February 25, 2018 22:54
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant