@designliquido/delegua 0.2.6 → 0.2.9
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/bin/package.json +1 -1
- package/fontes/avaliador-sintatico/avaliador-sintatico.d.ts +1 -1
- package/fontes/avaliador-sintatico/avaliador-sintatico.js +4 -2
- package/fontes/avaliador-sintatico/avaliador-sintatico.js.map +1 -1
- package/fontes/delegua.d.ts +5 -2
- package/fontes/delegua.js +30 -34
- package/fontes/delegua.js.map +1 -1
- package/fontes/importador/importador.d.ts +9 -0
- package/fontes/importador/importador.js +60 -0
- package/fontes/importador/importador.js.map +1 -0
- package/fontes/importador/index.d.ts +2 -0
- package/fontes/importador/index.js +19 -0
- package/fontes/importador/index.js.map +1 -0
- package/fontes/importador/retorno-importador.d.ts +9 -0
- package/fontes/importador/retorno-importador.js +3 -0
- package/fontes/importador/retorno-importador.js.map +1 -0
- package/fontes/interfaces/delegua-interface.d.ts +21 -1
- package/fontes/interfaces/importador-interface.d.ts +4 -0
- package/fontes/interfaces/importador-interface.js +3 -0
- package/fontes/interfaces/importador-interface.js.map +1 -0
- package/fontes/interfaces/index.d.ts +1 -0
- package/fontes/interfaces/index.js +1 -0
- package/fontes/interfaces/index.js.map +1 -1
- package/fontes/interfaces/interpretador-interface.d.ts +1 -1
- package/fontes/interpretador/dialetos/egua-classico.d.ts +1 -1
- package/fontes/interpretador/dialetos/egua-classico.js +3 -2
- package/fontes/interpretador/dialetos/egua-classico.js.map +1 -1
- package/fontes/interpretador/index.d.ts +3 -66
- package/fontes/interpretador/index.js +5 -685
- package/fontes/interpretador/index.js.map +1 -1
- package/fontes/interpretador/interpretador.d.ts +68 -0
- package/fontes/interpretador/interpretador.js +691 -0
- package/fontes/interpretador/interpretador.js.map +1 -0
- package/fontes/lexador/lexador.js +3 -0
- package/fontes/lexador/lexador.js.map +1 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.Interpretador = void 0;
|
|
30
|
+
var caminho = __importStar(require("path"));
|
|
31
|
+
var browser_process_hrtime_1 = __importDefault(require("browser-process-hrtime"));
|
|
32
|
+
var tipos_de_simbolos_1 = __importDefault(require("../lexador/tipos-de-simbolos"));
|
|
33
|
+
var ambiente_1 = require("../ambiente");
|
|
34
|
+
var biblioteca_global_1 = __importDefault(require("../bibliotecas/biblioteca-global"));
|
|
35
|
+
var importar_biblioteca_1 = __importDefault(require("../bibliotecas/importar-biblioteca"));
|
|
36
|
+
var excecoes_1 = require("../excecoes");
|
|
37
|
+
var estruturas_1 = require("../estruturas");
|
|
38
|
+
/**
|
|
39
|
+
* O Interpretador visita todos os elementos complexos gerados pelo analisador sintático (Parser),
|
|
40
|
+
* e de fato executa a lógica de programação descrita no código.
|
|
41
|
+
*/
|
|
42
|
+
var Interpretador = /** @class */ (function () {
|
|
43
|
+
function Interpretador(importador, resolvedor, diretorioBase, performance, funcaoDeRetorno) {
|
|
44
|
+
if (performance === void 0) { performance = false; }
|
|
45
|
+
this.funcaoDeRetorno = null;
|
|
46
|
+
this.importador = importador;
|
|
47
|
+
this.resolvedor = resolvedor;
|
|
48
|
+
this.diretorioBase = diretorioBase;
|
|
49
|
+
this.performance = performance;
|
|
50
|
+
this.funcaoDeRetorno = funcaoDeRetorno || console.log;
|
|
51
|
+
this.global = new ambiente_1.Ambiente();
|
|
52
|
+
this.ambiente = this.global;
|
|
53
|
+
this.locais = new Map();
|
|
54
|
+
this.erros = [];
|
|
55
|
+
this.global = (0, biblioteca_global_1.default)(this, this.global);
|
|
56
|
+
}
|
|
57
|
+
Interpretador.prototype.resolver = function (expressao, profundidade) {
|
|
58
|
+
this.locais.set(expressao, profundidade);
|
|
59
|
+
};
|
|
60
|
+
Interpretador.prototype.visitarExpressaoLiteral = function (expressao) {
|
|
61
|
+
return expressao.valor;
|
|
62
|
+
};
|
|
63
|
+
Interpretador.prototype.avaliar = function (expressao) {
|
|
64
|
+
if (expressao.aceitar) {
|
|
65
|
+
return expressao.aceitar(this);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
Interpretador.prototype.visitarExpressaoAgrupamento = function (expressao) {
|
|
69
|
+
return this.avaliar(expressao.expressao);
|
|
70
|
+
};
|
|
71
|
+
Interpretador.prototype.eVerdadeiro = function (objeto) {
|
|
72
|
+
if (objeto === null)
|
|
73
|
+
return false;
|
|
74
|
+
if (typeof objeto === 'boolean')
|
|
75
|
+
return Boolean(objeto);
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
Interpretador.prototype.verificarOperandoNumero = function (operador, operando) {
|
|
79
|
+
if (typeof operando === 'number')
|
|
80
|
+
return;
|
|
81
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(operador, 'Operando precisa ser um número.', operador.linha);
|
|
82
|
+
};
|
|
83
|
+
Interpretador.prototype.visitarExpressaoUnaria = function (expressao) {
|
|
84
|
+
var direita = this.avaliar(expressao.direita);
|
|
85
|
+
switch (expressao.operador.tipo) {
|
|
86
|
+
case tipos_de_simbolos_1.default.SUBTRACAO:
|
|
87
|
+
this.verificarOperandoNumero(expressao.operador, direita);
|
|
88
|
+
return -direita;
|
|
89
|
+
case tipos_de_simbolos_1.default.NEGACAO:
|
|
90
|
+
return !this.eVerdadeiro(direita);
|
|
91
|
+
case tipos_de_simbolos_1.default.BIT_NOT:
|
|
92
|
+
return ~direita;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
};
|
|
96
|
+
Interpretador.prototype.eIgual = function (esquerda, direita) {
|
|
97
|
+
if (esquerda === null && direita === null)
|
|
98
|
+
return true;
|
|
99
|
+
if (esquerda === null)
|
|
100
|
+
return false;
|
|
101
|
+
return esquerda === direita;
|
|
102
|
+
};
|
|
103
|
+
Interpretador.prototype.verificarOperandosNumeros = function (operador, direita, esquerda) {
|
|
104
|
+
if (typeof direita === 'number' && typeof esquerda === 'number')
|
|
105
|
+
return;
|
|
106
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(operador, 'Operandos precisam ser números.', operador.linha);
|
|
107
|
+
};
|
|
108
|
+
Interpretador.prototype.visitarExpressaoBinaria = function (expressao) {
|
|
109
|
+
var esquerda = this.avaliar(expressao.esquerda);
|
|
110
|
+
var direita = this.avaliar(expressao.direita);
|
|
111
|
+
switch (expressao.operador.tipo) {
|
|
112
|
+
case tipos_de_simbolos_1.default.EXPONENCIACAO:
|
|
113
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
114
|
+
return Math.pow(esquerda, direita);
|
|
115
|
+
case tipos_de_simbolos_1.default.MAIOR:
|
|
116
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
117
|
+
return Number(esquerda) > Number(direita);
|
|
118
|
+
case tipos_de_simbolos_1.default.MAIOR_IGUAL:
|
|
119
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
120
|
+
return Number(esquerda) >= Number(direita);
|
|
121
|
+
case tipos_de_simbolos_1.default.MENOR:
|
|
122
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
123
|
+
return Number(esquerda) < Number(direita);
|
|
124
|
+
case tipos_de_simbolos_1.default.MENOR_IGUAL:
|
|
125
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
126
|
+
return Number(esquerda) <= Number(direita);
|
|
127
|
+
case tipos_de_simbolos_1.default.SUBTRACAO:
|
|
128
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
129
|
+
return Number(esquerda) - Number(direita);
|
|
130
|
+
case tipos_de_simbolos_1.default.ADICAO:
|
|
131
|
+
if (typeof esquerda === 'number' &&
|
|
132
|
+
typeof direita === 'number') {
|
|
133
|
+
return Number(esquerda) + Number(direita);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return String(esquerda) + String(direita);
|
|
137
|
+
}
|
|
138
|
+
case tipos_de_simbolos_1.default.DIVISAO:
|
|
139
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
140
|
+
return Number(esquerda) / Number(direita);
|
|
141
|
+
case tipos_de_simbolos_1.default.MULTIPLICACAO:
|
|
142
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
143
|
+
return Number(esquerda) * Number(direita);
|
|
144
|
+
case tipos_de_simbolos_1.default.MODULO:
|
|
145
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
146
|
+
return Number(esquerda) % Number(direita);
|
|
147
|
+
case tipos_de_simbolos_1.default.BIT_AND:
|
|
148
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
149
|
+
return Number(esquerda) & Number(direita);
|
|
150
|
+
case tipos_de_simbolos_1.default.BIT_XOR:
|
|
151
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
152
|
+
return Number(esquerda) ^ Number(direita);
|
|
153
|
+
case tipos_de_simbolos_1.default.BIT_OR:
|
|
154
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
155
|
+
return Number(esquerda) | Number(direita);
|
|
156
|
+
case tipos_de_simbolos_1.default.MENOR_MENOR:
|
|
157
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
158
|
+
return Number(esquerda) << Number(direita);
|
|
159
|
+
case tipos_de_simbolos_1.default.MAIOR_MAIOR:
|
|
160
|
+
this.verificarOperandosNumeros(expressao.operador, esquerda, direita);
|
|
161
|
+
return Number(esquerda) >> Number(direita);
|
|
162
|
+
case tipos_de_simbolos_1.default.DIFERENTE:
|
|
163
|
+
return !this.eIgual(esquerda, direita);
|
|
164
|
+
case tipos_de_simbolos_1.default.IGUAL_IGUAL:
|
|
165
|
+
return this.eIgual(esquerda, direita);
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
};
|
|
169
|
+
Interpretador.prototype.visitarExpressaoDeChamada = function (expressao) {
|
|
170
|
+
var entidadeChamada = this.avaliar(expressao.entidadeChamada);
|
|
171
|
+
var argumentos = [];
|
|
172
|
+
for (var i = 0; i < expressao.argumentos.length; i++) {
|
|
173
|
+
argumentos.push(this.avaliar(expressao.argumentos[i]));
|
|
174
|
+
}
|
|
175
|
+
if (!(entidadeChamada instanceof estruturas_1.Chamavel)) {
|
|
176
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.parentese, 'Só pode chamar função ou classe.', expressao.linha);
|
|
177
|
+
}
|
|
178
|
+
var parametros;
|
|
179
|
+
if (entidadeChamada instanceof estruturas_1.DeleguaFuncao) {
|
|
180
|
+
parametros = entidadeChamada.declaracao.parametros;
|
|
181
|
+
}
|
|
182
|
+
else if (entidadeChamada instanceof estruturas_1.DeleguaClasse) {
|
|
183
|
+
parametros = entidadeChamada.metodos.init
|
|
184
|
+
? entidadeChamada.metodos.init.declaracao.parametros
|
|
185
|
+
: [];
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
parametros = [];
|
|
189
|
+
}
|
|
190
|
+
// Isso aqui completa os parâmetros não preenchidos com nulos.
|
|
191
|
+
if (argumentos.length < entidadeChamada.aridade()) {
|
|
192
|
+
var diferenca = entidadeChamada.aridade() - argumentos.length;
|
|
193
|
+
for (var i = 0; i < diferenca; i++) {
|
|
194
|
+
argumentos.push(null);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
if (parametros &&
|
|
199
|
+
parametros.length > 0 &&
|
|
200
|
+
parametros[parametros.length - 1]['tipo'] === 'wildcard') {
|
|
201
|
+
var novosArgumentos = argumentos.slice(0, parametros.length - 1);
|
|
202
|
+
novosArgumentos.push(argumentos.slice(parametros.length - 1, argumentos.length));
|
|
203
|
+
argumentos = novosArgumentos;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (entidadeChamada instanceof estruturas_1.FuncaoPadrao) {
|
|
207
|
+
return entidadeChamada.chamar(this, argumentos, expressao.entidadeChamada.nome);
|
|
208
|
+
}
|
|
209
|
+
return entidadeChamada.chamar(this, argumentos);
|
|
210
|
+
};
|
|
211
|
+
Interpretador.prototype.visitarExpressaoDeAtribuicao = function (expressao) {
|
|
212
|
+
var valor = this.avaliar(expressao.valor);
|
|
213
|
+
var distancia = this.locais.get(expressao);
|
|
214
|
+
if (distancia !== undefined) {
|
|
215
|
+
this.ambiente.atribuirVariavelEm(distancia, expressao.simbolo, valor);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
this.ambiente.atribuirVariavel(expressao.simbolo, valor);
|
|
219
|
+
}
|
|
220
|
+
return valor;
|
|
221
|
+
};
|
|
222
|
+
Interpretador.prototype.procurarVariavel = function (simbolo, expressao) {
|
|
223
|
+
var distancia = this.locais.get(expressao);
|
|
224
|
+
if (distancia !== undefined) {
|
|
225
|
+
return this.ambiente.obterVariavelEm(distancia, simbolo.lexema);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
return this.global.obterVariavel(simbolo);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
Interpretador.prototype.visitarExpressaoDeVariavel = function (expressao) {
|
|
232
|
+
return this.procurarVariavel(expressao.simbolo, expressao);
|
|
233
|
+
};
|
|
234
|
+
Interpretador.prototype.visitarDeclaracaoDeExpressao = function (declaracao) {
|
|
235
|
+
return this.avaliar(declaracao.expressao);
|
|
236
|
+
};
|
|
237
|
+
Interpretador.prototype.visitarExpressaoLogica = function (expressao) {
|
|
238
|
+
var esquerda = this.avaliar(expressao.esquerda);
|
|
239
|
+
if (expressao.operador.tipo === tipos_de_simbolos_1.default.EM) {
|
|
240
|
+
var direita = this.avaliar(expressao.direita);
|
|
241
|
+
if (Array.isArray(direita) || typeof direita === 'string') {
|
|
242
|
+
return direita.includes(esquerda);
|
|
243
|
+
}
|
|
244
|
+
else if (direita.constructor === Object) {
|
|
245
|
+
return esquerda in direita;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(esquerda, "Tipo de chamada inválida com 'em'.", expressao.linha);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// se um estado for verdadeiro, retorna verdadeiro
|
|
252
|
+
if (expressao.operador.tipo === tipos_de_simbolos_1.default.OU) {
|
|
253
|
+
if (this.eVerdadeiro(esquerda))
|
|
254
|
+
return esquerda;
|
|
255
|
+
}
|
|
256
|
+
// se um estado for falso, retorna falso
|
|
257
|
+
if (expressao.operador.tipo === tipos_de_simbolos_1.default.E) {
|
|
258
|
+
if (!this.eVerdadeiro(esquerda))
|
|
259
|
+
return esquerda;
|
|
260
|
+
}
|
|
261
|
+
return this.avaliar(expressao.direita);
|
|
262
|
+
};
|
|
263
|
+
Interpretador.prototype.visitarExpressaoSe = function (declaracao) {
|
|
264
|
+
if (this.eVerdadeiro(this.avaliar(declaracao.condicao))) {
|
|
265
|
+
this.executar(declaracao.caminhoEntao);
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
for (var i = 0; i < declaracao.caminhosSeSenao.length; i++) {
|
|
269
|
+
var atual = declaracao.caminhosSeSenao[i];
|
|
270
|
+
if (this.eVerdadeiro(this.avaliar(atual.condicao))) {
|
|
271
|
+
this.executar(atual.caminho);
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (declaracao.caminhoSenao !== null) {
|
|
276
|
+
this.executar(declaracao.caminhoSenao);
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
};
|
|
280
|
+
Interpretador.prototype.visitarExpressaoPara = function (declaracao) {
|
|
281
|
+
if (declaracao.inicializador !== null) {
|
|
282
|
+
this.avaliar(declaracao.inicializador);
|
|
283
|
+
}
|
|
284
|
+
while (true) {
|
|
285
|
+
if (declaracao.condicao !== null) {
|
|
286
|
+
if (!this.eVerdadeiro(this.avaliar(declaracao.condicao))) {
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
this.executar(declaracao.corpo);
|
|
292
|
+
}
|
|
293
|
+
catch (erro) {
|
|
294
|
+
if (erro instanceof excecoes_1.ExcecaoSustar) {
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
else if (erro instanceof excecoes_1.ExcecaoContinuar) {
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
throw erro;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (declaracao.incrementar !== null) {
|
|
304
|
+
this.avaliar(declaracao.incrementar);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
};
|
|
309
|
+
Interpretador.prototype.visitarExpressaoFazer = function (declaracao) {
|
|
310
|
+
do {
|
|
311
|
+
try {
|
|
312
|
+
this.executar(declaracao.caminhoFazer);
|
|
313
|
+
}
|
|
314
|
+
catch (erro) {
|
|
315
|
+
if (erro instanceof excecoes_1.ExcecaoSustar) {
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
else if (erro instanceof excecoes_1.ExcecaoContinuar) {
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
throw erro;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
} while (this.eVerdadeiro(this.avaliar(declaracao.condicaoEnquanto)));
|
|
325
|
+
};
|
|
326
|
+
Interpretador.prototype.visitarExpressaoEscolha = function (declaracao) {
|
|
327
|
+
var condicaoEscolha = this.avaliar(declaracao.condicao);
|
|
328
|
+
var caminhos = declaracao.caminhos;
|
|
329
|
+
var caminhoPadrao = declaracao.caminhoPadrao;
|
|
330
|
+
var encontrado = false;
|
|
331
|
+
try {
|
|
332
|
+
for (var i = 0; i < caminhos.length; i++) {
|
|
333
|
+
var caminho_1 = caminhos[i];
|
|
334
|
+
for (var j = 0; j < caminho_1.conditions.length; j++) {
|
|
335
|
+
if (this.avaliar(caminho_1.conditions[j]) === condicaoEscolha) {
|
|
336
|
+
encontrado = true;
|
|
337
|
+
try {
|
|
338
|
+
for (var k = 0; k < caminho_1.declaracoes.length; k++) {
|
|
339
|
+
this.executar(caminho_1.declaracoes[k]);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
catch (erro) {
|
|
343
|
+
if (erro instanceof excecoes_1.ExcecaoContinuar) {
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
throw erro;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (caminhoPadrao !== null && encontrado === false) {
|
|
353
|
+
for (var i = 0; i < caminhoPadrao.declaracoes.length; i++) {
|
|
354
|
+
this.executar(caminhoPadrao['declaracoes'][i]);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
catch (erro) {
|
|
359
|
+
if (erro instanceof excecoes_1.ExcecaoSustar) {
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
throw erro;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
Interpretador.prototype.visitarExpressaoTente = function (declaracao) {
|
|
367
|
+
try {
|
|
368
|
+
var sucesso = true;
|
|
369
|
+
try {
|
|
370
|
+
this.executarBloco(declaracao.caminhoTente, new ambiente_1.Ambiente(this.ambiente));
|
|
371
|
+
}
|
|
372
|
+
catch (erro) {
|
|
373
|
+
sucesso = false;
|
|
374
|
+
if (declaracao.caminhoPegue !== null) {
|
|
375
|
+
this.executarBloco(declaracao.caminhoPegue, new ambiente_1.Ambiente(this.ambiente));
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (sucesso && declaracao.caminhoSenao !== null) {
|
|
379
|
+
this.executarBloco(declaracao.caminhoSenao, new ambiente_1.Ambiente(this.ambiente));
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
finally {
|
|
383
|
+
if (declaracao.caminhoFinalmente !== null)
|
|
384
|
+
this.executarBloco(declaracao.caminhoFinalmente, new ambiente_1.Ambiente(this.ambiente));
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
Interpretador.prototype.visitarExpressaoEnquanto = function (declaracao) {
|
|
388
|
+
while (this.eVerdadeiro(this.avaliar(declaracao.condicao))) {
|
|
389
|
+
try {
|
|
390
|
+
this.executar(declaracao.corpo);
|
|
391
|
+
}
|
|
392
|
+
catch (erro) {
|
|
393
|
+
if (erro instanceof excecoes_1.ExcecaoSustar) {
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
else if (erro instanceof excecoes_1.ExcecaoContinuar) {
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
throw erro;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return null;
|
|
404
|
+
};
|
|
405
|
+
Interpretador.prototype.visitarExpressaoImportar = function (declaracao) {
|
|
406
|
+
var caminhoRelativo = this.avaliar(declaracao.caminho);
|
|
407
|
+
var caminhoTotal = caminho.join(this.diretorioBase, caminhoRelativo);
|
|
408
|
+
var nomeArquivo = caminho.basename(caminhoTotal);
|
|
409
|
+
if (!caminhoTotal.endsWith('.egua') && !caminhoTotal.endsWith('.delegua')) {
|
|
410
|
+
return (0, importar_biblioteca_1.default)(caminhoRelativo);
|
|
411
|
+
}
|
|
412
|
+
var conteudoImportacao = this.importador.importar(caminhoRelativo);
|
|
413
|
+
var retornoInterpretador = this.interpretar(conteudoImportacao.retornoAvaliadorSintatico);
|
|
414
|
+
var funcoesDeclaradas = this.global.obterTodasDeleguaFuncao();
|
|
415
|
+
var eDicionario = function (objeto) { return objeto.constructor === Object; };
|
|
416
|
+
if (eDicionario(funcoesDeclaradas)) {
|
|
417
|
+
var novoModulo = new estruturas_1.DeleguaModulo();
|
|
418
|
+
var chaves = Object.keys(funcoesDeclaradas);
|
|
419
|
+
for (var i = 0; i < chaves.length; i++) {
|
|
420
|
+
novoModulo.componentes[chaves[i]] = funcoesDeclaradas[chaves[i]];
|
|
421
|
+
}
|
|
422
|
+
return novoModulo;
|
|
423
|
+
}
|
|
424
|
+
return funcoesDeclaradas;
|
|
425
|
+
};
|
|
426
|
+
Interpretador.prototype.visitarExpressaoEscreva = function (declaracao) {
|
|
427
|
+
var valor = this.avaliar(declaracao.expressao);
|
|
428
|
+
this.funcaoDeRetorno(this.paraTexto(valor));
|
|
429
|
+
return null;
|
|
430
|
+
};
|
|
431
|
+
Interpretador.prototype.executarBloco = function (declaracoes, ambiente) {
|
|
432
|
+
var anterior = this.ambiente;
|
|
433
|
+
try {
|
|
434
|
+
this.ambiente = ambiente;
|
|
435
|
+
if (declaracoes && declaracoes.length) {
|
|
436
|
+
for (var i = 0; i < declaracoes.length; i++) {
|
|
437
|
+
this.executar(declaracoes[i]);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
catch (erro) {
|
|
442
|
+
// TODO: try sem catch é uma roubada total. Implementar uma forma de quebra de fluxo sem exceção.
|
|
443
|
+
throw erro;
|
|
444
|
+
}
|
|
445
|
+
finally {
|
|
446
|
+
this.ambiente = anterior;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
Interpretador.prototype.visitarExpressaoBloco = function (declaracao) {
|
|
450
|
+
this.executarBloco(declaracao.declaracoes, new ambiente_1.Ambiente(this.ambiente));
|
|
451
|
+
return null;
|
|
452
|
+
};
|
|
453
|
+
Interpretador.prototype.visitarExpressaoVar = function (declaracao) {
|
|
454
|
+
var valor = null;
|
|
455
|
+
if (declaracao.inicializador !== null) {
|
|
456
|
+
valor = this.avaliar(declaracao.inicializador);
|
|
457
|
+
}
|
|
458
|
+
this.ambiente.definirVariavel(declaracao.simbolo.lexema, valor);
|
|
459
|
+
return null;
|
|
460
|
+
};
|
|
461
|
+
Interpretador.prototype.visitarExpressaoContinua = function (declaracao) {
|
|
462
|
+
throw new excecoes_1.ExcecaoContinuar();
|
|
463
|
+
};
|
|
464
|
+
Interpretador.prototype.visitarExpressaoSustar = function (declaracao) {
|
|
465
|
+
throw new excecoes_1.ExcecaoSustar();
|
|
466
|
+
};
|
|
467
|
+
Interpretador.prototype.visitarExpressaoRetornar = function (declaracao) {
|
|
468
|
+
var valor = null;
|
|
469
|
+
if (declaracao.valor != null)
|
|
470
|
+
valor = this.avaliar(declaracao.valor);
|
|
471
|
+
throw new excecoes_1.ExcecaoRetornar(valor);
|
|
472
|
+
};
|
|
473
|
+
Interpretador.prototype.visitarExpressaoDeleguaFuncao = function (expressao) {
|
|
474
|
+
return new estruturas_1.DeleguaFuncao(null, expressao, this.ambiente, false);
|
|
475
|
+
};
|
|
476
|
+
Interpretador.prototype.visitarExpressaoAtribuicaoSobrescrita = function (expressao) {
|
|
477
|
+
var objeto = this.avaliar(expressao.objeto);
|
|
478
|
+
var indice = this.avaliar(expressao.indice);
|
|
479
|
+
var valor = this.avaliar(expressao.valor);
|
|
480
|
+
if (Array.isArray(objeto)) {
|
|
481
|
+
if (indice < 0 && objeto.length !== 0) {
|
|
482
|
+
while (indice < 0) {
|
|
483
|
+
indice += objeto.length;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
while (objeto.length < indice) {
|
|
487
|
+
objeto.push(null);
|
|
488
|
+
}
|
|
489
|
+
objeto[indice] = valor;
|
|
490
|
+
}
|
|
491
|
+
else if (objeto.constructor === Object ||
|
|
492
|
+
objeto instanceof estruturas_1.DeleguaInstancia ||
|
|
493
|
+
objeto instanceof estruturas_1.DeleguaFuncao ||
|
|
494
|
+
objeto instanceof estruturas_1.DeleguaClasse ||
|
|
495
|
+
objeto instanceof estruturas_1.DeleguaModulo) {
|
|
496
|
+
objeto[indice] = valor;
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.objeto.nome, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
Interpretador.prototype.visitarExpressaoAcessoIndiceVariavel = function (expressao) {
|
|
503
|
+
var objeto = this.avaliar(expressao.entidadeChamada);
|
|
504
|
+
var indice = this.avaliar(expressao.indice);
|
|
505
|
+
if (Array.isArray(objeto)) {
|
|
506
|
+
if (!Number.isInteger(indice)) {
|
|
507
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Somente inteiros podem ser usados para indexar um vetor.', expressao.linha);
|
|
508
|
+
}
|
|
509
|
+
if (indice < 0 && objeto.length !== 0) {
|
|
510
|
+
while (indice < 0) {
|
|
511
|
+
indice += objeto.length;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (indice >= objeto.length) {
|
|
515
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Índice do vetor fora do intervalo.', expressao.linha);
|
|
516
|
+
}
|
|
517
|
+
return objeto[indice];
|
|
518
|
+
}
|
|
519
|
+
else if (objeto.constructor === Object ||
|
|
520
|
+
objeto instanceof estruturas_1.DeleguaInstancia ||
|
|
521
|
+
objeto instanceof estruturas_1.DeleguaFuncao ||
|
|
522
|
+
objeto instanceof estruturas_1.DeleguaClasse ||
|
|
523
|
+
objeto instanceof estruturas_1.DeleguaModulo) {
|
|
524
|
+
return objeto[indice] || null;
|
|
525
|
+
}
|
|
526
|
+
else if (typeof objeto === 'string') {
|
|
527
|
+
if (!Number.isInteger(indice)) {
|
|
528
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Somente inteiros podem ser usados para indexar um vetor.', expressao.linha);
|
|
529
|
+
}
|
|
530
|
+
if (indice < 0 && objeto.length !== 0) {
|
|
531
|
+
while (indice < 0) {
|
|
532
|
+
indice += objeto.length;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
if (indice >= objeto.length) {
|
|
536
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.simboloFechamento, 'Índice fora do tamanho.', expressao.linha);
|
|
537
|
+
}
|
|
538
|
+
return objeto.charAt(indice);
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.entidadeChamada.nome, 'Somente listas, dicionários, classes e objetos podem ser mudados por sobrescrita.', expressao.linha);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
Interpretador.prototype.visitarExpressaoDefinir = function (expressao) {
|
|
545
|
+
var objeto = this.avaliar(expressao.objeto);
|
|
546
|
+
if (!(objeto instanceof estruturas_1.DeleguaInstancia) &&
|
|
547
|
+
objeto.constructor !== Object) {
|
|
548
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.objeto.nome, 'Somente instâncias e dicionários podem possuir campos.', expressao.linha);
|
|
549
|
+
}
|
|
550
|
+
var valor = this.avaliar(expressao.valor);
|
|
551
|
+
if (objeto instanceof estruturas_1.DeleguaInstancia) {
|
|
552
|
+
objeto.set(expressao.nome, valor);
|
|
553
|
+
return valor;
|
|
554
|
+
}
|
|
555
|
+
else if (objeto.constructor === Object) {
|
|
556
|
+
objeto[expressao.simbolo.lexema] = valor;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
Interpretador.prototype.visitarExpressaoFuncao = function (declaracao) {
|
|
560
|
+
var funcao = new estruturas_1.DeleguaFuncao(declaracao.simbolo.lexema, declaracao.funcao, this.ambiente, false);
|
|
561
|
+
this.ambiente.definirVariavel(declaracao.simbolo.lexema, funcao);
|
|
562
|
+
};
|
|
563
|
+
Interpretador.prototype.visitarExpressaoClasse = function (declaracao) {
|
|
564
|
+
var superClasse = null;
|
|
565
|
+
if (declaracao.superClasse !== null) {
|
|
566
|
+
superClasse = this.avaliar(declaracao.superClasse);
|
|
567
|
+
if (!(superClasse instanceof estruturas_1.DeleguaClasse)) {
|
|
568
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(declaracao.superClasse.nome, 'SuperClasse precisa ser uma classe.', declaracao.linha);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
this.ambiente.definirVariavel(declaracao.simbolo.lexema, null);
|
|
572
|
+
if (declaracao.superClasse !== null) {
|
|
573
|
+
this.ambiente = new ambiente_1.Ambiente(this.ambiente);
|
|
574
|
+
this.ambiente.definirVariavel('super', superClasse);
|
|
575
|
+
}
|
|
576
|
+
var metodos = {};
|
|
577
|
+
var definirMetodos = declaracao.metodos;
|
|
578
|
+
for (var i = 0; i < declaracao.metodos.length; i++) {
|
|
579
|
+
var metodoAtual = definirMetodos[i];
|
|
580
|
+
var eInicializado = metodoAtual.simbolo.lexema === 'construtor';
|
|
581
|
+
var funcao = new estruturas_1.DeleguaFuncao(metodoAtual.simbolo.lexema, metodoAtual.funcao, this.ambiente, eInicializado);
|
|
582
|
+
metodos[metodoAtual.simbolo.lexema] = funcao;
|
|
583
|
+
}
|
|
584
|
+
var criado = new estruturas_1.DeleguaClasse(declaracao.simbolo.lexema, superClasse, metodos);
|
|
585
|
+
if (superClasse !== null) {
|
|
586
|
+
this.ambiente = this.ambiente.enclosing;
|
|
587
|
+
}
|
|
588
|
+
this.ambiente.atribuirVariavel(declaracao.simbolo, criado);
|
|
589
|
+
return null;
|
|
590
|
+
};
|
|
591
|
+
Interpretador.prototype.visitarExpressaoAcessoMetodo = function (expressao) {
|
|
592
|
+
var objeto = this.avaliar(expressao.objeto);
|
|
593
|
+
if (objeto instanceof estruturas_1.DeleguaInstancia) {
|
|
594
|
+
return objeto.get(expressao.simbolo) || null;
|
|
595
|
+
}
|
|
596
|
+
else if (objeto.constructor === Object) {
|
|
597
|
+
return objeto[expressao.simbolo.lexema] || null;
|
|
598
|
+
}
|
|
599
|
+
else if (objeto instanceof estruturas_1.DeleguaModulo) {
|
|
600
|
+
return objeto.componentes[expressao.simbolo.lexema] || null;
|
|
601
|
+
}
|
|
602
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.nome, 'Você só pode acessar métodos do objeto e dicionários.', expressao.linha);
|
|
603
|
+
};
|
|
604
|
+
Interpretador.prototype.visitarExpressaoIsto = function (expressao) {
|
|
605
|
+
return this.procurarVariavel(expressao.palavraChave, expressao);
|
|
606
|
+
};
|
|
607
|
+
Interpretador.prototype.visitarExpressaoDicionario = function (expressao) {
|
|
608
|
+
var dicionario = {};
|
|
609
|
+
for (var i = 0; i < expressao.chaves.length; i++) {
|
|
610
|
+
dicionario[this.avaliar(expressao.chaves[i])] = this.avaliar(expressao.valores[i]);
|
|
611
|
+
}
|
|
612
|
+
return dicionario;
|
|
613
|
+
};
|
|
614
|
+
Interpretador.prototype.visitarExpressaoVetor = function (expressao) {
|
|
615
|
+
var valores = [];
|
|
616
|
+
for (var i = 0; i < expressao.valores.length; i++) {
|
|
617
|
+
valores.push(this.avaliar(expressao.valores[i]));
|
|
618
|
+
}
|
|
619
|
+
return valores;
|
|
620
|
+
};
|
|
621
|
+
Interpretador.prototype.visitarExpressaoSuper = function (expressao) {
|
|
622
|
+
var distancia = this.locais.get(expressao);
|
|
623
|
+
var superClasse = this.ambiente.obterVariavelEm(distancia, 'super');
|
|
624
|
+
var objeto = this.ambiente.obterVariavelEm(distancia - 1, 'isto');
|
|
625
|
+
var metodo = superClasse.encontrarMetodo(expressao.metodo.lexema);
|
|
626
|
+
if (metodo === undefined) {
|
|
627
|
+
throw new excecoes_1.ErroEmTempoDeExecucao(expressao.metodo, 'Método chamado indefinido.', expressao.linha);
|
|
628
|
+
}
|
|
629
|
+
return metodo.definirEscopo(objeto);
|
|
630
|
+
};
|
|
631
|
+
Interpretador.prototype.paraTexto = function (objeto) {
|
|
632
|
+
if (objeto === null)
|
|
633
|
+
return 'nulo';
|
|
634
|
+
if (typeof objeto === 'boolean') {
|
|
635
|
+
return objeto ? 'verdadeiro' : 'falso';
|
|
636
|
+
}
|
|
637
|
+
if (objeto instanceof Date) {
|
|
638
|
+
var formato = Intl.DateTimeFormat('pt', {
|
|
639
|
+
dateStyle: 'full',
|
|
640
|
+
timeStyle: 'full',
|
|
641
|
+
});
|
|
642
|
+
return formato.format(objeto);
|
|
643
|
+
}
|
|
644
|
+
if (Array.isArray(objeto))
|
|
645
|
+
return objeto;
|
|
646
|
+
if (typeof objeto === 'object')
|
|
647
|
+
return JSON.stringify(objeto);
|
|
648
|
+
return objeto.toString();
|
|
649
|
+
};
|
|
650
|
+
Interpretador.prototype.executar = function (declaracao, mostrarResultado) {
|
|
651
|
+
if (mostrarResultado === void 0) { mostrarResultado = false; }
|
|
652
|
+
var resultado = declaracao.aceitar(this);
|
|
653
|
+
if (mostrarResultado) {
|
|
654
|
+
this.funcaoDeRetorno(this.paraTexto(resultado));
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
Interpretador.prototype.interpretar = function (objeto) {
|
|
658
|
+
this.erros = [];
|
|
659
|
+
var retornoResolvedor = this.resolvedor.resolver(objeto);
|
|
660
|
+
this.locais = retornoResolvedor.locais;
|
|
661
|
+
var inicioInterpretacao = (0, browser_process_hrtime_1.default)();
|
|
662
|
+
try {
|
|
663
|
+
var declaracoes = objeto.declaracoes || objeto;
|
|
664
|
+
if (declaracoes.length === 1) {
|
|
665
|
+
var eObjetoExpressao = declaracoes[0].constructor.name === 'Expressao';
|
|
666
|
+
if (eObjetoExpressao) {
|
|
667
|
+
this.executar(declaracoes[0], true);
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (var i = 0; i < declaracoes.length; i++) {
|
|
672
|
+
this.executar(declaracoes[i]);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
catch (erro) {
|
|
676
|
+
this.erros.push(erro);
|
|
677
|
+
}
|
|
678
|
+
finally {
|
|
679
|
+
var deltaInterpretacao = (0, browser_process_hrtime_1.default)(inicioInterpretacao);
|
|
680
|
+
if (this.performance) {
|
|
681
|
+
console.log("[Interpretador] Tempo para interpreta\u00E7ao: ".concat(deltaInterpretacao[0] * 1e9 + deltaInterpretacao[1], "ns"));
|
|
682
|
+
}
|
|
683
|
+
return {
|
|
684
|
+
erros: this.erros
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
return Interpretador;
|
|
689
|
+
}());
|
|
690
|
+
exports.Interpretador = Interpretador;
|
|
691
|
+
//# sourceMappingURL=interpretador.js.map
|