vladx 1.3.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 CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vladx",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Мощный интерпретируемый язык программирования с русским синтаксисом",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -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
 
@@ -355,6 +355,7 @@ export class Lexer {
355
355
  'наконец': 'FINALLY',
356
356
  'throw': 'THROW',
357
357
  'бросить': 'THROW',
358
+ 'выбросить': 'THROW',
358
359
  'break': 'BREAK',
359
360
  'прервать': 'BREAK',
360
361
  'continue': 'CONTINUE',