瀏覽代碼

LibJS: Add SequenceExpression AST node (comma operator)

This patch only adds the AST node, the parser doesn't create them yet.
Andreas Kling 5 年之前
父節點
當前提交
19cbfaee54
共有 2 個文件被更改,包括 34 次插入0 次删除
  1. 18 0
      Libraries/LibJS/AST.cpp
  2. 16 0
      Libraries/LibJS/AST.h

+ 18 - 0
Libraries/LibJS/AST.cpp

@@ -1133,4 +1133,22 @@ void ConditionalExpression::dump(int indent) const
     m_test->dump(indent + 1);
 }
 
+void SequenceExpression::dump(int indent) const
+{
+    ASTNode::dump(indent);
+    for (auto& expression : m_expressions)
+        expression.dump(indent + 1);
+}
+
+Value SequenceExpression::execute(Interpreter& interpreter) const
+{
+    Value last_value;
+    for (auto& expression : m_expressions) {
+        last_value = expression.execute(interpreter);
+        if (interpreter.exception())
+            return {};
+    }
+    return last_value;
+}
+
 }

+ 16 - 0
Libraries/LibJS/AST.h

@@ -403,6 +403,22 @@ private:
     NonnullRefPtr<Expression> m_lhs;
 };
 
+class SequenceExpression final : public Expression {
+public:
+    SequenceExpression(NonnullRefPtrVector<Expression> expressions)
+        : m_expressions(move(expressions))
+    {
+    }
+
+    virtual void dump(int indent) const override;
+    virtual Value execute(Interpreter&) const override;
+
+private:
+    virtual const char* class_name() const override { return "SequenceExpression"; }
+
+    NonnullRefPtrVector<Expression> m_expressions;
+};
+
 class Literal : public Expression {
 protected:
     explicit Literal() {}