File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,22 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
114
114
115
115
ASSERT_NOT_REACHED ();
116
116
}
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 ();
117
133
}
118
134
119
135
ASSERT_NOT_REACHED ();
@@ -161,6 +177,24 @@ void BinaryExpression::dump(int indent) const
161
177
m_rhs->dump (indent + 1 );
162
178
}
163
179
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 ;
164
198
}
165
199
166
200
print_indent (indent);
Original file line number Diff line number Diff line change 27
27
#pragma once
28
28
29
29
#include < AK/NonnullOwnPtrVector.h>
30
+ #include < AK/OwnPtr.h>
30
31
#include < AK/String.h>
31
32
#include < LibJS/Forward.h>
32
33
#include < LibJS/Value.h>
@@ -152,6 +153,39 @@ class BinaryExpression : public Expression {
152
153
NonnullOwnPtr<Expression> m_rhs;
153
154
};
154
155
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
+
155
189
class Literal : public Expression {
156
190
public:
157
191
explicit Literal (Value value)
You can’t perform that action at this time.
0 commit comments