From de078639fc971eaf9965033ac35b6cfc76caa621 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 22:57:21 +0200 Subject: [PATCH] parser add multiplication, division, modulo, addition, subtraction --- Test/TestParser.hs | 42 ++++++++++++++++++++++++++++++++++++++++- src/Ast.hs | 1 + src/Parser/JavaParser.y | 22 ++++++++++----------- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index acb7ee7..a3ba800 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -90,6 +90,40 @@ testExpressionNot = TestCase $ testExpressionMinus = TestCase $ assertEqual "expect expression minus" (UnaryOperation Minus (Reference "boo")) $ parseExpression [MINUS,IDENTIFIER "boo"] +testExpressionMultiplication = TestCase $ + assertEqual "expect multiplication" (BinaryOperation Multiplication (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",TIMES,INTEGERLITERAL 3] +testExpressionDivision = TestCase $ + assertEqual "expect division" (BinaryOperation Division (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",DIV,INTEGERLITERAL 3] +testExpressionModulo = TestCase $ + assertEqual "expect modulo operation" (BinaryOperation Modulo (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",MODULO,INTEGERLITERAL 3] +testExpressionAddition = TestCase $ + assertEqual "expect addition" (BinaryOperation Addition (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",PLUS,INTEGERLITERAL 3] +testExpressionSubtraction = TestCase $ + assertEqual "expect subtraction" (BinaryOperation Subtraction (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",MINUS,INTEGERLITERAL 3] +testExpressionLessThan = TestCase $ + assertEqual "expect comparision less than" (BinaryOperation CompareLessThan (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",LESS,INTEGERLITERAL 3] +testExpressionGreaterThan = TestCase $ + assertEqual "expect comparision greater than" (BinaryOperation CompareGreaterThan (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",GREATER,INTEGERLITERAL 3] +testExpressionLessThanEqual = TestCase $ + assertEqual "expect comparision less than or equal" (BinaryOperation CompareLessOrEqual (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",LESSEQUAL,INTEGERLITERAL 3] +testExpressionGreaterThanOrEqual = TestCase $ + assertEqual "expect comparision greater than or equal" (BinaryOperation CompareGreaterOrEqual (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",GREATEREQUAL,INTEGERLITERAL 3] +testExpressionEqual = TestCase $ + assertEqual "expect comparison equal" (BinaryOperation CompareEqual (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",EQUAL,INTEGERLITERAL 3] +testExpressionNotEqual = TestCase $ + assertEqual "expect comparison equal" (BinaryOperation CompareNotEqual (Reference "bar") (IntegerLiteral 3)) $ + parseExpression [IDENTIFIER "bar",NOTEQUAL,INTEGERLITERAL 3] + tests = TestList [ testSingleEmptyClass, @@ -117,5 +151,11 @@ tests = TestList [ testFieldNullWithInitialization, testReturnVoid, testExpressionNot, - testExpressionMinus + testExpressionMinus, + testExpressionLessThan, + testExpressionGreaterThan, + testExpressionLessThanEqual, + testExpressionGreaterThanOrEqual, + testExpressionEqual, + testExpressionNotEqual ] \ No newline at end of file diff --git a/src/Ast.hs b/src/Ast.hs index e04cd3d..48dc6c6 100644 --- a/src/Ast.hs +++ b/src/Ast.hs @@ -31,6 +31,7 @@ data BinaryOperator | Subtraction | Multiplication | Division + | Modulo | BitwiseAnd | BitwiseOr | BitwiseXor diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 61bd1f1..9ba26bc 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -339,26 +339,26 @@ andexpression : equalityexpression { $1 } -- | andexpression AND equalityexpression { } equalityexpression : relationalexpression { $1 } - -- | equalityexpression EQUAL relationalexpression { } - -- | equalityexpression NOTEQUAL relationalexpression { } + | equalityexpression EQUAL relationalexpression { BinaryOperation CompareEqual $1 $3 } + | equalityexpression NOTEQUAL relationalexpression { BinaryOperation CompareNotEqual $1 $3 } relationalexpression : shiftexpression { $1 } - -- | relationalexpression LESS shiftexpression { } - -- | relationalexpression GREATER shiftexpression { } - -- | relationalexpression LESSEQUAL shiftexpression { } - -- | relationalexpression GREATEREQUAL shiftexpression { } + | relationalexpression LESS shiftexpression { BinaryOperation CompareLessThan $1 $3 } + | relationalexpression GREATER shiftexpression { BinaryOperation CompareGreaterThan $1 $3 } + | relationalexpression LESSEQUAL shiftexpression { BinaryOperation CompareLessOrEqual $1 $3 } + | relationalexpression GREATEREQUAL shiftexpression { BinaryOperation CompareGreaterOrEqual $1 $3 } -- | relationalexpression INSTANCEOF referencetype { } shiftexpression : additiveexpression { $1 } additiveexpression : multiplicativeexpression { $1 } - -- | additiveexpression PLUS multiplicativeexpression { } - -- | additiveexpression MINUS multiplicativeexpression { } + | additiveexpression PLUS multiplicativeexpression { BinaryOperation Addition $1 $3 } + | additiveexpression MINUS multiplicativeexpression { BinaryOperation Subtraction $1 $3 } multiplicativeexpression : unaryexpression { $1 } - -- | multiplicativeexpression MUL unaryexpression { } - -- | multiplicativeexpression DIV unaryexpression { } - -- | multiplicativeexpression MOD unaryexpression { } + | multiplicativeexpression MUL unaryexpression { BinaryOperation Multiplication $1 $3 } + | multiplicativeexpression DIV unaryexpression { BinaryOperation Division $1 $3 } + | multiplicativeexpression MOD unaryexpression { BinaryOperation Modulo $1 $3 } {