From 950a05bb51c231a690aff725068703946231a6a7 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 14:06:09 -0400 Subject: Add array type checking --- pc.y | 1 + sem_check.c | 15 +++++++++++---- 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; -- cgit v1.1