Merge pull request #6110 from gunnarbeutner/feature/for-loop-var

Implement support for optionally specifying the 'var' keyword in 'for' loops
This commit is contained in:
Michael Friedrich 2018-07-26 10:05:17 +02:00 committed by GitHub
commit e519d6bbc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 19 deletions

View File

@ -830,7 +830,7 @@ Example:
var list = [ "a", "b", "c" ] var list = [ "a", "b", "c" ]
for (item in list) { for (var item in list) {
log("Item: " + item) log("Item: " + item)
} }
@ -841,7 +841,7 @@ Iterating over dictionaries can be accomplished in a similar manner:
var dict = { a = 3, b = 7 } var dict = { a = 3, b = 7 }
for (key => value in dict) { for (var key => var value in dict) {
log("Key: " + key + ", Value: " + value) log("Key: " + key + ", Value: " + value)
} }
@ -849,6 +849,9 @@ The `continue` and `break` keywords can be used to control how the loop is execu
skips over the remaining expressions for the loop body and begins the next loop evaluation. The `break` keyword skips over the remaining expressions for the loop body and begins the next loop evaluation. The `break` keyword
breaks out of the loop. breaks out of the loop.
The `var` keyword is optional when declaring variables in the loop's header. Variables declared without the `var`
keyword are nonetheless local to the function.
## Constructors <a id="constructor"></a> ## Constructors <a id="constructor"></a>
In order to create a new value of a specific type constructor calls may be used. In order to create a new value of a specific type constructor calls may be used.

View File

@ -469,6 +469,10 @@ combined_set_op: T_SET
| T_SET_BINARY_OR | T_SET_BINARY_OR
; ;
optional_var: /* empty */
| T_VAR
;
lterm: T_LIBRARY rterm lterm: T_LIBRARY rterm
{ {
$$ = new LibraryExpression(std::unique_ptr<Expression>($2), @$); $$ = new LibraryExpression(std::unique_ptr<Expression>($2), @$);
@ -603,7 +607,7 @@ lterm: T_LIBRARY rterm
} }
| apply | apply
| object | object
| T_FOR '(' identifier T_FOLLOWS identifier T_IN rterm ')' | T_FOR '(' optional_var identifier T_FOLLOWS optional_var identifier T_IN rterm ')'
{ {
BeginFlowControlBlock(context, FlowControlContinue | FlowControlBreak, true); BeginFlowControlBlock(context, FlowControlContinue | FlowControlBreak, true);
} }
@ -611,11 +615,11 @@ lterm: T_LIBRARY rterm
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new ForExpression(*$3, *$5, std::unique_ptr<Expression>($7), std::unique_ptr<Expression>($10), @$); $$ = new ForExpression(*$4, *$7, std::unique_ptr<Expression>($9), std::unique_ptr<Expression>($12), @$);
delete $3; delete $4;
delete $5; delete $7;
} }
| T_FOR '(' identifier T_IN rterm ')' | T_FOR '(' optional_var identifier T_IN rterm ')'
{ {
BeginFlowControlBlock(context, FlowControlContinue | FlowControlBreak, true); BeginFlowControlBlock(context, FlowControlContinue | FlowControlBreak, true);
} }
@ -623,8 +627,8 @@ lterm: T_LIBRARY rterm
{ {
EndFlowControlBlock(context); EndFlowControlBlock(context);
$$ = new ForExpression(*$3, "", std::unique_ptr<Expression>($5), std::unique_ptr<Expression>($8), @$); $$ = new ForExpression(*$4, "", std::unique_ptr<Expression>($6), std::unique_ptr<Expression>($9), @$);
delete $3; delete $4;
} }
| T_FUNCTION identifier '(' identifier_items ')' use_specifier | T_FUNCTION identifier '(' identifier_items ')' use_specifier
{ {
@ -1103,24 +1107,24 @@ use_specifier_item: identifier
; ;
apply_for_specifier: /* empty */ apply_for_specifier: /* empty */
| T_FOR '(' identifier T_FOLLOWS identifier T_IN rterm ')' | T_FOR '(' optional_var identifier T_FOLLOWS optional_var identifier T_IN rterm ')'
{ {
context->m_FKVar.top() = *$3; context->m_FKVar.top() = *$4;
delete $3; delete $4;
context->m_FVVar.top() = *$5; context->m_FVVar.top() = *$7;
delete $5; delete $7;
context->m_FTerm.top() = $7; context->m_FTerm.top() = $9;
} }
| T_FOR '(' identifier T_IN rterm ')' | T_FOR '(' optional_var identifier T_IN rterm ')'
{ {
context->m_FKVar.top() = *$3; context->m_FKVar.top() = *$4;
delete $3; delete $4;
context->m_FVVar.top() = ""; context->m_FVVar.top() = "";
context->m_FTerm.top() = $5; context->m_FTerm.top() = $6;
} }
; ;