コード例 #1
0
        private static IMonkeyObject EvalIfExpression(IfExpression ie, MonkeyEnvironment env)
        {
            var condition = Eval(ie.Condition, env);

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

            if (IsTruthy(condition))
            {
                return(Eval(ie.Consequence, env));
            }
            else if (ie.Alternative != null)
            {
                return(Eval(ie.Alternative, env));
            }

            return(Null);
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: sgoguen/MonkeyLang
        private IExpression ParseIfExpression()
        {
            var expression = new IfExpression {
                Token = _curToken
            };

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

            NextToken();
            expression.Condition = ParseExpression(PrecedenceLevel.Lowest);

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

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

            expression.Consequence = ParseBlockStatement();

            if (PeekTokenIs(TokenType.Else))
            {
                NextToken();
                if (!ExpectPeek(TokenType.LBrace))
                {
                    return(null);
                }

                expression.Alternative = ParseBlockStatement();
            }

            return(expression);
        }