Advertisement
Guest User

Scanner MathParser.cc

a guest
Sep 17th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. AST::NodeT MathParser::parse_binary_operation(bool B_allow_args, int precedence_level) {
  2.     struct AST::Symbol* associativity;
  3.     bool B_visible_operator, B_unary_operator = false;
  4.     //printf("level is %d, input is: %s\n", precedence_level, input_value->str().c_str());
  5.     if(operator_precedence_list->empty_P(precedence_level))
  6.         return(B_allow_args ? parse_application() : parse_value());
  7.     /* special case for unary - */
  8.     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));
  9.     if(AST::NodeT actual_token = operator_precedence_list->match_operator(precedence_level, input_value, /*out*/associativity, /*out*/B_visible_operator)) {
  10.         while(actual_token && actual_token != Symbols::SlessEOFgreater) {
  11.             AST::NodeT operator_ = B_visible_operator ? consume() : Symbols::Sspace;
  12.             if(input_value == Symbols::Srightparen || input_value == Symbols::Sautorightparen) { // premature end.
  13.                 if(!B_visible_operator)
  14.                     raise_error("<operand>", ")");
  15.                 return(B_unary_operator ? operator_ : makeApplication(operator_, result)); /* default to the binary operator */
  16.             }
  17.             AST::NodeT b = parse_binary_operation(B_allow_args, associativity != Symbols::Sright ? operator_precedence_list->next_precedence_level(precedence_level) : precedence_level);
  18.             if(B_unary_operator && !b) // -nil
  19.                 return(operator_);
  20.             result = operation(operator_, result, b);
  21.             /* 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. */
  22.             if(associativity == Symbols::Snone)
  23.                 break;
  24.             actual_token = operator_precedence_list->match_operator(precedence_level, input_value, /*out*/associativity, /*out*/B_visible_operator);
  25.         }
  26.         return(result);
  27.     } else
  28.         return(result);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement