starlight-cli 1.0.23 → 1.0.24
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 +80 -12
- package/package.json +1 -1
- package/src/evaluator.js +19 -2
- package/src/lexer.js +13 -1
- package/src/parser.js +47 -8
- package/src/starlight.js +1 -1
package/dist/index.js
CHANGED
|
@@ -1448,6 +1448,9 @@ this.global.define('str', arg => {
|
|
|
1448
1448
|
case 'ImportStatement': return this.evalImport(node, env);
|
|
1449
1449
|
case 'FunctionDeclaration': return this.evalFunctionDeclaration(node, env);
|
|
1450
1450
|
case 'CallExpression': return this.evalCall(node, env);
|
|
1451
|
+
case 'ArrowFunctionExpression':
|
|
1452
|
+
return this.evalArrowFunction(node, env);
|
|
1453
|
+
|
|
1451
1454
|
case 'ReturnStatement': {
|
|
1452
1455
|
const val = node.argument ? this.evaluate(node.argument, env) : null;
|
|
1453
1456
|
throw new ReturnValue(val);
|
|
@@ -1538,6 +1541,14 @@ evalImport(node, env) {
|
|
|
1538
1541
|
const val = this.evaluate(node.expr, env);
|
|
1539
1542
|
return env.define(node.id, val);
|
|
1540
1543
|
}
|
|
1544
|
+
evalArrowFunction(node, env) {
|
|
1545
|
+
return {
|
|
1546
|
+
params: node.params,
|
|
1547
|
+
body: node.body,
|
|
1548
|
+
env: env,
|
|
1549
|
+
arrow: true
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1541
1552
|
|
|
1542
1553
|
evalAssignment(node, env) {
|
|
1543
1554
|
const rightVal = this.evaluate(node.right, env);
|
|
@@ -1700,8 +1711,14 @@ evalImport(node, env) {
|
|
|
1700
1711
|
const argVal = node.arguments[i] ? this.evaluate(node.arguments[i], env) : null;
|
|
1701
1712
|
callEnv.define(p, argVal);
|
|
1702
1713
|
});
|
|
1703
|
-
try {
|
|
1704
|
-
|
|
1714
|
+
try {
|
|
1715
|
+
const result = this.evaluate(fn.body, callEnv);
|
|
1716
|
+
return fn.arrow ? result : result;
|
|
1717
|
+
} catch (e) {
|
|
1718
|
+
if (e instanceof ReturnValue) return e.value;
|
|
1719
|
+
throw e;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1705
1722
|
}
|
|
1706
1723
|
|
|
1707
1724
|
evalIndex(node, env) {
|
|
@@ -1909,7 +1926,19 @@ class Lexer {
|
|
|
1909
1926
|
const startCol = this.column;
|
|
1910
1927
|
|
|
1911
1928
|
if (char === '=' && next === '=') { tokens.push({ type: 'EQEQ', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
1912
|
-
|
|
1929
|
+
|
|
1930
|
+
if (char === '=' && next === '>') {
|
|
1931
|
+
tokens.push({ type: 'ARROW', line: startLine, column: startCol });
|
|
1932
|
+
this.advance(); this.advance();
|
|
1933
|
+
continue;
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
if (char === '-' && next === '>') {
|
|
1937
|
+
tokens.push({ type: 'ARROW', line: startLine, column: startCol });
|
|
1938
|
+
this.advance(); this.advance();
|
|
1939
|
+
continue;
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1913
1942
|
if (char === '!' && next === '=') { tokens.push({ type: 'NOTEQ', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
1914
1943
|
if (char === '<' && next === '=') { tokens.push({ type: 'LTE', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
1915
1944
|
if (char === '>' && next === '=') { tokens.push({ type: 'GTE', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
@@ -2317,6 +2346,15 @@ class Parser {
|
|
|
2317
2346
|
}
|
|
2318
2347
|
return node;
|
|
2319
2348
|
}
|
|
2349
|
+
arrowFunction(params) {
|
|
2350
|
+
this.eat('ARROW');
|
|
2351
|
+
const body = this.expression();
|
|
2352
|
+
return {
|
|
2353
|
+
type: 'ArrowFunctionExpression',
|
|
2354
|
+
params,
|
|
2355
|
+
body
|
|
2356
|
+
};
|
|
2357
|
+
}
|
|
2320
2358
|
|
|
2321
2359
|
primary() {
|
|
2322
2360
|
const t = this.current;
|
|
@@ -2343,17 +2381,47 @@ class Parser {
|
|
|
2343
2381
|
}
|
|
2344
2382
|
|
|
2345
2383
|
if (t.type === 'IDENTIFIER') {
|
|
2346
|
-
|
|
2384
|
+
const name = t.value;
|
|
2385
|
+
this.eat('IDENTIFIER');
|
|
2386
|
+
|
|
2387
|
+
if (this.current.type === 'ARROW') {
|
|
2388
|
+
return this.arrowFunction([name]);
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
return { type: 'Identifier', name };
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
|
|
2395
|
+
if (t.type === 'LPAREN') {
|
|
2396
|
+
this.eat('LPAREN');
|
|
2397
|
+
|
|
2398
|
+
const params = [];
|
|
2399
|
+
|
|
2400
|
+
if (this.current.type !== 'RPAREN') {
|
|
2401
|
+
params.push(this.current.value);
|
|
2402
|
+
this.eat('IDENTIFIER');
|
|
2403
|
+
while (this.current.type === 'COMMA') {
|
|
2404
|
+
this.eat('COMMA');
|
|
2405
|
+
params.push(this.current.value);
|
|
2347
2406
|
this.eat('IDENTIFIER');
|
|
2348
|
-
return { type: 'Identifier', name };
|
|
2349
2407
|
}
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
this.eat('RPAREN');
|
|
2411
|
+
|
|
2412
|
+
if (this.current.type === 'ARROW') {
|
|
2413
|
+
return this.arrowFunction(params);
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
if (params.length === 1) {
|
|
2417
|
+
return { type: 'Identifier', name: params[0] };
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
throw new Error(
|
|
2421
|
+
`Invalid grouped expression at line ${this.current.line}, column ${this.current.column}`
|
|
2422
|
+
);
|
|
2423
|
+
}
|
|
2350
2424
|
|
|
2351
|
-
if (t.type === 'LPAREN') {
|
|
2352
|
-
this.eat('LPAREN');
|
|
2353
|
-
const expr = this.expression();
|
|
2354
|
-
this.eat('RPAREN');
|
|
2355
|
-
return expr;
|
|
2356
|
-
}
|
|
2357
2425
|
|
|
2358
2426
|
if (t.type === 'LBRACKET') {
|
|
2359
2427
|
this.eat('LBRACKET');
|
|
@@ -2492,7 +2560,7 @@ const Lexer = __nccwpck_require__(211);
|
|
|
2492
2560
|
const Parser = __nccwpck_require__(222);
|
|
2493
2561
|
const Evaluator = __nccwpck_require__(112);
|
|
2494
2562
|
|
|
2495
|
-
const VERSION = '1.0.
|
|
2563
|
+
const VERSION = '1.0.24';
|
|
2496
2564
|
|
|
2497
2565
|
const COLOR = {
|
|
2498
2566
|
reset: '\x1b[0m',
|
package/package.json
CHANGED
package/src/evaluator.js
CHANGED
|
@@ -105,6 +105,9 @@ this.global.define('str', arg => {
|
|
|
105
105
|
case 'ImportStatement': return this.evalImport(node, env);
|
|
106
106
|
case 'FunctionDeclaration': return this.evalFunctionDeclaration(node, env);
|
|
107
107
|
case 'CallExpression': return this.evalCall(node, env);
|
|
108
|
+
case 'ArrowFunctionExpression':
|
|
109
|
+
return this.evalArrowFunction(node, env);
|
|
110
|
+
|
|
108
111
|
case 'ReturnStatement': {
|
|
109
112
|
const val = node.argument ? this.evaluate(node.argument, env) : null;
|
|
110
113
|
throw new ReturnValue(val);
|
|
@@ -195,6 +198,14 @@ evalImport(node, env) {
|
|
|
195
198
|
const val = this.evaluate(node.expr, env);
|
|
196
199
|
return env.define(node.id, val);
|
|
197
200
|
}
|
|
201
|
+
evalArrowFunction(node, env) {
|
|
202
|
+
return {
|
|
203
|
+
params: node.params,
|
|
204
|
+
body: node.body,
|
|
205
|
+
env: env,
|
|
206
|
+
arrow: true
|
|
207
|
+
};
|
|
208
|
+
}
|
|
198
209
|
|
|
199
210
|
evalAssignment(node, env) {
|
|
200
211
|
const rightVal = this.evaluate(node.right, env);
|
|
@@ -357,8 +368,14 @@ evalImport(node, env) {
|
|
|
357
368
|
const argVal = node.arguments[i] ? this.evaluate(node.arguments[i], env) : null;
|
|
358
369
|
callEnv.define(p, argVal);
|
|
359
370
|
});
|
|
360
|
-
try {
|
|
361
|
-
|
|
371
|
+
try {
|
|
372
|
+
const result = this.evaluate(fn.body, callEnv);
|
|
373
|
+
return fn.arrow ? result : result;
|
|
374
|
+
} catch (e) {
|
|
375
|
+
if (e instanceof ReturnValue) return e.value;
|
|
376
|
+
throw e;
|
|
377
|
+
}
|
|
378
|
+
|
|
362
379
|
}
|
|
363
380
|
|
|
364
381
|
evalIndex(node, env) {
|
package/src/lexer.js
CHANGED
|
@@ -139,7 +139,19 @@ class Lexer {
|
|
|
139
139
|
const startCol = this.column;
|
|
140
140
|
|
|
141
141
|
if (char === '=' && next === '=') { tokens.push({ type: 'EQEQ', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
142
|
-
|
|
142
|
+
|
|
143
|
+
if (char === '=' && next === '>') {
|
|
144
|
+
tokens.push({ type: 'ARROW', line: startLine, column: startCol });
|
|
145
|
+
this.advance(); this.advance();
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (char === '-' && next === '>') {
|
|
150
|
+
tokens.push({ type: 'ARROW', line: startLine, column: startCol });
|
|
151
|
+
this.advance(); this.advance();
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
143
155
|
if (char === '!' && next === '=') { tokens.push({ type: 'NOTEQ', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
144
156
|
if (char === '<' && next === '=') { tokens.push({ type: 'LTE', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
|
145
157
|
if (char === '>' && next === '=') { tokens.push({ type: 'GTE', line: startLine, column: startCol }); this.advance(); this.advance(); continue; }
|
package/src/parser.js
CHANGED
|
@@ -368,6 +368,15 @@ class Parser {
|
|
|
368
368
|
}
|
|
369
369
|
return node;
|
|
370
370
|
}
|
|
371
|
+
arrowFunction(params) {
|
|
372
|
+
this.eat('ARROW');
|
|
373
|
+
const body = this.expression();
|
|
374
|
+
return {
|
|
375
|
+
type: 'ArrowFunctionExpression',
|
|
376
|
+
params,
|
|
377
|
+
body
|
|
378
|
+
};
|
|
379
|
+
}
|
|
371
380
|
|
|
372
381
|
primary() {
|
|
373
382
|
const t = this.current;
|
|
@@ -394,17 +403,47 @@ class Parser {
|
|
|
394
403
|
}
|
|
395
404
|
|
|
396
405
|
if (t.type === 'IDENTIFIER') {
|
|
397
|
-
|
|
406
|
+
const name = t.value;
|
|
407
|
+
this.eat('IDENTIFIER');
|
|
408
|
+
|
|
409
|
+
if (this.current.type === 'ARROW') {
|
|
410
|
+
return this.arrowFunction([name]);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return { type: 'Identifier', name };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
if (t.type === 'LPAREN') {
|
|
418
|
+
this.eat('LPAREN');
|
|
419
|
+
|
|
420
|
+
const params = [];
|
|
421
|
+
|
|
422
|
+
if (this.current.type !== 'RPAREN') {
|
|
423
|
+
params.push(this.current.value);
|
|
424
|
+
this.eat('IDENTIFIER');
|
|
425
|
+
while (this.current.type === 'COMMA') {
|
|
426
|
+
this.eat('COMMA');
|
|
427
|
+
params.push(this.current.value);
|
|
398
428
|
this.eat('IDENTIFIER');
|
|
399
|
-
return { type: 'Identifier', name };
|
|
400
429
|
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.eat('RPAREN');
|
|
433
|
+
|
|
434
|
+
if (this.current.type === 'ARROW') {
|
|
435
|
+
return this.arrowFunction(params);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (params.length === 1) {
|
|
439
|
+
return { type: 'Identifier', name: params[0] };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
throw new Error(
|
|
443
|
+
`Invalid grouped expression at line ${this.current.line}, column ${this.current.column}`
|
|
444
|
+
);
|
|
445
|
+
}
|
|
401
446
|
|
|
402
|
-
if (t.type === 'LPAREN') {
|
|
403
|
-
this.eat('LPAREN');
|
|
404
|
-
const expr = this.expression();
|
|
405
|
-
this.eat('RPAREN');
|
|
406
|
-
return expr;
|
|
407
|
-
}
|
|
408
447
|
|
|
409
448
|
if (t.type === 'LBRACKET') {
|
|
410
449
|
this.eat('LBRACKET');
|