removed unneccesary comments
This commit is contained in:
parent
d47c7f5a45
commit
b040130569
@ -9,7 +9,7 @@ typeCheckCompilationUnit classes = map (`typeCheckClass` classes) classes
|
|||||||
typeCheckClass :: Class -> [Class] -> Class
|
typeCheckClass :: Class -> [Class] -> Class
|
||||||
typeCheckClass (Class className methods fields) classes =
|
typeCheckClass (Class className methods fields) classes =
|
||||||
let
|
let
|
||||||
-- Create a symbol table from class fields
|
-- Create a symbol table from class fields and method entries
|
||||||
classFields = [(id, dt) | VariableDeclaration dt id _ <- fields]
|
classFields = [(id, dt) | VariableDeclaration dt id _ <- fields]
|
||||||
methodEntries = [(methodName, className) | MethodDeclaration _ methodName _ _ <- methods]
|
methodEntries = [(methodName, className) | MethodDeclaration _ methodName _ _ <- methods]
|
||||||
initalSymTab = ("this", className) : classFields ++ methodEntries
|
initalSymTab = ("this", className) : classFields ++ methodEntries
|
||||||
@ -21,9 +21,7 @@ typeCheckMethodDeclaration (MethodDeclaration retType name params body) classFie
|
|||||||
let
|
let
|
||||||
-- Combine class fields with method parameters to form the initial symbol table for the method
|
-- Combine class fields with method parameters to form the initial symbol table for the method
|
||||||
methodParams = [(identifier, dataType) | ParameterDeclaration dataType identifier <- params]
|
methodParams = [(identifier, dataType) | ParameterDeclaration dataType identifier <- params]
|
||||||
-- Ensure method parameters shadow class fields if names collide
|
|
||||||
initialSymtab = classFields ++ methodParams
|
initialSymtab = classFields ++ methodParams
|
||||||
-- Type check the body of the method using the combined symbol table
|
|
||||||
checkedBody = typeCheckStatement body initialSymtab classes
|
checkedBody = typeCheckStatement body initialSymtab classes
|
||||||
bodyType = getTypeFromStmt checkedBody
|
bodyType = getTypeFromStmt checkedBody
|
||||||
-- Check if the type of the body matches the declared return type
|
-- Check if the type of the body matches the declared return type
|
||||||
@ -211,17 +209,13 @@ typeCheckStatementExpression (ConstructorCall className args) symtab classes =
|
|||||||
TypedStatementExpression className (ConstructorCall className args')
|
TypedStatementExpression className (ConstructorCall className args')
|
||||||
|
|
||||||
typeCheckStatementExpression (MethodCall expr methodName args) symtab classes =
|
typeCheckStatementExpression (MethodCall expr methodName args) symtab classes =
|
||||||
-- First, evaluate the object expression to find its type
|
|
||||||
let objExprTyped = typeCheckExpression expr symtab classes
|
let objExprTyped = typeCheckExpression expr symtab classes
|
||||||
in case objExprTyped of
|
in case objExprTyped of
|
||||||
TypedExpression objType _ ->
|
TypedExpression objType _ ->
|
||||||
-- Look for the class of the object type to find the method
|
|
||||||
case find (\(Class className _ _) -> className == objType) classes of
|
case find (\(Class className _ _) -> className == objType) classes of
|
||||||
Just (Class _ methods _) ->
|
Just (Class _ methods _) ->
|
||||||
-- Find the method within the class
|
|
||||||
case find (\(MethodDeclaration retType name params _) -> name == methodName) methods of
|
case find (\(MethodDeclaration retType name params _) -> name == methodName) methods of
|
||||||
Just (MethodDeclaration retType _ params _) ->
|
Just (MethodDeclaration retType _ params _) ->
|
||||||
-- Check arguments
|
|
||||||
let args' = map (\arg -> typeCheckExpression arg symtab classes) args
|
let args' = map (\arg -> typeCheckExpression arg symtab classes) args
|
||||||
expectedTypes = [dataType | ParameterDeclaration dataType _ <- params]
|
expectedTypes = [dataType | ParameterDeclaration dataType _ <- params]
|
||||||
argTypes = map getTypeFromExpr args'
|
argTypes = map getTypeFromExpr args'
|
||||||
@ -240,7 +234,6 @@ typeCheckStatementExpression (MethodCall expr methodName args) symtab classes =
|
|||||||
Nothing -> error $ "Class for object type '" ++ objType ++ "' not found."
|
Nothing -> error $ "Class for object type '" ++ objType ++ "' not found."
|
||||||
_ -> error "Invalid object type for method call. Object must have a class type."
|
_ -> error "Invalid object type for method call. Object must have a class type."
|
||||||
|
|
||||||
|
|
||||||
-- ********************************** Type Checking: Statements **********************************
|
-- ********************************** Type Checking: Statements **********************************
|
||||||
|
|
||||||
typeCheckStatement :: Statement -> [(Identifier, DataType)] -> [Class] -> Statement
|
typeCheckStatement :: Statement -> [(Identifier, DataType)] -> [Class] -> Statement
|
||||||
|
Loading…
Reference in New Issue
Block a user