@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
package/src/ambiente.js DELETED
@@ -1,53 +0,0 @@
1
- const ErroEmTempoDeExecucao = require("./erro.js").ErroEmTempoDeExecucao;
2
-
3
- module.exports = class Ambiente {
4
- constructor(enclosing) {
5
- this.enclosing = enclosing || null;
6
- this.valores = {};
7
- }
8
-
9
- definirVariavel(nomeVariavel, valor) {
10
- this.valores[nomeVariavel] = valor;
11
- }
12
-
13
- atribuirVariavelEm(distancia, nome, valor) {
14
- this.ancestor(distancia).valores[nome.lexeme] = valor;
15
- }
16
-
17
- atribuirVariavel(nome, valor) {
18
- if (this.valores[nome.lexeme] !== undefined) {
19
- this.valores[nome.lexeme] = valor;
20
- return;
21
- }
22
-
23
- if (this.enclosing != null) {
24
- this.enclosing.atribuirVariavel(nome, valor);
25
- return;
26
- }
27
-
28
- throw new ErroEmTempoDeExecucao(nome, "Variável não definida '" + nome.lexeme + "'.");
29
- }
30
-
31
- ancestor(distancia) {
32
- let ambiente = this;
33
- for (let i = 0; i < distancia; i++) {
34
- ambiente = ambiente.enclosing;
35
- }
36
-
37
- return ambiente;
38
- }
39
-
40
- obterVariavelEm(distancia, nome) {
41
- return this.ancestor(distancia).valores[nome];
42
- }
43
-
44
- obterVariavel(simbolo) {
45
- if (this.valores[simbolo.lexeme] !== undefined) {
46
- return this.valores[simbolo.lexeme];
47
- }
48
-
49
- if (this.enclosing !== null) return this.enclosing.obterVariavel(simbolo);
50
-
51
- throw new ErroEmTempoDeExecucao(simbolo, "Variável não definida '" + simbolo.lexeme + "'.");
52
- }
53
- };
package/src/delegua.js DELETED
@@ -1,102 +0,0 @@
1
- const Lexer = require("./lexer.js");
2
- const Parser = require("./parser.js");
3
- const Resolver = require("./resolver.js");
4
- const Interpretador = require("./interpretador.js");
5
- const tiposDeSimbolos = require("./tiposDeSimbolos.js");
6
- const fs = require("fs");
7
- const caminho = require("path");
8
- const readline = require("readline");
9
-
10
- module.exports.Delegua = class Delegua {
11
- constructor(nomeArquivo) {
12
- this.nomeArquivo = nomeArquivo;
13
-
14
- this.teveErro = false;
15
- this.teveErroEmTempoDeExecucao = false;
16
- }
17
-
18
- runPrompt() {
19
- const interpretador = new Interpretador(this, process.cwd(), undefined);
20
- console.log("Console da Linguagem Delégua v0.0.1");
21
- const leiaLinha = readline.createInterface({
22
- input: process.stdin,
23
- output: process.stdout,
24
- prompt: "\ndelegua> "
25
- });
26
-
27
- leiaLinha.prompt();
28
-
29
- leiaLinha.on("line", linha => {
30
- this.teveErro = false;
31
- this.teveErroEmTempoDeExecucao = false;
32
-
33
- this.run(linha, interpretador);
34
- leiaLinha.prompt();
35
- });
36
- }
37
-
38
- runfile(nomeArquivo) {
39
- this.nomeArquivo = caminho.basename(nomeArquivo);
40
- const interpretador = new Interpretador(this, process.cwd());
41
-
42
- const dadosDoArquivo = fs.readFileSync(nomeArquivo).toString();
43
- this.run(dadosDoArquivo, interpretador);
44
-
45
- if (this.teveErro) process.exit(65);
46
- if (this.teveErroEmTempoDeExecucao) process.exit(70);
47
- }
48
-
49
- run(codigo, interpretador) {
50
- const lexer = new Lexer(codigo, this);
51
- const simbolos = lexer.scan();
52
-
53
- if (this.teveErro === true) return;
54
-
55
- const analisar = new Parser(simbolos, this);
56
- const declaracoes = analisar.analisar();
57
-
58
- if (this.teveErro === true) return;
59
-
60
- const resolver = new Resolver(interpretador, this);
61
- resolver.resolver(declaracoes);
62
-
63
- if (this.teveErro === true) return;
64
-
65
- interpretador.interpretar(declaracoes);
66
- }
67
-
68
- reportar(linha, onde, mensagem) {
69
- if (this.nomeArquivo)
70
- console.error(
71
- `[Arquivo: ${this.nomeArquivo}] [Linha: ${linha}] Erro${onde}: ${mensagem}`
72
- );
73
- else console.error(`[Linha: ${linha}] Erro${onde}: ${mensagem}`);
74
- this.teveErro = true;
75
- }
76
-
77
- erro(simbolo, mensagemDeErro) {
78
- if (simbolo.tipo === tiposDeSimbolos.EOF) {
79
- this.reportar(simbolo.line, " no final", mensagemDeErro);
80
- } else {
81
- this.reportar(simbolo.line, ` no '${simbolo.lexeme}'`, mensagemDeErro);
82
- }
83
- }
84
-
85
- lexerError(linha, caractere, mensagem) {
86
- this.reportar(linha, ` no '${caractere}'`, mensagem);
87
- }
88
-
89
- erroEmTempoDeExecucao(erro) {
90
- const linha = erro.simbolo.linha;
91
- if (erro.simbolo && linha) {
92
- if (this.nomeArquivo)
93
- console.error(
94
- `Erro: [Arquivo: ${this.nomeArquivo}] [Linha: ${erro.simbolo.linha}] ${erro.mensagem}`
95
- );
96
- else console.error(`Erro: [Linha: ${erro.simbolo.linha}] ${erro.mensagem}`);
97
- } else {
98
- console.error(`Erro: ${erro.mensagem}`);
99
- }
100
- this.teveErroEmTempoDeExecucao = true;
101
- }
102
- };
package/src/erro.js DELETED
@@ -1,18 +0,0 @@
1
- module.exports.ErroEmTempoDeExecucao = class ErroEmTempoDeExecucao extends Error {
2
- constructor(simbolo, mensagem) {
3
- super(mensagem);
4
- this.simbolo = simbolo;
5
- this.mensagem = mensagem;
6
- }
7
- };
8
-
9
- module.exports.ContinueException = class ContinueException extends Error { };
10
-
11
- module.exports.BreakException = class BreakException extends Error { };
12
-
13
- module.exports.ReturnException = class ReturnException extends Error {
14
- constructor(valor) {
15
- super(valor);
16
- this.valor = valor;
17
- }
18
- };
@@ -1,5 +0,0 @@
1
- module.exports = class Callable {
2
- aridade() {
3
- return this.valorAridade;
4
- }
5
- };
@@ -1,62 +0,0 @@
1
- const Callable = require("./callable.js");
2
- const Ambiente = require("../ambiente.js");
3
- const ReturnExpection = require("../erro.js").ReturnException;
4
-
5
- module.exports = class DeleguaFuncao extends Callable {
6
- constructor(nome, declaracao, closure, eInicializador = false) {
7
- super();
8
- this.nome = nome;
9
- this.declaracao = declaracao;
10
- this.closure = closure;
11
- this.eInicializador = eInicializador;
12
- }
13
-
14
- aridade() {
15
- return this.declaracao.parametros.length;
16
- }
17
-
18
- toString() {
19
- if (this.nome === null) return "<função>";
20
- return `<função ${this.nome}>`;
21
- }
22
-
23
- call(interpretador, argumentos) {
24
- let ambiente = new Ambiente(this.closure);
25
- let parametros = this.declaracao.parametros;
26
- for (let i = 0; i < parametros.length; i++) {
27
- const param = parametros[i];
28
-
29
- const nome = param["nome"].lexeme;
30
- let valor = argumentos[i];
31
- if (argumentos[i] === null) {
32
- valor = param["padrao"] ? param["padrao"].valor : null;
33
- }
34
- ambiente.definirVariavel(nome, valor);
35
- }
36
-
37
- try {
38
- interpretador.executeBlock(this.declaracao.corpo, ambiente);
39
- } catch (erro) {
40
- if (erro instanceof ReturnExpection) {
41
- if (this.eInicializador) return this.closure.obterVariavelEm(0, "isto");
42
- return erro.valor;
43
- } else {
44
- throw erro;
45
- }
46
- }
47
-
48
- if (this.eInicializador) return this.closure.obterVariavelEm(0, "isto");
49
- return null;
50
- }
51
-
52
- bind(instancia) {
53
- let ambiente = new Ambiente(this.closure);
54
- ambiente.definirVariavel("isto", instancia);
55
- return new DeleguaFuncao(
56
- this.nome,
57
- this.declaracao,
58
- ambiente,
59
- this.eInicializador
60
- );
61
- }
62
- };
@@ -1,9 +0,0 @@
1
- module.exports = class DeleguaModulo {
2
- constructor(nome) {
3
- if (nome !== undefined) this.nome = nome;
4
- }
5
-
6
- toString() {
7
- return this.nome ? `<modulo ${this.nome}>` : "<modulo>";
8
- }
9
- };
package/src/expr.js DELETED
@@ -1,228 +0,0 @@
1
- class Expr {
2
- aceitar(visitor) {}
3
- }
4
-
5
- class Assign extends Expr {
6
- constructor(nome, valor) {
7
- super();
8
- this.nome = nome;
9
- this.valor = valor;
10
- }
11
-
12
- aceitar(visitor) {
13
- return visitor.visitAssignExpr(this);
14
- }
15
- }
16
-
17
- class Binary extends Expr {
18
- constructor(esquerda, operador, direita) {
19
- super();
20
- this.esquerda = esquerda;
21
- this.operador = operador;
22
- this.direita = direita;
23
- }
24
-
25
- aceitar(visitor) {
26
- return visitor.visitBinaryExpr(this);
27
- }
28
- }
29
-
30
- class Funcao extends Expr {
31
- constructor(parametros, corpo) {
32
- super();
33
- this.parametros = parametros;
34
- this.corpo = corpo;
35
- }
36
-
37
- aceitar(visitor) {
38
- return visitor.visitFunctionExpr(this);
39
- }
40
- }
41
-
42
- class Call extends Expr {
43
- constructor(callee, paren, argumentos) {
44
- super();
45
- this.callee = callee;
46
- this.paren = paren;
47
- this.argumentos = argumentos;
48
- }
49
-
50
- aceitar(visitor) {
51
- return visitor.visitCallExpr(this);
52
- }
53
- }
54
-
55
- class Get extends Expr {
56
- constructor(objeto, nome) {
57
- super();
58
- this.objeto = objeto;
59
- this.nome = nome;
60
- }
61
-
62
- aceitar(visitor) {
63
- return visitor.visitGetExpr(this);
64
- }
65
- }
66
-
67
- class Grouping extends Expr {
68
- constructor(expressao) {
69
- super();
70
- this.expressao = expressao;
71
- }
72
-
73
- aceitar(visitor) {
74
- return visitor.visitGroupingExpr(this);
75
- }
76
- }
77
-
78
- class Literal extends Expr {
79
- constructor(valor) {
80
- super();
81
- this.valor = valor;
82
- }
83
-
84
- aceitar(visitor) {
85
- return visitor.visitLiteralExpr(this);
86
- }
87
- }
88
-
89
- class Array extends Expr {
90
- constructor(valores) {
91
- super();
92
- this.valores = valores;
93
- }
94
-
95
- aceitar(visitor) {
96
- return visitor.visitArrayExpr(this);
97
- }
98
- }
99
-
100
- class Dicionario extends Expr {
101
- constructor(chaves, valores) {
102
- super();
103
- this.chaves = chaves;
104
- this.valores = valores;
105
- }
106
-
107
- aceitar(visitor) {
108
- return visitor.visitDictionaryExpr(this);
109
- }
110
- }
111
-
112
- class Subscript extends Expr {
113
- constructor(callee, indice, closeBracket) {
114
- super();
115
- this.callee = callee;
116
- this.indice = indice;
117
- this.closeBracket = closeBracket;
118
- }
119
-
120
- aceitar(visitor) {
121
- return visitor.visitSubscriptExpr(this);
122
- }
123
- }
124
-
125
- class Assignsubscript extends Expr {
126
- constructor(objeto, indice, valor) {
127
- super();
128
- this.objeto = objeto;
129
- this.indice = indice;
130
- this.valor = valor;
131
- }
132
-
133
- aceitar(visitor) {
134
- return visitor.visitAssignsubscriptExpr(this);
135
- }
136
- }
137
-
138
- class Logical extends Expr {
139
- constructor(esquerda, operador, direita) {
140
- super();
141
- this.esquerda = esquerda;
142
- this.operador = operador;
143
- this.direita = direita;
144
- }
145
-
146
- aceitar(visitor) {
147
- return visitor.visitLogicalExpr(this);
148
- }
149
- }
150
-
151
- class Set extends Expr {
152
- constructor(objeto, nome, valor) {
153
- super();
154
- this.objeto = objeto;
155
- this.nome = nome;
156
- this.valor = valor;
157
- }
158
-
159
- aceitar(visitor) {
160
- return visitor.visitSetExpr(this);
161
- }
162
- }
163
-
164
- class Super extends Expr {
165
- constructor(palavraChave, metodo) {
166
- super();
167
- this.palavraChave = palavraChave;
168
- this.metodo = metodo;
169
- }
170
-
171
- aceitar(visitor) {
172
- return visitor.visitSuperExpr(this);
173
- }
174
- }
175
-
176
- class Isto extends Expr {
177
- constructor(palavraChave) {
178
- super();
179
- this.palavraChave = palavraChave;
180
- }
181
-
182
- aceitar(visitor) {
183
- return visitor.visitThisExpr(this);
184
- }
185
- }
186
-
187
- class Unary extends Expr {
188
- constructor(operador, direita) {
189
- super();
190
- this.operador = operador;
191
- this.direita = direita;
192
- }
193
-
194
- aceitar(visitor) {
195
- return visitor.visitUnaryExpr(this);
196
- }
197
- }
198
-
199
- class Variavel extends Expr {
200
- constructor(nome) {
201
- super();
202
- this.nome = nome;
203
- }
204
-
205
- aceitar(visitor) {
206
- return visitor.visitVariableExpr(this);
207
- }
208
- }
209
-
210
- module.exports = {
211
- Assign,
212
- Binary,
213
- Funcao,
214
- Call,
215
- Get,
216
- Grouping,
217
- Literal,
218
- Array,
219
- Dicionario,
220
- Subscript,
221
- Assignsubscript,
222
- Logical,
223
- Set,
224
- Super,
225
- Isto,
226
- Unary,
227
- Variavel
228
- };