public IExpression Parse(Parser parser, Token token) { var expr = new IfExpression() { token = parser.CurrentToken() }; if (!parser.ExpectPeek(TokenType.LPAREN)) { return(null); } parser.NextToken(); expr.Condition = parser.ParseExpression(BindingPower.LOWEST); if (!parser.ExpectPeek(TokenType.RPAREN)) { return(null); } if (!parser.ExpectPeek(TokenType.LBRACE)) { return(null); } expr.Consequence = new BlockStatementParslet().Parse(parser); if (parser.PeekTokenIs(TokenType.ELSE)) { parser.NextToken(); if (!parser.PeekTokenIs(TokenType.LBRACE) && !parser.PeekTokenIs(TokenType.IF)) { return(null); } if (parser.PeekTokenIs(TokenType.LBRACE)) { parser.NextToken(); expr.Alternative = new BlockStatementParslet().Parse(parser); } else { parser.NextToken(); expr.Alternative = parser.ParseExpression(BindingPower.LOWEST); } } return(expr); }
private IObject EvalIfExpression(IfExpression ie, Environment env) { var condition = Eval(ie.Condition, env); if (condition is Error) { return(condition); } if (IsTruthy(condition)) { return(Eval(ie.Consequence, env)); } else if (ie.Alternative != null) { return(Eval(ie.Alternative, env)); } else { return(null); } }