Skip to content
Snippets Groups Projects
Commit 68f30300 authored by hm19's avatar hm19
Browse files

retorna procedimento, dmem e outros erros arrumados

parent 0429935b
No related branches found
No related tags found
No related merge requests found
......@@ -5,17 +5,19 @@ R01: ENPR 1
AMEM 1
DSVS R02
R02: NADA
CRVI 1,-5
ARMZ 1,0
CRCT 1
ARMI 1,-5
DMEM 1
RTPR 1,2
R00: NADA
AMEM 1
CREN 1,0
CRCT 8
ARMZ 0,0
CREN 0,0
CRCT 2
CRCT 3
SOMA
CHPR R01,0
ARMZ 0,0
DMEM 0
CHPR R01,2
DMEM 1
PARA
This diff is collapsed.
......@@ -78,11 +78,12 @@ bloco :
}
comando_composto {
retira(desloc,ts);
char buffer[50];
sprintf(buffer,"DMEM %d",desloc);
desloc = retira(desloc,ts);
geraCodigo(NULL,buffer);
desloc = 0;
if (nivel_lexico > 0)
nivel_lexico += 1;
}
;
......@@ -186,8 +187,18 @@ declaracao_procedimento: PROCEDURE IDENT {
parametros_ou_nada
PONTO_E_VIRGULA bloco
{
// Retorna ao nível léxico anterior e mantém o deslocamento atualizado
nivel_lexico -= 1;
char buffer[50];
desempilha(buffer,proc);
l_elem = busca(buffer,ts);
int n;
if(l_elem->categ == PR)
n = l_elem->info.pr.quant;
else if (l_elem->categ == FUN)
n = l_elem->info.pr.quant;
sprintf(buffer,"RTPR %d,%d",l_elem->nivel,n);
geraCodigo(NULL,buffer);
desloc = retira(n,ts);
mostra_ts(ts);
}
PONTO_E_VIRGULA
;
......@@ -235,6 +246,7 @@ parametros_ou_nada: ABRE_PARENTESES
char proc_ident[50];
desempilha(proc_ident,proc);
adiciona_param(proc_ident,num_param,ts);
empilha(proc_ident,proc);
mostra_ts(ts);
}
FECHA_PARENTESES
......@@ -329,9 +341,9 @@ atribuicao: {
chamada_procedimento:
{
if (l_elem == NULL || (l_elem->categ != PR && l_elem->categ != FUN)) {
if (l_elem == NULL){
yyerror("Procedimento não declarado");
} else {
} else if (l_elem->categ == PR || l_elem->categ == FUN) {
// Gera código para chamar o procedimento
empilha(l_elem->ident, proc);
}
......@@ -343,6 +355,7 @@ chamada_procedimento:
lista_parametros
{
if (l_elem->categ == PR || l_elem->categ ==FUN) {
char buffer[50];
mostra_ts(ts);
desempilha(buffer, proc);
......@@ -351,13 +364,19 @@ chamada_procedimento:
// Verifica se o número de parâmetros reais bate com os formais
if (num_param != l_elem->info.pr.quant) {
char error_msg[100];
if(l_elem->categ == PR){
sprintf(error_msg, "Erro: número de parâmetros incorreto. Esperado: %d, recebido: %d.",
l_elem->info.pr.quant, num_param);
yyerror(error_msg);
} else if(l_elem->categ == FUN){
sprintf(error_msg, "Erro: número de parâmetros incorreto. Esperado: %d, recebido: %d.",
l_elem->info.fun.quant, num_param);
yyerror(error_msg);
}
}
// Gera o código de chamada do procedimento
if (l_elem->categ == PR || l_elem->categ ==FUN) {
sprintf(buffer, "CHPR %s,%d", l_elem->info.pr.rot, nivel_lexico);
geraCodigo(NULL, buffer);
}
......@@ -479,7 +498,7 @@ fator: IDENT {
char buffer[50];
desempilha(buffer, proc);
l_elem = busca(buffer, ts);
if (l_elem == NULL || l_elem->categ == PR) {
if (l_elem == NULL) {
yyerror("Variável não declarada");
} else {
if (val_ou_ref == REF) {
......
......@@ -61,14 +61,20 @@ simb_t *busca(char *ident, ts_t *ts){
return NULL;//nao tem
}
void retira(int n, ts_t *ts) {
if (ts == NULL || ts->topo == NULL || n <= 0) {
return; // Nenhum elemento a ser removido
simb_t *topo(ts_t *ts){
if(ts == NULL) return NULL;
return ts->topo;
}
int retira(int n, ts_t *ts) {
if (ts == NULL || ts->topo == NULL) {
return 0; // Nenhum elemento a ser removido
}
int i = 0; // Contador de elementos removidos
for (; i < n && ts->topo != NULL; i++) {
int i; // Contador de elementos removidos
simb_t *temp = ts->topo;
for (i = 0; i < n && ts->topo != NULL; i++) {
temp = ts->topo;
ts->topo = ts->topo->prox;
free(temp);
ts->tam--;
......@@ -77,6 +83,15 @@ void retira(int n, ts_t *ts) {
if (i < n) {
printf("Aviso: Tentativa de remover mais itens (%d) do que a pilha possui (%d). Removidos apenas %d itens.\n", n, i, i);
}
//retorna novo deslocamento
while (temp != NULL) {
if (temp->categ == VS) {
return temp->info.vs.desloc + 1;
}
temp = temp->prox;
}
return 0;
}
void atualiza_tipo(int n, int tipo, ts_t *ts){
......
......@@ -67,7 +67,9 @@ void insere_ts(char *ident, int categ, int nivel, info_t info, ts_t *ts);
simb_t *busca(char *ident, ts_t *ts); // Corrigido: retorna ponteiro para simb_t
void retira(int n, ts_t *ts);
simb_t *topo(ts_t *ts);
int retira(int n, ts_t *ts);
void atualiza_tipo(int n, int tipo, ts_t *ts);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment