Skip to content

Commit a96bf2c

Browse files
0xtechnobabbleawesomekling
authored andcommitted
LibJS: Implement logical expressions
Logical expressions are expressions which can return either true or false according to a provided condition.
1 parent 4e62dcd commit a96bf2c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Libraries/LibJS/AST.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
114114

115115
ASSERT_NOT_REACHED();
116116
}
117+
118+
Value LogicalExpression::execute(Interpreter& interpreter) const
119+
{
120+
auto lhs_result = m_lhs->execute(interpreter).as_bool();
121+
122+
if (m_op == LogicalOp::Not)
123+
return Value(!lhs_result);
124+
125+
auto rhs_result = m_rhs->execute(interpreter).as_bool();
126+
switch (m_op) {
127+
case LogicalOp::And:
128+
return Value(lhs_result && rhs_result);
129+
case LogicalOp::Or:
130+
return Value(lhs_result || rhs_result);
131+
case LogicalOp::Not:
132+
ASSERT_NOT_REACHED();
117133
}
118134

119135
ASSERT_NOT_REACHED();
@@ -161,6 +177,24 @@ void BinaryExpression::dump(int indent) const
161177
m_rhs->dump(indent + 1);
162178
}
163179

180+
void LogicalExpression::dump(int indent) const
181+
{
182+
const char* op_string = nullptr;
183+
switch (m_op) {
184+
case LogicalOp::And:
185+
op_string = "&&";
186+
break;
187+
case LogicalOp::Or:
188+
op_string = "||";
189+
break;
190+
case LogicalOp::Not:
191+
op_string = "!";
192+
print_indent(indent);
193+
printf("%s\n", class_name());
194+
print_indent(indent + 1);
195+
printf("%s\n", op_string);
196+
m_lhs->dump(indent + 1);
197+
return;
164198
}
165199

166200
print_indent(indent);

Libraries/LibJS/AST.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#pragma once
2828

2929
#include <AK/NonnullOwnPtrVector.h>
30+
#include <AK/OwnPtr.h>
3031
#include <AK/String.h>
3132
#include <LibJS/Forward.h>
3233
#include <LibJS/Value.h>
@@ -152,6 +153,39 @@ class BinaryExpression : public Expression {
152153
NonnullOwnPtr<Expression> m_rhs;
153154
};
154155

156+
enum class LogicalOp {
157+
And,
158+
Or,
159+
Not
160+
};
161+
162+
class LogicalExpression : public Expression {
163+
public:
164+
LogicalExpression(LogicalOp op, NonnullOwnPtr<Expression> lhs, NonnullOwnPtr<Expression> rhs)
165+
: m_op(op)
166+
, m_lhs(move(lhs))
167+
, m_rhs(move(rhs))
168+
{
169+
}
170+
171+
LogicalExpression(LogicalOp op, NonnullOwnPtr<Expression> lhs)
172+
: m_op(op)
173+
, m_lhs(move(lhs))
174+
{
175+
ASSERT(op == LogicalOp::Not);
176+
}
177+
178+
virtual Value execute(Interpreter&) const override;
179+
virtual void dump(int indent) const override;
180+
181+
private:
182+
virtual const char* class_name() const override { return "LogicalExpression"; }
183+
184+
LogicalOp m_op;
185+
NonnullOwnPtr<Expression> m_lhs;
186+
OwnPtr<Expression> m_rhs;
187+
};
188+
155189
class Literal : public Expression {
156190
public:
157191
explicit Literal(Value value)

0 commit comments

Comments
 (0)