parser add multiplication, division, modulo, addition, subtraction

This commit is contained in:
Marvin Schlegel 2024-05-08 22:57:21 +02:00
parent 4f61431c79
commit de078639fc
3 changed files with 53 additions and 12 deletions

View File

@ -90,6 +90,40 @@ testExpressionNot = TestCase $
testExpressionMinus = TestCase $ testExpressionMinus = TestCase $
assertEqual "expect expression minus" (UnaryOperation Minus (Reference "boo")) $ assertEqual "expect expression minus" (UnaryOperation Minus (Reference "boo")) $
parseExpression [MINUS,IDENTIFIER "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 [ tests = TestList [
testSingleEmptyClass, testSingleEmptyClass,
@ -117,5 +151,11 @@ tests = TestList [
testFieldNullWithInitialization, testFieldNullWithInitialization,
testReturnVoid, testReturnVoid,
testExpressionNot, testExpressionNot,
testExpressionMinus testExpressionMinus,
testExpressionLessThan,
testExpressionGreaterThan,
testExpressionLessThanEqual,
testExpressionGreaterThanOrEqual,
testExpressionEqual,
testExpressionNotEqual
] ]

View File

@ -31,6 +31,7 @@ data BinaryOperator
| Subtraction | Subtraction
| Multiplication | Multiplication
| Division | Division
| Modulo
| BitwiseAnd | BitwiseAnd
| BitwiseOr | BitwiseOr
| BitwiseXor | BitwiseXor

View File

@ -339,26 +339,26 @@ andexpression : equalityexpression { $1 }
-- | andexpression AND equalityexpression { } -- | andexpression AND equalityexpression { }
equalityexpression : relationalexpression { $1 } equalityexpression : relationalexpression { $1 }
-- | equalityexpression EQUAL relationalexpression { } | equalityexpression EQUAL relationalexpression { BinaryOperation CompareEqual $1 $3 }
-- | equalityexpression NOTEQUAL relationalexpression { } | equalityexpression NOTEQUAL relationalexpression { BinaryOperation CompareNotEqual $1 $3 }
relationalexpression : shiftexpression { $1 } relationalexpression : shiftexpression { $1 }
-- | relationalexpression LESS shiftexpression { } | relationalexpression LESS shiftexpression { BinaryOperation CompareLessThan $1 $3 }
-- | relationalexpression GREATER shiftexpression { } | relationalexpression GREATER shiftexpression { BinaryOperation CompareGreaterThan $1 $3 }
-- | relationalexpression LESSEQUAL shiftexpression { } | relationalexpression LESSEQUAL shiftexpression { BinaryOperation CompareLessOrEqual $1 $3 }
-- | relationalexpression GREATEREQUAL shiftexpression { } | relationalexpression GREATEREQUAL shiftexpression { BinaryOperation CompareGreaterOrEqual $1 $3 }
-- | relationalexpression INSTANCEOF referencetype { } -- | relationalexpression INSTANCEOF referencetype { }
shiftexpression : additiveexpression { $1 } shiftexpression : additiveexpression { $1 }
additiveexpression : multiplicativeexpression { $1 } additiveexpression : multiplicativeexpression { $1 }
-- | additiveexpression PLUS multiplicativeexpression { } | additiveexpression PLUS multiplicativeexpression { BinaryOperation Addition $1 $3 }
-- | additiveexpression MINUS multiplicativeexpression { } | additiveexpression MINUS multiplicativeexpression { BinaryOperation Subtraction $1 $3 }
multiplicativeexpression : unaryexpression { $1 } multiplicativeexpression : unaryexpression { $1 }
-- | multiplicativeexpression MUL unaryexpression { } | multiplicativeexpression MUL unaryexpression { BinaryOperation Multiplication $1 $3 }
-- | multiplicativeexpression DIV unaryexpression { } | multiplicativeexpression DIV unaryexpression { BinaryOperation Division $1 $3 }
-- | multiplicativeexpression MOD unaryexpression { } | multiplicativeexpression MOD unaryexpression { BinaryOperation Modulo $1 $3 }
{ {