Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src
diff options
context:
space:
mode:
Commit370d2afe025f899caab7829407363a37a37093b2 (patch)
Author
 martin dobrev <martindobrev0@gmail.com>
2018-10-14 18:09:45 +0300
Committer
 martin dobrev <martindobrev0@gmail.com>
2018-10-14 18:09:45 +0300
Filesc9cf14183738b92052879009babdad16d0f7dfad /JSImpl/src
IPL-370d2afe025f899caab7829407363a37a37093b2.tar
IPL-370d2afe025f899caab7829407363a37a37093b2.tar.gz
IPL-370d2afe025f899caab7829407363a37a37093b2.zip
Parenta24ea4d6be0a92f9d2d8e937d08f6ea5575021ef (diff)
Split literls in different classes and remove all handwritten code.
7 files changed, 67 insertions, 101 deletions
-rw-r--r--JSImpl/src/ASTPrinter.cpp60
-rw-r--r--JSImpl/src/ASTPrinter.h6
-rw-r--r--JSImpl/src/Expression.cpp29
-rw-r--r--JSImpl/src/Expression.h48
-rw-r--r--JSImpl/src/ExpressionVisitor.h6
-rw-r--r--JSImpl/src/ExpressionsFwd.h7
-rw-r--r--JSImpl/src/Parser.cpp12
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<const IPLString&>(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 <assert.h>
-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<Expression>
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<EmptyExpression>(); }
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<LiteralExpression>(Prev().Number);
+ return IPLMakeSharePtr<LiteralNumber>(Prev().Number);
}
else if (Match(TokenType::String))
{
- return IPLMakeSharePtr<LiteralExpression>(Prev().Lexeme);
+ return IPLMakeSharePtr<LiteralString>(Prev().Lexeme);
}
else if (Match(TokenType::Null))
{
- return IPLMakeSharePtr<LiteralExpression>(Prev().Type);
+ return IPLMakeSharePtr<LiteralNull>();
}
else if (Match(TokenType::Undefined))
{
- return IPLMakeSharePtr<LiteralExpression>(Prev().Type);
+ return IPLMakeSharePtr<LiteralUndefined>();
}
else if (Match(TokenType::True))
{
bool t = true;
- return IPLMakeSharePtr<LiteralExpression>(t);
+ return IPLMakeSharePtr<LiteralBoolean>(t);
}
else if (Match(TokenType::False))
{
bool f = false;
- return IPLMakeSharePtr<LiteralExpression>(f);
+ return IPLMakeSharePtr<LiteralBoolean>(f);
}
else if (Match(TokenType::Identifier))
{