vladx 1.2.0 → 1.4.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/bin/vladx.js +6 -8
- package/package.json +1 -1
- package/src/engine/vladx-engine.js +12 -0
- package/src/interpreter/interpreter.js +13 -0
- package/src/lexer/lexer.js +1 -0
package/bin/vladx.js
CHANGED
|
@@ -242,7 +242,7 @@ export class CLI {
|
|
|
242
242
|
await this.engine.execute(source, { filename: filepath });
|
|
243
243
|
} else {
|
|
244
244
|
// Искать тесты
|
|
245
|
-
const files = this.findTestFiles();
|
|
245
|
+
const files = await this.findTestFiles();
|
|
246
246
|
|
|
247
247
|
for (const file of files) {
|
|
248
248
|
const source = readFileSync(file, 'utf-8');
|
|
@@ -448,9 +448,9 @@ VladX - Мощный интерпретируемый язык программ
|
|
|
448
448
|
/**
|
|
449
449
|
* Найти тестовые файлы
|
|
450
450
|
*/
|
|
451
|
-
findTestFiles() {
|
|
452
|
-
const fs =
|
|
453
|
-
const path =
|
|
451
|
+
async findTestFiles() {
|
|
452
|
+
const fs = await import('fs');
|
|
453
|
+
const path = await import('path');
|
|
454
454
|
|
|
455
455
|
const testFiles = [];
|
|
456
456
|
|
|
@@ -478,9 +478,7 @@ VladX - Мощный интерпретируемый язык программ
|
|
|
478
478
|
/**
|
|
479
479
|
* Запуск CLI
|
|
480
480
|
*/
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
cli.start(process.argv);
|
|
484
|
-
}
|
|
481
|
+
const cli = new CLI();
|
|
482
|
+
cli.start(process.argv);
|
|
485
483
|
|
|
486
484
|
export default CLI;
|
package/package.json
CHANGED
|
@@ -676,6 +676,18 @@ export class VladXEngine {
|
|
|
676
676
|
this.interpreter.builtins.set('можетБыть', Functional.Maybe);
|
|
677
677
|
this.interpreter.builtins.set('илиИначе', (maybe, defaultValue) => maybe.getOrElse(defaultValue));
|
|
678
678
|
|
|
679
|
+
// Класс Error
|
|
680
|
+
const errorMethods = new Map();
|
|
681
|
+
errorMethods.set('constructor', function(message) {
|
|
682
|
+
const msg = message && message.value !== undefined ? message.value : message;
|
|
683
|
+
this.value = msg || '';
|
|
684
|
+
});
|
|
685
|
+
errorMethods.set('toString', function() {
|
|
686
|
+
return VladXObject.string(this.value);
|
|
687
|
+
});
|
|
688
|
+
this.interpreter.globalEnv.define('Ошибка', VladXObject.class('Ошибка', errorMethods), true);
|
|
689
|
+
this.interpreter.globalEnv.define('Error', VladXObject.class('Error', errorMethods), true);
|
|
690
|
+
|
|
679
691
|
// Структуры данных
|
|
680
692
|
this.interpreter.builtins.set('Стек', () => new DataStructures.Stack());
|
|
681
693
|
this.interpreter.builtins.set('Очередь', () => new DataStructures.Queue());
|
|
@@ -36,6 +36,7 @@ export class Interpreter {
|
|
|
36
36
|
this.asyncManager = new AsyncManager(options.async);
|
|
37
37
|
this.currentFilename = '<unknown>';
|
|
38
38
|
this.currentLine = 0;
|
|
39
|
+
this.callStack = [];
|
|
39
40
|
|
|
40
41
|
this.registerBuiltins();
|
|
41
42
|
}
|
|
@@ -623,6 +624,8 @@ export class Interpreter {
|
|
|
623
624
|
}
|
|
624
625
|
}
|
|
625
626
|
|
|
627
|
+
// Специальная обработка для класса Error
|
|
628
|
+
const isErrorClass = callee.name === 'Ошибка' || callee.name === 'Error';
|
|
626
629
|
const instance = VladXObject.instance(callee);
|
|
627
630
|
|
|
628
631
|
const args = [];
|
|
@@ -637,6 +640,10 @@ export class Interpreter {
|
|
|
637
640
|
this.currentInstance = instance;
|
|
638
641
|
try {
|
|
639
642
|
await this.executeFunction(constructorMethod, args);
|
|
643
|
+
// Для класса Error, если это первый аргумент - строка, используем её как значение экземпляра
|
|
644
|
+
if (isErrorClass && args[0] && args[0].type === 'string') {
|
|
645
|
+
instance.value = args[0].value;
|
|
646
|
+
}
|
|
640
647
|
} finally {
|
|
641
648
|
this.currentInstance = previousInstance;
|
|
642
649
|
}
|
|
@@ -1738,6 +1745,12 @@ export class Interpreter {
|
|
|
1738
1745
|
async evaluateThrowStatement(stmt) {
|
|
1739
1746
|
const value = await this.evaluateExpression(stmt.argument);
|
|
1740
1747
|
const val = value && value.value !== undefined ? value.value : value;
|
|
1748
|
+
|
|
1749
|
+
// Если это экземпляр класса Error или Error, используем его сообщение
|
|
1750
|
+
if (value && (value.type === 'instance' || value.type === 'error') && value.value !== undefined) {
|
|
1751
|
+
throw new Error(value.value);
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1741
1754
|
throw new Error(String(val));
|
|
1742
1755
|
}
|
|
1743
1756
|
|