Atom* lookupVariable(Cons* env, char* name) { Cons* parent = CARCONS(env); while(CDR(env) && (env = CDRCONS(env))) { Cons* pair = CARCONS(env); // (name . var) if(strcmp(CAR(pair)->value.symbol, name) == 0) return CDR(pair); } if(parent) return lookupVariable(parent, name); return 0; }
sExpression *lookupVariable(sSymbol *symbol, sEnvironment *env){ int i = 0; int keySize = strlen(symbol->name) * sizeof(char); TCXSTR *str = getTypeTag(symbol->name); void *result = tcmapget(env->varmap, symbol->name, keySize, &i); if(i == 0){ //fail find if(isNoParent(env)){ return &sNull; }else{ return lookupVariable(symbol, env->parent); } }else{ //success find sType *type = (sType *)tcmapget(env->varmap, tcxstrptr(str), tcxstrsize(str), &i); sExpression *resultExp = newExp(result, *type); return resultExp; } }
sExpression *eval(sExpression *exp, sEnvironment *env){ /* ------------------atom-----------------------*/ /* 1, 10, false, null, "abc" */ if(isSelfEval(exp)) { return exp; } /* a symbol */ else if(isVariable(exp, env)) { return lookupVariable(toSymb(exp), env); } /* ------------------list-----------------------*/ /* (quote blur blur) */ else if(isQuoted(exp)) { return textOfQuoted(exp); } /* (set! name value) */ else if(isAssignment(exp)) { return evalAssignment(exp, env); } /* (define name value) */ else if(isDefinition(exp)) { return evalDefine(exp, env); } /* (define-syntax name ...) */ else if(isDefinitionSyntax(exp)) { return evalDefineSyntax(exp, env); } /* (if blur blur blur) */ else if(isIf(exp)) { return evalIf(toList(exp), env); } /* (lambda (args) (body)) */ else if(isLambdaConst(exp)) { sList *body; sList *param = toList( cadr(toList(exp))); sExpression *temp = cdr(toList( cdr(toList(exp)))); if(isList(temp)){ body = toList(temp); }else{ body = toList(cons(temp, &sNull)); } return newLambda(param, body, env); } /* (syntax blur blur) syntax rule */ else if(isSymbol(car(toList(exp))) && isSyntaxRule(eval(car(toList(exp)), env))) { sExpression *exp2 = evalSyntaxRule(toSyntax(eval(car(toList(exp)), env)), exp); return eval(exp2, env); } /* the other list (x . y) */ else if(isApplication(exp)) { if(LAZY_EVAL){ sExpression *proexp = actualValue(operator(toList(exp)), env); if(isLambdaType(proexp) || isPrimitiveProc(proexp)){ sExpression *operand = operands(toList(exp)); return applyLazly(proexp, operand, env); } }else{ sExpression *proexp = eval(operator(toList(exp)), env); if(isLambdaType(proexp) || isPrimitiveProc(proexp)){ sExpression *operand = operands(toList(exp)); sExpression *arguments = listOfValues(operand, env); return apply(proexp, arguments, env); } } } return &sError; }
void LuaPlugin::handleScmpVarLookUpNormal(SCMP::VarLookUp *pLookUp) { if ((pLookUp == NULL) || (m_pCurHookLuaState == NULL)) return; lua_State* const luaState = m_pCurHookLuaState; const StackReconciler recon(luaState); if (!m_bLookUpWatches) { // Prepare network message based on var type switch (pLookUp->variable.what) { case LuaVariableScope::kGlobal: { const SCMP::GlobalVarLookUpBegin scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; case LuaVariableScope::kLocal: { const SCMP::LocalVarLookUpBegin scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; case LuaVariableScope::kUpvalue: { const SCMP::UpvalueVarLookUpBegin scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; case LuaVariableScope::kEnvironment: { const SCMP::EnvVarLookUpBegin scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; } } // Look up the variable lookupVariable(luaState, &(pLookUp->variable)); if (!m_bLookUpWatches) { // Prepare network message based on var type switch (pLookUp->variable.what) { case LuaVariableScope::kGlobal: { const SCMP::GlobalVarLookUpEnd scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; case LuaVariableScope::kLocal: { const SCMP::LocalVarLookUpEnd scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; case LuaVariableScope::kUpvalue: { const SCMP::UpvalueVarLookUpEnd scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; case LuaVariableScope::kEnvironment: { const SCMP::EnvVarLookUpEnd scmp(kLuaPluginId); m_pScriptMan->send((uint8_t*)&scmp, scmp.length); } break; } } }