Skip to content

Commit

Permalink
Pequenos ajustes de ordenação e refatoração (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelrvg authored Feb 21, 2024
1 parent f9dbbd0 commit b37e088
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
2 changes: 1 addition & 1 deletion fontes/avaliador-sintatico/avaliador-sintatico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface<SimboloIn

if (
[
tiposDeSimbolos.CHAVE_ESQUERDA,
tiposDeSimbolos.COLCHETE_ESQUERDO,
tiposDeSimbolos.FALSO,
tiposDeSimbolos.IDENTIFICADOR,
Expand All @@ -819,7 +820,6 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface<SimboloIn
tiposDeSimbolos.SUPER,
tiposDeSimbolos.TEXTO,
tiposDeSimbolos.VERDADEIRO,
tiposDeSimbolos.CHAVE_ESQUERDA
].includes(this.simbolos[this.atual].tipo)
) {
valor = this.expressao();
Expand Down
5 changes: 3 additions & 2 deletions fontes/interpretador/interpretador-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ export class InterpretadorBase implements InterpretadorInterface {
return Math.pow(valorEsquerdo, valorDireito);

case tiposDeSimbolos.MAIOR:
if ((tipoEsquerdo === tipoDeDadosDelegua.NUMERO) || (tipoEsquerdo === tipoDeDadosDelegua.NÚMERO) && (tipoDireito === tipoDeDadosDelegua.NUMERO) || (tipoDireito === tipoDeDadosDelegua.NÚMERO)) {

if (this.tiposNumericos.includes(tipoEsquerdo) && this.tiposNumericos.includes(tipoDireito)) {
return Number(valorEsquerdo) > Number(valorDireito);
}

Expand All @@ -541,7 +542,7 @@ export class InterpretadorBase implements InterpretadorInterface {
return Number(valorEsquerdo) >= Number(valorDireito);

case tiposDeSimbolos.MENOR:
if ((tipoEsquerdo === tipoDeDadosDelegua.NUMERO) || (tipoEsquerdo === tipoDeDadosDelegua.NÚMERO) && (tipoDireito === tipoDeDadosDelegua.NUMERO) || (tipoDireito === tipoDeDadosDelegua.NÚMERO)) {
if (this.tiposNumericos.includes(tipoEsquerdo) && this.tiposNumericos.includes(tipoDireito)) {
return Number(valorEsquerdo) < Number(valorDireito);
}

Expand Down
15 changes: 0 additions & 15 deletions testes/analisador-semantico.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,6 @@ describe('Analisador semântico', () => {
expect(retornoAnalisadorSemantico.diagnosticos[0].mensagem).toBe('A função não pode ter nenhum tipo de retorno.');
});

it('Função retorna Dicionario literal', async () => {
const retornoLexador = lexador.mapear(
[
'funcao executar() {',
' retorna { "chave": 100 }',
'}',
'escreva(executar())',
],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});

it('Não retornando o tipo que a função definiu - texto', () => {
const retornoLexador = lexador.mapear([
"funcao executar(valor1, valor2): texto {",
Expand Down
90 changes: 53 additions & 37 deletions testes/avaliador-sintatico.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,59 @@ describe('Avaliador sintático', () => {
})
});

describe('Funções', () => {
it('Função retorna Dicionario literal', async () => {
const retornoLexador = lexador.mapear(
[
'funcao executar() {',
' retorna { "chave": 100 }',
'}',
'escreva(executar())',
],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});

it('Função retorna vazio mas tem retorno de valores', async () => {
const retornoLexador = lexador.mapear(
[
'funcao executar(valor1, valor2): vazio {',
' var resultado = valor1 + valor2',
' retorna resultado',
'}',
],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});

it('Retorno texto sem retorno dentro da função', async () => {
const retornoLexador = lexador.mapear(
['funcao executar(valor1, valor2): texto {', ' var resultado = valor1 + valor2', '}'],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});

it('Função com retorno de vetor', () => {
const retornoLexador = lexador.mapear(
['funcao executar(): texto[] {', ' retorna ["1", "2"]', '}'],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico).toBeTruthy();
expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});
})

describe('Declarações de tuplas', () => {
it('Dupla', () => {
const retornoLexador = lexador.mapear(['var t = [(1, 2)]'], -1);
Expand Down Expand Up @@ -365,43 +418,6 @@ describe('Avaliador sintático', () => {
);
});

describe('Funções Anônimas', () => {
it('Função retorna vazio mas tem retorno de valores', async () => {
const retornoLexador = lexador.mapear(
[
'funcao executar(valor1, valor2): vazio {',
' var resultado = valor1 + valor2',
' retorna resultado',
'}',
],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});

it('Retorno texto sem retorno dentro da função', async () => {
const retornoLexador = lexador.mapear(
['funcao executar(valor1, valor2): texto {', ' var resultado = valor1 + valor2', '}'],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico.erros.length).toBeGreaterThanOrEqual(0);
});

it('Função com retorno de vetor', () => {
const retornoLexador = lexador.mapear(
['funcao executar(): texto[] {', ' retorna ["1", "2"]', '}'],
-1
);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);

expect(retornoAvaliadorSintatico).toBeTruthy();
});
});

it('Declaração `tente`', () => {
const retornoLexador = lexador.mapear(['tente { i = i + 1 } pegue (erro) { escreva(erro) }'], -1);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);
Expand Down

0 comments on commit b37e088

Please sign in to comment.