starlight-cli 1.0.24 → 1.0.25
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 +329 -365
- package/package.json +1 -1
- package/src/evaluator.js +355 -400
- package/src/starlight.js +1 -1
package/src/evaluator.js
CHANGED
|
@@ -4,435 +4,390 @@ const Lexer = require('./lexer');
|
|
|
4
4
|
const Parser = require('./parser');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
|
|
7
|
-
class ReturnValue {
|
|
8
|
-
constructor(value) { this.value = value; }
|
|
9
|
-
}
|
|
7
|
+
class ReturnValue { constructor(value) { this.value = value; } }
|
|
10
8
|
class BreakSignal {}
|
|
11
9
|
class ContinueSignal {}
|
|
12
10
|
|
|
13
11
|
class Environment {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
12
|
+
constructor(parent = null) {
|
|
13
|
+
this.store = Object.create(null);
|
|
14
|
+
this.parent = parent;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
has(name) {
|
|
18
|
+
if (name in this.store) return true;
|
|
19
|
+
if (this.parent) return this.parent.has(name);
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get(name) {
|
|
24
|
+
if (name in this.store) return this.store[name];
|
|
25
|
+
if (this.parent) return this.parent.get(name);
|
|
26
|
+
throw new Error(`Undefined variable: ${name}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
set(name, value) {
|
|
30
|
+
if (name in this.store) { this.store[name] = value; return value; }
|
|
31
|
+
if (this.parent && this.parent.has(name)) { return this.parent.set(name, value); }
|
|
32
|
+
this.store[name] = value;
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
define(name, value) {
|
|
37
|
+
this.store[name] = value;
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
42
40
|
}
|
|
43
41
|
|
|
44
42
|
class Evaluator {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
43
|
+
constructor() {
|
|
44
|
+
this.global = new Environment();
|
|
45
|
+
this.setupBuiltins();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
setupBuiltins() {
|
|
49
|
+
this.global.define('len', arg => {
|
|
50
|
+
if (Array.isArray(arg) || typeof arg === 'string') return arg.length;
|
|
51
|
+
if (arg && typeof arg === 'object') return Object.keys(arg).length;
|
|
52
|
+
return 0;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
this.global.define('print', arg => { console.log(arg); return null; });
|
|
56
|
+
this.global.define('type', arg => Array.isArray(arg) ? 'array' : typeof arg);
|
|
57
|
+
this.global.define('keys', arg => arg && typeof arg === 'object' ? Object.keys(arg) : []);
|
|
58
|
+
this.global.define('values', arg => arg && typeof arg === 'object' ? Object.values(arg) : []);
|
|
59
|
+
|
|
60
|
+
this.global.define('ask', prompt => readlineSync.question(prompt + ' '));
|
|
61
|
+
this.global.define('num', arg => {
|
|
62
|
+
const n = Number(arg);
|
|
63
|
+
if (Number.isNaN(n)) throw new Error('Cannot convert value to number');
|
|
64
|
+
return n;
|
|
65
|
+
});
|
|
66
|
+
this.global.define('str', arg => String(arg));
|
|
67
|
+
|
|
68
|
+
// Async fetch built-in for API requests
|
|
69
|
+
this.global.define('fetch', async (url, options) => {
|
|
70
|
+
const fetch = require('node-fetch'); // Make sure node-fetch is installed
|
|
71
|
+
const res = await fetch(url, options);
|
|
72
|
+
return res;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async evaluate(node, env = this.global) {
|
|
77
|
+
switch (node.type) {
|
|
78
|
+
case 'Program': return await this.evalProgram(node, env);
|
|
79
|
+
case 'BlockStatement': return await this.evalBlock(node, new Environment(env));
|
|
80
|
+
case 'VarDeclaration': return await this.evalVarDeclaration(node, env);
|
|
81
|
+
case 'AssignmentExpression': return await this.evalAssignment(node, env);
|
|
82
|
+
case 'CompoundAssignment': return await this.evalCompoundAssignment(node, env);
|
|
83
|
+
case 'SldeployStatement': return await this.evalSldeploy(node, env);
|
|
84
|
+
case 'AskStatement': return await this.evalAsk(node, env);
|
|
85
|
+
case 'DefineStatement': return await this.evalDefine(node, env);
|
|
86
|
+
case 'ExpressionStatement': return await this.evaluate(node.expression, env);
|
|
87
|
+
case 'BinaryExpression': return await this.evalBinary(node, env);
|
|
88
|
+
case 'LogicalExpression': return await this.evalLogical(node, env);
|
|
89
|
+
case 'UnaryExpression': return await this.evalUnary(node, env);
|
|
90
|
+
case 'Literal': return node.value;
|
|
91
|
+
case 'Identifier': return env.get(node.name);
|
|
92
|
+
case 'IfStatement': return await this.evalIf(node, env);
|
|
93
|
+
case 'WhileStatement': return await this.evalWhile(node, env);
|
|
94
|
+
case 'ForStatement': return await this.evalFor(node, env);
|
|
95
|
+
case 'BreakStatement': throw new BreakSignal();
|
|
96
|
+
case 'ContinueStatement': throw new ContinueSignal();
|
|
97
|
+
case 'ImportStatement': return await this.evalImport(node, env);
|
|
98
|
+
case 'FunctionDeclaration': return await this.evalFunctionDeclaration(node, env);
|
|
99
|
+
case 'CallExpression': return await this.evalCall(node, env);
|
|
100
|
+
case 'ArrowFunctionExpression': return await this.evalArrowFunction(node, env);
|
|
101
|
+
case 'ReturnStatement': {
|
|
102
|
+
const val = node.argument ? await this.evaluate(node.argument, env) : null;
|
|
103
|
+
throw new ReturnValue(val);
|
|
104
|
+
}
|
|
105
|
+
case 'ArrayExpression': return await Promise.all(node.elements.map(el => this.evaluate(el, env)));
|
|
106
|
+
case 'IndexExpression': return await this.evalIndex(node, env);
|
|
107
|
+
case 'ObjectExpression': return await this.evalObject(node, env);
|
|
108
|
+
case 'MemberExpression': return await this.evalMember(node, env);
|
|
109
|
+
case 'UpdateExpression': return await this.evalUpdate(node, env);
|
|
110
|
+
default:
|
|
111
|
+
throw new Error(`Unknown node type in evaluator: ${node.type}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async evalProgram(node, env) {
|
|
116
|
+
let result = null;
|
|
117
|
+
for (const stmt of node.body) result = await this.evaluate(stmt, env);
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async evalBlock(node, env) {
|
|
122
|
+
let result = null;
|
|
123
|
+
for (const stmt of node.body) {
|
|
124
|
+
try { result = await this.evaluate(stmt, env); }
|
|
125
|
+
catch (e) {
|
|
126
|
+
if (e instanceof ReturnValue || e instanceof BreakSignal || e instanceof ContinueSignal) throw e;
|
|
127
|
+
throw e;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async evalVarDeclaration(node, env) {
|
|
134
|
+
const val = await this.evaluate(node.expr, env);
|
|
135
|
+
return env.define(node.id, val);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async evalArrowFunction(node, env) {
|
|
139
|
+
return { params: node.params, body: node.body, env, arrow: true };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async evalAssignment(node, env) {
|
|
143
|
+
const rightVal = await this.evaluate(node.right, env);
|
|
144
|
+
const left = node.left;
|
|
145
|
+
|
|
146
|
+
if (left.type === 'Identifier') return env.set(left.name, rightVal);
|
|
147
|
+
if (left.type === 'MemberExpression') {
|
|
148
|
+
const obj = await this.evalMemberObj(left, env);
|
|
149
|
+
obj[left.property] = rightVal;
|
|
150
|
+
return rightVal;
|
|
151
|
+
}
|
|
152
|
+
if (left.type === 'IndexExpression') {
|
|
153
|
+
const obj = await this.evalIndexObj(left, env);
|
|
154
|
+
const idx = await this.evaluate(left.indexer, env);
|
|
155
|
+
obj[idx] = rightVal;
|
|
156
|
+
return rightVal;
|
|
157
|
+
}
|
|
158
|
+
throw new Error('Invalid assignment target');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async evalCompoundAssignment(node, env) {
|
|
162
|
+
const left = node.left;
|
|
163
|
+
let current;
|
|
164
|
+
if (left.type === 'Identifier') current = env.get(left.name);
|
|
165
|
+
else if (left.type === 'MemberExpression') current = await this.evalMember(left, env);
|
|
166
|
+
else if (left.type === 'IndexExpression') current = await this.evalIndex(left, env);
|
|
167
|
+
else throw new Error('Invalid compound assignment target');
|
|
168
|
+
|
|
169
|
+
const rhs = await this.evaluate(node.right, env);
|
|
170
|
+
let computed;
|
|
171
|
+
switch (node.operator) {
|
|
172
|
+
case 'PLUSEQ': computed = current + rhs; break;
|
|
173
|
+
case 'MINUSEQ': computed = current - rhs; break;
|
|
174
|
+
case 'STAREQ': computed = current * rhs; break;
|
|
175
|
+
case 'SLASHEQ': computed = current / rhs; break;
|
|
176
|
+
case 'MODEQ': computed = current % rhs; break;
|
|
177
|
+
default: throw new Error('Unknown compound operator');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (left.type === 'Identifier') env.set(left.name, computed);
|
|
181
|
+
else await this.evalAssignment({ left, right: { type: 'Literal', value: computed }, type: 'AssignmentExpression' }, env);
|
|
182
|
+
|
|
183
|
+
return computed;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async evalSldeploy(node, env) {
|
|
187
|
+
const val = await this.evaluate(node.expr, env);
|
|
188
|
+
console.log(val);
|
|
189
|
+
return val;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async evalAsk(node, env) {
|
|
193
|
+
const prompt = await this.evaluate(node.prompt, env);
|
|
67
194
|
return readlineSync.question(prompt + ' ');
|
|
68
|
-
});
|
|
69
|
-
this.global.define('num', arg => {
|
|
70
|
-
const n = Number(arg);
|
|
71
|
-
if (Number.isNaN(n)) {
|
|
72
|
-
throw new Error('Cannot convert value to number');
|
|
73
195
|
}
|
|
74
|
-
return n;
|
|
75
|
-
});
|
|
76
196
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
197
|
+
async evalDefine(node, env) {
|
|
198
|
+
const val = node.expr ? await this.evaluate(node.expr, env) : null;
|
|
199
|
+
return this.global.define(node.id, val);
|
|
200
|
+
}
|
|
80
201
|
|
|
81
|
-
|
|
202
|
+
async evalBinary(node, env) {
|
|
203
|
+
const l = await this.evaluate(node.left, env);
|
|
204
|
+
const r = await this.evaluate(node.right, env);
|
|
205
|
+
|
|
206
|
+
if (node.operator === 'SLASH' && r === 0) throw new Error('Division by zero');
|
|
207
|
+
|
|
208
|
+
switch (node.operator) {
|
|
209
|
+
case 'PLUS': return l + r;
|
|
210
|
+
case 'MINUS': return l - r;
|
|
211
|
+
case 'STAR': return l * r;
|
|
212
|
+
case 'SLASH': return l / r;
|
|
213
|
+
case 'MOD': return l % r;
|
|
214
|
+
case 'EQEQ': return l === r;
|
|
215
|
+
case 'NOTEQ': return l !== r;
|
|
216
|
+
case 'LT': return l < r;
|
|
217
|
+
case 'LTE': return l <= r;
|
|
218
|
+
case 'GT': return l > r;
|
|
219
|
+
case 'GTE': return l >= r;
|
|
220
|
+
default: throw new Error(`Unknown binary operator ${node.operator}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
82
223
|
|
|
224
|
+
async evalLogical(node, env) {
|
|
225
|
+
const l = await this.evaluate(node.left, env);
|
|
226
|
+
if (node.operator === 'AND') return l && await this.evaluate(node.right, env);
|
|
227
|
+
if (node.operator === 'OR') return l || await this.evaluate(node.right, env);
|
|
228
|
+
throw new Error(`Unknown logical operator ${node.operator}`);
|
|
229
|
+
}
|
|
83
230
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
case 'DefineStatement': return this.evalDefine(node, env);
|
|
94
|
-
case 'ExpressionStatement': return this.evaluate(node.expression, env);
|
|
95
|
-
case 'BinaryExpression': return this.evalBinary(node, env);
|
|
96
|
-
case 'LogicalExpression': return this.evalLogical(node, env);
|
|
97
|
-
case 'UnaryExpression': return this.evalUnary(node, env);
|
|
98
|
-
case 'Literal': return node.value;
|
|
99
|
-
case 'Identifier': return env.get(node.name);
|
|
100
|
-
case 'IfStatement': return this.evalIf(node, env);
|
|
101
|
-
case 'WhileStatement': return this.evalWhile(node, env);
|
|
102
|
-
case 'ForStatement': return this.evalFor(node, env);
|
|
103
|
-
case 'BreakStatement': throw new BreakSignal();
|
|
104
|
-
case 'ContinueStatement': throw new ContinueSignal();
|
|
105
|
-
case 'ImportStatement': return this.evalImport(node, env);
|
|
106
|
-
case 'FunctionDeclaration': return this.evalFunctionDeclaration(node, env);
|
|
107
|
-
case 'CallExpression': return this.evalCall(node, env);
|
|
108
|
-
case 'ArrowFunctionExpression':
|
|
109
|
-
return this.evalArrowFunction(node, env);
|
|
110
|
-
|
|
111
|
-
case 'ReturnStatement': {
|
|
112
|
-
const val = node.argument ? this.evaluate(node.argument, env) : null;
|
|
113
|
-
throw new ReturnValue(val);
|
|
114
|
-
}
|
|
115
|
-
case 'ArrayExpression': return node.elements.map(el => this.evaluate(el, env));
|
|
116
|
-
case 'IndexExpression': return this.evalIndex(node, env);
|
|
117
|
-
case 'ObjectExpression': return this.evalObject(node, env);
|
|
118
|
-
case 'MemberExpression': return this.evalMember(node, env);
|
|
119
|
-
case 'UpdateExpression': return this.evalUpdate(node, env);
|
|
120
|
-
default:
|
|
121
|
-
throw new Error(`Unknown node type in evaluator: ${node.type}`);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
evalProgram(node, env) {
|
|
126
|
-
let result = null;
|
|
127
|
-
for (const stmt of node.body) {
|
|
128
|
-
result = this.evaluate(stmt, env);
|
|
129
|
-
}
|
|
130
|
-
return result;
|
|
131
|
-
}
|
|
132
|
-
evalImport(node, env) {
|
|
133
|
-
const spec = node.path;
|
|
134
|
-
let lib;
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
const resolved = require.resolve(spec, {
|
|
138
|
-
paths: [process.cwd()]
|
|
139
|
-
});
|
|
140
|
-
lib = require(resolved);
|
|
141
|
-
} catch (e) {
|
|
142
|
-
const fullPath = path.isAbsolute(spec)
|
|
143
|
-
? spec
|
|
144
|
-
: path.join(process.cwd(), spec.endsWith('.sl') ? spec : spec + '.sl');
|
|
145
|
-
|
|
146
|
-
if (!fs.existsSync(fullPath)) {
|
|
147
|
-
throw new Error(`Import not found: ${spec}`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const code = fs.readFileSync(fullPath, 'utf-8');
|
|
151
|
-
const tokens = new Lexer(code).getTokens();
|
|
152
|
-
const ast = new Parser(tokens).parse();
|
|
153
|
-
|
|
154
|
-
const moduleEnv = new Environment(env);
|
|
155
|
-
this.evaluate(ast, moduleEnv);
|
|
156
|
-
|
|
157
|
-
lib = {};
|
|
158
|
-
for (const key of Object.keys(moduleEnv.store)) {
|
|
159
|
-
lib[key] = moduleEnv.store[key];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
lib.default = lib;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
for (const imp of node.specifiers) {
|
|
166
|
-
if (imp.type === 'DefaultImport') {
|
|
167
|
-
env.define(imp.local, lib.default ?? lib);
|
|
168
|
-
}
|
|
169
|
-
if (imp.type === 'NamespaceImport') {
|
|
170
|
-
env.define(imp.local, lib);
|
|
171
|
-
}
|
|
172
|
-
if (imp.type === 'NamedImport') {
|
|
173
|
-
if (!(imp.imported in lib)) {
|
|
174
|
-
throw new Error(`Module '${spec}' has no export '${imp.imported}'`);
|
|
175
|
-
}
|
|
176
|
-
env.define(imp.local, lib[imp.imported]);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
231
|
+
async evalUnary(node, env) {
|
|
232
|
+
const val = await this.evaluate(node.argument, env);
|
|
233
|
+
switch (node.operator) {
|
|
234
|
+
case 'NOT': return !val;
|
|
235
|
+
case 'MINUS': return -val;
|
|
236
|
+
case 'PLUS': return +val;
|
|
237
|
+
default: throw new Error(`Unknown unary operator ${node.operator}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
182
240
|
|
|
241
|
+
async evalIf(node, env) {
|
|
242
|
+
const test = await this.evaluate(node.test, env);
|
|
243
|
+
if (test) return await this.evaluate(node.consequent, env);
|
|
244
|
+
if (node.alternate) return await this.evaluate(node.alternate, env);
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
183
247
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
throw e;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
return result;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
evalVarDeclaration(node, env) {
|
|
198
|
-
const val = this.evaluate(node.expr, env);
|
|
199
|
-
return env.define(node.id, val);
|
|
200
|
-
}
|
|
201
|
-
evalArrowFunction(node, env) {
|
|
202
|
-
return {
|
|
203
|
-
params: node.params,
|
|
204
|
-
body: node.body,
|
|
205
|
-
env: env,
|
|
206
|
-
arrow: true
|
|
207
|
-
};
|
|
208
|
-
}
|
|
248
|
+
async evalWhile(node, env) {
|
|
249
|
+
while (await this.evaluate(node.test, env)) {
|
|
250
|
+
try { await this.evaluate(node.body, env); }
|
|
251
|
+
catch (e) { if (e instanceof BreakSignal) break; if (e instanceof ContinueSignal) continue; throw e; }
|
|
252
|
+
}
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
209
255
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
obj[idx] = rightVal;
|
|
224
|
-
return rightVal;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
throw new Error('Invalid assignment target');
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
evalCompoundAssignment(node, env) {
|
|
231
|
-
const left = node.left;
|
|
232
|
-
let current;
|
|
233
|
-
|
|
234
|
-
if (left.type === 'Identifier') current = env.get(left.name);
|
|
235
|
-
else if (left.type === 'MemberExpression') current = this.evalMember(left, env);
|
|
236
|
-
else if (left.type === 'IndexExpression') current = this.evalIndex(left, env);
|
|
237
|
-
else throw new Error('Invalid compound assignment target');
|
|
238
|
-
|
|
239
|
-
const rhs = this.evaluate(node.right, env);
|
|
240
|
-
let computed;
|
|
241
|
-
switch (node.operator) {
|
|
242
|
-
case 'PLUSEQ': computed = current + rhs; break;
|
|
243
|
-
case 'MINUSEQ': computed = current - rhs; break;
|
|
244
|
-
case 'STAREQ': computed = current * rhs; break;
|
|
245
|
-
case 'SLASHEQ': computed = current / rhs; break;
|
|
246
|
-
case 'MODEQ': computed = current % rhs; break;
|
|
247
|
-
default: throw new Error('Unknown compound operator');
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
if (left.type === 'Identifier') env.set(left.name, computed);
|
|
251
|
-
else if (left.type === 'MemberExpression') this.evalAssignment({ left, right: { type: 'Literal', value: computed }, type: 'AssignmentExpression' }, env);
|
|
252
|
-
else this.evalAssignment({ left, right: { type: 'Literal', value: computed }, type: 'AssignmentExpression' }, env);
|
|
253
|
-
|
|
254
|
-
return computed;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
evalSldeploy(node, env) {
|
|
258
|
-
const val = this.evaluate(node.expr, env);
|
|
259
|
-
console.log(val);
|
|
260
|
-
return val;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
evalAsk(node, env) {
|
|
264
|
-
const prompt = this.evaluate(node.prompt, env);
|
|
265
|
-
const input = readlineSync.question(prompt + ' ');
|
|
266
|
-
return input;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
evalDefine(node, env) {
|
|
270
|
-
const val = node.expr ? this.evaluate(node.expr, env) : null;
|
|
271
|
-
return this.global.define(node.id, val);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
evalBinary(node, env) {
|
|
275
|
-
const l = this.evaluate(node.left, env);
|
|
276
|
-
const r = this.evaluate(node.right, env);
|
|
277
|
-
|
|
278
|
-
if (node.operator === 'SLASH' && r === 0) {
|
|
279
|
-
throw new Error('Division by zero');
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
switch (node.operator) {
|
|
283
|
-
case 'PLUS': return l + r;
|
|
284
|
-
case 'MINUS': return l - r;
|
|
285
|
-
case 'STAR': return l * r;
|
|
286
|
-
case 'SLASH': return l / r;
|
|
287
|
-
case 'MOD': return l % r;
|
|
288
|
-
case 'EQEQ': return l === r;
|
|
289
|
-
case 'NOTEQ': return l !== r;
|
|
290
|
-
case 'LT': return l < r;
|
|
291
|
-
case 'LTE': return l <= r;
|
|
292
|
-
case 'GT': return l > r;
|
|
293
|
-
case 'GTE': return l >= r;
|
|
294
|
-
default: throw new Error(`Unknown binary operator ${node.operator}`);
|
|
256
|
+
async evalFor(node, env) {
|
|
257
|
+
const local = new Environment(env);
|
|
258
|
+
if (node.init) await this.evaluate(node.init, local);
|
|
259
|
+
while (!node.test || await this.evaluate(node.test, local)) {
|
|
260
|
+
try { await this.evaluate(node.body, local); }
|
|
261
|
+
catch (e) {
|
|
262
|
+
if (e instanceof BreakSignal) break;
|
|
263
|
+
if (e instanceof ContinueSignal) { if (node.update) await this.evaluate(node.update, local); continue; }
|
|
264
|
+
throw e;
|
|
265
|
+
}
|
|
266
|
+
if (node.update) await this.evaluate(node.update, local);
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
295
269
|
}
|
|
296
|
-
}
|
|
297
270
|
|
|
271
|
+
async evalFunctionDeclaration(node, env) {
|
|
272
|
+
const fn = { params: node.params, body: node.body, env };
|
|
273
|
+
env.define(node.name, fn);
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
298
276
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
if (node.operator === 'AND') return l && this.evaluate(node.right, env);
|
|
302
|
-
if (node.operator === 'OR') return l || this.evaluate(node.right, env);
|
|
303
|
-
throw new Error(`Unknown logical operator ${node.operator}`);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
evalUnary(node, env) {
|
|
307
|
-
const val = this.evaluate(node.argument, env);
|
|
308
|
-
switch (node.operator) {
|
|
309
|
-
case 'NOT': return !val;
|
|
310
|
-
case 'MINUS': return -val;
|
|
311
|
-
case 'PLUS': return +val;
|
|
312
|
-
default: throw new Error(`Unknown unary operator ${node.operator}`);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
evalIf(node, env) {
|
|
317
|
-
const test = this.evaluate(node.test, env);
|
|
318
|
-
if (test) return this.evaluate(node.consequent, env);
|
|
319
|
-
if (node.alternate) return this.evaluate(node.alternate, env);
|
|
320
|
-
return null;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
evalWhile(node, env) {
|
|
324
|
-
while (this.evaluate(node.test, env)) {
|
|
325
|
-
try { this.evaluate(node.body, env); }
|
|
326
|
-
catch (e) {
|
|
327
|
-
if (e instanceof BreakSignal) break;
|
|
328
|
-
if (e instanceof ContinueSignal) continue;
|
|
329
|
-
throw e;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
evalFor(node, env) {
|
|
336
|
-
const local = new Environment(env);
|
|
337
|
-
if (node.init) this.evaluate(node.init, local);
|
|
338
|
-
while (!node.test || this.evaluate(node.test, local)) {
|
|
339
|
-
try { this.evaluate(node.body, local); }
|
|
340
|
-
catch (e) {
|
|
341
|
-
if (e instanceof BreakSignal) break;
|
|
342
|
-
if (e instanceof ContinueSignal) { if (node.update) this.evaluate(node.update, local); continue; }
|
|
343
|
-
throw e;
|
|
344
|
-
}
|
|
345
|
-
if (node.update) this.evaluate(node.update, local);
|
|
346
|
-
}
|
|
347
|
-
return null;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
evalFunctionDeclaration(node, env) {
|
|
351
|
-
const fn = { params: node.params, body: node.body, env };
|
|
352
|
-
env.define(node.name, fn);
|
|
353
|
-
return null;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
evalCall(node, env) {
|
|
357
|
-
const calleeEvaluated = this.evaluate(node.callee, env);
|
|
358
|
-
if (typeof calleeEvaluated === 'function') {
|
|
359
|
-
const args = node.arguments.map(a => this.evaluate(a, env));
|
|
360
|
-
return calleeEvaluated(...args);
|
|
361
|
-
}
|
|
362
|
-
if (!calleeEvaluated || typeof calleeEvaluated !== 'object' || !calleeEvaluated.body) {
|
|
363
|
-
throw new Error('Call to non-function');
|
|
364
|
-
}
|
|
365
|
-
const fn = calleeEvaluated;
|
|
366
|
-
const callEnv = new Environment(fn.env);
|
|
367
|
-
fn.params.forEach((p, i) => {
|
|
368
|
-
const argVal = node.arguments[i] ? this.evaluate(node.arguments[i], env) : null;
|
|
369
|
-
callEnv.define(p, argVal);
|
|
370
|
-
});
|
|
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
|
-
}
|
|
277
|
+
async evalCall(node, env) {
|
|
278
|
+
const calleeEvaluated = await this.evaluate(node.callee, env);
|
|
378
279
|
|
|
379
|
-
|
|
280
|
+
if (typeof calleeEvaluated === 'function') {
|
|
281
|
+
const args = [];
|
|
282
|
+
for (const a of node.arguments) args.push(await this.evaluate(a, env));
|
|
283
|
+
return await calleeEvaluated(...args);
|
|
284
|
+
}
|
|
380
285
|
|
|
381
|
-
|
|
382
|
-
const obj = this.evaluate(node.object, env);
|
|
383
|
-
const idx = this.evaluate(node.indexer, env);
|
|
286
|
+
if (!calleeEvaluated || typeof calleeEvaluated !== 'object' || !calleeEvaluated.body) throw new Error('Call to non-function');
|
|
384
287
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
288
|
+
const fn = calleeEvaluated;
|
|
289
|
+
const callEnv = new Environment(fn.env);
|
|
290
|
+
for (let i = 0; i < fn.params.length; i++) {
|
|
291
|
+
const argVal = node.arguments[i] ? await this.evaluate(node.arguments[i], env) : null;
|
|
292
|
+
callEnv.define(fn.params[i], argVal);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
try {
|
|
296
|
+
const result = await this.evaluate(fn.body, callEnv);
|
|
297
|
+
return fn.arrow ? result : result;
|
|
298
|
+
} catch (e) { if (e instanceof ReturnValue) return e.value; throw e; }
|
|
388
299
|
}
|
|
389
|
-
|
|
390
|
-
|
|
300
|
+
|
|
301
|
+
async evalIndex(node, env) {
|
|
302
|
+
const obj = await this.evaluate(node.object, env);
|
|
303
|
+
const idx = await this.evaluate(node.indexer, env);
|
|
304
|
+
|
|
305
|
+
if (obj == null) throw new Error('Indexing null or undefined');
|
|
306
|
+
if (Array.isArray(obj) && (idx < 0 || idx >= obj.length)) throw new Error('Array index out of bounds');
|
|
307
|
+
if (typeof obj === 'object' && !(idx in obj)) throw new Error(`Property '${idx}' does not exist`);
|
|
308
|
+
return obj[idx];
|
|
391
309
|
}
|
|
392
310
|
|
|
393
|
-
|
|
394
|
-
}
|
|
311
|
+
async evalObject(node, env) {
|
|
312
|
+
const out = {};
|
|
313
|
+
for (const p of node.props) out[p.key] = await this.evaluate(p.value, env);
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
395
316
|
|
|
317
|
+
async evalMember(node, env) {
|
|
318
|
+
const obj = await this.evaluate(node.object, env);
|
|
319
|
+
if (obj == null) throw new Error('Member access of null or undefined');
|
|
320
|
+
if (!(node.property in obj)) throw new Error(`Property '${node.property}' does not exist`);
|
|
321
|
+
return obj[node.property];
|
|
322
|
+
}
|
|
396
323
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
324
|
+
async evalMemberObj(node, env) {
|
|
325
|
+
const obj = await this.evaluate(node.object, env);
|
|
326
|
+
if (obj == null) throw new Error('Member access of null or undefined');
|
|
327
|
+
return obj;
|
|
328
|
+
}
|
|
402
329
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
330
|
+
async evalIndexObj(node, env) {
|
|
331
|
+
const obj = await this.evaluate(node.object, env);
|
|
332
|
+
if (obj == null) throw new Error('Indexing null or undefined');
|
|
333
|
+
return obj;
|
|
334
|
+
}
|
|
409
335
|
|
|
336
|
+
async evalUpdate(node, env) {
|
|
337
|
+
const arg = node.argument;
|
|
338
|
+
const getCurrent = async () => {
|
|
339
|
+
if (arg.type === 'Identifier') return env.get(arg.name);
|
|
340
|
+
if (arg.type === 'MemberExpression') return await this.evalMember(arg, env);
|
|
341
|
+
if (arg.type === 'IndexExpression') return await this.evalIndex(arg, env);
|
|
342
|
+
throw new Error('Invalid update target');
|
|
343
|
+
};
|
|
344
|
+
const setValue = async (v) => {
|
|
345
|
+
if (arg.type === 'Identifier') env.set(arg.name, v);
|
|
346
|
+
else if (arg.type === 'MemberExpression') { const obj = await this.evalMemberObj(arg, env); obj[arg.property] = v; }
|
|
347
|
+
else if (arg.type === 'IndexExpression') { const obj = await this.evalIndexObj(arg, env); const idx = await this.evaluate(arg.indexer, env); obj[idx] = v; }
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const current = await getCurrent();
|
|
351
|
+
const newVal = (node.operator === 'PLUSPLUS') ? current + 1 : current - 1;
|
|
352
|
+
|
|
353
|
+
if (node.prefix) { await setValue(newVal); return newVal; }
|
|
354
|
+
else { await setValue(newVal); return current; }
|
|
355
|
+
}
|
|
410
356
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
357
|
+
async evalImport(node, env) {
|
|
358
|
+
const spec = node.path;
|
|
359
|
+
let lib;
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
const resolved = require.resolve(spec, { paths: [process.cwd()] });
|
|
363
|
+
lib = require(resolved);
|
|
364
|
+
} catch (e) {
|
|
365
|
+
const fullPath = path.isAbsolute(spec) ? spec : path.join(process.cwd(), spec.endsWith('.sl') ? spec : spec + '.sl');
|
|
366
|
+
if (!fs.existsSync(fullPath)) throw new Error(`Import not found: ${spec}`);
|
|
367
|
+
|
|
368
|
+
const code = fs.readFileSync(fullPath, 'utf-8');
|
|
369
|
+
const tokens = new Lexer(code).getTokens();
|
|
370
|
+
const ast = new Parser(tokens).parse();
|
|
371
|
+
|
|
372
|
+
const moduleEnv = new Environment(env);
|
|
373
|
+
await this.evaluate(ast, moduleEnv);
|
|
374
|
+
|
|
375
|
+
lib = {};
|
|
376
|
+
for (const key of Object.keys(moduleEnv.store)) lib[key] = moduleEnv.store[key];
|
|
377
|
+
lib.default = lib;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
for (const imp of node.specifiers) {
|
|
381
|
+
if (imp.type === 'DefaultImport') env.define(imp.local, lib.default ?? lib);
|
|
382
|
+
if (imp.type === 'NamespaceImport') env.define(imp.local, lib);
|
|
383
|
+
if (imp.type === 'NamedImport') {
|
|
384
|
+
if (!(imp.imported in lib)) throw new Error(`Module '${spec}' has no export '${imp.imported}'`);
|
|
385
|
+
env.define(imp.local, lib[imp.imported]);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
436
391
|
}
|
|
437
392
|
|
|
438
|
-
module.exports = Evaluator;
|
|
393
|
+
module.exports = Evaluator;
|