Skip to content

Commit

Permalink
allow for loops without variable
Browse files Browse the repository at this point in the history
  • Loading branch information
SenkJu committed Oct 23, 2020
1 parent 77f10c3 commit cb2b8f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/compiler/Compiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ class Compiler {
emit(OpCode.LoadIndex, node.position, []);
emit(OpCode.Call, node.position, [0]);
symbolTable.newScope();
compile(cFor.variable);
if (cFor.variable != null) {
compile(cFor.variable);
} else {
emit(OpCode.Pop, node.position, []);
}
compile(cFor.block);
symbolTable.setParent();
emit(OpCode.Jump, node.position, [jumpPos]);
Expand Down
25 changes: 13 additions & 12 deletions src/parser/Parser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -361,27 +361,28 @@ class Parser {

nextToken();

if (currentToken.type != TokenType.Let && currentToken.type != TokenType.Mut) {
error.unexpectedToken(currentToken, "`let` or `mut`");
}

final mutable = currentToken.type == TokenType.Mut;
nextToken();
final variableName = parseVariableName();
final variable = if (currentToken.type == TokenType.Let || currentToken.type == TokenType.Mut) {
final mutable = currentToken.type == TokenType.Mut;
nextToken();
final variableName = parseVariableName();

nextToken();
assertToken(TokenType.In, "`in`");
nextToken();

nextToken();
assertToken(TokenType.In, "`in`");
nextToken();
new VariableNode(nodePos, variableName, null, mutable);
} else {
null;
}

final iterator = expressionParser.parseExpression();

assertToken(TokenType.LBrace, "`{`");

final block = parseBlock();

nextToken();

return new ForNode(nodePos, new VariableNode(nodePos, variableName, null, mutable), iterator, block);
return new ForNode(nodePos, variable, iterator, block);
}

function parseVariableAssign():VariableAssignNode {
Expand Down

0 comments on commit cb2b8f5

Please sign in to comment.