diff --git a/ts/ts.c b/ts/ts.c index 7a2277391dab6b5bf7a4f4f058803923acee9b1d..d231a90f8f8bc9f4efbb7d64bd436b577f950631 100644 --- a/ts/ts.c +++ b/ts/ts.c @@ -32,6 +32,16 @@ void insere_ts(char *ident, int categ, int nivel, info_t info, ts_t *ts){ new->info.pf.tipo = info.pf.tipo; new->info.pf.desloc = info.pf.desloc; new->info.pf.passagem = info.pf.passagem; + } else if (categ == FUN) { + strcpy(new->info.fun.rot, info.fun.rot); + new->info.fun.quant = info.fun.quant; + new->info.fun.param = (int **)malloc(info.fun.quant * sizeof(int *)); + for (int i = 0; i < info.fun.quant; i++) { + new->info.fun.param[i] = (int *)malloc(2 * sizeof(int)); + new->info.fun.param[i][0] = info.fun.param[i][0]; + new->info.fun.param[i][1] = info.fun.param[i][1]; + } + new->info.fun.tipo = info.fun.tipo; } new->prox = ts->topo; @@ -73,13 +83,20 @@ void atualiza_tipo(int n, int tipo, ts_t *ts){ if(ts == NULL || ts->topo == NULL || n <= 0) return; simb_t *p = ts->topo; - for (int i = 0; i < n; ++i){ - if (p->categ == VS) + int count = 0; + while (p != NULL && count < n) { + if (p->categ == VS && p->info.vs.tipo == NSEI) { p->info.vs.tipo = tipo; - else if (p->categ == PF) + count++; + } else if (p->categ == PF && p->info.pf.tipo == NSEI) { p->info.pf.tipo = tipo; - p = p->prox; - } + count++; + } else if (p->categ == FUN && p->info.fun.tipo == NSEI) { + p->info.fun.tipo = tipo; + count++; + } + p = p->prox; + } } void adiciona_param(char *ident, int n, ts_t *ts){ @@ -87,22 +104,40 @@ void adiciona_param(char *ident, int n, ts_t *ts){ // Add n parameters to ident procedure params simb_t *p = busca(ident, ts); - if (p == NULL || p->categ != PR) return; + if (p == NULL || (p->categ != PR && p->categ != FUN)) return; + + if (p->categ == PR) { + if(p->info.pr.quant != 0) return; - if(p->info.pr.quant != 0) return; + p->info.pr.param = (int **)malloc(n * sizeof(int *)); + p->info.pr.quant = n; + + simb_t *pf = ts->topo; + for (int i = n-1, j = 0; i >= 0 && pf != NULL; i--, j++) { + if (pf->categ == PF) { + p->info.pr.param[i] = (int *)malloc(2 * sizeof(int)); + p->info.pr.param[i][0] = pf->info.pf.tipo; + p->info.pr.param[i][1] = pf->info.pf.passagem; + pf->info.pf.desloc = -4 - j; + } + pf = pf->prox; + } + } else if (p->categ == FUN) { + if(p->info.fun.quant != 0) return; - p->info.pr.param = (int **)malloc(n * sizeof(int *)); - p->info.pr.quant = n; + p->info.fun.param = (int **)malloc(n * sizeof(int *)); + p->info.fun.quant = n; - simb_t *pf = ts->topo; - for (int i = n-1, j = 0; i >= 0 && pf != NULL; i--, j++) { - if (pf->categ == PF) { - p->info.pr.param[i] = (int *)malloc(2 * sizeof(int)); - p->info.pr.param[i][0] = pf->info.pf.tipo; - p->info.pr.param[i][1] = pf->info.pf.passagem; - pf->info.pf.desloc = -4 - j; + simb_t *pf = ts->topo; + for (int i = n-1, j = 0; i >= 0 && pf != NULL; i--, j++) { + if (pf->categ == PF) { + p->info.fun.param[i] = (int *)malloc(2 * sizeof(int)); + p->info.fun.param[i][0] = pf->info.pf.tipo; + p->info.fun.param[i][1] = pf->info.pf.passagem; + pf->info.pf.desloc = -4 - j; + } + pf = pf->prox; } - pf = pf->prox; } } @@ -146,7 +181,7 @@ void mostra_ts(ts_t *ts) { categStr = "PR"; tipoStr = "-"; if (p->info.pr.quant > 1) { - char paramStr[150] = ""; // Smaller buffer for parameters + char paramStr[150] = ""; snprintf(paramStr, sizeof(paramStr), "%d{", p->info.pr.quant); for (int i = 0; i < p->info.pr.quant; i++) { const char *paramTipo = @@ -186,6 +221,43 @@ void mostra_ts(ts_t *ts) { snprintf(detalhes, sizeof(detalhes), "Passagem: %s", p->info.pf.passagem == VLR ? "Valor" : "ReferĂȘncia"); break; } + case FUN: { + categStr = "FUN"; + switch (p->info.fun.tipo) { + case INTEIRO: + tipoStr = "Inteiro"; + break; + case BOOLEAN: + tipoStr = "Boolean"; + break; + default: + tipoStr = "NSEI"; + break; + } + if (p->info.fun.quant > 1) { + char paramStr[150] = ""; + snprintf(paramStr, sizeof(paramStr), "%d{", p->info.fun.quant); + for (int i = 0; i < p->info.fun.quant; i++) { + const char *paramTipo = + (p->info.fun.param[i][0] == INTEIRO) ? "Int" : + (p->info.fun.param[i][0] == BOOLEAN) ? "Bool" : "NSEI"; + const char *paramPassagem = + (p->info.fun.param[i][1] == VLR) ? "VLR" : "REF"; + + char singleParam[30]; + snprintf(singleParam, sizeof(singleParam), "[%s,%s]", paramTipo, paramPassagem); + strncat(paramStr, singleParam, sizeof(paramStr) - strlen(paramStr) - 1); + if (i < p->info.fun.quant - 1) { + strncat(paramStr, ",", sizeof(paramStr) - strlen(paramStr) - 1); + } + } + strncat(paramStr, "}", sizeof(paramStr) - strlen(paramStr) - 1); + snprintf(detalhes, sizeof(detalhes), "%s, Params: %s", p->info.fun.rot, paramStr); + } else { + snprintf(detalhes, sizeof(detalhes), "%s, Params: %d", p->info.fun.rot, p->info.fun.quant); + } + break; + } default: categStr = "Outro"; } diff --git a/ts/ts.h b/ts/ts.h index 1a5fc6a4b8c08de13824a9ead9f7fe88d7d22778..79d00a7d9413277c804df880fcaca41a0c17b464 100644 --- a/ts/ts.h +++ b/ts/ts.h @@ -18,10 +18,18 @@ typedef struct pf_t { int passagem; } pf_t; +typedef struct fun_t { + char rot[4]; + int quant; + int **param; + int tipo; +} fun_t; + typedef union info_t { vs_t vs; pr_t pr; pf_t pf; + fun_t fun; } info_t; typedef struct simb_t { @@ -46,6 +54,7 @@ typedef struct ts_t { #define VS 0 #define PR 1 #define PF 2 +#define FUN 3 // enum passagem #define VLR 0