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); }
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* 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; } }
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 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"; }
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 << ")"; }
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; }
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* 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 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)); } }