From b11adcf907087830d544775ae3c3b8d9ac1502ba Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 15 May 2024 10:32:03 +0200 Subject: [PATCH 1/7] parser add this and braced expressions --- Test/TestParser.hs | 13 +++++++++++++ src/Parser/JavaParser.y | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 873f438..bf874fc 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -159,6 +159,16 @@ testExpressionPlusEqual = TestCase $ testExpressionMinusEqual = TestCase $ assertEqual "expect assign and subtraction" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Subtraction (Reference "a") (IntegerLiteral 5)))) $ parseExpression [IDENTIFIER "a",MINUSEQUAL,INTEGERLITERAL 5] +testExpressionThis = TestCase $ + assertEqual "expect this" (Reference "this") $ + parseExpression [THIS] +testExpressionBraced = TestCase $ + assertEqual "expect braced expresssion" (BinaryOperation Multiplication (Reference "b") (BinaryOperation Addition (Reference "a") (IntegerLiteral 3))) $ + parseExpression [IDENTIFIER "b",TIMES,LBRACE,IDENTIFIER "a",PLUS,INTEGERLITERAL 3,RBRACE] + +testExpressionPrecedence = TestCase $ + assertEqual "expect times to be inner expression" (BinaryOperation Addition (BinaryOperation Multiplication (Reference "b") (Reference "a")) (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "b",TIMES,IDENTIFIER "a",PLUS,INTEGERLITERAL 3] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -218,6 +228,9 @@ tests = TestList [ testExpressionDivideEqual, testExpressionPlusEqual, testExpressionMinusEqual, + testExpressionBraced, + testExpressionThis, + testExpressionPrecedence, testStatementIfThen, testStatementIfThenElse, testStatementWhile diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 4e66850..513b198 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -316,8 +316,8 @@ inclusiveorexpression : exclusiveorexpression { $1 } | inclusiveorexpression OR exclusiveorexpression { BinaryOperation Or $1 $3 } primarynonewarray : literal { $1 } - -- | THIS { } - -- | LBRACE expression RBRACE { } + | THIS { Reference "this" } + | LBRACE expression RBRACE { $2 } -- | classinstancecreationexpression { } -- | fieldaccess { } -- | methodinvocation { } From f81a812f59f95d1c2fb7e20ab462015d0646ef53 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:15:41 +0200 Subject: [PATCH 2/7] parser add assigment statement --- src/Parser/JavaParser.y | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 513b198..7002706 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -215,7 +215,7 @@ localvariabledeclaration : type variabledeclarators { map LocalVariableDeclarati statementwithouttrailingsubstatement : block { [$1] } | emptystatement { [] } - -- | expressionstatement { } + | expressionstatement { [$1] } | returnstatement { [$1] } ifthenstatement : IF LBRACE expression RBRACE statement { If $3 (Block $5) Nothing } @@ -229,7 +229,7 @@ assignmentexpression : conditionalexpression { $1 } emptystatement : SEMICOLON { Block [] } -expressionstatement : statementexpression SEMICOLON { } +expressionstatement : statementexpression SEMICOLON { StatementExpressionStatement $1 } returnstatement : RETURN SEMICOLON { Return Nothing } | RETURN expression SEMICOLON { Return $ Just $2 } @@ -248,13 +248,13 @@ assignment : lefthandside assignmentoperator assignmentexpression { } -statementexpression : assignment { } - | preincrementexpression { } - | predecrementexpression { } - | postincrementexpression { } - | postdecrementexpression { } - | methodinvocation { } - | classinstancecreationexpression { } +statementexpression : assignment { $1 } + -- | preincrementexpression { } + -- | predecrementexpression { } + -- | postincrementexpression { } + -- | postdecrementexpression { } + -- | methodinvocation { } + -- | classinstancecreationexpression { } ifthenelsestatementnoshortif :IF LBRACE expression RBRACE statementnoshortif ELSE statementnoshortif { } From c347e6c630e65d5848303f35ca80860f9a49dca4 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:31:58 +0200 Subject: [PATCH 3/7] add test --- Test/TestParser.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index bf874fc..5869961 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -179,6 +179,9 @@ testStatementIfThenElse = TestCase $ testStatementWhile = TestCase $ assertEqual "expect while" [While (Reference "a") (Block [Block []])] $ parseStatement [WHILE,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] +testStatementAssign = TestCase $ + assertEqual "expect assign 5" [StatementExpressionStatement (Assignment (Reference "a") (IntegerLiteral 5))] $ + parseStatement [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5,SEMICOLON] @@ -233,5 +236,6 @@ tests = TestList [ testExpressionPrecedence, testStatementIfThen, testStatementIfThenElse, - testStatementWhile + testStatementWhile, + testStatementAssign ] \ No newline at end of file From 3c70f9f1f64bf77028ff7c308ab52ea18261be89 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:49:52 +0200 Subject: [PATCH 4/7] parser add methodcall no params --- Test/TestParser.hs | 12 +++++++++++- src/Parser/JavaParser.y | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 5869961..e97d793 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -170,6 +170,10 @@ testExpressionPrecedence = TestCase $ assertEqual "expect times to be inner expression" (BinaryOperation Addition (BinaryOperation Multiplication (Reference "b") (Reference "a")) (IntegerLiteral 3)) $ parseExpression [IDENTIFIER "b",TIMES,IDENTIFIER "a",PLUS,INTEGERLITERAL 3] +testExpressionMethodCallNoParams = TestCase $ + assertEqual "expect methodcall no params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ + parseExpression [IDENTIFIER "foo",LBRACE,RBRACE] + testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ parseStatement [IF,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] @@ -183,6 +187,10 @@ testStatementAssign = TestCase $ assertEqual "expect assign 5" [StatementExpressionStatement (Assignment (Reference "a") (IntegerLiteral 5))] $ parseStatement [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5,SEMICOLON] +testStatementMethodCallNoParams = TestCase $ + assertEqual "expect methodcall statement no params" [StatementExpressionStatement (MethodCall (Reference "this") "foo" [])] $ + parseStatement [IDENTIFIER "foo",LBRACE,RBRACE,SEMICOLON] + tests = TestList [ @@ -234,8 +242,10 @@ tests = TestList [ testExpressionBraced, testExpressionThis, testExpressionPrecedence, + testExpressionMethodCallNoParams, testStatementIfThen, testStatementIfThenElse, testStatementWhile, - testStatementAssign + testStatementAssign, + testStatementMethodCallNoParams ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 7002706..07c11b5 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -253,7 +253,7 @@ statementexpression : assignment { $1 } -- | predecrementexpression { } -- | postincrementexpression { } -- | postdecrementexpression { } - -- | methodinvocation { } + | methodinvocation { $1 } -- | classinstancecreationexpression { } ifthenelsestatementnoshortif :IF LBRACE expression RBRACE statementnoshortif @@ -287,10 +287,10 @@ postincrementexpression : postfixexpression INCREMENT { UnaryOperation PostIncre postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecrement $1 } -methodinvocation : name LBRACE RBRACE { } - | name LBRACE argumentlist RBRACE { } - | primary DOT IDENTIFIER LBRACE RBRACE { } - | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } +methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } + -- | name LBRACE argumentlist RBRACE { } + -- | primary DOT IDENTIFIER LBRACE RBRACE { } + -- | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } classinstancecreationexpression : NEW classtype LBRACE RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { } @@ -320,7 +320,7 @@ primarynonewarray : literal { $1 } | LBRACE expression RBRACE { $2 } -- | classinstancecreationexpression { } -- | fieldaccess { } - -- | methodinvocation { } + | methodinvocation { StatementExpressionExpression $1 } unaryexpressionnotplusminus : postfixexpression { $1 } -- | TILDE unaryexpression { } From 24c2920c9c0c38410f53c6c67a8b83871aadfa71 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 16 May 2024 11:58:27 +0200 Subject: [PATCH 5/7] parser implement methodcall with params --- Test/TestParser.hs | 8 ++++++++ src/Parser/JavaParser.y | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index e97d793..c22968c 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -173,6 +173,12 @@ testExpressionPrecedence = TestCase $ testExpressionMethodCallNoParams = TestCase $ assertEqual "expect methodcall no params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ parseExpression [IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionMethodCallOneParam = TestCase $ + assertEqual "expect methodcall one param" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a"])) $ + parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",RBRACE] +testExpressionMethodCallTwoParams = TestCase $ + assertEqual "expect methocall two params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a", IntegerLiteral 5])) $ + parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",COMMA,INTEGERLITERAL 5,RBRACE] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -243,6 +249,8 @@ tests = TestList [ testExpressionThis, testExpressionPrecedence, testExpressionMethodCallNoParams, + testExpressionMethodCallOneParam, + testExpressionMethodCallTwoParams, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 07c11b5..068a821 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -189,8 +189,8 @@ blockstatement : localvariabledeclarationstatement { $1 } -- expected ty formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } -argumentlist : expression { } - | argumentlist COMMA expression { } +argumentlist : expression { [$1] } + | argumentlist COMMA expression { $1 ++ [$3] } numerictype : integraltype { $1 } @@ -288,7 +288,7 @@ postincrementexpression : postfixexpression INCREMENT { UnaryOperation PostIncre postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecrement $1 } methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } - -- | name LBRACE argumentlist RBRACE { } + | simplename LBRACE argumentlist RBRACE { MethodCall (Reference "this") $1 $3 } -- | primary DOT IDENTIFIER LBRACE RBRACE { } -- | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } From f4d31a85ccd49aafeb2835900ce5ba4f9977dec3 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 28 May 2024 23:27:23 +0200 Subject: [PATCH 6/7] parser add dot method call --- Test/TestParser.hs | 8 ++++++++ src/Parser/JavaParser.y | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index c22968c..63f4dc1 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -179,6 +179,12 @@ testExpressionMethodCallOneParam = TestCase $ testExpressionMethodCallTwoParams = TestCase $ assertEqual "expect methocall two params" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "a", IntegerLiteral 5])) $ parseExpression [IDENTIFIER "foo",LBRACE,IDENTIFIER "a",COMMA,INTEGERLITERAL 5,RBRACE] +testExpressionThisMethodCall = TestCase $ + assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [])) $ + parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionThisMethodCallParam = TestCase $ + assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "x"])) $ + parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,IDENTIFIER "x",RBRACE] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -251,6 +257,8 @@ tests = TestList [ testExpressionMethodCallNoParams, testExpressionMethodCallOneParam, testExpressionMethodCallTwoParams, + testExpressionThisMethodCall, + testExpressionThisMethodCallParam, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 068a821..d51b376 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -289,8 +289,8 @@ postdecrementexpression : postfixexpression DECREMENT { UnaryOperation PostDecre methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } | simplename LBRACE argumentlist RBRACE { MethodCall (Reference "this") $1 $3 } - -- | primary DOT IDENTIFIER LBRACE RBRACE { } - -- | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { } + | primary DOT IDENTIFIER LBRACE RBRACE { MethodCall $1 $3 [] } + | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { MethodCall $1 $3 $5 } classinstancecreationexpression : NEW classtype LBRACE RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { } From 1e59ba9e277f291285983f21347a605deefa3811 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 28 May 2024 23:47:34 +0200 Subject: [PATCH 7/7] parser implement constructor with statements --- Test/TestParser.hs | 8 ++++++++ src/Parser/JavaParser.y | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 63f4dc1..8e8db60 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -52,6 +52,12 @@ testClassWithMethodAndField = TestCase $ testClassWithConstructor = TestCase $ assertEqual "expect class with constructor" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithParams = TestCase $ + assertEqual "expect constructor with params" [Class "WithParams" [MethodDeclaration "void" "" [ParameterDeclaration "int" "p1"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParams",LBRACKET,IDENTIFIER "WithParams",LBRACE,INT,IDENTIFIER "p1",RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithStatements = TestCase $ + assertEqual "expect constructor with statement" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [Return Nothing])] []] $ + parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RETURN,SEMICOLON,RBRACKET,RBRACKET] testEmptyBlock = TestCase $ assertEqual "expect empty block" [Block []] $ parseStatement [LBRACKET,RBRACKET] @@ -220,6 +226,8 @@ tests = TestList [ testEmptyMethodWithParams, testClassWithMethodAndField, testClassWithConstructor, + testConstructorWithParams, + testConstructorWithStatements, testEmptyBlock, testBlockWithLocalVarDecl, testBlockWithMultipleLocalVarDecls, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index d51b376..45206a5 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -127,8 +127,8 @@ classorinterfacetype : simplename { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } -constructordeclaration : constructordeclarator constructorbody { case $1 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $2 } - | modifiers constructordeclarator constructorbody { case $2 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $3 } +constructordeclaration : constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $1 $2 } + | modifiers constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $2 $3 } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } @@ -138,12 +138,12 @@ methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, par block : LBRACKET RBRACKET { Block [] } | LBRACKET blockstatements RBRACKET { Block $2 } -constructordeclarator : simplename LBRACE RBRACE { ($1, []) } - | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } +constructordeclarator : simplename LBRACE RBRACE { [] } + | simplename LBRACE formalparameterlist RBRACE { $3 } constructorbody : LBRACKET RBRACKET { Block [] } -- | LBRACKET explicitconstructorinvocation RBRACKET { } - -- | LBRACKET blockstatements RBRACKET { } + | LBRACKET blockstatements RBRACKET { Block $2 } -- | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } methodheader : type methoddeclarator { ($1, $2) }