Refactoring parser methods. Adding more suitable constructors
to make code better.functions
parent
0e6bdecf2a
commit
4d3eaee1b6
|
@ -12,10 +12,6 @@ SenchaObject Assignment::evaluate()
|
|||
return static_cast<ASTExpression *>(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<ASTExpression *>(children[0])->evaluate().repr();
|
||||
debug_note += " = " + static_cast<ASTExpression *>(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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -21,15 +21,13 @@ public:
|
|||
bool native;
|
||||
Context * context;
|
||||
std::vector<ASTExpression *> 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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<ASTExpression *>(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<ASTExpression *>(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<ASTExpression *>(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<ASTExpression *>(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<ASTExpression *>(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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue