From ced5d1df9c79c72b265a5bf1666efd06dcfb6e77 Mon Sep 17 00:00:00 2001 From: MisterChaos96 Date: Tue, 7 May 2024 20:10:39 +0200 Subject: [PATCH] add program tests --- src/Example.hs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Example.hs b/src/Example.hs index c1ce6bf..7f345b3 100644 --- a/src/Example.hs +++ b/src/Example.hs @@ -5,6 +5,8 @@ import Typecheck import Control.Exception (catch, evaluate, SomeException, displayException) import Control.Exception.Base import System.IO (stderr, hPutStrLn) +import Data.Maybe +import Data.List green, red, yellow, blue, magenta, cyan, white :: String -> String green str = "\x1b[32m" ++ str ++ "\x1b[0m" @@ -90,6 +92,33 @@ exampleMethodCallAndAssignmentFail = Block [ StatementExpressionStatement (Assignment "a" (Reference "age")) ] +testClasses :: [Class] +testClasses = [ + Class "Person" [ + MethodDeclaration "Person" "Person" [ParameterDeclaration "int" "initialAge"] + (Block [ + Return (Just (Reference "this")) + ]), + MethodDeclaration "void" "setAge" [ParameterDeclaration "int" "newAge"] + (Block [ + LocalVariableDeclaration (VariableDeclaration "int" "age" (Just (Reference "newAge"))) + ]), + MethodDeclaration "int" "getAge" [] + (Return (Just (Reference "age"))) + ] [ + VariableDeclaration "int" "age" Nothing -- initially unassigned + ], + Class "Main" [ + MethodDeclaration "int" "main" [] + (Block [ + LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just (StatementExpressionExpression (ConstructorCall "Person" [IntegerLiteral 25])))), + StatementExpressionStatement (MethodCall (Reference "bob") "setAge" [IntegerLiteral 30]), + LocalVariableDeclaration (VariableDeclaration "int" "bobAge" (Just (StatementExpressionExpression (MethodCall (Reference "bob") "getAge" [])))), + Return (Just (Reference "bobAge")) + ]) + ] [] + ] + runTypeCheck :: IO () runTypeCheck = do catch (do @@ -147,7 +176,7 @@ runTypeCheck = do printSuccess "Type checking of method call and assignment completed successfully" printResult "Result Method Call and Assignment:" evaluatedMethodCallAndAssignment ) handleError - + catch (do print "=====================================================================================" evaluatedMethodCallAndAssignmentFail <- evaluate (typeCheckStatement exampleMethodCallAndAssignmentFail [] sampleClasses) @@ -155,3 +184,20 @@ runTypeCheck = do printResult "Result Method Call and Assignment:" evaluatedMethodCallAndAssignmentFail ) handleError + catch (do + print "=====================================================================================" + let mainClass = fromJust $ find (\(Class className _ _) -> className == "Main") testClasses + case mainClass of + Class _ [mainMethod] _ -> do + let result = typeCheckMethodDeclaration mainMethod [] testClasses + printSuccess "Full program type checking completed successfully." + printResult "Main method result:" result + ) handleError + + catch (do + print "=====================================================================================" + let typedProgram = typeCheckCompilationUnit testClasses + printSuccess "Type checking of Program completed successfully" + printResult "Typed Program:" typedProgram + ) handleError +