@designliquido/delegua 1.20.1 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.d.ts +1 -0
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.d.ts.map +1 -1
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.js +46 -40
- package/avaliador-sintatico/dialetos/avaliador-sintatico-pitugues.js.map +1 -1
- package/bibliotecas/dialetos/pitugues/biblioteca-global.d.ts +117 -141
- package/bibliotecas/dialetos/pitugues/biblioteca-global.d.ts.map +1 -1
- package/bibliotecas/dialetos/pitugues/biblioteca-global.js +723 -610
- package/bibliotecas/dialetos/pitugues/biblioteca-global.js.map +1 -1
- package/bibliotecas/dialetos/pitugues/primitivas-vetor.d.ts.map +1 -1
- package/bibliotecas/dialetos/pitugues/primitivas-vetor.js +21 -0
- package/bibliotecas/dialetos/pitugues/primitivas-vetor.js.map +1 -1
- package/bin/package.json +1 -1
- package/interpretador/depuracao/interpretador-base-com-depuracao.d.ts.map +1 -1
- package/interpretador/depuracao/interpretador-base-com-depuracao.js +12 -1
- package/interpretador/depuracao/interpretador-base-com-depuracao.js.map +1 -1
- package/lexador/dialetos/palavras-reservadas/pitugues.d.ts +0 -2
- package/lexador/dialetos/palavras-reservadas/pitugues.d.ts.map +1 -1
- package/lexador/dialetos/palavras-reservadas/pitugues.js +0 -2
- package/lexador/dialetos/palavras-reservadas/pitugues.js.map +1 -1
- package/package.json +1 -1
- package/tradutores/tradutor-assembly-arm.js +151 -151
- package/tradutores/tradutor-assembly-risc-v.js +85 -85
- package/tradutores/tradutor-assembly-x64.js +176 -176
- package/tradutores/tradutor-webassembly.js +29 -29
- package/umd/delegua.js +487 -483
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.aleatorio = aleatorio;
|
|
4
|
-
exports.aleatorio_entre = aleatorio_entre;
|
|
5
4
|
exports.algum = algum;
|
|
6
|
-
exports.
|
|
5
|
+
exports.combinar = combinar;
|
|
6
|
+
exports.contar = contar;
|
|
7
7
|
exports.encontrar = encontrar;
|
|
8
8
|
exports.encontrar_indice = encontrar_indice;
|
|
9
9
|
exports.encontrar_ultimo = encontrar_ultimo;
|
|
@@ -12,22 +12,21 @@ exports.filtrar_por = filtrar_por;
|
|
|
12
12
|
exports.incluido = incluido;
|
|
13
13
|
exports.inteiro = inteiro;
|
|
14
14
|
exports.intervalo = intervalo;
|
|
15
|
+
exports.inverter = inverter;
|
|
15
16
|
exports.enumerar = enumerar;
|
|
16
17
|
exports.mapear = mapear;
|
|
17
18
|
exports.maximo = maximo;
|
|
18
19
|
exports.minimo = minimo;
|
|
19
|
-
exports.numero = numero;
|
|
20
20
|
exports.ordenar = ordenar;
|
|
21
21
|
exports.para_cada = para_cada;
|
|
22
|
-
exports.primeiro_em_condicao = primeiro_em_condicao;
|
|
23
22
|
exports.real = real;
|
|
24
23
|
exports.reduzir = reduzir;
|
|
25
24
|
exports.somar = somar;
|
|
26
25
|
exports.tamanho = tamanho;
|
|
27
26
|
exports.texto = texto;
|
|
28
27
|
exports.todos = todos;
|
|
29
|
-
exports.todos_em_condicao = todos_em_condicao;
|
|
30
28
|
exports.tupla = tupla;
|
|
29
|
+
exports.unico = unico;
|
|
31
30
|
exports.vetor = vetor;
|
|
32
31
|
const excecoes_1 = require("../../../excecoes");
|
|
33
32
|
const objeto_delegua_classe_1 = require("../../../interpretador/estruturas/objeto-delegua-classe");
|
|
@@ -36,8 +35,9 @@ const descritor_tipo_classe_1 = require("../../../interpretador/estruturas/descr
|
|
|
36
35
|
const estruturas_1 = require("../../../interpretador/estruturas");
|
|
37
36
|
const construtos_1 = require("../../../construtos");
|
|
38
37
|
const quebras_1 = require("../../../quebras");
|
|
38
|
+
const iteravel_1 = require("../../../interpretador/estruturas/iteravel");
|
|
39
39
|
/**
|
|
40
|
-
* Compara dois valores (números ou vetores).
|
|
40
|
+
* Compara dois valores (números, textos, booleanos ou vetores).
|
|
41
41
|
* Retorna:
|
|
42
42
|
* > 0 se a > b
|
|
43
43
|
* < 0 se a < b
|
|
@@ -48,6 +48,12 @@ function compararElementosRecursivamente(a, b) {
|
|
|
48
48
|
if (typeof a === 'number' && typeof b === 'number') {
|
|
49
49
|
return a - b;
|
|
50
50
|
}
|
|
51
|
+
if (typeof a === 'string' && typeof b === 'string') {
|
|
52
|
+
return a.localeCompare(b);
|
|
53
|
+
}
|
|
54
|
+
if (typeof a === 'boolean' && typeof b === 'boolean') {
|
|
55
|
+
return (a === b) ? 0 : (a ? 1 : -1);
|
|
56
|
+
}
|
|
51
57
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
52
58
|
const tamanho = Math.min(a.length, b.length);
|
|
53
59
|
for (let i = 0; i < tamanho; i++) {
|
|
@@ -57,879 +63,962 @@ function compararElementosRecursivamente(a, b) {
|
|
|
57
63
|
}
|
|
58
64
|
return a.length - b.length;
|
|
59
65
|
}
|
|
60
|
-
// Tipos incompatíveis
|
|
66
|
+
// Tipos incompatíveis
|
|
61
67
|
throw new Error('Tipos incompatíveis para comparação.');
|
|
62
68
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
* Retorna um número aleatório de acordo com o parâmetro passado.
|
|
72
|
-
* Mínimo(inclusivo) - Máximo(exclusivo).
|
|
73
|
-
* @param {number} minimo O número mínimo.
|
|
74
|
-
* @param {number} maximo O número máximo.
|
|
75
|
-
* @returns {Promise<number>} Um número real entre os valores máximo e mínimo especificados.
|
|
76
|
-
*/
|
|
77
|
-
async function aleatorio_entre(interpretador, ...argumentos) {
|
|
78
|
-
const argumentosUsuario = argumentos.filter((arg) => !(arg && typeof arg === 'object' && 'lexema' in arg && 'linha' in arg));
|
|
79
|
-
if (argumentosUsuario.length <= 0) {
|
|
80
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
69
|
+
function validacao_comum_numeros(interpretador, valorParaConverter, nomeDaFuncao) {
|
|
70
|
+
const numeroConvertido = Number(valorParaConverter);
|
|
71
|
+
if (valorParaConverter === '' ||
|
|
72
|
+
valorParaConverter === null ||
|
|
73
|
+
valorParaConverter === undefined ||
|
|
74
|
+
typeof valorParaConverter === 'boolean' ||
|
|
75
|
+
Number.isNaN(numeroConvertido)) {
|
|
76
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
81
77
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
82
78
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
83
|
-
},
|
|
79
|
+
}, `Valor não parece estar estruturado como um número válido. Somente números ou textos com números podem ser convertidos na função ${nomeDaFuncao}().`);
|
|
84
80
|
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Retorna um número aleatório.
|
|
84
|
+
* - Sem argumentos: retorna um número real entre 0 (inclusivo) e 1 (exclusivo).
|
|
85
|
+
* - Com um argumento: retorna um número real entre 0 (inclusivo) e `maximo` (exclusivo).
|
|
86
|
+
* - Com dois argumentos: retorna um número real entre `minimo` (inclusivo) e `maximo` (exclusivo).
|
|
87
|
+
* @param {number} [minimo] O número mínimo (inclusivo), ou o máximo quando único argumento.
|
|
88
|
+
* @param {number} [maximo] O número máximo (exclusivo).
|
|
89
|
+
* @returns {Promise<number>} O número real aleatório gerado.
|
|
90
|
+
*/
|
|
91
|
+
async function aleatorio(interpretador, ...argumentos) {
|
|
92
|
+
const argumentosUsuario = argumentos.filter((arg) => !(arg &&
|
|
93
|
+
typeof arg === 'object' &&
|
|
94
|
+
'lexema' in arg &&
|
|
95
|
+
'linha' in arg));
|
|
96
|
+
if (argumentosUsuario.length === 0)
|
|
97
|
+
return Math.random();
|
|
85
98
|
if (argumentosUsuario.length > 2) {
|
|
86
|
-
|
|
87
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
99
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
88
100
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
89
|
-
|
|
101
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
102
|
+
}, 'A função aceita no máximo 2 parâmetros.');
|
|
90
103
|
}
|
|
91
104
|
const minimo = interpretador.resolverValor(argumentosUsuario[0]);
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}, 'O parâmetro deve ser um número.'));
|
|
98
|
-
}
|
|
99
|
-
return Math.floor(Math.random() * (0 - minimo)) + minimo;
|
|
105
|
+
if (typeof minimo !== 'number') {
|
|
106
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
107
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
108
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
109
|
+
}, 'O primeiro parâmetro deve ser um número.');
|
|
100
110
|
}
|
|
111
|
+
if (argumentosUsuario.length === 1)
|
|
112
|
+
return Math.random() * minimo;
|
|
101
113
|
const maximo = interpretador.resolverValor(argumentosUsuario[1]);
|
|
102
|
-
if (typeof
|
|
103
|
-
|
|
104
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
114
|
+
if (typeof maximo !== 'number') {
|
|
115
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
105
116
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
106
|
-
|
|
117
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
118
|
+
}, 'O segundo parâmetro deve ser um número.');
|
|
107
119
|
}
|
|
108
|
-
return
|
|
120
|
+
return Math.random() * (maximo - minimo) + minimo;
|
|
109
121
|
}
|
|
110
122
|
/**
|
|
111
|
-
* Verifica se algum dos elementos satisfaz
|
|
123
|
+
* Verifica se algum dos elementos satisfaz a condição passada por parâmetro.
|
|
112
124
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
113
|
-
* @param {
|
|
114
|
-
* @param {
|
|
115
|
-
* @returns {Promise<boolean>} Verdadeiro se há algum elemento no
|
|
125
|
+
* @param {any} iteravel Um iterável.
|
|
126
|
+
* @param {any} funcaoPesquisa A função que ensina o método de pesquisa.
|
|
127
|
+
* @returns {Promise<boolean>} Verdadeiro se há algum elemento no iterável com a condição. Falso caso contrário.
|
|
116
128
|
*/
|
|
117
|
-
async function algum(interpretador,
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (!Array.isArray(valorVetor)) {
|
|
123
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
124
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
129
|
+
async function algum(interpretador, iteravel, funcaoPesquisa) {
|
|
130
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
131
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
132
|
+
if (itens.length === 0) {
|
|
133
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
125
134
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
126
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um vetor.'));
|
|
127
|
-
}
|
|
128
|
-
if (valorFuncaoPesquisa.constructor !== estruturas_1.DeleguaFuncao) {
|
|
129
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
130
135
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
136
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
137
|
+
}
|
|
138
|
+
const valorFuncao = interpretador.resolverValor(funcaoPesquisa);
|
|
139
|
+
if (!(valorFuncao instanceof estruturas_1.DeleguaFuncao) &&
|
|
140
|
+
!(valorFuncao instanceof funcao_padrao_1.FuncaoPadrao)) {
|
|
141
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
131
142
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
132
|
-
|
|
143
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
144
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
133
145
|
}
|
|
134
|
-
for (
|
|
135
|
-
if (await
|
|
146
|
+
for (const item of itens) {
|
|
147
|
+
if (await valorFuncao.chamar(interpretador, item, {
|
|
148
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
149
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
150
|
+
})) {
|
|
136
151
|
return true;
|
|
137
152
|
}
|
|
138
153
|
}
|
|
139
154
|
return false;
|
|
140
155
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
const resultado =
|
|
159
|
-
|
|
156
|
+
async function combinar(interpretador, primeiroIteravel, segundoIteravel) {
|
|
157
|
+
const resolverEValidar = (iteravel, nomeParametro) => {
|
|
158
|
+
const valor = interpretador.resolverValor(iteravel);
|
|
159
|
+
const itens = new iteravel_1.Iteravel(valor).elementos;
|
|
160
|
+
if (itens.length === 0 &&
|
|
161
|
+
valor !== '' &&
|
|
162
|
+
!(valor instanceof construtos_1.TuplaN) &&
|
|
163
|
+
!Array.isArray(valor)) {
|
|
164
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
165
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
166
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
167
|
+
}, `Parâmetro inválido. O ${nomeParametro} parâmetro deve ser um iterável.`);
|
|
168
|
+
}
|
|
169
|
+
return itens;
|
|
170
|
+
};
|
|
171
|
+
const itensPrimeiroIteravel = resolverEValidar(primeiroIteravel, 'primeiro');
|
|
172
|
+
const itensSegundoIteravel = resolverEValidar(segundoIteravel, 'segundo');
|
|
173
|
+
const resultado = [];
|
|
174
|
+
const tamanhoMinimo = Math.min(itensPrimeiroIteravel.length, itensSegundoIteravel.length);
|
|
175
|
+
for (let i = 0; i < tamanhoMinimo; i++) {
|
|
176
|
+
resultado.push(new construtos_1.TuplaN(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual, [
|
|
177
|
+
new construtos_1.Literal(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual, itensPrimeiroIteravel[i], typeof itensPrimeiroIteravel[i] === 'string' ? 'texto' : 'qualquer'),
|
|
178
|
+
new construtos_1.Literal(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual, itensSegundoIteravel[i], typeof itensSegundoIteravel[i] === 'string' ? 'texto' : 'qualquer')
|
|
179
|
+
]));
|
|
180
|
+
}
|
|
181
|
+
return resultado;
|
|
182
|
+
}
|
|
183
|
+
;
|
|
184
|
+
async function contar(interpretador, iteravel, elemento) {
|
|
185
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
186
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
187
|
+
if (itens.length === 0 &&
|
|
188
|
+
valorIteravel !== '' &&
|
|
189
|
+
!(valorIteravel instanceof construtos_1.TuplaN) &&
|
|
190
|
+
!Array.isArray(valorIteravel)) {
|
|
191
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
192
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
193
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
194
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
195
|
+
}
|
|
196
|
+
const elementoResolvido = interpretador.resolverValor(elemento);
|
|
197
|
+
return itens.filter(item => item === elementoResolvido).length;
|
|
160
198
|
}
|
|
199
|
+
;
|
|
161
200
|
/**
|
|
162
|
-
*
|
|
163
|
-
*
|
|
201
|
+
* Retorna o primeiro elemento de um iterável que satisfaça a condição definida na função de pesquisa.
|
|
202
|
+
* A execução é interrompida assim que o elemento for encontrado.
|
|
164
203
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
165
|
-
* @param {
|
|
166
|
-
* @param {
|
|
167
|
-
* @returns {Promise<any>}
|
|
204
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
205
|
+
* @param {any} funcaoPesquisa A função que define a condição de busca.
|
|
206
|
+
* @returns {Promise<any>} O primeiro elemento encontrado, ou nulo caso nenhum elemento satisfaça a condição.
|
|
168
207
|
*/
|
|
169
|
-
async function encontrar(interpretador,
|
|
170
|
-
const
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (!Array.isArray(valorVetor)) {
|
|
175
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
176
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
208
|
+
async function encontrar(interpretador, iteravel, funcaoPesquisa) {
|
|
209
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
210
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
211
|
+
if (itens.length === 0) {
|
|
212
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
177
213
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
178
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um vetor.'));
|
|
179
|
-
}
|
|
180
|
-
if (valorFuncaoPesquisa.constructor !== estruturas_1.DeleguaFuncao) {
|
|
181
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
182
214
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
215
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
216
|
+
}
|
|
217
|
+
const valorFuncao = interpretador.resolverValor(funcaoPesquisa);
|
|
218
|
+
if (!(valorFuncao instanceof estruturas_1.DeleguaFuncao) &&
|
|
219
|
+
!(valorFuncao instanceof funcao_padrao_1.FuncaoPadrao)) {
|
|
220
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
183
221
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
184
|
-
|
|
222
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
223
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
185
224
|
}
|
|
186
|
-
for (
|
|
187
|
-
if (await
|
|
188
|
-
|
|
225
|
+
for (const item of itens) {
|
|
226
|
+
if (await valorFuncao.chamar(interpretador, item, {
|
|
227
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
228
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
229
|
+
})) {
|
|
230
|
+
return item;
|
|
189
231
|
}
|
|
190
232
|
}
|
|
191
233
|
return null;
|
|
192
234
|
}
|
|
193
235
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
236
|
+
* Retorna o índice do primeiro elemento de um iterável que satisfaça a condição definida na função de pesquisa.
|
|
237
|
+
* A execução é interrompida assim que o elemento for encontrado.
|
|
196
238
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
197
|
-
* @param {
|
|
198
|
-
* @param {
|
|
199
|
-
* @returns {Promise<number>} O
|
|
239
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
240
|
+
* @param {any} funcaoPesquisa A função que define a condição de busca.
|
|
241
|
+
* @returns {Promise<number>} O índice do primeiro elemento encontrado, ou -1 caso nenhum elemento satisfaça a condição.
|
|
200
242
|
*/
|
|
201
|
-
async function encontrar_indice(interpretador,
|
|
202
|
-
const
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (!Array.isArray(valorVetor)) {
|
|
207
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
208
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
243
|
+
async function encontrar_indice(interpretador, iteravel, funcaoPesquisa) {
|
|
244
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
245
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
246
|
+
if (itens.length === 0) {
|
|
247
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
209
248
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
210
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um vetor.'));
|
|
211
|
-
}
|
|
212
|
-
if (valorFuncaoPesquisa.constructor !== estruturas_1.DeleguaFuncao) {
|
|
213
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
214
249
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
250
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
251
|
+
}
|
|
252
|
+
const valorFuncao = interpretador.resolverValor(funcaoPesquisa);
|
|
253
|
+
if (!(valorFuncao instanceof estruturas_1.DeleguaFuncao) &&
|
|
254
|
+
!(valorFuncao instanceof funcao_padrao_1.FuncaoPadrao)) {
|
|
255
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
215
256
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
216
|
-
|
|
257
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
258
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
217
259
|
}
|
|
218
|
-
for (let
|
|
219
|
-
if (await
|
|
220
|
-
|
|
260
|
+
for (let i = 0; i < itens.length; i++) {
|
|
261
|
+
if (await valorFuncao.chamar(interpretador, itens[i], {
|
|
262
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
263
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
264
|
+
})) {
|
|
265
|
+
return i;
|
|
221
266
|
}
|
|
222
267
|
}
|
|
223
268
|
return -1;
|
|
224
269
|
}
|
|
225
270
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
271
|
+
* Retorna o último elemento de um iterável que satisfaça a condição definida na função de pesquisa.
|
|
272
|
+
* A execução é interrompida assim que o elemento for encontrado.
|
|
228
273
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
229
|
-
* @param {
|
|
230
|
-
* @param {
|
|
231
|
-
* @returns {Promise<any>} O
|
|
274
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
275
|
+
* @param {any} funcaoPesquisa A função que define a condição de busca.
|
|
276
|
+
* @returns {Promise<any>} O último elemento encontrado, ou nulo caso nenhum elemento satisfaça a condição.
|
|
232
277
|
*/
|
|
233
|
-
async function encontrar_ultimo(interpretador,
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
if (!Array.isArray(valorVetor)) {
|
|
239
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
240
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
278
|
+
async function encontrar_ultimo(interpretador, iteravel, funcaoPesquisa) {
|
|
279
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
280
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
281
|
+
if (itens.length === 0) {
|
|
282
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
241
283
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
242
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um vetor.'));
|
|
243
|
-
}
|
|
244
|
-
if (valorFuncaoPesquisa.constructor !== estruturas_1.DeleguaFuncao) {
|
|
245
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
246
284
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
285
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
286
|
+
}
|
|
287
|
+
const valorFuncao = interpretador.resolverValor(funcaoPesquisa);
|
|
288
|
+
if (!(valorFuncao instanceof estruturas_1.DeleguaFuncao) &&
|
|
289
|
+
!(valorFuncao instanceof funcao_padrao_1.FuncaoPadrao)) {
|
|
290
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
247
291
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
248
|
-
|
|
292
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
293
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
249
294
|
}
|
|
250
|
-
for (let
|
|
251
|
-
if (await
|
|
252
|
-
|
|
295
|
+
for (let i = itens.length - 1; i >= 0; i--) {
|
|
296
|
+
if (await valorFuncao.chamar(interpretador, [itens[i]], {
|
|
297
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
298
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
299
|
+
})) {
|
|
300
|
+
return itens[i];
|
|
253
301
|
}
|
|
254
302
|
}
|
|
255
303
|
return null;
|
|
256
304
|
}
|
|
257
305
|
/**
|
|
258
|
-
*
|
|
306
|
+
* Retorna o índice do último elemento de um iterável que satisfaça a condição definida na função de pesquisa.
|
|
307
|
+
* A execução é interrompida assim que o elemento for encontrado.
|
|
259
308
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
260
|
-
* @param {
|
|
261
|
-
* @param {
|
|
262
|
-
* @returns {Promise<number>} O
|
|
309
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
310
|
+
* @param {any} funcaoPesquisa A função que define a condição de busca.
|
|
311
|
+
* @returns {Promise<number>} O índice do último elemento encontrado, ou -1 caso nenhum elemento satisfaça a condição.
|
|
263
312
|
*/
|
|
264
|
-
async function encontrar_ultimo_indice(interpretador,
|
|
265
|
-
const
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (!Array.isArray(valorVetor)) {
|
|
270
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
271
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
313
|
+
async function encontrar_ultimo_indice(interpretador, iteravel, funcaoPesquisa) {
|
|
314
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
315
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
316
|
+
if (itens.length === 0) {
|
|
317
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
272
318
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
273
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um vetor.'));
|
|
274
|
-
}
|
|
275
|
-
if (valorFuncaoPesquisa.constructor !== estruturas_1.DeleguaFuncao) {
|
|
276
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
277
319
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
320
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
321
|
+
}
|
|
322
|
+
const valorFuncao = interpretador.resolverValor(funcaoPesquisa);
|
|
323
|
+
if (!(valorFuncao instanceof estruturas_1.DeleguaFuncao) &&
|
|
324
|
+
!(valorFuncao instanceof funcao_padrao_1.FuncaoPadrao)) {
|
|
325
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
278
326
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
279
|
-
|
|
327
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
328
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
280
329
|
}
|
|
281
|
-
for (let
|
|
282
|
-
if (await
|
|
283
|
-
|
|
330
|
+
for (let i = itens.length - 1; i >= 0; i--) {
|
|
331
|
+
if (await valorFuncao.chamar(interpretador, itens[i], {
|
|
332
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
333
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
334
|
+
})) {
|
|
335
|
+
return i;
|
|
284
336
|
}
|
|
285
337
|
}
|
|
286
|
-
return
|
|
338
|
+
return -1;
|
|
287
339
|
}
|
|
288
340
|
/**
|
|
289
|
-
*
|
|
290
|
-
* @param interpretador
|
|
291
|
-
* @param
|
|
292
|
-
* @param
|
|
293
|
-
* @returns
|
|
341
|
+
* Retorna os elementos que satisfazem a condição definida na função de pesquisa.
|
|
342
|
+
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
343
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
344
|
+
* @param {any} funcaoPesquisa A função que define a condição de busca.
|
|
345
|
+
* @returns {Promise<any[]>} Os elementos que satisfazem a função de pesquisa.
|
|
294
346
|
*/
|
|
295
|
-
async function filtrar_por(interpretador,
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
347
|
+
async function filtrar_por(interpretador, iteravel, funcaoPesquisa) {
|
|
348
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
349
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
350
|
+
if (itens.length === 0) {
|
|
351
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
299
352
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
300
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função filtrarPor() não pode ser nulo.'));
|
|
301
|
-
const valorVetor = vetor.hasOwnProperty('valor') ? vetor.valor : vetor;
|
|
302
|
-
const valorFuncaoFiltragem = funcaoFiltragem.hasOwnProperty('valor')
|
|
303
|
-
? funcaoFiltragem.valor
|
|
304
|
-
: funcaoFiltragem;
|
|
305
|
-
if (!Array.isArray(valorVetor)) {
|
|
306
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
307
353
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
308
|
-
|
|
309
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função filtrarPor() deve ser um vetor.'));
|
|
354
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
310
355
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
356
|
+
const valorFuncao = interpretador.resolverValor(funcaoPesquisa);
|
|
357
|
+
if (!(valorFuncao instanceof estruturas_1.DeleguaFuncao) &&
|
|
358
|
+
!(valorFuncao instanceof funcao_padrao_1.FuncaoPadrao)) {
|
|
359
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
314
360
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
315
|
-
|
|
361
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
362
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
316
363
|
}
|
|
317
364
|
const resultados = [];
|
|
318
|
-
for (
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
365
|
+
for (const item of itens) {
|
|
366
|
+
const retornoFuncao = await valorFuncao.chamar(interpretador, item, {
|
|
367
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
368
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
369
|
+
});
|
|
370
|
+
if (retornoFuncao === null || retornoFuncao === undefined)
|
|
323
371
|
continue;
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (deveRetornarValor === false)
|
|
372
|
+
const passouPeloFiltro = retornoFuncao.valorRetornado.valor;
|
|
373
|
+
if (passouPeloFiltro === false)
|
|
327
374
|
continue;
|
|
328
|
-
resultados.push(
|
|
375
|
+
resultados.push(item);
|
|
329
376
|
}
|
|
330
377
|
return resultados;
|
|
331
378
|
}
|
|
332
379
|
/**
|
|
333
|
-
*
|
|
380
|
+
* Verifica se um valor específico está incluído dentro de um iterável.
|
|
334
381
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
335
|
-
* @param {
|
|
336
|
-
* @param valor
|
|
337
|
-
* @returns
|
|
382
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
383
|
+
* @param {any} valor O valor a ser buscado.
|
|
384
|
+
* @returns {Promise<boolean>} verdadeiro se o valor for encontrado, falso caso contrário.
|
|
338
385
|
*/
|
|
339
|
-
async function incluido(interpretador,
|
|
340
|
-
const
|
|
341
|
-
const
|
|
342
|
-
if (
|
|
343
|
-
|
|
344
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
386
|
+
async function incluido(interpretador, iteravel, valor) {
|
|
387
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
388
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
389
|
+
if (itens.length === 0) {
|
|
390
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
345
391
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
346
|
-
|
|
392
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
393
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
347
394
|
}
|
|
348
|
-
|
|
349
|
-
|
|
395
|
+
const valorResolvido = interpretador.resolverValor(valor);
|
|
396
|
+
for (const item of itens) {
|
|
397
|
+
if (item == valorResolvido)
|
|
350
398
|
return true;
|
|
351
|
-
}
|
|
352
399
|
}
|
|
353
400
|
return false;
|
|
354
401
|
}
|
|
355
|
-
function validacao_comum_numeros(interpretador, valorParaConverter) {
|
|
356
|
-
if (isNaN(valorParaConverter)) {
|
|
357
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
358
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
359
|
-
linha: interpretador.linhaDeclaracaoAtual,
|
|
360
|
-
}, 'Valor não parece ser um número. Somente números ou textos com números podem ser convertidos para inteiro.'));
|
|
361
|
-
}
|
|
362
|
-
if (!/^(-)?\d+(\.\d+)?$/.test(valorParaConverter)) {
|
|
363
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
364
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
365
|
-
linha: interpretador.linhaDeclaracaoAtual,
|
|
366
|
-
}, 'Valor não parece estar estruturado como um número (texto vazio, falso ou não definido). Somente números ou textos com números podem ser convertidos para inteiro.'));
|
|
367
|
-
}
|
|
368
|
-
return null;
|
|
369
|
-
}
|
|
370
402
|
/**
|
|
371
403
|
* Converte um valor em um número inteiro.
|
|
372
404
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
373
|
-
* @param {
|
|
374
|
-
* @returns {Promise<
|
|
405
|
+
* @param {any} valor O valor a ser convertido.
|
|
406
|
+
* @returns {Promise<number>} o resultado da conversão.
|
|
375
407
|
*/
|
|
376
|
-
async function inteiro(interpretador,
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
? valorParaConverter.valor
|
|
381
|
-
: valorParaConverter;
|
|
382
|
-
const resultadoValidacao = validacao_comum_numeros(interpretador, valor);
|
|
383
|
-
return resultadoValidacao || Promise.resolve(parseInt(valor));
|
|
408
|
+
async function inteiro(interpretador, valor) {
|
|
409
|
+
const valorResolvido = interpretador.resolverValor(valor);
|
|
410
|
+
validacao_comum_numeros(interpretador, valorResolvido, 'inteiro');
|
|
411
|
+
return parseInt(String(valorResolvido), 10);
|
|
384
412
|
}
|
|
385
413
|
/**
|
|
386
414
|
* Cria um vetor com números inteiros no intervalo especificado.
|
|
387
415
|
* O valor inicial é inclusivo e o valor final é exclusivo.
|
|
388
416
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
389
|
-
* @param {VariavelInterface | number} valorInicial O valor inicial (inclusivo).
|
|
417
|
+
* @param {VariavelInterface | number} valorInicial O valor inicial (inclusivo) ou o limite final se for o único parâmetro.
|
|
390
418
|
* @param {VariavelInterface | number} valorFinal O valor final (exclusivo).
|
|
391
419
|
* @param {VariavelInterface | number} valorPasso O valor do passo.
|
|
392
420
|
* @returns {Promise<number[]>} Um vetor com os números no intervalo.
|
|
393
421
|
*/
|
|
394
422
|
async function intervalo(interpretador, valorInicial, valorFinal, valorPasso) {
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
if (segundoParam === undefined || segundoParam === null) {
|
|
403
|
-
if (typeof primeiroParam !== 'number' || isNaN(primeiroParam)) {
|
|
404
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
405
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
423
|
+
const validarEConverter = (valorBruto, mensagemErro) => {
|
|
424
|
+
if (valorBruto === undefined || valorBruto === null)
|
|
425
|
+
return undefined;
|
|
426
|
+
const valorResolvido = interpretador.resolverValor(valorBruto);
|
|
427
|
+
if (typeof valorResolvido !== 'number' ||
|
|
428
|
+
Number.isNaN(valorResolvido)) {
|
|
429
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
406
430
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
407
|
-
}, 'O parâmetro deve ser do tipo número ou inteiro.'));
|
|
408
|
-
}
|
|
409
|
-
inicioInteiro = 0;
|
|
410
|
-
fimInteiro = Math.floor(primeiroParam);
|
|
411
|
-
}
|
|
412
|
-
// intervalo(inicio, parada) ou intervalo(inicio, parada, passo)
|
|
413
|
-
else {
|
|
414
|
-
if (typeof primeiroParam !== 'number' ||
|
|
415
|
-
isNaN(primeiroParam) ||
|
|
416
|
-
typeof segundoParam !== 'number' ||
|
|
417
|
-
isNaN(segundoParam)) {
|
|
418
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
419
431
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
420
|
-
|
|
421
|
-
}, 'Os parâmetros de início e fim devem ser do tipo número ou inteiro.'));
|
|
422
|
-
}
|
|
423
|
-
inicioInteiro = Math.floor(primeiroParam);
|
|
424
|
-
fimInteiro = Math.floor(segundoParam);
|
|
425
|
-
// Se há um terceiro parâmetro (passo)
|
|
426
|
-
if (terceiroParam !== undefined && terceiroParam !== null) {
|
|
427
|
-
if (typeof terceiroParam !== 'number' || isNaN(terceiroParam)) {
|
|
428
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
429
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
430
|
-
linha: interpretador.linhaDeclaracaoAtual,
|
|
431
|
-
}, 'O parâmetro de passo deve ser do tipo número ou inteiro.'));
|
|
432
|
-
}
|
|
433
|
-
passoInteiro = Math.floor(terceiroParam);
|
|
434
|
-
if (passoInteiro === 0) {
|
|
435
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
436
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
437
|
-
linha: interpretador.linhaDeclaracaoAtual,
|
|
438
|
-
}, 'O passo não pode ser zero.'));
|
|
439
|
-
}
|
|
432
|
+
}, mensagemErro);
|
|
440
433
|
}
|
|
434
|
+
return Math.floor(valorResolvido);
|
|
435
|
+
};
|
|
436
|
+
let inicio = validarEConverter(valorInicial, 'O parâmetro de início deve ser do tipo número ou inteiro.');
|
|
437
|
+
let fim = validarEConverter(valorFinal, 'O parâmetro de fim deve ser do tipo número ou inteiro.');
|
|
438
|
+
const passo = validarEConverter(valorPasso, 'O parâmetro de passo deve ser do tipo número ou inteiro.') ?? 1;
|
|
439
|
+
if (passo === 0) {
|
|
440
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
441
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
442
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
443
|
+
}, 'O passo não pode ser zero.');
|
|
444
|
+
}
|
|
445
|
+
if (fim === undefined) {
|
|
446
|
+
fim = inicio;
|
|
447
|
+
inicio = 0;
|
|
441
448
|
}
|
|
442
449
|
const resultado = [];
|
|
443
|
-
if (
|
|
444
|
-
for (let i =
|
|
450
|
+
if (passo > 0) {
|
|
451
|
+
for (let i = inicio; i < fim; i += passo) {
|
|
445
452
|
resultado.push(i);
|
|
446
453
|
}
|
|
447
454
|
}
|
|
448
455
|
else {
|
|
449
|
-
//
|
|
450
|
-
for (let i =
|
|
456
|
+
// Passo negativo
|
|
457
|
+
for (let i = inicio; i > fim; i += passo) {
|
|
451
458
|
resultado.push(i);
|
|
452
459
|
}
|
|
453
460
|
}
|
|
454
|
-
return
|
|
461
|
+
return resultado;
|
|
455
462
|
}
|
|
463
|
+
async function inverter(interpretador, iteravel) {
|
|
464
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
465
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
466
|
+
if (itens.length === 0 &&
|
|
467
|
+
valorIteravel !== '' &&
|
|
468
|
+
!(valorIteravel instanceof construtos_1.TuplaN) &&
|
|
469
|
+
!Array.isArray(valorIteravel)) {
|
|
470
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
471
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
472
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
473
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
474
|
+
}
|
|
475
|
+
const itensInvertidos = [...itens].reverse();
|
|
476
|
+
if (iteravel instanceof construtos_1.TuplaN) {
|
|
477
|
+
return new construtos_1.TuplaN(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual, itensInvertidos);
|
|
478
|
+
}
|
|
479
|
+
if (typeof valorIteravel === 'string') {
|
|
480
|
+
return itensInvertidos.join('');
|
|
481
|
+
}
|
|
482
|
+
return itensInvertidos;
|
|
483
|
+
}
|
|
484
|
+
;
|
|
456
485
|
/**
|
|
457
486
|
* Dado um vetor e, opcionalmente, um valor de início, retorna um vetor de dicionários,
|
|
458
487
|
* onde cada dicionário contém o índice e o valor correspondente do vetor original.
|
|
459
488
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
460
|
-
* @param {
|
|
461
|
-
* @param {
|
|
489
|
+
* @param {any} iteravel Um iterável.
|
|
490
|
+
* @param {any} [inicio] O valor inicial do contador de índices (opcional, padrão 0).
|
|
462
491
|
* @returns {Promise<any[]>} Um vetor de dicionários com índice e valor.
|
|
463
492
|
*/
|
|
464
|
-
async function enumerar(interpretador,
|
|
465
|
-
if (
|
|
466
|
-
|
|
467
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
493
|
+
async function enumerar(interpretador, iteravel, inicio) {
|
|
494
|
+
if (iteravel === null || iteravel === undefined) {
|
|
495
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
468
496
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
469
|
-
|
|
497
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
498
|
+
}, 'Parâmetro inválido. O primeiro parâmetro da função enumerar() não pode ser nulo.');
|
|
470
499
|
}
|
|
471
|
-
|
|
472
|
-
|
|
500
|
+
const valorInicioResolvido = interpretador.resolverValor(inicio);
|
|
501
|
+
if (valorInicioResolvido !== undefined &&
|
|
502
|
+
(typeof valorInicioResolvido !== 'number' || Number.isNaN(valorInicioResolvido))) {
|
|
503
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
504
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
473
505
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
506
|
+
}, 'O parâmetro de início deve ser do tipo número ou inteiro.');
|
|
507
|
+
}
|
|
508
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
509
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
510
|
+
if (itens.length === 0 &&
|
|
511
|
+
valorIteravel !== '' &&
|
|
512
|
+
!Array.isArray(valorIteravel) &&
|
|
513
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
514
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
474
515
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
475
|
-
|
|
516
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
517
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
476
518
|
}
|
|
477
|
-
const
|
|
478
|
-
|
|
519
|
+
const inicioInteiro = typeof valorInicioResolvido === 'number'
|
|
520
|
+
? Math.floor(valorInicioResolvido)
|
|
521
|
+
: 0;
|
|
479
522
|
const resultados = [];
|
|
480
|
-
for (let i =
|
|
481
|
-
|
|
523
|
+
for (let i = 0; i < itens.length; i++) {
|
|
524
|
+
const valorResolvido = interpretador.resolverValor(itens[i]);
|
|
525
|
+
resultados.push({
|
|
526
|
+
indice: i + inicioInteiro,
|
|
527
|
+
valor: valorResolvido
|
|
528
|
+
});
|
|
482
529
|
}
|
|
483
|
-
return
|
|
530
|
+
return resultados;
|
|
484
531
|
}
|
|
485
532
|
/**
|
|
486
|
-
* Dado um
|
|
487
|
-
* passando como argumento cada elemento do
|
|
533
|
+
* Dado um iterável e uma função de mapeamento, executa a função de mapeamento
|
|
534
|
+
* passando como argumento cada elemento do iterável.
|
|
488
535
|
* @param interpretador A instância do interpretador.
|
|
489
|
-
* @param
|
|
536
|
+
* @param iteravel O iteravel a ser mapeado.
|
|
490
537
|
* @param funcaoMapeamento A função de mapeamento.
|
|
491
|
-
* @returns O resultado acumulado
|
|
538
|
+
* @returns {Promise<any[]>} O resultado acumulado em forma de vetor.
|
|
492
539
|
*/
|
|
493
|
-
async function mapear(interpretador,
|
|
494
|
-
if (
|
|
495
|
-
|
|
540
|
+
async function mapear(interpretador, iteravel, funcaoMapeamento) {
|
|
541
|
+
if (iteravel === null || iteravel === undefined) {
|
|
542
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
543
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
496
544
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
545
|
+
}, 'Parâmetro inválido. O primeiro parâmetro da função mapear() não pode ser nulo.');
|
|
546
|
+
}
|
|
547
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
548
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
549
|
+
if (itens.length === 0 &&
|
|
550
|
+
valorIteravel !== '' &&
|
|
551
|
+
!Array.isArray(valorIteravel) &&
|
|
552
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
553
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
497
554
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
555
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
556
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
557
|
+
}
|
|
558
|
+
const valorFuncao = interpretador.resolverValor(funcaoMapeamento);
|
|
559
|
+
const ehUmaFuncao = valorFuncao instanceof estruturas_1.DeleguaFuncao ||
|
|
560
|
+
valorFuncao instanceof funcao_padrao_1.FuncaoPadrao;
|
|
561
|
+
if (!valorFuncao || !ehUmaFuncao) {
|
|
562
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
563
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
564
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
565
|
+
}, 'Parâmetro inválido. O segundo parâmetro da função mapear() deve ser uma função.');
|
|
566
|
+
}
|
|
501
567
|
const resultados = [];
|
|
502
|
-
for (
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
568
|
+
for (const item of itens) {
|
|
569
|
+
const retornoFuncao = await valorFuncao.chamar(interpretador, [item], {
|
|
570
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
571
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
572
|
+
});
|
|
573
|
+
if (retornoFuncao !== null && retornoFuncao !== undefined) {
|
|
574
|
+
if (retornoFuncao.hasOwnProperty('valorRetornado')) {
|
|
575
|
+
if (retornoFuncao.valorRetornado instanceof quebras_1.RetornoQuebra) {
|
|
576
|
+
resultados.push(retornoFuncao.valorRetornado.valor);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
else if (retornoFuncao instanceof quebras_1.RetornoQuebra) {
|
|
580
|
+
resultados.push(retornoFuncao.valor);
|
|
581
|
+
}
|
|
513
582
|
}
|
|
514
|
-
resultados.push(informacoesRetorno.valorRetornado.valor);
|
|
515
583
|
}
|
|
516
584
|
return resultados;
|
|
517
585
|
}
|
|
518
586
|
/**
|
|
519
|
-
* Encontra o maior número dentro de um
|
|
587
|
+
* Encontra o maior número dentro de um iterável.
|
|
520
588
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
521
|
-
* @param {
|
|
522
|
-
* @returns {Promise<
|
|
589
|
+
* @param {any} iteravel O iterável a ser inspecionado.
|
|
590
|
+
* @returns {Promise<any>} O maior elemento encontrado.
|
|
523
591
|
*/
|
|
524
|
-
async function maximo(interpretador,
|
|
525
|
-
if (
|
|
526
|
-
|
|
527
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
592
|
+
async function maximo(interpretador, iteravel) {
|
|
593
|
+
if (iteravel === null || iteravel === undefined) {
|
|
594
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
528
595
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
529
|
-
}, 'Parâmetro inválido. O parâmetro da função maximo() não pode ser nulo.'));
|
|
530
|
-
}
|
|
531
|
-
const valorVetor = interpretador.resolverValor(vetor);
|
|
532
|
-
if (!Array.isArray(valorVetor)) {
|
|
533
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
534
596
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
535
|
-
|
|
536
|
-
}, 'Parâmetro inválido. O parâmetro da função maximo() deve ser um vetor.'));
|
|
597
|
+
}, 'Parâmetro inválido. O parâmetro da função maximo() não pode ser nulo.');
|
|
537
598
|
}
|
|
538
|
-
|
|
539
|
-
|
|
599
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
600
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
601
|
+
if (itens.length === 0 &&
|
|
602
|
+
valorIteravel !== '' &&
|
|
603
|
+
!Array.isArray(valorIteravel) &&
|
|
604
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
605
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
606
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
540
607
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
608
|
+
}, 'Parâmetro inválido. A função maximo() espera um iterável.');
|
|
609
|
+
}
|
|
610
|
+
if (itens.length === 0) {
|
|
611
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
541
612
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
542
|
-
|
|
613
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
614
|
+
}, 'Parâmetro inválido. O iterável não pode estar vazio.');
|
|
543
615
|
}
|
|
544
|
-
let maiorValor =
|
|
616
|
+
let maiorValor = interpretador.resolverValor(itens[0]);
|
|
545
617
|
try {
|
|
546
|
-
for (
|
|
547
|
-
const
|
|
548
|
-
if (compararElementosRecursivamente(
|
|
549
|
-
maiorValor =
|
|
618
|
+
for (const item of itens) {
|
|
619
|
+
const itemResolvido = interpretador.resolverValor(item);
|
|
620
|
+
if (compararElementosRecursivamente(itemResolvido, maiorValor) > 0) {
|
|
621
|
+
maiorValor = itemResolvido;
|
|
550
622
|
}
|
|
551
623
|
}
|
|
552
624
|
}
|
|
553
625
|
catch (erro) {
|
|
554
|
-
|
|
555
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
626
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
556
627
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
557
|
-
|
|
628
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
629
|
+
}, 'Não é possível comparar elementos de tipos incompatíveis dentro do iterável.');
|
|
558
630
|
}
|
|
559
|
-
return
|
|
631
|
+
return maiorValor;
|
|
560
632
|
}
|
|
561
633
|
/**
|
|
562
|
-
* Encontra o menor número dentro de um
|
|
634
|
+
* Encontra o menor número dentro de um iterável.
|
|
563
635
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
564
|
-
* @param {
|
|
565
|
-
* @returns {Promise<
|
|
636
|
+
* @param {any} iteravel O iterável a ser inspecionado.
|
|
637
|
+
* @returns {Promise<any>} O menor elemento encontrado.
|
|
566
638
|
*/
|
|
567
|
-
async function minimo(interpretador,
|
|
568
|
-
if (
|
|
569
|
-
|
|
570
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
639
|
+
async function minimo(interpretador, iteravel) {
|
|
640
|
+
if (iteravel === null || iteravel === undefined) {
|
|
641
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
571
642
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
572
|
-
}, 'Parâmetro inválido. O parâmetro da função minimo() não pode ser nulo.'));
|
|
573
|
-
}
|
|
574
|
-
const valorVetor = interpretador.resolverValor(vetor);
|
|
575
|
-
if (!Array.isArray(valorVetor)) {
|
|
576
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
577
643
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
578
|
-
|
|
579
|
-
}, 'Parâmetro inválido. O parâmetro da função minimo() deve ser um vetor.'));
|
|
644
|
+
}, 'Parâmetro inválido. O parâmetro da função minimo() não pode ser nulo.');
|
|
580
645
|
}
|
|
581
|
-
|
|
582
|
-
|
|
646
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
647
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
648
|
+
if (itens.length === 0 &&
|
|
649
|
+
valorIteravel !== '' &&
|
|
650
|
+
!Array.isArray(valorIteravel) &&
|
|
651
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
652
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
653
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
583
654
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
655
|
+
}, 'Parâmetro inválido. A função minimo() espera um iterável.');
|
|
656
|
+
}
|
|
657
|
+
if (itens.length === 0) {
|
|
658
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
584
659
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
585
|
-
|
|
660
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
661
|
+
}, 'Parâmetro inválido. O iterável não pode estar vazio.');
|
|
586
662
|
}
|
|
587
|
-
let menorValor =
|
|
663
|
+
let menorValor = interpretador.resolverValor(itens[0]);
|
|
588
664
|
try {
|
|
589
|
-
for (
|
|
590
|
-
const
|
|
591
|
-
if (compararElementosRecursivamente(
|
|
592
|
-
menorValor =
|
|
665
|
+
for (const item of itens) {
|
|
666
|
+
const itemResolvido = interpretador.resolverValor(item);
|
|
667
|
+
if (compararElementosRecursivamente(itemResolvido, menorValor) < 0) {
|
|
668
|
+
menorValor = itemResolvido;
|
|
593
669
|
}
|
|
594
670
|
}
|
|
595
671
|
}
|
|
596
672
|
catch (erro) {
|
|
597
|
-
|
|
598
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
673
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
599
674
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
600
|
-
|
|
675
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
676
|
+
}, 'Não é possível comparar elementos de tipos incompatíveis dentro do iterável.');
|
|
601
677
|
}
|
|
602
|
-
return
|
|
678
|
+
return menorValor;
|
|
603
679
|
}
|
|
604
680
|
/**
|
|
605
|
-
*
|
|
681
|
+
* Ordena os elementos de um iterável
|
|
606
682
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
607
|
-
* @param {
|
|
608
|
-
* @returns {Promise<any>}
|
|
683
|
+
* @param {any} iteravel O iterável a ser ordenado.
|
|
684
|
+
* @returns {Promise<any[]>} Um novo vetor com os elementos ordenados.
|
|
609
685
|
*/
|
|
610
|
-
async function
|
|
611
|
-
if (
|
|
612
|
-
return Promise.resolve(0);
|
|
613
|
-
const valor = valorParaConverter.hasOwnProperty('valor')
|
|
614
|
-
? valorParaConverter.valor
|
|
615
|
-
: valorParaConverter;
|
|
616
|
-
const resultadoValidacao = validacao_comum_numeros(interpretador, valor);
|
|
617
|
-
return resultadoValidacao || Promise.resolve(Number(valor));
|
|
618
|
-
}
|
|
619
|
-
/**
|
|
620
|
-
*
|
|
621
|
-
* @param vetor
|
|
622
|
-
* @returns
|
|
623
|
-
*/
|
|
624
|
-
async function ordenar(interpretador, vetor) {
|
|
625
|
-
if (vetor === null || vetor === undefined)
|
|
686
|
+
async function ordenar(interpretador, iteravel) {
|
|
687
|
+
if (iteravel === null || iteravel === undefined) {
|
|
626
688
|
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
627
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
628
689
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
629
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função ordenar() não pode ser nulo.');
|
|
630
|
-
const objeto = vetor.hasOwnProperty('valor') ? vetor.valor : vetor;
|
|
631
|
-
if (!Array.isArray(objeto)) {
|
|
632
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
633
690
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
691
|
+
}, 'Parâmetro inválido. O primeiro parâmetro da função ordenar() não pode ser nulo.');
|
|
692
|
+
}
|
|
693
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
694
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
695
|
+
if (itens.length === 0 &&
|
|
696
|
+
valorIteravel !== '' &&
|
|
697
|
+
!Array.isArray(valorIteravel) &&
|
|
698
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
699
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
634
700
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
635
|
-
|
|
701
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
702
|
+
}, 'Parâmetro inválido. A função ordenar() espera um iterável.');
|
|
636
703
|
}
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
if (
|
|
643
|
-
|
|
644
|
-
trocado = true;
|
|
704
|
+
const itensParaOrdenar = [...itens];
|
|
705
|
+
try {
|
|
706
|
+
itensParaOrdenar.sort((a, b) => {
|
|
707
|
+
const valorA = interpretador.resolverValor(a);
|
|
708
|
+
const valorB = interpretador.resolverValor(b);
|
|
709
|
+
if (typeof valorA === 'string' && typeof valorB === 'string') {
|
|
710
|
+
return valorA.localeCompare(valorB);
|
|
645
711
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
*
|
|
652
|
-
* @param interpretador
|
|
653
|
-
* @param vetor
|
|
654
|
-
* @param funcaoFiltragem
|
|
655
|
-
* @returns
|
|
656
|
-
*/
|
|
657
|
-
async function para_cada(interpretador, vetor, funcaoFiltragem) {
|
|
658
|
-
if (vetor === null || vetor === undefined)
|
|
659
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
660
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
712
|
+
return compararElementosRecursivamente(valorA, valorB);
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
catch (erro) {
|
|
716
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
661
717
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const valorFuncaoFiltragem = interpretador.resolverValor(funcaoFiltragem);
|
|
665
|
-
for (let indice = 0; indice < valorVetor.length; ++indice) {
|
|
666
|
-
await valorFuncaoFiltragem.chamar(interpretador, [valorVetor[indice]]);
|
|
718
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
719
|
+
}, 'Não é possível ordenar um iterável que contenha elementos de tipos incompatíveis.');
|
|
667
720
|
}
|
|
721
|
+
return itensParaOrdenar;
|
|
668
722
|
}
|
|
669
723
|
/**
|
|
670
|
-
*
|
|
671
|
-
* @param interpretador
|
|
672
|
-
* @param
|
|
673
|
-
* @param
|
|
674
|
-
* @returns
|
|
724
|
+
* Executa uma função para cada elemento do iterável.
|
|
725
|
+
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
726
|
+
* @param {any} iteravel O iterável a ser percorrido.
|
|
727
|
+
* @param {any} funcaoExecucao A função que será chamada para cada elemento.
|
|
728
|
+
* @returns {Promise<void>}
|
|
675
729
|
*/
|
|
676
|
-
async function
|
|
677
|
-
if (
|
|
678
|
-
|
|
679
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
730
|
+
async function para_cada(interpretador, iteravel, funcaoExecucao) {
|
|
731
|
+
if (iteravel === null || iteravel === undefined) {
|
|
732
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
680
733
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
681
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função primeiroEmCondicao() não pode ser nulo.'));
|
|
682
|
-
const valorVetor = interpretador.resolverValor(vetor);
|
|
683
|
-
const valorFuncaoFiltragem = interpretador.resolverValor(funcaoFiltragem);
|
|
684
|
-
if (!Array.isArray(valorVetor)) {
|
|
685
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
686
734
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
687
|
-
|
|
688
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função primeiroEmCondicao() deve ser um vetor.'));
|
|
735
|
+
}, 'Parâmetro inválido. O primeiro parâmetro da função para_cada() não pode ser nulo.');
|
|
689
736
|
}
|
|
690
|
-
|
|
691
|
-
|
|
737
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
738
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
739
|
+
if (itens.length === 0 &&
|
|
740
|
+
valorIteravel !== '' &&
|
|
741
|
+
!Array.isArray(valorIteravel) &&
|
|
742
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
743
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
744
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
692
745
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
746
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
747
|
+
}
|
|
748
|
+
const valorFuncao = interpretador.resolverValor(funcaoExecucao);
|
|
749
|
+
const ehUmaFuncao = valorFuncao instanceof estruturas_1.DeleguaFuncao ||
|
|
750
|
+
valorFuncao instanceof funcao_padrao_1.FuncaoPadrao;
|
|
751
|
+
if (!valorFuncao || !ehUmaFuncao) {
|
|
752
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
693
753
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
694
|
-
|
|
754
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
755
|
+
}, 'Parâmetro inválido. O segundo parâmetro da função para_cada() deve ser uma função.');
|
|
695
756
|
}
|
|
696
|
-
for (
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
return valorResolvido;
|
|
702
|
-
}
|
|
757
|
+
for (const item of itens) {
|
|
758
|
+
await valorFuncao.chamar(interpretador, [item], {
|
|
759
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
760
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
761
|
+
});
|
|
703
762
|
}
|
|
704
|
-
return undefined;
|
|
705
763
|
}
|
|
706
764
|
/**
|
|
707
|
-
*
|
|
708
|
-
* @param interpretador
|
|
709
|
-
* @param
|
|
710
|
-
* @returns
|
|
765
|
+
* Converte um valor em um número real (ponto flutuante).
|
|
766
|
+
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
767
|
+
* @param {any} valor O valor a ser convertido.
|
|
768
|
+
* @returns {Promise<number>} O resultado da conversão.
|
|
711
769
|
*/
|
|
712
|
-
async function real(interpretador,
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
if (!/^(-)?\d+(\.\d+)?$/.test(valor)) {
|
|
717
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
718
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
719
|
-
linha: interpretador.linhaDeclaracaoAtual,
|
|
720
|
-
}, 'Valor não parece estar estruturado como um número (texto/valor vazio, falso ou não definido). Somente números ou textos com números podem ser convertidos para real.'));
|
|
721
|
-
}
|
|
722
|
-
return Promise.resolve(parseFloat(valor));
|
|
770
|
+
async function real(interpretador, valor) {
|
|
771
|
+
const valorResolvido = interpretador.resolverValor(valor);
|
|
772
|
+
validacao_comum_numeros(interpretador, valorResolvido, 'real');
|
|
773
|
+
return parseFloat(String(valorResolvido));
|
|
723
774
|
}
|
|
724
775
|
/**
|
|
725
|
-
*
|
|
726
|
-
* @param interpretador
|
|
727
|
-
* @param
|
|
728
|
-
* @param funcaoReducao
|
|
729
|
-
* @param valorInicial
|
|
730
|
-
* @returns
|
|
776
|
+
* Reduz um iterável a um único valor, executando uma função acumuladora em cada elemento.
|
|
777
|
+
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
778
|
+
* @param {any} iteravel O iterável a ser reduzido.
|
|
779
|
+
* @param {any} funcaoReducao A função que será chamada (acumulador, valorAtual).
|
|
780
|
+
* @param {any} [valorInicial=null] O valor inicial do acumulador.
|
|
781
|
+
* @returns {Promise<any>} O resultado acumulado.
|
|
731
782
|
*/
|
|
732
|
-
async function reduzir(interpretador,
|
|
733
|
-
const
|
|
734
|
-
const valorFuncaoReducao =
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
783
|
+
async function reduzir(interpretador, iteravel, funcaoReducao, valorInicial = null) {
|
|
784
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
785
|
+
const valorFuncaoReducao = interpretador.resolverValor(funcaoReducao);
|
|
786
|
+
const valorPadrao = interpretador.resolverValor(valorInicial);
|
|
787
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
788
|
+
if (itens.length === 0 &&
|
|
789
|
+
valorIteravel !== '' &&
|
|
790
|
+
!Array.isArray(valorIteravel) &&
|
|
791
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
792
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
743
793
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
744
|
-
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um vetor.'));
|
|
745
|
-
}
|
|
746
|
-
if (valorFuncaoReducao.constructor.name !== 'DeleguaFuncao') {
|
|
747
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
748
794
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
795
|
+
}, 'Parâmetro inválido. O primeiro parâmetro da função deve ser um iterável.');
|
|
796
|
+
}
|
|
797
|
+
const ehUmaFuncao = valorFuncaoReducao instanceof estruturas_1.DeleguaFuncao ||
|
|
798
|
+
valorFuncaoReducao instanceof funcao_padrao_1.FuncaoPadrao ||
|
|
799
|
+
typeof valorFuncaoReducao?.chamar === 'function';
|
|
800
|
+
if (!valorFuncaoReducao || !ehUmaFuncao) {
|
|
801
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
749
802
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
750
|
-
|
|
803
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
804
|
+
}, 'Parâmetro inválido. O segundo parâmetro da função deve ser uma função.');
|
|
751
805
|
}
|
|
752
|
-
// Se não houver valor inicial e vetor vazio, não é possível reduzir
|
|
753
806
|
if ((valorPadrao === null || valorPadrao === undefined) &&
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
807
|
+
itens.length === 0) {
|
|
808
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
757
809
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
758
|
-
|
|
810
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
811
|
+
}, 'Não é possível reduzir um iterável vazio sem valor inicial.');
|
|
759
812
|
}
|
|
760
813
|
let resultado = valorPadrao;
|
|
761
814
|
let inicio = 0;
|
|
762
815
|
if (resultado === null || resultado === undefined) {
|
|
763
|
-
resultado =
|
|
816
|
+
resultado = interpretador.resolverValor(itens[0]);
|
|
764
817
|
inicio = 1;
|
|
765
818
|
}
|
|
766
|
-
for (let
|
|
767
|
-
|
|
819
|
+
for (let i = inicio; i < itens.length; i++) {
|
|
820
|
+
const elementoAtual = interpretador.resolverValor(itens[i]);
|
|
821
|
+
const resultadoFuncao = await valorFuncaoReducao.chamar(interpretador, [resultado, elementoAtual], {
|
|
822
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
823
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
824
|
+
});
|
|
825
|
+
resultado = interpretador.resolverValor(resultadoFuncao);
|
|
768
826
|
}
|
|
769
827
|
return resultado;
|
|
770
828
|
}
|
|
771
829
|
/**
|
|
772
|
-
* Realiza a soma de todos os números dentro de um
|
|
830
|
+
* Realiza a soma de todos os números dentro de um iterável.
|
|
773
831
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
774
|
-
* @param {
|
|
775
|
-
* @returns {Promise<number>} A soma de todos os elementos
|
|
832
|
+
* @param {any} iteravel O iterável contendo os números.
|
|
833
|
+
* @returns {Promise<number>} A soma de todos os elementos.
|
|
776
834
|
*/
|
|
777
|
-
async function somar(interpretador,
|
|
778
|
-
if (
|
|
779
|
-
|
|
780
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
835
|
+
async function somar(interpretador, iteravel) {
|
|
836
|
+
if (iteravel === null || iteravel === undefined) {
|
|
837
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
781
838
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
782
|
-
}, 'Parâmetro inválido. O parâmetro da função somar() não pode ser nulo.'));
|
|
783
|
-
}
|
|
784
|
-
const valorVetor = interpretador.resolverValor(vetor);
|
|
785
|
-
if (!Array.isArray(valorVetor)) {
|
|
786
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
787
839
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
840
|
+
}, 'Parâmetro inválido. O parâmetro da função somar() não pode ser nulo.');
|
|
841
|
+
}
|
|
842
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
843
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
844
|
+
if (itens.length === 0 &&
|
|
845
|
+
valorIteravel !== '' &&
|
|
846
|
+
!Array.isArray(valorIteravel) &&
|
|
847
|
+
!(valorIteravel instanceof construtos_1.TuplaN)) {
|
|
848
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
788
849
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
789
|
-
|
|
850
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
851
|
+
}, 'Parâmetro inválido. O parâmetro da função somar() deve ser um iterável.');
|
|
790
852
|
}
|
|
791
|
-
if (
|
|
792
|
-
return
|
|
853
|
+
if (itens.length === 0)
|
|
854
|
+
return 0;
|
|
793
855
|
let somaDosElementos = 0;
|
|
794
|
-
for (
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
856
|
+
for (const item of itens) {
|
|
857
|
+
const itemResolvido = interpretador.resolverValor(item);
|
|
858
|
+
if (typeof itemResolvido !== 'number' || Number.isNaN(itemResolvido)) {
|
|
859
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
798
860
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
799
|
-
|
|
861
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
862
|
+
}, 'A função somar() aceita apenas iteráveis contendo números.');
|
|
800
863
|
}
|
|
801
|
-
somaDosElementos +=
|
|
864
|
+
somaDosElementos += itemResolvido;
|
|
802
865
|
}
|
|
803
|
-
return
|
|
866
|
+
return somaDosElementos;
|
|
804
867
|
}
|
|
805
868
|
/**
|
|
806
|
-
*
|
|
807
|
-
* @param
|
|
808
|
-
* @
|
|
869
|
+
* Retorna o tamanho do objeto, vetor, texto ou número de parâmetros de uma função/classe.
|
|
870
|
+
* * @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
871
|
+
* @param {any} valor O valor a ser inspecionado.
|
|
872
|
+
* @returns {Promise<number>} O tamanho.
|
|
809
873
|
*/
|
|
810
|
-
async function tamanho(interpretador,
|
|
811
|
-
const
|
|
812
|
-
if (
|
|
813
|
-
|
|
814
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
874
|
+
async function tamanho(interpretador, valor) {
|
|
875
|
+
const valorResolvido = interpretador.resolverValor(valor);
|
|
876
|
+
if (valorResolvido === null || valorResolvido === undefined) {
|
|
877
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
815
878
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
816
|
-
|
|
879
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
880
|
+
}, 'Não é possível obter o tamanho de um valor nulo ou não definido.');
|
|
817
881
|
}
|
|
818
|
-
if (
|
|
819
|
-
|
|
882
|
+
if (typeof valorResolvido === 'number' ||
|
|
883
|
+
typeof valorResolvido === 'boolean') {
|
|
884
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
885
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
820
886
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
887
|
+
}, `A função global tamanho() não funciona com ${typeof valorResolvido === 'number' ? 'números' : 'booleanos'}.`);
|
|
888
|
+
}
|
|
889
|
+
if (valorResolvido instanceof objeto_delegua_classe_1.ObjetoDeleguaClasse) {
|
|
890
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
821
891
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
822
|
-
|
|
892
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
893
|
+
}, 'A função global tamanho() não funciona com objetos complexos instanciados.');
|
|
823
894
|
}
|
|
824
|
-
if (
|
|
825
|
-
return
|
|
895
|
+
if (valorResolvido instanceof estruturas_1.DeleguaFuncao) {
|
|
896
|
+
return valorResolvido.declaracao.parametros.length;
|
|
826
897
|
}
|
|
827
|
-
if (
|
|
828
|
-
return
|
|
898
|
+
if (valorResolvido instanceof funcao_padrao_1.FuncaoPadrao) {
|
|
899
|
+
return valorResolvido.valorAridade;
|
|
829
900
|
}
|
|
830
|
-
if (
|
|
831
|
-
const metodos =
|
|
832
|
-
let tamanho = 0;
|
|
901
|
+
if (valorResolvido instanceof descritor_tipo_classe_1.DescritorTipoClasse) {
|
|
902
|
+
const metodos = valorResolvido.metodos;
|
|
833
903
|
const metodoInicializacao = metodos.inicializacao;
|
|
904
|
+
let tamanho = 0;
|
|
834
905
|
if (metodoInicializacao &&
|
|
835
906
|
!Array.isArray(metodoInicializacao) &&
|
|
836
907
|
metodoInicializacao.eInicializador) {
|
|
837
908
|
tamanho = metodoInicializacao.declaracao.parametros.length;
|
|
838
909
|
}
|
|
839
|
-
return
|
|
910
|
+
return tamanho;
|
|
840
911
|
}
|
|
841
|
-
|
|
912
|
+
if (valorResolvido.length === undefined &&
|
|
913
|
+
typeof valorResolvido === 'object') {
|
|
914
|
+
if (valorResolvido.hasOwnProperty('elementos')) {
|
|
915
|
+
return valorResolvido.elementos.length;
|
|
916
|
+
}
|
|
917
|
+
return Object.keys(valorResolvido).length;
|
|
918
|
+
}
|
|
919
|
+
return valorResolvido.length;
|
|
842
920
|
}
|
|
843
921
|
/**
|
|
844
922
|
* Transforma o valor ou variável em texto.
|
|
845
923
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
846
|
-
* @param {
|
|
924
|
+
* @param {any} valor O valor ou variável a ser convertido em texto.
|
|
847
925
|
* @returns {Promise<string>} O valor resolvido em texto.
|
|
848
926
|
*/
|
|
849
|
-
async function texto(interpretador,
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
* Retorna verdadeiro se todos os elementos do iterável forem truly.
|
|
854
|
-
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
855
|
-
* @param {VariavelInterface | any} iteravel O primeiro parâmetro, qualquer dado que seja iterável (vetores, tuplas, dicionários etc.).
|
|
856
|
-
* @returns {Promise<boolean>} Verdadeiro, se todos os valores do iterável forem Truly.
|
|
857
|
-
*/
|
|
858
|
-
async function todos(interpretador, iteravel) {
|
|
859
|
-
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
860
|
-
const ehObjetoOuDicionario = valorIteravel && typeof valorIteravel === 'object' && !Array.isArray(valorIteravel);
|
|
861
|
-
const ehIteravelNativo = valorIteravel && typeof valorIteravel[Symbol.iterator] === 'function';
|
|
862
|
-
if (!ehIteravelNativo && !ehObjetoOuDicionario) {
|
|
863
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
864
|
-
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
865
|
-
linha: interpretador.linhaDeclaracaoAtual,
|
|
866
|
-
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.'));
|
|
927
|
+
async function texto(interpretador, valor) {
|
|
928
|
+
const valorResolvido = interpretador.resolverValor(valor);
|
|
929
|
+
if (valorResolvido === null || valorResolvido === undefined) {
|
|
930
|
+
return 'nulo';
|
|
867
931
|
}
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
const valorResolvido = interpretador.resolverValor(valor);
|
|
871
|
-
if (!interpretador.eVerdadeiro(valorResolvido))
|
|
872
|
-
return false;
|
|
932
|
+
if (typeof valorResolvido === 'boolean') {
|
|
933
|
+
return valorResolvido ? 'verdadeiro' : 'falso';
|
|
873
934
|
}
|
|
874
|
-
return
|
|
935
|
+
return String(valorResolvido);
|
|
875
936
|
}
|
|
876
937
|
/**
|
|
877
|
-
* Retorna verdadeiro se todos os elementos do
|
|
878
|
-
* serem aplicados como argumentos da função passada como segundo parâmetro.
|
|
938
|
+
* Retorna verdadeiro se todos os elementos do iterável forem verdadeiros (truthy). Caso seja passado uma função como segundo parâmetro, cada elemento do iterável será passado para ela.
|
|
879
939
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
880
|
-
* @param {
|
|
881
|
-
* @param {
|
|
882
|
-
*
|
|
883
|
-
* @returns {Promise<boolean>} Verdadeiro, se todos os valores do iterável fazem a função passada
|
|
884
|
-
* por parâmetro devolver verdadeiro, ou falso em caso contrário.
|
|
940
|
+
* @param {any} iteravel Qualquer dado que seja iterável
|
|
941
|
+
* @param {any} funcaoCondicional A função que será executada com cada valor.
|
|
942
|
+
* @returns {Promise<boolean>} Verdadeiro, se todos os valores do iterável forem verdadeiros ou se satisfazerem a função condicional.
|
|
885
943
|
*/
|
|
886
|
-
async function
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
};
|
|
891
|
-
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
892
|
-
const ehObjetoOuDicionario = valorIteravel && typeof valorIteravel === 'object' && !Array.isArray(valorIteravel);
|
|
893
|
-
const ehIteravelNativo = valorIteravel && typeof valorIteravel[Symbol.iterator] === 'function';
|
|
894
|
-
if (!ehIteravelNativo && !ehObjetoOuDicionario) {
|
|
895
|
-
return Promise.reject(new excecoes_1.ErroEmTempoDeExecucao({
|
|
944
|
+
async function todos(interpretador, iteravel, funcaoCondicional) {
|
|
945
|
+
if (iteravel === null || iteravel === undefined) {
|
|
946
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
947
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
896
948
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
949
|
+
}, 'Parâmetro inválido. O parâmetro da função todos() não pode ser nulo.');
|
|
950
|
+
}
|
|
951
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
952
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
953
|
+
if (itens.length === 0 &&
|
|
954
|
+
valorIteravel !== '' &&
|
|
955
|
+
!Array.isArray(valorIteravel) &&
|
|
956
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN)) {
|
|
957
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
897
958
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
898
|
-
|
|
959
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
960
|
+
}, 'Parâmetro inválido. O primeiro parâmetro deve ser um iterável.');
|
|
899
961
|
}
|
|
900
962
|
const valorFuncao = interpretador.resolverValor(funcaoCondicional);
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
963
|
+
if (valorFuncao) {
|
|
964
|
+
const ehUmaFuncao = valorFuncao instanceof estruturas_1.DeleguaFuncao ||
|
|
965
|
+
valorFuncao instanceof funcao_padrao_1.FuncaoPadrao;
|
|
966
|
+
if (!valorFuncao || !ehUmaFuncao) {
|
|
967
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
968
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
969
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
970
|
+
}, 'Parâmetro inválido. O segundo parâmetro deve ser uma função.');
|
|
971
|
+
}
|
|
907
972
|
}
|
|
908
|
-
const
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
973
|
+
for (const item of itens) {
|
|
974
|
+
let resultado;
|
|
975
|
+
if (valorFuncao) {
|
|
976
|
+
const resultadoChamada = await valorFuncao.chamar(interpretador, [item], {
|
|
977
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
978
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
979
|
+
});
|
|
980
|
+
if (resultadoChamada !== null && resultadoChamada !== undefined) {
|
|
981
|
+
if (resultadoChamada.hasOwnProperty('valorRetornado')) {
|
|
982
|
+
resultado = resultadoChamada.valorRetornado instanceof quebras_1.RetornoQuebra
|
|
983
|
+
? resultadoChamada.valorRetornado.valor
|
|
984
|
+
: resultadoChamada.valorRetornado;
|
|
985
|
+
}
|
|
986
|
+
else if (resultadoChamada instanceof quebras_1.RetornoQuebra) {
|
|
987
|
+
resultado = resultadoChamada.valor;
|
|
988
|
+
}
|
|
989
|
+
else {
|
|
990
|
+
resultado = resultadoChamada;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
else {
|
|
995
|
+
resultado = item;
|
|
996
|
+
}
|
|
997
|
+
const resultadoResolvido = interpretador.resolverValor(resultado);
|
|
912
998
|
if (!interpretador.eVerdadeiro(resultadoResolvido))
|
|
913
999
|
return false;
|
|
914
1000
|
}
|
|
915
1001
|
return true;
|
|
916
1002
|
}
|
|
917
1003
|
/**
|
|
918
|
-
* Transforma um
|
|
919
|
-
* largura do vetor.
|
|
1004
|
+
* Transforma um iteravel em uma tupla.
|
|
920
1005
|
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
921
|
-
* @param {
|
|
1006
|
+
* @param {any} iteravel O iterável a ser convertido.
|
|
922
1007
|
* @returns A tupla resolvida.
|
|
923
1008
|
*/
|
|
924
|
-
async function tupla(interpretador,
|
|
925
|
-
const
|
|
926
|
-
|
|
927
|
-
|
|
1009
|
+
async function tupla(interpretador, iteravel) {
|
|
1010
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
1011
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
1012
|
+
if (itens.length === 0 &&
|
|
1013
|
+
valorIteravel !== '' &&
|
|
1014
|
+
!(valorIteravel instanceof construtos_1.Vetor) &&
|
|
1015
|
+
!Array.isArray(valorIteravel)) {
|
|
1016
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
928
1017
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
929
1018
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
930
|
-
}, '
|
|
1019
|
+
}, 'O argumento passado para a função `tupla()` deve ser iterável.');
|
|
931
1020
|
}
|
|
932
|
-
const
|
|
1021
|
+
const itensDaTupla = itens.map(item => {
|
|
933
1022
|
const valorResolvido = interpretador.resolverValor(item);
|
|
934
1023
|
const literal = new construtos_1.Literal(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual, valorResolvido);
|
|
935
1024
|
if (typeof valorResolvido === 'string') {
|
|
@@ -937,17 +1026,41 @@ async function tupla(interpretador, vetor) {
|
|
|
937
1026
|
}
|
|
938
1027
|
return literal;
|
|
939
1028
|
});
|
|
940
|
-
return new construtos_1.TuplaN(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual,
|
|
1029
|
+
return new construtos_1.TuplaN(interpretador.hashArquivoDeclaracaoAtual, interpretador.linhaDeclaracaoAtual, itensDaTupla);
|
|
1030
|
+
}
|
|
1031
|
+
async function unico(interpretador, iteravel) {
|
|
1032
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
1033
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
1034
|
+
if (itens.length === 0 &&
|
|
1035
|
+
valorIteravel !== '' &&
|
|
1036
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN) &&
|
|
1037
|
+
!Array.isArray(valorIteravel)) {
|
|
1038
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
1039
|
+
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
1040
|
+
linha: interpretador.linhaDeclaracaoAtual,
|
|
1041
|
+
}, 'O argumento passado para a função `unico()` deve ser iterável.');
|
|
1042
|
+
}
|
|
1043
|
+
return [...new Set(itens)];
|
|
941
1044
|
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1045
|
+
;
|
|
1046
|
+
/**
|
|
1047
|
+
* Transforma um iteravel em um vetor.
|
|
1048
|
+
* @param {InterpretadorInterface} interpretador A instância do interpretador.
|
|
1049
|
+
* @param {any} iteravel O iterável a ser convertido.
|
|
1050
|
+
* @returns O vetor resolvido.
|
|
1051
|
+
*/
|
|
1052
|
+
async function vetor(interpretador, iteravel) {
|
|
1053
|
+
const valorIteravel = interpretador.resolverValor(iteravel);
|
|
1054
|
+
const itens = new iteravel_1.Iteravel(valorIteravel).elementos;
|
|
1055
|
+
if (itens.length === 0 &&
|
|
1056
|
+
valorIteravel !== '' &&
|
|
1057
|
+
!(valorIteravel?.constructor === construtos_1.TuplaN) &&
|
|
1058
|
+
!Array.isArray(valorIteravel)) {
|
|
1059
|
+
throw new excecoes_1.ErroEmTempoDeExecucao({
|
|
946
1060
|
hashArquivo: interpretador.hashArquivoDeclaracaoAtual,
|
|
947
1061
|
linha: interpretador.linhaDeclaracaoAtual,
|
|
948
|
-
}, '
|
|
1062
|
+
}, 'O argumento passado para a função `vetor()` deve ser iterável.');
|
|
949
1063
|
}
|
|
950
|
-
|
|
951
|
-
return Promise.resolve(resultado);
|
|
1064
|
+
return itens.map(elemento => interpretador.resolverValor(elemento));
|
|
952
1065
|
}
|
|
953
1066
|
//# sourceMappingURL=biblioteca-global.js.map
|