summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/Parser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Sencha-lang/Parser.cpp')
-rw-r--r--Sencha-lang/Parser.cpp166
1 files changed, 38 insertions, 128 deletions
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());
- }
- else
- {
- ASTExpression * ae;
- ae = static_cast<ASTExpression *>(be->children[0]);
- delete be;
- return ae;
+ string oper = tok_value;
+ read_next();
+ return new BasicExpression(left, mul_expr(), oper);
}
- 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());
- }
- else
- {
- ASTExpression * ae;
- ae = static_cast<ASTExpression *>(be->children[0]);
- delete be;
- return ae;
+ string oper = tok_value;
+ read_next();
+ return new BasicExpression(left, add_expr(), oper);
}
- 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());
- }
- else
- {
- ASTExpression * ae;
- ae = static_cast<ASTExpression *>(be->children[0]);
- delete be;
- return ae;
+ string oper;
+ if(accept("and")) oper = "&&";
+ else oper = "||";
+ ASTExpression * right = log_expr();
+ return new BasicExpression(left, right, oper);
}
- 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;
}