Skip to content
Snippets Groups Projects
Commit 0429935b authored by viniciusmioto's avatar viniciusmioto
Browse files

declaracao de funcoes (inicio)

parent 14486fde
No related branches found
No related tags found
No related merge requests found
......@@ -2,3 +2,20 @@
AMEM 1
DSVS R00
R01: ENPR 1
AMEM 1
DSVS R02
R02: NADA
ARMZ 1,0
CRCT 1
ARMI 1,-5
DMEM 1
R00: NADA
AMEM 1
CREN 1,0
CRCT 2
CRCT 3
SOMA
CHPR R01,0
ARMZ 0,0
DMEM 0
PARA
This diff is collapsed.
......@@ -218,6 +218,9 @@ declaracao_funcao: FUNCTION IDENT {
mostra_ts(ts);
}
parametros_ou_nada
DOIS_PONTOS {
num_vars = 1;
} tipo
PONTO_E_VIRGULA bloco
{
// Retorna ao nível léxico anterior e mantém o deslocamento atualizado
......@@ -326,12 +329,14 @@ atribuicao: {
chamada_procedimento:
{
if (l_elem == NULL || l_elem->categ != PR) {
if (l_elem == NULL || (l_elem->categ != PR && l_elem->categ != FUN)) {
yyerror("Procedimento não declarado");
} else {
// Gera código para chamar o procedimento
empilha(l_elem->ident, proc);
}
if (l_elem->categ == FUN)
geraCodigo(NULL, "AMEM 1");
num_param = 0; // Reinicia a contagem de parâmetros reais
}
......@@ -352,9 +357,11 @@ chamada_procedimento:
}
// 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);
}
}
;
lista_parametros: ABRE_PARENTESES
......@@ -465,18 +472,30 @@ termo: termo ASTERISCO fator {
fator: IDENT {
// Carrega variável
l_elem = busca(token, ts);
if (l_elem == NULL) {
empilha(token, proc);
}
chamada_procedimento
{
char buffer[50];
desempilha(buffer, proc);
l_elem = busca(buffer, ts);
if (l_elem == NULL || l_elem->categ == PR) {
yyerror("Variável não declarada");
} else {
char buffer[50];
if (val_ou_ref == REF) {
if (l_elem->categ == VS)
sprintf(buffer, "CREN %d,%d", l_elem->nivel, l_elem->info.vs.desloc);
else if (l_elem->categ == FUN)
sprintf(buffer, "CREN %d,%d", l_elem->nivel, l_elem->info.fun.desloc);
}
else if (l_elem->categ == PF && l_elem->info.pf.passagem == REF) {
sprintf(buffer, "CRVI %d,%d", l_elem->nivel, l_elem->info.vs.desloc);
sprintf(buffer, "CRVI %d,%d", l_elem->nivel, l_elem->info.pf.desloc);
}
else {
if (l_elem->categ == VS)
sprintf(buffer, "CRVL %d,%d", l_elem->nivel, l_elem->info.vs.desloc);
else if (l_elem->categ == FUN)
sprintf(buffer, "CRVL %d,%d", l_elem->nivel, l_elem->info.fun.desloc);
}
geraCodigo(NULL, buffer);
}
......
......@@ -23,6 +23,7 @@ typedef struct fun_t {
int quant;
int **param;
int tipo;
int desloc;
} fun_t;
typedef union info_t {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment