From d996b89678761d17c498d3dbf2e76b774780f1cf Mon Sep 17 00:00:00 2001 From: Justyna Att Ilczuk Date: Wed, 5 Dec 2012 23:27:03 +0100 Subject: [PATCH] Lots of stuff done in AST. But still not close to finish. --- Sencha-lang/AST/ASTExpression.cpp | 7 ++-- Sencha-lang/AST/ASTExpression.h | 3 +- Sencha-lang/AST/ASTNode.h | 1 + Sencha-lang/AST/AllTypesOfASTNodes.h | 6 ++- Sencha-lang/AST/BasicExpression.h | 6 +++ Sencha-lang/AST/BasicStatement.cpp | 32 +++++++++++++++ Sencha-lang/AST/BasicStatement.h | 21 ++++++++++ Sencha-lang/AST/ConstantExpression.cpp | 3 +- Sencha-lang/AST/ConstantExpression.h | 11 ++++-- Sencha-lang/AST/ProgramNode.cpp | 21 ++++++++++ Sencha-lang/AST/ProgramNode.h | 3 +- Sencha-lang/AST/SenchaObject.cpp | 29 ++++++++++++++ Sencha-lang/AST/SenchaObject.h | 2 + Sencha-lang/AST/to_string.h | 24 ++++++++++++ Sencha-lang/Parser.cpp | 54 +++++++++++++++----------- Sencha-lang/Parser.h | 19 +++++---- 16 files changed, 201 insertions(+), 41 deletions(-) create mode 100644 Sencha-lang/AST/BasicStatement.cpp create mode 100644 Sencha-lang/AST/BasicStatement.h create mode 100644 Sencha-lang/AST/to_string.h diff --git a/Sencha-lang/AST/ASTExpression.cpp b/Sencha-lang/AST/ASTExpression.cpp index 32a47d8..eab03f0 100644 --- a/Sencha-lang/AST/ASTExpression.cpp +++ b/Sencha-lang/AST/ASTExpression.cpp @@ -7,12 +7,13 @@ #include "ASTExpression.h" -ASTExpression::ASTExpression() { - // TODO Auto-generated constructor stub + +ASTExpression::ASTExpression(ASTNode * parent) { + this->parent = parent; } ASTExpression::~ASTExpression() { - // TODO Auto-generated destructor stub + } diff --git a/Sencha-lang/AST/ASTExpression.h b/Sencha-lang/AST/ASTExpression.h index df2c3d1..052e175 100644 --- a/Sencha-lang/AST/ASTExpression.h +++ b/Sencha-lang/AST/ASTExpression.h @@ -13,7 +13,8 @@ class ASTExpression : public ASTNode { public: - virtual int evaluate() = 0; + virtual SenchaObject evaluate() = 0; + ASTExpression(ASTNode * parent); virtual ~ASTExpression(); }; diff --git a/Sencha-lang/AST/ASTNode.h b/Sencha-lang/AST/ASTNode.h index eb077d2..b5d0355 100644 --- a/Sencha-lang/AST/ASTNode.h +++ b/Sencha-lang/AST/ASTNode.h @@ -19,6 +19,7 @@ public: void add_children(ASTNode *); void remove_most_right_children(); void set_parent(ASTNode *); + virtual std::string debug() = 0; virtual void execute() = 0; virtual ~ASTNode(); diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h index b5f30ee..38c65ff 100644 --- a/Sencha-lang/AST/AllTypesOfASTNodes.h +++ b/Sencha-lang/AST/AllTypesOfASTNodes.h @@ -12,7 +12,11 @@ #include "ProgramNode.h" #include "ASTStatement.h" #include "ASTExpression.h" -#include "ASTPrimary.h" + +#include "BasicStatement.h" +#include "BasicExpression.h" +#include "ConstantExpression.h" +#include "PostfixExpression.h" //And probably more //TODO actualize it diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h index ee67a54..10a05e1 100644 --- a/Sencha-lang/AST/BasicExpression.h +++ b/Sencha-lang/AST/BasicExpression.h @@ -12,6 +12,9 @@ class BasicExpression : public ASTExpression { public: //TODO change oper to enum + + std::string all_operators[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!=", "&&", "||"}; + std::string oper; ASTExpression * left; ASTExpression * right; @@ -23,6 +26,9 @@ public: virtual std::string debug(); + std::string debug() { return "Basic expression:\n" + "left operand: \n" + + left->debug() + "\nright operand:\n" + right->debug +"\n###\n"; } + BasicExpression(ASTNode * parent); virtual ~BasicExpression(); }; diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp new file mode 100644 index 0000000..7e64dcf --- /dev/null +++ b/Sencha-lang/AST/BasicStatement.cpp @@ -0,0 +1,32 @@ +/* + * BasicStatement.cpp + * + * Created on: Dec 5, 2012 + * Author: attero + */ + +#include "BasicStatement.h" + +BasicStatement::BasicStatement(ASTNode * parent) { + // TODO Auto-generated constructor stub + this->parent = parent; +} + +BasicStatement::~BasicStatement() { + // TODO Auto-generated destructor stub +} + +void BasicStatement::add_expression(ASTExpression * expr) +{ + this->add_children(expr); +} + +std::string BasicStatement::debug() +{ + return "Basic statement with expression:\n" + children[0]->debug() + "\n;\n"; +} + +void BasicStatement::execute() +{ + std::cout << children[0]->debug() << std::endl ; +} diff --git a/Sencha-lang/AST/BasicStatement.h b/Sencha-lang/AST/BasicStatement.h new file mode 100644 index 0000000..c06f989 --- /dev/null +++ b/Sencha-lang/AST/BasicStatement.h @@ -0,0 +1,21 @@ +/* + * BasicStatement.h + * + * Created on: Dec 5, 2012 + * Author: attero + */ + +#ifndef BASICSTATEMENT_H_ +#define BASICSTATEMENT_H_ +#include + +class BasicStatement : public ASTStatement{ +public: + BasicStatement(ASTNode * parent); + virtual std::string debug(); + void add_expression(ASTExpression * expr); + virtual void execute(); + virtual ~BasicStatement(); +}; + +#endif /* BASICSTATEMENT_H_ */ diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp index 0e5e134..f79e36d 100644 --- a/Sencha-lang/AST/ConstantExpression.cpp +++ b/Sencha-lang/AST/ConstantExpression.cpp @@ -7,8 +7,9 @@ #include "ConstantExpression.h" -ConstantExpression::ConstantExpression() { +ConstantExpression::ConstantExpression(ASTNode * parent) { // TODO Auto-generated constructor stub + this->parent = parent; value = SenchaObject(); } diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h index aafb38a..cfbf429 100644 --- a/Sencha-lang/AST/ConstantExpression.h +++ b/Sencha-lang/AST/ConstantExpression.h @@ -11,12 +11,15 @@ class ConstantExpression : public ASTExpression { public: - ConstantExpression(); - ConstantExpression(int number) { value = SenchaObject(number);}; - ConstantExpression(double number) { value = SenchaObject(number); }; - ConstantExpression(std::string text){ value = SenchaObject(text);}; + 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);}; SenchaObject value; + + std::string debug() { return "Constant expression:\n" + value.repr() + "\n"; } + virtual ~ConstantExpression(); virtual SenchaObject evaluate(); }; diff --git a/Sencha-lang/AST/ProgramNode.cpp b/Sencha-lang/AST/ProgramNode.cpp index 3a7c316..787b9fc 100644 --- a/Sencha-lang/AST/ProgramNode.cpp +++ b/Sencha-lang/AST/ProgramNode.cpp @@ -18,4 +18,25 @@ ProgramNode::~ProgramNode() { void ProgramNode::execute() { std::cout << "Program started!\n"; + for (std::vector::iterator it = children.begin(); it!=children.end(); ++it) { + (*it)->execute(); + } + + std::cout << "END\n"; } + +std::string ProgramNode::debug() +{ + std::string debug_note = "Program started debugging\n"; + + for (std::vector::iterator it = children.begin(); it!=children.end(); ++it) { + debug_note += (*it)->debug() + "\n"; + } + debug_note += "END\n"; + return debug_note; +} +void ProgramNode::add_statement(ASTStatement * statement) +{ + children.push_back(statement); +} + diff --git a/Sencha-lang/AST/ProgramNode.h b/Sencha-lang/AST/ProgramNode.h index dec8bff..375ab4b 100644 --- a/Sencha-lang/AST/ProgramNode.h +++ b/Sencha-lang/AST/ProgramNode.h @@ -14,7 +14,8 @@ class ProgramNode : public ASTNode { public: ProgramNode(); - ASTStatement * add_child(ASTStatement * node); + std::string debug(); + void add_statement(ASTStatement * statement); virtual ~ProgramNode(); virtual void execute(); }; diff --git a/Sencha-lang/AST/SenchaObject.cpp b/Sencha-lang/AST/SenchaObject.cpp index 8b15489..46694f3 100644 --- a/Sencha-lang/AST/SenchaObject.cpp +++ b/Sencha-lang/AST/SenchaObject.cpp @@ -18,6 +18,35 @@ SenchaObject::~SenchaObject() { // TODO Auto-generated destructor stub } + +std::string SenchaObject::repr() +{ + std::string representation = ""; + switch(type){ + case string_literal: + representation = "type: string\n"; + representation += this->text; + break; + case integer_number: + representation = "type: integer\n"; + representation += to_string(this->integer); + break; + case float_number: + representation = "type: float\n"; + representation += to_string(this->number); + break; + case null: + representation = "type: null\n"; + representation += "null"; + break; + case invalid: + representation = "type: invalid\n"; + representation += "some crap"; + break; + } + return representation; +} + SenchaObject SenchaObject::operator+(const SenchaObject& right)const { SenchaObject result; diff --git a/Sencha-lang/AST/SenchaObject.h b/Sencha-lang/AST/SenchaObject.h index 70cadfc..b49497f 100644 --- a/Sencha-lang/AST/SenchaObject.h +++ b/Sencha-lang/AST/SenchaObject.h @@ -8,6 +8,7 @@ #ifndef SENCHAOBJECT_H_ #define SENCHAOBJECT_H_ #include +#include "to_string.h" class SenchaObject { public: @@ -21,6 +22,7 @@ public: Type type; + virtual std::string repr(); std::string text; int integer; diff --git a/Sencha-lang/AST/to_string.h b/Sencha-lang/AST/to_string.h new file mode 100644 index 0000000..62f0639 --- /dev/null +++ b/Sencha-lang/AST/to_string.h @@ -0,0 +1,24 @@ +/* + * to_string.h + * + * Created on: Dec 5, 2012 + * Author: attero + */ + +#ifndef TO_STRING_H_ +#define TO_STRING_H_ + +#include + + + +template +inline std::string to_string (const T& t) +{ +std::stringstream ss; +ss << t; +return ss.str(); +} + + +#endif /* TO_STRING_H_ */ diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index baca878..a939ee3 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -7,6 +7,7 @@ Parser::Parser() report_message = "***PARSER REPORT***\n"; position_in_stream = 0; in_statement = false; + program = static_cast(tree.root); } Parser::~Parser() @@ -63,8 +64,8 @@ void Parser::interpret() { read_next(); while(tok_value!= "") - { - statement(); + { + program->add_statement(statement(program)); } @@ -114,13 +115,15 @@ bool Parser::is_type() else return false; } -ASTNode * Parser::statement(ASTNode * statement) -{ +ASTStatement * Parser::statement(ASTNode * parent) +{ + BasicStatement * stat(parent); + if(accept("{")) { while(!accept("}")) { - statement(); + stat = statement(parent); } } @@ -136,7 +139,8 @@ ASTNode * Parser::statement(ASTNode * statement) { report("Identifier: " + tok_value + "\n"); read_next(); - expr(); + stat->add_expression(expr(stat)); + ; report(" := \n"); } @@ -144,7 +148,7 @@ ASTNode * Parser::statement(ASTNode * statement) if(accept(";")) { report("Variable definition\n"); - return; + return stat; } if(expect("(")) @@ -167,7 +171,7 @@ ASTNode * Parser::statement(ASTNode * statement) if(!accept(";")) { report("function body:\n"); - statement(); + statement(stat); report("function definition\n"); } } @@ -184,32 +188,38 @@ ASTNode * Parser::statement(ASTNode * statement) else if(accept("if")) { //stuff - //TODO implement that + //TODO implement that + return stat; } else if(accept("while")) { - //similar stuff + //similar stuff + return stat; } else if(accept("return")) { if(!peek(";")) { - expr(); + stat->add_expression(expr(stat)); } expect(";"); - report("RETURN\n"); + report("RETURN\n"); + return stat; } else { - expr(); - while(!expect(";") && tok_value != "") read_next(); + stat->add_expression(expr(stat)); + while(!expect(";") && tok_value != "") read_next(); + return stat; - } + } + + return stat; } -ASTNode * Parser::prim_expr(ASTNode * expression) +ASTExpression * Parser::prim_expr(ASTNode * expression) { if(current_token.get_type() == t_integer) { @@ -242,7 +252,7 @@ ASTNode * Parser::prim_expr(ASTNode * expression) } -ASTNode * Parser::postfix_expr(ASTNode * expression) +ASTExpression * Parser::postfix_expr(ASTNode * expression) { prim_expr(); if(accept("[")) @@ -270,7 +280,7 @@ ASTNode * Parser::postfix_expr(ASTNode * expression) } } -ASTNode * Parser::mul_expr(ASTNode * expression) +ASTExpression * Parser::mul_expr(ASTNode * expression) { postfix_expr(); while(peek("*") || peek("/")) @@ -287,7 +297,7 @@ ASTNode * Parser::mul_expr(ASTNode * expression) } } -ASTNode * Parser::add_expr(ASTNode * expression) +ASTExpression * Parser::add_expr(ASTNode * expression) { mul_expr(); while(peek("+") || peek("-")) @@ -304,7 +314,7 @@ ASTNode * Parser::add_expr(ASTNode * expression) } } -ASTNode * Parser::rel_expr(ASTNode * expression) +ASTExpression * Parser::rel_expr(ASTNode * expression) { add_expr(); while(peek("<")) @@ -315,7 +325,7 @@ ASTNode * Parser::rel_expr(ASTNode * expression) } } -ASTNode * Parser::eq_expr(ASTNode * expression) +ASTExpression * Parser::eq_expr(ASTNode * expression) { rel_expr(); while(peek("==") || peek("!=")) @@ -333,7 +343,7 @@ ASTNode * Parser::eq_expr(ASTNode * expression) } } -ASTNode * Parser::expr(ASTNode * expression) +ASTExpression * Parser::expr(ASTNode * expression) { eq_expr(expression); if(accept("=")) diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h index aa0a8c1..4c97944 100644 --- a/Sencha-lang/Parser.h +++ b/Sencha-lang/Parser.h @@ -3,6 +3,7 @@ #include #include #include "Token.h" +#include "AST.h" #include "../AST/AllTypesOfASTNodes.h" using namespace std; @@ -25,6 +26,8 @@ class Parser string tok_value; vector token_stream; int position_in_stream; + AST tree; + ProgramNode * program; bool in_statement; bool read_next(); @@ -37,15 +40,15 @@ class Parser void report(string s); //TODO change functions below to use AST nodes - ASTNode * statement(ASTNode * node); + ASTStatement * statement(ASTNode * node); - ASTNode * mul_expr(ASTNode * node); - ASTNode * add_expr(ASTNode * node); - ASTNode * prim_expr(ASTNode * node); - ASTNode * postfix_expr(ASTNode * node); - ASTNode * rel_expr(ASTNode * node); - ASTNode * eq_expr(ASTNode * node); - ASTNode * expr(ASTNode * node); + ASTExpression * mul_expr(ASTNode * node); + ASTExpression * add_expr(ASTNode * node); + ASTExpression * prim_expr(ASTNode * node); + ASTExpression * postfix_expr(ASTNode * node); + ASTExpression * rel_expr(ASTNode * node); + ASTExpression * eq_expr(ASTNode * node); + ASTExpression * expr(ASTNode * node); }; #endif // PARSER_H