void parseTuple( POETCode* elemType) { POETList* elemTypeList = dynamic_cast<POETList*>(elemType); if (elemTypeList==0) { std::cerr << "expecting a list but get : " << elemType->toString() << "\n"; assert(0); } POETCode* start = eval_AST(elemTypeList->get_first()); if (start != EMPTY) apply(start,EMPTY); POETCode* result = 0; POETList *p = dynamic_cast<POETList*>(elemTypeList->get_rest()); while (p != 0 && p->get_rest() != 0) { POETCode* cur = p->get_first(); p = dynamic_cast<POETList*>(p->get_rest()); assert(p != 0); POETCode* sep = eval_AST(p->get_first()); apply(cur, sep); result = fac->append_tuple(result, res); p = dynamic_cast<POETList*>(p->get_rest()); } if (p != 0) { apply(p->get_first(), fullmatch); if (fullmatch != 0) fullmatch = EMPTY; } res = result; }
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); }
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; }
virtual void visitCodeVar(CodeVar* v2) { if (match_eval(v2)) { return; } POETCode* v2parse = v2->get_parseInfo(); assert (v2parse != 0); if (v2parse == EMPTY) PARSE_MISMATCH(v2,r1,lineno); // v2 is built only through scanning POETCode* pars=0; switch (v2parse->get_enum()) { case SRC_OP: case SRC_XVAR: break; /*QY: parameters are not involved in parsing*/ default: pars = v2->get_entry().get_param(); } POETCode* r1Save = r1; try { if (pars != 0) v2->get_entry().get_symTable()->push_table(false); LocalVar* inherit_var = v2->get_entry().get_inherit_var(); if (inherit_var != 0) { inherit_var->get_entry().set_code( inherit); if (debug_parse()) std::cerr << inherit_var->toString(OUTPUT_VAR_VAL) << "\n"; } apply(v2parse,fullmatch); if (fullmatch != 0) fullmatch=EMPTY; if (pars != 0) { /*QY get the values stored in pars*/ pars=ReplaceSingletonListVisitor().apply(pars); POETCode* cond = v2->get_static_attr("cond"); if (cond != 0 && !AST2Bool(eval_AST(cond))) PARSE_MISMATCH(v2,pars,lineno); res = v2->invoke_rebuild(pars); v2->get_entry().get_symTable()->pop_table(); } else if (v2->get_entry().get_param() != 0) { if (!v2->check_cond(res)) PARSE_MISMATCH(v2,res,lineno); POETCode* rres = v2->invoke_rebuild(res); res = rres; } else if (v2->get_entry().get_code() != 0) res = fac->build_codeRef(v2->get_entry(),0); } catch (ParseError err) { if (pars != 0) v2->get_entry().get_symTable()->pop_table(); r1 = r1Save; throw err; } catch (Error err) { std::cerr << " From parsing code template " << v2->toString() << "\n"; throw err; } }
void defaultVisit(POETCode* fc) { if (fc->get_enum() == SRC_PROGRAM) { static_cast<POETProgram*>(fc)->write_to_file(out); return; } POETCode* fc_eval = eval_AST(fc); if (fc_eval == fc) { std::string res = fc->toString(OUTPUT_NO_DEBUG); output_content(res, res); } else fc_eval->visit(this); }
CodeVar* ASTFactory:: build_codeRef(CvarSymbolTable::Entry e, POETCode* args, POETCode* attr) { POETCode* pars=e.get_param(); if (pars != 0 && args != 0) { e.get_symTable()->push_table(false); if (!match_parameters(pars, args, MATCH_PAR_MOD_CODE)) CVAR_MISMATCH(e.get_name(), pars, args); } int size = e.attr_size(); if (size > 0 && attr == 0) { if (size == 1) attr = eval_AST(e.get_attr(0)->get_entry().get_code()); else { attr = new_pair(eval_AST(e.get_attr(0)->get_entry().get_code()), eval_AST(e.get_attr(1)->get_entry().get_code())); for (int i = 2; i < size; ++i) attr = append_tuple(attr, eval_AST(e.get_attr(i)->get_entry().get_code())); } } if (pars != 0 && args != 0) e.get_symTable()->pop_table(); CvarTable* m = get_cvarTable(e); return static_cast<CodeVar*>(m->new_item(args, attr)); }
virtual void visitXformVar(XformVar* v) { POETCode* _r1 = r1; try { XformVarInvoke xform(v->get_entry(), v->get_config(), r1); res = eval_AST(&xform); if (!ExamineParseFunctionResult()) { res = 0; PARSE_MISMATCH(_r1,v,lineno); } } catch (ParseError err) { r1 = _r1; throw err; } catch (Error err) { std::cerr << "From invoking " << v->toString(DEBUG_NO_VAR) << "\n"; throw err; } }
extern "C" void finish_parameter(ParameterDecl* param) { try { POETCode* code = 0; std::string name = param->get_name(); if (parmap.find(name) != parmap.end()) { code = parmap[name]; assert(code != 0); param->set_val(code, true); } else { code = param->get_default(); if (code == 0) code = EMPTY; else code = eval_AST(code); param->set_val(code, false); } } catch (Error err) { std::cerr << "At line " << yylineno << " of file " << curfile->get_filename() << "\n"; exit(1); } }
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()); } } }
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 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)); } }