From c690b0139634a89bf6c7dca6013778ee3dd892d0 Mon Sep 17 00:00:00 2001 From: Fabian Noll Date: Fri, 31 May 2024 10:07:01 +0200 Subject: [PATCH] change back UnaryOperation and move it to StatementExpression --- src/Typecheck.hs | 102 ++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/Typecheck.hs b/src/Typecheck.hs index ba49157..0564ce9 100644 --- a/src/Typecheck.hs +++ b/src/Typecheck.hs @@ -78,22 +78,6 @@ typeCheckExpression (BinaryOperation op expr1 expr2) symtab classes = Or -> checkLogicalOperation op expr1' expr2' type1 type2 NameResolution -> resolveNameResolution expr1' expr2 symtab classes -typeCheckExpression (UnaryOperation op expr) symtab classes = - let expr' = typeCheckExpression expr symtab classes - type' = getTypeFromExpr expr' - in case op of - Not -> - if type' == "boolean" - then - TypedExpression "boolean" (UnaryOperation op expr') - else - error "Logical NOT operation requires an operand of type boolean" - Minus -> - if type' == "int" || type' == "char" - then - TypedExpression type' (UnaryOperation op expr') - else - error "Unary minus operation requires an operand of type int or char" typeCheckExpression (StatementExpressionExpression stmtExpr) symtab classes = let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes @@ -163,41 +147,61 @@ 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 = +typeCheckStatementExpression (UnaryOperation op 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" + in case op of + Not -> + if type' == "boolean" + then + TypedStatementExpression "boolean" (UnaryOperation op expr') + else + error "Logical NOT operation requires an operand of type boolean" + Minus -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Unary minus operation requires an operand of type int or char" + PostIncrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Post-increment operation requires an operand of type int or char" + PostDecrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Post-decrement operation requires an operand of type int or char" + PreIncrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Pre-increment operation requires an operand of type int or char" + PreDecrement -> + if type' == "int" + then + TypedStatementExpression "int" (UnaryOperation op expr') + else if type' == "char" + then + TypedStatementExpression "char" (UnaryOperation op expr') + else + error "Pre-decrement operation requires an operand of type int or char" -- ********************************** Type Checking: Statements **********************************