Add initial typechecker for AST #2
@ -89,7 +89,7 @@ exampleMethodCallAndAssignmentFail = Block [
|
||||
LocalVariableDeclaration (VariableDeclaration "Person" "bob" (Just (StatementExpressionExpression (ConstructorCall "Person" [IntegerLiteral 30])))),
|
||||
LocalVariableDeclaration (VariableDeclaration "int" "age" (Just (StatementExpressionExpression (MethodCall (Reference "bob") "getAge" [])))),
|
||||
StatementExpressionStatement (MethodCall (Reference "bob") "setAge" [IntegerLiteral 30]),
|
||||
StatementExpressionStatement (Assignment (Reference "age") (Reference "age"))
|
||||
StatementExpressionStatement (Assignment (Reference "a") (Reference "age"))
|
||||
]
|
||||
|
||||
exampleNameResolutionAssignment :: Statement
|
||||
|
@ -52,7 +52,7 @@ typeCheckExpression (Reference id) symtab classes =
|
||||
_ -> error $ "Ambiguous reference to field '" ++ id ++ "' in class '" ++ className ++ "'"
|
||||
Nothing -> error $ "Class '" ++ className ++ "' not found for 'this'"
|
||||
Nothing -> error $ "Context for 'this' not found in symbol table, unable to resolve '" ++ id ++ "'"
|
||||
|
||||
|
||||
typeCheckExpression (BinaryOperation op expr1 expr2) symtab classes =
|
||||
let expr1' = typeCheckExpression expr1 symtab classes
|
||||
expr2' = typeCheckExpression expr2 symtab classes
|
||||
@ -89,10 +89,10 @@ typeCheckExpression (UnaryOperation op expr) symtab classes =
|
||||
else
|
||||
error "Logical NOT operation requires an operand of type boolean"
|
||||
Minus ->
|
||||
if type' == "int"
|
||||
if type' == "int"
|
||||
then
|
||||
TypedExpression "int" (UnaryOperation op expr')
|
||||
else if type' == "char"
|
||||
else if type' == "char"
|
||||
then
|
||||
TypedExpression "char" (UnaryOperation op expr')
|
||||
else
|
||||
@ -147,11 +147,11 @@ typeCheckStatementExpression (Assignment ref expr) symtab classes =
|
||||
type' = getTypeFromExpr expr'
|
||||
type'' = getTypeFromExpr ref'
|
||||
in
|
||||
if type'' == type' then
|
||||
if type'' == type' then
|
||||
TypedStatementExpression type' (Assignment ref' expr')
|
||||
else
|
||||
error $ "Type mismatch in assignment to variable: expected " ++ type'' ++ ", found " ++ type'
|
||||
|
||||
|
||||
typeCheckStatementExpression (ConstructorCall className args) symtab classes =
|
||||
case find (\(Class name _ _) -> name == className) classes of
|
||||
Nothing -> error $ "Class '" ++ className ++ "' not found."
|
||||
@ -295,12 +295,6 @@ unifyReturnTypes dt1 dt2
|
||||
| dt1 == dt2 = dt1
|
||||
| otherwise = "Object"
|
||||
|
||||
lookupType :: Identifier -> [(Identifier, DataType)] -> Maybe DataType
|
||||
lookupType id symtab =
|
||||
case lookup id symtab of
|
||||
Just t -> Just t
|
||||
Nothing -> Nothing
|
||||
|
||||
resolveResultType :: DataType -> DataType -> DataType
|
||||
resolveResultType "char" "char" = "char"
|
||||
resolveResultType "int" "int" = "int"
|
||||
|
Loading…
Reference in New Issue
Block a user