From c49b7f556c055432dc18236311c3b0cc65a09e7a Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 13:29:10 +0200 Subject: [PATCH] parser implement varibale initialization --- Test/TestParser.hs | 10 +++++++++- src/Parser/JavaParser.y | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 7a79ba8..4a18581 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -74,6 +74,12 @@ testExpressionIntLiteral = TestCase $ testFieldWithInitialization = TestCase $ assertEqual "expect Class with initialized field" [Class "WithInitField" [] [VariableDeclaration "int" "number" $ Just $ IntegerLiteral 3]] $ parse [CLASS,IDENTIFIER "WithInitField",LBRACKET,INT,IDENTIFIER "number",ASSIGN,INTEGERLITERAL 3,SEMICOLON,RBRACKET] +testLocalBoolWithInitialization = TestCase $ + assertEqual "expect block with with initialized local var" (Block [LocalVariableDeclaration $ VariableDeclaration "boolean" "b" $ Just $ BooleanLiteral False]) $ + parseBlock [LBRACKET,BOOLEAN,IDENTIFIER "b",ASSIGN,BOOLLITERAL False,SEMICOLON,RBRACKET] +testFieldNullWithInitialization = TestCase $ + assertEqual "expect Class with initialized field" [Class "WithInitField" [] [VariableDeclaration "Object" "bar" $ Just NullLiteral]] $ + parse [CLASS,IDENTIFIER "WithInitField",LBRACKET,IDENTIFIER "Object",IDENTIFIER "bar",ASSIGN,NULLLITERAL,SEMICOLON,RBRACKET] tests = TestList [ testSingleEmptyClass, @@ -96,5 +102,7 @@ tests = TestList [ testNestedBlocks, testBlockWithEmptyStatement, testExpressionIntLiteral, - testFieldWithInitialization + testFieldWithInitialization, + testLocalBoolWithInitialization, + testFieldNullWithInitialization ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 9dd31c6..b0a8173 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -328,9 +328,9 @@ exclusiveorexpression : andexpression { $1 } -- | exclusiveorexpression XOR andexpression { } literal : INTLITERAL { IntegerLiteral $1 } - -- | BOOLLITERAL { } - -- | CHARLITERAL { } - -- | JNULL { } + | BOOLLITERAL { BooleanLiteral $1 } + | CHARLITERAL { CharacterLiteral $1 } + | JNULL { NullLiteral } castexpression : LBRACE primitivetype RBRACE unaryexpression { } | LBRACE expression RBRACE unaryexpressionnotplusminus{ }