starlight-cli 1.1.3 → 1.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Starlight Programming Language CLI",
5
5
  "bin": {
6
6
  "starlight": "index.js"
@@ -13,6 +13,9 @@
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
15
  "blessed": "^0.1.81",
16
+ "markdown-it": "^14.1.0",
17
+ "markdown-it-footnote": "^4.0.0",
18
+ "markdown-it-task-lists": "^2.1.1",
16
19
  "readline-sync": "^1.4.10",
17
20
  "starlight-color": "^1.0.4"
18
21
  },
package/src/evaluator.js CHANGED
@@ -107,7 +107,6 @@ suggest(name, env) {
107
107
 
108
108
  return best;
109
109
  }
110
-
111
110
  formatValue(value, seen = new Set()) {
112
111
  const color = require('starlight-color');
113
112
 
@@ -152,7 +151,7 @@ formatValue(value, seen = new Set()) {
152
151
  } catch {
153
152
  return color.red('[Unprintable]');
154
153
  }
155
- }
154
+ }
156
155
 
157
156
 
158
157
  setupBuiltins() {
@@ -531,16 +530,33 @@ evalArrowFunction(node, env) {
531
530
  if (!node.body) {
532
531
  throw new RuntimeError('Arrow function missing body', node, this.source);
533
532
  }
533
+
534
534
  if (!Array.isArray(node.params)) {
535
535
  throw new RuntimeError('Invalid arrow function parameters', node, this.source);
536
536
  }
537
537
 
538
- return {
539
- params: node.params,
540
- body: node.body,
541
- env: env,
542
- arrow: true,
543
- async: node.async || false
538
+ const evaluator = this;
539
+
540
+ return async function (...args) {
541
+ const localEnv = new Environment(env);
542
+
543
+ node.params.forEach((p, i) => {
544
+ localEnv.define(p.name, args[i]);
545
+ });
546
+
547
+ try {
548
+ if (node.isBlock) {
549
+ const result = await evaluator.evaluate(node.body, localEnv);
550
+ return result ?? null;
551
+ } else {
552
+ return await evaluator.evaluate(node.body, localEnv);
553
+ }
554
+ } catch (err) {
555
+ if (err instanceof ReturnValue) {
556
+ return err.value;
557
+ }
558
+ throw err;
559
+ }
544
560
  };
545
561
  }
546
562
 
package/src/parser.js CHANGED
@@ -700,15 +700,26 @@ postfix() {
700
700
 
701
701
  arrowFunction(params) {
702
702
  const t = this.current;
703
- if (t.type === 'ARROW') this.eat('ARROW');
704
- const body = this.expression();
703
+ this.eat('ARROW');
704
+
705
+ let body;
706
+ let isBlock = false;
707
+
708
+ if (this.current.type === 'LBRACE') {
709
+ body = this.block();
710
+ isBlock = true;
711
+ } else {
712
+ body = this.expression();
713
+ }
714
+
705
715
  const startLine = params.length > 0 ? params[0].line : t.line;
706
- const startCol = params.length > 0 ? params[0].column : t.column;
716
+ const startCol = params.length > 0 ? params[0].column : t.column;
707
717
 
708
718
  return {
709
719
  type: 'ArrowFunctionExpression',
710
720
  params,
711
721
  body,
722
+ isBlock,
712
723
  line: startLine,
713
724
  column: startCol
714
725
  };
package/src/starlight.js CHANGED
@@ -4,12 +4,15 @@ const os = require('os');
4
4
  const { exec } = require('child_process');
5
5
  const readline = require('readline');
6
6
  const readlineSync = require('readline-sync');
7
+ const MarkdownIt = require('markdown-it');
8
+ const mdTaskLists = require('markdown-it-task-lists');
9
+ const mdFootnote = require('markdown-it-footnote');
7
10
 
8
11
  const Lexer = require('./lexer');
9
12
  const Parser = require('./parser');
10
13
  const Evaluator = require('./evaluator');
11
14
 
12
- const VERSION = '1.1.3';
15
+ const VERSION = '1.1.5';
13
16
 
14
17
  const COLOR = {
15
18
  reset: '\x1b[0m',
@@ -35,6 +38,99 @@ function fatal(msg) {
35
38
  console.error(COLOR.white + msg + COLOR.reset);
36
39
  waitAndExit(1);
37
40
  }
41
+ function renderMarkdown(mdPath) {
42
+ let markdown;
43
+ try {
44
+ markdown = fs.readFileSync(mdPath, 'utf8');
45
+ } catch (e) {
46
+ console.error(COLOR.white + `Failed to read markdown: ${e.message}` + COLOR.reset);
47
+ return waitAndExit(1);
48
+ }
49
+
50
+ const md = new MarkdownIt({
51
+ html: true,
52
+ linkify: true,
53
+ typographer: true
54
+ })
55
+ .use(mdTaskLists, { enabled: true })
56
+ .use(mdFootnote);
57
+
58
+ const html = `<!DOCTYPE html>
59
+ <html lang="en">
60
+ <head>
61
+ <meta charset="UTF-8">
62
+ <title>${path.basename(mdPath)}</title>
63
+ <style>
64
+ body {
65
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
66
+ padding: 40px;
67
+ max-width: 900px;
68
+ margin: auto;
69
+ background: #0e0e11;
70
+ color: #eaeaea;
71
+ line-height: 1.6;
72
+ }
73
+ h1, h2, h3, h4 {
74
+ border-bottom: 1px solid #2a2a35;
75
+ padding-bottom: 6px;
76
+ }
77
+ pre {
78
+ background: #1e1e2e;
79
+ padding: 16px;
80
+ overflow-x: auto;
81
+ border-radius: 8px;
82
+ }
83
+ code {
84
+ background: #1e1e2e;
85
+ padding: 2px 6px;
86
+ border-radius: 4px;
87
+ color: #f8f8f2;
88
+ }
89
+ table {
90
+ border-collapse: collapse;
91
+ width: 100%;
92
+ margin: 20px 0;
93
+ }
94
+ th, td {
95
+ border: 1px solid #2a2a35;
96
+ padding: 8px;
97
+ }
98
+ th {
99
+ background: #1b1b25;
100
+ }
101
+ a {
102
+ color: #7aa2f7;
103
+ text-decoration: none;
104
+ }
105
+ a:hover {
106
+ text-decoration: underline;
107
+ }
108
+ blockquote {
109
+ border-left: 4px solid #7aa2f7;
110
+ padding-left: 16px;
111
+ color: #c0caf5;
112
+ margin: 20px 0;
113
+ }
114
+ </style>
115
+ </head>
116
+ <body>
117
+ ${md.render(markdown)}
118
+ </body>
119
+ </html>`;
120
+
121
+ const tempHtml = path.join(
122
+ os.tmpdir(),
123
+ `starlight-md-${Date.now()}.html`
124
+ );
125
+
126
+ fs.writeFileSync(tempHtml, html, 'utf8');
127
+
128
+ openURL(`file://${tempHtml}`);
129
+
130
+ setTimeout(() => {
131
+ try { fs.unlinkSync(tempHtml); } catch {}
132
+ }, 3000);
133
+ }
38
134
 
39
135
  function highlightCode(line) {
40
136
  return line
@@ -216,5 +312,12 @@ async function runFile(filePath, isTemp = false, callback) {
216
312
 
217
313
 
218
314
  if (!args[0].startsWith('--')) {
219
- runFile(path.resolve(args[0]));
315
+ const file = path.resolve(args[0]);
316
+ const ext = path.extname(file).toLowerCase();
317
+
318
+ if (ext === '.md') {
319
+ renderMarkdown(file);
320
+ } else {
321
+ runFile(file);
322
+ }
220
323
  }