// BIG IMPORTANT FUNCTION. // This is how VarStackRefs are decoded and the Variables they referenced are returned. public Variable GetVarAtIndex(VarStackRef stackRef) { if (!stackRef.isValid) { return(null); } if (null != stackRef.variable) { return(stackRef.variable); } if (!stackRef.memberRef.isInvalid) { if (stackRef.memberRef.memberType == ClassDef.MemberType.NORMAL) { int callIx = _callCount + stackRef.callIndexOffset; return(_callStack[callIx].classInstance.Get(stackRef.memberRef)); } else { return(stackRef.memberRef.classDef.GetVariable(stackRef.memberRef)); } } else if (stackRef.isGlobal) { return(_globals.Get(stackRef.varIndex)); } return(_varStack[_callStack[_callCount + stackRef.callIndexOffset].varStackStart + stackRef.varIndex]); }
// set uses this to 1) check to see if symbol already exists, and 2) caches the variable for use during evaluate // set can also be used to set global variables. // for uses this to 1) check to make sure symbol doesn't already exist, 2) push var on the stack for type checking public VarStackRef CreateTemp(string symbol, ITypeDef type, bool global = false, bool doesntCollideWithGlobal = false, bool unique = false) { if (null != GetTypeByName(symbol)) { return(new VarStackRef(VarStackRef.ErrorType.AlreadyExists)); } if (Pb.reservedWords.Contains(symbol)) { return(new VarStackRef(VarStackRef.ErrorType.ReservedSymbol)); } // Unsure about this null VarStackRef existingRef = stack.GetVarIndexByName(null, symbol, true); if (existingRef.isValid) { if (!(doesntCollideWithGlobal && existingRef.isGlobal)) { return(new VarStackRef(VarStackRef.ErrorType.AlreadyExists)); } } if (unique) { Variable var = new Variable(symbol, type, null); return(stack.AddExistingVariable(symbol, global, var)); } else { return(stack.AddVariable(symbol, global, type, null)); } }
// ******************************************************************** // Checks stack, class, and global. public bool IsSymbolAvailable(ExecContext context, string symbol, bool ignoreGlobals = false) { if (context.DoesTypeExist(symbol)) { return(false); } VarStackRef index = GetVarIndexByName(null, symbol, true); return(!index.isValid || (ignoreGlobals && index.isGlobal)); }
public bool CreateGlobal(string symbol, ITypeDef type, object value = null) { Variable existing = stack.GetGlobalVariable(symbol); // existence check if (null != existing) { return(false); } VarStackRef vsr = stack.AddVariable(symbol, true, type, value); return(null != stack.GetVarAtIndex(vsr)); // DoesVarExist }
// Call this one to create a variable *during execution*. // Doesn't do error checking, doesn't care if variable already exists. // If you aren't sure what you are doing, you probably want to use CreateGlobal. public Variable CreateEval(string symbol, ITypeDef type, object value = null, bool isGlobal = false) { VarStackRef vsr = stack.AddVariable(symbol, isGlobal, type, value); return(stack.GetVarAtIndex(vsr)); }