From d8a7a9b0ed653ec8061896efc0c8e11b5df6b166 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 5 Sep 2019 10:50:02 -0400 Subject: Add counting of function arguments --- main.c | 28 ++++++++++++++++++++++++++++ pc.h | 3 +++ pc.y | 15 ++++++++++----- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 34acd52..d6cffdd 100644 --- a/main.c +++ b/main.c @@ -134,6 +134,34 @@ char* msg; return 0; } +int count_args(t) +ptree *t; +{ + int r, l; + + if (t->r && t->r->type == LIST) + r = count_args(t->r); + else if (t->r && t->r->type == ID) + r = 1; + else { + char buf[100]; + snprintf(buf, 100, "COUNT ARGS1: %d\n",(t->r)); + yyerror(buf); + } + + if (t->l && t->l->type == LIST) + l = count_args(t->l); + else if (t->l && t->l->type == ID) + l = 1; + else{ + char buf[100]; + snprintf(buf, 100, "COUNT ARGS2: %d\n", (t->l)); + yyerror(buf); + } + + return l + r; +} + int main() { #ifdef DEBUG_TYPES diff --git a/pc.h b/pc.h index e13c481..ea49612 100644 --- a/pc.h +++ b/pc.h @@ -2,6 +2,7 @@ #define PC_H #include "y.tab.h" +#include "tree.h" char* pretty_type(int); @@ -10,4 +11,6 @@ void debug_print(int, union YYSTYPE*); int yyerror(char*); #endif +int count_args(ptree*); + #define DEBUG diff --git a/pc.y b/pc.y index 1bb4a4f..3f3f7ad 100644 --- a/pc.y +++ b/pc.y @@ -173,17 +173,22 @@ sub_prog_head :FUNC ID arguments ':' standard_type ';' { node *tmp; - int i = -1; + int i = 0; check_id(cur_scope->prev, $2); tmp = scope_insert(cur_scope->prev, $2); - assert(tmp->func_info = malloc(sizeof struct fi)); - /*TODO add count of parameters*/ + if ($3->type == ID) + i = 1; + else + i = count_args($3); + + tmp->func_info = malloc(sizeof(struct fi)); + assert(tmp->func_info); tmp->func_info->argc = i; - assert(tmp->func_info->argv = malloc(i * sizeof int)); + assert(tmp->func_info->argv = malloc(i * sizeof(int))); - tmp->var_type = $5 + tmp->var_type = $5; cur_scope->ret_var = scope_insert(cur_scope, $2); cur_scope->ret_var->var_type = $5; -- cgit v1.1