From 8d9d28cb1fd9c2ee3c7fac3835c40557072026b7 Mon Sep 17 00:00:00 2001 From: MisterChaos96 Date: Mon, 6 May 2024 23:15:22 +0200 Subject: [PATCH] added means of testing --- project.cabal | 2 +- src/Example.hs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Main.hs | 5 ++++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/Example.hs diff --git a/project.cabal b/project.cabal index 69ef52a..de2b99b 100644 --- a/project.cabal +++ b/project.cabal @@ -12,7 +12,7 @@ executable compiler default-language: Haskell2010 hs-source-dirs: src build-tool-depends: alex:alex, happy:happy - other-modules: Parser.Lexer, Ast + other-modules: Parser.Lexer, Ast, Example test-suite tests type: exitcode-stdio-1.0 diff --git a/src/Example.hs b/src/Example.hs new file mode 100644 index 0000000..b45130c --- /dev/null +++ b/src/Example.hs @@ -0,0 +1,49 @@ +module Example where + +import Ast +import Control.Exception (catch, evaluate, SomeException, displayException) +import Control.Exception.Base + +-- Example classes and their methods and fields +sampleClasses :: [Class] +sampleClasses = [ + Class "Person" [ + MethodDeclaration "void" "setAge" [ParameterDeclaration "Int" "newAge"] + (Block [ + LocalVariableDeclaration (VariableDeclaration "Int" "age" (Just (Reference "newAge"))) + ]), + MethodDeclaration "Int" "getAge" [] (Return (Just (Reference "age"))) + ] [ + VariableDeclaration "Int" "age" (Just (IntegerLiteral 25)), + VariableDeclaration "String" "name" (Just (CharacterLiteral 'A')) + ] + ] + +-- Symbol table, mapping identifiers to their data types +initialSymtab :: [(DataType, Identifier)] +initialSymtab = [] + +-- An example block of statements to type check +exampleBlock :: Statement +exampleBlock = Block [ + LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just (StatementExpressionExpression (ConstructorCall "Person" [])))), + StatementExpressionStatement (MethodCall "setAge" [IntegerLiteral 30]), + Return (Just (StatementExpressionExpression (MethodCall "getAge" []))) + ] + +exampleExpression :: Expression +exampleExpression = BinaryOperation NameResolution (Reference "bob") (Reference "age") + +-- Function to perform type checking and handle errors +runTypeCheck :: IO () +runTypeCheck = do + -- Evaluate the block of statements + --evaluatedBlock <- evaluate (typeCheckStatement exampleBlock initialSymtab sampleClasses) + --putStrLn "Type checking of block completed successfully:" + --print evaluatedBlock + + -- Evaluate the expression + evaluatedExpression <- evaluate (typeCheckExpression exampleExpression [("bob", "Person"), ("age", "int")] sampleClasses) + putStrLn "Type checking of expression completed successfully:" + print evaluatedExpression + diff --git a/src/Main.hs b/src/Main.hs index 9b3e8f6..c201618 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,6 +1,9 @@ module Main where import Parser.Lexer +import Example main = do - print $ alexScanTokens "/**/" \ No newline at end of file + --print $ alexScanTokens "/**/" + Example.runTypeCheck + \ No newline at end of file