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

Analisador semantico #29

Merged
merged 11 commits into from
Jul 7, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Declaracao,
Escreva,
EscrevaMesmaLinha
} from '@designliquido/delegua/declaracoes';

import { AnalisadorSemanticoBase } from '@designliquido/delegua/analisador-semantico/analisador-semantico-base';
import { SimboloInterface } from '@designliquido/delegua/interfaces';
import { DiagnosticoAnalisadorSemantico, DiagnosticoSeveridade } from '@designliquido/delegua/interfaces/erros';
import { FuncaoHipoteticaInterface } from '@designliquido/delegua/interfaces/funcao-hipotetica-interface';
import { RetornoAnalisadorSemantico } from '@designliquido/delegua/interfaces/retornos/retorno-analisador-semantico';
import { VariavelHipoteticaInterface } from '@designliquido/delegua/interfaces/variavel-hipotetica-interface';

import { PilhaVariaveis } from './pilha-variaveis';
export class AnalisadorSemanticoPortugolStudio extends AnalisadorSemanticoBase {
pilhaVariaveis: PilhaVariaveis;
variaveis: { [nomeVariavel: string]: VariavelHipoteticaInterface };
funcoes: { [nomeFuncao: string]: FuncaoHipoteticaInterface };
atual: number;
diagnosticos: DiagnosticoAnalisadorSemantico[];

constructor() {
super();
this.pilhaVariaveis = new PilhaVariaveis();
this.variaveis = {};
this.funcoes = {};
this.atual = 0;
this.diagnosticos = [];
}

adicionarDiagnostico(
simbolo: SimboloInterface,
mensagem: string,
severidade: DiagnosticoSeveridade = DiagnosticoSeveridade.ERRO
): void {
this.diagnosticos.push({
simbolo: simbolo,
mensagem: mensagem,
hashArquivo: simbolo.hashArquivo,
linha: simbolo.linha,
severidade: severidade,
});
}

visitarDeclaracaoEscrevaMesmaLinha(declaracao: EscrevaMesmaLinha): Promise<any>{
console.log(declaracao)
leonelsanchesdasilva marked this conversation as resolved.
Show resolved Hide resolved

return Promise.resolve()
}
visitarDeclaracaoEscreva(declaracao: Escreva): Promise<any>{
console.log(declaracao)
Aristidescosta marked this conversation as resolved.
Show resolved Hide resolved

return Promise.resolve()
}

analisar(declaracoes: Declaracao[]): RetornoAnalisadorSemantico {
this.variaveis = {};
this.atual = 0;
this.diagnosticos = [];
while (this.atual < declaracoes.length) {
console.log(declaracoes[this.atual])
leonelsanchesdasilva marked this conversation as resolved.
Show resolved Hide resolved
declaracoes[this.atual].aceitar(this);
this.atual++;
}

return {
diagnosticos: this.diagnosticos,
} as RetornoAnalisadorSemantico;
}

}
1 change: 1 addition & 0 deletions fontes/analisador-semantico/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './analisador-semantico-portugol-studio';
28 changes: 28 additions & 0 deletions fontes/analisador-semantico/pilha-variaveis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { VariavelInterface } from '@designliquido/delegua/interfaces';
import { PilhaInterface } from '@designliquido/delegua/interfaces';

export class PilhaVariaveis implements PilhaInterface<{ [nomeVariavel: string]: VariavelInterface }> {
pilha: { [nomeVariavel: string]: VariavelInterface }[];

constructor() {
this.pilha = [];
}

empilhar(item: { [nomeVariavel: string]: VariavelInterface }): void {
this.pilha.push(item);
}

eVazio(): boolean {
return this.pilha.length === 0;
}

topoDaPilha(): { [nomeVariavel: string]: VariavelInterface } {
if (this.eVazio()) throw new Error('Pilha vazia.');
return this.pilha[this.pilha.length - 1];
}

removerUltimo(): { [nomeVariavel: string]: VariavelInterface } {
if (this.eVazio()) throw new Error('Pilha vazia.');
return this.pilha.pop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -581,19 +581,19 @@ export class AvaliadorSintaticoPortugolStudio extends AvaliadorSintaticoBase {
} while (simboloComentario.tipo === tiposDeSimbolos.LINHA_COMENTARIO);

return new Comentario(
simboloComentario.hashArquivo,
simboloComentario.linha,
conteudos,
simboloComentario.hashArquivo,
simboloComentario.linha,
conteudos,
true
);
}

declaracaoComentarioUmaLinha(): Comentario {
const simboloComentario = this.avancarEDevolverAnterior();
return new Comentario(
simboloComentario.hashArquivo,
simboloComentario.linha,
simboloComentario.literal,
simboloComentario.hashArquivo,
simboloComentario.linha,
simboloComentario.literal,
false
);
}
Expand Down
1 change: 1 addition & 0 deletions fontes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './avaliador-sintatico';
export * from './analisador-semantico';
export * from './interpretador';
export * from './lexador';
37 changes: 37 additions & 0 deletions testes/analisador-semantico.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AnalisadorSemanticoPortugolStudio } from '../fontes/analisador-semantico';
import { AvaliadorSintaticoPortugolStudio } from "../fontes/avaliador-sintatico";
import { LexadorPortugolStudio } from "../fontes/lexador";

describe('Analisador sêmantico', () => {
describe('analisar()', () => {
let lexador: LexadorPortugolStudio;
let avaliadorSintatico: AvaliadorSintaticoPortugolStudio;
let analisadorSemantico: AnalisadorSemanticoPortugolStudio;

beforeEach(() => {
lexador = new LexadorPortugolStudio();
avaliadorSintatico = new AvaliadorSintaticoPortugolStudio();
analisadorSemantico = new AnalisadorSemanticoPortugolStudio();
});

describe('Cenários de falha', () => {
it('Variável indefinida, não declarada (escreva)', () => {
const retornoLexador = lexador.mapear([
'programa',
'{',
' funcao inicio()',
' {',
' escreva(teste)',
' }',
'}'
], -1);
const retornoAvaliadorSintatico = avaliadorSintatico.analisar(retornoLexador, -1);
const retornoAnalisadorSemantico = analisadorSemantico.analisar(retornoAvaliadorSintatico.declaracoes);

expect(retornoAnalisadorSemantico).toBeTruthy();
//expect(retornoAnalisadorSemantico.diagnosticos).toHaveLength(1);
});
})

})
})
12 changes: 6 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@designliquido/delegua": ["node_modules/@designliquido/delegua/dist"],
"@designliquido/delegua/*": ["node_modules/@designliquido/delegua/dist/*"],
//"@designliquido/delegua": ["node_modules/@designliquido/delegua/dist"],
//"@designliquido/delegua/*": ["node_modules/@designliquido/delegua/dist/*"],
// Para linkagem (`yarn link ...`), comente as linhas acima e descomente as linhas abaixo.
// "@designliquido/delegua": ["node_modules/@designliquido/delegua/fontes"],
// "@designliquido/delegua/*": ["node_modules/@designliquido/delegua/fontes/*"],
leonelsanchesdasilva marked this conversation as resolved.
Show resolved Hide resolved
"@designliquido/delegua": ["node_modules/@designliquido/delegua/fontes"],
"@designliquido/delegua/*": ["node_modules/@designliquido/delegua/fontes/*"],
},
"outDir": "dist",
"module": "CommonJS",
Expand All @@ -19,8 +19,8 @@
"esModuleInterop": true
},
"exclude": [
"babel.config.js",
"coverage",
"babel.config.js",
"coverage",
"dist",
"docs",
"jest.config.ts",
Expand Down
Loading
Loading