Compare commits
No commits in common. "53e1afc9e460a80b5fd73576e1bb48b0ef6a46bd" and "535a6891ad5c1772d52c7aef701f4b2cb1507d14" have entirely different histories.
53e1afc9e4
...
535a6891ad
@ -101,15 +101,6 @@ exampleNameResolutionAssignment = Block [
|
|||||||
exampleCharIntOperation :: Expression
|
exampleCharIntOperation :: Expression
|
||||||
exampleCharIntOperation = BinaryOperation Addition (CharacterLiteral 'a') (IntegerLiteral 1)
|
exampleCharIntOperation = BinaryOperation Addition (CharacterLiteral 'a') (IntegerLiteral 1)
|
||||||
|
|
||||||
exampleNullDeclaration :: Statement
|
|
||||||
exampleNullDeclaration = LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just NullLiteral))
|
|
||||||
|
|
||||||
exampleNullDeclarationFail :: Statement
|
|
||||||
exampleNullDeclarationFail = LocalVariableDeclaration (VariableDeclaration "int" "a" (Just NullLiteral))
|
|
||||||
|
|
||||||
exampleNullAssignment :: Statement
|
|
||||||
exampleNullAssignment = StatementExpressionStatement (Assignment (Reference "a") NullLiteral)
|
|
||||||
|
|
||||||
testClasses :: [Class]
|
testClasses :: [Class]
|
||||||
testClasses = [
|
testClasses = [
|
||||||
Class "Person" [
|
Class "Person" [
|
||||||
@ -235,23 +226,3 @@ runTypeCheck = do
|
|||||||
printResult "Result Char Int Operation:" evaluatedCharIntOperation
|
printResult "Result Char Int Operation:" evaluatedCharIntOperation
|
||||||
) handleError
|
) handleError
|
||||||
|
|
||||||
catch (do
|
|
||||||
print "====================================================================================="
|
|
||||||
evaluatedNullDeclaration <- evaluate (typeCheckStatement exampleNullDeclaration [] sampleClasses)
|
|
||||||
printSuccess "Type checking of null declaration completed successfully"
|
|
||||||
printResult "Result Null Declaration:" evaluatedNullDeclaration
|
|
||||||
) handleError
|
|
||||||
|
|
||||||
catch (do
|
|
||||||
print "====================================================================================="
|
|
||||||
evaluatedNullDeclarationFail <- evaluate (typeCheckStatement exampleNullDeclarationFail [] sampleClasses)
|
|
||||||
printSuccess "Type checking of null declaration failed"
|
|
||||||
printResult "Result Null Declaration:" evaluatedNullDeclarationFail
|
|
||||||
) handleError
|
|
||||||
|
|
||||||
catch (do
|
|
||||||
print "====================================================================================="
|
|
||||||
evaluatedNullAssignment <- evaluate (typeCheckStatement exampleNullAssignment [("a", "Person")] sampleClasses)
|
|
||||||
printSuccess "Type checking of null assignment completed successfully"
|
|
||||||
printResult "Result Null Assignment:" evaluatedNullAssignment
|
|
||||||
) handleError
|
|
||||||
|
@ -147,8 +147,8 @@ typeCheckStatementExpression (Assignment ref expr) symtab classes =
|
|||||||
type' = getTypeFromExpr expr'
|
type' = getTypeFromExpr expr'
|
||||||
type'' = getTypeFromExpr ref'
|
type'' = getTypeFromExpr ref'
|
||||||
in
|
in
|
||||||
if type'' == type' || (type' == "null" && isObjectType type'') then
|
if type'' == type' then
|
||||||
TypedStatementExpression type'' (Assignment ref' expr')
|
TypedStatementExpression type' (Assignment ref' expr')
|
||||||
else
|
else
|
||||||
error $ "Type mismatch in assignment to variable: expected " ++ type'' ++ ", found " ++ type'
|
error $ "Type mismatch in assignment to variable: expected " ++ type'' ++ ", found " ++ type'
|
||||||
|
|
||||||
@ -226,12 +226,8 @@ typeCheckStatement (LocalVariableDeclaration (VariableDeclaration dataType ident
|
|||||||
let checkedExpr = fmap (\expr -> typeCheckExpression expr symtab classes) maybeExpr
|
let checkedExpr = fmap (\expr -> typeCheckExpression expr symtab classes) maybeExpr
|
||||||
exprType = fmap getTypeFromExpr checkedExpr
|
exprType = fmap getTypeFromExpr checkedExpr
|
||||||
in case exprType of
|
in case exprType of
|
||||||
Just t
|
Just t | t /= dataType -> error $ "Type mismatch in declaration of '" ++ identifier ++ "': expected " ++ dataType ++ ", found " ++ t
|
||||||
| t == "null" && isObjectType dataType ->
|
_ -> TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr))
|
||||||
TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr))
|
|
||||||
| t /= dataType -> error $ "Type mismatch in declaration of '" ++ identifier ++ "': expected " ++ dataType ++ ", found " ++ t
|
|
||||||
| otherwise -> TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr))
|
|
||||||
Nothing -> TypedStatement dataType (LocalVariableDeclaration (VariableDeclaration dataType identifier checkedExpr))
|
|
||||||
|
|
||||||
typeCheckStatement (While cond stmt) symtab classes =
|
typeCheckStatement (While cond stmt) symtab classes =
|
||||||
let cond' = typeCheckExpression cond symtab classes
|
let cond' = typeCheckExpression cond symtab classes
|
||||||
@ -282,9 +278,6 @@ typeCheckStatement (StatementExpressionStatement stmtExpr) symtab classes =
|
|||||||
|
|
||||||
-- ********************************** Type Checking: Helpers **********************************
|
-- ********************************** Type Checking: Helpers **********************************
|
||||||
|
|
||||||
isObjectType :: DataType -> Bool
|
|
||||||
isObjectType dt = dt /= "int" && dt /= "boolean" && dt /= "char"
|
|
||||||
|
|
||||||
getTypeFromExpr :: Expression -> DataType
|
getTypeFromExpr :: Expression -> DataType
|
||||||
getTypeFromExpr (TypedExpression t _) = t
|
getTypeFromExpr (TypedExpression t _) = t
|
||||||
getTypeFromExpr _ = error "Untyped expression found where typed was expected"
|
getTypeFromExpr _ = error "Untyped expression found where typed was expected"
|
||||||
|
Loading…
Reference in New Issue
Block a user