diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp index 1e98070..a74d166 100644 --- a/Sencha-lang/AST/Assignment.cpp +++ b/Sencha-lang/AST/Assignment.cpp @@ -12,10 +12,6 @@ SenchaObject Assignment::evaluate() return static_cast(children[1])->evaluate(); } -void Assignment::set_name(std::string name) -{ - this->name = name; -} void Assignment::execute() { @@ -38,37 +34,15 @@ void Assignment::execute_quietly() { context->set(left_value.name, right_value); } - } -void Assignment::add_lvalue(ASTExpression * left) -{ - if(children.size()==0) - children.push_back(left); - else - children[0] = left; -} - -void Assignment::add_rvalue(ASTExpression * right) -{ - if(children.size()==1) - children.push_back(right); - else if(children.size()>1) - children[1] = right; - -} - -std::string Assignment::debug() -{ - std::string debug_note = static_cast(children[0])->evaluate().repr(); - debug_note += " = " + static_cast(children[1])->evaluate().repr() + "\n"; - return debug_note; -} - -Assignment::Assignment(Context * context) +Assignment::Assignment(Context * context, std::string name, ASTExpression * left, ASTExpression * right) { this->context = context; this->type = "Assignment"; + this->children.push_back(left); + this->children.push_back(right); + this->name = name; } diff --git a/Sencha-lang/AST/Assignment.h b/Sencha-lang/AST/Assignment.h index a646af4..55a79c3 100644 --- a/Sencha-lang/AST/Assignment.h +++ b/Sencha-lang/AST/Assignment.h @@ -27,13 +27,9 @@ public: std::string name; SenchaObject evaluate(); - void set_name(std::string name); void execute(); void execute_quietly(); - void add_lvalue(ASTExpression *); - void add_rvalue(ASTExpression *); - virtual std::string debug() ; - Assignment(Context * context); + Assignment(Context * context, std::string name, ASTExpression * left, ASTExpression * right); virtual ~Assignment(); }; diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp index 540dca4..a2fab44 100644 --- a/Sencha-lang/AST/BasicExpression.cpp +++ b/Sencha-lang/AST/BasicExpression.cpp @@ -7,41 +7,6 @@ #include "BasicExpression.h" -void BasicExpression::set_operator(std::string op) -{ - this->oper = op; -} - -void BasicExpression::set_left_operand(ASTNode * left) -{ - if(!this->children_set) - { - children.push_back(left); - } - else - { - this->children[0] = left; - } - - if(children.size() == 2) - children_set = true; -} - -void BasicExpression::set_right_operand(ASTNode * right) -{ - if(!this->children_set) - { - children.push_back(right); - - } - else - { - this->children[1] = right; - } - - if(children.size() == 2) - children_set = true; -} void BasicExpression::execute() { std::cout << evaluate().repr() << std::endl; @@ -51,6 +16,7 @@ void BasicExpression::execute_quietly() { evaluate(); } + SenchaObject BasicExpression::evaluate() { //TODO refactor it ;) @@ -115,8 +81,12 @@ SenchaObject BasicExpression::evaluate() return so; } -BasicExpression::BasicExpression() : children_set(false){ +BasicExpression::BasicExpression(ASTNode * left, ASTNode * right, std::string oper ) +{ this->type= "BasicExpression"; + this->children.push_back(left); + this->children.push_back(right); + this->oper = oper; } BasicExpression::~BasicExpression() { diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h index 69b3f18..ee75cac 100644 --- a/Sencha-lang/AST/BasicExpression.h +++ b/Sencha-lang/AST/BasicExpression.h @@ -20,21 +20,16 @@ class BasicExpression : public ASTExpression { public: - void set_operator(std::string op); - void set_left_operand(ASTNode * left); - void set_right_operand(ASTNode * right); virtual SenchaObject evaluate(); virtual void execute(); virtual void execute_quietly(); std::string get_operator() { return oper; } virtual void accept(Visitor * visitor); - BasicExpression(); + BasicExpression(ASTNode * left, ASTNode * right, std::string oper); virtual ~BasicExpression(); private: - //Do I use it for anything actually? - bool children_set; std::string oper; }; diff --git a/Sencha-lang/AST/IfNode.h b/Sencha-lang/AST/IfNode.h index 6accc7d..82ae46a 100644 --- a/Sencha-lang/AST/IfNode.h +++ b/Sencha-lang/AST/IfNode.h @@ -12,7 +12,7 @@ #include "ASTExpression.h" /** - * This is a regular if then_block else else_block + * This is a regular if then_block else else_block. * New IfNode is build like that: * First you add condition, then then-block which is called body * and if it is needed, it's possible to add else-block too. diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp index d277560..737b287 100644 --- a/Sencha-lang/AST/PostfixExpression.cpp +++ b/Sencha-lang/AST/PostfixExpression.cpp @@ -7,11 +7,10 @@ #include "PostfixExpression.h" -PostfixExpression::PostfixExpression(Context * context) { - this->context = context; - name = ""; - native = false; - this->type = "PostfixExpression"; +PostfixExpression::PostfixExpression(std::string name, Context * context): + context(context), name(name), native(false), type("PostfixExpression") +{ + } PostfixExpression::~PostfixExpression() { diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h index b6e4db1..4677a56 100644 --- a/Sencha-lang/AST/PostfixExpression.h +++ b/Sencha-lang/AST/PostfixExpression.h @@ -21,15 +21,13 @@ public: bool native; Context * context; std::vector arguments; - void set_name(std::string name); void add_argument(ASTExpression * expression); virtual SenchaObject evaluate(); virtual void execute(); - virtual void execute_quietly(){ execute(); - }; + virtual void execute_quietly(){ execute();}; - PostfixExpression( Context * context); + PostfixExpression( std::string name, Context * context); virtual ~PostfixExpression(); }; diff --git a/Sencha-lang/AST/VariableExpression.cpp b/Sencha-lang/AST/VariableExpression.cpp index 6ebf0dd..fafec2e 100644 --- a/Sencha-lang/AST/VariableExpression.cpp +++ b/Sencha-lang/AST/VariableExpression.cpp @@ -7,7 +7,7 @@ #include "VariableExpression.h" -VariableExpression::VariableExpression(Context * context, std::string name) { +VariableExpression::VariableExpression(std::string name, Context * context) { this->name = name; this->context = context; this->type = "VariableExpression"; diff --git a/Sencha-lang/AST/VariableExpression.h b/Sencha-lang/AST/VariableExpression.h index f07ba7a..add5190 100644 --- a/Sencha-lang/AST/VariableExpression.h +++ b/Sencha-lang/AST/VariableExpression.h @@ -19,7 +19,7 @@ class VariableExpression: public ASTExpression { public: VariableExpression(); - VariableExpression(Context * context, std::string name); + VariableExpression(std::string name, Context * context); Context * context; std::string name; diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index c1cbe92..ca52866 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -228,22 +228,19 @@ ASTExpression * Parser::prim_expr() { if(current_token.get_type() == t_integer) { - ConstantExpression * ce; - ce = new ConstantExpression(std::atoi(tok_value.c_str())); + ConstantExpression * ce = new ConstantExpression(std::atoi(tok_value.c_str())); read_next(); return ce; } else if(current_token.get_type() == t_float) { - ConstantExpression * ce; - ce = new ConstantExpression(std::atof(tok_value.c_str())); + ConstantExpression * ce = new ConstantExpression(std::atof(tok_value.c_str())); read_next(); return ce; } else if(current_token.get_type() == t_literal) { - ConstantExpression * ce; - ce = new ConstantExpression(tok_value); + ConstantExpression * ce = new ConstantExpression(tok_value); read_next(); return ce; } @@ -265,9 +262,8 @@ ASTExpression * Parser::prim_expr() else if(current_token.get_type() == t_symbol) { string name = current_token.value; - VariableExpression * ve; - ve = new VariableExpression(context, name); + ve = new VariableExpression(name, context); read_next(); return ve; } @@ -290,11 +286,10 @@ ASTExpression * Parser::prim_expr() ASTExpression * Parser::postfix_expr() { - auto name = tok_value; + string name = tok_value; if(is_function_name()) { - PostfixExpression * function_call = new PostfixExpression( context); - function_call->set_name(name); + PostfixExpression * function_call = new PostfixExpression(name, context); if(accept("(")) { if(!accept(")")) @@ -316,161 +311,76 @@ ASTExpression * Parser::postfix_expr() ASTExpression * Parser::mul_expr() { - BasicExpression * be = new BasicExpression(); - be->set_left_operand(postfix_expr()); + ASTExpression * left = postfix_expr(); if(peek("*") || peek("/")) { - if(accept("*")) - { - be->set_operator("*"); - - } else if(accept("/")) - { - be->set_operator("/"); - - } - be->set_right_operand(mul_expr()); + string oper = tok_value; + read_next(); + return new BasicExpression(left, mul_expr(), oper); } - else - { - ASTExpression * ae; - ae = static_cast(be->children[0]); - delete be; - return ae; - } - return be; + else return left; } ASTExpression * Parser::add_expr() { - BasicExpression * be = new BasicExpression(); - be->set_left_operand(mul_expr()); + ASTExpression * left = mul_expr(); if(peek("+") || peek("-")) { - if(accept("+")) - { - - be->set_operator("+"); - } - else if(accept("-")) - { - - be->set_operator("-"); - } - be->set_right_operand(add_expr()); + string oper = tok_value; + read_next(); + return new BasicExpression(left, add_expr(), oper); } - else - { - ASTExpression * ae; - ae = static_cast(be->children[0]); - delete be; - return ae; - } - return be; + else return left; } ASTExpression * Parser::rel_expr() { - BasicExpression * be = new BasicExpression(); - be->set_left_operand(add_expr()); - if(peek("<") || peek(">")) - { - if(accept("<")) - { - be->set_operator("<"); - - } - else if (accept(">")) - { - be->set_operator(">"); - - } - be->set_right_operand(rel_expr()); - } - else + ASTExpression * left = add_expr(); + if(peek("<") || peek(">") || peek("<=") || peek(">=")) { - ASTExpression * ae; - ae = static_cast(be->children[0]); - delete be; - return ae; + string oper = tok_value; + read_next(); + return new BasicExpression(left, rel_expr(), oper); } - return be; + else return left; } ASTExpression * Parser::eq_expr() { - BasicExpression * be = new BasicExpression(); - be->set_left_operand(rel_expr()); + ASTExpression * left = rel_expr(); if(peek("==") || peek("!=")) - { - if(accept("==")) - { - be->set_operator("=="); - - } - else if(accept("!=")) - { - be->set_operator("!="); - - } - be->set_right_operand(eq_expr()); - } - else { - ASTExpression * ae; - ae = static_cast(be->children[0]); - delete be; - return ae; + string oper; + if(accept("==")) oper = "=="; + else oper = "!="; + return new BasicExpression(left, eq_expr(), oper); } - return be; + else return left; } ASTExpression * Parser::log_expr() { - BasicExpression * be = new BasicExpression(); - be->set_left_operand(eq_expr()); + ASTExpression * left = eq_expr(); if(peek("and") || peek("or")) { - if(accept("and")) - { - be->set_operator("&&"); - - } - else if(accept("or")) - { - be->set_operator("||"); - - } - be->set_right_operand(log_expr()); + string oper; + if(accept("and")) oper = "&&"; + else oper = "||"; + ASTExpression * right = log_expr(); + return new BasicExpression(left, right, oper); } - else - { - ASTExpression * ae; - ae = static_cast(be->children[0]); - delete be; - return ae; - } - return be; + else return left; } ASTExpression * Parser::expr() { - Assignment * assignment = new Assignment( context); - auto name = tok_value; + string name = tok_value; ASTExpression * left = log_expr(); if(accept("=")) { ASTExpression * right = expr(); - - assignment->add_lvalue(left); - assignment->add_rvalue(right); - assignment->set_name(name); - + Assignment * assignment = new Assignment( context, name, left, right); return assignment; } - else - { - delete assignment; - return left; - } + else return left; }