Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src
diff options
context:
space:
mode:
9 files changed, 142 insertions, 202 deletions
-rw-r--r--JSImpl/src/ASTPrinter.cpp35
-rw-r--r--JSImpl/src/ASTPrinter.h6
-rw-r--r--JSImpl/src/Expression.cpp29
-rw-r--r--JSImpl/src/Expression.h120
-rw-r--r--JSImpl/src/ExpressionDefinitions.h100
-rw-r--r--JSImpl/src/ExpressionVisitor.h6
-rw-r--r--JSImpl/src/ExpressionsFwd.h7
-rw-r--r--JSImpl/src/Parser.cpp14
-rw-r--r--JSImpl/src/main.cpp27
diff --git a/JSImpl/src/ASTPrinter.cpp b/JSImpl/src/ASTPrinter.cpp
index d8f5ff8..85bd9ab 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<IPLString>& 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 << "]";
@@ -85,26 +86,6 @@ 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";
-}
-
std::ostream& operator<<(std::ostream& os, const TokenType& t)
{
switch (t)
@@ -203,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);;
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..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<Expression>
{
@@ -11,37 +12,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,
@@ -71,93 +41,7 @@ private:
bool m_dummy; \
};
-
-#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<Expression>, 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<ExpressionPtr>, Values, IPLVector<ExpressionPtr>())
-
-#define BLOCK_EXPRESSION_MEMBERS(MACRO)\
- MACRO(IPLVector<ExpressionPtr>, Values, IPLVector<ExpressionPtr>())
-
-#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<ExpressionPtr>, Cases, IPLVector<ExpressionPtr>())\
- 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<IPLString>, ArgumentsIdentifiers, IPLVector<IPLString>())\
- MACRO(ExpressionPtr, Body, nullptr)
-
-#define TOP_EXPRESSION_MEMBERS(MACRO)\
- MACRO(IPLVector<ExpressionPtr>, Values, IPLVector<ExpressionPtr>())
-
-#define EMPTY_EXPRESSION_MEMBERS(MACRO)
-
-#define EXPRESSION_DEFINITION_ITERATOR(MACRO)\
- 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<EmptyExpression>(); }
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<Expression>, 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<ExpressionPtr>, Values, IPLVector<ExpressionPtr>())
+
+#define BLOCK_EXPRESSION_MEMBERS(MACRO)\
+ MACRO(IPLVector<ExpressionPtr>, Values, IPLVector<ExpressionPtr>())
+
+#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<ExpressionPtr>, Cases, IPLVector<ExpressionPtr>())\
+ 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<IPLString>, ArgumentsIdentifiers, IPLVector<IPLString>())\
+ MACRO(ExpressionPtr, Body, nullptr)
+
+#define TOP_EXPRESSION_MEMBERS(MACRO)\
+ MACRO(IPLVector<ExpressionPtr>, Values, IPLVector<ExpressionPtr>())
+
+#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/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 7fb690f..a2a46e2 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))
{
@@ -930,7 +930,7 @@ ExpressionPtr Parser::Parse()
ExpressionPtr Parser::FunctionDefinition()
{
- auto FormalParameters = [=](IPLVector<IPLString> identifiers) -> bool {
+ auto FormalParameters = [=](IPLVector<IPLString>& identifiers) -> bool {
if (Match(TokenType::LeftParen))
{
while (Match(TokenType::Identifier))
diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp
index 655ee17..bd57ce7 100644
--- a/JSImpl/src/main.cpp
+++ b/JSImpl/src/main.cpp
@@ -1,9 +1,11 @@
#include "Lexer.h"
#include "Parser.h"
+#include "ASTPrinter.h"
#include <iostream>
#define LOG(msg) std::cout << msg << std::endl
#define EXECUTE_TEST(test) std::cout << #test << " "; test();
-#define CHECK(cond) cond ? LOG("PASS") : LOG("FAIL")
+
+#define CHECK(cond) if (cond) {LOG("PASS");} else {LOG(#cond " FAIL");}
// Lexer Tests
void TestLess()
@@ -96,22 +98,23 @@ void TestParseUnaryExpr()
// TODO make actual test :D
auto expr = Parse(tokens);
+ 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(TestStringError);
+ //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);
+ TestParseUnaryExpr();
#if defined(_WIN32)
std::system("pause");
#endif