parser implement methods with params

This commit is contained in:
Marvin Schlegel 2024-05-07 15:03:04 +02:00
parent 3130f7f7d4
commit f183b8b183
2 changed files with 17 additions and 5 deletions

View File

@ -40,6 +40,15 @@ testEmptyPrivateMethod = TestCase $
testEmptyVoidMethod = TestCase $ testEmptyVoidMethod = TestCase $
assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "void" "foo" [] (Block [])] []] $ assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "void" "foo" [] (Block [])] []] $
parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET]
testEmptyMethodWithParam = TestCase $
assertEqual "expect class with method with param" [Class "WithParam" [MethodDeclaration "void" "foo" [ParameterDeclaration "int" "param"] (Block [])] []] $
parse [CLASS,IDENTIFIER "WithParam",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,INT,IDENTIFIER "param",RBRACE,SEMICOLON,RBRACKET]
testEmptyMethodWithParams = TestCase $
assertEqual "expect class with multiple params" [Class "WithParams" [MethodDeclaration "void" "foo" [ParameterDeclaration "int" "p1",ParameterDeclaration "Custom" "p2"] (Block [])] []] $
parse [CLASS,IDENTIFIER "WithParams",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,INT,IDENTIFIER "p1",COMMA,IDENTIFIER "Custom",IDENTIFIER "p2",RBRACE,SEMICOLON,RBRACKET]
testClassWithMethodAndField = TestCase $
assertEqual "expect class with method and field" [Class "WithMethodAndField" [MethodDeclaration "void" "foo" [] (Block []), MethodDeclaration "int" "bar" [] (Block [])] [VariableDeclaration "int" "value" Nothing]] $
parse [CLASS,IDENTIFIER "WithMethodAndField",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,INT,IDENTIFIER "value",SEMICOLON,INT,IDENTIFIER "bar",LBRACE,RBRACE,SEMICOLON,RBRACKET]
tests = TestList [ tests = TestList [
@ -52,5 +61,8 @@ tests = TestList [
testWithModifier, testWithModifier,
testEmptyMethod, testEmptyMethod,
testEmptyPrivateMethod, testEmptyPrivateMethod,
testEmptyVoidMethod testEmptyVoidMethod,
testEmptyMethodWithParam,
testEmptyMethodWithParams,
testClassWithMethodAndField
] ]

View File

@ -161,8 +161,8 @@ methodbody : block { $1 }
blockstatements : blockstatement { } blockstatements : blockstatement { }
| blockstatements blockstatement { } | blockstatements blockstatement { }
formalparameterlist : formalparameter { } formalparameterlist : formalparameter { [$1] }
| formalparameterlist COMMA formalparameter{ } | formalparameterlist COMMA formalparameter { $1 ++ [$3] }
explicitconstructorinvocation : THIS LBRACE RBRACE SEMICOLON { } explicitconstructorinvocation : THIS LBRACE RBRACE SEMICOLON { }
| THIS LBRACE argumentlist RBRACE SEMICOLON { } | THIS LBRACE argumentlist RBRACE SEMICOLON { }
@ -171,7 +171,7 @@ classtypelist : classtype { }
| classtypelist COMMA classtype { } | classtypelist COMMA classtype { }
methoddeclarator : IDENTIFIER LBRACE RBRACE { ($1, []) } methoddeclarator : IDENTIFIER LBRACE RBRACE { ($1, []) }
-- | IDENTIFIER LBRACE formalparameterlist RBRACE { } | IDENTIFIER LBRACE formalparameterlist RBRACE { ($1, $3) }
primitivetype : BOOLEAN { "boolean" } primitivetype : BOOLEAN { "boolean" }
| numerictype { $1 } | numerictype { $1 }
@ -185,7 +185,7 @@ variabledeclarator : variabledeclaratorid { Declarator $1 Nothing }
blockstatement : localvariabledeclarationstatement { } blockstatement : localvariabledeclarationstatement { }
| statement { } | statement { }
formalparameter : type variabledeclaratorid { } formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 }
argumentlist : expression { } argumentlist : expression { }
| argumentlist COMMA expression { } | argumentlist COMMA expression { }