mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 08:37:34 +00:00
LibJS: Implement if statements
If statements execute a certain action or an alternative one depending on whether the tested condition is true or false, this commit helps establish basic control flow capabilities in the AST.
This commit is contained in:
parent
a96bf2c22e
commit
b6307beb6e
2 changed files with 47 additions and 0 deletions
|
@ -61,6 +61,16 @@ Value ReturnStatement::execute(Interpreter& interpreter) const
|
|||
return value;
|
||||
}
|
||||
|
||||
Value IfStatement::execute(Interpreter& interpreter) const
|
||||
{
|
||||
auto predicate_result = m_predicate->execute(interpreter);
|
||||
|
||||
if (predicate_result.as_bool())
|
||||
return interpreter.run(*m_consequent);
|
||||
else
|
||||
return interpreter.run(*m_alternate);
|
||||
}
|
||||
|
||||
Value add(Value lhs, Value rhs)
|
||||
{
|
||||
ASSERT(lhs.is_number());
|
||||
|
@ -236,4 +246,17 @@ void ReturnStatement::dump(int indent) const
|
|||
argument().dump(indent + 1);
|
||||
}
|
||||
|
||||
void IfStatement::dump(int indent) const
|
||||
{
|
||||
ASTNode::dump(indent);
|
||||
|
||||
print_indent(indent);
|
||||
printf("If\n");
|
||||
predicate().dump(indent + 1);
|
||||
consequent().dump(indent + 1);
|
||||
print_indent(indent);
|
||||
printf("Else\n");
|
||||
alternate().dump(indent + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue