mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +00:00
LibCpp: Parse If statements
This commit is contained in:
parent
e8f040139b
commit
8ed65d7b48
4 changed files with 70 additions and 0 deletions
|
@ -379,4 +379,32 @@ NonnullRefPtrVector<Declaration> BlockStatement::declarations() const
|
|||
return declarations;
|
||||
}
|
||||
|
||||
void IfStatement::dump(size_t indent) const
|
||||
{
|
||||
ASTNode::dump(indent);
|
||||
if (m_predicate) {
|
||||
print_indent(indent + 1);
|
||||
dbgprintf("Predicate:\n");
|
||||
m_predicate->dump(indent + 1);
|
||||
}
|
||||
if (m_then) {
|
||||
print_indent(indent + 1);
|
||||
dbgprintf("Then:\n");
|
||||
m_then->dump(indent + 1);
|
||||
}
|
||||
if (m_else) {
|
||||
print_indent(indent + 1);
|
||||
dbgprintf("Else:\n");
|
||||
m_else->dump(indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
NonnullRefPtrVector<Declaration> IfStatement::declarations() const
|
||||
{
|
||||
NonnullRefPtrVector<Declaration> declarations;
|
||||
declarations.append(m_predicate->declarations());
|
||||
declarations.append(m_then->declarations());
|
||||
declarations.append(m_else->declarations());
|
||||
return declarations;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -581,4 +581,22 @@ public:
|
|||
virtual ~Comment() override = default;
|
||||
virtual const char* class_name() const override { return "Comment"; }
|
||||
};
|
||||
|
||||
class IfStatement : public Statement {
|
||||
public:
|
||||
IfStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end)
|
||||
: Statement(parent, start, end)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~IfStatement() override = default;
|
||||
virtual const char* class_name() const override { return "IfStatement"; }
|
||||
virtual void dump(size_t indent) const override;
|
||||
virtual NonnullRefPtrVector<Declaration> declarations() const override;
|
||||
|
||||
RefPtr<Expression> m_predicate;
|
||||
RefPtr<Statement> m_then;
|
||||
RefPtr<Statement> m_else;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -186,6 +186,10 @@ NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent)
|
|||
if (match_keyword("for")) {
|
||||
consume_semicolumn.disarm();
|
||||
return parse_for_statement(parent);
|
||||
}
|
||||
if (match_keyword("if")) {
|
||||
consume_semicolumn.disarm();
|
||||
return parse_if_statement(parent);
|
||||
} else {
|
||||
error("unexpected statement type");
|
||||
consume_semicolumn.disarm();
|
||||
|
@ -997,4 +1001,23 @@ NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
|
|||
return for_statement;
|
||||
}
|
||||
|
||||
NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
|
||||
{
|
||||
SCOPE_LOGGER();
|
||||
auto if_statement = create_ast_node<IfStatement>(parent, position(), {});
|
||||
consume(Token::Type::Keyword);
|
||||
consume(Token::Type::LeftParen);
|
||||
if_statement->m_predicate = parse_expression(*if_statement);
|
||||
consume(Token::Type::RightParen);
|
||||
if_statement->m_then = parse_statement(*if_statement);
|
||||
if (match_keyword("else")) {
|
||||
consume(Token::Type::Keyword);
|
||||
if_statement->m_else = parse_statement(*if_statement);
|
||||
if_statement->set_end(if_statement->m_else->end());
|
||||
} else {
|
||||
if_statement->set_end(if_statement->m_then->end());
|
||||
}
|
||||
return if_statement;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -104,6 +104,7 @@ private:
|
|||
NonnullRefPtr<ForStatement> parse_for_statement(ASTNode& parent);
|
||||
NonnullRefPtr<BlockStatement> parse_block_statement(ASTNode& parent);
|
||||
NonnullRefPtr<Comment> parse_comment(ASTNode& parent);
|
||||
NonnullRefPtr<IfStatement> parse_if_statement(ASTNode& parent);
|
||||
|
||||
bool match(Token::Type);
|
||||
Token consume(Token::Type);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue