diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index f5fd51e3660..38e44e1edb8 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -970,4 +970,34 @@ void SwitchCase::dump(int indent) const } } +Value ConditionalExpression::execute(Interpreter& interpreter) const +{ + auto test_result = m_test->execute(interpreter); + if (interpreter.exception()) + return {}; + Value result; + if (test_result.to_boolean()) { + result = m_consequent->execute(interpreter); + } else { + result = m_alternate->execute(interpreter); + } + if (interpreter.exception()) + return {}; + return result; +} + +void ConditionalExpression::dump(int indent) const +{ + ASTNode::dump(indent); + print_indent(indent); + printf("(Test)\n"); + m_test->dump(indent + 1); + print_indent(indent); + printf("(Consequent)\n"); + m_test->dump(indent + 1); + print_indent(indent); + printf("(Alternate)\n"); + m_test->dump(indent + 1); +} + } diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index 5bc59aeca87..2c1d13b3865 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -639,6 +639,26 @@ private: bool m_computed { false }; }; +class ConditionalExpression final : public Expression { +public: + ConditionalExpression(NonnullRefPtr test, NonnullRefPtr consequent, NonnullRefPtr alternate) + : m_test(move(test)) + , m_consequent(move(consequent)) + , m_alternate(move(alternate)) + { + } + + virtual void dump(int indent) const override; + virtual Value execute(Interpreter&) const override; + +private: + virtual const char* class_name() const override { return "ConditionalExpression"; } + + NonnullRefPtr m_test; + NonnullRefPtr m_consequent; + NonnullRefPtr m_alternate; +}; + class CatchClause final : public ASTNode { public: CatchClause(const FlyString& parameter, NonnullRefPtr body) diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 31da30916b5..da735be71e9 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -516,6 +516,8 @@ NonnullRefPtr Parser::parse_secondary_expression(NonnullRefPtr(LogicalOp::Or, move(lhs), parse_expression(min_precedence, associativity)); + case TokenType::QuestionMark: + return parse_conditional_expression(move(lhs)); default: m_parser_state.m_has_errors = true; expected("secondary expression (missing switch case)"); @@ -660,6 +662,15 @@ NonnullRefPtr Parser::parse_break_statement() return create_ast_node(); } +NonnullRefPtr Parser::parse_conditional_expression(NonnullRefPtr test) +{ + consume(TokenType::QuestionMark); + auto consequent = parse_expression(0); + consume(TokenType::Colon); + auto alternate = parse_expression(0); + return create_ast_node(move(test), move(consequent), move(alternate)); +} + NonnullRefPtr Parser::parse_try_statement() { consume(TokenType::Try); @@ -865,7 +876,8 @@ bool Parser::match_secondary_expression() const || type == TokenType::BracketOpen || type == TokenType::PlusPlus || type == TokenType::MinusMinus - || type == TokenType::Instanceof; + || type == TokenType::Instanceof + || type == TokenType::QuestionMark; } bool Parser::match_statement() const diff --git a/Libraries/LibJS/Parser.h b/Libraries/LibJS/Parser.h index c929672fbeb..f4e759d4f53 100644 --- a/Libraries/LibJS/Parser.h +++ b/Libraries/LibJS/Parser.h @@ -58,6 +58,7 @@ public: NonnullRefPtr parse_switch_statement(); NonnullRefPtr parse_switch_case(); NonnullRefPtr parse_break_statement(); + NonnullRefPtr parse_conditional_expression(NonnullRefPtr test); NonnullRefPtr parse_expression(int min_precedence, Associativity associate = Associativity::Right); NonnullRefPtr parse_primary_expression(); diff --git a/Libraries/LibJS/Tests/ternary-basic.js b/Libraries/LibJS/Tests/ternary-basic.js new file mode 100644 index 00000000000..62de660275f --- /dev/null +++ b/Libraries/LibJS/Tests/ternary-basic.js @@ -0,0 +1,19 @@ +function assert(x) { if (!x) throw 1; } + +try { + var x = 1; + + assert(x === 1 ? true : false); + assert(x ? x : 0); + assert(1 < 2 ? (true) : (false)); + + var o = {}; + o.f = true; + assert(o.f ? true : false); + + assert(1 ? o.f : null); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +}