private Ast.IExpression ParseIfExpression() { var expression = new Ast.IfExpression { Token = _curToken }; NextToken(); expression.Condition = ParseExpression(Precedence.Lowest); if (!ExpectPeek(TokenType.Lbrace)) { return(null); } expression.Consequence = ParseBlockStatement(); if (PeekTokenIs(TokenType.Else)) { NextToken(); if (PeekTokenIs(TokenType.If)) { NextToken(); expression.Alternative = new Ast.BlockStatement { Statements = new List <Ast.IStatement> { new Ast.ExpressionStatement { Expression = ParseIfExpression() } } }; return(expression); } if (!ExpectPeek(TokenType.Lbrace)) { return(null); } expression.Alternative = ParseBlockStatement(); } return(expression); }
private static IObject EvalIfExpression(Ast.IfExpression ie, Environment 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); }