Reduce instances of Str_AST_Node

This commit is contained in:
Jason Turner
2016-04-10 21:38:44 -06:00
parent 5e97f459d8
commit 866db4ee8b
2 changed files with 29 additions and 33 deletions

View File

@@ -327,13 +327,13 @@ namespace chaiscript
struct Equation_AST_Node final : AST_Node { struct Equation_AST_Node final : AST_Node {
Equation_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) : Equation_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::Equation, std::move(t_loc), std::move(t_children)), AST_Node(std::move(t_ast_node_text), AST_Node_Type::Equation, std::move(t_loc), std::move(t_children)),
m_oper(Operators::to_operator(children[1]->text)) m_oper(Operators::to_operator(text))
{ assert(children.size() == 3); } { assert(children.size() == 2); }
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);
Boxed_Value rhs = this->children[2]->eval(t_ss); Boxed_Value rhs = this->children[1]->eval(t_ss);
Boxed_Value lhs = this->children[0]->eval(t_ss); Boxed_Value lhs = this->children[0]->eval(t_ss);
if (m_oper != Operators::Opers::invalid && lhs.get_type_info().is_arithmetic() && if (m_oper != Operators::Opers::invalid && lhs.get_type_info().is_arithmetic() &&
@@ -371,17 +371,17 @@ namespace chaiscript
} }
try { try {
return t_ss->call_function(this->children[1]->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions()); return t_ss->call_function(this->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions());
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, *t_ss); throw exception::eval_error("Unable to find appropriate'" + this->text + "' operator.", e.parameters, e.functions, false, *t_ss);
} }
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Missing clone or copy constructor for right hand side of equation", e.parameters, e.functions, false, *t_ss); throw exception::eval_error("Missing clone or copy constructor for right hand side of equation", e.parameters, e.functions, false, *t_ss);
} }
} }
else if (this->children[1]->text == ":=") { else if (this->text == ":=") {
if (lhs.is_undef() || Boxed_Value::type_match(lhs, rhs)) { if (lhs.is_undef() || Boxed_Value::type_match(lhs, rhs)) {
lhs.assign(rhs); lhs.assign(rhs);
lhs.reset_return_value(); lhs.reset_return_value();
@@ -391,9 +391,9 @@ namespace chaiscript
} }
else { else {
try { try {
return t_ss->call_function(this->children[1]->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions()); return t_ss->call_function(this->text, m_loc, {std::move(lhs), rhs}, t_ss.conversions());
} catch(const exception::dispatch_error &e){ } catch(const exception::dispatch_error &e){
throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, e.functions, false, *t_ss); throw exception::eval_error("Unable to find appropriate'" + this->text + "' operator.", e.parameters, e.functions, false, *t_ss);
} }
} }

View File

@@ -1272,7 +1272,7 @@ namespace chaiscript
} }
/// Reads (and potentially captures) a string from input if it matches the parameter /// Reads (and potentially captures) a string from input if it matches the parameter
bool Keyword(const char *t_s, bool t_capture = false) { bool Keyword(const char *t_s) {
SkipWS(); SkipWS();
const auto start = m_position; const auto start = m_position;
bool retval = Keyword_(t_s); bool retval = Keyword_(t_s);
@@ -1282,9 +1282,6 @@ namespace chaiscript
retval = false; retval = false;
} }
if ( t_capture && retval ) {
m_match_stack.push_back(make_node<eval::Str_AST_Node>(Position::str(start, m_position), start.line, start.col));
}
return retval; return retval;
} }
@@ -1553,7 +1550,7 @@ namespace chaiscript
bool is_method = false; bool is_method = false;
if (Symbol("::", false)) { if (Symbol("::")) {
//We're now a method //We're now a method
is_method = true; is_method = true;
@@ -1682,12 +1679,11 @@ namespace chaiscript
while (has_matches) { while (has_matches) {
while (Eol()) {} while (Eol()) {}
has_matches = false; has_matches = false;
if (Keyword("else", true)) { const auto line = m_position.line;
const auto col = m_position.col;
if (Keyword("else")) {
if (Keyword("if")) { if (Keyword("if")) {
const AST_NodePtr back(m_match_stack.back()); m_match_stack.emplace_back(make_node<eval::If_AST_Node>("else if", line, col, std::vector<AST_NodePtr>()));
m_match_stack.back() =
chaiscript::make_shared<AST_Node, eval::If_AST_Node>("else if", back->location, back->children);
m_match_stack.back()->annotation = back->annotation;
if (!Char('(')) { if (!Char('(')) {
throw exception::eval_error("Incomplete 'else if' expression", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Incomplete 'else if' expression", File_Position(m_position.line, m_position.col), *m_filename);
} }
@@ -1703,6 +1699,7 @@ namespace chaiscript
} }
has_matches = true; has_matches = true;
} else { } else {
m_match_stack.emplace_back(make_node<eval::If_AST_Node>("else", line, col, std::vector<AST_NodePtr>()));
while (Eol()) {} while (Eol()) {}
if (!Block()) { if (!Block()) {
@@ -2105,7 +2102,7 @@ namespace chaiscript
if (!Id()) { if (!Id()) {
throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
} }
if (!Symbol("::", false)) { if (!Symbol("::")) {
throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
} }
if (!Id()) { if (!Id()) {
@@ -2169,7 +2166,7 @@ namespace chaiscript
bool Reference() { bool Reference() {
const auto prev_stack_top = m_match_stack.size(); const auto prev_stack_top = m_match_stack.size();
if (Symbol("&", false)) { if (Symbol("&")) {
if (!Id()) { if (!Id()) {
throw exception::eval_error("Incomplete '&' expression", File_Position(m_position.line, m_position.col), *m_filename); throw exception::eval_error("Incomplete '&' expression", File_Position(m_position.line, m_position.col), *m_filename);
} }
@@ -2345,26 +2342,25 @@ namespace chaiscript
/// Parses a string of binary equation operators /// Parses a string of binary equation operators
bool Equation() { bool Equation() {
bool retval = false;
const auto prev_stack_top = m_match_stack.size(); const auto prev_stack_top = m_match_stack.size();
if (Operator()) { if (Operator()) {
retval = true; for (const auto sym : {"=", ":=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", "&=", "^=", "|="})
if (Symbol("=", true, true) || Symbol(":=", true, true) || Symbol("+=", true, true) || {
Symbol("-=", true, true) || Symbol("*=", true, true) || Symbol("/=", true, true) || if (Symbol(sym, false, true)) {
Symbol("%=", true, true) || Symbol("<<=", true, true) || Symbol(">>=", true, true) || SkipWS(true);
Symbol("&=", true, true) || Symbol("^=", true, true) || Symbol("|=", true, true)) { if (!Equation()) {
SkipWS(true); throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename);
if (!Equation()) { }
throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename);
}
build_match<eval::Equation_AST_Node>(prev_stack_top); build_match<eval::Equation_AST_Node>(prev_stack_top, sym);
return true;
}
} }
return true;
} }
return retval; return false;
} }
/// Parses statements allowed inside of a class block /// Parses statements allowed inside of a class block