Exemplo n.º 1
0
 virtual void visitLocalVar(LocalVar* var) 
   {
      LvarSymbolTable::Entry e = var->get_entry();
      POETCode *restr = e.get_restr();
      if (restr == 0) {
         CollectInfoVisitor::visitLocalVar(var);
         return;
      }
      if (restr->get_enum() == SRC_CVAR) {
         CodeVar* cvar = static_cast<CodeVar*>(restr);
         POETCode* code = e.get_code();
         if (code!=0 && !match_AST(code, cvar,MATCH_AST_EQ)) {
            CodeVar* tmp = ASTFactory::inst()->new_codeRef(cvar->get_entry(), code); 
            tmp->visit(this); 
            return; 
         }
      }
      else if (restr->get_enum() == SRC_OP) {
          POETOperator* op = static_cast<POETOperator*>(restr);
          if (op->get_op() == POET_OP_LIST || op->get_op()==POET_OP_LIST1)  {
              POETCode* _listsep = listsep;
              CodeVar* _listelem = listelem;
              int save = align;
              align = start_pos;
              listsep = op->get_arg(1);
              listelem=dynamic_cast<CodeVar*>(op->get_arg(0));
              CollectInfoVisitor::visitLocalVar(var);
              align = save; listsep = _listsep; listelem = _listelem;
              return;
           }
      }
      CollectInfoVisitor::visitLocalVar(var);
   }
Exemplo n.º 2
0
 virtual void visitTupleAccess(TupleAccess* fc) 
    { 
     try {
      POETCode* tuple = apply(fc->get_tuple());
      POETCode* tuple1 = EvalTrace(tuple);
      if (tuple1 == 0) SYM_UNDEFINED(tuple->toString())
      else tuple = tuple1;
      POETCode* access= EvalTrace(fc->get_access()); 
      switch (tuple->get_enum()) {
        case SRC_TUPLE: {
           access= apply(access);
           if (access->get_enum() == SRC_LVAR) {
             LvarSymbolTable::Entry e = static_cast<LocalVar*>(access)->get_entry();
             if (e.get_entry_type() == LVAR_OUTPUT) access = e.get_code(); 
             else TUPLE_ACC_MISMATCH(fc, tuple,access);
           }
           res = eval_tuple_access(tuple,access);
           if (res == 0) { TUPLE_ACC_MISMATCH(fc, tuple,access); }
           return;
         }
        case SRC_MAP: {
           POETMap* m = static_cast<POETMap*>(tuple);
           access= apply(access);
           res = m->find(access);
           if (res == 0) 
               res = EMPTY;
           return;
        }
        case SRC_CVAR: {
           CodeVar* cv = static_cast<CodeVar*>(tuple);
           CvarSymbolTable::Entry cv_e = cv->get_entry();
           LvarSymbolTable* symTable = cv_e.get_symTable();
           res = eval_cvar_access(cv, access);
           return;
        }
        default: { 
           res = eval_tuple_access(tuple,access);
           if (res == 0)
            TUPLE_ACC_MISMATCH( fc, tuple, access); 
          }
       }
     }
     catch (Error err) { std::cerr << "From invoking " << SHORT(fc->toString(),500) << "\n"; throw err; }
    } 
Exemplo n.º 3
0
void POETProgram:: write_to_file(std::ostream& out)
{  
  int lineNo=0;
  for (std::list<POETProgram*>::const_iterator 
           pincludeFiles = includeFiles.begin();
           pincludeFiles!=includeFiles.end(); ++pincludeFiles) {
        POETProgram* cur = (*pincludeFiles);
        out << "include " << cur->get_filename() << "\n";
        ++lineNo;
  }
  for (const_iterator pdef = define.begin(); pdef != define.end(); ++pdef) {
       POETCode* cur = *pdef;
       const FileInfo* curFile = cur->get_fileinfo();
       if (curFile != 0) {
           while (lineNo < curFile->get_lineno()) {
               out << "\n"; ++lineNo;
           }
       }
       switch (cur->get_enum())
       {
       case SRC_WRITE_OUTPUT: case SRC_READ_INPUT: case SRC_PARAM_DECL:
       case SRC_TRACE_DECL:
          out << cur->toString(DEBUG_NO_VAR) << "\n"; ++lineNo; break;
       case SRC_CVAR: 
         {
          CodeVar* cvar = static_cast<CodeVar*>(cur);
          out << "<code " << cvar->toString();
          const CodeDefSave& def = codeDef[cvar];
          unsigned par_size = def.par_type.size();
          if (par_size > 0) {
             out << " pars=(";
             for (unsigned i = 0; i < par_size; ++i) 
             {
               const LocalVarSave& par_save = def.par_type[i];
               assert(par_save.lvar != 0); 
               out << par_save.lvar->get_entry().get_name();
               if (par_save.restr != 0)
                   out << ":" << par_save.restr->toString(DEBUG_NO_VAR);
               if (i < par_size) out << ",";
             } 
             out << ")";
          }
          CvarSymbolTable::Entry cvar_entry = cvar->get_entry();
          for (unsigned i = 0; i < cvar_entry.attr_size(); ++i) {
             LocalVar* v = cvar_entry.get_attr(i); 
             out << " " << v->get_entry().get_name()->toString(OUTPUT_NO_DEBUG) << "=" << v->get_entry().get_code()->toString(DEBUG_NO_VAR);
          } 
          out << "/>\n";++lineNo;  
          break;
         } 
       case SRC_LVAR:
         {
          LocalVar* cur_var = static_cast<LocalVar*>(cur);
          switch (cur_var->get_entry().get_entry_type())
          {
           case LVAR_TUNE:
              out << "<define " << cur->toString(OUTPUT_NO_DEBUG) << "/>\n";
              ++lineNo;  
              break;
           default:
                std::cerr << "Something is wrong. Unexpected var decl at global scope: " << cur->toString(DEBUG_OUTPUT) << "\n"; 
               assert(0);
          }
          break;
         }
       case SRC_EVAL: // eval command
          if (dynamic_cast<POETCondExp*>(cur) != 0)
               out << "<cond ";
          else out << "<eval ";
          Write_POET_Exp write(out,6);
          lineNo += write(cur);
          out << "/>\n"; ++lineNo;
      }
   }
}
Exemplo n.º 4
0
/*QY: return the min length of tokens placed in the filter res*/
unsigned EvaluatePOET::
compute_lookaheadInfo(POETCode* cur, std::vector<POETCode*>& res, unsigned need)
{
 try {
    assert(cur != 0);
    switch (cur->get_enum()) {
     case SRC_TYPE: 
         if (cur != ANY) { res.push_back(cur); return 1; }
         return 0;
     case SRC_STRING: 
         if (cur != EMPTY) { res.push_back(cur); return 1; }
         return 0;
     case SRC_ICONST:  
         res.push_back(cur); res.push_back(AST2String(cur)); 
         return 1;
     case SRC_CVAR:  {
         CodeVar* cvar = static_cast<CodeVar*>(cur);
         POETCode* parse=cvar->get_parseInfo();
         unsigned lookahead = cvar->get_entry().get_lookahead();
         if (need < lookahead) need = lookahead;
         if (parse != EMPTY) {
            unsigned len = compute_lookaheadInfo(parse, res, need);
            if (lookahead > 1 && len < lookahead) 
               { LOOKAHEAD_AMBIGUOUS(cvar); return 0; }
            return len;
         }
         res.push_back(cvar);  /*QY: the code var is a token */
         return 1;
        }
     case SRC_LVAR: {
          LocalVar* lvar = static_cast<LocalVar*>(cur);
          POETCode* restr = 0;
          switch (lvar->get_entry().get_entry_type())
          {
            case LVAR_CODEPAR: case LVAR_ATTR:
                 restr = lvar->get_entry().get_restr(); break;
            default: restr = lvar->get_entry().get_code(); 
                 break;
          }
          if (restr == 0) { LOOKAHEAD_AMBIGUOUS(cur); return 0; }
          else return compute_lookaheadInfo(restr,res,need); 
        }
     case SRC_LIST: {
         POETList* curlist = static_cast<POETList*>(cur);
         int curstart = res.size();
         unsigned len = compute_lookaheadInfo(curlist->get_first(),res,need);
         cur = curlist->get_rest();
         if (cur == 0) return len;
         int cursize = res.size();
         if (need == 1) {
            if (len == 0) return compute_lookaheadInfo(cur, res, need);
            return len;
         }
         if (len < need ) {
            std::vector<POETCode*> tailres;
            unsigned len2 = compute_lookaheadInfo(cur, tailres, need-len);
            ASTFactory* fac = ASTFactory::inst();
            for (int i = curstart; i < cursize; ++i) {
               POETCode* first = res[i]; 
               res[i] = fac->append_tuple(first,tailres[0]);
               for (unsigned j = 1; j < tailres.size(); ++j) 
                   res.push_back(fac->append_tuple(first,tailres[j]));
            }
            return len+len2;
         }
         return len;
        }
     case SRC_OP : {
        POETOperator* op = static_cast<POETOperator*>(cur);
        switch(op->get_op()) {
        case TYPE_TOR: {
            POETTypeTor* tor = static_cast<POETTypeTor*>(cur);
            return tor->compute_lookaheadInfo(this, res, need);
           }
        case POET_OP_LIST1: 
        case TYPE_LIST1: 
            return compute_lookaheadInfo(op->get_arg(0),res,need);
        case POET_OP_LIST: 
        case TYPE_LIST: 
            compute_lookaheadInfo(op->get_arg(0),res); return 0;
        case POET_OP_EXP:
            return compute_exp_lookahead(res);
        default:
           LOOKAHEAD_AMBIGUOUS(cur); return 0;
        }
       }
     default:
        LOOKAHEAD_AMBIGUOUS(cur); return 0;
     }
   }
   catch (Error err) { std::cerr << " From computing filter information for " << cur->toString() << "\n"; throw err; }
}