mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 13:55:07 +00:00
LibJS: Implement While statements
This commit is contained in:
parent
4dee1ec0be
commit
1c83c5ed08
2 changed files with 41 additions and 0 deletions
|
@ -71,6 +71,16 @@ Value IfStatement::execute(Interpreter& interpreter) const
|
||||||
return interpreter.run(*m_alternate);
|
return interpreter.run(*m_alternate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value WhileStatement::execute(Interpreter& interpreter) const
|
||||||
|
{
|
||||||
|
Value last_value = js_undefined();
|
||||||
|
while (m_predicate->execute(interpreter).as_bool()) {
|
||||||
|
last_value = interpreter.run(*m_body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return last_value;
|
||||||
|
}
|
||||||
|
|
||||||
Value add(Value lhs, Value rhs)
|
Value add(Value lhs, Value rhs)
|
||||||
{
|
{
|
||||||
ASSERT(lhs.is_number());
|
ASSERT(lhs.is_number());
|
||||||
|
@ -259,4 +269,14 @@ void IfStatement::dump(int indent) const
|
||||||
alternate().dump(indent + 1);
|
alternate().dump(indent + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WhileStatement::dump(int indent) const
|
||||||
|
{
|
||||||
|
ASTNode::dump(indent);
|
||||||
|
|
||||||
|
print_indent(indent);
|
||||||
|
printf("While\n");
|
||||||
|
predicate().dump(indent + 1);
|
||||||
|
body().dump(indent + 1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,27 @@ private:
|
||||||
NonnullOwnPtr<ScopeNode> m_alternate;
|
NonnullOwnPtr<ScopeNode> m_alternate;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class WhileStatement : public ASTNode {
|
||||||
|
public:
|
||||||
|
WhileStatement(NonnullOwnPtr<Expression> predicate, NonnullOwnPtr<ScopeNode> body)
|
||||||
|
: m_predicate(move(predicate))
|
||||||
|
, m_body(move(body))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
const Expression& predicate() const { return *m_predicate; }
|
||||||
|
const ScopeNode& body() const { return *m_body; }
|
||||||
|
|
||||||
|
virtual Value execute(Interpreter&) const override;
|
||||||
|
virtual void dump(int indent) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual const char* class_name() const override { return "WhileStatement"; }
|
||||||
|
|
||||||
|
NonnullOwnPtr<Expression> m_predicate;
|
||||||
|
NonnullOwnPtr<ScopeNode> m_body;
|
||||||
|
};
|
||||||
|
|
||||||
enum class BinaryOp {
|
enum class BinaryOp {
|
||||||
Plus,
|
Plus,
|
||||||
Minus,
|
Minus,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue