/// <summary> /// Look up the given identifier as quickly as possible, without /// walking the __isa chain or doing anything fancy. (This is used /// when looking up local variables.) /// </summary> /// <param name="identifier">identifier to look up</param> /// <returns>true if found, false if not</returns> public bool TryGetValue(string identifier, out Value value) { var idVal = TempValString.Get(identifier); bool result = map.TryGetValue(idVal, out value); TempValString.Release(idVal); return(result); }
/// <summary> /// Convenience method to check whether the map contains a given string key. /// </summary> /// <param name="identifier">string key to check for</param> /// <returns>true if the map contains that key; false otherwise</returns> public bool ContainsKey(string identifier) { var idVal = TempValString.Get(identifier); bool result = map.ContainsKey(idVal); TempValString.Release(idVal); return(result); }
/// <summary> /// Accessor to get/set on element of this map by a string key, walking /// the __isa chain as needed. (Note that if you want to avoid that, then /// simply look up your value in .map directly.) /// </summary> /// <param name="identifier">string key to get/set</param> /// <returns>value associated with that key</returns> public Value this [string identifier] { get { var idVal = TempValString.Get(identifier); Value result = Lookup(idVal); TempValString.Release(idVal); return(result); } set { map[new ValString(identifier)] = value; } }
/// <summary> /// Look up the given identifier in the given sequence, walking the type chain /// until we either find it, or fail. /// </summary> /// <param name="sequence">Sequence (object) to look in.</param> /// <param name="identifier">Identifier to look for.</param> /// <param name="context">Context.</param> public static Value Resolve(Value sequence, string identifier, TAC.Context context, out ValMap valueFoundIn) { var includeMapType = true; valueFoundIn = null; int loopsLeft = 1000; // (max __isa chain depth) while (sequence != null) { if (sequence is ValTemp || sequence is ValVar) { sequence = sequence.Val(context); } if (sequence is ValMap) { // If the map contains this identifier, return its value. Value result = null; var idVal = TempValString.Get(identifier); bool found = ((ValMap)sequence).map.TryGetValue(idVal, out result); TempValString.Release(idVal); if (found) { valueFoundIn = (ValMap)sequence; return(result); } // Otherwise, if we have an __isa, try that next. if (loopsLeft < 0) { return(null); // (unless we've hit the loop limit) } if (!((ValMap)sequence).map.TryGetValue(ValString.magicIsA, out sequence)) { // ...and if we don't have an __isa, try the generic map type if allowed if (!includeMapType) { throw new KeyException(identifier); } sequence = context.vm.mapType ?? Intrinsics.MapType(); includeMapType = false; } } else if (sequence is ValList) { sequence = context.vm.listType ?? Intrinsics.ListType(); includeMapType = false; } else if (sequence is ValString) { sequence = context.vm.stringType ?? Intrinsics.StringType(); includeMapType = false; } else if (sequence is ValNumber) { sequence = context.vm.numberType ?? Intrinsics.NumberType(); includeMapType = false; } else if (sequence is ValFunction) { sequence = context.vm.functionType ?? Intrinsics.FunctionType(); includeMapType = false; } else { throw new TypeException("Type Error (while attempting to look up " + identifier + ")"); } loopsLeft--; } return(null); }