hispano-lang 1.0.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/LICENSE +21 -0
- package/README.md +404 -0
- package/bin/hispano.js +313 -0
- package/dist/cli.d.ts +46 -0
- package/dist/evaluator.js +1391 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +74 -0
- package/dist/interpreter.js +73 -0
- package/dist/parser.js +1050 -0
- package/dist/tokenizer.js +445 -0
- package/package.json +72 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HispanoLang - TypeScript Definitions
|
|
3
|
+
* Un lenguaje de programación educativo en español
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resultado de la interpretación de código
|
|
8
|
+
*/
|
|
9
|
+
export interface InterpretationResult {
|
|
10
|
+
/** Indica si la ejecución fue exitosa */
|
|
11
|
+
success: boolean;
|
|
12
|
+
/** Array de salidas generadas durante la ejecución */
|
|
13
|
+
output: string[];
|
|
14
|
+
/** Mensaje de error si la ejecución falló */
|
|
15
|
+
error: string | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Token del tokenizador
|
|
20
|
+
*/
|
|
21
|
+
export interface Token {
|
|
22
|
+
/** Tipo del token */
|
|
23
|
+
type: string;
|
|
24
|
+
/** Lexema del token */
|
|
25
|
+
lexeme: string;
|
|
26
|
+
/** Valor literal del token */
|
|
27
|
+
literal: any;
|
|
28
|
+
/** Línea donde se encontró el token */
|
|
29
|
+
line: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Nodo del AST (Abstract Syntax Tree)
|
|
34
|
+
*/
|
|
35
|
+
export interface ASTNode {
|
|
36
|
+
/** Tipo del nodo */
|
|
37
|
+
type: string;
|
|
38
|
+
/** Propiedades específicas del nodo */
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Entorno de variables
|
|
44
|
+
*/
|
|
45
|
+
export interface Environment {
|
|
46
|
+
/** Valores de las variables */
|
|
47
|
+
values: { [key: string]: any };
|
|
48
|
+
/** Entorno padre (para scope) */
|
|
49
|
+
enclosing?: Environment;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Clase principal del intérprete HispanoLang
|
|
54
|
+
*/
|
|
55
|
+
export class Interpreter {
|
|
56
|
+
/** Tokenizador del intérprete */
|
|
57
|
+
tokenizer: Tokenizer;
|
|
58
|
+
/** Evaluador del intérprete */
|
|
59
|
+
evaluator: Evaluator;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Crea una nueva instancia del intérprete
|
|
63
|
+
*/
|
|
64
|
+
constructor();
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Interpreta código fuente en español
|
|
68
|
+
* @param source - Código fuente a interpretar
|
|
69
|
+
* @returns Resultado de la interpretación
|
|
70
|
+
*/
|
|
71
|
+
interpret(source: string): InterpretationResult;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Ejecuta código y retorna solo las salidas
|
|
75
|
+
* @param source - Código fuente a ejecutar
|
|
76
|
+
* @returns Array de salidas
|
|
77
|
+
*/
|
|
78
|
+
run(source: string): string[];
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Obtiene el entorno de variables actual
|
|
82
|
+
* @returns Entorno de variables
|
|
83
|
+
*/
|
|
84
|
+
getEnvironment(): { [key: string]: any };
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Limpia el entorno de variables
|
|
88
|
+
*/
|
|
89
|
+
clearEnvironment(): void;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Tokenizador para HispanoLang
|
|
94
|
+
*/
|
|
95
|
+
export class Tokenizer {
|
|
96
|
+
/**
|
|
97
|
+
* Tokeniza código fuente y retorna una lista de tokens
|
|
98
|
+
* @param source - Código fuente a tokenizar
|
|
99
|
+
* @returns Lista de tokens
|
|
100
|
+
*/
|
|
101
|
+
tokenize(source: string): Token[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Parser para HispanoLang
|
|
106
|
+
*/
|
|
107
|
+
export class Parser {
|
|
108
|
+
/**
|
|
109
|
+
* Crea una nueva instancia del parser
|
|
110
|
+
* @param tokens - Lista de tokens a parsear
|
|
111
|
+
*/
|
|
112
|
+
constructor(tokens: Token[]);
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Parsea tokens y retorna una lista de declaraciones
|
|
116
|
+
* @returns Lista de declaraciones (AST)
|
|
117
|
+
*/
|
|
118
|
+
parse(): ASTNode[];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Evaluador para HispanoLang
|
|
123
|
+
*/
|
|
124
|
+
export class Evaluator {
|
|
125
|
+
/** Entorno de variables */
|
|
126
|
+
environment: Environment;
|
|
127
|
+
/** Array de salidas */
|
|
128
|
+
output: string[];
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Crea una nueva instancia del evaluador
|
|
132
|
+
*/
|
|
133
|
+
constructor();
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Evalúa una lista de declaraciones
|
|
137
|
+
* @param statements - Lista de declaraciones AST
|
|
138
|
+
* @returns Resultado de la ejecución
|
|
139
|
+
*/
|
|
140
|
+
evaluate(statements: ASTNode[]): string[];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Función principal para interpretar código
|
|
145
|
+
* @param code - Código fuente en español
|
|
146
|
+
* @returns Resultado de la interpretación
|
|
147
|
+
*/
|
|
148
|
+
export function interpret(code: string): InterpretationResult;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Ejecuta código y retorna solo las salidas
|
|
152
|
+
* @param code - Código fuente en español
|
|
153
|
+
* @returns Lista de salidas
|
|
154
|
+
*/
|
|
155
|
+
export function run(code: string): string[];
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Obtiene las variables definidas
|
|
159
|
+
* @returns Variables en el entorno actual
|
|
160
|
+
*/
|
|
161
|
+
export function getVariables(): { [key: string]: any };
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Limpia el entorno de variables
|
|
165
|
+
*/
|
|
166
|
+
export function clearVariables(): void;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Clase del intérprete (para uso directo)
|
|
170
|
+
*/
|
|
171
|
+
export { Interpreter };
|
|
172
|
+
|
|
173
|
+
// Re-export everything as default for CommonJS compatibility
|
|
174
|
+
declare const _default: {
|
|
175
|
+
interpret: typeof interpret;
|
|
176
|
+
run: typeof run;
|
|
177
|
+
getVariables: typeof getVariables;
|
|
178
|
+
clearVariables: typeof clearVariables;
|
|
179
|
+
Interpreter: typeof Interpreter;
|
|
180
|
+
Tokenizer: typeof Tokenizer;
|
|
181
|
+
Parser: typeof Parser;
|
|
182
|
+
Evaluator: typeof Evaluator;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export = _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HispanoLang
|
|
3
|
+
* A simple interpreter to teach programming without language barriers
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Variables: variable nombre = valor
|
|
7
|
+
* - Show: mostrar "texto" o mostrar variable
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const Interpreter = require('./src/interpreter');
|
|
11
|
+
|
|
12
|
+
// Create an interpreter instance
|
|
13
|
+
const interpreter = new Interpreter();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Main function to interpret code
|
|
17
|
+
* @param {string} code - Source code in Spanish
|
|
18
|
+
* @returns {Object} Interpretation result
|
|
19
|
+
*/
|
|
20
|
+
function interpret(code) {
|
|
21
|
+
return interpreter.interpret(code);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Executes code and returns only the output
|
|
26
|
+
* @param {string} code - Source code in Spanish
|
|
27
|
+
* @returns {Array} List of outputs
|
|
28
|
+
*/
|
|
29
|
+
function run(code) {
|
|
30
|
+
return interpreter.run(code);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Gets the defined variables
|
|
35
|
+
* @returns {Object} Variables in the current environment
|
|
36
|
+
*/
|
|
37
|
+
function getVariables() {
|
|
38
|
+
return interpreter.getEnvironment();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Clears the variable environment
|
|
43
|
+
*/
|
|
44
|
+
function clearVariables() {
|
|
45
|
+
interpreter.clearEnvironment();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Example usage
|
|
49
|
+
|
|
50
|
+
const exampleCode = `
|
|
51
|
+
mostrar "Hola mundo"
|
|
52
|
+
`;
|
|
53
|
+
|
|
54
|
+
console.log('Code to execute:');
|
|
55
|
+
console.log(exampleCode);
|
|
56
|
+
console.log('Result:');
|
|
57
|
+
|
|
58
|
+
const result = interpret(exampleCode);
|
|
59
|
+
|
|
60
|
+
if (result.success) {
|
|
61
|
+
console.log('✓ Execution successful');
|
|
62
|
+
console.log('Defined variables:', getVariables());
|
|
63
|
+
} else {
|
|
64
|
+
console.log('✗ Error:', result.error);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Export functions for use in other modules
|
|
68
|
+
module.exports = {
|
|
69
|
+
interpret,
|
|
70
|
+
run,
|
|
71
|
+
getVariables,
|
|
72
|
+
clearVariables,
|
|
73
|
+
Interpreter,
|
|
74
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main interpreter for HispanoLang
|
|
3
|
+
* Coordinates tokenizer, parser and evaluator
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const Tokenizer = require("./tokenizer");
|
|
7
|
+
const Parser = require("./parser");
|
|
8
|
+
const Evaluator = require("./evaluator");
|
|
9
|
+
|
|
10
|
+
class Interpreter {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.tokenizer = new Tokenizer();
|
|
13
|
+
this.evaluator = new Evaluator();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Interprets source code
|
|
18
|
+
* @param {string} source - Source code to interpret
|
|
19
|
+
* @returns {Object} Interpretation result
|
|
20
|
+
*/
|
|
21
|
+
interpret(source) {
|
|
22
|
+
try {
|
|
23
|
+
// 1. Tokenize source code
|
|
24
|
+
const tokens = this.tokenizer.tokenize(source);
|
|
25
|
+
|
|
26
|
+
// 2. Parse tokens into AST
|
|
27
|
+
const parser = new Parser(tokens);
|
|
28
|
+
const statements = parser.parse();
|
|
29
|
+
|
|
30
|
+
// 3. Evaluate statements
|
|
31
|
+
const output = this.evaluator.evaluate(statements);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
success: true,
|
|
35
|
+
output: output,
|
|
36
|
+
error: null,
|
|
37
|
+
};
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return {
|
|
40
|
+
success: false,
|
|
41
|
+
output: [],
|
|
42
|
+
error: error.message,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Interprets source code and returns only the output
|
|
49
|
+
* @param {string} source - Source code to interpret
|
|
50
|
+
* @returns {Array} List of outputs
|
|
51
|
+
*/
|
|
52
|
+
run(source) {
|
|
53
|
+
const result = this.interpret(source);
|
|
54
|
+
return result.output;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Gets the current variable environment
|
|
59
|
+
* @returns {Object} Variable environment
|
|
60
|
+
*/
|
|
61
|
+
getEnvironment() {
|
|
62
|
+
return this.evaluator.environment.values;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Clears the variable environment
|
|
67
|
+
*/
|
|
68
|
+
clearEnvironment() {
|
|
69
|
+
this.evaluator = new Evaluator();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = Interpreter;
|