More parser simplification

This commit is contained in:
Jason Turner 2016-04-10 22:27:35 -06:00
parent 866db4ee8b
commit 443828fa23
2 changed files with 53 additions and 55 deletions

View File

@ -505,8 +505,8 @@ namespace chaiscript
Dot_Access_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) : Dot_Access_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) :
AST_Node(std::move(t_ast_node_text), AST_Node_Type::Dot_Access, std::move(t_loc), std::move(t_children)), AST_Node(std::move(t_ast_node_text), AST_Node_Type::Dot_Access, std::move(t_loc), std::move(t_children)),
m_fun_name( m_fun_name(
((children[2]->identifier == AST_Node_Type::Fun_Call) || (children[2]->identifier == AST_Node_Type::Array_Call))? ((children[1]->identifier == AST_Node_Type::Fun_Call) || (children[1]->identifier == AST_Node_Type::Array_Call))?
children[2]->children[0]->text:children[2]->text) { } children[1]->children[0]->text:children[1]->text) { }
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
@ -516,9 +516,9 @@ namespace chaiscript
std::vector<Boxed_Value> params{retval}; std::vector<Boxed_Value> params{retval};
bool has_function_params = false; bool has_function_params = false;
if (children[2]->children.size() > 1) { if (children[1]->children.size() > 1) {
has_function_params = true; has_function_params = true;
for (const auto &child : children[2]->children[1]->children) { for (const auto &child : children[1]->children[1]->children) {
params.push_back(child->eval(t_ss)); params.push_back(child->eval(t_ss));
} }
} }
@ -540,9 +540,9 @@ namespace chaiscript
retval = std::move(rv.retval); retval = std::move(rv.retval);
} }
if (this->children[2]->identifier == AST_Node_Type::Array_Call) { if (this->children[1]->identifier == AST_Node_Type::Array_Call) {
try { try {
retval = t_ss->call_function("[]", m_array_loc, {retval, this->children[2]->children[1]->eval(t_ss)}, t_ss.conversions()); retval = t_ss->call_function("[]", m_array_loc, {retval, this->children[1]->children[1]->eval(t_ss)}, t_ss.conversions());
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, e.functions, true, *t_ss); throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, e.functions, true, *t_ss);

View File

@ -2036,7 +2036,7 @@ namespace chaiscript
func_call->children.insert(func_call->children.begin(), dot_access->children.back()); func_call->children.insert(func_call->children.begin(), dot_access->children.back());
dot_access->children.pop_back(); dot_access->children.pop_back();
dot_access->children.push_back(std::move(func_call)); dot_access->children.push_back(std::move(func_call));
if (dot_access->children.size() != 3) throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename); if (dot_access->children.size() != 2) throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
m_match_stack.push_back(std::move(dot_access)); m_match_stack.push_back(std::move(dot_access));
} }
} }
@ -2049,13 +2049,13 @@ namespace chaiscript
build_match<eval::Array_Call_AST_Node>(prev_stack_top); build_match<eval::Array_Call_AST_Node>(prev_stack_top);
} }
else if (Symbol(".", true)) { else if (Symbol(".")) {
has_more = true; has_more = true;
if (!(Id())) { if (!(Id())) {
throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
} }
if ( std::distance(m_match_stack.begin() + static_cast<int>(prev_stack_top), m_match_stack.end()) != 3) { if ( std::distance(m_match_stack.begin() + static_cast<int>(prev_stack_top), m_match_stack.end()) != 2) {
throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
} }
build_match<eval::Dot_Access_AST_Node>(prev_stack_top); build_match<eval::Dot_Access_AST_Node>(prev_stack_top);
@ -2221,61 +2221,59 @@ namespace chaiscript
if (t_precedence < m_operators.size()) { if (t_precedence < m_operators.size()) {
if (Operator(t_precedence+1)) { if (Operator(t_precedence+1)) {
retval = true; retval = true;
if (Operator_Helper(t_precedence)) { while (Operator_Helper(t_precedence)) {
do { while (Eol()) {}
while (Eol()) {} if (!Operator(t_precedence+1)) {
if (!Operator(t_precedence+1)) { throw exception::eval_error("Incomplete "
throw exception::eval_error("Incomplete " + std::string(ast_node_type_to_string(m_operators[t_precedence])) + " expression",
+ std::string(ast_node_type_to_string(m_operators[t_precedence])) + " expression", File_Position(m_position.line, m_position.col), *m_filename);
File_Position(m_position.line, m_position.col), *m_filename); }
}
AST_NodePtr oper = m_match_stack.at(m_match_stack.size()-2); AST_NodePtr oper = m_match_stack.at(m_match_stack.size()-2);
switch (m_operators[t_precedence]) { switch (m_operators[t_precedence]) {
case(AST_Node_Type::Ternary_Cond) : case(AST_Node_Type::Ternary_Cond) :
m_match_stack.erase(advance_copy(m_match_stack.begin(), m_match_stack.size() - 2), m_match_stack.erase(advance_copy(m_match_stack.begin(), m_match_stack.size() - 2),
advance_copy(m_match_stack.begin(), m_match_stack.size() - 1)); advance_copy(m_match_stack.begin(), m_match_stack.size() - 1));
if (Symbol(":")) { if (Symbol(":")) {
if (!Operator(t_precedence+1)) { if (!Operator(t_precedence+1)) {
throw exception::eval_error("Incomplete "
+ std::string(ast_node_type_to_string(m_operators[t_precedence])) + " expression",
File_Position(m_position.line, m_position.col), *m_filename);
}
build_match<eval::Ternary_Cond_AST_Node>(prev_stack_top);
}
else {
throw exception::eval_error("Incomplete " throw exception::eval_error("Incomplete "
+ std::string(ast_node_type_to_string(m_operators[t_precedence])) + " expression", + std::string(ast_node_type_to_string(m_operators[t_precedence])) + " expression",
File_Position(m_position.line, m_position.col), *m_filename); File_Position(m_position.line, m_position.col), *m_filename);
} }
break; build_match<eval::Ternary_Cond_AST_Node>(prev_stack_top);
}
else {
throw exception::eval_error("Incomplete "
+ std::string(ast_node_type_to_string(m_operators[t_precedence])) + " expression",
File_Position(m_position.line, m_position.col), *m_filename);
}
break;
case(AST_Node_Type::Addition) : case(AST_Node_Type::Addition) :
case(AST_Node_Type::Multiplication) : case(AST_Node_Type::Multiplication) :
case(AST_Node_Type::Shift) : case(AST_Node_Type::Shift) :
case(AST_Node_Type::Equality) : case(AST_Node_Type::Equality) :
case(AST_Node_Type::Bitwise_And) : case(AST_Node_Type::Bitwise_And) :
case(AST_Node_Type::Bitwise_Xor) : case(AST_Node_Type::Bitwise_Xor) :
case(AST_Node_Type::Bitwise_Or) : case(AST_Node_Type::Bitwise_Or) :
case(AST_Node_Type::Comparison) : case(AST_Node_Type::Comparison) :
assert(m_match_stack.size() > 1); assert(m_match_stack.size() > 1);
m_match_stack.erase(advance_copy(m_match_stack.begin(), m_match_stack.size() - 2), m_match_stack.erase(advance_copy(m_match_stack.begin(), m_match_stack.size() - 2),
advance_copy(m_match_stack.begin(), m_match_stack.size() - 1)); advance_copy(m_match_stack.begin(), m_match_stack.size() - 1));
build_match<eval::Binary_Operator_AST_Node>(prev_stack_top, oper->text); build_match<eval::Binary_Operator_AST_Node>(prev_stack_top, oper->text);
break; break;
case(AST_Node_Type::Logical_And) : case(AST_Node_Type::Logical_And) :
build_match<eval::Logical_And_AST_Node>(prev_stack_top); build_match<eval::Logical_And_AST_Node>(prev_stack_top);
break; break;
case(AST_Node_Type::Logical_Or) : case(AST_Node_Type::Logical_Or) :
build_match<eval::Logical_Or_AST_Node>(prev_stack_top); build_match<eval::Logical_Or_AST_Node>(prev_stack_top);
break; break;
default: default:
throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_position.line, m_position.col), *m_filename);
} }
} while (Operator_Helper(t_precedence));
} }
} }
} }