diff --git a/Sencha-lang/AST/ASTExpression.cpp b/Sencha-lang/AST/ASTExpression.cpp index eab03f0..72ba75b 100644 --- a/Sencha-lang/AST/ASTExpression.cpp +++ b/Sencha-lang/AST/ASTExpression.cpp @@ -8,7 +8,7 @@ #include "ASTExpression.h" -ASTExpression::ASTExpression(ASTNode * parent) { +ASTExpression::ASTExpression() { this->parent = parent; } diff --git a/Sencha-lang/AST/ASTExpression.h b/Sencha-lang/AST/ASTExpression.h index 052e175..af5f487 100644 --- a/Sencha-lang/AST/ASTExpression.h +++ b/Sencha-lang/AST/ASTExpression.h @@ -14,7 +14,7 @@ class ASTExpression : public ASTNode { public: virtual SenchaObject evaluate() = 0; - ASTExpression(ASTNode * parent); + ASTExpression(); virtual ~ASTExpression(); }; diff --git a/Sencha-lang/AST/ASTStatement.h b/Sencha-lang/AST/ASTStatement.h index b2bfd84..f1e9451 100644 --- a/Sencha-lang/AST/ASTStatement.h +++ b/Sencha-lang/AST/ASTStatement.h @@ -12,6 +12,7 @@ class ASTStatement : public ASTNode { public: ASTStatement(); + virtual void execute() = 0; virtual ~ASTStatement(); }; diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h index 38c65ff..60aa468 100644 --- a/Sencha-lang/AST/AllTypesOfASTNodes.h +++ b/Sencha-lang/AST/AllTypesOfASTNodes.h @@ -17,6 +17,7 @@ #include "BasicExpression.h" #include "ConstantExpression.h" #include "PostfixExpression.h" +#include "IncorrectExpression.h" //And probably more //TODO actualize it diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp index b370619..5e1dc7a 100644 --- a/Sencha-lang/AST/BasicExpression.cpp +++ b/Sencha-lang/AST/BasicExpression.cpp @@ -14,33 +14,49 @@ void BasicExpression::set_operator(std::string op) void BasicExpression::set_left_operand(ASTNode * left) { + if(!this->children_set) + { + children.push_back(left); + } + else + { this->children[0] = left; - this->left = static_cast(children[0]); + } + + if(children.size() == 2) + children_set = true; } void BasicExpression::set_right_operand(ASTNode * right) { - this->children[1] = right; - this->right = static_cast(children[1]); + if(!this->children_set) + { + children.push_back(right); + + } + else + { + this->children[1] = right; + } + + if(children.size() == 2) + children_set = true; } -virtual SenchaObject BasicExpression::evaluate() +SenchaObject BasicExpression::evaluate() { + if(oper == "+") { - return left->evaluate() + right->evaluate(); + return SenchaObject(static_cast(children[0])->evaluate() + static_cast(children[1])->evaluate()); } - return 0; + return SenchaObject(); } BasicExpression::BasicExpression(ASTNode * parent) { this->parent = parent; - // initialize pointers - this->add_children(0); - this->add_children(0); - left = static_cast(children[0]); - right = static_cast(children[1]); + children_set = false; } BasicExpression::~BasicExpression() { @@ -48,3 +64,12 @@ BasicExpression::~BasicExpression() { // TODO free children memory } + +std::string BasicExpression::debug() +{ + std::string debug_note = "Basic expression:\n"; + debug_note += "left operand: \n"; + debug_note += this->children[0]->debug(); + debug_note += "\nright operand:\n" + this->children[1]->debug() +"\n###\n"; + return debug_note; +} diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h index 10a05e1..c9a79e3 100644 --- a/Sencha-lang/AST/BasicExpression.h +++ b/Sencha-lang/AST/BasicExpression.h @@ -13,21 +13,19 @@ class BasicExpression : public ASTExpression { public: //TODO change oper to enum - std::string all_operators[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!=", "&&", "||"}; + std::string oper; - ASTExpression * left; - ASTExpression * right; + bool children_set; void set_operator(std::string op); void set_left_operand(ASTNode * left); void set_right_operand(ASTNode * right); virtual SenchaObject evaluate(); + virtual void execute() { evaluate(); }; - virtual std::string debug(); - std::string debug() { return "Basic expression:\n" + "left operand: \n" + - left->debug() + "\nright operand:\n" + right->debug +"\n###\n"; } + std::string debug() ; BasicExpression(ASTNode * parent); virtual ~BasicExpression(); diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp index 7e64dcf..3229321 100644 --- a/Sencha-lang/AST/BasicStatement.cpp +++ b/Sencha-lang/AST/BasicStatement.cpp @@ -18,7 +18,7 @@ BasicStatement::~BasicStatement() { void BasicStatement::add_expression(ASTExpression * expr) { - this->add_children(expr); + children.push_back(expr); } std::string BasicStatement::debug() diff --git a/Sencha-lang/AST/BasicStatement.h b/Sencha-lang/AST/BasicStatement.h index c06f989..a34c4e7 100644 --- a/Sencha-lang/AST/BasicStatement.h +++ b/Sencha-lang/AST/BasicStatement.h @@ -8,8 +8,10 @@ #ifndef BASICSTATEMENT_H_ #define BASICSTATEMENT_H_ #include +#include "ASTExpression.h" +#include "ASTStatement.h" -class BasicStatement : public ASTStatement{ +class BasicStatement : public ASTStatement { public: BasicStatement(ASTNode * parent); virtual std::string debug(); diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp index f79e36d..9858bb9 100644 --- a/Sencha-lang/AST/ConstantExpression.cpp +++ b/Sencha-lang/AST/ConstantExpression.cpp @@ -17,7 +17,25 @@ ConstantExpression::~ConstantExpression() { // TODO Auto-generated destructor stub } -virtual SenchaObject ConstantExpression::evaluate() +SenchaObject ConstantExpression::evaluate() { return value; } + +ConstantExpression::ConstantExpression(ASTNode * parent, int number) +{ + this->parent = parent; value = SenchaObject(number); +} + +ConstantExpression::ConstantExpression(ASTNode * parent, double number) +{ + this->parent = parent; + value = SenchaObject(number); +} + +ConstantExpression::ConstantExpression(ASTNode * parent, std::string text) +{ + this->parent = parent; value = SenchaObject(text); +} + +std::string ConstantExpression::debug() { return "Constant expression:\n" + value.repr() + "\n"; } diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h index cfbf429..5ba474c 100644 --- a/Sencha-lang/AST/ConstantExpression.h +++ b/Sencha-lang/AST/ConstantExpression.h @@ -12,13 +12,16 @@ class ConstantExpression : public ASTExpression { public: ConstantExpression(ASTNode * parent); - ConstantExpression(ASTNode * parent, int number) { this->parent = parent; value = SenchaObject(number);}; - ConstantExpression(ASTNode * parent, double number) {this->parent = parent; value = SenchaObject(number); }; - ConstantExpression(std::string text){ this->parent = parent; value = SenchaObject(text);}; + ConstantExpression(ASTNode * parent, int number) ; + ConstantExpression(ASTNode * parent, double number) ; + ConstantExpression(ASTNode * parent, std::string text); SenchaObject value; - std::string debug() { return "Constant expression:\n" + value.repr() + "\n"; } + std::string debug(); + + virtual void execute() { //Do nothing + }; virtual ~ConstantExpression(); virtual SenchaObject evaluate(); diff --git a/Sencha-lang/AST/IncorrectExpression.cpp b/Sencha-lang/AST/IncorrectExpression.cpp new file mode 100644 index 0000000..42d4411 --- /dev/null +++ b/Sencha-lang/AST/IncorrectExpression.cpp @@ -0,0 +1,24 @@ +/* + * IncorrectExpression.cpp + * + * Created on: Dec 6, 2012 + * Author: attero + */ + +#include "IncorrectExpression.h" + +IncorrectExpression::IncorrectExpression(ASTNode * parent, std::string error_message) +{ + this->parent = parent; + this->error_message = error_message; +} + +IncorrectExpression::~IncorrectExpression() { + // TODO Auto-generated destructor stub +} + +SenchaObject IncorrectExpression::evaluate() +{ + SenchaObject null_object; + return null_object; +} diff --git a/Sencha-lang/AST/IncorrectExpression.h b/Sencha-lang/AST/IncorrectExpression.h new file mode 100644 index 0000000..58c8abd --- /dev/null +++ b/Sencha-lang/AST/IncorrectExpression.h @@ -0,0 +1,29 @@ +/* + * IncorrectExpression.h + * + * Created on: Dec 6, 2012 + * Author: attero + */ + +#ifndef INCORRECTEXPRESSION_H_ +#define INCORRECTEXPRESSION_H_ + +#include "ASTExpression.h" +#include + +class IncorrectExpression: public ASTExpression { +public: + + std::string error_message; + + virtual SenchaObject evaluate() ; + + void execute() { std::cout << debug(); } + + std::string debug() { return "Incorrect Expression:\n" + error_message; } + + IncorrectExpression(ASTNode * parent, std::string error_message); + virtual ~IncorrectExpression(); +}; + +#endif /* INCORRECTEXPRESSION_H_ */ diff --git a/Sencha-lang/AST/SenchaObject.h b/Sencha-lang/AST/SenchaObject.h index b49497f..9a4f1bb 100644 --- a/Sencha-lang/AST/SenchaObject.h +++ b/Sencha-lang/AST/SenchaObject.h @@ -56,7 +56,7 @@ public: SenchaObject(); SenchaObject(int integer) { set_value(integer); } - SenchaObject(float number) { set_value(number); } + SenchaObject(double number) { set_value(number); } SenchaObject(std::string text) { set_value(text); } //TODO overload operators as it should be done diff --git a/Sencha-lang/Debug/AST/AST.d b/Sencha-lang/Debug/AST/AST.d index bc99b45..5ed72ff 100644 --- a/Sencha-lang/Debug/AST/AST.d +++ b/Sencha-lang/Debug/AST/AST.d @@ -1,11 +1,18 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \ - ../AST/AllTypesOfASTNodes.h ../AST/ProgramNode.h ../AST/ASTStatement.h \ - ../AST/ASTExpression.h ../AST/ASTPrimary.h + ../AST/SenchaObject.h ../AST/to_string.h ../AST/AllTypesOfASTNodes.h \ + ../AST/ProgramNode.h ../AST/ASTStatement.h ../AST/ASTExpression.h \ + ../AST/BasicStatement.h ../AST/BasicExpression.h \ + ../AST/ConstantExpression.h ../AST/PostfixExpression.h \ + ../AST/IncorrectExpression.h ../AST/AST.h: ../AST/ASTNode.h: +../AST/SenchaObject.h: + +../AST/to_string.h: + ../AST/AllTypesOfASTNodes.h: ../AST/ProgramNode.h: @@ -14,4 +21,12 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \ ../AST/ASTExpression.h: -../AST/ASTPrimary.h: +../AST/BasicStatement.h: + +../AST/BasicExpression.h: + +../AST/ConstantExpression.h: + +../AST/PostfixExpression.h: + +../AST/IncorrectExpression.h: diff --git a/Sencha-lang/Debug/AST/ASTExpression.d b/Sencha-lang/Debug/AST/ASTExpression.d index ce93350..00d9d81 100644 --- a/Sencha-lang/Debug/AST/ASTExpression.d +++ b/Sencha-lang/Debug/AST/ASTExpression.d @@ -1,6 +1,10 @@ AST/ASTExpression.d: ../AST/ASTExpression.cpp ../AST/ASTExpression.h \ - ../AST/ASTNode.h + ../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h ../AST/ASTExpression.h: ../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/ASTNode.d b/Sencha-lang/Debug/AST/ASTNode.d index 867f306..b0275f5 100644 --- a/Sencha-lang/Debug/AST/ASTNode.d +++ b/Sencha-lang/Debug/AST/ASTNode.d @@ -1,3 +1,8 @@ -AST/ASTNode.d: ../AST/ASTNode.cpp ../AST/ASTNode.h +AST/ASTNode.d: ../AST/ASTNode.cpp ../AST/ASTNode.h ../AST/SenchaObject.h \ + ../AST/to_string.h ../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/ASTStatement.d b/Sencha-lang/Debug/AST/ASTStatement.d index 707abb4..2742aca 100644 --- a/Sencha-lang/Debug/AST/ASTStatement.d +++ b/Sencha-lang/Debug/AST/ASTStatement.d @@ -1,6 +1,10 @@ AST/ASTStatement.d: ../AST/ASTStatement.cpp ../AST/ASTStatement.h \ - ../AST/ASTNode.h + ../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h ../AST/ASTStatement.h: ../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/BasicExpression.d b/Sencha-lang/Debug/AST/BasicExpression.d new file mode 100644 index 0000000..34ea1f5 --- /dev/null +++ b/Sencha-lang/Debug/AST/BasicExpression.d @@ -0,0 +1,13 @@ +AST/BasicExpression.d: ../AST/BasicExpression.cpp \ + ../AST/BasicExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \ + ../AST/SenchaObject.h ../AST/to_string.h + +../AST/BasicExpression.h: + +../AST/ASTExpression.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/BasicStatement.d b/Sencha-lang/Debug/AST/BasicStatement.d new file mode 100644 index 0000000..4030566 --- /dev/null +++ b/Sencha-lang/Debug/AST/BasicStatement.d @@ -0,0 +1,15 @@ +AST/BasicStatement.d: ../AST/BasicStatement.cpp ../AST/BasicStatement.h \ + ../AST/ASTExpression.h ../AST/ASTNode.h ../AST/SenchaObject.h \ + ../AST/to_string.h ../AST/ASTStatement.h + +../AST/BasicStatement.h: + +../AST/ASTExpression.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: + +../AST/ASTStatement.h: diff --git a/Sencha-lang/Debug/AST/ConstantExpression.d b/Sencha-lang/Debug/AST/ConstantExpression.d new file mode 100644 index 0000000..53d1b60 --- /dev/null +++ b/Sencha-lang/Debug/AST/ConstantExpression.d @@ -0,0 +1,13 @@ +AST/ConstantExpression.d: ../AST/ConstantExpression.cpp \ + ../AST/ConstantExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \ + ../AST/SenchaObject.h ../AST/to_string.h + +../AST/ConstantExpression.h: + +../AST/ASTExpression.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/IfNode.d b/Sencha-lang/Debug/AST/IfNode.d index 6fa223b..5182578 100644 --- a/Sencha-lang/Debug/AST/IfNode.d +++ b/Sencha-lang/Debug/AST/IfNode.d @@ -1,5 +1,6 @@ AST/IfNode.d: ../AST/IfNode.cpp ../AST/IfNode.h ../AST/ASTStatement.h \ - ../AST/ASTNode.h ../AST/ASTExpression.h + ../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h \ + ../AST/ASTExpression.h ../AST/IfNode.h: @@ -7,4 +8,8 @@ AST/IfNode.d: ../AST/IfNode.cpp ../AST/IfNode.h ../AST/ASTStatement.h \ ../AST/ASTNode.h: +../AST/SenchaObject.h: + +../AST/to_string.h: + ../AST/ASTExpression.h: diff --git a/Sencha-lang/Debug/AST/IncorrectExpression.d b/Sencha-lang/Debug/AST/IncorrectExpression.d new file mode 100644 index 0000000..d59a738 --- /dev/null +++ b/Sencha-lang/Debug/AST/IncorrectExpression.d @@ -0,0 +1,13 @@ +AST/IncorrectExpression.d: ../AST/IncorrectExpression.cpp \ + ../AST/IncorrectExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \ + ../AST/SenchaObject.h ../AST/to_string.h + +../AST/IncorrectExpression.h: + +../AST/ASTExpression.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/PostfixExpression.d b/Sencha-lang/Debug/AST/PostfixExpression.d new file mode 100644 index 0000000..4160736 --- /dev/null +++ b/Sencha-lang/Debug/AST/PostfixExpression.d @@ -0,0 +1,13 @@ +AST/PostfixExpression.d: ../AST/PostfixExpression.cpp \ + ../AST/PostfixExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \ + ../AST/SenchaObject.h ../AST/to_string.h + +../AST/PostfixExpression.h: + +../AST/ASTExpression.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/ProgramNode.d b/Sencha-lang/Debug/AST/ProgramNode.d index 3fa589a..b86457e 100644 --- a/Sencha-lang/Debug/AST/ProgramNode.d +++ b/Sencha-lang/Debug/AST/ProgramNode.d @@ -1,8 +1,13 @@ AST/ProgramNode.d: ../AST/ProgramNode.cpp ../AST/ProgramNode.h \ - ../AST/ASTNode.h ../AST/ASTStatement.h + ../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h \ + ../AST/ASTStatement.h ../AST/ProgramNode.h: ../AST/ASTNode.h: +../AST/SenchaObject.h: + +../AST/to_string.h: + ../AST/ASTStatement.h: diff --git a/Sencha-lang/Debug/AST/SenchaObject.d b/Sencha-lang/Debug/AST/SenchaObject.d new file mode 100644 index 0000000..e0fa9e3 --- /dev/null +++ b/Sencha-lang/Debug/AST/SenchaObject.d @@ -0,0 +1,6 @@ +AST/SenchaObject.d: ../AST/SenchaObject.cpp ../AST/SenchaObject.h \ + ../AST/to_string.h + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/AST/subdir.mk b/Sencha-lang/Debug/AST/subdir.mk index 0311c90..bd9cee4 100644 --- a/Sencha-lang/Debug/AST/subdir.mk +++ b/Sencha-lang/Debug/AST/subdir.mk @@ -7,28 +7,43 @@ CPP_SRCS += \ ../AST/AST.cpp \ ../AST/ASTExpression.cpp \ ../AST/ASTNode.cpp \ -../AST/ASTPrimary.cpp \ ../AST/ASTStatement.cpp \ +../AST/BasicExpression.cpp \ +../AST/BasicStatement.cpp \ +../AST/ConstantExpression.cpp \ ../AST/IfNode.cpp \ -../AST/ProgramNode.cpp +../AST/IncorrectExpression.cpp \ +../AST/PostfixExpression.cpp \ +../AST/ProgramNode.cpp \ +../AST/SenchaObject.cpp OBJS += \ ./AST/AST.o \ ./AST/ASTExpression.o \ ./AST/ASTNode.o \ -./AST/ASTPrimary.o \ ./AST/ASTStatement.o \ +./AST/BasicExpression.o \ +./AST/BasicStatement.o \ +./AST/ConstantExpression.o \ ./AST/IfNode.o \ -./AST/ProgramNode.o +./AST/IncorrectExpression.o \ +./AST/PostfixExpression.o \ +./AST/ProgramNode.o \ +./AST/SenchaObject.o CPP_DEPS += \ ./AST/AST.d \ ./AST/ASTExpression.d \ ./AST/ASTNode.d \ -./AST/ASTPrimary.d \ ./AST/ASTStatement.d \ +./AST/BasicExpression.d \ +./AST/BasicStatement.d \ +./AST/ConstantExpression.d \ ./AST/IfNode.d \ -./AST/ProgramNode.d +./AST/IncorrectExpression.d \ +./AST/PostfixExpression.d \ +./AST/ProgramNode.d \ +./AST/SenchaObject.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Sencha-lang/Debug/Parser.d b/Sencha-lang/Debug/Parser.d index 764149f..0dafb63 100644 --- a/Sencha-lang/Debug/Parser.d +++ b/Sencha-lang/Debug/Parser.d @@ -1,5 +1,36 @@ -Parser.d: ../Parser.cpp ../Parser.h ../Token.h +Parser.d: ../Parser.cpp ../Parser.h ../Token.h \ + ../AST/AllTypesOfASTNodes.h ../AST/ASTNode.h ../AST/SenchaObject.h \ + ../AST/to_string.h ../AST/ProgramNode.h ../AST/ASTStatement.h \ + ../AST/ASTExpression.h ../AST/BasicStatement.h ../AST/BasicExpression.h \ + ../AST/ConstantExpression.h ../AST/PostfixExpression.h \ + ../AST/IncorrectExpression.h ../AST/AST.h ../Parser.h: ../Token.h: + +../AST/AllTypesOfASTNodes.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: + +../AST/ProgramNode.h: + +../AST/ASTStatement.h: + +../AST/ASTExpression.h: + +../AST/BasicStatement.h: + +../AST/BasicExpression.h: + +../AST/ConstantExpression.h: + +../AST/PostfixExpression.h: + +../AST/IncorrectExpression.h: + +../AST/AST.h: diff --git a/Sencha-lang/Debug/Sencha-lang b/Sencha-lang/Debug/Sencha-lang index aeacc48..ee9f9ac 100755 Binary files a/Sencha-lang/Debug/Sencha-lang and b/Sencha-lang/Debug/Sencha-lang differ diff --git a/Sencha-lang/Debug/main.d b/Sencha-lang/Debug/main.d index cf1d5e1..6f48a87 100644 --- a/Sencha-lang/Debug/main.d +++ b/Sencha-lang/Debug/main.d @@ -1,6 +1,10 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h \ - ../Tests/TestLexer.h ../Tests/TestSuite.h ../Tests/minunit.h \ - ../Tests/../Lexer.h + ../AST/AllTypesOfASTNodes.h ../AST/ASTNode.h ../AST/SenchaObject.h \ + ../AST/to_string.h ../AST/ProgramNode.h ../AST/ASTStatement.h \ + ../AST/ASTExpression.h ../AST/BasicStatement.h ../AST/BasicExpression.h \ + ../AST/ConstantExpression.h ../AST/PostfixExpression.h \ + ../AST/IncorrectExpression.h ../AST/AST.h ../Tests/TestLexer.h \ + ../Tests/TestSuite.h ../Tests/minunit.h ../Tests/../Lexer.h ../Token.h: @@ -8,6 +12,32 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h \ ../Parser.h: +../AST/AllTypesOfASTNodes.h: + +../AST/ASTNode.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: + +../AST/ProgramNode.h: + +../AST/ASTStatement.h: + +../AST/ASTExpression.h: + +../AST/BasicStatement.h: + +../AST/BasicExpression.h: + +../AST/ConstantExpression.h: + +../AST/PostfixExpression.h: + +../AST/IncorrectExpression.h: + +../AST/AST.h: + ../Tests/TestLexer.h: ../Tests/TestSuite.h: diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index a939ee3..9cde1dc 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -117,13 +117,13 @@ bool Parser::is_type() ASTStatement * Parser::statement(ASTNode * parent) { - BasicStatement * stat(parent); + BasicStatement * stat = new BasicStatement(parent); if(accept("{")) { while(!accept("}")) { - stat = statement(parent); + stat = static_cast(statement(parent)); } } @@ -220,44 +220,56 @@ ASTStatement * Parser::statement(ASTNode * parent) } ASTExpression * Parser::prim_expr(ASTNode * expression) -{ +{ + ConstantExpression * ce; + BasicExpression * be; +//TODO add reference type to prims, syblol is now just a literal... and floats if(current_token.get_type() == t_integer) { report("Number: " + tok_value + "\n"); + + ce = new ConstantExpression(expression, std::atoi(tok_value.c_str())); read_next(); } else if(current_token.get_type() == t_symbol) { report("Variable: " + tok_value + "\n"); + ce = new ConstantExpression(expression, tok_value); read_next(); } else if(current_token.get_type() == t_literal) - { + { + report("Character literal: " + tok_value + "\n"); + ce = new ConstantExpression(expression, tok_value); read_next(); } else if(accept("(")) { report("( "); - expr(); + be = static_cast(expr(expression)); report(" ) "); expect(")"); - + return be; } else - { - error("ERROR: unexpected primary expression " + tok_value + "\n"); - read_next(); + { + string error_message = "ERROR: unexpected primary expression " + tok_value + "\n"; + error(error_message); + read_next(); + return new IncorrectExpression(expression, error_message); } - + return ce; } ASTExpression * Parser::postfix_expr(ASTNode * expression) -{ - prim_expr(); +{ + //TODO implement postfix expression ASAP + BasicExpression * be = new BasicExpression(expression); + prim_expr(be); if(accept("[")) { - expr(); + expr(be); expect("]"); report(" [] "); @@ -266,89 +278,115 @@ ASTExpression * Parser::postfix_expr(ASTNode * expression) { if(!accept(")")) { - expr(); + expr(be); report("function argument\n"); while(accept(",")) { - expr(); + expr(be); report("function argument\n"); } expect(")"); } report("FUNC_CALL\n"); - } + } + //TODO implement postfix_expression + return be; } ASTExpression * Parser::mul_expr(ASTNode * expression) { - postfix_expr(); + BasicExpression * be = new BasicExpression(expression); + be->set_left_operand(postfix_expr(be)); while(peek("*") || peek("/")) { if(accept("*")) { - postfix_expr(); + be->set_operator("*"); report(" *\n"); } else if(accept("/")) { - postfix_expr(); + be->set_operator("/"); report(" /\n"); } + be->set_right_operand(postfix_expr(be)); } + return be; } ASTExpression * Parser::add_expr(ASTNode * expression) -{ - mul_expr(); +{ + BasicExpression * be = new BasicExpression(expression); + be->set_left_operand(mul_expr(be)); while(peek("+") || peek("-")) { if(accept("+")) { - mul_expr(); - report(" +\n"); - } else if(accept("-")) + report(" +\n"); + be->set_operator("+"); + } + else if(accept("-")) { - mul_expr(); - report(" -\n"); - } - } + report(" -\n"); + be->set_operator("-"); + } + be->set_right_operand(mul_expr(be)); + } + return be; } ASTExpression * Parser::rel_expr(ASTNode * expression) -{ - add_expr(); - while(peek("<")) +{ + BasicExpression * be = new BasicExpression(expression); + be->set_left_operand(add_expr(be)); + while(peek("<") || peek(">")) { - accept("<"); - add_expr(); - report(" <\n"); - } + if(accept("<")) + { + be->set_operator("<"); + report(" <\n"); + } + else if (accept(">")) + { + be->set_operator(">"); + report(" >\n"); + } + be->set_right_operand(add_expr(be)); + } + return be; } ASTExpression * Parser::eq_expr(ASTNode * expression) -{ - rel_expr(); +{ + BasicExpression * be = new BasicExpression(expression); + be->set_left_operand(rel_expr(be)); while(peek("==") || peek("!=")) { if(accept("==")) { - rel_expr(); + be->set_operator("=="); report("==\n"); } else if(accept("!=")) { - rel_expr(); + be->set_operator("!="); report("!=\n"); - } - } + } + be->set_right_operand(rel_expr(be)); + } + return be; } ASTExpression * Parser::expr(ASTNode * expression) { - eq_expr(expression); + BasicExpression * be = new BasicExpression(expression); + be->set_left_operand(eq_expr(be)); if(accept("=")) - { - expr(); + { + be->set_operator("="); + be->set_right_operand(expr(be)); report(" :=\n"); - } + } + //return be; + return new ConstantExpression(expression, 3); } diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h index 4c97944..1d3f3e2 100644 --- a/Sencha-lang/Parser.h +++ b/Sencha-lang/Parser.h @@ -1,10 +1,12 @@ #ifndef PARSER_H #define PARSER_H #include -#include +#include +#include #include "Token.h" -#include "AST.h" -#include "../AST/AllTypesOfASTNodes.h" + +#include "AST/AllTypesOfASTNodes.h" +#include "AST/AST.h" using namespace std;