summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-28 15:40:54 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-28 15:40:54 +0100
commit0e6bdecf2a2fe49122cd7a271521746f6de0cfb6 (patch)
tree10e8971f342a88ff11e4de39a32b9ba3ee9e567b
parent2d649a149bad632e224d48e2020e0ab6c2d5fb1f (diff)
downloadsencha-lang-0e6bdecf2a2fe49122cd7a271521746f6de0cfb6.tar.gz
sencha-lang-0e6bdecf2a2fe49122cd7a271521746f6de0cfb6.tar.bz2
sencha-lang-0e6bdecf2a2fe49122cd7a271521746f6de0cfb6.tar.xz
sencha-lang-0e6bdecf2a2fe49122cd7a271521746f6de0cfb6.zip
Formatting.
-rw-r--r--Sencha-lang/Parser.cpp587
1 files changed, 269 insertions, 318 deletions
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index a6637ab..c1cbe92 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -4,33 +4,26 @@
Parser::Parser(Context * context)
{
this->context = context;
- error_message = "***ERRORS DURING PARSING***\n";
- position_in_stream = 0;
- in_statement = false;
- program = static_cast<ProgramNode *>(tree.root);
+ error_message = "***ERRORS DURING PARSING***\n";
+ position_in_stream = 0;
+ in_statement = false;
+ program = static_cast<ProgramNode *>(tree.root);
}
Parser::~Parser()
{
- //dtor
+
}
void Parser::erase_all()
{
- /*tree.delete_all_children();
- tree.root = new ProgramNode();
- */
-
error_message = "***ERRORS DURING PARSING***\n";
-
position_in_stream = 0;
in_statement = false;
this->token_stream = vector<Token>();
delete program;
program = new ProgramNode();
-
-
}
string Parser::show_tokens()
{
@@ -53,79 +46,74 @@ void Parser::add_tokens(vector<Token> tokens)
void Parser::error(string s)
{
- error_message += s ;
+ error_message += s ;
}
bool Parser::read_next()
{
- if(position_in_stream < token_stream.size())
- {
- current_token = token_stream[position_in_stream];
- tok_value = current_token.get_value();
- position_in_stream++;
- return true;
- }
- else
- {
- current_token = Token(t_invalid_token, "");
- tok_value = current_token.get_value();
- return false;
- }
+ if(position_in_stream < token_stream.size())
+ {
+ current_token = token_stream[position_in_stream];
+ tok_value = current_token.get_value();
+ position_in_stream++;
+ return true;
+ }
+ else
+ {
+ current_token = Token(t_invalid_token, "");
+ tok_value = current_token.get_value();
+ return false;
+ }
}
void Parser::interpret()
{
read_next();
- while(tok_value!= "")
- {
- program->add_statement(statement());
- }
-
-
+ while(tok_value!= "")
+ {
+ program->add_statement(statement());
+ }
}
bool Parser::peek(string s)
{
- return tok_value == s;
+ return tok_value == s;
}
bool Parser::accept(string s)
{
- if(peek(s))
- {
- read_next();
- return true;
- }
- else return false;
+ if(peek(s))
+ {
+ read_next();
+ return true;
+ }
+ else return false;
}
bool Parser::expect(string s)
{
- if(!accept(s))
- {
- string error_message = "Error: expected ";
- error_message += s;
- error_message += " but received: " + tok_value + "\n";
-
- error(error_message);
- return false;
- }
- else return true;
+ if(!accept(s))
+ {
+ string error_message = "Error: expected ";
+ error_message += s;
+ error_message += " but received: " + tok_value + "\n";
+ error(error_message);
+ return false;
+ }
+ else return true;
}
bool Parser::is_type()
-{
-
- if(tok_value == "def" || tok_value == "string" || tok_value == "num")
- {
- read_next();
- return true;
- }
-
- else
- {
- return false;
- }
+{
+ if(tok_value == "def" || tok_value == "string" || tok_value == "num")
+ {
+ read_next();
+ return true;
+ }
+ else
+ {
+ return false;
+ }
}
bool Parser::is_function_name()
@@ -139,129 +127,105 @@ bool Parser::is_function_name()
{
return false;
}
-
}
ASTStatement * Parser::statement()
{
- BasicStatement * stat = new BasicStatement();
-
- if(accept("{"))
- {
- while(!accept("}"))
- {
- stat->children.push_back( statement());
- }
- }
-
-
- else if(is_type())
- {
- DeclarationStatement * declaration = new DeclarationStatement(context);
- std::string identifier = tok_value;
-
- read_next();
-
- declaration->add_name(identifier);
-
- if(accept("="))
- {
- ASTExpression * ae = expr();
- declaration->add_right_value(ae);
- accept(";");
- }
-
-
- if(expect("("))
- {
- int argc = 0;
- while(tok_value != ")")
- {
- argc++;
- is_type();
-
- declaration->add_argument(tok_value);
- read_next();
- if(peek(")"))
- {
- break;
- }
- expect(",");
- }
- expect(")");
-
- if(!accept(";"))
- {
-
- declaration->add_body(statement());
-
- }
-
- }
-
- delete stat;
- return declaration;
-
- }
- else if(accept("if"))
- {
- IfNode * ifStatement = new IfNode();
- ifStatement->add_condition(expr());
- ifStatement->add_body(statement());
- if(accept("else"))
- {
- ifStatement->add_else_block(statement());
- }
-
- delete stat;
- return ifStatement;
- }
- else if(accept("repeat"))
- {
- RepeatStatement * repeat = new RepeatStatement();
- repeat->add_iteration_number(expr()->evaluate());
- repeat->add_body(statement());
-
- //similar stuff
- delete stat;
- return repeat;
- }
- else if(accept("while"))
- {
- WhileNode * while_node = new WhileNode();
- while_node->add_condition(expr());
- while_node->add_body(statement());
-
- delete stat;
- return while_node;
- }
- else if(accept("return"))
- {
-
- if(!peek(";"))
- {
- stat->add_expression(expr());
- }
- expect(";");
-
- return stat;
-
- }
- else
- {
-
- stat->add_expression(expr());
- while(!expect(";") && tok_value != "") read_next();
- return stat;
+ BasicStatement * stat = new BasicStatement();
+ if(accept("{"))
+ {
+ while(!accept("}"))
+ {
+ stat->children.push_back( statement());
+ }
+ }
+ else if(is_type())
+ {
+ DeclarationStatement * declaration = new DeclarationStatement(context);
+ std::string identifier = tok_value;
+ read_next();
+ declaration->add_name(identifier);
+ if(accept("="))
+ {
+ ASTExpression * ae = expr();
+ declaration->add_right_value(ae);
+ accept(";");
+ }
+ if(expect("("))
+ {
+ int argc = 0;
+ while(tok_value != ")")
+ {
+ argc++;
+ is_type();
+
+ declaration->add_argument(tok_value);
+ read_next();
+ if(peek(")"))
+ {
+ break;
+ }
+ expect(",");
+ }
+ expect(")");
+ if(!accept(";"))
+ {
+
+ declaration->add_body(statement());
+ }
+ }
+ delete stat;
+ return declaration;
+ }
+ else if(accept("if"))
+ {
+ IfNode * ifStatement = new IfNode();
+ ifStatement->add_condition(expr());
+ ifStatement->add_body(statement());
+ if(accept("else"))
+ {
+ ifStatement->add_else_block(statement());
+ }
+ delete stat;
+ return ifStatement;
+ }
+ else if(accept("repeat"))
+ {
+ RepeatStatement * repeat = new RepeatStatement();
+ repeat->add_iteration_number(expr()->evaluate());
+ repeat->add_body(statement());
+ delete stat;
+ return repeat;
+ }
+ else if(accept("while"))
+ {
+ WhileNode * while_node = new WhileNode();
+ while_node->add_condition(expr());
+ while_node->add_body(statement());
+ delete stat;
+ return while_node;
+ }
+ else if(accept("return"))
+ {
+ if(!peek(";"))
+ {
+ stat->add_expression(expr());
+ }
+ expect(";");
+ return stat;
+ }
+ else
+ {
+ stat->add_expression(expr());
+ while(!expect(";") && tok_value != "") read_next();
+ return stat;
- }
-
- return stat;
+ }
+ return stat;
}
ASTExpression * Parser::prim_expr()
-{
-
+{
if(current_token.get_type() == t_integer)
{
ConstantExpression * ce;
@@ -288,13 +252,11 @@ ASTExpression * Parser::prim_expr()
ConstantExpression * ce;
if(tok_value == "true")
{
-
ce = new ConstantExpression( SenchaObject(true));
read_next();
}
else if (tok_value == "false")
{
-
ce = new ConstantExpression(SenchaObject(false));
read_next();
}
@@ -323,8 +285,7 @@ ASTExpression * Parser::prim_expr()
error(error_message);
read_next();
return new IncorrectExpression(error_message);
- }
-
+ }
}
ASTExpression * Parser::postfix_expr()
@@ -335,191 +296,181 @@ ASTExpression * Parser::postfix_expr()
PostfixExpression * function_call = new PostfixExpression( context);
function_call->set_name(name);
if(accept("("))
- {
- if(!accept(")"))
- {
-
- function_call->add_argument(expr());
- while(accept(","))
- {
- function_call->add_argument(expr());
-
- }
- expect(")");
- }
-
- return function_call;
- }
+ {
+ if(!accept(")"))
+ {
+
+ function_call->add_argument(expr());
+ while(accept(","))
+ {
+ function_call->add_argument(expr());
+
+ }
+ expect(")");
+ }
+ return function_call;
+ }
}
- return prim_expr();
+ return prim_expr();
}
ASTExpression * Parser::mul_expr()
{
BasicExpression * be = new BasicExpression();
be->set_left_operand(postfix_expr());
- if(peek("*") || peek("/"))
- {
- if(accept("*"))
- {
- be->set_operator("*");
-
- } else if(accept("/"))
- {
- be->set_operator("/");
+ if(peek("*") || peek("/"))
+ {
+ 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;
- }
+ } 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;
+ }
return be;
}
ASTExpression * Parser::add_expr()
{
BasicExpression * be = new BasicExpression();
- be->set_left_operand(mul_expr());
- if(peek("+") || peek("-"))
- {
- if(accept("+"))
- {
+ be->set_left_operand(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;
- }
+ be->set_operator("+");
+ }
+ else if(accept("-"))
+ {
- return be;
+ be->set_operator("-");
+ }
+ be->set_right_operand(add_expr());
+ }
+ else
+ {
+ ASTExpression * ae;
+ ae = static_cast<ASTExpression *>(be->children[0]);
+ delete be;
+ return ae;
+ }
+ return be;
}
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 * ae;
- ae = static_cast<ASTExpression *>(be->children[0]);
- delete be;
- return ae;
- }
+ if(peek("<") || peek(">"))
+ {
+ if(accept("<"))
+ {
+ be->set_operator("<");
+ }
+ else if (accept(">"))
+ {
+ be->set_operator(">");
- return be;
+ }
+ be->set_right_operand(rel_expr());
+ }
+ else
+ {
+ ASTExpression * ae;
+ ae = static_cast<ASTExpression *>(be->children[0]);
+ delete be;
+ return ae;
+ }
+ return be;
}
ASTExpression * Parser::eq_expr()
{
BasicExpression * be = new BasicExpression();
be->set_left_operand(rel_expr());
- if(peek("==") || peek("!="))
- {
- if(accept("=="))
- {
- be->set_operator("==");
+ if(peek("==") || peek("!="))
+ {
+ if(accept("=="))
+ {
+ be->set_operator("==");
- }
- else 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;
- }
-
- return be;
+ }
+ be->set_right_operand(eq_expr());
+ }
+ else
+ {
+ ASTExpression * ae;
+ ae = static_cast<ASTExpression *>(be->children[0]);
+ delete be;
+ return ae;
+ }
+ return be;
}
ASTExpression * Parser::log_expr()
{
BasicExpression * be = new BasicExpression();
be->set_left_operand(eq_expr());
- if(peek("and") || peek("or"))
- {
- if(accept("and"))
- {
- be->set_operator("&&");
-
- }
- else if(accept("or"))
- {
- be->set_operator("||");
+ if(peek("and") || peek("or"))
+ {
+ if(accept("and"))
+ {
+ be->set_operator("&&");
- }
- be->set_right_operand(log_expr());
- }
- else
- {
- ASTExpression * ae;
- ae = static_cast<ASTExpression *>(be->children[0]);
- delete be;
- return ae;
- }
+ }
+ else if(accept("or"))
+ {
+ be->set_operator("||");
- return be;
+ }
+ be->set_right_operand(log_expr());
+ }
+ else
+ {
+ ASTExpression * ae;
+ ae = static_cast<ASTExpression *>(be->children[0]);
+ delete be;
+ return ae;
+ }
+ return be;
}
ASTExpression * Parser::expr()
{
Assignment * assignment = new Assignment( context);
auto name = tok_value;
- ASTExpression * left = log_expr();
-
- if(accept("="))
- {
- ASTExpression * right = expr();
-
- assignment->add_lvalue(left);
- assignment->add_rvalue(right);
- assignment->set_name(name);
+ ASTExpression * left = log_expr();
+ if(accept("="))
+ {
+ ASTExpression * right = expr();
- return assignment;
- }
+ assignment->add_lvalue(left);
+ assignment->add_rvalue(right);
+ assignment->set_name(name);
- else
- {
- delete assignment;
- return left;
- }
-
+ return assignment;
+ }
+ else
+ {
+ delete assignment;
+ return left;
+ }
}