LocalVar* POETProgram:: set_local_static(LvarSymbolTable* local, POETCode* id, LocalVarType t, POETCode* code, POETCode * restr, bool insert) { assert(local != 0); LocalVar* lvar = local->find(id); POETCode* orig = 0; if (lvar == 0) { if (insert) { lvar = local->insert(id, t); lvar->get_entry().set_code(code); lvar->get_entry().set_restr(restr); } else SYM_UNDEFINED(id->toString()+"="+code->toString()); } else if (code != 0 && (orig = lvar->get_entry().get_code()) != 0 && orig != code && !match_AST(orig, code, MATCH_AST_EQ)) { SYM_DIFF_DEFINED(id->toString()+"="+code->toString(), orig->toString()); } else { if (code != 0) lvar->get_entry().set_code(code); if (restr != 0) lvar->get_entry().set_restr(restr); } return lvar; }
POETCode* CodeVar::get_static_attr(const std::string& name) const { LvarSymbolTable* local = get_entry().get_symTable(); LocalVar* b = local->find(ASTFactory::inst()->new_string(name)); if (b != 0) return b->get_entry().get_code(); return 0; }
extern "C" POETCode* make_localPar(POETCode* id, POETCode* restr, LocalVarType type) { try { LvarSymbolTable* local = top_scope(); assert(local != 0); LocalVar* res = local->insert(id, type); POETCode* restr1 = res->get_entry().get_restr(); if (restr != 0 && restr1 != 0 && restr != restr1 && !match_AST(restr,restr1, MATCH_AST_EQ)) { SYM_DIFF_DEFINED(id->toString() + ":" + restr->get_className() + ":" + restr->toString(),restr1->get_className() + ":" + restr1->toString()); return res; } if (restr != 0) res->get_entry().set_restr(restr); return res; } catch (Error err) { std::cerr << "\nAt line " << yylineno << " of file " << curfile->get_filename() << "\n"; exit(1); } }
void CodeVar:: set_attr() { if (attr != 0) { POETTuple * attrTuple = dynamic_cast<POETTuple*>(attr); if (attrTuple == 0) { LocalVar* var = entry.get_attr(0); assert (var != 0); var->get_entry().set_code(attr); } else { unsigned size = attrTuple->size(); for (unsigned i = 0; i < size; ++i) { LocalVar* var = entry.get_attr(i); assert(var != 0); var->get_entry().set_code(attrTuple->get_entry(i)); } } } }
POETCode* TraceEval(POETCode* vars, POETCode* res) { std::vector<LocalVar*> vec; switch (vars->get_enum()) { case SRC_LVAR: { LocalVar* lvar = static_cast<LocalVar*>(vars); vec.push_back(lvar); break; } case SRC_LIST: { POETList* l = static_cast<POETList*>(vars); while (l != 0) { POETCode* cur = l->get_first(); if (cur->get_enum() != SRC_LVAR) INCORRECT_TRACE_HANDLE(cur->toString()); vec.push_back(static_cast<LocalVar*>(cur)); l = dynamic_cast<POETList*>(l->get_rest()); } } break; default: INCORRECT_TRACE_HANDLE(vars->toString()); } for (int i = vec.size()-1; i >= 0; --i) { LocalVar* lvar = vec[i]; LvarSymbolTable::Entry entry = lvar->get_entry(); entry.push(true); entry.set_entry_type(LVAR_TRACE); } res = eval_AST(res); for (int i = vec.size()-1; i >= 0; --i) { LocalVar* lvar = dynamic_cast<LocalVar*>(vec[i]); assert(lvar != 0); LvarSymbolTable::Entry entry = lvar->get_entry(); POETCode* val = entry.get_code(); entry.pop(); entry.set_code(val); } return res; }
LocalVar* LvarSymbolTable::insert(POETCode* var, LocalVarType _t) { LocalVar* res = find(var); if (res == 0) { if (_t == LVAR_UNKNOWN) _t = LVAR_REG; Entry r(impl.insert(var),_t); res = new LocalVar(r); r.set_var(res); } else if (_t != LVAR_UNKNOWN) { res->get_entry().set_entry_type(_t); } return res; }
virtual void defaultVisit(POETCode* val) { int size = traceVars.size(); LocalVar *v = traceVars[curVar]; if (curVar >= size || EvalTrace(v) != val) { return; } res = v; while (++curVar < size) { LocalVar* trace = traceVars[curVar]; if (EvalTrace(trace) != val) return; if (trace != v) { v->get_entry().set_code(trace); v = trace; } } }
POETCode* EraseTrace(POETCode* vars, POETCode* res) { std::vector<LocalVar*> vec; switch (vars->get_enum()) { case SRC_LVAR: { vars = eval_AST(vars); if (vars->get_enum() != SRC_LVAR) break; LocalVar* lvar = static_cast<LocalVar*>(vars); if (lvar->get_entry().get_entry_type() == LVAR_TRACE) vec.push_back(lvar); break; } case SRC_LIST: { POETList* l = static_cast<POETList*>(vars); while (l != 0) { POETCode* cur = l->get_first(); if (cur->get_enum() == SRC_LVAR) { cur = eval_AST(cur); if (cur->get_enum() == SRC_LVAR) { LocalVar* cur_lvar = static_cast<LocalVar*>(cur); if (cur_lvar->get_entry().get_entry_type() == LVAR_TRACE) vec.push_back(cur_lvar); } } l = dynamic_cast<POETList*>(l->get_rest()); } } break; default: return res; } int size = vec.size(); if (size == 0) return res; for (int i = size-1; i >= 0; --i) { LvarSymbolTable::Entry e = vec[i]->get_entry(); assert (e.get_entry_type() == LVAR_TRACE); e.set_entry_type(LVAR_TRACE_OUTDATE); } res = ReplInfoVisitor().apply(res); for (int i = size-1; i >= 0; --i) { LvarSymbolTable::Entry e = vec[i]->get_entry(); e.set_entry_type(LVAR_TRACE); } return res; }
POETCode* POETProgram:: make_Iconst( char* text, int len, bool parseInput) { if (parseInput) { LocalVar* config = find_macroVar(ASTFactory::inst()->new_string("PARSE_INT")); POETCode* res = 0; if (config == 0 || (res = config->get_entry().get_code()) == 0 || !AST2Bool(res)) { std::string val; for (int i = 0; i < len; ++i) val.push_back(text[i]); return ASTFactory::inst()->new_string(val); } } POETCode *res = string2Iconst(text, len); if (res == 0) { std::cerr << "Expecting an integer string but get : "; for (int i = 0; i < len; ++i) std::cerr << text[i]; std::cerr << "\n"; assert(0); } return res; }
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; } } }
/*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; } }