Skip to content

Commit

Permalink
Correção de bug relativo a funções anônimas. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelsanchesdasilva authored Jun 19, 2022
1 parent dfb0814 commit 9fb71ac
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 44 deletions.
31 changes: 23 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,28 @@
{
"type": "node",
"request": "launch",
"name": "Importação (Delégua)",
"name": "Classes > Construtor (Delégua)",
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"args": [
"${workspaceFolder}\\index.ts",
"--depurador",
"./testes/exemplos/importacao/importacao-1.egua"
"./testes/exemplos/classes/construtor-classe.egua"
],
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"]
},
{
"type": "node",
"request": "launch",
"name": "Classes > Construtor (Delégua)",
"name": "Classes > Construtor (Égua Clássico)",
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"args": [
"${workspaceFolder}\\index.ts",
"--dialeto",
"egua",
"./testes/exemplos/classes/construtor-classe.egua"
],
"runtimeExecutable": "node",
Expand All @@ -220,15 +221,29 @@
{
"type": "node",
"request": "launch",
"name": "Classes > Construtor (Égua Clássico)",
"name": "Funções Anônimas (Delégua)",
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"args": [
"${workspaceFolder}\\index.ts",
"--dialeto",
"egua",
"./testes/exemplos/classes/construtor-classe.egua"
"--depurador",
"./testes/exemplos/funcoes/funcoes-anonimas.delegua"
],
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"]
},
{
"type": "node",
"request": "launch",
"name": "Importação (Delégua)",
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"args": [
"${workspaceFolder}\\index.ts",
"--depurador",
"./testes/exemplos/importacao/importacao-1.egua"
],
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"]
Expand Down
47 changes: 11 additions & 36 deletions fontes/avaliador-sintatico/avaliador-sintatico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
return new Super(this.hashArquivo, simboloChave, metodo);
}

if (
this.verificarSeSimboloAtualEIgualA(
tiposDeSimbolos.COLCHETE_ESQUERDO
)
) {
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.COLCHETE_ESQUERDO)) {
const valores = [];

if (
Expand All @@ -181,11 +177,7 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
return new Vetor(this.hashArquivo, Number(simboloAtual.linha), []);
}

while (
!this.verificarSeSimboloAtualEIgualA(
tiposDeSimbolos.COLCHETE_DIREITO
)
) {
while (!this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.COLCHETE_DIREITO)) {
const valor = this.atribuir();
valores.push(valor);
if (
Expand All @@ -202,25 +194,15 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
return new Vetor(this.hashArquivo, Number(simboloAtual.linha), valores);
}

if (
this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.CHAVE_ESQUERDA)
) {
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.CHAVE_ESQUERDA)) {
const chaves = [];
const valores = [];

if (
this.verificarSeSimboloAtualEIgualA(
tiposDeSimbolos.CHAVE_DIREITA
)
) {
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.CHAVE_DIREITA)) {
return new Dicionario(this.hashArquivo, Number(simboloAtual.linha), [], []);
}

while (
!this.verificarSeSimboloAtualEIgualA(
tiposDeSimbolos.CHAVE_DIREITA
)
) {
while (!this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.CHAVE_DIREITA)) {
let chave = this.atribuir();
this.consumir(
tiposDeSimbolos.DOIS_PONTOS,
Expand All @@ -231,9 +213,7 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
chaves.push(chave);
valores.push(valor);

if (
this.simboloAtual().tipo !== tiposDeSimbolos.CHAVE_DIREITA
) {
if (this.simboloAtual().tipo !== tiposDeSimbolos.CHAVE_DIREITA) {
this.consumir(
tiposDeSimbolos.VIRGULA,
'Esperado vírgula antes da próxima expressão.'
Expand All @@ -244,6 +224,8 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
return new Dicionario(this.hashArquivo, Number(simboloAtual.linha), chaves, valores);
}

if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.FUNÇÃO, tiposDeSimbolos.FUNCAO))
return this.corpoDaFuncao(this.simboloAnterior().lexema);
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.FALSO))
return new Literal(this.hashArquivo, Number(simboloAtual.linha), false);
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.VERDADEIRO))
Expand All @@ -253,8 +235,7 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.ISTO))
return new Isto(this.hashArquivo, Number(simboloAtual.linha), this.simboloAnterior());

if (
this.verificarSeSimboloAtualEIgualA(
if (this.verificarSeSimboloAtualEIgualA(
tiposDeSimbolos.NUMERO,
tiposDeSimbolos.TEXTO
)
Expand All @@ -263,17 +244,11 @@ export class AvaliadorSintatico implements AvaliadorSintaticoInterface {
return new Literal(this.hashArquivo, Number(simboloAnterior.linha), simboloAnterior.literal);
}

if (
this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.IDENTIFICADOR)
) {
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.IDENTIFICADOR)) {
return new Variavel(this.hashArquivo, this.simboloAnterior());
}

if (
this.verificarSeSimboloAtualEIgualA(
tiposDeSimbolos.PARENTESE_ESQUERDO
)
) {
if (this.verificarSeSimboloAtualEIgualA(tiposDeSimbolos.PARENTESE_ESQUERDO)) {
let expressao = this.expressao();
this.consumir(
tiposDeSimbolos.PARENTESE_DIREITO,
Expand Down
19 changes: 19 additions & 0 deletions testes/avaliador-sintatico.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,24 @@ describe('Avaliador sintático', () => {
expect(retornoAvaliadorSintatico).toBeTruthy();
expect(retornoAvaliadorSintatico.declaracoes).toHaveLength(0);
});

describe('Funções Anônimas', () => {
it('Função anônima com mais de 255 parâmetros', () => {
let acumulador = "";
for (let i = 1; i <= 256; i++) {
acumulador += "a" + i + ", "
}

acumulador = acumulador.substring(0, acumulador.length - 2);

const funcaoCom256Argumentos = "var f = funcao(" + acumulador + ") {}"
const retornoLexador = delegua.lexador.mapear([funcaoCom256Argumentos], -1);
const retornoAvaliadorSintatico = delegua.avaliadorSintatico.analisar(retornoLexador);

expect(retornoAvaliadorSintatico).toBeTruthy();
expect(retornoAvaliadorSintatico.erros).toHaveLength(1);
expect(retornoAvaliadorSintatico.erros[0].message).toBe('Não pode haver mais de 255 parâmetros');
})
});
});
});
4 changes: 4 additions & 0 deletions testes/exemplos/funcoes/funcoes-anonimas.delegua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var funcaoParaMapear = função(a) {
retorna a * 2;
};
escreva(mapear([5, 3], funcaoParaMapear));

0 comments on commit 9fb71ac

Please sign in to comment.