public Expression Parse(Parser parser, Token token, out bool trailingSemicolon) { trailingSemicolon = false; parser.Take(TokenType.LeftParen); BlockExpression initializer = null; if (!parser.Match(TokenType.Semicolon)) { var initializerExpr = parser.ParseStatement(false); if (initializerExpr is IStatementExpression && !(initializerExpr is VarExpression)) throw new MondCompilerException(token, CompilerError.BadForLoopInitializer); initializer = new BlockExpression(new List<Expression>() { initializerExpr }); } parser.Take(TokenType.Semicolon); Expression condition = null; if (!parser.Match(TokenType.Semicolon)) condition = parser.ParseExpression(); parser.Take(TokenType.Semicolon); BlockExpression increment = null; if (!parser.Match(TokenType.RightParen)) { var statements = new List<Expression>(); do { statements.Add(parser.ParseExpression()); if (!parser.Match(TokenType.Comma)) break; parser.Take(TokenType.Comma); } while (true); increment = new BlockExpression(statements); } parser.Take(TokenType.RightParen); var block = new ScopeExpression(parser.ParseBlock()); return new ForExpression(token, initializer, condition, increment, block); }
public Expression Parse(Parser parser, Token token, out bool trailingSemicolon) { trailingSemicolon = false; string name = null; var arguments = new List<string>(); BlockExpression body; // optional name if (parser.Match(TokenType.Identifier)) { name = parser.Take(TokenType.Identifier).Contents; } // parse argument list parser.Take(TokenType.LeftParen); if (!parser.Match(TokenType.RightParen)) { while (true) { var identifier = parser.Take(TokenType.Identifier); arguments.Add(identifier.Contents); if (parser.Match(TokenType.RightParen)) break; parser.Take(TokenType.Comma); } } parser.Take(TokenType.RightParen); // parse body if (parser.MatchAndTake(TokenType.Pointy)) { body = new BlockExpression(new List<Expression> { new ReturnExpression(token, parser.ParseExpession()) }); } else { body = parser.ParseBlock(false); } return new FunctionExpression(token, name, arguments, body); }
public Expression Parse(Parser parser, Token token, out bool trailingSemicolon) { trailingSemicolon = false; parser.Take(TokenType.LeftParen); Expression initializer = null; if (!parser.Match(TokenType.Semicolon)) initializer = parser.ParseStatement(false); if (initializer is IBlockStatementExpression) throw new MondCompilerException(token.FileName, token.Line, "For loop initializer can not be block statement"); parser.Take(TokenType.Semicolon); Expression condition = null; if (!parser.Match(TokenType.Semicolon)) condition = parser.ParseExpession(); parser.Take(TokenType.Semicolon); BlockExpression increment = null; if (!parser.Match(TokenType.RightParen)) { var statements = new List<Expression>(); do { statements.Add(parser.ParseStatement(false)); if (!parser.Match(TokenType.Comma)) break; parser.Take(TokenType.Comma); } while (true); increment = new BlockExpression(token, statements); } parser.Take(TokenType.RightParen); var block = new ScopeExpression(parser.ParseBlock()); return new ForExpression(token, initializer, condition, increment, block); }
public ScopeExpression(BlockExpression block) : base(block.Statements) { }