Exemple #1
0
        private IObject EvalHashLiteral(HashLiteral hl, Environment env)
        {
            var pairs = new Dictionary <int, HashPair>();

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

                if (!(key is IHashable))
                {
                    return new Error {
                               Message = "unusable as hashkey..."
                    }
                }
                ;

                var hashKey = key as IHashable;

                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
            });
        }
Exemple #2
0
        public IExpression Parse(Parser parser, Token token)
        {
            var hash = new HashLiteral
            {
                token = token,
                Pairs = new Dictionary <IExpression, IExpression>()
            };

            while (!parser.PeekTokenIs(TokenType.RBRACE))
            {
                parser.NextToken();
                var key = parser.ParseExpression(BindingPower.LOWEST);

                if (!parser.ExpectPeek(TokenType.COLON))
                {
                    return(null);
                }

                parser.NextToken();
                var value = parser.ParseExpression(BindingPower.LOWEST);

                hash.Pairs[key] = value;

                if (!parser.PeekTokenIs(TokenType.RBRACE) && !parser.ExpectPeek(TokenType.COMMA))
                {
                    return(null);
                }
            }

            if (!parser.ExpectPeek(TokenType.RBRACE))
            {
                return(null);
            }

            return(hash);
        }