diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index da8215f497..2d36cbfe8e 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -262,10 +263,22 @@ void CallExpression::dump(int indent) const printf("%s '%s'\n", class_name(), name().characters()); } -void Literal::dump(int indent) const +void StringLiteral::dump(int indent) const { print_indent(indent); - printf("Literal _%s_\n", m_value.to_string().characters()); + printf("StringLiteral \"%s\"\n", m_value.characters()); +} + +void NumericLiteral::dump(int indent) const +{ + print_indent(indent); + printf("NumberLiteral %g\n", m_value); +} + +void BooleanLiteral::dump(int indent) const +{ + print_indent(indent); + printf("BooleanLiteral %s\n", m_value ? "true" : "false"); } void FunctionDeclaration::dump(int indent) const @@ -415,4 +428,19 @@ Value MemberExpression::execute(Interpreter& interpreter) const return object_result.as_object()->get(property_name); } +Value StringLiteral::execute(Interpreter& interpreter) const +{ + return Value(js_string(interpreter.heap(), m_value)); +} + +Value NumericLiteral::execute(Interpreter&) const +{ + return Value(m_value); +} + +Value BooleanLiteral::execute(Interpreter&) const +{ + return Value(m_value); +} + } diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index 97d318dbb6..8a01f4659c 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -290,19 +290,56 @@ private: }; class Literal : public Expression { +protected: + explicit Literal() {} +}; + +class BooleanLiteral final : public Literal { public: - explicit Literal(Value value) + explicit BooleanLiteral(bool value) + : m_value(value) + { + } + + virtual Value execute(Interpreter&) const override; + virtual void dump(int indent) const override; + +private: + virtual const char* class_name() const override { return "BooleanLiteral"; } + + bool m_value { false }; +}; + +class NumericLiteral final : public Literal { +public: + explicit NumericLiteral(double value) + : m_value(value) + { + } + + virtual Value execute(Interpreter&) const override; + virtual void dump(int indent) const override; + +private: + virtual const char* class_name() const override { return "NumericLiteral"; } + + double m_value { 0 }; +}; + +class StringLiteral final : public Literal { +public: + explicit StringLiteral(String value) : m_value(move(value)) { } - virtual Value execute(Interpreter&) const override { return m_value; } + virtual Value execute(Interpreter&) const override; virtual void dump(int indent) const override; private: - virtual const char* class_name() const override { return "Literal"; } + virtual const char* class_name() const override { return "StringLiteral"; } - Value m_value; + String m_value; }; class Identifier final : public Expression { diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 194bc01ddd..1cfc3be231 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -86,9 +86,9 @@ NonnullOwnPtr Parser::parse_primary_expression() case TokenType::Identifier: return make(consume().value()); case TokenType::NumericLiteral: - return make(Value(consume().double_value())); + return make(consume().double_value()); case TokenType::BoolLiteral: - return make(Value(consume().bool_value())); + return make(consume().bool_value()); case TokenType::CurlyOpen: return parse_object_expression(); default: