parser add ifthenelse

This commit is contained in:
Marvin Schlegel 2024-05-14 13:57:01 +02:00
parent fef619ac03
commit 5723f6c662
2 changed files with 18 additions and 6 deletions

View File

@ -145,6 +145,13 @@ testExpressionPreDecrement = TestCase $
assertEqual "expect PreIncrement" (UnaryOperation PreDecrement (Reference "a")) $
parseExpression [DECREMENT,IDENTIFIER "a"]
testStatementIfThen = TestCase $
assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $
parseStatement [IF,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET]
testStatementIfThenElse = TestCase $
assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) (Just (Block [Block []]))] $
parseStatement [IF,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET,ELSE,LBRACKET,RBRACKET]
tests = TestList [
testSingleEmptyClass,
@ -185,5 +192,7 @@ tests = TestList [
testExpressionPostIncrement,
testExpressionPostDecrement,
testExpressionPreIncrement,
testExpressionPreDecrement
testExpressionPreDecrement,
testStatementIfThen,
testStatementIfThenElse
]

View File

@ -202,7 +202,7 @@ localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 }
statement : statementwithouttrailingsubstatement{ $1 } -- statement returns a list of statements
| ifthenstatement { [$1] }
-- | ifthenelsestatement { }
| ifthenelsestatement { [$1] }
-- | whilestatement { }
@ -220,7 +220,7 @@ statementwithouttrailingsubstatement : block { [$1] }
ifthenstatement : IF LBRACE expression RBRACE statement { If $3 (Block $5) Nothing }
ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE statement { }
ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE statement { If $3 (Block $5) (Just (Block $7)) }
whilestatement : WHILE LBRACE expression RBRACE statement { }
@ -234,9 +234,9 @@ expressionstatement : statementexpression SEMICOLON { }
returnstatement : RETURN SEMICOLON { Return Nothing }
| RETURN expression SEMICOLON { Return $ Just $2 }
statementnoshortif : statementwithouttrailingsubstatement { }
| ifthenelsestatementnoshortif { }
| whilestatementnoshortif { }
statementnoshortif : statementwithouttrailingsubstatement { $1 }
-- | ifthenelsestatementnoshortif { }
-- | whilestatementnoshortif { }
conditionalexpression : conditionalorexpression { $1 }
-- | conditionalorexpression QUESMARK expression COLON conditionalexpression { }
@ -370,6 +370,9 @@ data Declarator = Declarator Identifier (Maybe Expression)
convertDeclarator :: DataType -> Declarator -> VariableDeclaration
convertDeclarator dataType (Declarator id assigment) = VariableDeclaration dataType id assigment
data StatementWithoutSub = Statement
parseError :: ([Token], [String]) -> a
parseError (errortoken, expected) = error ("parse error on token: " ++ show errortoken ++ "\nexpected one of: " ++ show expected)