Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pequenos ajustes de ordenação e refatoração #663

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading