Ready to merge visit branch with master!
parent
4d72059200
commit
f0bc0e235b
|
@ -5,7 +5,6 @@ Parser::Parser(Context * context)
|
|||
{
|
||||
this->context = context;
|
||||
error_message = "***ERRORS DURING PARSING***\n";
|
||||
report_message = "***PARSER REPORT***\n";
|
||||
position_in_stream = 0;
|
||||
in_statement = false;
|
||||
program = static_cast<ProgramNode *>(tree.root);
|
||||
|
@ -22,7 +21,7 @@ void Parser::erase_all()
|
|||
tree.delete_all_children();
|
||||
tree.root = new ProgramNode();
|
||||
error_message = "***ERRORS DURING PARSING***\n";
|
||||
report_message = "***PARSER REPORT***\n";
|
||||
|
||||
position_in_stream = 0;
|
||||
in_statement = false;
|
||||
program = static_cast<ProgramNode *>(tree.root);
|
||||
|
@ -47,10 +46,6 @@ void Parser::add_tokens(vector<Token> tokens)
|
|||
}
|
||||
}
|
||||
|
||||
void Parser::report(string s)
|
||||
{
|
||||
report_message += s ;
|
||||
}
|
||||
|
||||
void Parser::error(string s)
|
||||
{
|
||||
|
@ -131,18 +126,13 @@ bool Parser::is_type()
|
|||
|
||||
bool Parser::is_function_name()
|
||||
{
|
||||
//check in registered functions
|
||||
//cout << tok_value << " is it registered?" << endl;
|
||||
|
||||
if(context->registered_functions.count(tok_value) == 1)
|
||||
{
|
||||
//cout << "Yes" << endl;
|
||||
read_next();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//cout << "No" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -165,7 +155,7 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
{
|
||||
DeclarationStatement * declaration = new DeclarationStatement(parent, context);
|
||||
std::string identifier = tok_value;
|
||||
report("Identifier: " + identifier + "\n");
|
||||
|
||||
read_next();
|
||||
|
||||
declaration->add_name(identifier);
|
||||
|
@ -174,15 +164,9 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
{
|
||||
ASTExpression * ae = expr(stat);
|
||||
declaration->add_right_value(ae);
|
||||
|
||||
report(" := \n");
|
||||
accept(";");
|
||||
}
|
||||
|
||||
if(accept(";"))
|
||||
{
|
||||
report("Variable declaration\n");
|
||||
}
|
||||
|
||||
if(expect("("))
|
||||
{
|
||||
|
@ -191,7 +175,7 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
{
|
||||
argc++;
|
||||
is_type();
|
||||
report("function argument: " + tok_value + "\n");
|
||||
|
||||
declaration->add_argument(tok_value);
|
||||
read_next();
|
||||
if(peek(")"))
|
||||
|
@ -204,9 +188,9 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
|
||||
if(!accept(";"))
|
||||
{
|
||||
report("function body:\n");
|
||||
|
||||
declaration->add_body(statement(stat));
|
||||
report("function definition\n");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -255,7 +239,7 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
stat->add_expression(expr(stat));
|
||||
}
|
||||
expect(";");
|
||||
report("RETURN\n");
|
||||
|
||||
return stat;
|
||||
|
||||
}
|
||||
|
@ -273,13 +257,9 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
|
||||
ASTExpression * Parser::prim_expr(ASTNode * expression)
|
||||
{
|
||||
|
||||
|
||||
|
||||
if(current_token.get_type() == t_integer)
|
||||
{
|
||||
report("Number: " + tok_value + "\n");
|
||||
|
||||
{
|
||||
ConstantExpression * ce;
|
||||
ce = new ConstantExpression(expression, std::atoi(tok_value.c_str()));
|
||||
read_next();
|
||||
|
@ -287,15 +267,13 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
|
|||
}
|
||||
else if(current_token.get_type() == t_float)
|
||||
{
|
||||
report("Number: " + tok_value + "\n");
|
||||
ConstantExpression * ce;
|
||||
ce = new ConstantExpression(expression, std::atof(tok_value.c_str()));
|
||||
read_next();
|
||||
return ce;
|
||||
}
|
||||
else if(current_token.get_type() == t_literal)
|
||||
{
|
||||
report("Character literal: " + tok_value + "\n");
|
||||
{
|
||||
ConstantExpression * ce;
|
||||
ce = new ConstantExpression(expression, tok_value);
|
||||
read_next();
|
||||
|
@ -306,13 +284,13 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
|
|||
ConstantExpression * ce;
|
||||
if(tok_value == "true")
|
||||
{
|
||||
report("Boolean: " + tok_value + "\n");
|
||||
|
||||
ce = new ConstantExpression(expression, SenchaObject(true));
|
||||
read_next();
|
||||
}
|
||||
else if (tok_value == "false")
|
||||
{
|
||||
report("Boolean: " + tok_value + "\n");
|
||||
|
||||
ce = new ConstantExpression(expression, SenchaObject(false));
|
||||
read_next();
|
||||
}
|
||||
|
@ -320,7 +298,6 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
|
|||
}
|
||||
else if(current_token.get_type() == t_symbol)
|
||||
{
|
||||
report("variable: " + tok_value + "\n");
|
||||
string name = current_token.value;
|
||||
|
||||
VariableExpression * ve;
|
||||
|
@ -330,10 +307,9 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
|
|||
}
|
||||
else if(accept("("))
|
||||
{
|
||||
report("( ");
|
||||
BasicExpression * be;
|
||||
be = static_cast<BasicExpression *>(expr(expression));
|
||||
report(" ) ");
|
||||
|
||||
expect(")");
|
||||
return be;
|
||||
}
|
||||
|
@ -360,15 +336,14 @@ ASTExpression * Parser::postfix_expr(ASTNode * expression)
|
|||
{
|
||||
|
||||
function_call->add_argument(expr(function_call));
|
||||
report("function argument\n");
|
||||
while(accept(","))
|
||||
{
|
||||
function_call->add_argument(expr(function_call));
|
||||
report("function argument\n");
|
||||
|
||||
}
|
||||
expect(")");
|
||||
}
|
||||
report("FUNC_CALL\n");
|
||||
|
||||
return function_call;
|
||||
}
|
||||
}
|
||||
|
@ -384,11 +359,11 @@ ASTExpression * Parser::mul_expr(ASTNode * expression)
|
|||
if(accept("*"))
|
||||
{
|
||||
be->set_operator("*");
|
||||
report(" *\n");
|
||||
|
||||
} else if(accept("/"))
|
||||
{
|
||||
be->set_operator("/");
|
||||
report(" /\n");
|
||||
|
||||
}
|
||||
be->set_right_operand(mul_expr(be));
|
||||
}
|
||||
|
@ -413,12 +388,12 @@ ASTExpression * Parser::add_expr(ASTNode * expression)
|
|||
{
|
||||
if(accept("+"))
|
||||
{
|
||||
report(" +\n");
|
||||
|
||||
be->set_operator("+");
|
||||
}
|
||||
else if(accept("-"))
|
||||
{
|
||||
report(" -\n");
|
||||
|
||||
be->set_operator("-");
|
||||
}
|
||||
be->set_right_operand(add_expr(be));
|
||||
|
@ -445,12 +420,12 @@ ASTExpression * Parser::rel_expr(ASTNode * expression)
|
|||
if(accept("<"))
|
||||
{
|
||||
be->set_operator("<");
|
||||
report(" <\n");
|
||||
|
||||
}
|
||||
else if (accept(">"))
|
||||
{
|
||||
be->set_operator(">");
|
||||
report(" >\n");
|
||||
|
||||
}
|
||||
be->set_right_operand(rel_expr(be));
|
||||
}
|
||||
|
@ -477,12 +452,12 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
|
|||
if(accept("=="))
|
||||
{
|
||||
be->set_operator("==");
|
||||
report("==\n");
|
||||
|
||||
}
|
||||
else if(accept("!="))
|
||||
{
|
||||
be->set_operator("!=");
|
||||
report("!=\n");
|
||||
|
||||
}
|
||||
be->set_right_operand(eq_expr(be));
|
||||
}
|
||||
|
@ -512,7 +487,7 @@ ASTExpression * Parser::expr(ASTNode * expression)
|
|||
assignment->add_lvalue(left);
|
||||
assignment->add_rvalue(right);
|
||||
assignment->set_name(name);
|
||||
report(" :=\n");
|
||||
|
||||
return assignment;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ class Parser
|
|||
|
||||
virtual ~Parser();
|
||||
void interpret();
|
||||
string report_message;
|
||||
string error_message;
|
||||
void add_tokens(vector<Token> tokens);
|
||||
string show_tokens();
|
||||
|
@ -43,7 +42,7 @@ class Parser
|
|||
bool is_type();
|
||||
|
||||
void error(string s);
|
||||
void report(string s);
|
||||
|
||||
|
||||
ASTStatement * statement(ASTNode * node);
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ void test_parser()
|
|||
cout << "<<<Parsing number: " << i << " >>>" << endl;
|
||||
cout << "Instructions: " << endl ;
|
||||
cout << lines[i] << endl << endl;
|
||||
cout << parser.report_message;
|
||||
//cout << parser.report_message;
|
||||
cout << parser.error_message << endl;
|
||||
}
|
||||
|
||||
|
|
|
@ -153,11 +153,8 @@ void interactive()
|
|||
if(level_of_depth == 0) {
|
||||
parser.interpret();
|
||||
parser.program->execute_last();
|
||||
//cout << parser.report_message << endl;
|
||||
//cout << parser.error_message << endl;
|
||||
//cout << parser.show_tokens() << endl;
|
||||
//cout << "My tree:\n";
|
||||
//cout << parser.program->debug();
|
||||
inspector.visit(parser.program);
|
||||
|
||||
//cout << parser.context->debug();
|
||||
|
|
Loading…
Reference in New Issue