From b11adcf907087830d544775ae3c3b8d9ac1502ba Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 15 May 2024 10:32:03 +0200 Subject: [PATCH 1/2] 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/2] 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 { }