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