From 227b13eccaecc05d76ca8dceb922a9d280ead0c4 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 29 Aug 2019 11:53:06 -0400 Subject: Fix return value for correctly typed statements Change okay return value to 1 so 0 can be used to denote an unset return type --- sem_check.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index 3d42996..c5e9fb6 100644 --- a/sem_check.c +++ b/sem_check.c @@ -107,7 +107,7 @@ ptree *t; yyerror("Incomplete parse tree\n"); if (t->l->ret_type == t->r->ret_type) - return 0; + return 1; else { snprintf(buf, 100, "Mismached types: " "Cannot assign type %s " @@ -142,13 +142,13 @@ ptree *t; if (t->l->ret_type != BOOL) yyerror("If condition must be of type BOOL\n"); - return 0; + return 1; 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); + snprintf(buf, 100, "Unknown tree node: %d...\n", t->type); yyerror(buf); } -- cgit v1.1 From 6f3310ddead0a00c6b8c1d2085fd0e83a1ada827 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 29 Aug 2019 11:54:46 -0400 Subject: Fix consolidate yyerror calls to after switch Any failed type checking now sets buf string to error message and breaks out of switch case and will then call yyerror with buf string. --- sem_check.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index c5e9fb6..88f41ad 100644 --- a/sem_check.c +++ b/sem_check.c @@ -65,20 +65,19 @@ ptree *t; "Cannot use boolean " "operator on type %s\n", pretty_type(type)); - yyerror(buf); + break; } } if (t->r->ret_type == t->l->ret_type) return t->r->ret_type; - else { + 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)) @@ -91,7 +90,6 @@ ptree *t; "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->l && t->l->ret_type == BOOL) @@ -108,15 +106,14 @@ ptree *t; if (t->l->ret_type == t->r->ret_type) return 1; - else { + 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; @@ -128,7 +125,7 @@ ptree *t; snprintf(buf, 100, "Cannot access array" "with type %s\n", pretty_type(t->r->ret_type)); - yyerror(buf); + break; } type = t->l->attr.nval -> var_type; @@ -149,9 +146,9 @@ ptree *t; default: return -200; snprintf(buf, 100, "Unknown tree node: %d...\n", t->type); - yyerror(buf); } + yyerror(buf); return -1; } -- cgit v1.1 From 389c2765afebb0b6112493e53117405488a4f3f1 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 29 Aug 2019 11:57:55 -0400 Subject: Add for loop type checking --- sem_check.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index 88f41ad..773c70a 100644 --- a/sem_check.c +++ b/sem_check.c @@ -141,7 +141,27 @@ ptree *t; yyerror("If condition must be of type BOOL\n"); return 1; case FOR: - /*TODO add for type checking after parsing is correct*/ + /* + FOR (0) + / \ + TD(0) STATEMENT(0) + / \ + ASSIGNOP(0) INUM(INT) + */ + if (!(t->r && t->l)) + yyerror("Missing nodes\n"); + if (t->l->ret_type == 1 && t->r->ret_type == 1) + return 1; + snprintf(buf, 100, "Incorrect types in for statement...\n"); + break; + case TO: + case DT: + if (!(t->r && t->l)) + yyerror("Missing nodes\n"); + + if (t->l->ret_type == 1 && t->r->ret_type == INT) + return 1; + snprintf(buf, 100, "Incorrect types HERE...\n"); break; default: return -200; -- cgit v1.1 From 4fced0fc39d3aeacb3c6d434aeeb622468a857cc Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Sep 2019 13:34:06 -0400 Subject: Add unary minus to parsing --- sem_check.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index 773c70a..aa83155 100644 --- a/sem_check.c +++ b/sem_check.c @@ -54,7 +54,7 @@ ptree *t; if (!(t->r && t->l)) yyerror("Missing nodes\n"); - if (t->attr.opval == ADD || t->attr.opval == OR) { + if (t->attr.opval == AND || t->attr.opval == OR) { if(t->l->ret_type == BOOL && t->r->ret_type ==BOOL) return BOOL; else { @@ -162,6 +162,8 @@ ptree *t; if (t->l->ret_type == 1 && t->r->ret_type == INT) return 1; snprintf(buf, 100, "Incorrect types HERE...\n"); + case SUB: + return t->l->ret_type; break; default: return -200; -- cgit v1.1 From 948b0460c9d079e5eff4e6247d35aa946956e4a5 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 8 Sep 2019 21:18:44 -0400 Subject: Add checking types on function call --- sem_check.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index aa83155..f6e412a 100644 --- a/sem_check.c +++ b/sem_check.c @@ -2,6 +2,8 @@ #include #include +#include +#include #include "y.tab.h" #include "pc.h" @@ -113,9 +115,6 @@ ptree *t; pretty_type(t->r->ret_type), t->l->attr.nval->name, pretty_type(t->l->attr.nval->var_type)); - - - break; case ARRAY_ACCESS: if (!(t->r && t->l && t->l->attr.nval)) @@ -165,6 +164,10 @@ ptree *t; case SUB: return t->l->ret_type; break; + case PCALL: + case FCALL: + if (t->l && t->l->attr.nval) + return t->l->attr.nval->var_type; default: return -200; snprintf(buf, 100, "Unknown tree node: %d...\n", t->type); @@ -175,3 +178,26 @@ ptree *t; } +void check_call(t) +ptree *t; +{ + int argc, *argv; + + print_tree(t); + if (!(t && t->attr.nval && t->attr.nval->func_info)) + yyerror("Tree is not a function call\n"); + + argc = t->l->attr.nval->func_info->argc; + if (t->l->attr.nval->func_info->argc != count_args(t->r)) + /*TODO add info about expected argument count*/ + yyerror("Incorrect argument count"); + + assert(argv = malloc(sizeof(int) * argc)); + + get_call_types(t->r, argv, argc); + + if (memcmp(argv, t->l->attr.nval->func_info->argv, argc * sizeof(int))) + /*TODO add info on which argument causes error*/ + yyerror("Incorrect types in fuction arguments"); + +} -- cgit v1.1 From 6130381c23c2ca4e050e83b7a20e3ba56d0fc283 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 8 Sep 2019 22:00:47 -0400 Subject: Adds type checking procedure arguments --- sem_check.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'sem_check.c') diff --git a/sem_check.c b/sem_check.c index f6e412a..af97507 100644 --- a/sem_check.c +++ b/sem_check.c @@ -183,10 +183,12 @@ ptree *t; { int argc, *argv; - print_tree(t); - if (!(t && t->attr.nval && t->attr.nval->func_info)) + if (!(t && (t->type == FCALL || t->type == PCALL))) yyerror("Tree is not a function call\n"); + if (!(t->l && t->l->attr.nval && t->l->attr.nval->func_info)) + yyerror("Incorrect Call Tree\n"); + argc = t->l->attr.nval->func_info->argc; if (t->l->attr.nval->func_info->argc != count_args(t->r)) /*TODO add info about expected argument count*/ -- cgit v1.1