POETCode* eval_cvar_attr(CodeVar* cv, LocalVar* lv) { CvarSymbolTable::Entry cv_e = cv->get_entry(); LvarSymbolTable* symTable = cv_e.get_symTable(); LvarSymbolTable::Entry e = lv->get_entry(); if (symTable->find(e.get_name()) != lv) return 0; POETCode* res = 0; switch (e.get_entry_type()) { case LVAR_CODEPAR: res = eval_tuple_access(cv->get_args(), e.get_code()); break; case LVAR_ATTR: for (int i = 0; i < cv_e.attr_size(); ++i) { if (cv_e.get_attr(i) == lv) { POETCode* tuple = cv->get_attr(); if (tuple == 0) res = e.get_code(); else res = eval_tuple_access(tuple,i); break; } } break; default: /*QY: access is a local variable. treat this as tuple access*/ res = eval_tuple_access(cv, lv); } return res; }
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); }
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 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; } }
virtual void visitLocalVar(LocalVar* v) { if (v->get_entry().get_entry_type() == LVAR_TRACE) res = v; else { res = v->get_entry().get_code(); if (res == 0) { SYM_UNDEFINED(v->toString()); } if (res != v) { while (res->get_enum() == SRC_LVAR) { LvarSymbolTable::Entry e = static_cast<LocalVar*>(res)->get_entry(); if (e.get_entry_type() == LVAR_TRACE) break; POETCode* code = e.get_code(); if (code == 0) SYM_UNDEFINED(res->toString()); if (code == res) break; res = code; } } } }
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; }