Add initial typechecker for AST #2

Merged
mrab merged 121 commits from typedAST into master 2024-06-14 07:53:30 +00:00
Showing only changes of commit c690b01396 - Show all commits

View File

@ -78,22 +78,6 @@ typeCheckExpression (BinaryOperation op expr1 expr2) symtab classes =
Or -> checkLogicalOperation op expr1' expr2' type1 type2 Or -> checkLogicalOperation op expr1' expr2' type1 type2
NameResolution -> resolveNameResolution expr1' expr2 symtab classes NameResolution -> resolveNameResolution expr1' expr2 symtab classes
typeCheckExpression (UnaryOperation op expr) symtab classes =
let expr' = typeCheckExpression expr symtab classes
type' = getTypeFromExpr expr'
in case op of
Not ->
if type' == "boolean"
then
TypedExpression "boolean" (UnaryOperation op expr')
else
error "Logical NOT operation requires an operand of type boolean"
Minus ->
if type' == "int" || type' == "char"
then
TypedExpression type' (UnaryOperation op expr')
else
error "Unary minus operation requires an operand of type int or char"
typeCheckExpression (StatementExpressionExpression stmtExpr) symtab classes = typeCheckExpression (StatementExpressionExpression stmtExpr) symtab classes =
let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes let stmtExpr' = typeCheckStatementExpression stmtExpr symtab classes
@ -163,41 +147,61 @@ 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."
typeCheckStatementExpression (PostIncrement expr) symtab classes = typeCheckStatementExpression (UnaryOperation op expr) symtab classes =
let expr' = typeCheckExpression expr symtab classes let expr' = typeCheckExpression expr symtab classes
type' = getTypeFromExpr expr' type' = getTypeFromExpr expr'
in if type' == "int" || type' == "char" in case op of
then Not ->
TypedStatementExpression type' (PostIncrement expr') if type' == "boolean"
else then
error "Post-increment operation requires an operand of type int or char" TypedStatementExpression "boolean" (UnaryOperation op expr')
else
typeCheckStatementExpression (PostDecrement expr) symtab classes = error "Logical NOT operation requires an operand of type boolean"
let expr' = typeCheckExpression expr symtab classes Minus ->
type' = getTypeFromExpr expr' if type' == "int"
in if type' == "int" || type' == "char" then
then TypedStatementExpression "int" (UnaryOperation op expr')
TypedStatementExpression type' (PostDecrement expr') else if type' == "char"
else then
error "Post-decrement operation requires an operand of type int or char" TypedStatementExpression "char" (UnaryOperation op expr')
else
typeCheckStatementExpression (PreIncrement expr) symtab classes = error "Unary minus operation requires an operand of type int or char"
let expr' = typeCheckExpression expr symtab classes PostIncrement ->
type' = getTypeFromExpr expr' if type' == "int"
in if type' == "int" || type' == "char" then
then TypedStatementExpression "int" (UnaryOperation op expr')
TypedStatementExpression type' (PreIncrement expr') else if type' == "char"
else then
error "Pre-increment operation requires an operand of type int or char" TypedStatementExpression "char" (UnaryOperation op expr')
else
typeCheckStatementExpression (PreDecrement expr) symtab classes = error "Post-increment operation requires an operand of type int or char"
let expr' = typeCheckExpression expr symtab classes PostDecrement ->
type' = getTypeFromExpr expr' if type' == "int"
in if type' == "int" || type' == "char" then
then TypedStatementExpression "int" (UnaryOperation op expr')
TypedStatementExpression type' (PreDecrement expr') else if type' == "char"
else then
error "Pre-decrement operation requires an operand of type int or char" TypedStatementExpression "char" (UnaryOperation op expr')
else
error "Post-decrement operation requires an operand of type int or char"
PreIncrement ->
if type' == "int"
then
TypedStatementExpression "int" (UnaryOperation op expr')
else if type' == "char"
then
TypedStatementExpression "char" (UnaryOperation op expr')
else
error "Pre-increment operation requires an operand of type int or char"
PreDecrement ->
if type' == "int"
then
TypedStatementExpression "int" (UnaryOperation op expr')
else if type' == "char"
then
TypedStatementExpression "char" (UnaryOperation op expr')
else
error "Pre-decrement operation requires an operand of type int or char"
-- ********************************** Type Checking: Statements ********************************** -- ********************************** Type Checking: Statements **********************************