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); }
void set_code_xform_parameters( Entry e, POETCode* par) { POETCode* p = e.get_param(); if (p == 0) e.set_param(par); else { if (p != par && !match_AST(p, par, MATCH_AST_EQ)) { std::cerr << "par=" << par << "; p = " << p << "\n"; SYM_DIFF_DEFINED(par->get_className() + ":" + par->toString(OUTPUT_VAR_VAL), p->get_className() + ":" + p->toString(OUTPUT_VAR_VAL)); } return; } switch (par->get_enum()) { case SRC_LVAR: static_cast<LocalVar*>(par)->get_entry().set_code(ASTFactory::inst()->new_iconst(0)); break; case SRC_TUPLE: { POETTuple* vec = static_cast<POETTuple*>(par); for (unsigned i = 0; i < vec->size(); ++i) { LocalVar* cur_par = dynamic_cast<LocalVar*>(vec->get_entry(i)); assert (cur_par != 0); cur_par->get_entry().set_code(ASTFactory::inst()->new_iconst(i) ); } break; } default: assert(false); } }
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* EvaluatePOET:: eval_readInput_nosyntax(POETCode* inputFiles, POETCode* codeType, POETCode* inputInline) { inputFiles = eval_AST(inputFiles); std::cerr << "Using ROSE Parser for " << inputFiles->toString() << "\n"; std::vector<std::string> argvList; argvList.push_back("pcg_rose"); for (std::list<std::string>::const_iterator p_lib = lib_dir.begin(); p_lib != lib_dir.end(); ++p_lib) { std::string cur_dir=(*p_lib); argvList.push_back("-I"+cur_dir); } for (POETCode* p = inputFiles; p != 0; p = get_tail(p)) { std::string curname = p->toString(OUTPUT_NO_DEBUG); assert(curname != ""); if (curname.find("/") == curname.npos) { for (std::list<std::string>::const_iterator p_lib = lib_dir.begin(); p_lib != lib_dir.end(); ++p_lib) { std::string cur_dir=(*p_lib) + "/" + curname; if (access(cur_dir.c_str(), R_OK) == 0) { curname = cur_dir; break;} } } argvList.push_back(curname); } assert(argvList.size() > 1); SgProject *sageProject = new SgProject ( argvList); /*TODO: must add support for inserting trace handles. optimization won't work otherwise*/ return POETAstInterface::Ast2POET(sageProject); }
virtual void visitList(POETList* l) { POETCode* cur = l->get_first(); cur->visit(this) ; if (l->get_rest() != 0) { if (listsep != 0) listsep->visit(this); l->get_rest()->visit(this); } }
POETEnum get_matchEnum(POETCode* v2first) { POETEnum v2first_enum = v2first->get_enum(); if (v2first_enum == SRC_ASSIGN) { POETCode* c = static_cast<POETAssign*>(v2first)->get_rhs(); if (c != 0) { v2first_enum = c->get_enum(); } } return v2first_enum; }
virtual void visitLocalVar(LocalVar* v) { POETCode* res = v->get_entry().get_code(); if (res != 0 && res != v) { v->get_entry().set_code(0); out << v->get_entry().get_name()->toString(OUTPUT_NO_DEBUG) << "={"; res->visit(this); out << "}"; v->get_entry().set_code(res); } }
virtual void visitOperator(POETOperator *op) { if (op->get_op() == POET_OP_TYPEMATCH || op->get_op() == POET_OP_TYPEMATCH_Q) { POETBop* bop = dynamic_cast<POETBop*>(op); assert(bop != 0); POETCode* arg = bop->get_arg1(); arg->visit(this); if (arg != res) bop->set_arg1(res); res = op; } }
virtual void visitLocalVar(LocalVar* v) { LvarSymbolTable::Entry e =v->get_entry(); POETCode* code = e.get_code(); if (code != v && code != 0) code->visit(this); else { POETCode* restr = e.get_restr(); if (restr != 0) restr->visit(this); else SYM_UNDEFINED(v->toString()); e.set_code(res); } }
virtual void visitTuple(POETTuple* l) { out << "("; print_linebreak(); int size=l->size(); for (int i = 0; i < size; ++i) { POETCode* cur = l->get_entry(i); if (cur == 0) out << "NULL,"; else if (cur->get_enum() == SRC_LIST) { col+=2; out << "("; cur->visit(this); out << ")"; col-=2; } else cur->visit(this); if (i < size-1) { out << ","; print_linebreak(); } } out << ")"; }
virtual void visitList(POETList* l) { POETCode* cur = l->get_first(); /* if (cur->get_enum() != SRC_LIST && listelem != 0 && !match_AST(cur, listelem,MATCH_AST_EQ)) { cur = ASTFactory::inst()->new_codeRef(listelem->get_entry(), cur); } */ cur->visit(this) ; if (l->get_rest() != 0) { if (listsep != 0) listsep->visit(this); l->get_rest()->visit(this); } }
std::string POETTuple :: toString(ASTOutputEnum config) { std:: stringstream r; r << "("; for (unsigned i = 0; i < impl.size(); ++i) { POETCode* cur = impl[i]; if (cur == 0) r << "NULL,"; else if (cur->get_enum() == SRC_LIST) r << "(" << cur->toString(config) << "),"; else r << cur->toString(config) << ","; } std::string str = r.str(); str[str.size()-1] = ')' ; return str; }
std::string LocalVar:: toString(ASTOutputEnum config) { std::string str = entry.get_name()->toString(OUTPUT_NO_DEBUG) ; if ((config & OUTPUT_VAR_VAL) || ((config & OUTPUT_TRACE_VAL) && entry.get_entry_type() == LVAR_TRACE)) { POETCode* res = entry.get_code(); if (res != 0 && res != this) { entry.set_code(0); str = str + "={" + res->toString(config) + "}"; entry.set_code(res); } } if (config & OUTPUT_VAR_RESTR && entry.get_restr() != 0) { str = "(" + str + ":" + entry.get_restr()->toString() + ")"; } return str; }
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; } }
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; }
void output(POETString* s) { ASTFactory* fac = ASTFactory::inst(); std::string r = s->get_content(); if (r == "\n") { for (int i = 0; i < align; ++i) r = r + " "; s = fac->new_string(r); } if (output_xform == 0) output_content(r,r); else { POETCode* args = fac->new_pair(s, fac->new_iconst(cur_pos)); if (!match_parameters(output_xform->get_entry().get_param(), args, MATCH_PAR_MOD_CODE)) INCORRECT_XFORM_DEF("output(string,cur_pos)", output_xform->toString()); XformVarInvoke invoke(output_xform->get_entry(), output_xform->get_config(), args); POETCode* res = invoke.eval(0); output_content(res->toString(OUTPUT_NO_DEBUG),r); } }
void apply (POETCode* output) { while (output != 0) { POETList* outputList = dynamic_cast<POETList*>(output); if (outputList != 0) { apply(outputList->get_first()); output = outputList->get_rest(); } else { if (output->get_enum() == SRC_STRING) { out << output->toString(OUTPUT_NO_DEBUG); } else { POETCode* res = eval_AST(output); res->visit(this); } output = 0; } } }
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; }
void ReadFiles(POETCode* _files, LexState _lexStateStart, std::list<POETProgram*>& resultFiles) { POETCode* files = eval_AST(_files); POETCode* p_files=files; while (p_files != 0) { POETList* fileList = dynamic_cast<POETList*>(p_files); POETCode* fileCur = p_files; if (fileList != 0) { fileCur = fileList->get_first(); p_files=fileList->get_rest(); } else p_files = 0; std::string fname= fileCur->toString(OUTPUT_NO_DEBUG); if (fname == "") { std::cerr << "Empty file name: " << fileCur->toString() << " from list " << files->toString() << "\n"; } lexState = _lexStateStart; POETProgram* programFile = process_file(fname.c_str()); resultFiles.push_back(programFile); lexState=LEX_DEFAULT; } }
virtual void visitCodeVar(CodeVar* v) { try { POETCode* _listsep = listsep; CodeVar* _listelem = listelem; POETCode* result = v->invoke_output(v->get_args()); if (result != 0 && result != v) { listsep = 0; listelem=0; result->visit(this); listsep = _listsep; listelem=_listelem; return; } CvarSymbolTable::Entry e = v->get_entry(); LvarSymbolTable *local = 0; POETCode* f = 0; POETCode *parse = e.get_parse(); if (parse != 0 && parse->get_enum()==SRC_OP) { POETOperator* op = static_cast<POETOperator*>(parse); if (op->get_op() == POET_OP_LIST || op->get_op()==POET_OP_LIST1) { listsep = op->get_arg(1); listelem=dynamic_cast<CodeVar*>(op->get_arg(0)); f = v->get_args(); } } if (f == 0) { listsep = 0; listelem=0; f = e.get_code(); if (f == 0) { if (parse==0) { CODE_SYNTAX_UNDEFINED(v->toString()); } else f = v->get_args(); } else { local = e.get_symTable(); if (local != 0) local->push_table(false); POETCode* pars = e.get_param(); if (pars != 0 && !match_parameters(pars, v->get_args(),MATCH_PAR_MOD_CODE)) CVAR_MISMATCH(v,pars, v->get_args()); v->set_attr(); } } if (f == 0) { CODE_SYNTAX_UNDEFINED(v->toString()); } int save = align; align = start_pos; f->visit(this); align = save; if (local != 0) local->pop_table(); listsep = _listsep; listelem=_listelem; } catch (Error err) { std::cerr << " From unparsing code template " << SHORT(v->toString(),500) << "\n"; throw err; } }
virtual void visitList(POETList* l) { POETCode* first = l->get_first(); int prevline = line; if (first->get_enum() == SRC_LIST) {out << "("; first->visit(this); out << ")"; } else first->visit(this); if (line > prevline) print_linebreak(); POETCode* rest = l->get_rest(); if (rest != 0) { rest->visit(this); } else out << " NULL"; }
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; } } }
virtual void visitOperator(POETOperator *op) { switch (op->get_op()) { case POET_OP_TUPLE: { parseTuple(op->get_arg(0)); break; } case TYPE_LIST1: case TYPE_LIST: { POETCode* arg = op->get_arg(0); POETCode* matchSave = fullmatch; fullmatch = EMPTY; std::vector<POETCode*> match_res; do { POETCode* r1_first=match_eval(arg); if (r1_first == 0) { fullmatch = matchSave; if (fullmatch != 0) { res = Vector2List(match_res); if (res == EMPTY && op->get_op() == TYPE_LIST1) PARSE_MISMATCH(r1,op,lineno); return; } else PARSE_MISMATCH(r1,op,lineno); } match_res.push_back(r1_first); } while (r1 != 0); fullmatch = matchSave; if (match_res.size() == 0) PARSE_MISMATCH(r1,op,lineno); res = Vector2List(match_res); assert(res != EMPTY); return; } case TYPE_TOR: { POETCode* r1_first = get_head(r1); if (r1_first->get_enum() == SRC_CVAR && static_cast<CodeVar*>(r1_first)->get_parseInfo() != EMPTY) { /*QY: r1_first is already parsed and is not a token*/ if (!match_eval(op)) PARSE_MISMATCH(r1,op,lineno); return; } if (!backtrack) { POETTypeTor* tor = static_cast<POETTypeTor*>(op); POETCode* arg = tor->get_parseInfo(this, r1); apply(arg, fullmatch); if (fullmatch != 0) fullmatch=EMPTY; return; } else { POETCode* r1save = r1; size_t size = op->numOfArgs(); for (unsigned i = 0; i < size; ++i) { POETCode* arg = op->get_arg(i); POETCode* filter = 0; switch (arg->get_enum()) { case SRC_STRING: if (arg != EMPTY) filter = arg; break; case SRC_CVAR: filter=static_cast<CodeVar*>(arg)->get_parseInfo(); if (filter != 0 && filter->get_enum() == SRC_LIST) filter = static_cast<POETList*>(filter)->get_first(); else filter = 0; break; default: ; /* no filtering */ } if (filter != 0 && filter->get_enum()==SRC_STRING && filter != r1_first) { continue; } try { apply(arg, EMPTY); if (res != 0) return; } catch (ParseError err) { r1 = r1save; } } } PARSE_MISMATCH(r1,op,lineno); return; } case POET_OP_EXP: { ParseExp::Result exp_res = ParseExp::inst()->parse(r1, &lineno); res = exp_res.first; if (res == 0) PARSE_MISMATCH(r1,op,lineno); if (fullmatch != 0) r1 = exp_res.second; else if (exp_res.second != 0 && exp_res.second != EMPTY) { PARSE_MISMATCH_LEFT(exp_res.second,lineno); } else r1 = 0; break; } case POET_OP_LIST1: case POET_OP_LIST: { POETParseList* oplist = dynamic_cast<POETParseList*>(op); assert(oplist != 0); std::string sepString; POETCode* sep = op->get_arg(1); if (sep != 0) { sep = eval_AST(sep); POETString* sep1 = AST2String(sep); if (sep1 == 0) INCORRECT_STRING(sep); sepString = sep1->get_content(); } else sep = EMPTY; if (IS_SPACE(sepString)) sep = EMPTY; try { parseList(oplist, op->get_arg(0), sep); } catch (ParseError err) { if (op->get_op() == POET_OP_LIST && backtrack && fullmatch != 0) res = EMPTY; else throw err; } break; } default: defaultVisit(eval_AST(op)); } }