static Object.Object evalHashLiteral(ast.HashLiteral node, Object.Environment env) { Dictionary <Object.HashKey, Object.HashPair> pairs = new Dictionary <Object.HashKey, Object.HashPair>(); foreach (KeyValuePair <ast.Expression, ast.Expression> _node_pair in node.Pairs) { Object.Object key = Eval(_node_pair.Key, env); if (isError(key)) { return(key); } if (!(key is Object.Hashable)) { return(newError("unusable as hash key: {0}", key.Type())); } Object.Hashable hashKey = (Object.Hashable)key; Object.Object value = Eval(_node_pair.Value, env); if (isError(value)) { return(value); } Object.HashKey hashed = hashKey.HashKey(); pairs.Add(hashed, new Object.HashPair { Key = key, Value = value }); } return(new Object.Hash { Pairs = pairs }); }
static Object.Object buildHash(int startIndex, int endIndex, out error _err) { Dictionary <Object.HashKey, Object.HashPair> hashedPairs = new Dictionary <Object.HashKey, Object.HashPair>(); for (int i = startIndex; i < endIndex; i += 2) { Object.Object key = vm.stack[i]; Object.Object value = vm.stack[i + 1]; Object.HashPair pair = new Object.HashPair { Key = key, Value = value }; if (!(key is Object.Hashable)) { _err = string.Format("unusable as hash key: {0}", key.Type()); return(null); } Object.Hashable hashKey = (Object.Hashable)key; hashedPairs.Add(hashKey.HashKey(), pair); } _err = null; return(new Object.Hash { Pairs = hashedPairs }); }
static Object.Object evalHashIndexExpression(Object.Object hash, Object.Object index) { Object.Hash hashObject = (Object.Hash)hash; if (!(index is Object.Hashable)) { return(newError("unusable as hash key: {0}", index.Type())); } Object.Hashable key = (Object.Hashable)index; Object.HashPair pair; if (!hashObject.Pairs.TryGetValue(key.HashKey(), out pair)) { return(NULL); } return(pair.Value); }
static error executeHashIndex(Object.Object hash, Object.Object index) { Object.Hash hashObject = (Object.Hash)hash; if (!(index is Object.Hashable)) { return(string.Format("unusable as hash key: {0}", index.Type())); } Object.Hashable key = (Object.Hashable)index; Object.HashPair pair; if (!hashObject.Pairs.TryGetValue(key.HashKey(), out pair)) { return(push(Null)); } return(push(pair.Value)); }