コード例 #1
0
ファイル: Parser.cs プロジェクト: mtakagi/MacacaUnity
        public Expression ParseHashLiteral()
        {
            var hash = new HashLiteral()
            {
                Token = this.currentToken, Pairs = new Dictionary <Expression, Expression>()
            };

            while (!this.IsPeekTokenTypeEqualTo(TokenType.RBRACE))
            {
                this.NextToken();
                var key = this.ParseExpression(Precedence.LOWEST);

                if (!this.Expect(TokenType.COLON))
                {
                    return(null);
                }

                this.NextToken();
                var value = this.ParseExpression(Precedence.LOWEST);
                hash.Pairs[key] = value;

                if (!this.IsPeekTokenTypeEqualTo(TokenType.RBRACE) && !this.Expect(TokenType.COMMA))
                {
                    return(null);
                }
            }

            if (!this.Expect(TokenType.RBRACE))
            {
                return(null);
            }

            return(hash);
        }
コード例 #2
0
        private Object EvalHashLiteral(HashLiteral hash, Environment env)
        {
            var pairs = new Dictionary <HashKey, HashPair>();

            foreach (var kvp in hash.Pairs)
            {
                var key = this.Eval(kvp.Key, env);

                if (this.IsError(key))
                {
                    return(key);
                }

                var hashKey = key as Hashable;

                if (hashKey == null)
                {
                    return(new Error()
                    {
                        Message = $"Unusable as hash key: {key.Type}"
                    });
                }

                var value = this.Eval(kvp.Value, env);

                if (this.IsError(value))
                {
                    return(value);
                }

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

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