From 524c667c43d41790c0100f09d0985cdefc68f6f2 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 14 May 2024 16:48:45 +0200 Subject: [PATCH 1/8] parser add while loop --- Test/TestParser.hs | 7 ++++++- src/Parser/JavaParser.y | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index b7cd93e..a5b94fa 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -151,6 +151,10 @@ testStatementIfThen = TestCase $ 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] +testStatementWhile = TestCase $ + assertEqual "expect while" [While (Reference "a") (Block [Block []])] $ + parseStatement [WHILE,LBRACE,IDENTIFIER "a",RBRACE,LBRACKET,RBRACKET] + tests = TestList [ @@ -194,5 +198,6 @@ tests = TestList [ testExpressionPreIncrement, testExpressionPreDecrement, testStatementIfThen, - testStatementIfThenElse + testStatementIfThenElse, + testStatementWhile ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 2e80289..b3e0e43 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -203,7 +203,7 @@ localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 } statement : statementwithouttrailingsubstatement{ $1 } -- statement returns a list of statements | ifthenstatement { [$1] } | ifthenelsestatement { [$1] } - -- | whilestatement { } + | whilestatement { [$1] } expression : assignmentexpression { $1 } @@ -222,7 +222,7 @@ ifthenstatement : IF LBRACE expression RBRACE statement { If $3 (Block $5) No ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE statement { If $3 (Block $5) (Just (Block $7)) } -whilestatement : WHILE LBRACE expression RBRACE statement { } +whilestatement : WHILE LBRACE expression RBRACE statement { While $3 (Block $5) } assignmentexpression : conditionalexpression { $1 } -- | assignment { } From 9f658397de204f501d80b1318969dc0f9b983b5c Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 14 May 2024 23:14:43 +0200 Subject: [PATCH 2/8] parser implement assign --- Test/TestParser.hs | 4 ++++ src/Parser/JavaParser.y | 30 ++++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index a5b94fa..2911f3f 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -144,6 +144,9 @@ testExpressionPreIncrement = TestCase $ testExpressionPreDecrement = TestCase $ assertEqual "expect PreIncrement" (UnaryOperation PreDecrement (Reference "a")) $ parseExpression [DECREMENT,IDENTIFIER "a"] +testExpressionAssign = TestCase $ + assertEqual "expect assign and addition" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $ + parseExpression [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -197,6 +200,7 @@ tests = TestList [ testExpressionPostDecrement, testExpressionPreIncrement, testExpressionPreDecrement, + testExpressionAssign, testStatementIfThen, testStatementIfThenElse, testStatementWhile diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index b3e0e43..fcd3559 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -82,7 +82,7 @@ compilationunit : typedeclarations { $1 } typedeclarations : typedeclaration { [$1] } | typedeclarations typedeclaration { $1 ++ [$2] } -name : simplename { $1 } +name : simplename { Reference $1 } -- | qualifiedname { } typedeclaration : classdeclaration { $1 } @@ -122,7 +122,7 @@ classtype : classorinterfacetype{ } classbodydeclaration : classmemberdeclaration { $1 } | constructordeclaration { $1 } -classorinterfacetype : name { $1 } +classorinterfacetype : simplename { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } @@ -225,7 +225,7 @@ ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE state whilestatement : WHILE LBRACE expression RBRACE statement { While $3 (Block $5) } assignmentexpression : conditionalexpression { $1 } - -- | assignment { } + | assignment { StatementExpressionExpression $1 } emptystatement : SEMICOLON { Block [] } @@ -241,7 +241,9 @@ statementnoshortif : statementwithouttrailingsubstatement { $1 } conditionalexpression : conditionalorexpression { $1 } -- | conditionalorexpression QUESMARK expression COLON conditionalexpression { } -assignment : lefthandside assignmentoperator assignmentexpression { } +assignment : lefthandside assignmentoperator assignmentexpression { + Assignment $1 $3 + } statementexpression : assignment { } @@ -262,18 +264,18 @@ conditionalorexpression : conditionalandexpression { $1 } lefthandside : name { $1 } -assignmentoperator : ASSIGN{ } - -- | TIMESEQUAL { } - -- | DIVIDEEQUAL { } - -- | MODULOEQUAL { } - -- | PLUSEQUAL { } - -- | MINUSEQUAL { } +assignmentoperator : ASSIGN { Nothing } + | TIMESEQUAL { Just Multiplication } + | DIVIDEEQUAL { Just Division } + | MODULOEQUAL { Just Modulo } + | PLUSEQUAL { Just Addition } + | MINUSEQUAL { Just Subtraction } -- | SHIFTLEFTEQUAL { } -- | SIGNEDSHIFTRIGHTEQUAL { } -- | UNSIGNEDSHIFTRIGHTEQUAL { } - -- | ANDEQUAL { } - -- | XOREQUAL { } - -- | OREQUAL{ } + | ANDEQUAL { Just BitwiseAnd } + | XOREQUAL { Just BitwiseXor } + | OREQUAL{ Just BitwiseOr } preincrementexpression : INCREMENT unaryexpression { UnaryOperation PreIncrement $2 } @@ -302,7 +304,7 @@ unaryexpression : unaryexpressionnotplusminus { $1 } | preincrementexpression { $1 } postfixexpression : primary { $1 } - | name { Reference $1 } + | name { $1 } | postincrementexpression { $1 } | postdecrementexpression{ $1 } From a4d41b9ef73429649f262acf7183a80d7bd21b50 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 14 May 2024 23:35:56 +0200 Subject: [PATCH 3/8] parser add timesequal, divideequal, moduloequal, minusequal, andequal, xorequal, orequal --- Test/TestParser.hs | 19 ++++++++++++++++++- src/Parser/JavaParser.y | 4 +++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 2911f3f..873f438 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -145,8 +145,20 @@ testExpressionPreDecrement = TestCase $ assertEqual "expect PreIncrement" (UnaryOperation PreDecrement (Reference "a")) $ parseExpression [DECREMENT,IDENTIFIER "a"] testExpressionAssign = TestCase $ - assertEqual "expect assign and addition" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $ + assertEqual "expect assign 5 to a" (StatementExpressionExpression (Assignment (Reference "a") (IntegerLiteral 5))) $ parseExpression [IDENTIFIER "a",ASSIGN,INTEGERLITERAL 5] +testExpressionTimesEqual = TestCase $ + assertEqual "expect assign and multiplication" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Multiplication (Reference "a") (IntegerLiteral 5)))) $ + parseExpression [IDENTIFIER "a",TIMESEQUAL,INTEGERLITERAL 5] +testExpressionDivideEqual = TestCase $ + assertEqual "expect assign and division" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Division (Reference "a") (IntegerLiteral 5)))) $ + parseExpression [IDENTIFIER "a",DIVEQUAL,INTEGERLITERAL 5] +testExpressionPlusEqual = TestCase $ + assertEqual "expect assign and addition" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Addition (Reference "a") (IntegerLiteral 5)))) $ + parseExpression [IDENTIFIER "a",PLUSEQUAL,INTEGERLITERAL 5] +testExpressionMinusEqual = TestCase $ + assertEqual "expect assign and subtraction" (StatementExpressionExpression (Assignment (Reference "a") (BinaryOperation Subtraction (Reference "a") (IntegerLiteral 5)))) $ + parseExpression [IDENTIFIER "a",MINUSEQUAL,INTEGERLITERAL 5] testStatementIfThen = TestCase $ assertEqual "expect empty ifthen" [If (Reference "a") (Block [Block []]) Nothing] $ @@ -201,6 +213,11 @@ tests = TestList [ testExpressionPreIncrement, testExpressionPreDecrement, testExpressionAssign, + testExpressionTimesEqual, + testExpressionTimesEqual, + testExpressionDivideEqual, + testExpressionPlusEqual, + testExpressionMinusEqual, testStatementIfThen, testStatementIfThenElse, testStatementWhile diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index fcd3559..4e66850 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -242,7 +242,9 @@ conditionalexpression : conditionalorexpression { $1 } -- | conditionalorexpression QUESMARK expression COLON conditionalexpression { } assignment : lefthandside assignmentoperator assignmentexpression { - Assignment $1 $3 + case $2 of + Nothing -> Assignment $1 $3 + Just operator -> Assignment $1 (BinaryOperation operator $1 $3) } From a80dc1d34be6717fbf39ef77a979c00f9c327a5a Mon Sep 17 00:00:00 2001 From: MisterChaos96 Date: Wed, 15 May 2024 12:51:14 +0200 Subject: [PATCH 4/8] null is now a valid declaration value for a object --- src/Example.hs | 30 +++++++++++++++++++++++++----- src/Typecheck.hs | 11 +++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Example.hs b/src/Example.hs index db4213f..38df899 100644 --- a/src/Example.hs +++ b/src/Example.hs @@ -101,24 +101,30 @@ exampleNameResolutionAssignment = Block [ exampleCharIntOperation :: Expression exampleCharIntOperation = BinaryOperation Addition (CharacterLiteral 'a') (IntegerLiteral 1) +exampleNullDeclaration :: Statement +exampleNullDeclaration = LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just NullLiteral)) + +exampleNullDeclarationFail :: Statement +exampleNullDeclarationFail = LocalVariableDeclaration (VariableDeclaration "int" "a" (Just NullLiteral)) + testClasses :: [Class] testClasses = [ Class "Person" [ - MethodDeclaration "Person" "Person" [ParameterDeclaration "int" "initialAge"] + MethodDeclaration "Person" "Person" [ParameterDeclaration "int" "initialAge"] (Block [ Return (Just (Reference "this")) ]), - MethodDeclaration "void" "setAge" [ParameterDeclaration "int" "newAge"] + MethodDeclaration "void" "setAge" [ParameterDeclaration "int" "newAge"] (Block [ LocalVariableDeclaration (VariableDeclaration "int" "age" (Just (Reference "newAge"))) ]), - MethodDeclaration "int" "getAge" [] + MethodDeclaration "int" "getAge" [] (Return (Just (Reference "age"))) ] [ VariableDeclaration "int" "age" Nothing -- initially unassigned ], Class "Main" [ - MethodDeclaration "int" "main" [] + MethodDeclaration "int" "main" [] (Block [ LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just (StatementExpressionExpression (ConstructorCall "Person" [IntegerLiteral 25])))), StatementExpressionStatement (MethodCall (Reference "bob") "setAge" [IntegerLiteral 30]), @@ -211,7 +217,7 @@ runTypeCheck = do printSuccess "Type checking of Program completed successfully" printResult "Typed Program:" typedProgram ) handleError - + catch (do print "=====================================================================================" typedAssignment <- evaluate (typeCheckStatement exampleNameResolutionAssignment [] sampleClasses) @@ -226,3 +232,17 @@ runTypeCheck = do printResult "Result Char Int Operation:" evaluatedCharIntOperation ) handleError + catch (do + print "=====================================================================================" + evaluatedNullDeclaration <- evaluate (typeCheckStatement exampleNullDeclaration [] sampleClasses) + printSuccess "Type checking of null declaration completed successfully" + printResult "Result Null Declaration:" evaluatedNullDeclaration + ) handleError + + catch (do + print "=====================================================================================" + evaluatedNullDeclarationFail <- evaluate (typeCheckStatement exampleNullDeclarationFail [] sampleClasses) + printSuccess "Type checking of null declaration failed" + printResult "Result Null Declaration:" evaluatedNullDeclarationFail + ) handleError + diff --git a/src/Typecheck.hs b/src/Typecheck.hs index 0032d46..f691558 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -226,8 +226,12 @@ typeCheckStatement (LocalVariableDeclaration (VariableDeclaration dataType ident let checkedExpr = fmap (\expr -> typeCheckExpression expr symtab classes) maybeExpr exprType = fmap getTypeFromExpr checkedExpr in case exprType of - Just t | t /= dataType -> error $ "Type mismatch in declaration of '" ++ identifier ++ "': expected " ++ dataType ++ ", found " ++ t - _ -> TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr)) + Just t + | t == "null" && isObjectType dataType -> + TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr)) + | t /= dataType -> error $ "Type mismatch in declaration of '" ++ identifier ++ "': expected " ++ dataType ++ ", found " ++ t + | otherwise -> TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr)) + Nothing -> TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr)) typeCheckStatement (While cond stmt) symtab classes = let cond' = typeCheckExpression cond symtab classes @@ -278,6 +282,9 @@ typeCheckStatement (StatementExpressionStatement stmtExpr) symtab classes = -- ********************************** Type Checking: Helpers ********************************** +isObjectType :: DataType -> Bool +isObjectType dt = dt /= "int" && dt /= "boolean" && dt /= "char" + getTypeFromExpr :: Expression -> DataType getTypeFromExpr (TypedExpression t _) = t getTypeFromExpr _ = error "Untyped expression found where typed was expected" From 07837f7d5f5e9de3bbd9279c1c9fbbf75e958f8e Mon Sep 17 00:00:00 2001 From: MisterChaos96 Date: Wed, 15 May 2024 13:04:06 +0200 Subject: [PATCH 5/8] null is now a valid declaration value for a object --- src/Example.hs | 9 +++++++++ src/Typecheck.hs | 10 +++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Example.hs b/src/Example.hs index 38df899..080b738 100644 --- a/src/Example.hs +++ b/src/Example.hs @@ -107,6 +107,9 @@ exampleNullDeclaration = LocalVariableDeclaration (VariableDeclaration "Person" exampleNullDeclarationFail :: Statement exampleNullDeclarationFail = LocalVariableDeclaration (VariableDeclaration "int" "a" (Just NullLiteral)) +exampleNullAssignment :: Statement +exampleNullAssignment = StatementExpressionStatement (Assignment (Reference "a") NullLiteral) + testClasses :: [Class] testClasses = [ Class "Person" [ @@ -246,3 +249,9 @@ runTypeCheck = do printResult "Result Null Declaration:" evaluatedNullDeclarationFail ) handleError + catch (do + print "=====================================================================================" + evaluatedNullAssignment <- evaluate (typeCheckStatement exampleNullAssignment [("a", "Person")] sampleClasses) + printSuccess "Type checking of null assignment completed successfully" + printResult "Result Null Assignment:" evaluatedNullAssignment + ) handleError diff --git a/src/Typecheck.hs b/src/Typecheck.hs index f691558..8755458 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -146,11 +146,11 @@ typeCheckStatementExpression (Assignment ref expr) symtab classes = ref' = typeCheckExpression ref symtab classes type' = getTypeFromExpr expr' type'' = getTypeFromExpr ref' - in - if type'' == type' then - TypedStatementExpression type' (Assignment ref' expr') - else - error $ "Type mismatch in assignment to variable: expected " ++ type'' ++ ", found " ++ type' + in + if type'' == type' || (type' == "null" && isObjectType type'') then + TypedStatementExpression type'' (Assignment ref' expr') + else + error $ "Type mismatch in assignment to variable: expected " ++ type'' ++ ", found " ++ type' typeCheckStatementExpression (ConstructorCall className args) symtab classes = case find (\(Class name _ _) -> name == className) classes of From 2a502a6c673c00809dda0760f5b2633b60ea6c08 Mon Sep 17 00:00:00 2001 From: MisterChaos96 Date: Thu, 16 May 2024 09:36:35 +0200 Subject: [PATCH 6/8] fix return null for object return type not possible --- src/Typecheck.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Typecheck.hs b/src/Typecheck.hs index 8755458..5834f57 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -25,7 +25,7 @@ typeCheckMethodDeclaration (MethodDeclaration retType name params body) classFie checkedBody = typeCheckStatement body initialSymtab classes bodyType = getTypeFromStmt checkedBody -- Check if the type of the body matches the declared return type - in if bodyType == retType || (bodyType == "void" && retType == "void") + in if bodyType == retType || (bodyType == "void" && retType == "void") || (bodyType == "null" && isObjectType retType) then MethodDeclaration retType name params checkedBody else error $ "Return type mismatch in method " ++ name ++ ": expected " ++ retType ++ ", found " ++ bodyType From 067bf8d7965af5da0ff2bfe946a6b36f289f13f0 Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Sun, 19 May 2024 09:20:43 +0200 Subject: [PATCH 7/8] refactor: move incremental types to statementsexpressions --- src/Ast.hs | 8 ++--- src/Example.hs | 10 ++++++ src/Typecheck.hs | 79 +++++++++++++++++++++++------------------------- 3 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/Ast.hs b/src/Ast.hs index 9634ab8..a20b8e8 100644 --- a/src/Ast.hs +++ b/src/Ast.hs @@ -24,6 +24,10 @@ data StatementExpression | ConstructorCall DataType [Expression] | MethodCall Expression Identifier [Expression] | TypedStatementExpression DataType StatementExpression + | PostIncrement Expression + | PostDecrement Expression + | PreIncrement Expression + | PreDecrement Expression deriving (Show, Eq) data BinaryOperator @@ -49,10 +53,6 @@ data BinaryOperator data UnaryOperator = Not | Minus - | PostIncrement - | PostDecrement - | PreIncrement - | PreDecrement deriving (Show, Eq) data Expression diff --git a/src/Example.hs b/src/Example.hs index 080b738..03ff209 100644 --- a/src/Example.hs +++ b/src/Example.hs @@ -110,6 +110,9 @@ exampleNullDeclarationFail = LocalVariableDeclaration (VariableDeclaration "int" exampleNullAssignment :: Statement exampleNullAssignment = StatementExpressionStatement (Assignment (Reference "a") NullLiteral) +exampleIncrement :: Statement +exampleIncrement = StatementExpressionStatement (PostIncrement (Reference "a")) + testClasses :: [Class] testClasses = [ Class "Person" [ @@ -255,3 +258,10 @@ runTypeCheck = do printSuccess "Type checking of null assignment completed successfully" printResult "Result Null Assignment:" evaluatedNullAssignment ) handleError + + catch (do + print "=====================================================================================" + evaluatedIncrement <- evaluate (typeCheckStatement exampleIncrement [("a", "int")] sampleClasses) + printSuccess "Type checking of increment completed successfully" + printResult "Result Increment:" evaluatedIncrement + ) handleError diff --git a/src/Typecheck.hs b/src/Typecheck.hs index 5834f57..bac30a7 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -89,50 +89,11 @@ typeCheckExpression (UnaryOperation op expr) symtab classes = else error "Logical NOT operation requires an operand of type boolean" Minus -> - if type' == "int" + if type' == "int" || type' == "char" then - TypedExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedExpression "char" (UnaryOperation op expr') + TypedExpression type' (UnaryOperation op expr') else error "Unary minus operation requires an operand of type int or char" - PostIncrement -> - if type' == "int" - then - TypedExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedExpression "char" (UnaryOperation op expr') - else - error "Post-increment operation requires an operand of type int or char" - PostDecrement -> - if type' == "int" - then - TypedExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedExpression "char" (UnaryOperation op expr') - else - error "Post-decrement operation requires an operand of type int or char" - PreIncrement -> - if type' == "int" - then - TypedExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedExpression "char" (UnaryOperation op expr') - else - error "Pre-increment operation requires an operand of type int or char" - PreDecrement -> - if type' == "int" - then - TypedExpression "int" (UnaryOperation op expr') - else if type' == "char" - then - TypedExpression "char" (UnaryOperation op expr') - else - error "Pre-decrement operation requires an operand of type int or char" typeCheckExpression (StatementExpressionExpression stmtExpr) symtab classes = let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes @@ -202,6 +163,42 @@ typeCheckStatementExpression (MethodCall expr methodName args) symtab classes = Nothing -> error $ "Class for object type '" ++ objType ++ "' not found." _ -> error "Invalid object type for method call. Object must have a class type." +typeCheckStatementExpression (PostIncrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PostIncrement expr') + else + error "Post-increment operation requires an operand of type int or char" + +typeCheckStatementExpression (PostDecrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PostDecrement expr') + else + error "Post-decrement operation requires an operand of type int or char" + +typeCheckStatementExpression (PreIncrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PreIncrement expr') + else + error "Pre-increment operation requires an operand of type int or char" + +typeCheckStatementExpression (PreDecrement expr) symtab classes = + let expr' = typeCheckExpression expr symtab classes + type' = getTypeFromExpr expr' + in if type' == "int" || type' == "char" + then + TypedStatementExpression type' (PreDecrement expr') + else + error "Pre-decrement operation requires an operand of type int or char" + -- ********************************** Type Checking: Statements ********************************** typeCheckStatement :: Statement -> [(Identifier, DataType)] -> [Class] -> Statement From 6ab64371b5a7595160731bf79b40c644816520f9 Mon Sep 17 00:00:00 2001 From: MisterChaos96 Date: Sun, 26 May 2024 21:45:11 +0200 Subject: [PATCH 8/8] feat: Add support for block statements in type checking --- src/Typecheck.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Typecheck.hs b/src/Typecheck.hs index 5834f57..1bfda74 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -258,6 +258,7 @@ typeCheckStatement (Block statements) symtab classes = If {} -> (accSts ++ [checkedStmt], currentSymtab, if stmtType /= "void" then types ++ [stmtType] else types) While _ _ -> (accSts ++ [checkedStmt], currentSymtab, if stmtType /= "void" then types ++ [stmtType] else types) Return _ -> (accSts ++ [checkedStmt], currentSymtab, if stmtType /= "void" then types ++ [stmtType] else types) + Block _ -> (accSts ++ [checkedStmt], currentSymtab, if stmtType /= "void" then types ++ [stmtType] else types) _ -> (accSts ++ [checkedStmt], currentSymtab, types) -- Initial accumulator: empty statements list, initial symbol table, empty types list