aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-08-17 14:06:09 -0400
committerTucker Evans <tuckerevans24@gmail.com>2019-08-17 14:06:09 -0400
commit950a05bb51c231a690aff725068703946231a6a7 (patch)
treeadb858dc7ea5cea0ee665a1a4e97874ef0ca814f
parent47016c39ed801aecfa900f30a49d94ae247b1156 (diff)
Add array type checking
-rw-r--r--pc.y1
-rw-r--r--sem_check.c15
2 files changed, 12 insertions, 4 deletions
diff --git a/pc.y b/pc.y
index 97bf33e..264218c 100644
--- a/pc.y
+++ b/pc.y
@@ -277,6 +277,7 @@ var
tmp = scope_safe_search(cur_scope, $1);
$$ = mktree(ARRAY_ACCESS, mkid(tmp), $3);
+ $$->attr.nval = $$->l->attr.nval;
}
;
diff --git a/sem_check.c b/sem_check.c
index 44eb82f..a3d08b8 100644
--- a/sem_check.c
+++ b/sem_check.c
@@ -42,7 +42,7 @@ ptree *t;
int type;
if (!t)
- printf("TYPE: %d\n", t->type);
+ fprintf(stderr, "TYPE: %d\n", t->type);
switch (t->type) {
case ID:
@@ -91,10 +91,10 @@ ptree *t;
case RNUM:
return REAL;
case ASSIGNOP:
- if (!(t->r && t->l && t->r->attr.nval))
+ if (!(t->r && t->l))
yyerror("Incomplete parse tree\n");
- if (t->l->attr.nval->var_type == t->r->ret_type)
+ if (t->l->ret_type == t->r->ret_type)
return 0;
else {
snprintf(buf, 100, "Mismached types: "
@@ -112,9 +112,16 @@ ptree *t;
if (!(t->r && t->l && t->l->attr.nval))
yyerror("Incorrect Array Access\n");
+ if (t->r->ret_type != INT) {
+ snprintf(buf, 100, "Cannot access array"
+ "with type %s\n",
+ pretty_type(t->r->ret_type));
+ yyerror(buf);
+ }
+
type = t->l->attr.nval -> var_type;
if (type == ARRAY - INT || type == ARRAY - REAL)
- return 0;
+ return ARRAY - type;
break;
default:
return -200;