From 8aa55f1c0d34f0938dc3abc29f5cd1bfd705b487 Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sun, 14 Oct 2018 17:49:08 +0300 Subject: Minor fixes. --- JSImpl/src/Parser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'JSImpl/src/Parser.cpp') diff --git a/JSImpl/src/Parser.cpp b/JSImpl/src/Parser.cpp index 28fee72..9bf9922 100644 --- a/JSImpl/src/Parser.cpp +++ b/JSImpl/src/Parser.cpp @@ -930,7 +930,7 @@ ExpressionPtr Parser::Parse() ExpressionPtr Parser::FunctionDefinition() { - auto FormalParameters = [=](IPLVector identifiers) -> bool { + auto FormalParameters = [=](IPLVector& identifiers) -> bool { if (Match(TokenType::LeftParen)) { while (Match(TokenType::Identifier)) -- cgit v1.3.1-23-gd82d From 370d2afe025f899caab7829407363a37a37093b2 Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sun, 14 Oct 2018 18:09:45 +0300 Subject: Split literls in different classes and remove all handwritten code. --- JSImpl/src/ASTPrinter.cpp | 60 +++++++++++++++++++++--------------------- JSImpl/src/ASTPrinter.h | 6 ++++- JSImpl/src/Expression.cpp | 29 -------------------- JSImpl/src/Expression.h | 48 +++++++++++---------------------- JSImpl/src/ExpressionVisitor.h | 6 ++++- JSImpl/src/ExpressionsFwd.h | 7 +++-- JSImpl/src/Parser.cpp | 12 ++++----- 7 files changed, 67 insertions(+), 101 deletions(-) (limited to 'JSImpl/src/Parser.cpp') diff --git a/JSImpl/src/ASTPrinter.cpp b/JSImpl/src/ASTPrinter.cpp index 4dc07f4..864c4fc 100644 --- a/JSImpl/src/ASTPrinter.cpp +++ b/JSImpl/src/ASTPrinter.cpp @@ -85,26 +85,26 @@ void Print(std::ostream& os, ASTPrinter &printer, const char* } os << "\n"; } - -IPLString AsString(LiteralType type) -{ - switch (type) - { - case LiteralType::Number: - return "Number"; - case LiteralType::String: - return "String"; - case LiteralType::Boolean: - return "Boolean"; - case LiteralType::Null: - return "Null"; - case LiteralType::Undefined: - return "Undefined"; - default: - break; - } - return "Undefined"; -} +// +//IPLString AsString(LiteralType type) +//{ +// switch (type) +// { +// case LiteralType::Number: +// return "Number"; +// case LiteralType::String: +// return "String"; +// case LiteralType::Boolean: +// return "Boolean"; +// case LiteralType::Null: +// return "Null"; +// case LiteralType::Undefined: +// return "Undefined"; +// default: +// break; +// } +// return "Undefined"; +//} std::ostream& operator<<(std::ostream& os, const TokenType& t) { @@ -204,16 +204,16 @@ std::ostream& operator<<(std::ostream& os, const TokenType& t) return os; } -void ASTPrinter::Visit(LiteralExpression* e) -{ - os << "{\n" << "\"ExpresionType\": \"LiteralExpression\"," << '\n'; - Print(os, *this, "BooleanValue", e->GetBooleanValue(), false); - Print(os, *this, "LiteralType", AsString(e->GetLiteralType()), false); - Print(os, *this, "NumValue", e->GetNumValue(), false); - Print(os, *this, "StringValue", e->GetStringValue(), true); - os << "}\n"; -} - +//void ASTPrinter::Visit(LiteralExpression* e) +//{ +// os << "{\n" << "\"ExpresionType\": \"LiteralExpression\"," << '\n'; +// Print(os, *this, "BooleanValue", e->GetBooleanValue(), false); +// Print(os, *this, "LiteralType", AsString(e->GetLiteralType()), false); +// Print(os, *this, "NumValue", e->GetNumValue(), false); +// Print(os, *this, "StringValue", e->GetStringValue(), true); +// os << "}\n"; +//} +// #define MEMBERS_COUNT(type, name, def) ++membersCount; #define VISIT_MEMBER(type, name, def) ++currentVisitCount; Print(os, *this, #name, e->Get##name(), currentVisitCount == membersCount);; diff --git a/JSImpl/src/ASTPrinter.h b/JSImpl/src/ASTPrinter.h index 54a9090..9d74fa6 100644 --- a/JSImpl/src/ASTPrinter.h +++ b/JSImpl/src/ASTPrinter.h @@ -8,9 +8,13 @@ class ASTPrinter : public ExpressionVisitor public: ASTPrinter(std::ostream& os); ~ASTPrinter() {} + virtual void Visit(LiteralNull* e) override; + virtual void Visit(LiteralUndefined* e) override; + virtual void Visit(LiteralString* e) override; + virtual void Visit(LiteralNumber* e) override; + virtual void Visit(LiteralBoolean* e) override; virtual void Visit(BinaryExpression* e) override; virtual void Visit(UnaryExpression* e) override; - virtual void Visit(LiteralExpression* e) override; virtual void Visit(IdentifierExpression* e) override; virtual void Visit(ListExpression* e) override; virtual void Visit(VariableDefinitionExpression* e) override; diff --git a/JSImpl/src/Expression.cpp b/JSImpl/src/Expression.cpp index b8044e1..7153b3f 100644 --- a/JSImpl/src/Expression.cpp +++ b/JSImpl/src/Expression.cpp @@ -1,33 +1,4 @@ #include "Expression.h" #include -LiteralExpression::LiteralExpression(double v) - :m_Type(LiteralType::Number) - , m_NumValue(v) -{} -LiteralExpression::LiteralExpression(IPLString& v) - : m_Type(LiteralType::String) - , m_StringValue(v) -{} - -LiteralExpression::LiteralExpression(bool v) - : m_Type(LiteralType::Boolean) - , m_BooleanValue(v) -{} - -LiteralExpression::LiteralExpression(TokenType type) -{ - if (type == TokenType::Null) - { - m_Type = LiteralType::Null; - } - else if (type == TokenType::Undefined) - { - m_Type = LiteralType::Undefined; - } - else - { - assert(false); - } -} diff --git a/JSImpl/src/Expression.h b/JSImpl/src/Expression.h index df8c00b..30405f7 100644 --- a/JSImpl/src/Expression.h +++ b/JSImpl/src/Expression.h @@ -11,37 +11,6 @@ class Expression : public IPLEnableShared virtual void Accept(ExpressionVisitor& v) = 0; }; -enum class LiteralType -{ - Number, - String, - Boolean, - Null, - Undefined -}; - -class LiteralExpression : public Expression -{ -public: - - virtual ~LiteralExpression() {} - LiteralExpression(double); - LiteralExpression(IPLString&); - LiteralExpression(bool); - LiteralExpression(TokenType type); - virtual void Accept(ExpressionVisitor& v) override { v.Visit(this); } - double GetNumValue() const { return m_NumValue; } - const IPLString& GetStringValue() const { return m_StringValue; } - bool GetBooleanValue() const { return m_BooleanValue; } - LiteralType GetLiteralType() const { return m_Type; } -private: - LiteralType m_Type; - bool m_BooleanValue; - double m_NumValue; - IPLString m_StringValue; -}; - - #define EXPAND_ARGUMENT(type, name, def)\ type name = def, @@ -72,6 +41,17 @@ private: }; +#define NO_MEMBERS(MACRO) + +#define LITERAL_BOOLEAN_MEMBERS(MACRO)\ + MACRO(bool, Value, false) + +#define LITERAL_NUMBER_MEMBERS(MACRO)\ + MACRO(double, Value, 0.0) + +#define LITERAL_STRING_MEMBERS(MACRO)\ + MACRO(IPLString, Value, "") + #define BINARY_EXPRESSION_MEMBERS(MACRO)\ MACRO(ExpressionPtr, Left, nullptr)\ MACRO(ExpressionPtr, Right, nullptr)\ @@ -141,6 +121,11 @@ private: #define EMPTY_EXPRESSION_MEMBERS(MACRO) #define EXPRESSION_DEFINITION_ITERATOR(MACRO)\ + MACRO(LiteralNull, Expression, NO_MEMBERS)\ + MACRO(LiteralUndefined, Expression, NO_MEMBERS)\ + MACRO(LiteralBoolean, Expression, LITERAL_BOOLEAN_MEMBERS)\ + MACRO(LiteralNumber, Expression, LITERAL_NUMBER_MEMBERS)\ + MACRO(LiteralString, Expression, LITERAL_STRING_MEMBERS)\ MACRO(BinaryExpression, Expression, BINARY_EXPRESSION_MEMBERS)\ MACRO(UnaryExpression, Expression, UNARY_EXPRESSION_MEMBERS)\ MACRO(IdentifierExpression, Expression, IDENTIFIER_EXPRESSION_MEMBERS)\ @@ -157,7 +142,6 @@ private: MACRO(TopStatements, Expression, TOP_EXPRESSION_MEMBERS)\ MACRO(EmptyExpression, Expression, EMPTY_EXPRESSION_MEMBERS) - EXPRESSION_DEFINITION_ITERATOR(GENERATE_EXPRESSION); inline ExpressionPtr CreateEmptyExpression() { return IPLMakeSharePtr(); } diff --git a/JSImpl/src/ExpressionVisitor.h b/JSImpl/src/ExpressionVisitor.h index 044a38c..f35c1cb 100644 --- a/JSImpl/src/ExpressionVisitor.h +++ b/JSImpl/src/ExpressionVisitor.h @@ -6,9 +6,13 @@ class ExpressionVisitor { public: ~ExpressionVisitor() {} + virtual void Visit(LiteralNull* e) = 0; + virtual void Visit(LiteralUndefined* e) = 0; + virtual void Visit(LiteralString* e) = 0; + virtual void Visit(LiteralNumber* e) = 0; + virtual void Visit(LiteralBoolean* e) = 0; virtual void Visit(BinaryExpression* e) = 0; virtual void Visit(UnaryExpression* e) = 0; - virtual void Visit(LiteralExpression* e) = 0; virtual void Visit(IdentifierExpression* e) = 0; virtual void Visit(ListExpression* e) = 0; virtual void Visit(VariableDefinitionExpression* e) = 0; diff --git a/JSImpl/src/ExpressionsFwd.h b/JSImpl/src/ExpressionsFwd.h index 4de5bfa..f8db609 100644 --- a/JSImpl/src/ExpressionsFwd.h +++ b/JSImpl/src/ExpressionsFwd.h @@ -1,8 +1,11 @@ #pragma once #include "CommonTypes.h" -#include "CommonTypes.h" - +class LiteralNull; +class LiteralUndefined; +class LiteralNumber; +class LiteralString; +class LiteralBoolean; class Expression; class BinaryExpression; class UnaryExpression; diff --git a/JSImpl/src/Parser.cpp b/JSImpl/src/Parser.cpp index 9bf9922..09166db 100644 --- a/JSImpl/src/Parser.cpp +++ b/JSImpl/src/Parser.cpp @@ -248,29 +248,29 @@ ExpressionPtr Parser::SimpleExpression() // | ArrayLiteral if (Match(TokenType::Number)) { - return IPLMakeSharePtr(Prev().Number); + return IPLMakeSharePtr(Prev().Number); } else if (Match(TokenType::String)) { - return IPLMakeSharePtr(Prev().Lexeme); + return IPLMakeSharePtr(Prev().Lexeme); } else if (Match(TokenType::Null)) { - return IPLMakeSharePtr(Prev().Type); + return IPLMakeSharePtr(); } else if (Match(TokenType::Undefined)) { - return IPLMakeSharePtr(Prev().Type); + return IPLMakeSharePtr(); } else if (Match(TokenType::True)) { bool t = true; - return IPLMakeSharePtr(t); + return IPLMakeSharePtr(t); } else if (Match(TokenType::False)) { bool f = false; - return IPLMakeSharePtr(f); + return IPLMakeSharePtr(f); } else if (Match(TokenType::Identifier)) { -- cgit v1.3.1-23-gd82d