@designliquido/delegua
Advanced tools
Comparing version 0.29.4 to 0.30.0
{ | ||
"name": "@designliquido/delegua", | ||
"version": "0.29.3", | ||
"version": "0.29.4", | ||
"description": "Linguagem de programação simples e moderna usando português estruturado, com suporte a múltiplos dialetos", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ import { Atribuir, ExpressaoRegular, FimPara, FormatacaoEscrita, Literal, Super, TipoDe } from '../../construtos'; | ||
import { PilhaVariaveis } from '../pilha-variaveis'; | ||
interface VariavelHipoteticaInterface { | ||
interface VariavelHipoteticaBirlInterface { | ||
tipo: 'texto' | 'número' | 'longo' | 'vetor' | 'dicionário' | 'nulo' | 'lógico' | 'função' | 'símbolo' | 'objeto' | 'módulo'; | ||
@@ -17,3 +17,3 @@ subtipo?: 'texto' | 'número' | 'longo' | 'lógico'; | ||
variaveis: { | ||
[nomeVariavel: string]: VariavelHipoteticaInterface; | ||
[nomeVariavel: string]: VariavelHipoteticaBirlInterface; | ||
}; | ||
@@ -20,0 +20,0 @@ atual: number; |
@@ -6,15 +6,7 @@ import { Atribuir, Chamada, ExpressaoRegular, FimPara, FormatacaoEscrita, Literal, Super, TipoDe } from '../../construtos'; | ||
import { DiagnosticoAnalisadorSemantico } from '../../interfaces/erros'; | ||
import { FuncaoHipoteticaInterface } from '../../interfaces/funcao-hipotetica-interface'; | ||
import { RetornoAnalisadorSemantico } from '../../interfaces/retornos/retorno-analisador-semantico'; | ||
import { TiposDadosInterface } from '../../interfaces/tipos-dados-interface'; | ||
import { VariavelHipoteticaInterface } from '../../interfaces/variavel-hipotetica-interface'; | ||
import { ContinuarQuebra, RetornoQuebra, SustarQuebra } from '../../quebras'; | ||
import { PilhaVariaveis } from './../pilha-variaveis'; | ||
interface VariavelHipoteticaInterface { | ||
tipo: TiposDadosInterface; | ||
subtipo?: 'texto' | 'número' | 'inteiro' | 'longo' | 'lógico'; | ||
imutavel: boolean; | ||
valor?: any; | ||
} | ||
interface FuncaoHipoteticaInterface { | ||
valor: any; | ||
} | ||
export declare class AnalisadorSemanticoMapler implements AnalisadorSemanticoInterface { | ||
@@ -83,2 +75,1 @@ pilhaVariaveis: PilhaVariaveis; | ||
} | ||
export {}; |
export * from './analisador-semantico-birl'; | ||
export * from './analisador-semantico-mapler'; | ||
export * from './analisador-semantico-visualg'; |
@@ -19,2 +19,3 @@ "use strict"; | ||
__exportStar(require("./analisador-semantico-mapler"), exports); | ||
__exportStar(require("./analisador-semantico-visualg"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -139,3 +139,3 @@ "use strict"; | ||
declaracaoEnquanto() { | ||
this.consumir(birl_1.default.NEGATIVA, 'Esperado expressão `NEGATIVA` para iniciar o bloco `ENQUANTO`.'); | ||
const simboloNegatica = this.consumir(birl_1.default.NEGATIVA, 'Esperado expressão `NEGATIVA` para iniciar o bloco `ENQUANTO`.'); | ||
this.consumir(birl_1.default.BAMBAM, 'Esperado expressão `BAMBAM` após `NEGATIVA` para iniciar o bloco `ENQUANTO`.'); | ||
@@ -150,3 +150,3 @@ this.consumir(birl_1.default.PARENTESE_ESQUERDO, 'Esperado expressão `(` após `BAMBAM` para iniciar o bloco `ENQUANTO`.'); | ||
this.consumir(birl_1.default.BIRL, 'Esperado expressão `BIRL` para fechar o bloco `ENQUANTO`.'); | ||
return new declaracoes_1.Enquanto(codicao, declaracoes); | ||
return new declaracoes_1.Enquanto(codicao, new declaracoes_1.Bloco(this.hashArquivo, simboloNegatica.linha, declaracoes)); | ||
} | ||
@@ -153,0 +153,0 @@ declaracaoExpressao() { |
@@ -65,3 +65,3 @@ "use strict"; | ||
const corpo = new construtos_1.FuncaoConstruto(simboloPrimario.hashArquivo, simboloPrimario.linha, parametros, [ | ||
this.expressao(), | ||
new declaracoes_1.Expressao(this.expressao()), | ||
]); | ||
@@ -68,0 +68,0 @@ return new declaracoes_1.FuncaoDeclaracao(simboloPrimario, corpo, tipoRetorno); |
@@ -6,2 +6,3 @@ import { RetornoLexador, RetornoAvaliadorSintatico } from '../../../interfaces/retornos'; | ||
import { ParametroInterface, SimboloInterface } from '../../../interfaces'; | ||
import { TiposDadosInterface } from '../../../interfaces/tipos-dados-interface'; | ||
export declare class AvaliadorSintaticoVisuAlg extends AvaliadorSintaticoBase { | ||
@@ -40,2 +41,4 @@ blocoPrincipalIniciado: boolean; | ||
chamar(): Construto; | ||
simboloAtual(): SimboloInterface; | ||
verificarDefinicaoTipoAtual(): TiposDadosInterface; | ||
corpoDaFuncao(tipo: any): FuncaoConstruto; | ||
@@ -42,0 +45,0 @@ declaracaoEnquanto(): Enquanto; |
@@ -133,13 +133,14 @@ "use strict"; | ||
for (let identificador of dadosVariaveis.identificadores) { | ||
const tipo = dadosVariaveis.tipo; | ||
switch (dadosVariaveis.tipo) { | ||
case visualg_1.default.CARACTER: | ||
case visualg_1.default.CARACTERE: | ||
inicializacoes.push(new declaracoes_1.Var(identificador, new construtos_1.Literal(this.hashArquivo, Number(dadosVariaveis.simbolo.linha), ''))); | ||
inicializacoes.push(new declaracoes_1.Var(identificador, new construtos_1.Literal(this.hashArquivo, Number(dadosVariaveis.simbolo.linha), ''), tipo)); | ||
break; | ||
case visualg_1.default.INTEIRO: | ||
case visualg_1.default.REAL: | ||
inicializacoes.push(new declaracoes_1.Var(identificador, new construtos_1.Literal(this.hashArquivo, Number(dadosVariaveis.simbolo.linha), 0))); | ||
inicializacoes.push(new declaracoes_1.Var(identificador, new construtos_1.Literal(this.hashArquivo, Number(dadosVariaveis.simbolo.linha), 0), tipo)); | ||
break; | ||
case visualg_1.default.LOGICO: | ||
inicializacoes.push(new declaracoes_1.Var(identificador, new construtos_1.Literal(this.hashArquivo, Number(dadosVariaveis.simbolo.linha), false))); | ||
inicializacoes.push(new declaracoes_1.Var(identificador, new construtos_1.Literal(this.hashArquivo, Number(dadosVariaveis.simbolo.linha), false), tipo)); | ||
break; | ||
@@ -277,2 +278,21 @@ } | ||
} | ||
simboloAtual() { | ||
return this.simbolos[this.atual - 2]; | ||
} | ||
verificarDefinicaoTipoAtual() { | ||
const tipos = ['inteiro', 'qualquer', 'real', 'texto', 'vazio', 'vetor', 'caracter']; | ||
const lexema = this.simboloAtual().lexema.toLowerCase(); | ||
const contemTipo = tipos.find((tipo) => tipo === lexema); | ||
if (contemTipo && this.verificarTipoProximoSimbolo(visualg_1.default.COLCHETE_ESQUERDO)) { | ||
const tiposVetores = ['inteiro[]', 'qualquer[]', 'real[]', 'texto[]', 'caracter[]']; | ||
this.avancarEDevolverAnterior(); | ||
if (!this.verificarTipoProximoSimbolo(visualg_1.default.COLCHETE_DIREITO)) { | ||
throw this.erro(this.simbolos[this.atual - 1], "Esperado símbolo de fechamento do vetor ']'."); | ||
} | ||
const contemTipoVetor = tiposVetores.find((tipo) => tipo === `${lexema}[]`); | ||
this.avancarEDevolverAnterior(); | ||
return contemTipoVetor; | ||
} | ||
return contemTipo; | ||
} | ||
corpoDaFuncao(tipo) { | ||
@@ -284,2 +304,3 @@ const simboloAnterior = this.simbolos[this.atual - 1]; | ||
// Tipo retornado pela função. | ||
let tipoRetorno = null; | ||
if (!this.verificarSeSimboloAtualEIgualA(visualg_1.default.INTEIRO, visualg_1.default.CARACTER, visualg_1.default.CARACTERE, visualg_1.default.REAL, visualg_1.default.LOGICO)) { | ||
@@ -289,6 +310,7 @@ throw this.erro(this.simbolos[this.atual], 'Esperado um tipo válido para retorno de função'); | ||
this.consumir(visualg_1.default.QUEBRA_LINHA, "Esperado quebra de linha após tipo retornado por 'funcao'."); | ||
tipoRetorno = this.verificarDefinicaoTipoAtual(); | ||
const inicializacoes = this.validarSegmentoVar(); | ||
this.validarSegmentoInicio('função'); | ||
const corpo = inicializacoes.concat(this.blocoEscopo()); | ||
return new construtos_1.FuncaoConstruto(this.hashArquivo, Number(simboloAnterior.linha), parametros, corpo.filter((d) => d)); | ||
return new construtos_1.FuncaoConstruto(this.hashArquivo, Number(simboloAnterior.linha), parametros, corpo.filter((d) => d), tipoRetorno); | ||
} | ||
@@ -524,2 +546,7 @@ declaracaoEnquanto() { | ||
const dadosParametros = this.logicaComumParametroVisuAlg(); | ||
const tipoDadoParametro = { | ||
nome: dadosParametros.simbolo.lexema, | ||
tipo: dadosParametros.tipo, | ||
tipoInvalido: !dadosParametros.tipo ? this.simboloAtual().lexema : null | ||
}; | ||
for (let parametro of dadosParametros.identificadores) { | ||
@@ -530,2 +557,3 @@ parametros.push({ | ||
referencia: dadosParametros.referencia, | ||
tipoDado: tipoDadoParametro, | ||
}); | ||
@@ -532,0 +560,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { VisitanteComumInterface } from '../interfaces'; | ||
import { SimboloInterface, VisitanteComumInterface } from '../interfaces'; | ||
import { Construto } from './construto'; | ||
@@ -6,7 +6,7 @@ export declare class DefinirValor implements Construto { | ||
hashArquivo: number; | ||
objeto: any; | ||
nome: any; | ||
objeto: Construto; | ||
nome: SimboloInterface; | ||
valor: any; | ||
constructor(hashArquivo: number, linha: number, objeto: any, nome: any, valor: any); | ||
constructor(hashArquivo: number, linha: number, objeto: Construto, nome: SimboloInterface, valor: any); | ||
aceitar(visitante: VisitanteComumInterface): Promise<any>; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { Declaracao } from '../declaracoes'; | ||
import { VisitanteComumInterface, ParametroInterface } from '../interfaces'; | ||
@@ -8,5 +9,5 @@ import { Construto } from './construto'; | ||
tipoRetorno?: string; | ||
corpo: any[]; | ||
constructor(hashArquivo: number, linha: number, parametros: ParametroInterface[], corpo: any[], tipoRetorno?: string); | ||
corpo: Declaracao[]; | ||
constructor(hashArquivo: number, linha: number, parametros: ParametroInterface[], corpo: Declaracao[], tipoRetorno?: string); | ||
aceitar(visitante: VisitanteComumInterface): Promise<any>; | ||
} |
import { Construto } from '../construtos'; | ||
import { VisitanteComumInterface } from '../interfaces'; | ||
import { Bloco } from './bloco'; | ||
import { Declaracao } from './declaracao'; | ||
export declare class Enquanto extends Declaracao { | ||
condicao: Construto; | ||
corpo: any; | ||
constructor(condicao: Construto, corpo: any); | ||
corpo: Bloco; | ||
constructor(condicao: Construto, corpo: Bloco); | ||
aceitar(visitante: VisitanteComumInterface): Promise<any>; | ||
} |
@@ -0,8 +1,10 @@ | ||
import { Construto } from '../construtos'; | ||
import { VisitanteComumInterface } from '../interfaces'; | ||
import { Bloco } from './bloco'; | ||
import { Declaracao } from './declaracao'; | ||
export declare class Fazer extends Declaracao { | ||
caminhoFazer: any; | ||
condicaoEnquanto: any; | ||
caminhoFazer: Bloco; | ||
condicaoEnquanto: Construto; | ||
constructor(hashArquivo: number, linha: number, caminhoFazer: any, condicaoEnquanto: any); | ||
aceitar(visitante: VisitanteComumInterface): Promise<any>; | ||
} |
@@ -10,4 +10,4 @@ import { Construto } from '../construtos'; | ||
export declare class Para extends Declaracao { | ||
inicializador: any; | ||
condicao: any; | ||
inicializador?: Declaracao | Declaracao[]; | ||
condicao: Construto; | ||
incrementar: Construto; | ||
@@ -14,0 +14,0 @@ corpo: Bloco; |
@@ -8,8 +8,8 @@ import { FuncaoConstruto } from '../construtos'; | ||
export declare class Tente extends Declaracao { | ||
caminhoTente: any[]; | ||
caminhoTente: Declaracao[]; | ||
caminhoPegue: FuncaoConstruto | Declaracao[]; | ||
caminhoSenao: any[]; | ||
caminhoFinalmente: any[]; | ||
caminhoFinalmente: Declaracao[]; | ||
constructor(hashArquivo: number, linha: number, caminhoTente: any[], caminhoPegue: FuncaoConstruto | Declaracao[], caminhoSenao: any[], caminhoFinalmente: any[]); | ||
aceitar(visitante: VisitanteComumInterface): Promise<any>; | ||
} |
@@ -1,2 +0,5 @@ | ||
import { SimboloInterface } from '../interfaces'; | ||
import { AcessoIndiceVariavel, AcessoMetodoOuPropriedade, Agrupamento, AtribuicaoPorIndice, Atribuir, Binario, Chamada, Construto, DefinirValor, ExpressaoRegular, FimPara, FormatacaoEscrita, FuncaoConstruto, Isto, Literal, Super, TipoDe, Unario, Variavel, Vetor } from '../construtos'; | ||
import { Classe, Const, ConstMultiplo, Expressao, FuncaoDeclaracao, Enquanto, Escolha, Escreva, Fazer, Importar, Para, ParaCada, Se, Tente, Var, VarMultiplo, Bloco, Continua, EscrevaMesmaLinha, Leia, LeiaMultiplo, Retorna, Sustar, Declaracao, Falhar } from '../declaracoes'; | ||
import { VisitanteComumInterface } from '../interfaces'; | ||
import { SustarQuebra } from '../quebras'; | ||
/** | ||
@@ -6,14 +9,66 @@ * O formatador de código Delégua. | ||
*/ | ||
export declare class FormatadorDelegua { | ||
indentacao: number; | ||
blocoAberto: boolean; | ||
constructor(); | ||
export declare class FormatadorDelegua implements VisitanteComumInterface { | ||
indentacaoAtual: number; | ||
quebraLinha: string; | ||
tamanhoIndentacao: number; | ||
codigoFormatado: string; | ||
devePularLinha: boolean; | ||
deveIndentar: boolean; | ||
constructor(quebraLinha: string, tamanhoIndentacao?: number); | ||
visitarDeclaracaoClasse(declaracao: Classe): void; | ||
visitarDeclaracaoConst(declaracao: Const): any; | ||
visitarDeclaracaoConstMultiplo(declaracao: ConstMultiplo): any; | ||
visitarDeclaracaoDeAtribuicao(expressao: Atribuir): void; | ||
visitarDeclaracaoDeExpressao(declaracao: Expressao): void; | ||
visitarDeclaracaoDefinicaoFuncao(declaracao: FuncaoDeclaracao): void; | ||
visitarDeclaracaoEnquanto(declaracao: Enquanto): void; | ||
visitarDeclaracaoEscolha(declaracao: Escolha): void; | ||
visitarDeclaracaoEscreva(declaracao: Escreva): void; | ||
visitarDeclaracaoFazer(declaracao: Fazer): void; | ||
visitarDeclaracaoImportar(declaracao: Importar): void; | ||
visitarDeclaracaoPara(declaracao: Para): any; | ||
visitarDeclaracaoParaCada(declaracao: ParaCada): Promise<any>; | ||
visitarDeclaracaoSe(declaracao: Se): void; | ||
visitarDeclaracaoTente(declaracao: Tente): void; | ||
visitarDeclaracaoVar(declaracao: Var): any; | ||
visitarDeclaracaoVarMultiplo(declaracao: VarMultiplo): any; | ||
visitarExpressaoAcessoIndiceVariavel(expressao: AcessoIndiceVariavel): void; | ||
visitarExpressaoAcessoMetodo(expressao: AcessoMetodoOuPropriedade): void; | ||
visitarExpressaoAgrupamento(expressao: Agrupamento): any; | ||
visitarExpressaoAtribuicaoPorIndice(expressao: AtribuicaoPorIndice): any; | ||
visitarExpressaoAtribuicaoPorIndicesMatriz(expressao: any): Promise<any>; | ||
visitarExpressaoAcessoElementoMatriz(expressao: any): void; | ||
visitarExpressaoBinaria(expressao: Binario): void; | ||
private formatarBlocoOuVetorDeclaracoes; | ||
visitarExpressaoBloco(declaracao: Bloco): any; | ||
visitarExpressaoContinua(declaracao?: Continua): any; | ||
visitarExpressaoDeChamada(expressao: Chamada): void; | ||
visitarExpressaoDefinirValor(expressao: DefinirValor): void; | ||
visitarExpressaoDeleguaFuncao(expressao: any): void; | ||
visitarExpressaoDeVariavel(expressao: Variavel): void; | ||
visitarExpressaoDicionario(expressao: any): void; | ||
visitarExpressaoExpressaoRegular(expressao: ExpressaoRegular): any; | ||
visitarDeclaracaoEscrevaMesmaLinha(declaracao: EscrevaMesmaLinha): void; | ||
visitarExpressaoFalhar(expressao: Falhar): any; | ||
visitarExpressaoFimPara(declaracao: FimPara): void; | ||
visitarExpressaoFormatacaoEscrita(declaracao: FormatacaoEscrita): void; | ||
visitarExpressaoFuncaoConstruto(expressao: FuncaoConstruto): void; | ||
visitarExpressaoIsto(expressao: Isto): void; | ||
visitarExpressaoLeia(expressao: Leia): Promise<any>; | ||
visitarExpressaoLeiaMultiplo(expressao: LeiaMultiplo): Promise<any>; | ||
visitarExpressaoLiteral(expressao: Literal): any; | ||
visitarExpressaoLogica(expressao: any): void; | ||
visitarExpressaoRetornar(declaracao: Retorna): any; | ||
visitarExpressaoSuper(expressao: Super): void; | ||
visitarExpressaoSustar(declaracao?: Sustar): SustarQuebra; | ||
visitarExpressaoTipoDe(expressao: TipoDe): Promise<any>; | ||
visitarExpressaoUnaria(expressao: Unario): void; | ||
visitarExpressaoVetor(expressao: Vetor): void; | ||
formatarDeclaracaoOuConstruto(declaracaoOuConstruto: Declaracao | Construto): void; | ||
/** | ||
* Devolve código formatado de acordo com os símbolos encontrados. | ||
* @param simbolos Um vetor de símbolos. | ||
* @param quebraLinha O símbolo de quebra de linha. Normalmente `\r\n` para Windows e `\n` para outros sistemas operacionais. | ||
* @param tamanhoIndentacao O tamanho de cada bloco de indentação (por padrão, 4) | ||
* @param {Declaracao[]} declaracoes Um vetor de declarações. | ||
* @returns Código Delégua formatado. | ||
*/ | ||
formatar(simbolos: SimboloInterface[], quebraLinha: string, tamanhoIndentacao?: number): string; | ||
formatar(declaracoes: Declaracao[]): string; | ||
} |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.FormatadorDelegua = void 0; | ||
const construtos_1 = require("../construtos"); | ||
const delegua_1 = __importDefault(require("../tipos-de-simbolos/delegua")); | ||
@@ -14,69 +15,579 @@ /** | ||
class FormatadorDelegua { | ||
constructor() { | ||
this.indentacao = 0; | ||
this.blocoAberto = true; | ||
constructor(quebraLinha, tamanhoIndentacao = 4) { | ||
this.quebraLinha = quebraLinha; | ||
this.tamanhoIndentacao = tamanhoIndentacao; | ||
this.indentacaoAtual = 0; | ||
this.codigoFormatado = ''; | ||
this.devePularLinha = true; | ||
this.deveIndentar = true; | ||
} | ||
visitarDeclaracaoClasse(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}classe ${declaracao.simbolo.lexema} {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let propriedade of declaracao.propriedades) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}${propriedade.nome.lexema}: ${propriedade.tipo || 'qualquer'}${this.quebraLinha}`; | ||
} | ||
this.codigoFormatado += `${this.quebraLinha}`; | ||
for (let metodo of declaracao.metodos) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}${metodo.simbolo.lexema}`; | ||
this.visitarExpressaoFuncaoConstruto(metodo.funcao); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoConst(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}constante ${declaracao.simbolo.lexema}`; | ||
if (declaracao.inicializador) { | ||
this.codigoFormatado += ` = `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.inicializador); | ||
} | ||
if (this.devePularLinha) { | ||
this.codigoFormatado += this.quebraLinha; | ||
} | ||
} | ||
visitarDeclaracaoConstMultiplo(declaracao) { | ||
// TODO: Até então o código nunca passa por aqui, porque o avaliador sintático | ||
// converte `ConstMultiplo` em várias declarações `const`. | ||
// Talvez mudar a avaliação sintática para não fazer mais isso. | ||
console.log(declaracao); | ||
} | ||
visitarDeclaracaoDeAtribuicao(expressao) { | ||
if ([ | ||
delegua_1.default.MAIS_IGUAL, | ||
delegua_1.default.MENOS_IGUAL, | ||
delegua_1.default.MULTIPLICACAO_IGUAL, | ||
delegua_1.default.DIVISAO_IGUAL, | ||
delegua_1.default.DIVISAO_INTEIRA_IGUAL, | ||
delegua_1.default.MODULO_IGUAL, | ||
].includes(expressao.valor.operador.tipo)) { | ||
this.visitarExpressaoBinaria(expressao.valor); | ||
} | ||
else { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`; | ||
this.codigoFormatado += `${expressao.simbolo.lexema} = `; | ||
this.formatarDeclaracaoOuConstruto(expressao.valor); | ||
} | ||
this.codigoFormatado += `${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoDeExpressao(declaracao) { | ||
this.codigoFormatado += ' '.repeat(this.indentacaoAtual); | ||
this.formatarDeclaracaoOuConstruto(declaracao.expressao); | ||
} | ||
visitarDeclaracaoDefinicaoFuncao(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}função `; | ||
if (declaracao.simbolo) { | ||
this.codigoFormatado += `${declaracao.simbolo.lexema}`; | ||
} | ||
this.formatarDeclaracaoOuConstruto(declaracao.funcao); | ||
} | ||
visitarDeclaracaoEnquanto(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}enquanto `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.condicao); | ||
this.formatarDeclaracaoOuConstruto(declaracao.corpo); | ||
} | ||
visitarDeclaracaoEscolha(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}escolha `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.identificadorOuLiteral); | ||
this.codigoFormatado += ` {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let caminho of declaracao.caminhos) { | ||
for (let declaracoes of caminho.condicoes) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}caso `; | ||
this.formatarDeclaracaoOuConstruto(declaracoes); | ||
this.codigoFormatado += ':'; | ||
this.codigoFormatado += this.quebraLinha; | ||
} | ||
for (let declaracoes of caminho.declaracoes) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`; | ||
this.formatarDeclaracaoOuConstruto(declaracoes); | ||
} | ||
} | ||
for (let padrao of declaracao.caminhoPadrao.declaracoes) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}padrao:`; | ||
this.codigoFormatado += this.quebraLinha; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`; | ||
this.formatarDeclaracaoOuConstruto(padrao); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoEscreva(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}escreva(`; | ||
for (let argumento of declaracao.argumentos) { | ||
this.formatarDeclaracaoOuConstruto(argumento); | ||
} | ||
this.codigoFormatado += `)${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoFazer(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}fazer {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.caminhoFazer.declaracoes) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}} enquanto `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.condicaoEnquanto); | ||
this.codigoFormatado += `${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoImportar(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}importar(`; | ||
this.formatarDeclaracaoOuConstruto(declaracao.caminho); | ||
this.codigoFormatado += `)`; | ||
} | ||
visitarDeclaracaoPara(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}para `; | ||
this.devePularLinha = false; | ||
if (declaracao.inicializador) { | ||
if (Array.isArray(declaracao.inicializador)) { | ||
this.deveIndentar = false; | ||
for (let declaracaoInicializador of declaracao.inicializador) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoInicializador); | ||
} | ||
this.deveIndentar = true; | ||
} | ||
else { | ||
this.formatarDeclaracaoOuConstruto(declaracao.inicializador); | ||
} | ||
} | ||
this.codigoFormatado += `; `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.condicao); | ||
this.codigoFormatado += `; `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.incrementar); | ||
this.devePularLinha = true; | ||
this.codigoFormatado += ` {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.corpo.declaracoes) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoParaCada(declaracao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarDeclaracaoSe(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}se `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.condicao); | ||
this.codigoFormatado += ` {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.caminhoEntao.declaracoes) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
if (declaracao.caminhoSenao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}} senão {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.caminhoSenao.declaracoes) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
} | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoTente(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}tente {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.caminhoTente) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
if (declaracao.caminhoPegue) { | ||
this.codigoFormatado += `} pegue {${this.quebraLinha}`; | ||
if (declaracao.caminhoPegue instanceof construtos_1.FuncaoConstruto) { | ||
// Se tem um parâmetro de erro. | ||
} | ||
else { | ||
// Se não tem um parâmetro de erro. | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.caminhoPegue) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
} | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
if (declaracao.caminhoFinalmente) { | ||
this.codigoFormatado += `} finalmente {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracao.caminhoFinalmente) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`; | ||
} | ||
visitarDeclaracaoVar(declaracao) { | ||
if (this.deveIndentar) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}`; | ||
} | ||
this.codigoFormatado += `var ${declaracao.simbolo.lexema}`; | ||
if (declaracao.inicializador) { | ||
this.codigoFormatado += ` = `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.inicializador); | ||
} | ||
if (this.devePularLinha) { | ||
this.codigoFormatado += this.quebraLinha; | ||
} | ||
} | ||
visitarDeclaracaoVarMultiplo(declaracao) { | ||
// TODO: Até então o código nunca passa por aqui, porque o avaliador sintático | ||
// converte `VarMultiplo` em várias declarações `var`. | ||
// Talvez mudar a avaliação sintática para não fazer mais isso. | ||
console.log(declaracao); | ||
} | ||
visitarExpressaoAcessoIndiceVariavel(expressao) { | ||
this.formatarDeclaracaoOuConstruto(expressao.entidadeChamada); | ||
this.codigoFormatado += `[`; | ||
this.formatarDeclaracaoOuConstruto(expressao.indice); | ||
this.codigoFormatado += `]`; | ||
} | ||
visitarExpressaoAcessoMetodo(expressao) { | ||
this.formatarDeclaracaoOuConstruto(expressao.objeto); | ||
this.codigoFormatado += '.'; | ||
this.codigoFormatado += expressao.simbolo.lexema; | ||
} | ||
visitarExpressaoAgrupamento(expressao) { | ||
this.codigoFormatado += '('; | ||
this.formatarDeclaracaoOuConstruto(expressao.expressao); | ||
this.codigoFormatado += ')'; | ||
} | ||
visitarExpressaoAtribuicaoPorIndice(expressao) { | ||
this.formatarDeclaracaoOuConstruto(expressao.objeto); | ||
this.codigoFormatado += '['; | ||
this.formatarDeclaracaoOuConstruto(expressao.indice); | ||
this.codigoFormatado += '] = '; | ||
this.formatarDeclaracaoOuConstruto(expressao.valor); | ||
this.codigoFormatado += this.quebraLinha; | ||
} | ||
visitarExpressaoAtribuicaoPorIndicesMatriz(expressao) { | ||
// Implementar somente para VisuAlg que tem atribuição diferente de Delégua - matriz[1, 2] = 1 | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoAcessoElementoMatriz(expressao) { | ||
// Implementar somente para VisuAlg que tem acesso ao elemento da matriz diferente de Delégua - var valor = matriz[1, 2] | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoBinaria(expressao) { | ||
this.formatarDeclaracaoOuConstruto(expressao.esquerda); | ||
switch (expressao.operador.tipo) { | ||
case delegua_1.default.ADICAO: | ||
this.codigoFormatado += ` + `; | ||
break; | ||
case delegua_1.default.DIVISAO: | ||
this.codigoFormatado += ` / `; | ||
break; | ||
case delegua_1.default.DIVISAO_IGUAL: | ||
this.codigoFormatado += ` /= `; | ||
break; | ||
case delegua_1.default.DIVISAO_INTEIRA: | ||
this.codigoFormatado += ` \\ `; | ||
break; | ||
case delegua_1.default.DIVISAO_INTEIRA_IGUAL: | ||
this.codigoFormatado += ` \\= `; | ||
break; | ||
case delegua_1.default.EXPONENCIACAO: | ||
this.codigoFormatado += ` ** `; | ||
break; | ||
case delegua_1.default.IGUAL_IGUAL: | ||
this.codigoFormatado += ` == `; | ||
break; | ||
case delegua_1.default.MAIOR: | ||
this.codigoFormatado += ` > `; | ||
break; | ||
case delegua_1.default.MAIOR_IGUAL: | ||
this.codigoFormatado += ` >= `; | ||
break; | ||
case delegua_1.default.MAIS_IGUAL: | ||
this.codigoFormatado += ` += `; | ||
break; | ||
case delegua_1.default.MENOR: | ||
this.codigoFormatado += ` < `; | ||
break; | ||
case delegua_1.default.MENOR_IGUAL: | ||
this.codigoFormatado += ` <= `; | ||
break; | ||
case delegua_1.default.MENOS_IGUAL: | ||
this.codigoFormatado += ` -= `; | ||
break; | ||
case delegua_1.default.MODULO: | ||
this.codigoFormatado += ` % `; | ||
break; | ||
case delegua_1.default.MODULO_IGUAL: | ||
this.codigoFormatado += ` %= `; | ||
break; | ||
case delegua_1.default.MULTIPLICACAO: | ||
this.codigoFormatado += ` * `; | ||
break; | ||
case delegua_1.default.MULTIPLICACAO_IGUAL: | ||
this.codigoFormatado += ` * `; | ||
break; | ||
case delegua_1.default.SUBTRACAO: | ||
this.codigoFormatado += ` - `; | ||
break; | ||
} | ||
this.formatarDeclaracaoOuConstruto(expressao.direita); | ||
} | ||
formatarBlocoOuVetorDeclaracoes(declaracoes) { | ||
this.codigoFormatado += ` {${this.quebraLinha}`; | ||
this.indentacaoAtual += this.tamanhoIndentacao; | ||
for (let declaracaoBloco of declaracoes) { | ||
this.formatarDeclaracaoOuConstruto(declaracaoBloco); | ||
} | ||
this.indentacaoAtual -= this.tamanhoIndentacao; | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}}${this.quebraLinha}`; | ||
} | ||
visitarExpressaoBloco(declaracao) { | ||
this.formatarBlocoOuVetorDeclaracoes(declaracao.declaracoes); | ||
} | ||
visitarExpressaoContinua(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}continua${this.quebraLinha}`; | ||
} | ||
visitarExpressaoDeChamada(expressao) { | ||
this.formatarDeclaracaoOuConstruto(expressao.entidadeChamada); | ||
this.codigoFormatado += '('; | ||
for (let argumento of expressao.argumentos) { | ||
this.formatarDeclaracaoOuConstruto(argumento); | ||
this.codigoFormatado += ', '; | ||
} | ||
if (expressao.argumentos.length > 0) { | ||
this.codigoFormatado = this.codigoFormatado.slice(0, -2); | ||
} | ||
this.codigoFormatado += ')'; | ||
} | ||
visitarExpressaoDefinirValor(expressao) { | ||
this.formatarDeclaracaoOuConstruto(expressao.objeto); | ||
this.codigoFormatado += `.${expressao.nome.lexema} = `; | ||
this.formatarDeclaracaoOuConstruto(expressao.valor); | ||
this.codigoFormatado += `${this.quebraLinha}`; | ||
} | ||
visitarExpressaoDeleguaFuncao(expressao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoDeVariavel(expressao) { | ||
this.codigoFormatado += expressao.simbolo.lexema; | ||
} | ||
visitarExpressaoDicionario(expressao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoExpressaoRegular(expressao) { | ||
this.codigoFormatado += `||${expressao.valor}||`; | ||
} | ||
visitarDeclaracaoEscrevaMesmaLinha(declaracao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoFalhar(expressao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}falhar`; | ||
if (expressao.explicacao) { | ||
this.codigoFormatado += ` `; | ||
this.formatarDeclaracaoOuConstruto(expressao.explicacao); | ||
} | ||
this.codigoFormatado += `${this.quebraLinha}`; | ||
} | ||
visitarExpressaoFimPara(declaracao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoFormatacaoEscrita(declaracao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoFuncaoConstruto(expressao) { | ||
this.codigoFormatado += `(`; | ||
for (let argumento of expressao.parametros) { | ||
this.codigoFormatado += `${argumento.nome.lexema}${argumento.tipoDado && argumento.tipoDado.tipo ? `: ${argumento.tipoDado.tipo}, ` : ', '}`; | ||
} | ||
if (expressao.parametros.length > 0) { | ||
this.codigoFormatado = this.codigoFormatado.slice(0, -2); | ||
} | ||
this.codigoFormatado += `) `; | ||
this.formatarBlocoOuVetorDeclaracoes(expressao.corpo); | ||
} | ||
visitarExpressaoIsto(expressao) { | ||
this.codigoFormatado += `isto`; | ||
} | ||
visitarExpressaoLeia(expressao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoLeiaMultiplo(expressao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoLiteral(expressao) { | ||
if (typeof expressao.valor === 'string') { | ||
this.codigoFormatado += `'${expressao.valor}'`; | ||
return; | ||
} | ||
this.codigoFormatado += expressao.valor; | ||
} | ||
visitarExpressaoLogica(expressao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoRetornar(declaracao) { | ||
this.codigoFormatado += `${' '.repeat(this.indentacaoAtual)}retorna`; | ||
if (declaracao.valor) { | ||
this.codigoFormatado += ` `; | ||
this.formatarDeclaracaoOuConstruto(declaracao.valor); | ||
} | ||
this.codigoFormatado += `${this.quebraLinha}`; | ||
} | ||
visitarExpressaoSuper(expressao) { | ||
// TODO: `expressao.metodo` deveria ser um `Construto`, não um `Simbolo` aqui. | ||
this.codigoFormatado += `super.${expressao.metodo.lexema}()`; | ||
console.log(expressao); | ||
} | ||
visitarExpressaoSustar(declaracao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoTipoDe(expressao) { | ||
throw new Error('Método não implementado.'); | ||
} | ||
visitarExpressaoUnaria(expressao) { | ||
let operador; | ||
switch (expressao.operador.tipo) { | ||
case delegua_1.default.INCREMENTAR: | ||
operador = `++`; | ||
break; | ||
case delegua_1.default.DECREMENTAR: | ||
operador = `--`; | ||
break; | ||
} | ||
switch (expressao.incidenciaOperador) { | ||
case 'ANTES': | ||
this.codigoFormatado += operador; | ||
this.formatarDeclaracaoOuConstruto(expressao.operando); | ||
break; | ||
case 'DEPOIS': | ||
this.formatarDeclaracaoOuConstruto(expressao.operando); | ||
this.codigoFormatado += operador; | ||
break; | ||
} | ||
if (this.devePularLinha) { | ||
this.codigoFormatado += this.quebraLinha; | ||
} | ||
} | ||
visitarExpressaoVetor(expressao) { | ||
this.codigoFormatado += '['; | ||
for (let valor of expressao.valores) { | ||
this.formatarDeclaracaoOuConstruto(valor); | ||
this.codigoFormatado += ', '; | ||
} | ||
if (expressao.valores.length > 0) { | ||
this.codigoFormatado = this.codigoFormatado.slice(0, -2); | ||
} | ||
this.codigoFormatado += ']'; | ||
} | ||
formatarDeclaracaoOuConstruto(declaracaoOuConstruto) { | ||
switch (declaracaoOuConstruto.constructor.name) { | ||
case 'AcessoIndiceVariavel': | ||
this.visitarExpressaoAcessoIndiceVariavel(declaracaoOuConstruto); | ||
break; | ||
case 'AcessoMetodoOuPropriedade': | ||
this.visitarExpressaoAcessoMetodo(declaracaoOuConstruto); | ||
break; | ||
case 'Agrupamento': | ||
this.visitarExpressaoAgrupamento(declaracaoOuConstruto); | ||
break; | ||
case 'AtribuicaoPorIndice': | ||
this.visitarExpressaoAtribuicaoPorIndice(declaracaoOuConstruto); | ||
break; | ||
case 'Atribuir': | ||
this.visitarDeclaracaoDeAtribuicao(declaracaoOuConstruto); | ||
break; | ||
case 'Binario': | ||
this.visitarExpressaoBinaria(declaracaoOuConstruto); | ||
break; | ||
case 'Bloco': | ||
this.visitarExpressaoBloco(declaracaoOuConstruto); | ||
break; | ||
case 'Chamada': | ||
this.visitarExpressaoDeChamada(declaracaoOuConstruto); | ||
break; | ||
case 'Classe': | ||
this.visitarDeclaracaoClasse(declaracaoOuConstruto); | ||
break; | ||
case 'Continua': | ||
this.visitarExpressaoContinua(declaracaoOuConstruto); | ||
break; | ||
case 'DefinirValor': | ||
this.visitarExpressaoDefinirValor(declaracaoOuConstruto); | ||
break; | ||
case 'Escolha': | ||
this.visitarDeclaracaoEscolha(declaracaoOuConstruto); | ||
break; | ||
case 'Enquanto': | ||
this.visitarDeclaracaoEnquanto(declaracaoOuConstruto); | ||
break; | ||
case 'Escreva': | ||
this.visitarDeclaracaoEscreva(declaracaoOuConstruto); | ||
break; | ||
case 'Expressao': | ||
this.visitarDeclaracaoDeExpressao(declaracaoOuConstruto); | ||
break; | ||
case 'ExpressaoRegular': | ||
this.visitarExpressaoExpressaoRegular(declaracaoOuConstruto); | ||
break; | ||
case 'Falhar': | ||
this.visitarExpressaoFalhar(declaracaoOuConstruto); | ||
break; | ||
case 'Fazer': | ||
this.visitarDeclaracaoFazer(declaracaoOuConstruto); | ||
break; | ||
case 'FuncaoConstruto': | ||
this.visitarExpressaoFuncaoConstruto(declaracaoOuConstruto); | ||
break; | ||
case 'FuncaoDeclaracao': | ||
this.visitarDeclaracaoDefinicaoFuncao(declaracaoOuConstruto); | ||
break; | ||
case 'Importar': | ||
this.visitarDeclaracaoImportar(declaracaoOuConstruto); | ||
break; | ||
case 'Isto': | ||
this.visitarExpressaoIsto(declaracaoOuConstruto); | ||
break; | ||
case 'Literal': | ||
this.visitarExpressaoLiteral(declaracaoOuConstruto); | ||
break; | ||
case 'Para': | ||
this.visitarDeclaracaoPara(declaracaoOuConstruto); | ||
break; | ||
case 'Retorna': | ||
this.visitarExpressaoRetornar(declaracaoOuConstruto); | ||
break; | ||
case 'Se': | ||
this.visitarDeclaracaoSe(declaracaoOuConstruto); | ||
break; | ||
case 'Super': | ||
this.visitarExpressaoSuper(declaracaoOuConstruto); | ||
break; | ||
case 'Tente': | ||
this.visitarDeclaracaoTente(declaracaoOuConstruto); | ||
break; | ||
case 'Unario': | ||
this.visitarExpressaoUnaria(declaracaoOuConstruto); | ||
break; | ||
case 'Const': | ||
this.visitarDeclaracaoConst(declaracaoOuConstruto); | ||
break; | ||
case 'Var': | ||
this.visitarDeclaracaoVar(declaracaoOuConstruto); | ||
break; | ||
case 'Variavel': | ||
this.visitarExpressaoDeVariavel(declaracaoOuConstruto); | ||
break; | ||
case 'Vetor': | ||
this.visitarExpressaoVetor(declaracaoOuConstruto); | ||
break; | ||
default: | ||
console.log(declaracaoOuConstruto.constructor.name); | ||
break; | ||
} | ||
} | ||
/** | ||
* Devolve código formatado de acordo com os símbolos encontrados. | ||
* @param simbolos Um vetor de símbolos. | ||
* @param quebraLinha O símbolo de quebra de linha. Normalmente `\r\n` para Windows e `\n` para outros sistemas operacionais. | ||
* @param tamanhoIndentacao O tamanho de cada bloco de indentação (por padrão, 4) | ||
* @param {Declaracao[]} declaracoes Um vetor de declarações. | ||
* @returns Código Delégua formatado. | ||
*/ | ||
formatar(simbolos, quebraLinha, tamanhoIndentacao = 4) { | ||
let resultado = ''; | ||
let deveQuebrarLinha = false; | ||
for (let simbolo of simbolos) { | ||
switch (simbolo.tipo) { | ||
case delegua_1.default.CHAVE_ESQUERDA: | ||
this.indentacao += tamanhoIndentacao; | ||
resultado += '{' + quebraLinha; | ||
resultado += ' '.repeat(this.indentacao); | ||
break; | ||
case delegua_1.default.CHAVE_DIREITA: | ||
this.indentacao -= tamanhoIndentacao; | ||
resultado += quebraLinha + ' '.repeat(this.indentacao) + '}' + quebraLinha; | ||
break; | ||
case delegua_1.default.ESCREVA: | ||
deveQuebrarLinha = true; | ||
case delegua_1.default.ENQUANTO: | ||
case delegua_1.default.FAZER: | ||
case delegua_1.default.FINALMENTE: | ||
case delegua_1.default.PARA: | ||
case delegua_1.default.PEGUE: | ||
case delegua_1.default.RETORNA: | ||
case delegua_1.default.SE: | ||
case delegua_1.default.SENAO: | ||
case delegua_1.default.SENÃO: | ||
case delegua_1.default.TENTE: | ||
case delegua_1.default.VARIAVEL: | ||
resultado += quebraLinha + ' '.repeat(this.indentacao) + simbolo.lexema + ' '; | ||
break; | ||
case delegua_1.default.PARENTESE_ESQUERDO: | ||
resultado += '('; | ||
break; | ||
case delegua_1.default.PARENTESE_DIREITO: | ||
resultado = resultado.trimEnd(); | ||
resultado += ')'; | ||
if (deveQuebrarLinha) { | ||
deveQuebrarLinha = false; | ||
resultado += quebraLinha; | ||
} | ||
else { | ||
resultado += ' '; | ||
} | ||
break; | ||
case delegua_1.default.FUNCAO: | ||
case delegua_1.default.FUNÇÃO: | ||
case delegua_1.default.IDENTIFICADOR: | ||
case delegua_1.default.IGUAL: | ||
case delegua_1.default.IGUAL_IGUAL: | ||
resultado += simbolo.lexema + ' '; | ||
break; | ||
default: | ||
resultado += simbolo.lexema; | ||
break; | ||
} | ||
formatar(declaracoes) { | ||
this.codigoFormatado = ''; | ||
this.indentacaoAtual = 0; | ||
for (let declaracao of declaracoes) { | ||
this.formatarDeclaracaoOuConstruto(declaracao); | ||
} | ||
return resultado; | ||
return this.codigoFormatado; | ||
} | ||
@@ -83,0 +594,0 @@ } |
@@ -1,1 +0,1 @@ | ||
export type TiposDadosInterface = 'dicionário' | 'função' | 'inteiro' | 'inteiro[]' | 'longo' | 'lógico' | 'módulo' | 'nulo' | 'numero' | 'qualquer' | 'qualquer[]' | 'real' | 'real[]' | 'símbolo' | 'texto' | 'texto[]' | 'vetor' | undefined; | ||
export type TiposDadosInterface = 'dicionário' | 'função' | 'inteiro' | 'inteiro[]' | 'longo' | 'lógico' | 'módulo' | 'nulo' | 'numero' | 'qualquer' | 'qualquer[]' | 'real' | 'real[]' | 'símbolo' | 'texto' | 'texto[]' | 'caracter' | 'caracter[]' | 'vetor' | undefined; |
import { Construto, Literal } from '../../../construtos'; | ||
import { Declaracao, Leia, Para } from '../../../declaracoes'; | ||
import { InterpretadorBirlInterface } from '../../../interfaces/dialeto/interpretador-birl-interface'; | ||
import { InterpretadorBirlInterface } from '../../../interfaces/dialetos/interpretador-birl-interface'; | ||
import { RetornoInterpretador } from '../../../interfaces/retornos'; | ||
@@ -5,0 +5,0 @@ export declare function atribuirVariavel(interpretador: InterpretadorBirlInterface, expressao: Construto, valor: any, tipo: string): Promise<any>; |
import { Construto, Literal } from '../../../construtos'; | ||
import { Declaracao, Leia, Para } from '../../../declaracoes'; | ||
import { InterpretadorBirlInterface } from '../../../interfaces/dialeto/interpretador-birl-interface'; | ||
import { InterpretadorBirlInterface } from '../../../interfaces/dialetos/interpretador-birl-interface'; | ||
import { RetornoInterpretador } from '../../../interfaces/retornos'; | ||
@@ -5,0 +5,0 @@ import { InterpretadorComDepuracao } from '../../interpretador-com-depuracao'; |
@@ -39,3 +39,3 @@ import { EspacoVariaveis } from '../../../espaco-variaveis'; | ||
visitarExpressaoLiteral(expressao: Literal): any; | ||
avaliar(expressao: Construto): VariavelInterface | any; | ||
avaliar(expressao: Construto | Declaracao): VariavelInterface | any; | ||
visitarExpressaoAgrupamento(expressao: any): any; | ||
@@ -42,0 +42,0 @@ eVerdadeiro(objeto: any): boolean; |
@@ -1,2 +0,2 @@ | ||
import { Binario, Construto, Logico } from '../../../construtos'; | ||
import { AcessoElementoMatriz, AtribuicaoPorIndicesMatriz, Binario, Construto, Logico } from '../../../construtos'; | ||
import { Para } from '../../../declaracoes'; | ||
@@ -14,1 +14,3 @@ import { InterpretadorBase } from '../../interpretador-base'; | ||
export declare function resolverIncrementoPara(interpretador: InterpretadorBase, declaracao: Para): Promise<any>; | ||
export declare function visitarExpressaoAcessoElementoMatriz(interpretador: InterpretadorBase, expressao: AcessoElementoMatriz): Promise<any>; | ||
export declare function visitarExpressaoAtribuicaoPorIndicesMatriz(interpretador: InterpretadorBase, expressao: AtribuicaoPorIndicesMatriz): Promise<any>; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resolverIncrementoPara = exports.visitarExpressaoLogica = exports.visitarExpressaoBinaria = exports.atribuirVariavel = void 0; | ||
exports.visitarExpressaoAtribuicaoPorIndicesMatriz = exports.visitarExpressaoAcessoElementoMatriz = exports.resolverIncrementoPara = exports.visitarExpressaoLogica = exports.visitarExpressaoBinaria = exports.atribuirVariavel = void 0; | ||
const construtos_1 = require("../../../construtos"); | ||
@@ -213,2 +213,70 @@ const lexador_1 = require("../../../lexador"); | ||
exports.resolverIncrementoPara = resolverIncrementoPara; | ||
async function visitarExpressaoAcessoElementoMatriz(interpretador, expressao) { | ||
const promises = await Promise.all([ | ||
avaliar(interpretador, expressao.entidadeChamada), | ||
avaliar(interpretador, expressao.indicePrimario), | ||
avaliar(interpretador, expressao.indiceSecundario), | ||
]); | ||
const variavelObjeto = promises[0]; | ||
const indicePrimario = promises[1]; | ||
const indiceSecundario = promises[2]; | ||
const objeto = variavelObjeto.hasOwnProperty('valor') ? variavelObjeto.valor : variavelObjeto; | ||
let valorIndicePrimario = indicePrimario.hasOwnProperty('valor') ? indicePrimario.valor : indicePrimario; | ||
let valorIndiceSecundario = indiceSecundario.hasOwnProperty('valor') ? indiceSecundario.valor : indiceSecundario; | ||
if (Array.isArray(objeto)) { | ||
if (!Number.isInteger(valorIndicePrimario) || !Number.isInteger(valorIndiceSecundario)) { | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Somente inteiros podem ser usados para indexar um vetor.', expressao.linha)); | ||
} | ||
if (valorIndicePrimario < 0 && objeto.length !== 0) { | ||
while (valorIndicePrimario < 0) { | ||
valorIndicePrimario += objeto.length; | ||
} | ||
} | ||
if (valorIndiceSecundario < 0 && objeto.length !== 0) { | ||
while (valorIndiceSecundario < 0) { | ||
valorIndiceSecundario += objeto.length; | ||
} | ||
} | ||
if (valorIndicePrimario >= objeto.length || valorIndiceSecundario >= objeto.length) { | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Índice do vetor fora do intervalo.', expressao.linha)); | ||
} | ||
return objeto[valorIndicePrimario][valorIndiceSecundario]; | ||
} | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.entidadeChamada.valor, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha)); | ||
} | ||
exports.visitarExpressaoAcessoElementoMatriz = visitarExpressaoAcessoElementoMatriz; | ||
async function visitarExpressaoAtribuicaoPorIndicesMatriz(interpretador, expressao) { | ||
const promises = await Promise.all([ | ||
avaliar(interpretador, expressao.objeto), | ||
avaliar(interpretador, expressao.indicePrimario), | ||
avaliar(interpretador, expressao.indiceSecundario), | ||
avaliar(interpretador, expressao.valor), | ||
]); | ||
let objeto = promises[0]; | ||
let indicePrimario = promises[1]; | ||
let indiceSecundario = promises[2]; | ||
const valor = promises[3]; | ||
objeto = objeto.hasOwnProperty('valor') ? objeto.valor : objeto; | ||
indicePrimario = indicePrimario.hasOwnProperty('valor') ? indicePrimario.valor : indicePrimario; | ||
indiceSecundario = indiceSecundario.hasOwnProperty('valor') ? indiceSecundario.valor : indiceSecundario; | ||
if (Array.isArray(objeto)) { | ||
if (indicePrimario < 0 && objeto.length !== 0) { | ||
while (indicePrimario < 0) { | ||
indicePrimario += objeto.length; | ||
} | ||
} | ||
if (indiceSecundario < 0 && objeto.length !== 0) { | ||
while (indiceSecundario < 0) { | ||
indiceSecundario += objeto.length; | ||
} | ||
} | ||
while (objeto.length < indicePrimario || objeto.length < indiceSecundario) { | ||
objeto.push(null); | ||
} | ||
objeto[indicePrimario][indiceSecundario] = valor; | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.objeto.nome, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha)); | ||
} | ||
exports.visitarExpressaoAtribuicaoPorIndicesMatriz = visitarExpressaoAtribuicaoPorIndicesMatriz; | ||
//# sourceMappingURL=comum.js.map |
@@ -31,3 +31,2 @@ "use strict"; | ||
const interpretador_com_depuracao_1 = require("../../interpretador-com-depuracao"); | ||
const excecoes_1 = require("../../../excecoes"); | ||
const comum = __importStar(require("./comum")); | ||
@@ -48,66 +47,6 @@ /** | ||
async visitarExpressaoAcessoElementoMatriz(expressao) { | ||
const promises = await Promise.all([ | ||
this.avaliar(expressao.entidadeChamada), | ||
this.avaliar(expressao.indicePrimario), | ||
this.avaliar(expressao.indiceSecundario), | ||
]); | ||
const variavelObjeto = promises[0]; | ||
const indicePrimario = promises[1]; | ||
const indiceSecundario = promises[2]; | ||
const objeto = variavelObjeto.hasOwnProperty('valor') ? variavelObjeto.valor : variavelObjeto; | ||
let valorIndicePrimario = indicePrimario.hasOwnProperty('valor') ? indicePrimario.valor : indicePrimario; | ||
let valorIndiceSecundario = indiceSecundario.hasOwnProperty('valor') ? indiceSecundario.valor : indiceSecundario; | ||
if (Array.isArray(objeto)) { | ||
if (!Number.isInteger(valorIndicePrimario) || !Number.isInteger(valorIndiceSecundario)) { | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Somente inteiros podem ser usados para indexar um vetor.', expressao.linha)); | ||
} | ||
if (valorIndicePrimario < 0 && objeto.length !== 0) { | ||
while (valorIndicePrimario < 0) { | ||
valorIndicePrimario += objeto.length; | ||
} | ||
} | ||
if (valorIndiceSecundario < 0 && objeto.length !== 0) { | ||
while (valorIndiceSecundario < 0) { | ||
valorIndiceSecundario += objeto.length; | ||
} | ||
} | ||
if (valorIndicePrimario >= objeto.length || valorIndiceSecundario >= objeto.length) { | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Índice do vetor fora do intervalo.', expressao.linha)); | ||
} | ||
return objeto[valorIndicePrimario][valorIndiceSecundario]; | ||
} | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.entidadeChamada.valor, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha)); | ||
return await comum.visitarExpressaoAcessoElementoMatriz(this, expressao); | ||
} | ||
async visitarExpressaoAtribuicaoPorIndicesMatriz(expressao) { | ||
const promises = await Promise.all([ | ||
this.avaliar(expressao.objeto), | ||
this.avaliar(expressao.indicePrimario), | ||
this.avaliar(expressao.indiceSecundario), | ||
this.avaliar(expressao.valor), | ||
]); | ||
let objeto = promises[0]; | ||
let indicePrimario = promises[1]; | ||
let indiceSecundario = promises[2]; | ||
const valor = promises[3]; | ||
objeto = objeto.hasOwnProperty('valor') ? objeto.valor : objeto; | ||
indicePrimario = indicePrimario.hasOwnProperty('valor') ? indicePrimario.valor : indicePrimario; | ||
indiceSecundario = indiceSecundario.hasOwnProperty('valor') ? indiceSecundario.valor : indiceSecundario; | ||
if (Array.isArray(objeto)) { | ||
if (indicePrimario < 0 && objeto.length !== 0) { | ||
while (indicePrimario < 0) { | ||
indicePrimario += objeto.length; | ||
} | ||
} | ||
if (indiceSecundario < 0 && objeto.length !== 0) { | ||
while (indiceSecundario < 0) { | ||
indiceSecundario += objeto.length; | ||
} | ||
} | ||
while (objeto.length < indicePrimario || objeto.length < indiceSecundario) { | ||
objeto.push(null); | ||
} | ||
objeto[indicePrimario][indiceSecundario] = valor; | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.objeto.nome, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha)); | ||
return await comum.visitarExpressaoAtribuicaoPorIndicesMatriz(this, expressao); | ||
} | ||
@@ -114,0 +53,0 @@ async avaliarArgumentosEscrevaVisuAlg(argumentos) { |
@@ -31,3 +31,2 @@ "use strict"; | ||
const visualg_1 = require("../../../bibliotecas/dialetos/visualg"); | ||
const excecoes_1 = require("../../../excecoes"); | ||
const comum = __importStar(require("./comum")); | ||
@@ -51,66 +50,6 @@ /** | ||
async visitarExpressaoAcessoElementoMatriz(expressao) { | ||
const promises = await Promise.all([ | ||
this.avaliar(expressao.entidadeChamada), | ||
this.avaliar(expressao.indicePrimario), | ||
this.avaliar(expressao.indiceSecundario), | ||
]); | ||
const variavelObjeto = promises[0]; | ||
const indicePrimario = promises[1]; | ||
const indiceSecundario = promises[2]; | ||
const objeto = variavelObjeto.hasOwnProperty('valor') ? variavelObjeto.valor : variavelObjeto; | ||
let valorIndicePrimario = indicePrimario.hasOwnProperty('valor') ? indicePrimario.valor : indicePrimario; | ||
let valorIndiceSecundario = indiceSecundario.hasOwnProperty('valor') ? indiceSecundario.valor : indiceSecundario; | ||
if (Array.isArray(objeto)) { | ||
if (!Number.isInteger(valorIndicePrimario) || !Number.isInteger(valorIndiceSecundario)) { | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Somente inteiros podem ser usados para indexar um vetor.', expressao.linha)); | ||
} | ||
if (valorIndicePrimario < 0 && objeto.length !== 0) { | ||
while (valorIndicePrimario < 0) { | ||
valorIndicePrimario += objeto.length; | ||
} | ||
} | ||
if (valorIndiceSecundario < 0 && objeto.length !== 0) { | ||
while (valorIndiceSecundario < 0) { | ||
valorIndiceSecundario += objeto.length; | ||
} | ||
} | ||
if (valorIndicePrimario >= objeto.length || valorIndiceSecundario >= objeto.length) { | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Índice do vetor fora do intervalo.', expressao.linha)); | ||
} | ||
return objeto[valorIndicePrimario][valorIndiceSecundario]; | ||
} | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.entidadeChamada.valor, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha)); | ||
return await comum.visitarExpressaoAcessoElementoMatriz(this, expressao); | ||
} | ||
async visitarExpressaoAtribuicaoPorIndicesMatriz(expressao) { | ||
const promises = await Promise.all([ | ||
this.avaliar(expressao.objeto), | ||
this.avaliar(expressao.indicePrimario), | ||
this.avaliar(expressao.indiceSecundario), | ||
this.avaliar(expressao.valor), | ||
]); | ||
let objeto = promises[0]; | ||
let indicePrimario = promises[1]; | ||
let indiceSecundario = promises[2]; | ||
const valor = promises[3]; | ||
objeto = objeto.hasOwnProperty('valor') ? objeto.valor : objeto; | ||
indicePrimario = indicePrimario.hasOwnProperty('valor') ? indicePrimario.valor : indicePrimario; | ||
indiceSecundario = indiceSecundario.hasOwnProperty('valor') ? indiceSecundario.valor : indiceSecundario; | ||
if (Array.isArray(objeto)) { | ||
if (indicePrimario < 0 && objeto.length !== 0) { | ||
while (indicePrimario < 0) { | ||
indicePrimario += objeto.length; | ||
} | ||
} | ||
if (indiceSecundario < 0 && objeto.length !== 0) { | ||
while (indiceSecundario < 0) { | ||
indiceSecundario += objeto.length; | ||
} | ||
} | ||
while (objeto.length < indicePrimario || objeto.length < indiceSecundario) { | ||
objeto.push(null); | ||
} | ||
objeto[indicePrimario][indiceSecundario] = valor; | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao(expressao.objeto.nome, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha)); | ||
return await comum.visitarExpressaoAtribuicaoPorIndicesMatriz(this, expressao); | ||
} | ||
@@ -117,0 +56,0 @@ async avaliarArgumentosEscrevaVisuAlg(argumentos) { |
{ | ||
"name": "@designliquido/delegua", | ||
"version": "0.29.4", | ||
"version": "0.30.0", | ||
"description": "Linguagem de programação simples e moderna usando português estruturado, com suporte a múltiplos dialetos", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3201352
776
47202