starlight-cli 1.0.31 → 1.0.33

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/dist/index.js CHANGED
@@ -1497,14 +1497,33 @@ async evaluate(node, env = this.global) {
1497
1497
  return val;
1498
1498
  }
1499
1499
  case 'NewExpression': {
1500
- const fn = await this.evaluate(node.callee, env); // the class or constructor
1501
- if (typeof fn !== 'function') {
1502
- throw new Error('NewExpression callee is not a function');
1500
+ const callee = await this.evaluate(node.callee, env);
1501
+
1502
+ if (typeof callee === 'object' && callee.body) {
1503
+ const evaluator = this; // <- capture the current evaluator
1504
+ const Constructor = function(...args) {
1505
+ const newEnv = new Environment(callee.env);
1506
+ newEnv.define('this', this);
1507
+ for (let i = 0; i < callee.params.length; i++) {
1508
+ newEnv.define(callee.params[i], args[i]);
1503
1509
  }
1504
- const args = [];
1505
- for (const a of node.arguments) args.push(await this.evaluate(a, env));
1506
- return new fn(...args);
1507
- }
1510
+ return evaluator.evaluate(callee.body, newEnv); // use captured evaluator
1511
+ };
1512
+
1513
+ const args = [];
1514
+ for (const a of node.arguments) args.push(await this.evaluate(a, env));
1515
+ return new Constructor(...args);
1516
+ }
1517
+
1518
+ if (typeof callee !== 'function') {
1519
+ throw new Error('NewExpression callee is not a function');
1520
+ }
1521
+
1522
+ const args = [];
1523
+ for (const a of node.arguments) args.push(await this.evaluate(a, env));
1524
+ return new callee(...args);
1525
+ }
1526
+
1508
1527
  default:
1509
1528
  throw new Error(`Unknown node type in evaluator: ${node.type}`);
1510
1529
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "Starlight Programming Language CLI",
5
5
  "bin": {
6
6
  "starlight": "index.js"
package/src/evaluator.js CHANGED
@@ -154,14 +154,33 @@ async evaluate(node, env = this.global) {
154
154
  return val;
155
155
  }
156
156
  case 'NewExpression': {
157
- const fn = await this.evaluate(node.callee, env); // the class or constructor
158
- if (typeof fn !== 'function') {
159
- throw new Error('NewExpression callee is not a function');
157
+ const callee = await this.evaluate(node.callee, env);
158
+
159
+ if (typeof callee === 'object' && callee.body) {
160
+ const evaluator = this; // <- capture the current evaluator
161
+ const Constructor = function(...args) {
162
+ const newEnv = new Environment(callee.env);
163
+ newEnv.define('this', this);
164
+ for (let i = 0; i < callee.params.length; i++) {
165
+ newEnv.define(callee.params[i], args[i]);
160
166
  }
161
- const args = [];
162
- for (const a of node.arguments) args.push(await this.evaluate(a, env));
163
- return new fn(...args);
164
- }
167
+ return evaluator.evaluate(callee.body, newEnv); // use captured evaluator
168
+ };
169
+
170
+ const args = [];
171
+ for (const a of node.arguments) args.push(await this.evaluate(a, env));
172
+ return new Constructor(...args);
173
+ }
174
+
175
+ if (typeof callee !== 'function') {
176
+ throw new Error('NewExpression callee is not a function');
177
+ }
178
+
179
+ const args = [];
180
+ for (const a of node.arguments) args.push(await this.evaluate(a, env));
181
+ return new callee(...args);
182
+ }
183
+
165
184
  default:
166
185
  throw new Error(`Unknown node type in evaluator: ${node.type}`);
167
186
  }