MiniJavaCompiler/src/Example.hs

105 lines
4.5 KiB
Haskell
Raw Normal View History

2024-05-06 21:15:22 +00:00
module Example where
2024-05-07 09:04:40 +00:00
import Typecheck
2024-05-06 21:15:22 +00:00
import Control.Exception (catch, evaluate, SomeException, displayException)
import Control.Exception.Base
2024-05-07 09:04:40 +00:00
import System.IO (stderr, hPutStrLn)
-- ANSI color codes
green, red, yellow, blue, magenta, cyan, white :: String -> String
green str = "\x1b[32m" ++ str ++ "\x1b[0m"
red str = "\x1b[31m" ++ str ++ "\x1b[0m"
yellow str = "\x1b[33m" ++ str ++ "\x1b[0m"
blue str = "\x1b[34m" ++ str ++ "\x1b[0m"
magenta str = "\x1b[35m" ++ str ++ "\x1b[0m"
cyan str = "\x1b[36m" ++ str ++ "\x1b[0m"
white str = "\x1b[37m" ++ str ++ "\x1b[0m"
-- Custom print function
printSuccess :: String -> IO ()
printSuccess msg = putStrLn $ green "Success:" ++ white msg
handleError :: SomeException -> IO ()
handleError e = hPutStrLn stderr $ red ("Error: " ++ displayException e)
printResult :: Show a => String -> a -> IO ()
printResult title result = do
putStrLn $ green title
print result
2024-05-06 21:15:22 +00:00
-- Example classes and their methods and fields
sampleClasses :: [Class]
sampleClasses = [
Class "Person" [
2024-05-07 09:04:40 +00:00
MethodDeclaration "void" "setAge" [ParameterDeclaration "int" "newAge"]
2024-05-06 21:15:22 +00:00
(Block [
LocalVariableDeclaration (VariableDeclaration "Int" "age" (Just (Reference "newAge")))
]),
2024-05-07 09:04:40 +00:00
MethodDeclaration "Int" "getAge" [] (Return (Just (Reference "age"))),
MethodDeclaration "Person" "Person" [ParameterDeclaration "int" "initialAge"] (Block [])
2024-05-06 21:15:22 +00:00
] [
VariableDeclaration "Int" "age" (Just (IntegerLiteral 25)),
VariableDeclaration "String" "name" (Just (CharacterLiteral 'A'))
]
]
-- Symbol table, mapping identifiers to their data types
initialSymtab :: [(DataType, Identifier)]
initialSymtab = []
exampleBlock :: Statement
exampleBlock = Block [
LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just (StatementExpressionExpression (ConstructorCall "Person" [])))),
2024-05-07 09:04:40 +00:00
BinaryOperation NameResolution (Reference "bob") (StatementExpressionExpression (MethodCall "setAge" [IntegerLiteral 30])),
2024-05-06 21:15:22 +00:00
Return (Just (StatementExpressionExpression (MethodCall "getAge" [])))
]
exampleExpression :: Expression
exampleExpression = BinaryOperation NameResolution (Reference "bob") (Reference "age")
2024-05-07 09:04:40 +00:00
exampleAssignment :: Expression
exampleAssignment = StatementExpressionExpression (Assignment "a" (IntegerLiteral 30))
exampleMethodCall :: Statement
exampleMethodCall = StatementExpressionStatement (MethodCall "setAge" [IntegerLiteral 30])
exampleConstructorCall :: Statement
exampleConstructorCall = LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just (StatementExpressionExpression (ConstructorCall "Person" [IntegerLiteral 30]))))
2024-05-06 21:15:22 +00:00
-- 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
2024-05-07 09:04:40 +00:00
catch (do
print "====================================================================================="
evaluatedExpression <- evaluate (typeCheckExpression exampleExpression [("bob", "Person")] sampleClasses)
printSuccess "Type checking of expression completed successfully"
printResult "Result Expression:" evaluatedExpression
) handleError
catch (do
print "====================================================================================="
evaluatedAssignment <- evaluate (typeCheckExpression exampleAssignment [("a", "int")] sampleClasses)
printSuccess "Type checking of assignment completed successfully"
printResult "Result Assignment:" evaluatedAssignment
) handleError
catch (do
print "====================================================================================="
evaluatedMethodCall <- evaluate (typeCheckStatement exampleMethodCall [("this", "Person"), ("setAge", "Person"), ("getAge", "Person")] sampleClasses)
printSuccess "Type checking of method call this completed successfully"
printResult "Result MethodCall:" evaluatedMethodCall
) handleError
catch (do
print "====================================================================================="
evaluatedConstructorCall <- evaluate (typeCheckStatement exampleConstructorCall [] sampleClasses)
printSuccess "Type checking of constructor call completed successfully"
printResult "Result Constructor Call:" evaluatedConstructorCall
) handleError
2024-05-06 21:15:22 +00:00