Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AST::NodeT MathParser::parse_binary_operation(bool B_allow_args, int precedence_level) {
- struct AST::Symbol* associativity;
- bool B_visible_operator, B_unary_operator = false;
- //printf("level is %d, input is: %s\n", precedence_level, input_value->str().c_str());
- if(operator_precedence_list->empty_P(precedence_level))
- return(B_allow_args ? parse_application() : parse_value());
- /* special case for unary - */
- AST::NodeT result = (precedence_level == operator_precedence_list->minus_level && input_value == Symbols::Sdash) ? (B_unary_operator = true, Symbols::Szero) : parse_binary_operation(B_allow_args, operator_precedence_list->next_precedence_level(precedence_level));
- if(AST::NodeT actual_token = operator_precedence_list->match_operator(precedence_level, input_value, /*out*/associativity, /*out*/B_visible_operator)) {
- while(actual_token && actual_token != Symbols::SlessEOFgreater) {
- AST::NodeT operator_ = B_visible_operator ? consume() : Symbols::Sspace;
- if(input_value == Symbols::Srightparen || input_value == Symbols::Sautorightparen) { // premature end.
- if(!B_visible_operator)
- raise_error("<operand>", ")");
- return(B_unary_operator ? operator_ : makeApplication(operator_, result)); /* default to the binary operator */
- }
- AST::NodeT b = parse_binary_operation(B_allow_args, associativity != Symbols::Sright ? operator_precedence_list->next_precedence_level(precedence_level) : precedence_level);
- if(B_unary_operator && !b) // -nil
- return(operator_);
- result = operation(operator_, result, b);
- /* for right associative operations, the recursion will have consumed all the operators on that level and by virtue of that, the while loop will always stop after one iteration. */
- if(associativity == Symbols::Snone)
- break;
- actual_token = operator_precedence_list->match_operator(precedence_level, input_value, /*out*/associativity, /*out*/B_visible_operator);
- }
- return(result);
- } else
- return(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement