Exemplo n.º 1
0
        public Expression ParseIfExpression()
        {
            var expression = new IfExpression()
            {
                Token = this.currentToken
            };

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

            this.NextToken();
            expression.Condition = this.ParseExpression(Precedence.LOWEST);

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

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

            expression.Cons = this.ParseBlockStatement();

            if (this.IsPeekTokenTypeEqualTo(TokenType.ELSE))
            {
                this.NextToken();

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

                expression.Els = this.ParseBlockStatement();
            }

            return(expression);
        }
Exemplo n.º 2
0
        private Object EvalIfExpression(IfExpression expression, Environment env)
        {
            var condition = this.Eval(expression.Condition, env);

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

            if (this.IsTruthy(condition))
            {
                return(this.Eval(expression.Cons, env));
            }
            else if (expression.Els != null)
            {
                return(this.Eval(expression.Els, env));
            }
            else
            {
                return(Null);
            }
        }