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/ASTPrinter.cpp | 5 +++-- JSImpl/src/Parser.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'JSImpl/src') diff --git a/JSImpl/src/ASTPrinter.cpp b/JSImpl/src/ASTPrinter.cpp index d8f5ff8..4dc07f4 100644 --- a/JSImpl/src/ASTPrinter.cpp +++ b/JSImpl/src/ASTPrinter.cpp @@ -36,12 +36,13 @@ void Print(std::ostream& os, ASTPrinter &printer, const char* name, const IPLVec void Print(std::ostream& os, ASTPrinter &printer, const char* name, const IPLVector& list, bool isLast) { - os << "\"" << name << "\" : [ \n"; + os << "\"" << name << "\" : ["; for (auto i = 0; i < list.size(); ++i) { + os << "\"" << list[i] << "\""; if (i + 1 != list.size()) { - os << list[i] << ","; + os << ","; } } os << "]"; 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 a24ea4d6be0a92f9d2d8e937d08f6ea5575021ef Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sun, 14 Oct 2018 17:49:22 +0300 Subject: Clean up main.cpp --- JSImpl/src/main.cpp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'JSImpl/src') diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp index 4263bfa..b51d1a9 100644 --- a/JSImpl/src/main.cpp +++ b/JSImpl/src/main.cpp @@ -1,12 +1,11 @@ #include "Lexer.h" #include "Parser.h" +#include "ASTPrinter.h" #include #define LOG(msg) std::cout << msg << std::endl #define EXECUTE_TEST(test) std::cout << #test << " "; test(); #define CHECK(cond) if (cond) {LOG("PASS");} else {LOG(#cond " FAIL");} -#define CHECK(cond) cond ? LOG("PASS") : LOG("FAIL") - // Lexer Tests void TestLess() { @@ -23,13 +22,13 @@ void TestNumber() void TestNumberStartWithNine() { auto tokens = Tokenize("999"); - CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 999) + CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 999); } void TestNumberStartWithZero() { auto tokens = Tokenize("0999"); - CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 999) + CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 999); } void TestSpaceNewLineSpace() @@ -73,25 +72,27 @@ void TestVariableDeclaration() void TestParseUnaryExpr() { // TODO make actual test :D - auto expr = Parse(Tokenize("function pesho() { var a = 0; return a; }")); + auto expr = Parse(Tokenize("function pesho(v1) { var a = 0; return a; }")); + ASTPrinter p(std::cout); + expr->Accept(p); } int main() { - EXECUTE_TEST(TestLess); - EXECUTE_TEST(TestNumber); - EXECUTE_TEST(TestNumberStartWithNine); - EXECUTE_TEST(TestNumberStartWithZero); - EXECUTE_TEST(TestString); - EXECUTE_TEST(TestSpaceNewLineSpace); - EXECUTE_TEST(TestStringSingleQuotedStrings); - EXECUTE_TEST(TestKeyWord); - EXECUTE_TEST(TestVariableDeclaration); + //EXECUTE_TEST(TestLess); + //EXECUTE_TEST(TestNumber); + //EXECUTE_TEST(TestNumberStartWithNine); + //EXECUTE_TEST(TestNumberStartWithZero); + //EXECUTE_TEST(TestString); + //EXECUTE_TEST(TestSpaceNewLineSpace); + //EXECUTE_TEST(TestStringSingleQuotedStrings); + //EXECUTE_TEST(TestKeyWord); + //EXECUTE_TEST(TestVariableDeclaration); - //EXECUTE_TEST(TestParseUnaryExpr); + EXECUTE_TEST(TestParseUnaryExpr); #if defined(_WIN32) - std::system("pause"); + //std::system("pause"); #endif return 0; } -- 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') 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 From 0c666088bdf8ad15bbdfbc687cef38532dff96b4 Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sun, 14 Oct 2018 18:27:53 +0300 Subject: Split expression definitions in different file. --- JSImpl/solution/JSImpl.vcxproj | 1 + JSImpl/solution/JSImpl.vcxproj.filters | 57 +++++++++--------- JSImpl/src/Expression.h | 104 +-------------------------------- JSImpl/src/ExpressionDefinitions.h | 100 +++++++++++++++++++++++++++++++ JSImpl/src/main.cpp | 2 +- 5 files changed, 134 insertions(+), 130 deletions(-) create mode 100644 JSImpl/src/ExpressionDefinitions.h (limited to 'JSImpl/src') diff --git a/JSImpl/solution/JSImpl.vcxproj b/JSImpl/solution/JSImpl.vcxproj index e55b38d..99b34cc 100644 --- a/JSImpl/solution/JSImpl.vcxproj +++ b/JSImpl/solution/JSImpl.vcxproj @@ -115,6 +115,7 @@ + diff --git a/JSImpl/solution/JSImpl.vcxproj.filters b/JSImpl/solution/JSImpl.vcxproj.filters index d56f6a1..314e0f3 100644 --- a/JSImpl/solution/JSImpl.vcxproj.filters +++ b/JSImpl/solution/JSImpl.vcxproj.filters @@ -1,53 +1,56 @@  - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + {9e65384d-2e18-4a3f-9b02-4abf632f9f06} - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;xsd + + {177dbff8-a919-4a03-89ba-cb0a96e9e21e} + + + {948e229c-5a7a-4b3c-a75a-4b1b299c9e38} + + + {cf3787a3-5a0e-49f9-9a9f-99e14b57f391} - - Header Files + + Expressions + + + Expressions + + + Expressions - Header Files + Lexer - Header Files - - - Header Files + Parser - Header Files - - - Header Files + Visitors - Header Files + Visitors + - - Source Files + + Expressions - - Source Files + + Lexer - Source Files - - - Source Files + Parser - Source Files + Visitors + \ No newline at end of file diff --git a/JSImpl/src/Expression.h b/JSImpl/src/Expression.h index 30405f7..01c9b68 100644 --- a/JSImpl/src/Expression.h +++ b/JSImpl/src/Expression.h @@ -3,6 +3,7 @@ #include "CommonTypes.h" #include "Lexer.h" #include "ExpressionVisitor.h" +#include "ExpressionDefinitions.h" class Expression : public IPLEnableShared { @@ -40,108 +41,7 @@ class Expression : public IPLEnableShared bool m_dummy; \ }; - -#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)\ - MACRO(TokenType, Operator, TokenType::Invalid) - -#define UNARY_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLSharedPtr, Expr, nullptr)\ - MACRO(TokenType, Operator, TokenType::Invalid)\ - MACRO(bool, Suffix, false) - -#define LITERAL_EXPRESSION_MEMBERS(MACRO)\ - MACRO(double, NumValue, 0.)\ - MACRO(IPLString, StringValue, "")\ - MACRO(bool, BooleanValue, false)\ - MACRO(LiteralType, Type, LiteralType::Undefined) - -#define IDENTIFIER_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLString, Name, "") - -#define VARIABLEDEFINITION_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLString, Name, "")\ - MACRO(ExpressionPtr, Value, nullptr) - -#define LIST_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLVector, Values, IPLVector()) - -#define BLOCK_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLVector, Values, IPLVector()) - -#define LABELED_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLString, Identifier, "")\ - MACRO(ExpressionPtr, Statement, nullptr) - -#define IF_EXPRESSION_MEMBERS(MACRO)\ - MACRO(ExpressionPtr, Condition, nullptr)\ - MACRO(ExpressionPtr, IfStatement, nullptr)\ - MACRO(ExpressionPtr, ElseStatement, nullptr) - -#define CASE_EXPRESSION_MEMBERS(MACRO)\ - MACRO(ExpressionPtr, Condition, nullptr)\ - MACRO(ExpressionPtr, Body, nullptr) - -#define SWITCH_EXPRESSION_MEMBERS(MACRO)\ - MACRO(ExpressionPtr, Condition, nullptr)\ - MACRO(IPLVector, Cases, IPLVector())\ - MACRO(ExpressionPtr, DefaultCase, nullptr) - -#define WHILE_EXPRESSION_MEMBERS(MACRO)\ - MACRO(ExpressionPtr, Condition, nullptr)\ - MACRO(ExpressionPtr, Body, nullptr)\ - MACRO(bool, DoWhile, false) - -#define FOR_EXPRESSION_MEMBERS(MACRO)\ - MACRO(ExpressionPtr, Initialization, nullptr)\ - MACRO(ExpressionPtr, Condition, nullptr)\ - MACRO(ExpressionPtr, Iteration, nullptr)\ - MACRO(ExpressionPtr, Body, nullptr) - -#define FUNCTION_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLString, Name, "")\ - MACRO(IPLVector, ArgumentsIdentifiers, IPLVector())\ - MACRO(ExpressionPtr, Body, nullptr) - -#define TOP_EXPRESSION_MEMBERS(MACRO)\ - MACRO(IPLVector, Values, IPLVector()) - -#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)\ - MACRO(VariableDefinitionExpression, Expression, VARIABLEDEFINITION_EXPRESSION_MEMBERS)\ - MACRO(ListExpression, Expression, LIST_EXPRESSION_MEMBERS)\ - MACRO(BlockStatement, Expression, BLOCK_EXPRESSION_MEMBERS)\ - MACRO(LabeledStatement, Expression, LABELED_EXPRESSION_MEMBERS)\ - MACRO(IfStatement, Expression, IF_EXPRESSION_MEMBERS)\ - MACRO(SwitchStatement, Expression, SWITCH_EXPRESSION_MEMBERS)\ - MACRO(CaseStatement, Expression, CASE_EXPRESSION_MEMBERS)\ - MACRO(WhileStatement, Expression, WHILE_EXPRESSION_MEMBERS)\ - MACRO(ForStatement, Expression, FOR_EXPRESSION_MEMBERS)\ - MACRO(FunctionDeclaration, Expression, FUNCTION_EXPRESSION_MEMBERS)\ - MACRO(TopStatements, Expression, TOP_EXPRESSION_MEMBERS)\ - MACRO(EmptyExpression, Expression, EMPTY_EXPRESSION_MEMBERS) - EXPRESSION_DEFINITION_ITERATOR(GENERATE_EXPRESSION); +#undef GENERATE_EXPRESSION inline ExpressionPtr CreateEmptyExpression() { return IPLMakeSharePtr(); } diff --git a/JSImpl/src/ExpressionDefinitions.h b/JSImpl/src/ExpressionDefinitions.h new file mode 100644 index 0000000..b1b2595 --- /dev/null +++ b/JSImpl/src/ExpressionDefinitions.h @@ -0,0 +1,100 @@ +#pragma once + +#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)\ + MACRO(TokenType, Operator, TokenType::Invalid) + +#define UNARY_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLSharedPtr, Expr, nullptr)\ + MACRO(TokenType, Operator, TokenType::Invalid)\ + MACRO(bool, Suffix, false) + +#define LITERAL_EXPRESSION_MEMBERS(MACRO)\ + MACRO(double, NumValue, 0.)\ + MACRO(IPLString, StringValue, "")\ + MACRO(bool, BooleanValue, false)\ + MACRO(LiteralType, Type, LiteralType::Undefined) + +#define IDENTIFIER_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLString, Name, "") + +#define VARIABLEDEFINITION_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLString, Name, "")\ + MACRO(ExpressionPtr, Value, nullptr) + +#define LIST_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLVector, Values, IPLVector()) + +#define BLOCK_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLVector, Values, IPLVector()) + +#define LABELED_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLString, Identifier, "")\ + MACRO(ExpressionPtr, Statement, nullptr) + +#define IF_EXPRESSION_MEMBERS(MACRO)\ + MACRO(ExpressionPtr, Condition, nullptr)\ + MACRO(ExpressionPtr, IfStatement, nullptr)\ + MACRO(ExpressionPtr, ElseStatement, nullptr) + +#define CASE_EXPRESSION_MEMBERS(MACRO)\ + MACRO(ExpressionPtr, Condition, nullptr)\ + MACRO(ExpressionPtr, Body, nullptr) + +#define SWITCH_EXPRESSION_MEMBERS(MACRO)\ + MACRO(ExpressionPtr, Condition, nullptr)\ + MACRO(IPLVector, Cases, IPLVector())\ + MACRO(ExpressionPtr, DefaultCase, nullptr) + +#define WHILE_EXPRESSION_MEMBERS(MACRO)\ + MACRO(ExpressionPtr, Condition, nullptr)\ + MACRO(ExpressionPtr, Body, nullptr)\ + MACRO(bool, DoWhile, false) + +#define FOR_EXPRESSION_MEMBERS(MACRO)\ + MACRO(ExpressionPtr, Initialization, nullptr)\ + MACRO(ExpressionPtr, Condition, nullptr)\ + MACRO(ExpressionPtr, Iteration, nullptr)\ + MACRO(ExpressionPtr, Body, nullptr) + +#define FUNCTION_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLString, Name, "")\ + MACRO(IPLVector, ArgumentsIdentifiers, IPLVector())\ + MACRO(ExpressionPtr, Body, nullptr) + +#define TOP_EXPRESSION_MEMBERS(MACRO)\ + MACRO(IPLVector, Values, IPLVector()) + +#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)\ + MACRO(VariableDefinitionExpression, Expression, VARIABLEDEFINITION_EXPRESSION_MEMBERS)\ + MACRO(ListExpression, Expression, LIST_EXPRESSION_MEMBERS)\ + MACRO(BlockStatement, Expression, BLOCK_EXPRESSION_MEMBERS)\ + MACRO(LabeledStatement, Expression, LABELED_EXPRESSION_MEMBERS)\ + MACRO(IfStatement, Expression, IF_EXPRESSION_MEMBERS)\ + MACRO(SwitchStatement, Expression, SWITCH_EXPRESSION_MEMBERS)\ + MACRO(CaseStatement, Expression, CASE_EXPRESSION_MEMBERS)\ + MACRO(WhileStatement, Expression, WHILE_EXPRESSION_MEMBERS)\ + MACRO(ForStatement, Expression, FOR_EXPRESSION_MEMBERS)\ + MACRO(FunctionDeclaration, Expression, FUNCTION_EXPRESSION_MEMBERS)\ + MACRO(TopStatements, Expression, TOP_EXPRESSION_MEMBERS)\ + MACRO(EmptyExpression, Expression, NO_MEMBERS) diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp index b51d1a9..586eef1 100644 --- a/JSImpl/src/main.cpp +++ b/JSImpl/src/main.cpp @@ -90,7 +90,7 @@ int main() //EXECUTE_TEST(TestVariableDeclaration); - EXECUTE_TEST(TestParseUnaryExpr); + TestParseUnaryExpr(); #if defined(_WIN32) //std::system("pause"); #endif -- cgit v1.3.1-23-gd82d From f671c0492871418a7b6fed235196b85f79236cff Mon Sep 17 00:00:00 2001 From: martin dobrev Date: Sun, 14 Oct 2018 18:34:11 +0300 Subject: Clean up AST json ptrinter. --- JSImpl/src/ASTPrinter.cpp | 30 ------------------------------ 1 file changed, 30 deletions(-) (limited to 'JSImpl/src') diff --git a/JSImpl/src/ASTPrinter.cpp b/JSImpl/src/ASTPrinter.cpp index 864c4fc..85bd9ab 100644 --- a/JSImpl/src/ASTPrinter.cpp +++ b/JSImpl/src/ASTPrinter.cpp @@ -85,26 +85,6 @@ 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"; -//} std::ostream& operator<<(std::ostream& os, const TokenType& t) { @@ -204,16 +184,6 @@ 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"; -//} -// #define MEMBERS_COUNT(type, name, def) ++membersCount; #define VISIT_MEMBER(type, name, def) ++currentVisitCount; Print(os, *this, #name, e->Get##name(), currentVisitCount == membersCount);; -- cgit v1.3.1-23-gd82d