private FunctionDeclarationStmt CreateFunction(string name, BlockStmt block = null) { var declaration = new FunctionDeclarationStmt(0, 0); declaration.Identifier = name; declaration.ReturnType = new SimpleType(0, 0, ExprType.Int); if (block == null) { declaration.ProcedureBlock = new BlockStmt(0, 0); } else { declaration.ProcedureBlock = block; } declaration.AddParameter("arg1", new SimpleType(0, 0, ExprType.Int), false); return(declaration); }
public void TestFunctionDeclaration() { var programSource = new TokenList() { { TokenType.KwFunction }, { TokenType.Identifier, "func" }, { TokenType.LParen }, { TokenType.Identifier, "par1" }, { TokenType.Colon }, { TokenType.Identifier, "int" }, { TokenType.RParen }, { TokenType.Colon }, { TokenType.Identifier, "bool" }, { TokenType.LineTerm }, { TokenType.KwBegin }, { TokenType.KwReturn }, { TokenType.IntLiteral, "123" }, { TokenType.KwEnd } }; Parser parser = new Parser(CreateMockScanner(programSource), new ErrorHandler()); ProgramNode program = parser.Parse(); var declr = new FunctionDeclarationStmt(0, 0); declr.Identifier = "func"; declr.ReturnType = new SimpleType(0, 0, ExprType.Bool); declr.AddParameter("par1", new SimpleType(0, 0, ExprType.Int), false); declr.ProcedureBlock = new BlockStmt(0, 0); var returnStmt = new ReturnStmt(0, 0); returnStmt.ReturnExpression = new IntLiteralExpr(0, 0, 123); declr.ProcedureBlock.Statements.Add(returnStmt); expected.Block.Statements.Add(declr); program.ShouldBeEquivalentTo(expected); }