Skip to content

Instantly share code, notes, and snippets.

@juanarbol
Created February 8, 2024 01:37
Show Gist options
  • Save juanarbol/2ce5d2ca75016d3b1103a1bb546d0953 to your computer and use it in GitHub Desktop.
Save juanarbol/2ce5d2ca75016d3b1103a1bb546d0953 to your computer and use it in GitHub Desktop.
V8 loop syntax diff
diff --git a/src/parsing/keywords.txt b/src/parsing/keywords.txt
index 5ed039459e2..9872a7bad09 100644
--- a/src/parsing/keywords.txt
+++ b/src/parsing/keywords.txt
@@ -43,6 +43,7 @@ in, Token::IN
instanceof, Token::INSTANCEOF
interface, Token::FUTURE_STRICT_RESERVED_WORD
let, Token::LET
+loop, Token::LOOP
new, Token::NEW
null, Token::NULL_LITERAL
of, Token::OF
diff --git a/src/parsing/parser-base.h b/src/parsing/parser-base.h
index be62bca423d..1acb4d56fdd 100644
--- a/src/parsing/parser-base.h
+++ b/src/parsing/parser-base.h
@@ -1376,6 +1376,8 @@ class ParserBase {
StatementT ParseThrowStatement();
StatementT ParseSwitchStatement(ZonePtrList<const AstRawString>* labels);
V8_INLINE StatementT ParseTryStatement();
+ StatementT ParseLoopStatement(ZonePtrList<const AstRawString>* labels,
+ ZonePtrList<const AstRawString>* own_labels);
StatementT ParseForStatement(ZonePtrList<const AstRawString>* labels,
ZonePtrList<const AstRawString>* own_labels);
StatementT ParseForEachStatementWithDeclarations(
@@ -5478,6 +5480,8 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseStatement(
return ParseForAwaitStatement(labels, own_labels);
}
return ParseForStatement(labels, own_labels);
+ case Token::LOOP:
+ return ParseLoopStatement(labels, own_labels);
case Token::CONTINUE:
return ParseContinueStatement();
case Token::BREAK:
@@ -6198,6 +6202,37 @@ typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseTryStatement() {
pos);
}
+template <typename Impl>
+typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseLoopStatement(
+ ZonePtrList<const AstRawString>* labels,
+ ZonePtrList<const AstRawString>* own_labels) {
+ // LoopStatement ::
+ // 'loop' Statement ';'
+ typename FunctionState::LoopScope loop_scope(function_state_);
+
+ // Reuse the same 'while' loop statements.
+ // This is gonna be a while (1) loop.
+ auto loop = factory()->NewWhileStatement(peek_position());
+ Target target(this, loop, labels, own_labels, Target::TARGET_FOR_ANONYMOUS);
+
+ SourceRange body_range;
+ StatementT body = impl()->NullStatement();
+
+ Consume(Token::LOOP);
+ // Create an infinite loop, condition will be something like a while (1).
+ ExpressionT cond = factory()->NewNumberLiteral(1, peek_position());
+ {
+ // Parse the body of the loop.
+ SourceRangeScope range_scope(scanner(), &body_range);
+ body = ParseStatement(nullptr, nullptr);
+ }
+
+ loop->Initialize(cond, body);
+ impl()->RecordIterationStatementSourceRange(loop, body_range);
+
+ return loop;
+}
+
template <typename Impl>
typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForStatement(
ZonePtrList<const AstRawString>* labels,
diff --git a/src/parsing/scanner-inl.h b/src/parsing/scanner-inl.h
index 18896852677..1603dd1305a 100644
--- a/src/parsing/scanner-inl.h
+++ b/src/parsing/scanner-inl.h
@@ -53,6 +53,7 @@ namespace internal {
KEYWORD("instanceof", Token::INSTANCEOF) \
KEYWORD("interface", Token::FUTURE_STRICT_RESERVED_WORD) \
KEYWORD_GROUP('l') \
+ KEYWORD("loop", Token::LOOP) \
KEYWORD("let", Token::LET) \
KEYWORD_GROUP('n') \
KEYWORD("new", Token::NEW) \
diff --git a/src/parsing/token.h b/src/parsing/token.h
index c6c9354489c..ad4a7853034 100644
--- a/src/parsing/token.h
+++ b/src/parsing/token.h
@@ -144,6 +144,7 @@ namespace internal {
K(ELSE, "else", 0) \
K(FINALLY, "finally", 0) \
K(FOR, "for", 0) \
+ K(LOOP, "loop", 0) \
K(FUNCTION, "function", 0) \
K(IF, "if", 0) \
/* IN */ \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment