From 408111df5160a2bfcc01bd66b9ddb2c708db681a Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Fri, 31 May 2024 11:04:23 +0200 Subject: [PATCH 1/4] parser implement field access --- 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 090fac1..dee3985 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -191,6 +191,12 @@ testExpressionThisMethodCall = TestCase $ testExpressionThisMethodCallParam = TestCase $ assertEqual "expect this methocall" (StatementExpressionExpression (MethodCall (Reference "this") "foo" [Reference "x"])) $ parseExpression [THIS,DOT,IDENTIFIER "foo",LBRACE,IDENTIFIER "x",RBRACE] +testExpressionFieldAccess = TestCase $ + assertEqual "expect NameResolution" (BinaryOperation NameResolution (Reference "this") (Reference "b")) $ + parseExpression [THIS,DOT,IDENTIFIER "b"] +testExpressionSimpleFieldAccess = TestCase $ + assertEqual "expect Reference" (Reference "a") $ + parseExpression [IDENTIFIER "a"] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -267,6 +273,8 @@ tests = TestList [ testExpressionMethodCallTwoParams, testExpressionThisMethodCall, testExpressionThisMethodCallParam, + testExpressionFieldAccess, + testExpressionSimpleFieldAccess, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 21d7be1..eb47864 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -297,7 +297,7 @@ classinstancecreationexpression : NEW classtype LBRACE RBRACE { } conditionalandexpression : inclusiveorexpression { $1 } -fieldaccess : primary DOT IDENTIFIER { } +fieldaccess : primary DOT IDENTIFIER { BinaryOperation NameResolution $1 (Reference $3) } unaryexpression : unaryexpressionnotplusminus { $1 } | predecrementexpression { StatementExpressionExpression $1 } @@ -319,7 +319,7 @@ primarynonewarray : literal { $1 } | THIS { Reference "this" } | LBRACE expression RBRACE { $2 } -- | classinstancecreationexpression { } - -- | fieldaccess { } + | fieldaccess { $1 } | methodinvocation { StatementExpressionExpression $1 } unaryexpressionnotplusminus : postfixexpression { $1 } From 84613fabe089c9e8b5bf53c07cdb5d578fab632e Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Fri, 31 May 2024 11:07:19 +0200 Subject: [PATCH 2/4] parser add field subaccess --- Test/TestParser.hs | 4 ++++ src/Parser/JavaParser.y | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index dee3985..521a615 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -197,6 +197,9 @@ testExpressionFieldAccess = TestCase $ testExpressionSimpleFieldAccess = TestCase $ assertEqual "expect Reference" (Reference "a") $ parseExpression [IDENTIFIER "a"] +testExpressionFieldSubAccess = TestCase $ + assertEqual "expect NameResolution without this" (BinaryOperation NameResolution (Reference "a") (Reference "b")) $ + parseExpression [IDENTIFIER "a",DOT,IDENTIFIER "b"] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -275,6 +278,7 @@ tests = TestList [ testExpressionThisMethodCallParam, testExpressionFieldAccess, testExpressionSimpleFieldAccess, + testExpressionFieldSubAccess, testStatementIfThen, testStatementIfThenElse, testStatementWhile, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index eb47864..fd694b6 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -83,11 +83,11 @@ typedeclarations : typedeclaration { [$1] } | typedeclarations typedeclaration { $1 ++ [$2] } name : simplename { Reference $1 } - -- | qualifiedname { } + | qualifiedname { $1 } typedeclaration : classdeclaration { $1 } -qualifiedname : name DOT IDENTIFIER { } +qualifiedname : name DOT IDENTIFIER { BinaryOperation NameResolution $1 (Reference $3) } simplename : IDENTIFIER { $1 } From 2acba0f28330613ecc831128c8790a4d3869ff0f Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Fri, 31 May 2024 11:23:25 +0200 Subject: [PATCH 3/4] parser add preincrement and decrement conversion --- Test/TestParser.hs | 4 ++-- src/Parser/JavaParser.y | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 521a615..7c38433 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -145,10 +145,10 @@ testExpressionPostDecrement = TestCase $ assertEqual "expect PostDecrement" (StatementExpressionExpression $ PostDecrement (Reference "a")) $ parseExpression [IDENTIFIER "a",DECREMENT] testExpressionPreIncrement = TestCase $ - assertEqual "expect PreIncrement" (StatementExpressionExpression $ PreIncrement (Reference "a")) $ + assertEqual "expect PreIncrement" (StatementExpressionExpression $ Assignment (Reference "a") (BinaryOperation Addition (IntegerLiteral 1) (Reference "a"))) $ parseExpression [INCREMENT,IDENTIFIER "a"] testExpressionPreDecrement = TestCase $ - assertEqual "expect PreIncrement" (StatementExpressionExpression $ PreDecrement (Reference "a")) $ + assertEqual "expect PreIncrement" (StatementExpressionExpression $ Assignment (Reference "a") (BinaryOperation Subtraction (IntegerLiteral 1) (Reference "a"))) $ parseExpression [DECREMENT,IDENTIFIER "a"] testExpressionAssign = TestCase $ assertEqual "expect assign 5 to a" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $ diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index fd694b6..5f19c34 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -279,9 +279,9 @@ assignmentoperator : ASSIGN { Nothing } | XOREQUAL { Just BitwiseXor } | OREQUAL{ Just BitwiseOr } -preincrementexpression : INCREMENT unaryexpression { PreIncrement $2 } +preincrementexpression : INCREMENT unaryexpression { Assignment $2 (BinaryOperation Addition (IntegerLiteral 1) $2) } -predecrementexpression : DECREMENT unaryexpression { PreDecrement $2 } +predecrementexpression : DECREMENT unaryexpression { Assignment $2 (BinaryOperation Subtraction (IntegerLiteral 1) $2) } postincrementexpression : postfixexpression INCREMENT { PostIncrement $1 } From 30365d76bd95fa74ecd3a00c8cc89d9e501d00c0 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Fri, 31 May 2024 11:44:10 +0200 Subject: [PATCH 4/4] Revert "parser add preincrement and decrement conversion" This reverts commit 2acba0f28330613ecc831128c8790a4d3869ff0f. --- Test/TestParser.hs | 4 ++-- src/Parser/JavaParser.y | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 7c38433..521a615 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -145,10 +145,10 @@ testExpressionPostDecrement = TestCase $ assertEqual "expect PostDecrement" (StatementExpressionExpression $ PostDecrement (Reference "a")) $ parseExpression [IDENTIFIER "a",DECREMENT] testExpressionPreIncrement = TestCase $ - assertEqual "expect PreIncrement" (StatementExpressionExpression $ Assignment (Reference "a") (BinaryOperation Addition (IntegerLiteral 1) (Reference "a"))) $ + assertEqual "expect PreIncrement" (StatementExpressionExpression $ PreIncrement (Reference "a")) $ parseExpression [INCREMENT,IDENTIFIER "a"] testExpressionPreDecrement = TestCase $ - assertEqual "expect PreIncrement" (StatementExpressionExpression $ Assignment (Reference "a") (BinaryOperation Subtraction (IntegerLiteral 1) (Reference "a"))) $ + assertEqual "expect PreIncrement" (StatementExpressionExpression $ PreDecrement (Reference "a")) $ parseExpression [DECREMENT,IDENTIFIER "a"] testExpressionAssign = TestCase $ assertEqual "expect assign 5 to a" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $ diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 5f19c34..fd694b6 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -279,9 +279,9 @@ assignmentoperator : ASSIGN { Nothing } | XOREQUAL { Just BitwiseXor } | OREQUAL{ Just BitwiseOr } -preincrementexpression : INCREMENT unaryexpression { Assignment $2 (BinaryOperation Addition (IntegerLiteral 1) $2) } +preincrementexpression : INCREMENT unaryexpression { PreIncrement $2 } -predecrementexpression : DECREMENT unaryexpression { Assignment $2 (BinaryOperation Subtraction (IntegerLiteral 1) $2) } +predecrementexpression : DECREMENT unaryexpression { PreDecrement $2 } postincrementexpression : postfixexpression INCREMENT { PostIncrement $1 }