starlight-cli 1.0.11 → 1.0.13

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
@@ -2449,12 +2449,14 @@ const fs = __nccwpck_require__(896);
2449
2449
  const path = __nccwpck_require__(928);
2450
2450
  const os = __nccwpck_require__(857);
2451
2451
  const { exec } = __nccwpck_require__(317);
2452
+ const readline = __nccwpck_require__(785);
2453
+ const readlineSync = __nccwpck_require__(552);
2452
2454
 
2453
2455
  const Lexer = __nccwpck_require__(211);
2454
2456
  const Parser = __nccwpck_require__(222);
2455
2457
  const Evaluator = __nccwpck_require__(112);
2456
2458
 
2457
- const VERSION = '1.0.11';
2459
+ const VERSION = '1.0.13';
2458
2460
 
2459
2461
  const COLOR = {
2460
2462
  reset: '\x1b[0m',
@@ -2464,36 +2466,38 @@ const COLOR = {
2464
2466
  blue: '\x1b[34m',
2465
2467
  cyan: '\x1b[36m',
2466
2468
  gray: '\x1b[90m',
2467
- green: '\x1b[32m'
2469
+ green: '\x1b[32m',
2470
+ magenta: '\x1b[35m'
2468
2471
  };
2469
2472
 
2470
- function waitAndExit(code = 1) {
2473
+
2474
+ function waitAndExit(code = 0) {
2475
+ try { process.stdin.setRawMode(true); } catch {}
2471
2476
  console.error(COLOR.gray + '\nPress any key to exit...' + COLOR.reset);
2472
- process.stdin.setRawMode(true);
2473
2477
  process.stdin.resume();
2474
2478
  process.stdin.once('data', () => process.exit(code));
2475
2479
  }
2476
2480
 
2477
2481
  function fatal(msg) {
2478
2482
  console.error(COLOR.red + msg + COLOR.reset);
2479
- return waitAndExit(1);
2483
+ waitAndExit(1);
2480
2484
  }
2481
2485
 
2482
- function printSourceContext(code, line, column) {
2483
- const lines = code.split('\n');
2484
- const srcLine = lines[line - 1];
2485
- if (!srcLine) return;
2486
-
2487
- console.error(
2488
- COLOR.gray + `\n ${line} | ` + COLOR.reset + srcLine
2489
- );
2490
- console.error(
2491
- COLOR.gray + ' | ' +
2492
- COLOR.red + ' '.repeat(Math.max(0, column - 1)) + '^' +
2493
- COLOR.reset
2494
- );
2486
+
2487
+ function highlightCode(line) {
2488
+ return line
2489
+ // strings
2490
+ .replace(/"(?:\\.|[^"])*"/g, m => COLOR.yellow + m + COLOR.reset)
2491
+ // numbers
2492
+ .replace(/\b\d+(\.\d+)?\b/g, m => COLOR.magenta + m + COLOR.reset)
2493
+ // keywords
2494
+ .replace(
2495
+ /\b(sldeploy|import|from|const|let|var|if|else|for|while|func|return|break|continue|define|ask)\b/g,
2496
+ m => COLOR.blue + m + COLOR.reset
2497
+ );
2495
2498
  }
2496
2499
 
2500
+
2497
2501
  const args = process.argv.slice(2);
2498
2502
 
2499
2503
  if (args.length === 0) {
@@ -2501,12 +2505,11 @@ if (args.length === 0) {
2501
2505
  Starlight Programming Language
2502
2506
 
2503
2507
  Usage:
2504
- starlight <file.sl> Run a Starlight program
2505
- starlight -v Show version
2506
- starlight --help Show help
2507
- starlight --learn Open learning guide
2508
- starlight --writedirectly Write and run temporary code
2509
- starlight --viewfilelist View SL files in a folder
2508
+ starlight <file.sl>
2509
+ starlight -v
2510
+ starlight --help
2511
+ starlight --learn
2512
+ starlight --writedirectly
2510
2513
  `);
2511
2514
  process.exit(0);
2512
2515
  }
@@ -2518,160 +2521,104 @@ if (args[0] === '-v' || args[0] === '--version') {
2518
2521
 
2519
2522
  if (args[0] === '--help') {
2520
2523
  console.log(`
2521
- Starlight CLI Help
2522
-
2523
- Commands:
2524
- starlight file.sl Run a Starlight program
2525
- starlight -v Show version
2526
- starlight --learn Open learning guide
2527
- starlight --writedirectly Write & run temporary code
2528
- starlight --viewfilelist View SL files in a folder
2524
+ starlight <file.sl> Run file
2525
+ starlight --writedirectly Interactive editor
2526
+ starlight --learn Learning guide
2529
2527
  `);
2530
2528
  process.exit(0);
2531
2529
  }
2532
2530
 
2533
2531
  if (args[0] === '--learn') {
2534
- console.log(COLOR.cyan + 'Opening Starlight Learning Guide...' + COLOR.reset);
2535
2532
  exec('start https://programming-lang.pages.dev/learning-guide');
2536
2533
  process.exit(0);
2537
2534
  }
2538
2535
 
2539
- if (args[0] === '--viewfilelist') {
2540
- const readlineSync = __nccwpck_require__(552);
2541
- let folderPath = readlineSync.question(COLOR.cyan + 'Paste folder path to search .sl files: ' + COLOR.reset).trim();
2542
- if (!folderPath) return waitAndExit(0);
2543
-
2544
- if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
2545
- return fatal('Invalid folder path.');
2546
- }
2547
-
2548
- const files = fs.readdirSync(folderPath).filter(f => f.endsWith('.sl'));
2549
- if (files.length === 0) console.log(COLOR.yellow + 'No .sl files found in folder.' + COLOR.reset);
2550
- else {
2551
- console.log(COLOR.green + `Found ${files.length} .sl files:` + COLOR.reset);
2552
- files.forEach(f => console.log(' ' + f));
2553
- }
2554
- return waitAndExit(0);
2555
- }
2556
2536
 
2557
2537
  if (args[0] === '--writedirectly') {
2558
- const readline = __nccwpck_require__(785);
2559
- const tempFile = path.join(os.tmpdir(), `starlight-temp-${Date.now()}.sl`);
2538
+ const tempFile = path.join(os.tmpdir(), `starlight-${Date.now()}.sl`);
2539
+ const lines = [];
2560
2540
 
2561
2541
  console.log(COLOR.green + 'Interactive Starlight Editor' + COLOR.reset);
2562
- console.log(COLOR.gray + 'Type your code. Enter ":run" on a new line to execute.\n' + COLOR.reset);
2542
+ console.log(COLOR.gray + 'Type code. Use :run to execute.\n' + COLOR.reset);
2563
2543
 
2564
2544
  const rl = readline.createInterface({
2565
2545
  input: process.stdin,
2566
2546
  output: process.stdout,
2567
- prompt: COLOR.cyan + '> ' + COLOR.reset
2547
+ terminal: false
2568
2548
  });
2569
2549
 
2570
- let lines = [];
2571
- rl.prompt();
2550
+ process.stdout.write(COLOR.cyan + '> ' + COLOR.reset);
2572
2551
 
2573
2552
  rl.on('line', (line) => {
2574
2553
  if (line.trim() === ':run') {
2575
2554
  rl.close();
2576
2555
  fs.writeFileSync(tempFile, lines.join('\n'), 'utf8');
2577
- runFile(tempFile, true, () => saveTempFilePrompt(lines));
2556
+ runFile(tempFile, true, () => savePrompt(lines));
2578
2557
  return;
2579
2558
  }
2580
2559
 
2581
- let coloredLine = line
2582
- .replace(/"(.*?)"/g, COLOR.yellow + '"$1"' + COLOR.reset)
2583
- .replace(/\b(sldeploy|import|from|const|let|var|if|else|for|while|func|return|break|continue|define|ask)\b/g, COLOR.blue + '$1' + COLOR.reset);
2584
-
2585
- console.log(coloredLine);
2586
-
2587
2560
  lines.push(line);
2588
- rl.prompt();
2561
+ process.stdout.write(
2562
+ COLOR.cyan + '> ' + COLOR.reset + highlightCode(line) + '\n'
2563
+ );
2589
2564
  });
2590
2565
 
2591
2566
  return;
2592
2567
  }
2593
2568
 
2594
- function saveTempFilePrompt(lines) {
2595
- const readlineSync = __nccwpck_require__(552);
2596
- let saveChoice = readlineSync.question(COLOR.cyan + 'Do you want to save this code? (y/n): ' + COLOR.reset).trim().toLowerCase();
2597
- if (saveChoice !== 'y') return;
2598
2569
 
2599
- let folderPath = readlineSync.question(COLOR.cyan + 'Paste folder path to save (C:/ or D:/...): ' + COLOR.reset).trim();
2600
- if (!folderPath || !fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) return console.log(COLOR.yellow + 'Skipping save.' + COLOR.reset);
2570
+ function savePrompt(lines) {
2571
+ try { process.stdin.setRawMode(false); } catch {}
2601
2572
 
2602
- let fileName = readlineSync.question(COLOR.cyan + 'Enter file name (without extension): ' + COLOR.reset).trim();
2603
- if (!fileName) return console.log(COLOR.yellow + 'Skipping save.' + COLOR.reset);
2573
+ const save = readlineSync
2574
+ .question(COLOR.cyan + 'Do you want to save this code? (y/n): ' + COLOR.reset)
2575
+ .trim()
2576
+ .toLowerCase();
2604
2577
 
2605
- let fullPath = path.join(folderPath, fileName.endsWith('.sl') ? fileName : fileName + '.sl');
2606
- while (fs.existsSync(fullPath)) {
2607
- fileName = readlineSync.question(COLOR.red + 'File exists! Enter a new name (or leave blank to cancel): ' + COLOR.reset).trim();
2608
- if (!fileName) return console.log(COLOR.yellow + 'Skipping save.' + COLOR.reset);
2609
- fullPath = path.join(folderPath, fileName.endsWith('.sl') ? fileName : fileName + '.sl');
2610
- }
2578
+ if (save !== 'y') return waitAndExit(0);
2611
2579
 
2612
- fs.writeFileSync(fullPath, lines.join('\n'), 'utf8');
2613
- console.log(COLOR.green + `Saved to ${fullPath}` + COLOR.reset);
2614
- }
2580
+ const dir = readlineSync.question(
2581
+ COLOR.cyan + 'Folder path: ' + COLOR.reset
2582
+ ).trim();
2615
2583
 
2616
- function runFile(filePath, isTemp = false, callback = null) {
2617
- if (!fs.existsSync(filePath)) {
2618
- return fatal(`File not found: ${filePath}`);
2584
+ if (!fs.existsSync(dir)) {
2585
+ console.log(COLOR.yellow + 'Invalid folder.' + COLOR.reset);
2586
+ return waitAndExit(0);
2619
2587
  }
2620
2588
 
2621
- let code;
2622
- try {
2623
- code = fs.readFileSync(filePath, 'utf-8');
2624
- } catch (err) {
2625
- return fatal(`Failed to read file: ${err.message}`);
2626
- }
2589
+ const name = readlineSync.question(
2590
+ COLOR.cyan + 'File name: ' + COLOR.reset
2591
+ ).trim();
2627
2592
 
2628
- let tokens;
2629
- try {
2630
- const lexer = new Lexer(code);
2631
- tokens = lexer.getTokens();
2632
- } catch (err) {
2633
- console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
2634
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2635
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2636
- if (err.line && err.column) printSourceContext(code, err.line, err.column);
2637
- return waitAndExit(1);
2638
- }
2593
+ if (!name) return waitAndExit(0);
2639
2594
 
2640
- let ast;
2641
- try {
2642
- const parser = new Parser(tokens);
2643
- ast = parser.parse();
2644
- } catch (err) {
2645
- console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
2646
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2647
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2648
- if (err.token && err.token.line && err.token.column) printSourceContext(code, err.token.line, err.token.column);
2649
- return waitAndExit(1);
2650
- }
2595
+ const file = path.join(dir, name.endsWith('.sl') ? name : name + '.sl');
2596
+ fs.writeFileSync(file, lines.join('\n'), 'utf8');
2597
+
2598
+ console.log(COLOR.green + `Saved to ${file}` + COLOR.reset);
2599
+ waitAndExit(0);
2600
+ }
2601
+
2602
+ function runFile(filePath, isTemp = false, callback) {
2603
+ const code = fs.readFileSync(filePath, 'utf8');
2651
2604
 
2652
2605
  try {
2653
- const evaluator = new Evaluator();
2654
- evaluator.evaluate(ast);
2655
- } catch (err) {
2656
- console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
2657
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2658
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2659
- if (err.line && err.column) printSourceContext(code, err.line, err.column);
2606
+ const lexer = new Lexer(code);
2607
+ const parser = new Parser(lexer.getTokens());
2608
+ const ast = parser.parse();
2609
+ new Evaluator().evaluate(ast);
2610
+ } catch (e) {
2611
+ console.error(COLOR.red + e.message + COLOR.reset);
2660
2612
  return waitAndExit(1);
2661
2613
  }
2662
2614
 
2663
2615
  console.log(COLOR.green + '\nProgram finished successfully.' + COLOR.reset);
2664
2616
 
2665
2617
  if (callback) callback();
2666
-
2667
- if (isTemp) {
2668
- try { fs.unlinkSync(filePath); } catch {}
2669
- }
2670
-
2671
- waitAndExit(0);
2618
+ if (isTemp) try { fs.unlinkSync(filePath); } catch {}
2672
2619
  }
2673
2620
 
2674
- if (!['--writedirectly', '--viewfilelist', '--learn', '--help', '-v', '--version'].includes(args[0])) {
2621
+ if (!args[0].startsWith('--')) {
2675
2622
  runFile(path.resolve(args[0]));
2676
2623
  }
2677
2624
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "Starlight Programming Language CLI",
5
5
  "bin": {
6
6
  "starlight": "index.js"
package/src/starlight.js CHANGED
@@ -2,12 +2,14 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
  const os = require('os');
4
4
  const { exec } = require('child_process');
5
+ const readline = require('readline');
6
+ const readlineSync = require('readline-sync');
5
7
 
6
8
  const Lexer = require('./lexer');
7
9
  const Parser = require('./parser');
8
10
  const Evaluator = require('./evaluator');
9
11
 
10
- const VERSION = '1.0.11';
12
+ const VERSION = '1.0.13';
11
13
 
12
14
  const COLOR = {
13
15
  reset: '\x1b[0m',
@@ -17,36 +19,38 @@ const COLOR = {
17
19
  blue: '\x1b[34m',
18
20
  cyan: '\x1b[36m',
19
21
  gray: '\x1b[90m',
20
- green: '\x1b[32m'
22
+ green: '\x1b[32m',
23
+ magenta: '\x1b[35m'
21
24
  };
22
25
 
23
- function waitAndExit(code = 1) {
26
+
27
+ function waitAndExit(code = 0) {
28
+ try { process.stdin.setRawMode(true); } catch {}
24
29
  console.error(COLOR.gray + '\nPress any key to exit...' + COLOR.reset);
25
- process.stdin.setRawMode(true);
26
30
  process.stdin.resume();
27
31
  process.stdin.once('data', () => process.exit(code));
28
32
  }
29
33
 
30
34
  function fatal(msg) {
31
35
  console.error(COLOR.red + msg + COLOR.reset);
32
- return waitAndExit(1);
36
+ waitAndExit(1);
33
37
  }
34
38
 
35
- function printSourceContext(code, line, column) {
36
- const lines = code.split('\n');
37
- const srcLine = lines[line - 1];
38
- if (!srcLine) return;
39
-
40
- console.error(
41
- COLOR.gray + `\n ${line} | ` + COLOR.reset + srcLine
42
- );
43
- console.error(
44
- COLOR.gray + ' | ' +
45
- COLOR.red + ' '.repeat(Math.max(0, column - 1)) + '^' +
46
- COLOR.reset
47
- );
39
+
40
+ function highlightCode(line) {
41
+ return line
42
+ // strings
43
+ .replace(/"(?:\\.|[^"])*"/g, m => COLOR.yellow + m + COLOR.reset)
44
+ // numbers
45
+ .replace(/\b\d+(\.\d+)?\b/g, m => COLOR.magenta + m + COLOR.reset)
46
+ // keywords
47
+ .replace(
48
+ /\b(sldeploy|import|from|const|let|var|if|else|for|while|func|return|break|continue|define|ask)\b/g,
49
+ m => COLOR.blue + m + COLOR.reset
50
+ );
48
51
  }
49
52
 
53
+
50
54
  const args = process.argv.slice(2);
51
55
 
52
56
  if (args.length === 0) {
@@ -54,12 +58,11 @@ if (args.length === 0) {
54
58
  Starlight Programming Language
55
59
 
56
60
  Usage:
57
- starlight <file.sl> Run a Starlight program
58
- starlight -v Show version
59
- starlight --help Show help
60
- starlight --learn Open learning guide
61
- starlight --writedirectly Write and run temporary code
62
- starlight --viewfilelist View SL files in a folder
61
+ starlight <file.sl>
62
+ starlight -v
63
+ starlight --help
64
+ starlight --learn
65
+ starlight --writedirectly
63
66
  `);
64
67
  process.exit(0);
65
68
  }
@@ -71,159 +74,103 @@ if (args[0] === '-v' || args[0] === '--version') {
71
74
 
72
75
  if (args[0] === '--help') {
73
76
  console.log(`
74
- Starlight CLI Help
75
-
76
- Commands:
77
- starlight file.sl Run a Starlight program
78
- starlight -v Show version
79
- starlight --learn Open learning guide
80
- starlight --writedirectly Write & run temporary code
81
- starlight --viewfilelist View SL files in a folder
77
+ starlight <file.sl> Run file
78
+ starlight --writedirectly Interactive editor
79
+ starlight --learn Learning guide
82
80
  `);
83
81
  process.exit(0);
84
82
  }
85
83
 
86
84
  if (args[0] === '--learn') {
87
- console.log(COLOR.cyan + 'Opening Starlight Learning Guide...' + COLOR.reset);
88
85
  exec('start https://programming-lang.pages.dev/learning-guide');
89
86
  process.exit(0);
90
87
  }
91
88
 
92
- if (args[0] === '--viewfilelist') {
93
- const readlineSync = require('readline-sync');
94
- let folderPath = readlineSync.question(COLOR.cyan + 'Paste folder path to search .sl files: ' + COLOR.reset).trim();
95
- if (!folderPath) return waitAndExit(0);
96
-
97
- if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
98
- return fatal('Invalid folder path.');
99
- }
100
-
101
- const files = fs.readdirSync(folderPath).filter(f => f.endsWith('.sl'));
102
- if (files.length === 0) console.log(COLOR.yellow + 'No .sl files found in folder.' + COLOR.reset);
103
- else {
104
- console.log(COLOR.green + `Found ${files.length} .sl files:` + COLOR.reset);
105
- files.forEach(f => console.log(' ' + f));
106
- }
107
- return waitAndExit(0);
108
- }
109
89
 
110
90
  if (args[0] === '--writedirectly') {
111
- const readline = require('readline');
112
- const tempFile = path.join(os.tmpdir(), `starlight-temp-${Date.now()}.sl`);
91
+ const tempFile = path.join(os.tmpdir(), `starlight-${Date.now()}.sl`);
92
+ const lines = [];
113
93
 
114
94
  console.log(COLOR.green + 'Interactive Starlight Editor' + COLOR.reset);
115
- console.log(COLOR.gray + 'Type your code. Enter ":run" on a new line to execute.\n' + COLOR.reset);
95
+ console.log(COLOR.gray + 'Type code. Use :run to execute.\n' + COLOR.reset);
116
96
 
117
97
  const rl = readline.createInterface({
118
98
  input: process.stdin,
119
99
  output: process.stdout,
120
- prompt: COLOR.cyan + '> ' + COLOR.reset
100
+ terminal: false
121
101
  });
122
102
 
123
- let lines = [];
124
- rl.prompt();
103
+ process.stdout.write(COLOR.cyan + '> ' + COLOR.reset);
125
104
 
126
105
  rl.on('line', (line) => {
127
106
  if (line.trim() === ':run') {
128
107
  rl.close();
129
108
  fs.writeFileSync(tempFile, lines.join('\n'), 'utf8');
130
- runFile(tempFile, true, () => saveTempFilePrompt(lines));
109
+ runFile(tempFile, true, () => savePrompt(lines));
131
110
  return;
132
111
  }
133
112
 
134
- let coloredLine = line
135
- .replace(/"(.*?)"/g, COLOR.yellow + '"$1"' + COLOR.reset)
136
- .replace(/\b(sldeploy|import|from|const|let|var|if|else|for|while|func|return|break|continue|define|ask)\b/g, COLOR.blue + '$1' + COLOR.reset);
137
-
138
- console.log(coloredLine);
139
-
140
113
  lines.push(line);
141
- rl.prompt();
114
+ process.stdout.write(
115
+ COLOR.cyan + '> ' + COLOR.reset + highlightCode(line) + '\n'
116
+ );
142
117
  });
143
118
 
144
119
  return;
145
120
  }
146
121
 
147
- function saveTempFilePrompt(lines) {
148
- const readlineSync = require('readline-sync');
149
- let saveChoice = readlineSync.question(COLOR.cyan + 'Do you want to save this code? (y/n): ' + COLOR.reset).trim().toLowerCase();
150
- if (saveChoice !== 'y') return;
151
122
 
152
- let folderPath = readlineSync.question(COLOR.cyan + 'Paste folder path to save (C:/ or D:/...): ' + COLOR.reset).trim();
153
- if (!folderPath || !fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) return console.log(COLOR.yellow + 'Skipping save.' + COLOR.reset);
123
+ function savePrompt(lines) {
124
+ try { process.stdin.setRawMode(false); } catch {}
154
125
 
155
- let fileName = readlineSync.question(COLOR.cyan + 'Enter file name (without extension): ' + COLOR.reset).trim();
156
- if (!fileName) return console.log(COLOR.yellow + 'Skipping save.' + COLOR.reset);
126
+ const save = readlineSync
127
+ .question(COLOR.cyan + 'Do you want to save this code? (y/n): ' + COLOR.reset)
128
+ .trim()
129
+ .toLowerCase();
157
130
 
158
- let fullPath = path.join(folderPath, fileName.endsWith('.sl') ? fileName : fileName + '.sl');
159
- while (fs.existsSync(fullPath)) {
160
- fileName = readlineSync.question(COLOR.red + 'File exists! Enter a new name (or leave blank to cancel): ' + COLOR.reset).trim();
161
- if (!fileName) return console.log(COLOR.yellow + 'Skipping save.' + COLOR.reset);
162
- fullPath = path.join(folderPath, fileName.endsWith('.sl') ? fileName : fileName + '.sl');
163
- }
131
+ if (save !== 'y') return waitAndExit(0);
164
132
 
165
- fs.writeFileSync(fullPath, lines.join('\n'), 'utf8');
166
- console.log(COLOR.green + `Saved to ${fullPath}` + COLOR.reset);
167
- }
133
+ const dir = readlineSync.question(
134
+ COLOR.cyan + 'Folder path: ' + COLOR.reset
135
+ ).trim();
168
136
 
169
- function runFile(filePath, isTemp = false, callback = null) {
170
- if (!fs.existsSync(filePath)) {
171
- return fatal(`File not found: ${filePath}`);
137
+ if (!fs.existsSync(dir)) {
138
+ console.log(COLOR.yellow + 'Invalid folder.' + COLOR.reset);
139
+ return waitAndExit(0);
172
140
  }
173
141
 
174
- let code;
175
- try {
176
- code = fs.readFileSync(filePath, 'utf-8');
177
- } catch (err) {
178
- return fatal(`Failed to read file: ${err.message}`);
179
- }
142
+ const name = readlineSync.question(
143
+ COLOR.cyan + 'File name: ' + COLOR.reset
144
+ ).trim();
180
145
 
181
- let tokens;
182
- try {
183
- const lexer = new Lexer(code);
184
- tokens = lexer.getTokens();
185
- } catch (err) {
186
- console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
187
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
188
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
189
- if (err.line && err.column) printSourceContext(code, err.line, err.column);
190
- return waitAndExit(1);
191
- }
146
+ if (!name) return waitAndExit(0);
192
147
 
193
- let ast;
194
- try {
195
- const parser = new Parser(tokens);
196
- ast = parser.parse();
197
- } catch (err) {
198
- console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
199
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
200
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
201
- if (err.token && err.token.line && err.token.column) printSourceContext(code, err.token.line, err.token.column);
202
- return waitAndExit(1);
203
- }
148
+ const file = path.join(dir, name.endsWith('.sl') ? name : name + '.sl');
149
+ fs.writeFileSync(file, lines.join('\n'), 'utf8');
150
+
151
+ console.log(COLOR.green + `Saved to ${file}` + COLOR.reset);
152
+ waitAndExit(0);
153
+ }
154
+
155
+ function runFile(filePath, isTemp = false, callback) {
156
+ const code = fs.readFileSync(filePath, 'utf8');
204
157
 
205
158
  try {
206
- const evaluator = new Evaluator();
207
- evaluator.evaluate(ast);
208
- } catch (err) {
209
- console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
210
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
211
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
212
- if (err.line && err.column) printSourceContext(code, err.line, err.column);
159
+ const lexer = new Lexer(code);
160
+ const parser = new Parser(lexer.getTokens());
161
+ const ast = parser.parse();
162
+ new Evaluator().evaluate(ast);
163
+ } catch (e) {
164
+ console.error(COLOR.red + e.message + COLOR.reset);
213
165
  return waitAndExit(1);
214
166
  }
215
167
 
216
168
  console.log(COLOR.green + '\nProgram finished successfully.' + COLOR.reset);
217
169
 
218
170
  if (callback) callback();
219
-
220
- if (isTemp) {
221
- try { fs.unlinkSync(filePath); } catch {}
222
- }
223
-
224
- waitAndExit(0);
171
+ if (isTemp) try { fs.unlinkSync(filePath); } catch {}
225
172
  }
226
173
 
227
- if (!['--writedirectly', '--viewfilelist', '--learn', '--help', '-v', '--version'].includes(args[0])) {
174
+ if (!args[0].startsWith('--')) {
228
175
  runFile(path.resolve(args[0]));
229
176
  }