Skip to content
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
6 changes: 4 additions & 2 deletions fontes/construtos/coluna.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Simbolo } from "../lexador/simbolo";

export class Coluna {
public nomeColuna: string;
public tipo: 'INTEIRO' | 'LOGICO' | 'NUMERO' | 'TEXTO';
public tamanho: number;
public tamanho: number | Simbolo;
public nulo: boolean;
public chavePrimaria: boolean;
public chaveEstrangeira: boolean;

constructor(
nomeColuna: string,
tipo: 'INTEIRO' | 'LOGICO' | 'NUMERO' | 'TEXTO',
tamanho?: number,
tamanho?: number | Simbolo,
nulo?: boolean,
chavePrimaria?: boolean,
chaveEstrangeira?: boolean
Expand Down
32 changes: 31 additions & 1 deletion fontes/tradutor/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Coluna } from "../avaliador-sintatico";
import { Atualizar, Comando, Criar, Excluir, Inserir, Selecionar } from "../comandos";
import { Simbolo } from "../lexador/simbolo";

import tiposDeSimbolos from "../tipos-de-simbolos";

Expand Down Expand Up @@ -46,10 +48,38 @@ export class Tradutor {
return resultado;
}

traduzirColuna(coluna: Coluna) {
let traduzir = "";
if(coluna.chavePrimaria)
traduzir += "PRIMARY KEY "
if(tiposDeSimbolos.INTEIRO === coluna.tipo){
traduzir += `INTEGER `;
}
else if(tiposDeSimbolos.TEXTO === coluna.tipo){
const simbolo = coluna.tamanho as Simbolo
traduzir += `VARCHAR(${simbolo.literal}) `;
}
else if(tiposDeSimbolos.LOGICO === coluna.tipo)
traduzir += "BOOLEAN "
if(coluna.nulo)
traduzir += "NULL"
else
traduzir += "NOT NULL"

return traduzir;
}

traduzirComandoCriar(comandoCriar: Criar) {
let resultado = `CREATE TABLE ${comandoCriar.tabela} (`

for (const coluna of comandoCriar.colunas) {
resultado += `${coluna.nomeColuna} ${this.traduzirColuna(coluna)}, `;
}

resultado = resultado.slice(0, -2);
resultado += ")"

return '';
return resultado;
}

traduzirComandoExcluir(comandoExcluir: Excluir) {
Expand Down
31 changes: 18 additions & 13 deletions testes/tradutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,24 @@ describe('Tradutor', () => {
expect(resultado).toContain('WHERE');
});

// it('Criar', () => {
// const codigo = [
// 'CRIAR TABELA clientes(ID INTEIRO NAO NULO CHAVE PRIMARIA AUTO INCREMENTO, NOME TEXTO(100) NAO NULO, IDADE INTEIRO NAO NULO, EMAIL TEXTO(255) NAO NULO, ATIVO LOGICO NAO NULO);'
// ];
// const retornoLexador = lexador.mapear(codigo);
// const retornoAvaliadorSintatico =
// avaliadorSintatico.analisar(retornoLexador);
// const resultado = tradutor.traduzir(retornoAvaliadorSintatico.comandos);
// expect(resultado).toBeTruthy();
// expect(resultado).toContain('SELECT');
// expect(resultado).toContain('FROM');
// expect(resultado).toContain('WHERE');
// });
it('Criar', () => {
const codigo = [
'CRIAR TABELA clientes(ID INTEIRO NAO NULO CHAVE PRIMARIA AUTO INCREMENTO, NOME TEXTO(100) NAO NULO, IDADE INTEIRO NAO NULO, EMAIL TEXTO(255) NULO, ATIVO LOGICO NAO NULO);'
];
const retornoLexador = lexador.mapear(codigo);
const retornoAvaliadorSintatico =
avaliadorSintatico.analisar(retornoLexador);
const resultado = tradutor.traduzir(retornoAvaliadorSintatico.comandos);
expect(resultado).toBeTruthy();
expect(resultado).toContain('CREATE');
expect(resultado).toContain('TABLE');
expect(resultado).toContain('clientes');
expect(resultado).toContain('PRIMARY KEY');
expect(resultado).toContain('INTEGER');
expect(resultado).toContain('NOT NULL');
expect(resultado).toContain('BOOLEAN');
expect(resultado).toContain('VARCHAR');
});
});
});
});