commit 4e9078f8ce007efca3f0f6eed956089748b5c2c9
parent 99c1d0c53ab240683b7715c1e6065dbb6fa78296
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Mon, 10 Nov 2025 12:53:44 +0000
update the type checker
Diffstat:
| M | types.c | | | 24 | +++++++++++++++++------- |
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/types.c b/types.c
@@ -1,6 +1,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
+#include <stdbool.h>
#include "ast.h"
@@ -12,23 +13,30 @@ type_error(const char *expected, const char *got) {
exit(1);
}
-const char *
+bool
+same_type(struct ast_type *t1, struct ast_type *t2) {
+ if (t1->t != t2->t)
+ return false;
+ switch (t1->t) {
+ case BASIC_T:
+ return (strcmp(t1->type, t2->type) == 0);
+ case FUNCTION_T:
+ return same_type(t1->ret, t2);
+ }
+ return false;
+}
+
+struct ast_type *
typeof_expr(struct ast_expr *expr) {
switch (expr->op) {
case VALUE:
- return "long";
case ADD:
case SUB:
case DIV:
case MUL:
- exit(255);
-
case NESTED:
- return typeof_expr(&expr->children[0]);
-
case FUNCTION:
case VAR:
- exit(255);
default:
return NULL;
}
@@ -36,6 +44,8 @@ typeof_expr(struct ast_expr *expr) {
void
typecheck_dec(struct ast_dec *dec) {
+ if (same_type(dec->type, typeof_expr(dec->value)))
+ type_error("ah", "ah");
}
void