From 2d6c7b1a0690c4e695b42fdc5d73989935626287 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Thu, 13 Jun 2024 12:27:57 +0200 Subject: [PATCH 1/3] fix external methocall --- Test/TestParser.hs | 4 ++++ src/Parser/JavaParser.y | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 5041e3e..7c180e9 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -204,6 +204,10 @@ testExpressionConstructorCall = TestCase $ assertEqual "expect constructor call" (StatementExpressionExpression (ConstructorCall "Foo" [])) $ parseExpression [NEW,IDENTIFIER "Foo",LBRACE,RBRACE] +testExpresssionExternalMethodCall = TestCase $ + assertEqual "expect method call on sub" (StatementExpressionExpression (MethodCall (Reference "Obj") "foo" [])) $ + parseExpression [IDENTIFIER "Obj",IDENTIFIER "foo",LBRACE,RBRACE] + testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ parseStatement [IF,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index aa22bb8..c3fc42d 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -287,8 +287,8 @@ postincrementexpression : postfixexpression INCREMENT { PostIncrement $1 } postdecrementexpression : postfixexpression DECREMENT { PostDecrement $1 } -methodinvocation : simplename LBRACE RBRACE { MethodCall (Reference "this") $1 [] } - | simplename LBRACE argumentlist RBRACE { MethodCall (Reference "this") $1 $3 } +methodinvocation : name LBRACE RBRACE { let (exp, functionname) = extractFunctionName $1 in (MethodCall exp functionname []) } + | name LBRACE argumentlist RBRACE { let (exp, functionname) = extractFunctionName $1 in (MethodCall exp functionname $3) } | primary DOT IDENTIFIER LBRACE RBRACE { MethodCall $1 $3 [] } | primary DOT IDENTIFIER LBRACE argumentlist RBRACE { MethodCall $1 $3 $5 } @@ -374,8 +374,9 @@ data Declarator = Declarator Identifier (Maybe Expression) convertDeclarator :: DataType -> Declarator -> VariableDeclaration convertDeclarator dataType (Declarator id assigment) = VariableDeclaration dataType id assigment -data StatementWithoutSub = Statement - +extractFunctionName :: Expression -> (Expression, Identifier) +extractFunctionName (BinaryOperation NameResolution exp (Reference functionname)) = (exp, functionname) +extractFunctionName (Reference functionname) = ((Reference "this"), functionname) parseError :: ([Token], [String]) -> a parseError (errortoken, expected) = error ("parse error on token: " ++ show errortoken ++ "\nexpected one of: " ++ show expected) -- 2.34.1 From e4693729dcae31264a1ada75806b577817f085fe Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Fri, 14 Jun 2024 15:08:50 +0200 Subject: [PATCH 2/3] fix assignment with this --- Test/TestParser.hs | 7 ++++++- src/Parser/JavaParser.y | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 7c180e9..3d05001 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -206,7 +206,10 @@ testExpressionConstructorCall = TestCase $ testExpresssionExternalMethodCall = TestCase $ assertEqual "expect method call on sub" (StatementExpressionExpression (MethodCall (Reference "Obj") "foo" [])) $ - parseExpression [IDENTIFIER "Obj",IDENTIFIER "foo",LBRACE,RBRACE] + parseExpression [IDENTIFIER "Obj",DOT,IDENTIFIER "foo",LBRACE,RBRACE] +testExpressionAssignWithThis = TestCase $ + assertEqual "expect assignment on Field" (StatementExpressionExpression (Assignment (BinaryOperation NameResolution (Reference "this") (Reference "x")) (Reference "y"))) $ + parseExpression [THIS,DOT,IDENTIFIER "x",ASSIGN,IDENTIFIER "y"] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -296,6 +299,8 @@ tests = TestList [ testExpressionSimpleFieldAccess, testExpressionFieldSubAccess, testExpressionConstructorCall, + testExpresssionExternalMethodCall, + testExpressionAssignWithThis, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index c3fc42d..d7dc862 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -265,6 +265,7 @@ conditionalorexpression : conditionalandexpression { $1 } -- | conditionalorexpression LOGICALOR conditionalandexpression{ } lefthandside : name { $1 } + | primary DOT IDENTIFIER { BinaryOperation NameResolution $1 (Reference $3) } assignmentoperator : ASSIGN { Nothing } | TIMESEQUAL { Just Multiplication } -- 2.34.1 From d2554c9b227cc2e2ab2dff50800f09d79f36082d Mon Sep 17 00:00:00 2001 From: mrab Date: Mon, 17 Jun 2024 19:25:24 +0200 Subject: [PATCH 3/3] updated recursive test using this. syntax --- Test/JavaSources/TestRecursion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/JavaSources/TestRecursion.java b/Test/JavaSources/TestRecursion.java index baa6a31..bcebce1 100644 --- a/Test/JavaSources/TestRecursion.java +++ b/Test/JavaSources/TestRecursion.java @@ -5,7 +5,7 @@ public class TestRecursion { public TestRecursion(int n) { - value = n; + this.value = n; if(n > 0) { -- 2.34.1