Exemplo n.º 1
0
ParseExp::Result ParseExp::ParseItemType(POETCode* input, int *p_lineno)
   {
      POETCode* res = 0, *tail=0;
      if ( get_head(input) == lp) {
           Result resOfRest = ParseExpImpl(NextToken(input),exp_bop->get_entry().get_code(),0, p_lineno);
           if (get_head(resOfRest.second) == rp) {
                 input = NextToken(resOfRest.second);
                 res = resOfRest.first;
           }
           else return Result(0,input);
      }
      else if (match_expItem(input)) {
          try {
           res = parse_AST(input, exp_item->get_entry().get_code(), &tail);
           assert(res != 0);
           input=SkipEmpty(tail, p_lineno);
          }
          catch (ParseError err) { 
                   if (backtrack) return Result(0,input); 
                   throw err;
             }
      }
      else return Result(0,input);
     if (funcall->get_entry().get_code() != 0 && get_head(input) == lp)
     {
        CodeVar* fvar = dynamic_cast<CodeVar*>(funcall->get_entry().get_code());
        if (fvar == 0) INCORRECT_CVAR(funcall->get_entry().get_code());
        Result resOfTail = ParseExpImpl(NextToken(input),exp_bop->get_entry().get_code(),0, p_lineno);
        std::vector<POETCode*> argVec; 
        while (resOfTail.first!=0 && (get_head(resOfTail.second) != rp)) {
              argVec.push_back(resOfTail.first);
              resOfTail=ParseExpImpl(NextToken(resOfTail.second),exp_bop->get_entry().get_code(),0, p_lineno);
        }
        argVec.push_back(resOfTail.first);
        POETCode* args = Vector2List(argVec);
        if (args == 0) args = EMPTY;
        return Result(CODE_REF(fvar,PAIR(res,args)),
                      NextToken(resOfTail.second));
     }
     else if (arrref->get_entry().get_code() != 0 && get_head(input) == lb )
     {
        CodeVar* fvar = dynamic_cast<CodeVar*>(arrref->get_entry().get_code());
        if (fvar == 0) INCORRECT_CVAR(arrref->get_entry().get_code());
        std::vector<POETCode*> argVec; 
        while (get_head(input) == lb) 
        {
          Result resOfTail = ParseExpImpl(NextToken(input),exp_bop->get_entry().get_code(),0, p_lineno);
          if (resOfTail.first==0 || get_head(resOfTail.second) != rb) 
              return Result(res,input);
          argVec.push_back(resOfTail.first);
          input = NextToken(resOfTail.second);
        }
        POETCode* args = Vector2List(argVec);
        if (args == 0) args = EMPTY;
        return Result(CODE_REF(fvar,PAIR(res,args)),input);
     }
     return Result(res, input);
  }
Exemplo n.º 2
0
POETCode* EvaluatePOET::parse_input(POETCode* input, POETCode* pattern)
{
   static double token_time = 0, prep_time=0, parse_time=0;
   static bool  first=true;

   bool dt = debug_time;
if (dt) {
 if (first) {
    register_timing(&token_time, "time spent in tokenizer:");
    register_timing(&prep_time, "time spent in parse preparation:");
    register_timing(&parse_time, "time spent in parsing and AST construction:");
 }
first=false;
}

   if (prep->get_entry().get_code() != 0) {
      double cur = (dt?GetWallTime():0);
      XformVar* prep_xform = dynamic_cast<XformVar*>(prep->get_entry().get_code());
      if (prep_xform == 0) INCORRECT_XVAR(prep);
      input = prep_xform->eval(input);
      if (dt) prep_time +=GetWallTime()-cur;
   }
   if (tokens->get_entry().get_code() != 0) {
      double cur = (dt?GetWallTime():0);
      ApplyTokenOperator op(tokens->get_entry().get_code());
      input = op.apply(input);
      if (dt) token_time +=GetWallTime()-cur;
   }
   input = eval_AST(input);
   if (pattern == 0) pattern = parseTarget->get_entry().get_code();
   if (pattern == 0) return input; 
   switch (pattern->get_enum())
    {
    case SRC_READ_INPUT: return input;
    default:
       try { 
          double cur = (dt?GetWallTime():0);
          POETCode* result = parse_AST(input, pattern);
          if (dt) parse_time +=GetWallTime()-cur;
            return result;
          }
       catch (ParseError err) { EXIT(err.message()); }
   }
}