Skip to content

Instantly share code, notes, and snippets.

@goranmoomin
Created November 22, 2019 01:08
Show Gist options
  • Save goranmoomin/a576229789890f29325b733f4bc3bfdc to your computer and use it in GitHub Desktop.
Save goranmoomin/a576229789890f29325b733f4bc3bfdc to your computer and use it in GitHub Desktop.
enforestForStatement
enforestForStatement() {
this.matchKeyword("for");
const condition = this.matchParens();
const conditionEnforester = new Enforester(condition);
if(conditionEnforester.isPunctuator(conditionEnforester.peek(), ";")) {
conditionEnforester.advance();
const test = conditionEnforester.isPunctuator(conditionEnforester.peek(), ";") ? null : conditionEnforester.enforestExpression();
conditionEnforester.matchPunctuator(";");
const update = conditionEnforester.terms.length === 0 ? null : conditionEnforester.enforestExpression();
const body = this.enforestStatement();
return new AST.ForStatement({
init: null,
test,
update,
body
});
} else {
// TODO
const lookahead = conditionEnforester.peek();
if(conditionEnforester.isKeyword(lookahead, "var") ||
conditionEnforester.isKeyword(lookahead, "let") ||
conditionEnforester.isKeyword(lookahead, "const")) {
const init = conditionEnforester.enforestVariableDeclaration();
const lookahead = conditionEnforester.peek();
if(this.isKeyword(lookahead, "in") ||
this.isKeyword(lookahead, "of") ||
this.isKeyword(lookahead, "in")) {
conditionEnforester.advance();
const right = conditionEnforester.enforestExpression();
const body = this.enforestStatement();
return new AST.ForInStatement({
left: init,
right,
body
});
} else if (this.isIdentifier(lookahead, "of")) {
conditionEnforester.advance();
const right = conditionEnforester.enforestExpression();
const body = this.enforestStatement();
return new AST.ForOfStatement({
left: init,
right,
body
});
}
conditionEnforester.matchPunctuator(";");
if(conditionEnforester.isPunctuator(conditionEnforester.peek(), ";")) {
conditionEnforest.advance();
return new AST.ForStatement({
init,
test: null,
update: conditionEnforester.enforestExpression(),
body: this.enforestStatement()
});
} else {
if(this.isKeyword(conditionEnforester.peek(1), "in") ||
this.isIdentifier(conditionEnforester.peek(1), "of")) {
const left = conditionEnforester.enforestBindingIdentifier();
const right = conditionEnforester.enforestExpression();
const body = this.enforestStatement();
if(this.isKeyword(conditionEnforester.advance(), "in")) {
return new AST.ForInStatement({ left, right, body });
} else {
return new AST.ForOfStatement({ left, right, body });
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment