@designliquido/delegua 0.0.1

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.
@@ -0,0 +1,62 @@
1
+ const Callable = require("./callable.js");
2
+ const Environment = require("../environment.js");
3
+ const ReturnExpection = require("../errors.js").ReturnException;
4
+
5
+ module.exports = class EguaFunction extends Callable {
6
+ constructor(name, declaration, closure, isInitializer = false) {
7
+ super();
8
+ this.name = name;
9
+ this.declaration = declaration;
10
+ this.closure = closure;
11
+ this.isInitializer = isInitializer;
12
+ }
13
+
14
+ arity() {
15
+ return this.declaration.params.length;
16
+ }
17
+
18
+ toString() {
19
+ if (this.name === null) return "<função>";
20
+ return `<função ${this.name}>`;
21
+ }
22
+
23
+ call(interpreter, args) {
24
+ let environment = new Environment(this.closure);
25
+ let params = this.declaration.params;
26
+ for (let i = 0; i < params.length; i++) {
27
+ let param = params[i];
28
+
29
+ let name = param["name"].lexeme;
30
+ let value = args[i];
31
+ if (args[i] === null) {
32
+ value = param["default"] ? param["default"].value : null;
33
+ }
34
+ environment.defineVar(name, value);
35
+ }
36
+
37
+ try {
38
+ interpreter.executeBlock(this.declaration.body, environment);
39
+ } catch (error) {
40
+ if (error instanceof ReturnExpection) {
41
+ if (this.isInitializer) return this.closure.getVarAt(0, "isto");
42
+ return error.value;
43
+ } else {
44
+ throw error;
45
+ }
46
+ }
47
+
48
+ if (this.isInitializer) return this.closure.getVarAt(0, "isto");
49
+ return null;
50
+ }
51
+
52
+ bind(instance) {
53
+ let environment = new Environment(this.closure);
54
+ environment.defineVar("isto", instance);
55
+ return new EguaFunction(
56
+ this.name,
57
+ this.declaration,
58
+ environment,
59
+ this.isInitializer
60
+ );
61
+ }
62
+ };
@@ -0,0 +1,27 @@
1
+ const RuntimeError = require("../errors.js").RuntimeError;
2
+
3
+ module.exports = class EguaInstance {
4
+ constructor(creatorClass) {
5
+ this.creatorClass = creatorClass;
6
+ this.fields = {};
7
+ }
8
+
9
+ get(name) {
10
+ if (this.fields.hasOwnProperty(name.lexeme)) {
11
+ return this.fields[name.lexeme];
12
+ }
13
+
14
+ let method = this.creatorClass.findMethod(name.lexeme);
15
+ if (method) return method.bind(this);
16
+
17
+ throw new RuntimeError(name, "Método indefinido não recuperado.");
18
+ }
19
+
20
+ set(name, value) {
21
+ this.fields[name.lexeme] = value;
22
+ }
23
+
24
+ toString() {
25
+ return "<Objeto " + this.creatorClass.name + ">";
26
+ }
27
+ };
@@ -0,0 +1,9 @@
1
+ module.exports = class EguaModule {
2
+ constructor(name) {
3
+ if (name !== undefined) this.name = name;
4
+ }
5
+
6
+ toString() {
7
+ return this.name ? `<module ${this.name}>` : "<module>";
8
+ }
9
+ };
@@ -0,0 +1,18 @@
1
+ const Callable = require("./callable.js");
2
+
3
+ module.exports = class StandardFn extends Callable {
4
+ constructor(arityValue, func) {
5
+ super();
6
+ this.arityValue = arityValue;
7
+ this.func = func;
8
+ }
9
+
10
+ call(interpreter, args, token) {
11
+ this.token = token;
12
+ return this.func.apply(this, args);
13
+ }
14
+
15
+ toString() {
16
+ return "<função>";
17
+ }
18
+ };
@@ -0,0 +1,67 @@
1
+ module.exports = {
2
+ BANG: "BANG",
3
+ BANG_EQUAL: "BANG_EQUAL",
4
+ BIT_AND: "BIT_AND",
5
+ BIT_OR: "BIT_OR",
6
+ BIT_XOR: "BIT_XOR",
7
+ BIT_NOT: "BIT_NOT",
8
+ CASO: "CASO",
9
+ CLASSE: "CLASSE",
10
+ COLON: "COLON",
11
+ COMMA: "COMMA",
12
+ CONTINUA: "CONTINUA",
13
+ DOT: "DOT",
14
+ E: "E",
15
+ EM: "EM",
16
+ ENQUANTO: "ENQUANTO",
17
+ EOF: "EOF",
18
+ EQUAL: "EQUAL",
19
+ EQUAL_EQUAL: "EQUAL_EQUAL",
20
+ ESCOLHA: "ESCOLHA",
21
+ ESCREVA: "ESCREVA",
22
+ FALSO: "FALSO",
23
+ FAZER: "FAZER",
24
+ FINALMENTE: "FINALMENTE",
25
+ FUNCAO: "FUNCAO",
26
+ GREATER: "GREATER",
27
+ GREATER_EQUAL: "GREATER_EQUAL",
28
+ GREATER_GREATER: "GREATER_GREATER",
29
+ HERDA: "HERDA",
30
+ IDENTIFIER: "IDENTIFIER",
31
+ IMPORTAR: "IMPORTAR",
32
+ ISTO: "ISTO",
33
+ LEFT_BRACE: "LEFT_BRACE",
34
+ LEFT_PAREN: "LEFT_PAREN",
35
+ LEFT_SQUARE_BRACKET: "LEFT_SQUARE_BRACKET",
36
+ LESS: "LESS",
37
+ LESS_EQUAL: "LESS_EQUAL",
38
+ LESSER_LESSER: "LESSER_LESSER",
39
+ MAIS_IGUAL: "MAIS_IGUAL",
40
+ MENOR_IGUAL: "MENOR_IGUAL",
41
+ MINUS: "MINUS",
42
+ MODULUS: "MODULUS",
43
+ NULO: "NULO",
44
+ NUMBER: "NUMBER",
45
+ OU: "OU",
46
+ PADRAO: "PADRAO",
47
+ PARA: "PARA",
48
+ PAUSA: "PAUSA",
49
+ PEGUE: "PEGUE",
50
+ PLUS: "PLUS",
51
+ RETORNA: "RETORNA",
52
+ RIGHT_PAREN: "RIGHT_PAREN",
53
+ RIGHT_BRACE: "RIGHT_BRACE",
54
+ RIGHT_SQUARE_BRACKET: "RIGHT_SQUARE_BRACKET",
55
+ SEMICOLON: "SEMICOLON",
56
+ SLASH: "SLASH",
57
+ STAR: "STAR",
58
+ STAR_STAR: "STAR_STAR",
59
+ STRING: "STRING",
60
+ SE: "SE",
61
+ SENAO: "SENAO",
62
+ SENAOSE: "SENAOSE",
63
+ SUPER: "SUPER",
64
+ TENTE: "TENTE",
65
+ VAR: "VAR",
66
+ VERDADEIRO: "VERDADEIRO"
67
+ };
package/src/web.js ADDED
@@ -0,0 +1,70 @@
1
+ const Lexer = require("./lexer.js");
2
+ const Parser = require("./parser.js");
3
+ const Resolver = require("./resolver.js");
4
+ const Interpreter = require("./interpreter.js");
5
+ const tokenTypes = require("./tokenTypes.js");
6
+
7
+ module.exports.Egua = class Egua {
8
+ constructor(filename) {
9
+ this.filename = filename;
10
+
11
+ this.hadError = false;
12
+ this.hadRuntimeError = false;
13
+ }
14
+
15
+ runBlock(code) {
16
+ const interpreter = new Interpreter(this, process.cwd());
17
+
18
+ const lexer = new Lexer(code, this);
19
+ const tokens = lexer.scan();
20
+
21
+ if (this.hadError === true) return;
22
+
23
+ const parser = new Parser(tokens, this);
24
+ const statements = parser.parse();
25
+
26
+ if (this.hadError === true) return;
27
+
28
+ const resolver = new Resolver(interpreter, this);
29
+ resolver.resolve(statements);
30
+
31
+ if (this.hadError === true) return;
32
+
33
+ interpreter.interpret(statements);
34
+ }
35
+
36
+ report(line, where, message) {
37
+ if (this.filename)
38
+ console.error(
39
+ `[Arquivo: ${this.filename}] [Linha: ${line}] Erro${where}: ${message}`
40
+ );
41
+ else console.error(`[Linha: ${line}] Erro${where}: ${message}`);
42
+ this.hadError = true;
43
+ }
44
+
45
+ error(token, errorMessage) {
46
+ if (token.type === tokenTypes.EOF) {
47
+ this.report(token.line, " no fim", errorMessage);
48
+ } else {
49
+ this.report(token.line, ` no '${token.lexeme}'`, errorMessage);
50
+ }
51
+ }
52
+
53
+ lexerError(line, char, msg) {
54
+ this.report(line, ` no '${char}'`, msg);
55
+ }
56
+
57
+ runtimeError(error) {
58
+ let line = error.token.line;
59
+ if (error.token && line) {
60
+ if (this.filename)
61
+ console.error(
62
+ `Erro: [Arquivo: ${this.filename}] [Linha: ${error.token.line}] ${error.message}`
63
+ );
64
+ else console.error(`Erro: [Linha: ${error.token.line}] ${error.message}`);
65
+ } else {
66
+ console.error(error);
67
+ }
68
+ this.hadRuntimeError = true;
69
+ }
70
+ };