aboutsummaryrefslogtreecommitdiff
path: root/sem_check.c
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-08-17 11:34:48 -0400
committerTucker Evans <tuckerevans24@gmail.com>2019-08-17 11:34:48 -0400
commit06ebf6a87ca1db975bdbf4f7f3126ea7c26ddde6 (patch)
tree03215f60ea0c776e60f38288470dac10546ad501 /sem_check.c
parentee6c598892518d956388bb008095de6d4d7fe123 (diff)
Add basic type checking
Squashed commit of WIP-type_check@5dadc4f5667ae69a709dd45c020780f2f424d67e
Diffstat (limited to 'sem_check.c')
-rw-r--r--sem_check.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/sem_check.c b/sem_check.c
index 637d044..ae1fa4c 100644
--- a/sem_check.c
+++ b/sem_check.c
@@ -34,3 +34,79 @@ char *n;
return tmp;
}
+
+int check_ret_type(t)
+ptree *t;
+{
+ char buf[100];
+ int type;
+
+ if (!t)
+ printf("TYPE: %d\n", t->type);
+
+ switch (t->type) {
+ case ID:
+ if (!(t->attr.nval))
+ yyerror("Missing ID\n");
+
+ return t->attr.nval->var_type;
+
+ case ADDOP :
+ case MULOP :
+ case RELOP :
+ if (!(t->r && t->l))
+ yyerror("Missing nodes\n");
+
+ if (t->r->ret_type == t->l->ret_type)
+ return BOOL;
+ else
+ yyerror("Misssing nodes\n");
+ break;
+ case NOT:
+ if (t->ret_type == BOOL)
+ return BOOL;
+ yyerror("NOT needs bool input\n");
+ break;
+ case INUM:
+ printf("INUM: %d", t->attr.ival);
+ return INT;
+ case RNUM:
+ return REAL;
+ case ASSIGNOP:
+ fprintf(stderr, "HERE\n");
+ if (!(t->r && t->l && t->r->attr.nval))
+ yyerror("Incomplete parse tree\n");
+
+ if (t->l->attr.nval->var_type == t->r->ret_type)
+ return 0;
+ else {
+ snprintf(buf, 100, "Mismached types: "
+ "Cannot assign type %s "
+ "to variable \"%s\" of type %s\n",
+ pretty_type(t->r->ret_type),
+ t->l->attr.nval->name,
+ pretty_type(t->l->attr.nval->var_type));
+ yyerror(buf);
+ }
+
+
+ break;
+ case ARRAY_ACCESS:
+ if (!(t->r && t->l && t->l->attr.nval))
+ yyerror("Incorrect Array Access\n");
+
+ type = t->l->attr.nval -> var_type;
+ if (type == ARRAY - INT || type == ARRAY - REAL)
+ return 0;
+ break;
+ default:
+ return -200;
+ snprintf(buf, 101, "Unknown tree node: %d...\n", t->type);
+ yyerror(buf);
+ }
+
+
+ return -1;
+
+}
+