mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 05:05:19 -05:00
patterns: Added break and continue statements for loops
This commit is contained in:
@@ -146,6 +146,7 @@ namespace hex::pl {
|
||||
std::vector<PatternData*> patterns;
|
||||
|
||||
try {
|
||||
this->setCurrentControlFlowStatement(ControlFlowStatement::None);
|
||||
pushScope(nullptr, patterns);
|
||||
|
||||
for (auto node : ast) {
|
||||
|
||||
@@ -420,6 +420,10 @@ namespace hex::pl {
|
||||
tokens.emplace_back(TOKEN(Keyword, In));
|
||||
else if (identifier == "out")
|
||||
tokens.emplace_back(TOKEN(Keyword, Out));
|
||||
else if (identifier == "break")
|
||||
tokens.emplace_back(TOKEN(Keyword, Break));
|
||||
else if (identifier == "continue")
|
||||
tokens.emplace_back(TOKEN(Keyword, Continue));
|
||||
|
||||
// Check for built-in types
|
||||
else if (identifier == "u8")
|
||||
|
||||
@@ -496,8 +496,8 @@ namespace hex::pl {
|
||||
|
||||
if (MATCHES(sequence(IDENTIFIER, OPERATOR_ASSIGNMENT)))
|
||||
statement = parseFunctionVariableAssignment();
|
||||
else if (MATCHES(sequence(KEYWORD_RETURN)))
|
||||
statement = parseFunctionReturnStatement();
|
||||
else if (MATCHES(oneOf(KEYWORD_RETURN, KEYWORD_BREAK, KEYWORD_CONTINUE)))
|
||||
statement = parseFunctionControlFlowStatement();
|
||||
else if (MATCHES(sequence(KEYWORD_IF, SEPARATOR_ROUNDBRACKETOPEN))) {
|
||||
statement = parseFunctionConditional();
|
||||
needsSemicolon = false;
|
||||
@@ -546,11 +546,21 @@ namespace hex::pl {
|
||||
return create(new ASTNodeAssignment(lvalue, rvalue));
|
||||
}
|
||||
|
||||
ASTNode* Parser::parseFunctionReturnStatement() {
|
||||
if (peek(SEPARATOR_ENDOFEXPRESSION))
|
||||
return create(new ASTNodeReturnStatement(nullptr));
|
||||
ASTNode* Parser::parseFunctionControlFlowStatement() {
|
||||
ControlFlowStatement type;
|
||||
if (peek(KEYWORD_RETURN, -1))
|
||||
type = ControlFlowStatement::Return;
|
||||
else if (peek(KEYWORD_BREAK, -1))
|
||||
type = ControlFlowStatement::Break;
|
||||
else if (peek(KEYWORD_CONTINUE, -1))
|
||||
type = ControlFlowStatement::Continue;
|
||||
else
|
||||
return create(new ASTNodeReturnStatement(this->parseMathematicalExpression()));
|
||||
throwParseError("invalid control flow statement. Expected 'return', 'break' or 'continue'");
|
||||
|
||||
if (peek(SEPARATOR_ENDOFEXPRESSION))
|
||||
return create(new ASTNodeControlFlowStatement(type, nullptr));
|
||||
else
|
||||
return create(new ASTNodeControlFlowStatement(type, this->parseMathematicalExpression()));
|
||||
}
|
||||
|
||||
std::vector<ASTNode*> Parser::parseStatementBody() {
|
||||
@@ -650,14 +660,12 @@ namespace hex::pl {
|
||||
|
||||
body = parseStatementBody();
|
||||
|
||||
body.push_back(postExpression);
|
||||
|
||||
variableCleanup.release();
|
||||
conditionCleanup.release();
|
||||
postExpressionCleanup.release();
|
||||
bodyCleanup.release();
|
||||
|
||||
return create(new ASTNodeCompoundStatement({ variable, create(new ASTNodeWhileStatement(condition, body)) }, true));
|
||||
return create(new ASTNodeCompoundStatement({ variable, create(new ASTNodeWhileStatement(condition, body, postExpression)) }, true));
|
||||
}
|
||||
|
||||
/* Control flow */
|
||||
|
||||
Reference in New Issue
Block a user