Esempio n. 1
0
        private AST_Helper.Expression parseHashLiteral()
        {
            HashLiteral hashLiteral = new HashLiteral {
                Token = this.curToken
            };

            while (!this.peekTokenIs(TokenHelper.TokenType.RBRACE))
            {
                this.nextToken();
                AST_Helper.Expression keyExpr = this.parseExpression(Parser_Helper.precedence.LOWEST);

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

                this.nextToken();
                AST_Helper.Expression valueExpr = this.parseExpression(Parser_Helper.precedence.LOWEST);

                hashLiteral.Pairs.Add(keyExpr, valueExpr);

                if (!this.peekTokenIs(TokenHelper.TokenType.RBRACE) && !this.expectPeek(TokenHelper.TokenType.COMMA))
                {
                    return(null);
                }
            }

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

            return(hashLiteral);
        }
Esempio n. 2
0
        private static Object_AST evalHashLiteral(HashLiteral hash, Environment_AST env)
        {
            Dictionary <HashKey, HashPair> pairs = new Dictionary <HashKey, HashPair>();

            foreach (var item in hash.Pairs)
            {
                Object_AST key = Eval(item.Key, env);
                if (key is Error_AST)  //for recursive Eval calls, check the returned result of the recursive Eval. If an error, return rightaway. Otherwise we will get a
                {
                    return(key);       //higher level error obscuring the true source of error
                }

                IHashable hashKey = key as IHashable;
                if (hashKey is null)
                {
                    return(new Error_AST(string.Format("unusable as hash key: {0}", key.Type)));
                }

                Object_AST val = Eval(item.Value, env);
                if (val is Error_AST)  //for recursive Eval calls, check the returned result of the recursive Eval. If an error, return rightaway. Otherwise we will get a
                {
                    return(val);       //higher level error obscuring the true source of error
                }

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

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