private static IObject EvalHashLiteral(Ast.HashLiteral node, Environment env)
        {
            var pairs = new Dictionary <HashKey, HashPair>();

            foreach (var pair in node.Pairs)
            {
                var key = Eval(pair.Key, env);
                if (IsError(key))
                {
                    return(key);
                }

                if (!(key is IHashable hashKey))
                {
                    return new Error {
                               Message = $"unusable as hash key: {key.getType()}"
                    }
                }
                ;

                var value = Eval(pair.Value, env);
                if (IsError(value))
                {
                    return(value);
                }

                var hashed = hashKey.HashKey();
                pairs[hashed] = new HashPair {
                    Key = key, Value = value
                };
            }

            return(new Hash {
                Pairs = pairs
            });
        }
    }
示例#2
0
        private static IMonkeyObject EvalHashLiteral(HashLiteral node, MonkeyEnvironment env)
        {
            var pairs = new Dictionary <HashKey, HashPair>();

            foreach (var kv in node.Pairs)
            {
                var key = Eval(kv.Key, env);
                if (IsError(key))
                {
                    return(key);
                }

                if (key is IHashable k)
                {
                    var value = Eval(kv.Value, env);
                    if (IsError(value))
                    {
                        return(value);
                    }

                    var hashKey  = k.HashKey();
                    var hashPair = new HashPair {
                        Key = key, Value = value
                    };
                    pairs.Add(hashKey, hashPair);
                }
                else
                {
                    return(NewError($"Unusable as hash key: {key.GetType()}"));
                }
            }

            return(new MonkeyHash {
                Pairs = pairs
            });
        }