null is now a valid declaration value for a object

This commit is contained in:
2 changed files with 14 additions and 5 deletions

View File

@ -107,6 +107,9 @@ exampleNullDeclaration = LocalVariableDeclaration (VariableDeclaration "Person"
exampleNullDeclarationFail :: Statement exampleNullDeclarationFail :: Statement
exampleNullDeclarationFail = LocalVariableDeclaration (VariableDeclaration "int" "a" (Just NullLiteral)) 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" [
@ -246,3 +249,9 @@ runTypeCheck = do
printResult "Result Null Declaration:" evaluatedNullDeclarationFail printResult "Result Null Declaration:" evaluatedNullDeclarationFail
) handleError ) 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

View File

@ -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' then if type'' == type' || (type' == "null" && isObjectType 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'