@designliquido/delegua 0.0.2 → 0.1.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.
Files changed (81) hide show
  1. package/.github/CONTRIBUTING.md +37 -0
  2. package/.github/workflows/principal.yml +3 -1
  3. package/.release-it.json +2 -1
  4. package/.vscode/launch.json +43 -8
  5. package/README.md +9 -1
  6. package/bin/delegua +2 -2
  7. package/bin/delegua.cmd +1 -1
  8. package/index.ts +29 -0
  9. package/package.json +9 -4
  10. package/src/ambiente.ts +56 -0
  11. package/src/avaliador-sintatico/dialetos/egua-classico.ts +983 -0
  12. package/src/avaliador-sintatico/dialetos/index.ts +1 -0
  13. package/src/avaliador-sintatico/index.ts +983 -0
  14. package/src/avaliador-sintatico/parser-error.ts +1 -0
  15. package/src/{lib/globalLib.js → bibliotecas/bibliotecaGlobal.ts} +34 -34
  16. package/src/{lib/importStdlib.js → bibliotecas/importarBiblioteca.ts} +9 -9
  17. package/src/construtos/assign-subscript.ts +19 -0
  18. package/src/construtos/atribuir.ts +17 -0
  19. package/src/construtos/binario.ts +19 -0
  20. package/src/construtos/call.ts +19 -0
  21. package/src/construtos/conjunto.ts +19 -0
  22. package/src/construtos/dicionario.ts +17 -0
  23. package/src/construtos/expr.ts +3 -0
  24. package/src/construtos/funcao.ts +17 -0
  25. package/src/construtos/get.ts +17 -0
  26. package/src/construtos/grouping.ts +15 -0
  27. package/src/construtos/index.ts +18 -0
  28. package/src/construtos/isto.ts +15 -0
  29. package/src/construtos/literal.ts +15 -0
  30. package/src/construtos/logical.ts +19 -0
  31. package/src/construtos/subscript.ts +19 -0
  32. package/src/construtos/super.ts +17 -0
  33. package/src/construtos/unario.ts +17 -0
  34. package/src/construtos/variavel.ts +15 -0
  35. package/src/construtos/vetor.ts +15 -0
  36. package/src/{stmt.js → declaracoes/index.ts} +79 -51
  37. package/src/delegua.ts +156 -0
  38. package/src/estruturas/callable.ts +11 -0
  39. package/src/estruturas/{classe.js → classe.ts} +11 -7
  40. package/src/estruturas/{funcaoPadrao.js → funcao-padrao.ts} +8 -4
  41. package/src/estruturas/funcao.ts +70 -0
  42. package/src/estruturas/index.ts +6 -0
  43. package/src/estruturas/{instancia.js → instancia.ts} +12 -9
  44. package/src/estruturas/modulo.ts +11 -0
  45. package/src/excecoes/break-exception.ts +1 -0
  46. package/src/excecoes/continue-exception.ts +1 -0
  47. package/src/excecoes/erro-em-tempo-de-execucao.ts +11 -0
  48. package/src/excecoes/index.ts +4 -0
  49. package/src/excecoes/return-exception.ts +10 -0
  50. package/src/interfaces/avaliador-sintatico-interface.ts +58 -0
  51. package/src/interfaces/index.ts +6 -0
  52. package/src/interfaces/interpretador-interface.ts +52 -0
  53. package/src/interfaces/lexador-interface.ts +24 -0
  54. package/src/interfaces/pilha-interface.ts +7 -0
  55. package/src/interfaces/resolvedor-interface.ts +49 -0
  56. package/src/interfaces/simbolo-interface.ts +6 -0
  57. package/src/interpretador/dialetos/egua-classico.ts +825 -0
  58. package/src/interpretador/dialetos/index.ts +1 -0
  59. package/src/interpretador/index.ts +825 -0
  60. package/src/lexador/dialetos/egua-classico.ts +333 -0
  61. package/src/lexador/dialetos/index.ts +1 -0
  62. package/src/{lexer.js → lexador/index.ts} +38 -21
  63. package/src/resolvedor/Pilha.ts +29 -0
  64. package/src/resolvedor/ResolverError.ts +8 -0
  65. package/src/resolvedor/dialetos/egua-classico.ts +412 -0
  66. package/src/resolvedor/dialetos/index.ts +1 -0
  67. package/src/{resolver.js → resolvedor/index.ts} +101 -114
  68. package/src/{tiposDeSimbolos.js → tiposDeSimbolos.ts} +21 -22
  69. package/src/web.ts +75 -0
  70. package/tsconfig.json +11 -0
  71. package/indice.js +0 -14
  72. package/src/ambiente.js +0 -53
  73. package/src/delegua.js +0 -102
  74. package/src/erro.js +0 -18
  75. package/src/estruturas/callable.js +0 -5
  76. package/src/estruturas/funcao.js +0 -62
  77. package/src/estruturas/modulo.js +0 -9
  78. package/src/expr.js +0 -228
  79. package/src/interpretador.js +0 -802
  80. package/src/parser.js +0 -822
  81. package/src/web.js +0 -70
@@ -0,0 +1,412 @@
1
+ import { ResolvedorInterface } from "../../interfaces/resolvedor-interface";
2
+ import { Pilha } from "../Pilha";
3
+ import { ResolverError } from "../ResolverError";
4
+
5
+ const TipoFuncao = {
6
+ NENHUM: "NENHUM",
7
+ FUNCAO: "FUNCAO",
8
+ CONSTRUTOR: "CONSTRUTOR",
9
+ METODO: "METODO"
10
+ };
11
+
12
+ const TipoClasse = {
13
+ NENHUM: "NENHUM",
14
+ CLASSE: "CLASSE",
15
+ SUBCLASSE: "SUBCLASSE"
16
+ };
17
+
18
+ const LoopType = {
19
+ NENHUM: "NENHUM",
20
+ ENQUANTO: "ENQUANTO",
21
+ ESCOLHA: "ESCOLHA",
22
+ PARA: "PARA",
23
+ FAZER: "FAZER"
24
+ };
25
+
26
+ /**
27
+ * O Resolvedor (Resolver) é responsável por catalogar todos os identificadores complexos, como por exemplo: funções, classes, variáveis,
28
+ * e delimitar os escopos onde esses identificadores existem.
29
+ * Exemplo: uma classe A declara dois métodos chamados M e N. Todas as variáveis declaradas dentro de M não podem ser vistas por N, e vice-versa.
30
+ * No entanto, todas as variáveis declaradas dentro da classe A podem ser vistas tanto por M quanto por N.
31
+ */
32
+ export class ResolverEguaClassico implements ResolvedorInterface {
33
+ interpretador: any;
34
+ Delegua: any;
35
+ escopos: any;
36
+ FuncaoAtual: any;
37
+ ClasseAtual: any;
38
+ cicloAtual: any;
39
+
40
+ constructor(Delegua: any, interpretador: any) {
41
+ this.interpretador = interpretador;
42
+ this.Delegua = Delegua;
43
+ this.escopos = new Pilha();
44
+
45
+ this.FuncaoAtual = TipoFuncao.NENHUM;
46
+ this.ClasseAtual = TipoClasse.NENHUM;
47
+ this.cicloAtual = TipoClasse.NENHUM;
48
+ }
49
+
50
+ definir(nome: any): void {
51
+ if (this.escopos.eVazio()) return;
52
+ this.escopos.peek()[nome.lexema] = true;
53
+ }
54
+
55
+ declarar(nome: any): void {
56
+ if (this.escopos.eVazio()) return;
57
+ let escopo = this.escopos.peek();
58
+ if (escopo.hasOwnProperty(nome.lexema))
59
+ this.Delegua.erro(
60
+ nome,
61
+ "Variável com esse nome já declarada neste escopo."
62
+ );
63
+ escopo[nome.lexema] = false;
64
+ }
65
+
66
+ inicioDoEscopo(): void {
67
+ this.escopos.empilhar({});
68
+ }
69
+
70
+ finalDoEscopo(): void {
71
+ this.escopos.removerUltimo();
72
+ }
73
+
74
+ resolver(declaracoes: any): void {
75
+ if (Array.isArray(declaracoes)) {
76
+ for (let i = 0; i < declaracoes.length; i++) {
77
+ if (declaracoes[i] && declaracoes[i].aceitar) {
78
+ declaracoes[i].aceitar(this);
79
+ }
80
+ }
81
+ } else if (declaracoes) {
82
+ declaracoes.aceitar(this);
83
+ }
84
+ }
85
+
86
+ resolverLocal(expr: any, nome: any): void {
87
+ for (let i = this.escopos.pilha.length - 1; i >= 0; i--) {
88
+ if (this.escopos.pilha[i].hasOwnProperty(nome.lexema)) {
89
+ this.interpretador.resolver(expr, this.escopos.pilha.length - 1 - i);
90
+ }
91
+ }
92
+ }
93
+
94
+ visitBlockStmt(stmt: any) : any {
95
+ this.inicioDoEscopo();
96
+ this.resolver(stmt.declaracoes);
97
+ this.finalDoEscopo();
98
+ return null;
99
+ }
100
+
101
+ visitVariableExpr(expr: any): any {
102
+ if (
103
+ !this.escopos.eVazio() &&
104
+ this.escopos.peek()[expr.nome.lexema] === false
105
+ ) {
106
+ throw new ResolverError(
107
+ "Não é possível ler a variável local em seu próprio inicializador."
108
+ );
109
+ }
110
+ this.resolverLocal(expr, expr.nome);
111
+ return null;
112
+ }
113
+
114
+ visitVarStmt(stmt: any): any {
115
+ this.declarar(stmt.nome);
116
+ if (stmt.inicializador !== null) {
117
+ this.resolver(stmt.inicializador);
118
+ }
119
+ this.definir(stmt.nome);
120
+ return null;
121
+ }
122
+
123
+ visitAssignExpr(expr: any): any {
124
+ this.resolver(expr.valor);
125
+ this.resolverLocal(expr, expr.nome);
126
+ return null;
127
+ }
128
+
129
+ resolverFuncao(funcao: any, funcType: any): void {
130
+ let enclosingFunc = this.FuncaoAtual;
131
+ this.FuncaoAtual = funcType;
132
+
133
+ this.inicioDoEscopo();
134
+ let parametros = funcao.parametros;
135
+
136
+ if (parametros && parametros.length > 0) {
137
+ for (let i = 0; i < parametros.length; i++) {
138
+ this.declarar(parametros[i]["nome"]);
139
+ this.definir(parametros[i]["nome"]);
140
+ }
141
+ }
142
+
143
+ this.resolver(funcao.funcao);
144
+ this.finalDoEscopo();
145
+
146
+ this.FuncaoAtual = enclosingFunc;
147
+ }
148
+
149
+ visitFunctionStmt(stmt: any): any {
150
+ this.declarar(stmt.nome);
151
+ this.definir(stmt.nome);
152
+
153
+ this.resolverFuncao(stmt.funcao, TipoFuncao.FUNCAO);
154
+ return null;
155
+ }
156
+
157
+ visitFunctionExpr(stmt: any): any {
158
+ this.resolverFuncao(stmt, TipoFuncao.FUNCAO);
159
+ return null;
160
+ }
161
+
162
+ visitTryStmt(stmt: any): any {
163
+ this.resolver(stmt.tryBranch);
164
+
165
+ if (stmt.catchBranch !== null) this.resolver(stmt.catchBranch);
166
+ if (stmt.elseBranch !== null) this.resolver(stmt.elseBranch);
167
+ if (stmt.finallyBranch !== null) this.resolver(stmt.finallyBranch);
168
+ }
169
+
170
+ visitClassStmt(stmt: any): any {
171
+ let enclosingClass = this.ClasseAtual;
172
+ this.ClasseAtual = TipoClasse.CLASSE;
173
+
174
+ this.declarar(stmt.nome);
175
+ this.definir(stmt.nome);
176
+
177
+ if (
178
+ stmt.superClasse !== null &&
179
+ stmt.nome.lexema === stmt.superClasse.nome.lexema
180
+ ) {
181
+ this.Delegua.error("Uma classe não pode herdar de si mesma.");
182
+ }
183
+
184
+ if (stmt.superClasse !== null) {
185
+ this.ClasseAtual = TipoClasse.SUBCLASSE;
186
+ this.resolver(stmt.superClasse);
187
+ }
188
+
189
+ if (stmt.superClasse !== null) {
190
+ this.inicioDoEscopo();
191
+ this.escopos.peek()["super"] = true;
192
+ }
193
+
194
+ this.inicioDoEscopo();
195
+ this.escopos.peek()["isto"] = true;
196
+
197
+ let metodos = stmt.metodos;
198
+ for (let i = 0; i < metodos.length; i++) {
199
+ let declaracao = TipoFuncao.METODO;
200
+
201
+ if (metodos[i].nome.lexema === "isto") {
202
+ declaracao = TipoFuncao.CONSTRUTOR;
203
+ }
204
+
205
+ this.resolverFuncao(metodos[i].funcao, declaracao);
206
+ }
207
+
208
+ this.finalDoEscopo();
209
+
210
+ if (stmt.superClasse !== null) this.finalDoEscopo();
211
+
212
+ this.ClasseAtual = enclosingClass;
213
+ return null;
214
+ }
215
+
216
+ visitSuperExpr(expr: any): any {
217
+ if (this.ClasseAtual === TipoClasse.NENHUM) {
218
+ this.Delegua.error(expr.palavraChave, "Não pode usar 'super' fora de uma classe.");
219
+ } else if (this.ClasseAtual !== TipoClasse.SUBCLASSE) {
220
+ this.Delegua.error(
221
+ expr.palavraChave,
222
+ "Não se usa 'super' numa classe sem SuperClasse."
223
+ );
224
+ }
225
+
226
+ this.resolverLocal(expr, expr.palavraChave);
227
+ return null;
228
+ }
229
+
230
+ visitGetExpr(expr: any): any {
231
+ this.resolver(expr.objeto);
232
+ return null;
233
+ }
234
+
235
+ visitExpressionStmt(stmt: any): any {
236
+ this.resolver(stmt.expressao);
237
+ return null;
238
+ }
239
+
240
+ visitIfStmt(stmt: any): any {
241
+ this.resolver(stmt.condicao);
242
+ this.resolver(stmt.thenBranch);
243
+
244
+ for (let i = 0; i < stmt.elifBranches.length; i++) {
245
+ this.resolver(stmt.elifBranches[i].condicao);
246
+ this.resolver(stmt.elifBranches[i].branch);
247
+ }
248
+
249
+ if (stmt.elseBranch !== null) this.resolver(stmt.elseBranch);
250
+ return null;
251
+ }
252
+
253
+ visitImportStmt(stmt: any): void {
254
+ this.resolver(stmt.caminho);
255
+ }
256
+
257
+ visitPrintStmt(stmt: any): void {
258
+ this.resolver(stmt.expressao);
259
+ }
260
+
261
+ visitReturnStmt(stmt: any): any {
262
+ if (this.FuncaoAtual === TipoFuncao.NENHUM) {
263
+ this.Delegua.error(stmt.palavraChave, "Não é possível retornar do código do escopo superior.");
264
+ }
265
+
266
+ if (stmt.valor !== null) {
267
+ if (this.FuncaoAtual === TipoFuncao.CONSTRUTOR) {
268
+ this.Delegua.error(
269
+ stmt.palavraChave,
270
+ "Não pode retornar o valor do construtor."
271
+ );
272
+ }
273
+ this.resolver(stmt.valor);
274
+ }
275
+ return null;
276
+ }
277
+
278
+ visitSwitchStmt(stmt: any): void {
279
+ let enclosingType = this.cicloAtual;
280
+ this.cicloAtual = LoopType.ESCOLHA;
281
+
282
+ let branches = stmt.branches;
283
+ let defaultBranch = stmt.defaultBranch;
284
+
285
+ for (let i = 0; i < branches.length; i++) {
286
+ this.resolver(branches[i]["stmts"]);
287
+ }
288
+
289
+ if (defaultBranch !== null) this.resolver(defaultBranch["stmts"]);
290
+
291
+ this.cicloAtual = enclosingType;
292
+ }
293
+
294
+ visitWhileStmt(stmt: any): any {
295
+ this.resolver(stmt.condicao);
296
+ this.resolver(stmt.corpo);
297
+ return null;
298
+ }
299
+
300
+ visitForStmt(stmt: any): any {
301
+ if (stmt.inicializador !== null) {
302
+ this.resolver(stmt.inicializador);
303
+ }
304
+ if (stmt.condicao !== null) {
305
+ this.resolver(stmt.condicao);
306
+ }
307
+ if (stmt.incrementar !== null) {
308
+ this.resolver(stmt.incrementar);
309
+ }
310
+
311
+ let enclosingType = this.cicloAtual;
312
+ this.cicloAtual = LoopType.ENQUANTO;
313
+ this.resolver(stmt.corpo);
314
+ this.cicloAtual = enclosingType;
315
+
316
+ return null;
317
+ }
318
+
319
+ visitDoStmt(stmt: any): any {
320
+ this.resolver(stmt.whileCondition);
321
+
322
+ let enclosingType = this.cicloAtual;
323
+ this.cicloAtual = LoopType.FAZER;
324
+ this.resolver(stmt.doBranch);
325
+ this.cicloAtual = enclosingType;
326
+ return null;
327
+ }
328
+
329
+ visitBinaryExpr(expr: any): any {
330
+ this.resolver(expr.esquerda);
331
+ this.resolver(expr.direita);
332
+ return null;
333
+ }
334
+
335
+ visitCallExpr(expr: any): any {
336
+ this.resolver(expr.callee);
337
+
338
+ let argumentos = expr.argumentos;
339
+ for (let i = 0; i < argumentos.length; i++) {
340
+ this.resolver(argumentos[i]);
341
+ }
342
+
343
+ return null;
344
+ }
345
+
346
+ visitGroupingExpr(expr: any): any {
347
+ this.resolver(expr.expressao);
348
+ return null;
349
+ }
350
+
351
+ visitDictionaryExpr(expr: any): any {
352
+ for (let i = 0; i < expr.chaves.length; i++) {
353
+ this.resolver(expr.chaves[i]);
354
+ this.resolver(expr.valores[i]);
355
+ }
356
+ return null;
357
+ }
358
+
359
+ visitArrayExpr(expr: any): any {
360
+ for (let i = 0; i < expr.valores.length; i++) {
361
+ this.resolver(expr.valores[i]);
362
+ }
363
+ return null;
364
+ }
365
+
366
+ visitSubscriptExpr(expr: any): any {
367
+ this.resolver(expr.callee);
368
+ this.resolver(expr.indice);
369
+ return null;
370
+ }
371
+
372
+ visitContinueStmt(stmt?: any): any {
373
+ return null;
374
+ }
375
+
376
+ visitBreakStmt(stmt?: any): any {
377
+ return null;
378
+ }
379
+
380
+ visitAssignsubscriptExpr(expr?: any): any {
381
+ return null;
382
+ }
383
+
384
+ visitLiteralExpr(expr?: any): any {
385
+ return null;
386
+ }
387
+
388
+ visitLogicalExpr(expr?: any): any {
389
+ this.resolver(expr.esquerda);
390
+ this.resolver(expr.direita);
391
+ return null;
392
+ }
393
+
394
+ visitUnaryExpr(expr?: any): any {
395
+ this.resolver(expr.direita);
396
+ return null;
397
+ }
398
+
399
+ visitSetExpr(expr?: any): any {
400
+ this.resolver(expr.valor);
401
+ this.resolver(expr.objeto);
402
+ return null;
403
+ }
404
+
405
+ visitThisExpr(expr?: any): any {
406
+ if (this.ClasseAtual == TipoClasse.NENHUM) {
407
+ this.Delegua.error(expr.palavraChave, "Não pode usar 'isto' fora da classe.");
408
+ }
409
+ this.resolverLocal(expr, expr.palavraChave);
410
+ return null;
411
+ }
412
+ };
@@ -0,0 +1 @@
1
+ export * from './egua-classico'