From 06ebf6a87ca1db975bdbf4f7f3126ea7c26ddde6 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 11:34:48 -0400 Subject: Add basic type checking Squashed commit of WIP-type_check@5dadc4f5667ae69a709dd45c020780f2f424d67e --- sem_check.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'sem_check.c') 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; + +} + -- cgit v1.1 From 190274a66622d65cd30726a8daf3edb02dfe009e Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 16 Aug 2019 22:06:54 -0400 Subject: Fix MULOP/ADDOP return BOOL in check_ret_type Removes some debug printing --- sem_check.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index ae1fa4c..2e9b5ba 100644 --- a/sem_check.c +++ b/sem_check.c @@ -53,6 +53,14 @@ ptree *t; case ADDOP : case MULOP : + if (!(t->r && t->l)) + yyerror("Missing nodes\n"); + + if (t->r->ret_type == t->l->ret_type) + return t->r->ret_type; + else + yyerror("Misssing nodes\n"); + break; case RELOP : if (!(t->r && t->l)) yyerror("Missing nodes\n"); -- cgit v1.1 From 7449656c35052b29c65f0b7b53895b22b958a259 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 12:16:17 -0400 Subject: Fix error message for ADDOP/MULOP Removes some debugging prints --- sem_check.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index 2e9b5ba..da54a21 100644 --- a/sem_check.c +++ b/sem_check.c @@ -58,8 +58,14 @@ ptree *t; if (t->r->ret_type == t->l->ret_type) return t->r->ret_type; - else - yyerror("Misssing nodes\n"); + else { + snprintf(buf, 100, "Mismached types: " + "Type %s " + "cannot be used with type %s\n", + pretty_type(t->r->ret_type), + pretty_type(t->l->ret_type)); + yyerror(buf); + } break; case RELOP : if (!(t->r && t->l)) @@ -81,7 +87,6 @@ ptree *t; case RNUM: return REAL; case ASSIGNOP: - fprintf(stderr, "HERE\n"); if (!(t->r && t->l && t->r->attr.nval)) yyerror("Incomplete parse tree\n"); -- cgit v1.1 From 47016c39ed801aecfa900f30a49d94ae247b1156 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 13:04:08 -0400 Subject: Fix error messages for RELOP type checking Removes debug prints --- sem_check.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index da54a21..44eb82f 100644 --- a/sem_check.c +++ b/sem_check.c @@ -74,7 +74,12 @@ ptree *t; if (t->r->ret_type == t->l->ret_type) return BOOL; else - yyerror("Misssing nodes\n"); + snprintf(buf, 100, "Mismached types: " + "Type %s " + "cannot be compared to type %s\n", + pretty_type(t->r->ret_type), + pretty_type(t->l->ret_type)); + yyerror(buf); break; case NOT: if (t->ret_type == BOOL) @@ -82,7 +87,6 @@ ptree *t; yyerror("NOT needs bool input\n"); break; case INUM: - printf("INUM: %d", t->attr.ival); return INT; case RNUM: return REAL; -- cgit v1.1 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 --- sem_check.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'sem_check.c') 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 From 1ff4c71e7a5140cd045a9483a76d667bd6837744 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 14:15:21 -0400 Subject: Fix not type checking --- sem_check.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index a3d08b8..d174bf8 100644 --- a/sem_check.c +++ b/sem_check.c @@ -82,7 +82,7 @@ ptree *t; yyerror(buf); break; case NOT: - if (t->ret_type == BOOL) + if (t->l && t->l->ret_type == BOOL) return BOOL; yyerror("NOT needs bool input\n"); break; -- cgit v1.1 From e1d044ff79638cf781816e8723fa78f1e9a7b34e Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 15:47:08 -0400 Subject: Add Type checking if statement condition --- sem_check.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index d174bf8..7e6e338 100644 --- a/sem_check.c +++ b/sem_check.c @@ -123,13 +123,20 @@ ptree *t; if (type == ARRAY - INT || type == ARRAY - REAL) return ARRAY - type; break; + case IF: + if (!(t->r && t->l)) + yyerror("Incomplete parse tree\n"); + + if (t->l->ret_type != BOOL) + yyerror("If condition must be of type BOOL\n"); + return 0; + default: return -200; snprintf(buf, 101, "Unknown tree node: %d...\n", t->type); yyerror(buf); } - return -1; } -- cgit v1.1 From ee21031fb16f5ed89dd1dbaf73bc2a1201f955b1 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 17 Aug 2019 16:30:28 -0400 Subject: Fix deal with and/or ops correctly in type checking --- sem_check.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index 7e6e338..d18fb59 100644 --- a/sem_check.c +++ b/sem_check.c @@ -56,6 +56,21 @@ ptree *t; if (!(t->r && t->l)) yyerror("Missing nodes\n"); + if (t->attr.opval == ADD || t->attr.opval == OR) { + if(t->l->ret_type == BOOL && t->r->ret_type ==BOOL) + return BOOL; + else { + type = t->l->ret_type == BOOL ? + t->r->ret_type : t->l->ret_type; + + snprintf(buf, 100, "Mismached types:" + "Cannot use boolean " + "operator on type %s\n", + pretty_type(type)); + yyerror(buf); + } + } + if (t->r->ret_type == t->l->ret_type) return t->r->ret_type; else { @@ -70,7 +85,6 @@ ptree *t; case RELOP : if (!(t->r && t->l)) yyerror("Missing nodes\n"); - if (t->r->ret_type == t->l->ret_type) return BOOL; else -- cgit v1.1 From fe712311578b014a9e074f25856520732a1b3da8 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 18 Aug 2019 18:14:11 -0400 Subject: Add while (and holding place for for) type checking --- sem_check.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index d18fb59..c6f4c4e 100644 --- a/sem_check.c +++ b/sem_check.c @@ -138,13 +138,16 @@ ptree *t; return ARRAY - type; break; case IF: + case WHILE: if (!(t->r && t->l)) yyerror("Incomplete parse tree\n"); if (t->l->ret_type != BOOL) yyerror("If condition must be of type BOOL\n"); return 0; - + case FOR: + /*TODO add for type checking after parsing is correct*/ + break; default: return -200; snprintf(buf, 101, "Unknown tree node: %d...\n", t->type); -- cgit v1.1 From 3f1cee416602070225b81deb8c2222a5324f8204 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 18 Aug 2019 18:31:36 -0400 Subject: Fix clean up header includes --- sem_check.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index c6f4c4e..3d42996 100644 --- a/sem_check.c +++ b/sem_check.c @@ -1,12 +1,10 @@ +#include "sem_check.h" + #include #include -#include "node.h" -#include "scope.h" -#include "tree.h" #include "y.tab.h" #include "pc.h" -#include "sem_check.h" void check_id(s, n) scope *s; -- cgit v1.1