Skip to content

Commit

Permalink
Declaracao retorne de Portugol Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelrvg committed Jan 27, 2024
1 parent 82c26e5 commit 07bab23
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
Var,
Bloco,
EscrevaMesmaLinha,
Retorna,
} from '../../declaracoes';
import { RetornoLexador, RetornoAvaliadorSintatico } from '../../interfaces/retornos';
import { AvaliadorSintaticoBase } from '../avaliador-sintatico-base';
Expand Down Expand Up @@ -563,6 +564,29 @@ export class AvaliadorSintaticoPortugolStudio extends AvaliadorSintaticoBase {
return inicializacoes;
}

declaracaoRetorne(): Retorna {
this.avancarEDevolverAnterior()
const simboloChave = this.simbolos[this.atual];
let valor = null;

if (
[
tiposDeSimbolos.CADEIA,
tiposDeSimbolos.CARACTER,
tiposDeSimbolos.FALSO,
tiposDeSimbolos.IDENTIFICADOR,
tiposDeSimbolos.INTEIRO,
tiposDeSimbolos.NEGACAO,
tiposDeSimbolos.REAL,
tiposDeSimbolos.VERDADEIRO,
].includes(this.simbolos[this.atual].tipo)
) {
valor = this.expressao();
}

return new Retorna(simboloChave, valor);
}

declaracaoPara(): Para {
try {
const simboloPara: SimboloInterface = this.avancarEDevolverAnterior();
Expand Down Expand Up @@ -687,6 +711,8 @@ export class AvaliadorSintaticoPortugolStudio extends AvaliadorSintaticoBase {
return null;
case tiposDeSimbolos.REAL:
return this.declaracaoReais();
case tiposDeSimbolos.RETORNE:
return this.declaracaoRetorne();
case tiposDeSimbolos.SE:
return this.declaracaoSe();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const palavrasReservadas = {
para: tiposDeSimbolos.PARA,
programa: tiposDeSimbolos.PROGRAMA,
real: tiposDeSimbolos.REAL,
retorne: tiposDeSimbolos.RETORNE,
se: tiposDeSimbolos.SE,
senao: tiposDeSimbolos.SENAO,
vazio: tiposDeSimbolos.VAZIO,
Expand Down
1 change: 1 addition & 0 deletions fontes/tipos-de-simbolos/portugol-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
PONTO: 'PONTO',
PONTO_E_VIRGULA: 'PONTO_E_VIRGULA',
PROGRAMA: 'PROGRAMA',
RETORNE: 'RETORNE',
REAL: 'REAL',
SUBTRACAO: 'SUBTRACAO',
VIRGULA: 'VIRGULA',
Expand Down
43 changes: 43 additions & 0 deletions testes/portugol-studio/interpretador.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,49 @@ describe('Interpretador (Portugol Studio)', () => {

expect(retornoInterpretador.erros).toHaveLength(0);
});

it('Retorne', async () => {
const respostas = [1];
interpretador.interfaceEntradaSaida = {
question: (mensagem: string, callback: Function) => {
callback(respostas.pop());
}
};

const retornoLexador = lexador.mapear([
'programa',
'{ ',
'funcao inicio()',
'{ ',
'inteiro numero',
'escreva("Quantos elementos da sequência de Fibonacci deseja calcular? ")',
'leia(numero)',
'para (inteiro i = 1; i <= numero ; i++)',
'{',
' escreva(fibonacci(i), " ")',
'}',
'escreva("\n")',
'}',
'funcao inteiro fibonacci(inteiro posicao)',
'{ ',
'se (posicao == 1)',
'{',
'retorne 0',
'}',
'senao se (posicao == 2)',
'{',
'retorne 1',
'}',
'retorne fibonacci(posicao - 1) + fibonacci(posicao - 2)',
'}',
'}',
], -1);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

const retornoInterpretador = await interpretador.interpretar(retornoAvaliadorSintatico.declaracoes);

expect(retornoInterpretador.erros).toHaveLength(0);
});
});
});
});

0 comments on commit 07bab23

Please sign in to comment.