parser implement nested blocks

This commit is contained in:
Marvin Schlegel 2024-05-08 10:37:40 +02:00
parent a4b933d659
commit e8151ad2f0
2 changed files with 14 additions and 10 deletions

View File

@ -61,6 +61,9 @@ testBlockWithLocalVarDecl = TestCase $
testBlockWithMultipleLocalVarDecls = TestCase $ testBlockWithMultipleLocalVarDecls = TestCase $
assertEqual "expect block with multiple local var declarations" (Block [LocalVariableDeclaration $ VariableDeclaration "int" "var1" Nothing, LocalVariableDeclaration $ VariableDeclaration "boolean" "var2" Nothing]) $ assertEqual "expect block with multiple local var declarations" (Block [LocalVariableDeclaration $ VariableDeclaration "int" "var1" Nothing, LocalVariableDeclaration $ VariableDeclaration "boolean" "var2" Nothing]) $
parseBlock [LBRACKET,INT,IDENTIFIER "var1",SEMICOLON,BOOLEAN,IDENTIFIER "var2",SEMICOLON,RBRACKET] parseBlock [LBRACKET,INT,IDENTIFIER "var1",SEMICOLON,BOOLEAN,IDENTIFIER "var2",SEMICOLON,RBRACKET]
testNestedBlocks = TestCase $
assertEqual "expect block with block inside" (Block [Block []]) $
parseBlock [LBRACKET,LBRACKET,RBRACKET,RBRACKET]
tests = TestList [ tests = TestList [
@ -80,5 +83,6 @@ tests = TestList [
testClassWithConstructor, testClassWithConstructor,
testEmptyBlock, testEmptyBlock,
testBlockWithLocalVarDecl, testBlockWithLocalVarDecl,
testBlockWithMultipleLocalVarDecls testBlockWithMultipleLocalVarDecls,
testNestedBlocks
] ]

View File

@ -185,7 +185,7 @@ variabledeclarator : variabledeclaratorid { Declarator $1 Nothing }
-- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO -- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO
blockstatement : localvariabledeclarationstatement { $1 } blockstatement : localvariabledeclarationstatement { $1 }
-- | statement { } | statement { [$1] }
formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 }
@ -200,10 +200,10 @@ variableinitializer : expression { }
localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 } localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 }
statement : statementwithouttrailingsubstatement{ } statement : statementwithouttrailingsubstatement{ $1 }
| ifthenstatement { } -- | ifthenstatement { }
| ifthenelsestatement { } -- | ifthenelsestatement { }
| whilestatement { } -- | whilestatement { }
expression : assignmentexpression { } expression : assignmentexpression { }
@ -213,10 +213,10 @@ integraltype : INT { "int" }
localvariabledeclaration : type variabledeclarators { map LocalVariableDeclaration $ map (convertDeclarator $1) $2 } localvariabledeclaration : type variabledeclarators { map LocalVariableDeclaration $ map (convertDeclarator $1) $2 }
statementwithouttrailingsubstatement : block { } statementwithouttrailingsubstatement : block { $1 }
| emptystatement { } -- | emptystatement { }
| expressionstatement { } -- | expressionstatement { }
| returnstatement { } -- | returnstatement { }
ifthenstatement : IF LBRACE expression RBRACE statement { } ifthenstatement : IF LBRACE expression RBRACE statement { }