plainscript-lang 1.0.35 → 1.0.361
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/README.md +1052 -201
- package/compiler/bundler.js +4 -4
- package/compiler/cli.js +82 -18
- package/compiler/dependency-detector.js +34 -22
- package/compiler/generator.js +587 -172
- package/compiler/lexer.js +61 -39
- package/compiler/parser.js +685 -182
- package/compiler/sourcemap.js +1 -1
- package/compiler/version.js +1 -1
- package/docs/CAPABILITY_GAP_AUDIT.md +25 -4
- package/docs/FB-PROMPT.md +524 -0
- package/docs/GAME-PROMPT.md +916 -0
- package/docs/PLAINSCRIPT-SPEC.md +135 -5
- package/docs/index.html +28 -14
- package/docs/website/script.js +1 -1
- package/docs/website/style.css +162 -195
- package/examples/ai-providers.pln +1 -1
- package/examples/arrays.pln +1 -1
- package/examples/async-errors.pln +2 -1
- package/examples/auth-api.pln +1 -1
- package/examples/auth-sessions.pln +1 -1
- package/examples/basics.pln +1 -1
- package/examples/bots.pln +1 -1
- package/examples/browser-interactive/README.md +29 -0
- package/examples/browser-interactive/public/index.html +83 -0
- package/examples/browser-interactive/public/interactive.js +27 -0
- package/examples/browser-interactive/src/interactive.pln +34 -0
- package/examples/cache-and-jobs.pln +1 -1
- package/examples/cache-schedule.pln +1 -1
- package/examples/canvas-game/README.md +34 -0
- package/examples/canvas-game/public/game.js +131 -0
- package/examples/canvas-game/public/index.html +48 -0
- package/examples/canvas-game/src/game.pln +139 -0
- package/examples/collections.pln +1 -1
- package/examples/concurrency.pln +1 -1
- package/examples/conditions.pln +1 -1
- package/examples/database.pln +1 -1
- package/examples/day2.pln +1 -1
- package/examples/day3.pln +1 -1
- package/examples/deployment.pln +1 -1
- package/examples/email-send.pln +1 -1
- package/examples/error-handling.pln +3 -2
- package/examples/expressions.pln +1 -1
- package/examples/file-uploads.pln +1 -1
- package/examples/files-and-config.pln +1 -1
- package/examples/files.pln +1 -1
- package/examples/football-backend/app.pln +1 -1
- package/examples/functions.pln +1 -1
- package/examples/hello.pln +1 -1
- package/examples/http-client.pln +1 -1
- package/examples/http.pln +1 -1
- package/examples/id-verification/app.pln +1 -1
- package/examples/javascript-library/README.md +54 -0
- package/examples/javascript-library/package-lock.json +21 -0
- package/examples/javascript-library/package.json +8 -0
- package/examples/javascript-library/public/config.json +4 -0
- package/examples/javascript-library/public/index.html +40 -0
- package/examples/javascript-library/src/interop.pln +53 -0
- package/examples/json.pln +1 -1
- package/examples/loops.pln +1 -1
- package/examples/modules/app.pln +1 -1
- package/examples/modules/main.pln +1 -1
- package/examples/modules/math.pln +1 -1
- package/examples/mongo.pln +1 -1
- package/examples/objects.pln +1 -1
- package/examples/ocr.pln +1 -1
- package/examples/packages.pln +1 -1
- package/examples/record-kinds.pln +1 -1
- package/examples/records.pln +1 -1
- package/examples/rest-api-fetch.pln +1 -1
- package/examples/rest-api.pln +1 -1
- package/examples/scheduled-tasks.pln +1 -1
- package/examples/search-and-filter.pln +1 -1
- package/examples/server.pln +1 -1
- package/examples/sessions-and-cookies.pln +1 -1
- package/examples/sqlite.pln +1 -1
- package/examples/start.pln +1 -1
- package/examples/stdlib.pln +1 -1
- package/examples/string-verbs.pln +1 -1
- package/examples/telegram-ai-bot.pln +1 -1
- package/examples/telegram-bot.pln +1 -1
- package/examples/testing.pln +1 -1
- package/examples/three-dimensional/README.md +51 -0
- package/examples/three-dimensional/public/index.html +33 -0
- package/examples/three-dimensional/src/scene.pln +34 -0
- package/examples/web-api.pln +1 -1
- package/examples/web-app.pln +1 -1
- package/examples/websocket-server.pln +1 -1
- package/examples/websocket.pln +1 -1
- package/examples/whatsapp-bot/pairing.pln +2 -2
- package/examples/whatsapp-bot/qr.pln +2 -2
- package/package.json +9 -10
package/compiler/parser.js
CHANGED
|
@@ -4,8 +4,8 @@ const { TOKEN } = require('./lexer');
|
|
|
4
4
|
|
|
5
5
|
// Statement-starting PlainScript keywords, used for "did you mean?" suggestions.
|
|
6
6
|
const STATEMENT_KEYWORDS = [
|
|
7
|
-
'remember', 'show', 'if', 'make', 'give',
|
|
8
|
-
'for', 'while', 'use', 'import', 'when', 'listen', 'reply', 'serve',
|
|
7
|
+
'remember', 'show', 'display', 'log', 'if', 'make', 'give',
|
|
8
|
+
'for', 'while', 'repeat', 'until', 'use', 'import', 'when', 'listen', 'reply', 'serve',
|
|
9
9
|
'web', 'route', 'start', 'database', 'query', 'insert', 'update', 'delete', 'execute',
|
|
10
10
|
'ask', 'javascript', 'bot', 'ocr', 'try', 'recover', 'retry',
|
|
11
11
|
'gather', 'filter', 'total', 'match', 'emit', 'stream', 'run',
|
|
@@ -36,20 +36,22 @@ const NUMBER_WORDS = {
|
|
|
36
36
|
nineteen: 19, twenty: 20,
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
// HTTP methods accepted by the route statement (v2.1.0). Lowercase only
|
|
39
|
+
// HTTP methods accepted by the route statement (v2.1.0). Lowercase only -
|
|
40
40
|
// PlainScript keywords are lowercase by convention.
|
|
41
41
|
const HTTP_METHODS = ['get', 'post', 'put', 'patch', 'delete'];
|
|
42
42
|
|
|
43
43
|
// Time units accepted by the "every <n> <unit>" statement, in milliseconds
|
|
44
|
-
// (used by the generator to build the interval directly).
|
|
44
|
+
// (used by the generator to build the interval directly). "every 16
|
|
45
|
+
// milliseconds" powers requestAnimationFrame-style loops.
|
|
45
46
|
const TIME_UNITS = {
|
|
47
|
+
millisecond: 1, milliseconds: 1,
|
|
46
48
|
second: 1000, seconds: 1000,
|
|
47
49
|
minute: 60 * 1000, minutes: 60 * 1000,
|
|
48
50
|
hour: 60 * 60 * 1000, hours: 60 * 60 * 1000,
|
|
49
51
|
day: 24 * 60 * 60 * 1000, days: 24 * 60 * 60 * 1000,
|
|
50
52
|
};
|
|
51
53
|
|
|
52
|
-
// v2.1.0
|
|
54
|
+
// v2.1.0 - split raw SQL into placeholder-free text and ordered parameter
|
|
53
55
|
// names. "{name}" marks a bound parameter; the generator renders "?" for
|
|
54
56
|
// SQLite or "$1…" for PostgreSQL.
|
|
55
57
|
function extractSqlParams(rawSql) {
|
|
@@ -61,7 +63,7 @@ function extractSqlParams(rawSql) {
|
|
|
61
63
|
return { sql, params };
|
|
62
64
|
}
|
|
63
65
|
|
|
64
|
-
// v2.1.1
|
|
66
|
+
// v2.1.1 - parse an upload size limit such as "5 MB", "512 KB", "1GB" or
|
|
65
67
|
// "100B" into a byte count. Returns null when the text is not a valid size.
|
|
66
68
|
const UPLOAD_SIZE_UNITS = { b: 1, kb: 1024, mb: 1024 * 1024, gb: 1024 * 1024 * 1024 };
|
|
67
69
|
function parseUploadSize(text) {
|
|
@@ -105,6 +107,7 @@ function makeError(message, token) {
|
|
|
105
107
|
|
|
106
108
|
function parse(tokens) {
|
|
107
109
|
let pos = 0;
|
|
110
|
+
let _inRepeatCount = false;
|
|
108
111
|
|
|
109
112
|
function peek() { return tokens[pos]; }
|
|
110
113
|
function peekAt(offset) { return tokens[pos + offset] || { type: TOKEN.EOF }; }
|
|
@@ -130,13 +133,49 @@ function parse(tokens) {
|
|
|
130
133
|
// comparison := the single-comparison forms below
|
|
131
134
|
//
|
|
132
135
|
// Node shapes produced by the comparison level:
|
|
133
|
-
// BinaryCondition { type, left, op, right }
|
|
134
|
-
// UnaryCondition { type, left, op }
|
|
135
|
-
// BetweenCondition { type, left, low, high }
|
|
136
|
-
// StringCondition { type, left, method, right }
|
|
136
|
+
// BinaryCondition { type, left, op, right } - left op right
|
|
137
|
+
// UnaryCondition { type, left, op } - left is empty / is not empty
|
|
138
|
+
// BetweenCondition { type, left, low, high } - left between low and high
|
|
139
|
+
// StringCondition { type, left, method, right } - left contains/startsWith/endsWith right
|
|
137
140
|
// plus LogicalCondition { type, op: "and"|"or", left, right } and
|
|
138
141
|
// { type, op: "not", operand } from the combinator levels.
|
|
139
142
|
|
|
143
|
+
// A comparison-composed expression (produced either by the condition level
|
|
144
|
+
// or, since comparisons are also expressions, directly by parseExpression).
|
|
145
|
+
function isComparisonNode(node) {
|
|
146
|
+
return node && typeof node === 'object' && [
|
|
147
|
+
'BinaryCondition',
|
|
148
|
+
'UnaryCondition',
|
|
149
|
+
'BetweenCondition',
|
|
150
|
+
'StringCondition',
|
|
151
|
+
'InCondition',
|
|
152
|
+
'NotInCondition',
|
|
153
|
+
'LogicalCondition',
|
|
154
|
+
].includes(node.type);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Comparison-operator continuation words/tokens. Used to disambiguate the
|
|
158
|
+
// object-literal shorthand (`let person is name is "Ada"`) from a comparison
|
|
159
|
+
// expression (`let flag is score is above 80`): if the token after the inner
|
|
160
|
+
// `is` can begin a comparison operator, the author means a comparison value.
|
|
161
|
+
function isComparisonContinuationToken(tok) {
|
|
162
|
+
return (
|
|
163
|
+
(tok.type === TOKEN.IDENTIFIER && [
|
|
164
|
+
'equal', 'more', 'fewer', 'same', 'different', 'made', 'has', 'than', 'to'
|
|
165
|
+
].includes(tok.value))
|
|
166
|
+
) || [
|
|
167
|
+
TOKEN.ABOVE, TOKEN.BELOW, TOKEN.AT, TOKEN.GREATER, TOKEN.LESS,
|
|
168
|
+
TOKEN.EMPTY, TOKEN.IN, TOKEN.NOT, TOKEN.CONTAINS, TOKEN.STARTS, TOKEN.ENDS, TOKEN.BETWEEN
|
|
169
|
+
].includes(tok.type);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// True when `name is <op-word> ...` would be a comparison, not an object
|
|
173
|
+
// literal shorthand.
|
|
174
|
+
function isObjectShorthandAmbiguousWithComparison() {
|
|
175
|
+
const afterIs = peekAt(2);
|
|
176
|
+
return !!afterIs && afterIs.type !== TOKEN.EOF && isComparisonContinuationToken(afterIs);
|
|
177
|
+
}
|
|
178
|
+
|
|
140
179
|
// Entry point used by if/while.
|
|
141
180
|
function parseCondition() {
|
|
142
181
|
let left = parseAndCondition();
|
|
@@ -168,121 +207,174 @@ function parse(tokens) {
|
|
|
168
207
|
|
|
169
208
|
function parseComparisonCondition() {
|
|
170
209
|
const left = parseExpression();
|
|
210
|
+
// Comparisons are expressions too, so parseExpression may already have
|
|
211
|
+
// folded this into a comparison node - that IS the comparison level.
|
|
212
|
+
if (isComparisonNode(left)) return left;
|
|
213
|
+
return tryParseComparisonOperator(left, true);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Shared comparison-operator continuation. Given an already-parsed left
|
|
217
|
+
// operand, consumes the comparison operator that follows (if any) and
|
|
218
|
+
// returns the comparison node. The condition level passes required=true
|
|
219
|
+
// (throws a teaching error when no operator follows); the expression level
|
|
220
|
+
// passes required=false (returns `left` untouched, making comparisons
|
|
221
|
+
// optional in expression position the same way they are in JS).
|
|
222
|
+
function tryParseComparisonOperator(left, required) {
|
|
171
223
|
|
|
172
224
|
// ── instanceof condition ───────────────────────────────────────────────────
|
|
173
225
|
if (peek().type === TOKEN.INSTANCEOF) {
|
|
174
226
|
advance();
|
|
175
|
-
const right =
|
|
227
|
+
const right = parseBooleanAtom();
|
|
176
228
|
return { type: 'BinaryCondition', left, op: 'instanceof', right };
|
|
177
229
|
}
|
|
178
230
|
|
|
179
|
-
// v2.4
|
|
231
|
+
// v2.4 - "has field" → "x" in obj (contextual: "has" followed by "field")
|
|
180
232
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'has' &&
|
|
181
233
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'field') {
|
|
182
234
|
advance(); // has
|
|
183
235
|
advance(); // field
|
|
184
|
-
const right =
|
|
236
|
+
const right = parseBooleanAtom();
|
|
185
237
|
return { type: 'BinaryCondition', left: right, op: 'in', right: left };
|
|
186
238
|
}
|
|
187
|
-
// v2.4
|
|
239
|
+
// v2.4 - "starts as" → startsWith (contextual: "starts" followed by "as")
|
|
188
240
|
if (peek().type === TOKEN.STARTS && peekAt(1).type === TOKEN.AS) {
|
|
189
241
|
advance(); // starts
|
|
190
242
|
advance(); // as
|
|
191
|
-
const right =
|
|
243
|
+
const right = parseBooleanAtom();
|
|
192
244
|
return { type: 'StringCondition', left, method: 'startsWith', right };
|
|
193
245
|
}
|
|
194
|
-
// v2.4
|
|
246
|
+
// v2.4 - "ends as" → endsWith (contextual: "ends" followed by "as")
|
|
195
247
|
if (peek().type === TOKEN.ENDS && peekAt(1).type === TOKEN.AS) {
|
|
196
248
|
advance(); // ends
|
|
197
249
|
advance(); // as
|
|
198
|
-
const right =
|
|
250
|
+
const right = parseBooleanAtom();
|
|
199
251
|
return { type: 'StringCondition', left, method: 'endsWith', right };
|
|
200
252
|
}
|
|
201
|
-
// v2.4
|
|
253
|
+
// v2.4 - "made of" → includes (contextual: "made" followed by "of")
|
|
202
254
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'made' &&
|
|
203
255
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
204
256
|
advance(); // made
|
|
205
257
|
advance(); // of
|
|
206
|
-
const right =
|
|
258
|
+
const right = parseBooleanAtom();
|
|
207
259
|
return { type: 'StringCondition', left, method: 'includes', right };
|
|
208
260
|
}
|
|
209
|
-
// v2.4
|
|
261
|
+
// v2.4 - "more than" → > (contextual: "more" followed by "than")
|
|
210
262
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'more' &&
|
|
211
263
|
peekAt(1).type === TOKEN.THAN) {
|
|
212
264
|
advance(); // more
|
|
213
265
|
advance(); // than
|
|
214
|
-
const right =
|
|
266
|
+
const right = parseBooleanAtom();
|
|
215
267
|
return { type: 'BinaryCondition', left, op: '>', right };
|
|
216
268
|
}
|
|
217
|
-
// v2.4
|
|
269
|
+
// v2.4 - "fewer than" → < (contextual: "fewer" followed by "than")
|
|
218
270
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'fewer' &&
|
|
219
271
|
peekAt(1).type === TOKEN.THAN) {
|
|
220
272
|
advance(); // fewer
|
|
221
273
|
advance(); // than
|
|
222
|
-
const right =
|
|
274
|
+
const right = parseBooleanAtom();
|
|
223
275
|
return { type: 'BinaryCondition', left, op: '<', right };
|
|
224
276
|
}
|
|
225
|
-
// v2.4
|
|
277
|
+
// v2.4 - "same as" → === (contextual: "same" followed by "as")
|
|
226
278
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'same' &&
|
|
227
279
|
peekAt(1).type === TOKEN.AS) {
|
|
228
280
|
advance(); // same
|
|
229
281
|
advance(); // as
|
|
230
|
-
const right =
|
|
282
|
+
const right = parseBooleanAtom();
|
|
231
283
|
return { type: 'BinaryCondition', left, op: '===', right };
|
|
232
284
|
}
|
|
233
|
-
// v2.4
|
|
285
|
+
// v2.4 - "different from" → !== (contextual: "different" followed by "from")
|
|
234
286
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'different' &&
|
|
235
287
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'from') {
|
|
236
288
|
advance(); // different
|
|
237
289
|
advance(); // from
|
|
238
|
-
const right =
|
|
290
|
+
const right = parseBooleanAtom();
|
|
239
291
|
return { type: 'BinaryCondition', left, op: '!==', right };
|
|
240
292
|
}
|
|
241
293
|
|
|
242
294
|
// ── Non-"is" operators ──────────────────────────────────────────────────
|
|
295
|
+
// "in" / "not in" condition (e.g. if item in list)
|
|
296
|
+
if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
|
|
297
|
+
advance();
|
|
298
|
+
const right = parseBooleanAtom();
|
|
299
|
+
return { type: 'InCondition', left, right };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (peek().type === TOKEN.NOT && (peekAt(1).type === TOKEN.IN || (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'in'))) {
|
|
303
|
+
advance(); // not
|
|
304
|
+
advance(); // in
|
|
305
|
+
const right = parseBooleanAtom();
|
|
306
|
+
return { type: 'NotInCondition', left, right };
|
|
307
|
+
}
|
|
308
|
+
|
|
243
309
|
if (peek().type === TOKEN.CONTAINS) {
|
|
244
310
|
advance();
|
|
245
|
-
const right =
|
|
311
|
+
const right = parseBooleanAtom();
|
|
246
312
|
return { type: 'StringCondition', left, method: 'includes', right };
|
|
247
313
|
}
|
|
248
314
|
|
|
249
315
|
if (peek().type === TOKEN.STARTS) {
|
|
250
316
|
advance();
|
|
251
317
|
consume(TOKEN.WITH, makeError('Expected "with" after "starts". Use: starts with', peek()));
|
|
252
|
-
const right =
|
|
318
|
+
const right = parseBooleanAtom();
|
|
253
319
|
return { type: 'StringCondition', left, method: 'startsWith', right };
|
|
254
320
|
}
|
|
255
321
|
|
|
256
322
|
if (peek().type === TOKEN.ENDS) {
|
|
257
323
|
advance();
|
|
258
324
|
consume(TOKEN.WITH, makeError('Expected "with" after "ends". Use: ends with', peek()));
|
|
259
|
-
const right =
|
|
325
|
+
const right = parseBooleanAtom();
|
|
260
326
|
return { type: 'StringCondition', left, method: 'endsWith', right };
|
|
261
327
|
}
|
|
262
328
|
|
|
263
329
|
if (peek().type === TOKEN.BETWEEN) {
|
|
264
330
|
advance();
|
|
265
|
-
const low =
|
|
331
|
+
const low = parseBooleanAtom();
|
|
266
332
|
consume(TOKEN.AND, makeError('Expected "and" after the lower bound in "between" expression.\n\nExample:\n if x between 1 and 10', peek()));
|
|
267
|
-
const high =
|
|
333
|
+
const high = parseBooleanAtom();
|
|
268
334
|
return { type: 'BetweenCondition', left, low, high };
|
|
269
335
|
}
|
|
270
336
|
|
|
271
337
|
// ── "is ..." comparisons ────────────────────────────────────────────────
|
|
272
338
|
const isToken = peek();
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
339
|
+
if (isToken.type !== TOKEN.IS) {
|
|
340
|
+
if (!required) return left;
|
|
341
|
+
throw new Error(makeError(
|
|
342
|
+
'Expected a comparison after the value. Use "is", "is above", "is below", "contains", "starts with", etc.',
|
|
343
|
+
isToken
|
|
344
|
+
));
|
|
345
|
+
}
|
|
346
|
+
advance();
|
|
347
|
+
|
|
348
|
+
// is in <list>
|
|
349
|
+
if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
|
|
350
|
+
advance();
|
|
351
|
+
const right = parseBooleanAtom();
|
|
352
|
+
return { type: 'InCondition', left, right };
|
|
353
|
+
}
|
|
277
354
|
|
|
278
|
-
// is not empty / is not <expr>
|
|
355
|
+
// is not empty / is not in <expr> / is not equal to <expr> / is not <expr>
|
|
279
356
|
if (peek().type === TOKEN.NOT) {
|
|
280
357
|
advance();
|
|
358
|
+
// is not in <list>
|
|
359
|
+
if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
|
|
360
|
+
advance();
|
|
361
|
+
const right = parseBooleanAtom();
|
|
362
|
+
return { type: 'NotInCondition', left, right };
|
|
363
|
+
}
|
|
281
364
|
if (peek().type === TOKEN.EMPTY) {
|
|
282
365
|
advance();
|
|
283
366
|
return { type: 'UnaryCondition', left, op: 'isNotEmpty' };
|
|
284
367
|
}
|
|
285
|
-
|
|
368
|
+
// is not equal [to] <expr>
|
|
369
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'equal') {
|
|
370
|
+
advance();
|
|
371
|
+
if (peek().type === TOKEN.TO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'to')) {
|
|
372
|
+
advance();
|
|
373
|
+
}
|
|
374
|
+
const right = parseBooleanAtom();
|
|
375
|
+
return { type: 'BinaryCondition', left, op: '!==', right };
|
|
376
|
+
}
|
|
377
|
+
const right = parseBooleanAtom();
|
|
286
378
|
return { type: 'BinaryCondition', left, op: '!==', right };
|
|
287
379
|
}
|
|
288
380
|
|
|
@@ -292,17 +384,36 @@ function parse(tokens) {
|
|
|
292
384
|
return { type: 'UnaryCondition', left, op: 'isEmpty' };
|
|
293
385
|
}
|
|
294
386
|
|
|
387
|
+
// is equal to <expr> / is equal <expr>
|
|
388
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'equal') {
|
|
389
|
+
advance();
|
|
390
|
+
if (peek().type === TOKEN.TO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'to')) {
|
|
391
|
+
advance();
|
|
392
|
+
}
|
|
393
|
+
const right = parseBooleanAtom();
|
|
394
|
+
return { type: 'BinaryCondition', left, op: '===', right };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// is more than <expr>
|
|
398
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'more' &&
|
|
399
|
+
(peekAt(1).type === TOKEN.THAN || (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'than'))) {
|
|
400
|
+
advance(); // more
|
|
401
|
+
advance(); // than
|
|
402
|
+
const right = parseBooleanAtom();
|
|
403
|
+
return { type: 'BinaryCondition', left, op: '>', right };
|
|
404
|
+
}
|
|
405
|
+
|
|
295
406
|
// is above (alias: >)
|
|
296
407
|
if (peek().type === TOKEN.ABOVE) {
|
|
297
408
|
advance();
|
|
298
|
-
const right =
|
|
409
|
+
const right = parseBooleanAtom();
|
|
299
410
|
return { type: 'BinaryCondition', left, op: '>', right };
|
|
300
411
|
}
|
|
301
412
|
|
|
302
413
|
// is below (alias: <)
|
|
303
414
|
if (peek().type === TOKEN.BELOW) {
|
|
304
415
|
advance();
|
|
305
|
-
const right =
|
|
416
|
+
const right = parseBooleanAtom();
|
|
306
417
|
return { type: 'BinaryCondition', left, op: '<', right };
|
|
307
418
|
}
|
|
308
419
|
|
|
@@ -311,35 +422,53 @@ function parse(tokens) {
|
|
|
311
422
|
advance();
|
|
312
423
|
if (peek().type === TOKEN.LEAST) {
|
|
313
424
|
advance();
|
|
314
|
-
const right =
|
|
425
|
+
const right = parseBooleanAtom();
|
|
315
426
|
return { type: 'BinaryCondition', left, op: '>=', right };
|
|
316
427
|
}
|
|
317
428
|
if (peek().type === TOKEN.MOST) {
|
|
318
429
|
advance();
|
|
319
|
-
const right =
|
|
430
|
+
const right = parseBooleanAtom();
|
|
320
431
|
return { type: 'BinaryCondition', left, op: '<=', right };
|
|
321
432
|
}
|
|
322
433
|
throw new Error(makeError('Expected "least" or "most" after "at". Use: is at least / is at most', peek()));
|
|
323
434
|
}
|
|
324
435
|
|
|
325
|
-
// is greater than
|
|
436
|
+
// is greater than [or equal to]
|
|
326
437
|
if (peek().type === TOKEN.GREATER) {
|
|
327
438
|
advance();
|
|
328
439
|
consume(TOKEN.THAN, 'Expected "than" after "greater". Use: is greater than');
|
|
329
|
-
|
|
440
|
+
if (peek().type === TOKEN.OR && peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'equal') {
|
|
441
|
+
advance(); // or
|
|
442
|
+
advance(); // equal
|
|
443
|
+
if (peek().type === TOKEN.TO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'to')) {
|
|
444
|
+
advance(); // to
|
|
445
|
+
}
|
|
446
|
+
const right = parseBooleanAtom();
|
|
447
|
+
return { type: 'BinaryCondition', left, op: '>=', right };
|
|
448
|
+
}
|
|
449
|
+
const right = parseBooleanAtom();
|
|
330
450
|
return { type: 'BinaryCondition', left, op: '>', right };
|
|
331
451
|
}
|
|
332
452
|
|
|
333
|
-
// is less than
|
|
453
|
+
// is less than [or equal to]
|
|
334
454
|
if (peek().type === TOKEN.LESS) {
|
|
335
455
|
advance();
|
|
336
456
|
consume(TOKEN.THAN, 'Expected "than" after "less". Use: is less than');
|
|
337
|
-
|
|
457
|
+
if (peek().type === TOKEN.OR && peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'equal') {
|
|
458
|
+
advance(); // or
|
|
459
|
+
advance(); // equal
|
|
460
|
+
if (peek().type === TOKEN.TO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'to')) {
|
|
461
|
+
advance(); // to
|
|
462
|
+
}
|
|
463
|
+
const right = parseBooleanAtom();
|
|
464
|
+
return { type: 'BinaryCondition', left, op: '<=', right };
|
|
465
|
+
}
|
|
466
|
+
const right = parseBooleanAtom();
|
|
338
467
|
return { type: 'BinaryCondition', left, op: '<', right };
|
|
339
468
|
}
|
|
340
469
|
|
|
341
470
|
// is <expr> (equality)
|
|
342
|
-
const right =
|
|
471
|
+
const right = parseBooleanAtom();
|
|
343
472
|
return { type: 'BinaryCondition', left, op: '===', right };
|
|
344
473
|
}
|
|
345
474
|
|
|
@@ -383,6 +512,17 @@ function parse(tokens) {
|
|
|
383
512
|
if (token.type === TOKEN.SHOW) return parseShow();
|
|
384
513
|
if (token.type === TOKEN.PRINT) return parseShow(); // alias for show
|
|
385
514
|
if (token.type === TOKEN.DISPLAY) return parseShow(); // alias for show
|
|
515
|
+
// log <expr> / log message (WhatsApp)
|
|
516
|
+
if (token.type === TOKEN.LOG_KW || (token.type === TOKEN.IDENTIFIER && token.value === 'log')) {
|
|
517
|
+
if (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'message') {
|
|
518
|
+
advance(); // log
|
|
519
|
+
advance(); // message
|
|
520
|
+
return { type: 'WhatsAppLogStatement' };
|
|
521
|
+
}
|
|
522
|
+
if (token.type === TOKEN.LOG_KW || (peekAt(1).type !== TOKEN.DOT && peekAt(1).type !== TOKEN.LPAREN && peekAt(1).type !== TOKEN.BECOMES)) {
|
|
523
|
+
return parseShow();
|
|
524
|
+
}
|
|
525
|
+
}
|
|
386
526
|
if (token.type === TOKEN.IF) return parseIf();
|
|
387
527
|
if (token.type === TOKEN.MAKE) return parseMake();
|
|
388
528
|
if (token.type === TOKEN.DEFINE) return parseMake(); // alias for make
|
|
@@ -390,17 +530,28 @@ function parse(tokens) {
|
|
|
390
530
|
if (token.type === TOKEN.GIVE) return parseGive();
|
|
391
531
|
if (token.type === TOKEN.RETURN) return parseGive(); // alias for give
|
|
392
532
|
if (token.type === TOKEN.GIVE_BACK) return parseGive(); // alias for give
|
|
393
|
-
// v1.0.1
|
|
533
|
+
// v1.0.1 - generators: `yield <expr>` is only meaningful inside a function
|
|
394
534
|
// body; the generator marks the enclosing `make ... done` as a function*.
|
|
395
535
|
if (token.type === TOKEN.YIELD) return parseYield();
|
|
396
536
|
if (token.type === TOKEN.FOR) return parseForEach();
|
|
397
|
-
//
|
|
537
|
+
// Natural intent repeat loops (counted, repeat with, repeat while, repeat until)
|
|
538
|
+
if (token.type === TOKEN.REPEAT || (token.type === TOKEN.IDENTIFIER && token.value === 'repeat')) {
|
|
539
|
+
return parseRepeat();
|
|
540
|
+
}
|
|
541
|
+
// v2.1.0 - every <n> <unit>s … done: interval scheduling. "every" also
|
|
398
542
|
// lexes as TOKEN.EACH (the "for every" alias), so only the number+unit
|
|
399
543
|
// form is intercepted; "for every item in list" keeps its meaning.
|
|
400
544
|
if (token.type === TOKEN.EACH && peekAt(1).type === TOKEN.NUMBER &&
|
|
401
545
|
peekAt(2).type === TOKEN.IDENTIFIER && TIME_UNITS[peekAt(2).value]) {
|
|
402
546
|
return parseEvery();
|
|
403
547
|
}
|
|
548
|
+
// v1.0.361 - every frame … done: requestAnimationFrame loop. "every" lexes
|
|
549
|
+
// as TOKEN.EACH; guard on the following "frame" identifier ("for every
|
|
550
|
+
// item in list" is intercepted by TOKEN.FOR above).
|
|
551
|
+
if (token.type === TOKEN.EACH && peekAt(1).type === TOKEN.IDENTIFIER &&
|
|
552
|
+
peekAt(1).value === 'frame') {
|
|
553
|
+
return parseEveryFrame();
|
|
554
|
+
}
|
|
404
555
|
if (token.type === TOKEN.WHILE) return parseWhile();
|
|
405
556
|
if (token.type === TOKEN.USE) return parseUse();
|
|
406
557
|
if (token.type === TOKEN.IMPORT || token.type === TOKEN.INCLUDE || token.type === TOKEN.LOAD ||
|
|
@@ -408,9 +559,9 @@ function parse(tokens) {
|
|
|
408
559
|
return parseImport();
|
|
409
560
|
}
|
|
410
561
|
if (token.type === TOKEN.WHEN) return parseWhen();
|
|
411
|
-
// v2.3
|
|
562
|
+
// v2.3 - English-like function declaration: "to add a and b together ... done"
|
|
412
563
|
if (token.type === TOKEN.TO && peekAt(1).type === TOKEN.IDENTIFIER) return parseToFunction();
|
|
413
|
-
// v2.4
|
|
564
|
+
// v2.4 - "list with" and "record with" as expressions
|
|
414
565
|
if (token.type === TOKEN.IDENTIFIER && token.value === 'list' && peekAt(1).type === TOKEN.WITH) return parseListWith();
|
|
415
566
|
if (token.type === TOKEN.IDENTIFIER && token.value === 'record' && peekAt(1).type === TOKEN.WITH) return parseRecordWith(false);
|
|
416
567
|
if (token.type === TOKEN.IDENTIFIER && token.value === 'put' && peekAt(1).type !== TOKEN.LPAREN) return parsePutStatement();
|
|
@@ -432,7 +583,7 @@ function parse(tokens) {
|
|
|
432
583
|
advance();
|
|
433
584
|
return { type: 'DebuggerStatement' };
|
|
434
585
|
}
|
|
435
|
-
// v2.0.1
|
|
586
|
+
// v2.0.1 - OCR capability
|
|
436
587
|
if (token.type === TOKEN.OCR_KW) return parseOcr();
|
|
437
588
|
// v0.6
|
|
438
589
|
if (token.type === TOKEN.WEB) return parseWebApp();
|
|
@@ -442,7 +593,7 @@ function parse(tokens) {
|
|
|
442
593
|
if (token.type === TOKEN.DATABASE_KW) return parseDatabase();
|
|
443
594
|
if (token.type === TOKEN.CONNECT_DB) return parseDatabase(); // alias
|
|
444
595
|
if (token.type === TOKEN.USE_DATABASE) return parseDatabase(); // alias
|
|
445
|
-
// v2.1.0
|
|
596
|
+
// v2.1.0 - query("field"): the HTTP query-string accessor. The SQLite
|
|
446
597
|
// block form keeps priority when "query" stands alone on a line; a
|
|
447
598
|
// call form is always the accessor.
|
|
448
599
|
if (token.type === TOKEN.QUERY_KW && peekAt(1).type === TOKEN.LPAREN) {
|
|
@@ -451,7 +602,7 @@ function parse(tokens) {
|
|
|
451
602
|
if (token.type === TOKEN.QUERY_KW) return parseSqlBlock('query', 'QueryStatement');
|
|
452
603
|
if (token.type === TOKEN.INSERT_KW) return parseSqlBlock('insert', 'InsertStatement');
|
|
453
604
|
if (token.type === TOKEN.UPDATE_KW) return parseSqlBlock('update', 'UpdateStatement');
|
|
454
|
-
// v2.1.1
|
|
605
|
+
// v2.1.1 - delete "<url>" is an HTTP DELETE request; a bare "delete"
|
|
455
606
|
// starting a raw block keeps its SQL meaning.
|
|
456
607
|
if (token.type === TOKEN.DELETE_KW && tokenStartsValue(peekAt(1))) {
|
|
457
608
|
return { type: 'ExpressionStatement', expression: parseHttpCall('delete') };
|
|
@@ -477,7 +628,7 @@ function parse(tokens) {
|
|
|
477
628
|
|
|
478
629
|
// Statements starting with an identifier: call, becomes, index/member becomes
|
|
479
630
|
if (token.type === TOKEN.IDENTIFIER) {
|
|
480
|
-
// v2.5
|
|
631
|
+
// v2.5 - natural string and collection verbs that transform a variable
|
|
481
632
|
// in place: "lowercase title", "uppercase first letter of each word in title",
|
|
482
633
|
// "split title by \" \"", "join title by \" \"", "trim title".
|
|
483
634
|
const TRANSFORM_VERB = token.value === 'lowercase' || token.value === 'uppercase' ||
|
|
@@ -544,12 +695,15 @@ function parse(tokens) {
|
|
|
544
695
|
return { type: 'ReturnStatement', value };
|
|
545
696
|
}
|
|
546
697
|
|
|
547
|
-
// new expression: new ClassName(args)
|
|
698
|
+
// new expression: new ClassName(args). "new" is not a keyword, so
|
|
699
|
+
// parsePrimary intercepts it anywhere an expression appears; routing the
|
|
700
|
+
// statement-start form through parsePrimary keeps member chains after the
|
|
701
|
+
// constructor intact (new THREE.Scene().add(...)).
|
|
548
702
|
if (token.value === 'new' && peekAt(1).type !== TOKEN.BECOMES && peekAt(1).type !== TOKEN.LPAREN) {
|
|
549
|
-
return { type: 'ExpressionStatement', expression:
|
|
703
|
+
return { type: 'ExpressionStatement', expression: parsePrimary() };
|
|
550
704
|
}
|
|
551
705
|
|
|
552
|
-
// v1.2
|
|
706
|
+
// v1.2 - bot "<token>" / bot <expr>: creates the polling Telegram bot.
|
|
553
707
|
// Bound to BOT by the generator's `bot` stdlib. Only intercepts when a
|
|
554
708
|
// value follows; bot(...) calls and ordinary identifiers (e.g. a
|
|
555
709
|
// variable named bot) keep their normal meaning.
|
|
@@ -559,7 +713,7 @@ function parse(tokens) {
|
|
|
559
713
|
peekAt(1).type === TOKEN.LBRACKET || peekAt(1).type === TOKEN.LBRACE;
|
|
560
714
|
if (token.value === 'bot' && nextIsValue) return parseBot();
|
|
561
715
|
|
|
562
|
-
// v2.1.0
|
|
716
|
+
// v2.1.0 - allow cors: enables CORS middleware on the current app.
|
|
563
717
|
// Contextual (not a reserved keyword): a variable named "allow" keeps
|
|
564
718
|
// its ordinary meaning.
|
|
565
719
|
if (token.value === 'allow' &&
|
|
@@ -569,7 +723,7 @@ function parse(tokens) {
|
|
|
569
723
|
return { type: 'AllowCorsStatement' };
|
|
570
724
|
}
|
|
571
725
|
|
|
572
|
-
// v2.1.0
|
|
726
|
+
// v2.1.0 - group "<prefix>" ... done: composes routes under a shared
|
|
573
727
|
// path prefix. Contextual like "bot": only intercepted when a quoted
|
|
574
728
|
// prefix follows.
|
|
575
729
|
if (token.value === 'group' && peekAt(1).type === TOKEN.STRING) {
|
|
@@ -579,7 +733,7 @@ function parse(tokens) {
|
|
|
579
733
|
return { type: 'GroupStatement', prefix, body };
|
|
580
734
|
}
|
|
581
735
|
|
|
582
|
-
// v2.1.0
|
|
736
|
+
// v2.1.0 - postgres "<connection>": binds the PostgreSQL driver.
|
|
583
737
|
// Contextual: a variable named "postgres" keeps its meaning unless a
|
|
584
738
|
// connection value follows on the same line.
|
|
585
739
|
if (token.value === 'postgres' && (
|
|
@@ -588,7 +742,7 @@ function parse(tokens) {
|
|
|
588
742
|
return parsePostgres();
|
|
589
743
|
}
|
|
590
744
|
|
|
591
|
-
// v2.2.0
|
|
745
|
+
// v2.2.0 - mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
|
|
592
746
|
// Contextual: a variable named "mongo" or "mongodb" keeps its meaning unless
|
|
593
747
|
// a connection string follows on the same line.
|
|
594
748
|
if ((token.value === 'mongo' || token.value === 'mongodb') && (
|
|
@@ -597,7 +751,7 @@ function parse(tokens) {
|
|
|
597
751
|
return parseMongo();
|
|
598
752
|
}
|
|
599
753
|
|
|
600
|
-
// v2.1.0
|
|
754
|
+
// v2.1.0 - cache "<redis-url>" / cache env("REDIS_URL"): connects the
|
|
601
755
|
// shared Redis client used by cacheGet / cacheSet / cacheDelete.
|
|
602
756
|
// Contextual like "postgres": a variable named "cache" keeps its
|
|
603
757
|
// meaning unless a connection value follows on the same line.
|
|
@@ -609,7 +763,7 @@ function parse(tokens) {
|
|
|
609
763
|
return { type: 'CacheStatement', url };
|
|
610
764
|
}
|
|
611
765
|
|
|
612
|
-
// v2.1.0
|
|
766
|
+
// v2.1.0 - transaction … done: groups database writes atomically.
|
|
613
767
|
// Contextual: "transaction becomes x" and "transaction(...)" keep
|
|
614
768
|
// their ordinary variable/function meanings.
|
|
615
769
|
if (token.value === 'transaction' &&
|
|
@@ -619,7 +773,7 @@ function parse(tokens) {
|
|
|
619
773
|
return { type: 'TransactionStatement', body };
|
|
620
774
|
}
|
|
621
775
|
|
|
622
|
-
// v2.1.0
|
|
776
|
+
// v2.1.0 - mail transport … done: configures the outgoing mailer.
|
|
623
777
|
if (token.value === 'mail' &&
|
|
624
778
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'transport') {
|
|
625
779
|
advance(); // mail
|
|
@@ -627,7 +781,7 @@ function parse(tokens) {
|
|
|
627
781
|
return { type: 'MailTransportStatement', options: parsePropertyList('"mail transport" block') };
|
|
628
782
|
}
|
|
629
783
|
|
|
630
|
-
// v2.1.0
|
|
784
|
+
// v2.1.0 - send mail … done: sends one email through the transport.
|
|
631
785
|
if (token.value === 'send' &&
|
|
632
786
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'mail') {
|
|
633
787
|
advance(); // send
|
|
@@ -635,15 +789,31 @@ function parse(tokens) {
|
|
|
635
789
|
return { type: 'SendMailStatement', fields: parsePropertyList('"send mail" block') };
|
|
636
790
|
}
|
|
637
791
|
|
|
638
|
-
// v2.1.0
|
|
639
|
-
// ("every" lexes as TOKEN.EACH
|
|
792
|
+
// v2.1.0 - every <n> <unit>s … done: repeat work on an interval.
|
|
793
|
+
// ("every" lexes as TOKEN.EACH - see the EACH dispatch above - so this
|
|
640
794
|
// identifier-form branch only guards against future lexer changes.)
|
|
641
795
|
if (token.value === 'every' && peekAt(1).type === TOKEN.NUMBER &&
|
|
642
796
|
peekAt(2).type === TOKEN.IDENTIFIER && TIME_UNITS[peekAt(2).value]) {
|
|
643
797
|
return parseEvery();
|
|
644
798
|
}
|
|
645
799
|
|
|
646
|
-
//
|
|
800
|
+
// v1.0.361 - every frame … done: requestAnimationFrame loop. Identifier
|
|
801
|
+
// mirror of the EACH dispatch above.
|
|
802
|
+
if (token.value === 'every' && peekAt(1).type === TOKEN.IDENTIFIER &&
|
|
803
|
+
peekAt(1).value === 'frame') {
|
|
804
|
+
return parseEveryFrame();
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// v1.0.361 - after <n> <unit>s … done: one-shot delayed execution.
|
|
808
|
+
// Contextual like "retry": "after becomes 5" and after(...) keep their
|
|
809
|
+
// ordinary meanings.
|
|
810
|
+
if (token.value === 'after' &&
|
|
811
|
+
tokenStartsValue(peekAt(1)) &&
|
|
812
|
+
peekAt(2).type === TOKEN.IDENTIFIER && TIME_UNITS[peekAt(2).value]) {
|
|
813
|
+
return parseAfter();
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// v2.1.0 - schedule "<cron>" … done: run work on a cron schedule.
|
|
647
817
|
if (token.value === 'schedule' && peekAt(1).type === TOKEN.STRING) {
|
|
648
818
|
advance(); // schedule
|
|
649
819
|
const expression = advance().value; // cron string
|
|
@@ -651,7 +821,7 @@ function parse(tokens) {
|
|
|
651
821
|
return { type: 'ScheduleStatement', expression, body };
|
|
652
822
|
}
|
|
653
823
|
|
|
654
|
-
// v2.1.0
|
|
824
|
+
// v2.1.0 - run background <call>: fire-and-forget execution.
|
|
655
825
|
if (token.value === 'run' &&
|
|
656
826
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'background') {
|
|
657
827
|
advance(); // run
|
|
@@ -672,13 +842,13 @@ function parse(tokens) {
|
|
|
672
842
|
return parseRunParallelStatement();
|
|
673
843
|
}
|
|
674
844
|
|
|
675
|
-
// v2.1.0
|
|
845
|
+
// v2.1.0 - websocket server on <port> … done: realtime endpoint.
|
|
676
846
|
if (token.value === 'websocket' &&
|
|
677
847
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'server') {
|
|
678
848
|
return parseWebSocketServer();
|
|
679
849
|
}
|
|
680
850
|
|
|
681
|
-
// v2.1.1
|
|
851
|
+
// v2.1.1 - whatsapp bot … done: WhatsApp bot runtime (Baileys under
|
|
682
852
|
// the hood). Contextual: a variable named "whatsapp" keeps its meaning;
|
|
683
853
|
// only "whatsapp bot" opens the block.
|
|
684
854
|
if (token.value === 'whatsapp' &&
|
|
@@ -686,7 +856,7 @@ function parse(tokens) {
|
|
|
686
856
|
return parseWhatsAppBot();
|
|
687
857
|
}
|
|
688
858
|
|
|
689
|
-
// v2.1.1
|
|
859
|
+
// v2.1.1 - pair whatsapp "<phone>": starts an on-demand WhatsApp pairing
|
|
690
860
|
// session for the given phone number. Used by hybrid bots where a
|
|
691
861
|
// Telegram command triggers a WhatsApp pairing flow.
|
|
692
862
|
// Accepts a string literal (validated at compile time) or any expression
|
|
@@ -703,7 +873,7 @@ function parse(tokens) {
|
|
|
703
873
|
return { type: 'WhatsAppPairStatement', phone: phoneExpr };
|
|
704
874
|
}
|
|
705
875
|
|
|
706
|
-
// v2.1.1
|
|
876
|
+
// v2.1.1 - log message: prints the normalized message record inside an
|
|
707
877
|
// "on message" handler. Generation rejects it everywhere else with a
|
|
708
878
|
// teaching error.
|
|
709
879
|
if (token.value === 'log' &&
|
|
@@ -713,7 +883,7 @@ function parse(tokens) {
|
|
|
713
883
|
return { type: 'WhatsAppLogStatement' };
|
|
714
884
|
}
|
|
715
885
|
|
|
716
|
-
// v2.14
|
|
886
|
+
// v2.14 - download "<path>": saves the current message's media (image,
|
|
717
887
|
// video, audio, document, sticker, …) to a file, inside an "on message"
|
|
718
888
|
// handler. Generation rejects it everywhere else with a teaching error.
|
|
719
889
|
if (token.value === 'download' && peekAt(1).type === TOKEN.STRING) {
|
|
@@ -722,14 +892,14 @@ function parse(tokens) {
|
|
|
722
892
|
return { type: 'WhatsAppDownloadStatement', filePath };
|
|
723
893
|
}
|
|
724
894
|
|
|
725
|
-
// v2.1.0
|
|
895
|
+
// v2.1.0 - broadcast <expr>: sends to every connected socket.
|
|
726
896
|
if (token.value === 'broadcast') {
|
|
727
897
|
advance(); // broadcast
|
|
728
898
|
const value = parseExpression();
|
|
729
899
|
return { type: 'BroadcastStatement', value };
|
|
730
900
|
}
|
|
731
901
|
|
|
732
|
-
// v2.2.0
|
|
902
|
+
// v2.2.0 - send to <connection> <message>: send to a specific stored connection.
|
|
733
903
|
if (token.value === 'send' &&
|
|
734
904
|
(peekAt(1).type === TOKEN.TO || (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'to'))) {
|
|
735
905
|
advance(); // send
|
|
@@ -739,7 +909,7 @@ function parse(tokens) {
|
|
|
739
909
|
return { type: 'SendToStatement', connection, value };
|
|
740
910
|
}
|
|
741
911
|
|
|
742
|
-
// v2.1.0
|
|
912
|
+
// v2.1.0 - send socket <expr>: replies to one connected socket.
|
|
743
913
|
if (token.value === 'send' &&
|
|
744
914
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'socket') {
|
|
745
915
|
advance(); // send
|
|
@@ -748,7 +918,7 @@ function parse(tokens) {
|
|
|
748
918
|
return { type: 'SendSocketStatement', value };
|
|
749
919
|
}
|
|
750
920
|
|
|
751
|
-
// v2.1.0
|
|
921
|
+
// v2.1.0 - status 404 / status <expr>: sets the HTTP response status.
|
|
752
922
|
// Contextual: "status becomes 404" is still a normal reassignment of a
|
|
753
923
|
// variable named "status", and "status(...)" remains a function call.
|
|
754
924
|
if (token.value === 'status' && peekAt(1).type !== TOKEN.BECOMES &&
|
|
@@ -760,7 +930,7 @@ function parse(tokens) {
|
|
|
760
930
|
return { type: 'StatusStatement', value };
|
|
761
931
|
}
|
|
762
932
|
|
|
763
|
-
// v2.2.0
|
|
933
|
+
// v2.2.0 - redirect to "<url>": sends an HTTP redirect from a route
|
|
764
934
|
// handler. "redirect(...)" calls and "redirect becomes x" stay ordinary.
|
|
765
935
|
if (token.value === 'redirect' && (peekAt(1).value === 'to' || peekAt(1).type === TOKEN.TO)) {
|
|
766
936
|
advance(); // redirect
|
|
@@ -846,7 +1016,7 @@ function parse(tokens) {
|
|
|
846
1016
|
return { type: 'GoogleOAuthStatement', options: parsePropertyList('"google oauth" block') };
|
|
847
1017
|
}
|
|
848
1018
|
|
|
849
|
-
// ── v1.0.1
|
|
1019
|
+
// ── v1.0.1 - capability-gap features (all contextual, IOPL-native) ──
|
|
850
1020
|
|
|
851
1021
|
// define a kind called "Person" with … done → record schema (classes)
|
|
852
1022
|
if ((token.type === TOKEN.DEFINE || (token.type === TOKEN.IDENTIFIER && token.value === 'define')) &&
|
|
@@ -917,7 +1087,19 @@ function parse(tokens) {
|
|
|
917
1087
|
|
|
918
1088
|
// Assignment operators: becomes, is now, set to, change to, or becomes (||=), and becomes (&&=), nullish becomes (??=)
|
|
919
1089
|
let becomeOp = null;
|
|
920
|
-
if (peek().type === TOKEN.
|
|
1090
|
+
if (peek().type === TOKEN.PLUS_ASSIGN) {
|
|
1091
|
+
advance(); // ++=
|
|
1092
|
+
becomeOp = '+=';
|
|
1093
|
+
} else if (peek().type === TOKEN.LOGICAL_OR_ASSIGN) {
|
|
1094
|
+
advance(); // ||=
|
|
1095
|
+
becomeOp = '||=';
|
|
1096
|
+
} else if (peek().type === TOKEN.LOGICAL_AND_ASSIGN) {
|
|
1097
|
+
advance(); // &&=
|
|
1098
|
+
becomeOp = '&&=';
|
|
1099
|
+
} else if (peek().type === TOKEN.NULLISH_ASSIGN) {
|
|
1100
|
+
advance(); // ??=
|
|
1101
|
+
becomeOp = '??=';
|
|
1102
|
+
} else if (peek().type === TOKEN.OR && peekAt(1).type === TOKEN.BECOMES) {
|
|
921
1103
|
advance(); // or
|
|
922
1104
|
advance(); // becomes
|
|
923
1105
|
becomeOp = '||=';
|
|
@@ -1011,7 +1193,7 @@ function parse(tokens) {
|
|
|
1011
1193
|
}
|
|
1012
1194
|
|
|
1013
1195
|
// Object literal: next token is IDENTIFIER (except dictionary/map) followed by IS or BE
|
|
1014
|
-
if (peek().type === TOKEN.IDENTIFIER && peek().value !== 'dictionary' && peek().value !== 'map' && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE)) {
|
|
1196
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value !== 'dictionary' && peek().value !== 'map' && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE) && !isObjectShorthandAmbiguousWithComparison()) {
|
|
1015
1197
|
const init = parseInlineObjectLiteral(false);
|
|
1016
1198
|
return { type: 'VariableDeclaration', name: target, initializer: init, isLet, isConstant: !isLet };
|
|
1017
1199
|
}
|
|
@@ -1027,11 +1209,11 @@ function parse(tokens) {
|
|
|
1027
1209
|
);
|
|
1028
1210
|
|
|
1029
1211
|
// Object literal: next token is IDENTIFIER followed by IS or BE
|
|
1030
|
-
if (peek().type === TOKEN.IDENTIFIER && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE)) {
|
|
1212
|
+
if (peek().type === TOKEN.IDENTIFIER && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE) && !isObjectShorthandAmbiguousWithComparison()) {
|
|
1031
1213
|
return { type: 'RememberStatement', name: target, value: parseObjectLiteral() };
|
|
1032
1214
|
}
|
|
1033
1215
|
|
|
1034
|
-
// v2.1.0
|
|
1216
|
+
// v2.1.0 - remember <name> as query|insert|update|delete … done
|
|
1035
1217
|
// Captures the SQL result: rows for "query", the run info (changes /
|
|
1036
1218
|
// lastInsertRowid) for the write forms. The call form query("field")
|
|
1037
1219
|
// is the HTTP accessor and still parses as an expression below.
|
|
@@ -1072,7 +1254,7 @@ function parseAsk() {
|
|
|
1072
1254
|
return { type: 'AskStatement', variable };
|
|
1073
1255
|
}
|
|
1074
1256
|
|
|
1075
|
-
// v2.0.1
|
|
1257
|
+
// v2.0.1 - OCR capability.
|
|
1076
1258
|
//
|
|
1077
1259
|
// ocr "<image>" as <variable>
|
|
1078
1260
|
// ocr "<image>" as <variable> using "<lang>"
|
|
@@ -1102,10 +1284,13 @@ function parseAsk() {
|
|
|
1102
1284
|
function parseShow() {
|
|
1103
1285
|
const isPrint = peek().type === TOKEN.PRINT;
|
|
1104
1286
|
const isDisplay = peek().type === TOKEN.DISPLAY;
|
|
1287
|
+
const isLog = peek().type === TOKEN.LOG_KW || (peek().type === TOKEN.IDENTIFIER && peek().value === 'log');
|
|
1105
1288
|
if (isPrint) {
|
|
1106
1289
|
consume(TOKEN.PRINT);
|
|
1107
1290
|
} else if (isDisplay) {
|
|
1108
1291
|
consume(TOKEN.DISPLAY);
|
|
1292
|
+
} else if (isLog) {
|
|
1293
|
+
advance();
|
|
1109
1294
|
} else {
|
|
1110
1295
|
consume(TOKEN.SHOW);
|
|
1111
1296
|
}
|
|
@@ -1179,7 +1364,7 @@ function parseAsk() {
|
|
|
1179
1364
|
return { type: 'FunctionDeclaration', name, params, body };
|
|
1180
1365
|
}
|
|
1181
1366
|
|
|
1182
|
-
// v2.3
|
|
1367
|
+
// v2.3 - English-like function declaration: "to <name> <a> and <b> together ... done"
|
|
1183
1368
|
// Parameters are identifiers joined by "and". Body ends with "together" or "done".
|
|
1184
1369
|
function parseToFunction() {
|
|
1185
1370
|
consume(TOKEN.TO);
|
|
@@ -1213,21 +1398,24 @@ function parseAsk() {
|
|
|
1213
1398
|
}
|
|
1214
1399
|
params.push({ name: advance().value });
|
|
1215
1400
|
}
|
|
1401
|
+
// The params loop stops at "together"/"done" - consume the terminator here
|
|
1402
|
+
// so parseBody parses the real body instead of treating this as an empty one.
|
|
1403
|
+
advance(); // consume "together" or "done"
|
|
1216
1404
|
const body = parseBody(`function "${name}"`);
|
|
1217
1405
|
return { type: 'FunctionDeclaration', name, params, body };
|
|
1218
1406
|
}
|
|
1219
1407
|
|
|
1220
|
-
// v2.3
|
|
1408
|
+
// v2.3 - English-like array literal: "list with 1, 2, 3"
|
|
1221
1409
|
// Parse comma-separated elements for "list with" (shared helper)
|
|
1222
1410
|
function parseListWithElements() {
|
|
1223
1411
|
const elements = [];
|
|
1224
1412
|
if (peek().type !== TOKEN.DONE && peek().type !== TOKEN.TOGETHER && peek().type !== TOKEN.EOF &&
|
|
1225
1413
|
peek().type !== TOKEN.FOLDER &&
|
|
1226
1414
|
!(peek().type === TOKEN.IDENTIFIER && (peek().value === 'folder' || peek().value === 'done' || peek().value === 'together'))) {
|
|
1227
|
-
elements.push(
|
|
1415
|
+
elements.push(parseBooleanAtom());
|
|
1228
1416
|
while (peek().type === TOKEN.COMMA) {
|
|
1229
1417
|
advance(); // consume comma
|
|
1230
|
-
elements.push(
|
|
1418
|
+
elements.push(parseBooleanAtom());
|
|
1231
1419
|
}
|
|
1232
1420
|
}
|
|
1233
1421
|
return elements;
|
|
@@ -1245,7 +1433,7 @@ function parseAsk() {
|
|
|
1245
1433
|
return { type: 'ArrayLiteral', elements };
|
|
1246
1434
|
}
|
|
1247
1435
|
|
|
1248
|
-
// v2.3
|
|
1436
|
+
// v2.3 - English-like object literal: "record with name 'Alice' and age 30"
|
|
1249
1437
|
// consumeTerminator: true when used as expression (done closes record), false when standalone statement
|
|
1250
1438
|
function parseRecordWith(consumeTerminator) {
|
|
1251
1439
|
// Accept both "record_with" keyword and contextual "record" + "with"
|
|
@@ -1289,9 +1477,14 @@ function parseAsk() {
|
|
|
1289
1477
|
if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1290
1478
|
while (true) {
|
|
1291
1479
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1292
|
-
|
|
1480
|
+
// Key parsed below the comparison level so `"a" is 1 and "b" is 2`
|
|
1481
|
+
// keeps the `is` as the dictionary separator (parseExpression would
|
|
1482
|
+
// fold `"a" is 1` into a comparison).
|
|
1483
|
+
const key = parseNullish();
|
|
1293
1484
|
consume(TOKEN.IS, 'Expected "is" between key and value in dictionary/map.');
|
|
1294
|
-
|
|
1485
|
+
// Value is parsed below the boolean level so `"a" is 1 and "b" is 2`
|
|
1486
|
+
// keeps `and` as the pair separator. Comparisons still fold.
|
|
1487
|
+
const value = parseBooleanAtom();
|
|
1295
1488
|
pairs.push({ key, value });
|
|
1296
1489
|
if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
|
|
1297
1490
|
advance();
|
|
@@ -1313,7 +1506,7 @@ function parseAsk() {
|
|
|
1313
1506
|
if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1314
1507
|
while (true) {
|
|
1315
1508
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1316
|
-
elements.push(
|
|
1509
|
+
elements.push(parseBooleanAtom());
|
|
1317
1510
|
if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
1318
1511
|
advance();
|
|
1319
1512
|
continue;
|
|
@@ -1334,7 +1527,7 @@ function parseAsk() {
|
|
|
1334
1527
|
if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1335
1528
|
while (true) {
|
|
1336
1529
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1337
|
-
elements.push(
|
|
1530
|
+
elements.push(parseBooleanAtom());
|
|
1338
1531
|
if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
1339
1532
|
advance();
|
|
1340
1533
|
continue;
|
|
@@ -1382,7 +1575,7 @@ function parseAsk() {
|
|
|
1382
1575
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'tuple') {
|
|
1383
1576
|
advance(); // tuple
|
|
1384
1577
|
}
|
|
1385
|
-
const tupleExpr =
|
|
1578
|
+
const tupleExpr = parseBooleanAtom();
|
|
1386
1579
|
if (peek().type === TOKEN.INTO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'into')) {
|
|
1387
1580
|
advance(); // into
|
|
1388
1581
|
}
|
|
@@ -1483,9 +1676,10 @@ function parseAsk() {
|
|
|
1483
1676
|
}
|
|
1484
1677
|
|
|
1485
1678
|
function parseGive() {
|
|
1486
|
-
const
|
|
1487
|
-
const
|
|
1488
|
-
const
|
|
1679
|
+
const kwToken = peek();
|
|
1680
|
+
const isReturn = kwToken.type === TOKEN.RETURN;
|
|
1681
|
+
const isGiveBack = kwToken.type === TOKEN.GIVE_BACK;
|
|
1682
|
+
const isGive = kwToken.type === TOKEN.GIVE;
|
|
1489
1683
|
if (isReturn) {
|
|
1490
1684
|
consume(TOKEN.RETURN);
|
|
1491
1685
|
} else if (isGiveBack) {
|
|
@@ -1496,11 +1690,18 @@ function parseAsk() {
|
|
|
1496
1690
|
} else {
|
|
1497
1691
|
consume(TOKEN.GIVE);
|
|
1498
1692
|
}
|
|
1499
|
-
|
|
1693
|
+
let value = null;
|
|
1694
|
+
const next = peek();
|
|
1695
|
+
if (next.type !== TOKEN.DONE && next.type !== TOKEN.TOGETHER && next.type !== TOKEN.OTHERWISE &&
|
|
1696
|
+
next.type !== TOKEN.ELSE && next.type !== TOKEN.END && next.type !== TOKEN.EOF) {
|
|
1697
|
+
value = parseExpression();
|
|
1698
|
+
} else if (!isReturn) {
|
|
1699
|
+
throw new Error(makeError('Expected a value after "give".\n\nExample:\n give a + b', kwToken));
|
|
1700
|
+
}
|
|
1500
1701
|
return { type: 'GiveStatement', value };
|
|
1501
1702
|
}
|
|
1502
1703
|
|
|
1503
|
-
// v1.0.1
|
|
1704
|
+
// v1.0.1 - generators: `yield <expr>` (optionally bare `yield`).
|
|
1504
1705
|
function parseYield() {
|
|
1505
1706
|
consume(TOKEN.YIELD);
|
|
1506
1707
|
let value = null;
|
|
@@ -1510,13 +1711,13 @@ function parseAsk() {
|
|
|
1510
1711
|
return { type: 'YieldStatement', value };
|
|
1511
1712
|
}
|
|
1512
1713
|
|
|
1513
|
-
// v1.0.1
|
|
1714
|
+
// v1.0.1 - record kinds: `define a kind called "Person" with … done`
|
|
1514
1715
|
// The field block reuses the property-list grammar (`age is 0`), so each
|
|
1515
1716
|
// field gets a default expression just like an object literal.
|
|
1516
1717
|
function parseDefineKind() {
|
|
1517
1718
|
advance(); // define
|
|
1518
1719
|
const art = advance(); // "a" / "an"
|
|
1519
|
-
// `define a kind called "Person"`
|
|
1720
|
+
// `define a kind called "Person"` - the word "kind" is optional prose.
|
|
1520
1721
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'kind') {
|
|
1521
1722
|
advance(); // kind
|
|
1522
1723
|
} else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'called') {
|
|
@@ -1556,7 +1757,7 @@ function parseAsk() {
|
|
|
1556
1757
|
return { type: 'DefineKindStatement', name, fields, extends: extendsKind };
|
|
1557
1758
|
}
|
|
1558
1759
|
|
|
1559
|
-
// v1.0.1
|
|
1760
|
+
// v1.0.1 - record constructor (expression): `create a Person with name "Ada" and age 17`
|
|
1560
1761
|
function parseCreateKind() {
|
|
1561
1762
|
advance(); // create
|
|
1562
1763
|
advance(); // a / an
|
|
@@ -1576,7 +1777,7 @@ function parseAsk() {
|
|
|
1576
1777
|
return { type: 'CreateKindExpression', kind, pairs };
|
|
1577
1778
|
}
|
|
1578
1779
|
|
|
1579
|
-
// v1.0.1
|
|
1780
|
+
// v1.0.1 - native test DSL: `test "name" … done`
|
|
1580
1781
|
function parseTestStatement() {
|
|
1581
1782
|
advance(); // test
|
|
1582
1783
|
const name = consume(TOKEN.STRING, 'Expected a test name string after "test".').value;
|
|
@@ -1584,10 +1785,13 @@ function parseAsk() {
|
|
|
1584
1785
|
return { type: 'TestStatement', name, body };
|
|
1585
1786
|
}
|
|
1586
1787
|
|
|
1587
|
-
// v1.0.1
|
|
1788
|
+
// v1.0.1 - assertions: `check <a> (equals|is|contains|raises) <b>`
|
|
1588
1789
|
function parseCheckStatement() {
|
|
1589
1790
|
advance(); // check
|
|
1590
|
-
|
|
1791
|
+
// Parse the operand below the comparison level: `check` consumes the
|
|
1792
|
+
// equals/is/contains/raises operator itself, so parseExpression must not
|
|
1793
|
+
// fold `a contains b` into a comparison here.
|
|
1794
|
+
const a = parseNullish();
|
|
1591
1795
|
const opToken = peek();
|
|
1592
1796
|
if (!['equals', 'is', 'contains', 'raises'].includes(opToken.value)) {
|
|
1593
1797
|
throw new Error(makeError(
|
|
@@ -1596,12 +1800,12 @@ function parseAsk() {
|
|
|
1596
1800
|
));
|
|
1597
1801
|
}
|
|
1598
1802
|
const op = advance().value;
|
|
1599
|
-
const b =
|
|
1803
|
+
const b = parseBooleanAtom();
|
|
1600
1804
|
return { type: 'CheckStatement', a, op, b };
|
|
1601
1805
|
}
|
|
1602
1806
|
|
|
1603
1807
|
// for each <item> in <collection> ... done
|
|
1604
|
-
// for every <item> in <collection> ... done (alias
|
|
1808
|
+
// for every <item> in <collection> ... done (alias - "every" maps to EACH token)
|
|
1605
1809
|
function parseForEach() {
|
|
1606
1810
|
consume(TOKEN.FOR);
|
|
1607
1811
|
|
|
@@ -1613,7 +1817,7 @@ function parseAsk() {
|
|
|
1613
1817
|
'Expected an index name after "for index".\n\nExample:\n for index i from 0 to 9\n show i\n done'
|
|
1614
1818
|
).value;
|
|
1615
1819
|
if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
|
|
1616
|
-
// for index <name> in <collection>
|
|
1820
|
+
// for index <name> in <collection> - zero-based index over a list.
|
|
1617
1821
|
advance(); // in
|
|
1618
1822
|
const collection = parseExpression();
|
|
1619
1823
|
const body = parseBody('"for index" loop');
|
|
@@ -1666,6 +1870,54 @@ function parseAsk() {
|
|
|
1666
1870
|
return { type: 'WhileStatement', condition, body };
|
|
1667
1871
|
}
|
|
1668
1872
|
|
|
1873
|
+
// Natural intent repeat loops:
|
|
1874
|
+
// repeat N times ... done
|
|
1875
|
+
// repeat with <item> in <collection> ... done
|
|
1876
|
+
// repeat while <condition> ... done
|
|
1877
|
+
// repeat until <condition> ... done
|
|
1878
|
+
function parseRepeat() {
|
|
1879
|
+
advance(); // consume repeat
|
|
1880
|
+
// Form 1: repeat with <item> in <collection> ... done
|
|
1881
|
+
if ((peek().type === TOKEN.WITH || (peek().type === TOKEN.IDENTIFIER && peek().value === 'with')) &&
|
|
1882
|
+
peekAt(1).type === TOKEN.IDENTIFIER) {
|
|
1883
|
+
advance(); // with
|
|
1884
|
+
const item = consume(TOKEN.IDENTIFIER, 'Expected an item name after "repeat with".\n\nExample:\n repeat with item in items\n show item\n done').value;
|
|
1885
|
+
consume(TOKEN.IN, `Expected "in" after "${item}".\n\nExample:\n repeat with item in items`);
|
|
1886
|
+
const collection = parseExpression();
|
|
1887
|
+
const body = parseBody('"repeat with" loop');
|
|
1888
|
+
return { type: 'ForEachStatement', item, collection, body };
|
|
1889
|
+
}
|
|
1890
|
+
// Form 2: repeat while <condition> ... done
|
|
1891
|
+
if (peek().type === TOKEN.WHILE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'while')) {
|
|
1892
|
+
advance(); // while
|
|
1893
|
+
const condition = parseCondition();
|
|
1894
|
+
const body = parseBody('"repeat while" loop');
|
|
1895
|
+
return { type: 'WhileStatement', condition, body };
|
|
1896
|
+
}
|
|
1897
|
+
// Form 3: repeat until <condition> ... done
|
|
1898
|
+
if (peek().type === TOKEN.UNTIL || (peek().type === TOKEN.IDENTIFIER && peek().value === 'until')) {
|
|
1899
|
+
advance(); // until
|
|
1900
|
+
const condition = parseCondition();
|
|
1901
|
+
const body = parseBody('"repeat until" loop');
|
|
1902
|
+
return { type: 'WhileStatement', condition: { type: 'LogicalCondition', op: 'not', operand: condition }, body };
|
|
1903
|
+
}
|
|
1904
|
+
// Form 4: repeat <count> times ... done
|
|
1905
|
+
_inRepeatCount = true;
|
|
1906
|
+
let count;
|
|
1907
|
+
try {
|
|
1908
|
+
count = parseExpression();
|
|
1909
|
+
} finally {
|
|
1910
|
+
_inRepeatCount = false;
|
|
1911
|
+
}
|
|
1912
|
+
if (peek().type === TOKEN.TIMES_WORD || (peek().type === TOKEN.IDENTIFIER && peek().value === 'times')) {
|
|
1913
|
+
advance(); // times
|
|
1914
|
+
} else {
|
|
1915
|
+
throw new Error(makeError('Expected "times" after count in "repeat" loop.\n\nExample:\n repeat 5 times\n show "hello"\n done', peek()));
|
|
1916
|
+
}
|
|
1917
|
+
const body = parseBody('"repeat" loop');
|
|
1918
|
+
return { type: 'RepeatTimesStatement', count, body };
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1669
1921
|
// Enterprise & Intent-Oriented Module Importing & Exporting
|
|
1670
1922
|
// bring circleArea from "./math.pln"
|
|
1671
1923
|
// bring circleArea and squareArea from "./math.pln"
|
|
@@ -1811,8 +2063,8 @@ function parseAsk() {
|
|
|
1811
2063
|
};
|
|
1812
2064
|
}
|
|
1813
2065
|
|
|
1814
|
-
// use <module>
|
|
1815
|
-
// use <module> as <name>
|
|
2066
|
+
// use <module> - side-effect or canonical binding
|
|
2067
|
+
// use <module> as <name> - bind the package to a custom variable name
|
|
1816
2068
|
// <module> may carry a version range: use left-pad@^1.3.0
|
|
1817
2069
|
function parseUse() {
|
|
1818
2070
|
consume(TOKEN.USE);
|
|
@@ -1842,7 +2094,7 @@ function parseAsk() {
|
|
|
1842
2094
|
function parseWhen() {
|
|
1843
2095
|
consume(TOKEN.WHEN);
|
|
1844
2096
|
|
|
1845
|
-
// v2.3
|
|
2097
|
+
// v2.3 - "when <condition> ... done": English-like if statement.
|
|
1846
2098
|
// Detect: when <expr> is ... / when <expr> above ... etc.
|
|
1847
2099
|
// Must come after existing "when" event-handler checks.
|
|
1848
2100
|
// Check if next token starts a condition (not someone, not "nothing", not socket,
|
|
@@ -1854,6 +2106,31 @@ function parseAsk() {
|
|
|
1854
2106
|
(nextToken.type === TOKEN.IDENTIFIER && (nextToken.value === 'nothing' || nextToken.value === 'socket'));
|
|
1855
2107
|
|
|
1856
2108
|
if (!isEventWhen) {
|
|
2109
|
+
// v1.0.361 - when <target> "<event>" happens [as <name>] … done binds a
|
|
2110
|
+
// DOM event listener: when button "click" happens => addEventListener.
|
|
2111
|
+
// Detection probes the target expression and commits only when a string
|
|
2112
|
+
// followed by "happens" is next; otherwise the tokens are replayed and
|
|
2113
|
+
// the block parses as the English-like if-condition "when <condition>".
|
|
2114
|
+
const savedPos = pos;
|
|
2115
|
+
try {
|
|
2116
|
+
const target = parseExpression();
|
|
2117
|
+
if (peek().type === TOKEN.STRING && peekAt(1).type === TOKEN.HAPPENS) {
|
|
2118
|
+
const eventStr = advance().value;
|
|
2119
|
+
advance(); // happens
|
|
2120
|
+
let paramName = null;
|
|
2121
|
+
if (peek().type === TOKEN.AS) {
|
|
2122
|
+
advance(); // as
|
|
2123
|
+
paramName = consume(TOKEN.IDENTIFIER,
|
|
2124
|
+
'Expected a parameter name after "as".\n\nExample:\n when button "click" happens as event').value;
|
|
2125
|
+
}
|
|
2126
|
+
const body = parseBody('"when happens" block');
|
|
2127
|
+
return { type: 'WhenTargetedStatement', target, event: eventStr, paramName, body };
|
|
2128
|
+
}
|
|
2129
|
+
} catch (_e) {
|
|
2130
|
+
// Not a targeted "when" - replay and fall through to the condition form.
|
|
2131
|
+
}
|
|
2132
|
+
pos = savedPos;
|
|
2133
|
+
|
|
1857
2134
|
const condition = parseCondition();
|
|
1858
2135
|
// Optional "then" keyword
|
|
1859
2136
|
if (peek().type === TOKEN.THEN) {
|
|
@@ -1874,7 +2151,7 @@ function parseAsk() {
|
|
|
1874
2151
|
return { type: 'IfStatement', condition, consequent, alternate: null };
|
|
1875
2152
|
}
|
|
1876
2153
|
|
|
1877
|
-
// v2.1.1
|
|
2154
|
+
// v2.1.1 - "when nothing matches … done" registers the 404 catch-all.
|
|
1878
2155
|
// It must appear after every route in the source; the generated handler
|
|
1879
2156
|
// keeps that position.
|
|
1880
2157
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'nothing') {
|
|
@@ -1891,7 +2168,7 @@ function parseAsk() {
|
|
|
1891
2168
|
return { type: 'NotFoundStatement', body };
|
|
1892
2169
|
}
|
|
1893
2170
|
|
|
1894
|
-
// IOPL-native
|
|
2171
|
+
// IOPL-native - "when \"event\" happens as data … done": event listener.
|
|
1895
2172
|
// Must be checked before the "when someone" path.
|
|
1896
2173
|
if (peek().type === TOKEN.STRING &&
|
|
1897
2174
|
peekAt(1).type === TOKEN.HAPPENS) {
|
|
@@ -1907,7 +2184,7 @@ function parseAsk() {
|
|
|
1907
2184
|
return { type: 'WhenHappensStatement', event: eventStr, paramName, body };
|
|
1908
2185
|
}
|
|
1909
2186
|
|
|
1910
|
-
// v2.1.0
|
|
2187
|
+
// v2.1.0 - socket handlers inside a "websocket server" block come first:
|
|
1911
2188
|
// they read "when socket …", not "when someone …".
|
|
1912
2189
|
const whenToken = peek();
|
|
1913
2190
|
if (whenToken.type === TOKEN.IDENTIFIER && whenToken.value === 'socket') {
|
|
@@ -1936,7 +2213,7 @@ function parseAsk() {
|
|
|
1936
2213
|
));
|
|
1937
2214
|
}
|
|
1938
2215
|
|
|
1939
|
-
// v2.1.0
|
|
2216
|
+
// v2.1.0 - socket handlers inside a "websocket server" block:
|
|
1940
2217
|
// when socket connects … done
|
|
1941
2218
|
// when socket sends message … done
|
|
1942
2219
|
// when socket disconnects … done
|
|
@@ -2127,7 +2404,7 @@ function parseAsk() {
|
|
|
2127
2404
|
return { type: 'ServeFolderStatement', folder };
|
|
2128
2405
|
}
|
|
2129
2406
|
|
|
2130
|
-
// ── v0.6
|
|
2407
|
+
// ── v0.6 - Express DX ──────────────────────────────────────────────────────
|
|
2131
2408
|
|
|
2132
2409
|
// web app
|
|
2133
2410
|
function parseWebApp() {
|
|
@@ -2171,7 +2448,7 @@ function parseAsk() {
|
|
|
2171
2448
|
|
|
2172
2449
|
// bot "<token>" / bot env("BOT_TOKEN")
|
|
2173
2450
|
// Creates the polling Telegram bot and binds it to BOT (v1.2). The token
|
|
2174
|
-
// argument may be any expression
|
|
2451
|
+
// argument may be any expression - the token literal is never required to
|
|
2175
2452
|
// appear in source, and the runtime falls back to TELEGRAM_BOT_TOKEN.
|
|
2176
2453
|
function parseBot() {
|
|
2177
2454
|
advance(); // consume "bot"
|
|
@@ -2180,7 +2457,7 @@ function parseAsk() {
|
|
|
2180
2457
|
}
|
|
2181
2458
|
|
|
2182
2459
|
// start <port> / run on <port>
|
|
2183
|
-
// start telegram bot
|
|
2460
|
+
// start telegram bot - explicit Telegram startup marker. Polling keeps
|
|
2184
2461
|
// the bot alive, so this form is only for documentation/intent.
|
|
2185
2462
|
function parseStart() {
|
|
2186
2463
|
const isRunOn = peek().type === TOKEN.RUN_ON;
|
|
@@ -2205,14 +2482,14 @@ function parseAsk() {
|
|
|
2205
2482
|
return { type: 'StartStatement', port };
|
|
2206
2483
|
}
|
|
2207
2484
|
|
|
2208
|
-
// ── v0.6
|
|
2485
|
+
// ── v0.6 - SQLite DX ───────────────────────────────────────────────────────
|
|
2209
2486
|
|
|
2210
2487
|
// database "<file>" [using "<driver>"] / connect database "<file>" [using "<driver>"] / use database "<file>" [using "<driver>"]
|
|
2211
2488
|
//
|
|
2212
|
-
// v2.1.1
|
|
2489
|
+
// v2.1.1 - the optional driver selects the SQLite engine without changing
|
|
2213
2490
|
// any other PlainScript database semantics:
|
|
2214
|
-
// "native"
|
|
2215
|
-
// "wasm"
|
|
2491
|
+
// "native" - better-sqlite3 (requires a working native binding)
|
|
2492
|
+
// "wasm" - sql.js WebAssembly build (runs anywhere Node runs)
|
|
2216
2493
|
// The default ("auto") tries native first and falls back to the wasm
|
|
2217
2494
|
// engine when the native binding is unavailable on this platform.
|
|
2218
2495
|
function parseDatabase() {
|
|
@@ -2253,7 +2530,7 @@ function parseAsk() {
|
|
|
2253
2530
|
return { type: 'DatabaseStatement', file, driver };
|
|
2254
2531
|
}
|
|
2255
2532
|
|
|
2256
|
-
// v2.1.0
|
|
2533
|
+
// v2.1.0 - postgres "<connection-string>": binds the PostgreSQL pool to
|
|
2257
2534
|
// "db". SQL statements afterwards compile to async pool queries.
|
|
2258
2535
|
function parsePostgres() {
|
|
2259
2536
|
// "postgres" is contextual: only a declaration when followed by a value.
|
|
@@ -2262,7 +2539,7 @@ function parseAsk() {
|
|
|
2262
2539
|
return { type: 'PostgresStatement', connection };
|
|
2263
2540
|
}
|
|
2264
2541
|
|
|
2265
|
-
// v2.2.0
|
|
2542
|
+
// v2.2.0 - mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
|
|
2266
2543
|
// Usage: mongo "mongodb://localhost:27017" db "mydb"
|
|
2267
2544
|
function parseMongo() {
|
|
2268
2545
|
const token = advance(); // mongo or mongodb
|
|
@@ -2277,10 +2554,10 @@ function parseAsk() {
|
|
|
2277
2554
|
|
|
2278
2555
|
// query/insert/update/delete/execute SQL_BODY DONE
|
|
2279
2556
|
//
|
|
2280
|
-
// v2.1.0
|
|
2557
|
+
// v2.1.0 - the raw SQL may reference PlainScript variables with {name}
|
|
2281
2558
|
// placeholders. They are extracted at parse time and replaced: SQLite gets
|
|
2282
2559
|
// anonymous "?" markers, PostgreSQL numbered "$1…" markers (the generator
|
|
2283
|
-
// decides). Values are always passed as bound parameters
|
|
2560
|
+
// decides). Values are always passed as bound parameters - never spliced
|
|
2284
2561
|
// into the SQL text.
|
|
2285
2562
|
function parseSqlBlock(keyword, nodeType) {
|
|
2286
2563
|
advance(); // consume the keyword token (QUERY_KW, INSERT_KW, etc.)
|
|
@@ -2296,7 +2573,7 @@ function parseAsk() {
|
|
|
2296
2573
|
return { type: nodeType, ...extractSqlParams(rawSql) };
|
|
2297
2574
|
}
|
|
2298
2575
|
|
|
2299
|
-
// ── v2.1.1
|
|
2576
|
+
// ── v2.1.1 - error handling, retries and backend middleware ───────────────
|
|
2300
2577
|
|
|
2301
2578
|
function isHandledBy() {
|
|
2302
2579
|
return peek().type === TOKEN.IDENTIFIER && peek().value === 'handled' &&
|
|
@@ -2364,7 +2641,10 @@ function parseAsk() {
|
|
|
2364
2641
|
const stmt = parseStatement();
|
|
2365
2642
|
if (stmt) recoverBody.push(stmt);
|
|
2366
2643
|
}
|
|
2367
|
-
|
|
2644
|
+
// A recover body may be followed directly by another recover block or
|
|
2645
|
+
// by finally. Only consume a real "done" terminator here; consuming
|
|
2646
|
+
// "finally" would silently discard cleanup code.
|
|
2647
|
+
if (peek().type === TOKEN.DONE) advance();
|
|
2368
2648
|
catches.push({ param: paramName, errorType, body: recoverBody });
|
|
2369
2649
|
}
|
|
2370
2650
|
let finallyBody = null;
|
|
@@ -2383,7 +2663,7 @@ function parseAsk() {
|
|
|
2383
2663
|
}
|
|
2384
2664
|
advance(); // consume "done" that closes the finally block
|
|
2385
2665
|
}
|
|
2386
|
-
// No final "done" advance here
|
|
2666
|
+
// No final "done" advance here - the enclosing parseBody() consumes it
|
|
2387
2667
|
return { type: 'TryStatement', body: tryBody, catches, finallyBody };
|
|
2388
2668
|
}
|
|
2389
2669
|
|
|
@@ -2457,7 +2737,7 @@ function parseAsk() {
|
|
|
2457
2737
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'allow' && mimes === null) {
|
|
2458
2738
|
advance(); // allow
|
|
2459
2739
|
let elements;
|
|
2460
|
-
// v2.4
|
|
2740
|
+
// v2.4 - accept "list with" as alternative to bracket syntax
|
|
2461
2741
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'list' &&
|
|
2462
2742
|
peekAt(1).type === TOKEN.WITH) {
|
|
2463
2743
|
advance(); // "list"
|
|
@@ -2712,7 +2992,7 @@ function parseAsk() {
|
|
|
2712
2992
|
|
|
2713
2993
|
// ── Expressions ────────────────────────────────────────────────────────────
|
|
2714
2994
|
|
|
2715
|
-
// v2.1.1
|
|
2995
|
+
// v2.1.1 - full arithmetic precedence:
|
|
2716
2996
|
// nullish := additive ("??" additive)*
|
|
2717
2997
|
// additive := term (("+" | "-") term)*
|
|
2718
2998
|
// multiplicative ("term") := power (("*" | "/" | "%") power)*
|
|
@@ -2729,10 +3009,54 @@ function parseAsk() {
|
|
|
2729
3009
|
const alternate = parseExpression();
|
|
2730
3010
|
return { type: 'ConditionalExpression', condition, consequent, alternate };
|
|
2731
3011
|
}
|
|
2732
|
-
|
|
3012
|
+
return parseBooleanOrExpression();
|
|
3013
|
+
}
|
|
3014
|
+
|
|
3015
|
+
// Expression-level boolean algebra: boolean operators usable in ordinary
|
|
3016
|
+
// value positions (e.g. `(x is above 3) and (x is below 10)` as a value).
|
|
3017
|
+
// Produces the SAME LogicalCondition nodes the condition level builds, so no
|
|
3018
|
+
// separate boolean system exists - the existing condition generator emits
|
|
3019
|
+
// them, and comparisons fold as expression atoms via parseBooleanAtom.
|
|
3020
|
+
// booleanOr := booleanAnd ("or" booleanAnd)*
|
|
3021
|
+
// booleanAnd := booleanNot ("and" booleanNot)*
|
|
3022
|
+
// booleanNot := "not" booleanNot | booleanAtom
|
|
3023
|
+
function parseBooleanOrExpression() {
|
|
3024
|
+
let left = parseBooleanAndExpression();
|
|
3025
|
+
while (peek().type === TOKEN.OR) {
|
|
3026
|
+
advance();
|
|
3027
|
+
const right = parseBooleanAndExpression();
|
|
3028
|
+
left = { type: 'LogicalCondition', op: 'or', left, right };
|
|
3029
|
+
}
|
|
3030
|
+
return left;
|
|
3031
|
+
}
|
|
3032
|
+
|
|
3033
|
+
function parseBooleanAndExpression() {
|
|
3034
|
+
let left = parseBooleanNotExpression();
|
|
3035
|
+
while (peek().type === TOKEN.AND) {
|
|
3036
|
+
advance();
|
|
3037
|
+
const right = parseBooleanNotExpression();
|
|
3038
|
+
left = { type: 'LogicalCondition', op: 'and', left, right };
|
|
3039
|
+
}
|
|
2733
3040
|
return left;
|
|
2734
3041
|
}
|
|
2735
3042
|
|
|
3043
|
+
function parseBooleanNotExpression() {
|
|
3044
|
+
if (peek().type === TOKEN.NOT) {
|
|
3045
|
+
advance();
|
|
3046
|
+
return { type: 'LogicalCondition', op: 'not', operand: parseBooleanNotExpression() };
|
|
3047
|
+
}
|
|
3048
|
+
return parseBooleanAtom();
|
|
3049
|
+
}
|
|
3050
|
+
|
|
3051
|
+
// One boolean operand: a plain value with an optional comparison operator.
|
|
3052
|
+
// A comparison's OWN operands must stay at this level (never consuming
|
|
3053
|
+
// "and"/"or"), so the combinators above can see the "and"/"or" that joins
|
|
3054
|
+
// two comparison atoms - e.g. "x is above 3 and y is below 5".
|
|
3055
|
+
function parseBooleanAtom() {
|
|
3056
|
+
const left = parseNullish();
|
|
3057
|
+
return tryParseComparisonOperator(left, false);
|
|
3058
|
+
}
|
|
3059
|
+
|
|
2736
3060
|
function parseNullish() {
|
|
2737
3061
|
let left = parseAdditive();
|
|
2738
3062
|
while (peek().type === TOKEN.NULLISH_COALESCE) {
|
|
@@ -2763,7 +3087,7 @@ function parseAsk() {
|
|
|
2763
3087
|
peek().type === TOKEN.STAR ||
|
|
2764
3088
|
peek().type === TOKEN.SLASH ||
|
|
2765
3089
|
peek().type === TOKEN.PERCENT ||
|
|
2766
|
-
peek().type === TOKEN.TIMES_WORD ||
|
|
3090
|
+
(!_inRepeatCount && peek().type === TOKEN.TIMES_WORD) ||
|
|
2767
3091
|
peek().type === TOKEN.DIVIDED_BY_WORD
|
|
2768
3092
|
) {
|
|
2769
3093
|
const opToken = advance();
|
|
@@ -2802,7 +3126,7 @@ function parseAsk() {
|
|
|
2802
3126
|
advance();
|
|
2803
3127
|
return { type: 'AwaitExpression', value: parseUnary() };
|
|
2804
3128
|
}
|
|
2805
|
-
// v2.1.1
|
|
3129
|
+
// v2.1.1 - await semantics: "wait for <value>" awaits an async operation.
|
|
2806
3130
|
// The operand binds tightly (a full postfix chain), so
|
|
2807
3131
|
// "wait for loadUser(id) + 1" means "(await loadUser(id)) + 1".
|
|
2808
3132
|
// ("for" lexes as the FOR keyword, not an identifier.)
|
|
@@ -2830,9 +3154,77 @@ function parseAsk() {
|
|
|
2830
3154
|
return parsePrimary();
|
|
2831
3155
|
}
|
|
2832
3156
|
|
|
3157
|
+
// Speculatively check whether the current token stream matches an arrow
|
|
3158
|
+
// function pattern: "(" IDENTIFIER ("," IDENTIFIER)* ")" "->" ...
|
|
3159
|
+
// (or the block-bodied form "(" IDENTIFIER ("," IDENTIFIER)* ")" "do" ... "done").
|
|
3160
|
+
// Returns true when the pattern is detected WITHOUT consuming any tokens.
|
|
3161
|
+
function isArrowFunctionPattern() {
|
|
3162
|
+
if (peek().type !== TOKEN.LPAREN) return false;
|
|
3163
|
+
let j = pos + 1;
|
|
3164
|
+
// Empty params: () -> ... / () do ...
|
|
3165
|
+
if (tokens[j] && tokens[j].type === TOKEN.RPAREN) {
|
|
3166
|
+
if (tokens[j + 1] && (tokens[j + 1].type === TOKEN.ARROW || tokens[j + 1].type === TOKEN.DO)) return true;
|
|
3167
|
+
return false;
|
|
3168
|
+
}
|
|
3169
|
+
// At least one IDENTIFIER param
|
|
3170
|
+
if (!tokens[j] || tokens[j].type !== TOKEN.IDENTIFIER) return false;
|
|
3171
|
+
j++;
|
|
3172
|
+
// Skip comma-separated identifiers
|
|
3173
|
+
while (tokens[j] && tokens[j].type === TOKEN.COMMA) {
|
|
3174
|
+
j++;
|
|
3175
|
+
if (!tokens[j] || tokens[j].type !== TOKEN.IDENTIFIER) return false;
|
|
3176
|
+
j++;
|
|
3177
|
+
}
|
|
3178
|
+
// Must be followed by ) and then -> or do
|
|
3179
|
+
if (!tokens[j] || tokens[j].type !== TOKEN.RPAREN) return false;
|
|
3180
|
+
if (!tokens[j + 1] || (tokens[j + 1].type !== TOKEN.ARROW && tokens[j + 1].type !== TOKEN.DO)) return false;
|
|
3181
|
+
return true;
|
|
3182
|
+
}
|
|
3183
|
+
|
|
3184
|
+
// Parse an arrow function expression: (params) -> body (single-expression)
|
|
3185
|
+
// or its block-bodied form (params) do ... done (multi-statement; distinct
|
|
3186
|
+
// marker "do" so the plain arrow stays single-expression).
|
|
3187
|
+
function parseArrowFunction() {
|
|
3188
|
+
consume(TOKEN.LPAREN);
|
|
3189
|
+
const params = [];
|
|
3190
|
+
if (peek().type !== TOKEN.RPAREN) {
|
|
3191
|
+
params.push(consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value);
|
|
3192
|
+
while (peek().type === TOKEN.COMMA) {
|
|
3193
|
+
advance();
|
|
3194
|
+
params.push(consume(TOKEN.IDENTIFIER, 'Expected a parameter name after ",".').value);
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
consume(TOKEN.RPAREN, 'Expected ")" to close the parameter list.');
|
|
3198
|
+
if (peek().type === TOKEN.DO) {
|
|
3199
|
+
advance(); // do
|
|
3200
|
+
// A block body is a genuine function body: every normal statement is
|
|
3201
|
+
// valid, `done` closes it, and generation reuses the exact same body
|
|
3202
|
+
// emitter as `make` functions (so closures, returns, loops, conditionals,
|
|
3203
|
+
// and async/await behave identically).
|
|
3204
|
+
const body = parseBody('lambda');
|
|
3205
|
+
return { type: 'ArrowFunctionExpression', params, body, block: true };
|
|
3206
|
+
}
|
|
3207
|
+
consume(TOKEN.ARROW, 'Expected "->" after the parameter list.');
|
|
3208
|
+
const body = parseArrowFunctionBody();
|
|
3209
|
+
return { type: 'ArrowFunctionExpression', params, body };
|
|
3210
|
+
}
|
|
3211
|
+
|
|
3212
|
+
// Parse the body of an arrow function. Supports chained arrow functions:
|
|
3213
|
+
// (x) -> (y) -> x + y
|
|
3214
|
+
function parseArrowFunctionBody() {
|
|
3215
|
+
if (peek().type === TOKEN.LPAREN && isArrowFunctionPattern()) {
|
|
3216
|
+
return parseArrowFunction();
|
|
3217
|
+
}
|
|
3218
|
+
return parseExpression();
|
|
3219
|
+
}
|
|
3220
|
+
|
|
2833
3221
|
// primary → itemExpr | atom (postfix)*
|
|
2834
3222
|
function parsePrimary() {
|
|
2835
|
-
//
|
|
3223
|
+
// Arrow function: (params) -> body - detected before grouped expression
|
|
3224
|
+
if (peek().type === TOKEN.LPAREN && isArrowFunctionPattern()) {
|
|
3225
|
+
return parseArrowFunction();
|
|
3226
|
+
}
|
|
3227
|
+
// v2.4 - "record with" and "list with" as expressions (contextual)
|
|
2836
3228
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'record' &&
|
|
2837
3229
|
peekAt(1).type === TOKEN.WITH) {
|
|
2838
3230
|
return parseRecordWith(true);
|
|
@@ -2873,7 +3265,7 @@ function parseAsk() {
|
|
|
2873
3265
|
advance(); // of
|
|
2874
3266
|
const left = parsePrimary();
|
|
2875
3267
|
if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) advance();
|
|
2876
|
-
const right =
|
|
3268
|
+
const right = parseBooleanAtom();
|
|
2877
3269
|
return { type: 'SetAlgebraExpression', op, left, right };
|
|
2878
3270
|
}
|
|
2879
3271
|
|
|
@@ -2887,7 +3279,7 @@ function parseAsk() {
|
|
|
2887
3279
|
return { type: 'CollectionReflectExpression', accessor, target };
|
|
2888
3280
|
}
|
|
2889
3281
|
|
|
2890
|
-
// v1.0.1
|
|
3282
|
+
// v1.0.1 - record constructor: `create a Person with name "Ada" and age 17`.
|
|
2891
3283
|
// `create` then an article ("a"/"an") then a kind name then "with" pairs.
|
|
2892
3284
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'create') {
|
|
2893
3285
|
const a1 = peekAt(1);
|
|
@@ -2901,7 +3293,7 @@ function parseAsk() {
|
|
|
2901
3293
|
}
|
|
2902
3294
|
}
|
|
2903
3295
|
|
|
2904
|
-
// v1.0.1
|
|
3296
|
+
// v1.0.1 - concurrency combinators: `all of [a(), b()]`, `any of [...]`,
|
|
2905
3297
|
// `settled of [...]`. Guard on the identifier + "of" lookahead so plain
|
|
2906
3298
|
// property reads like `all of the_list` still work as well as prose.
|
|
2907
3299
|
if (peek().type === TOKEN.IDENTIFIER &&
|
|
@@ -2913,7 +3305,7 @@ function parseAsk() {
|
|
|
2913
3305
|
return { type: 'ConcurrencyExpression', combo: comboKw, items };
|
|
2914
3306
|
}
|
|
2915
3307
|
|
|
2916
|
-
// v1.0.1
|
|
3308
|
+
// v1.0.1 - `spread of <collection>` unfolds an iterable into a new array.
|
|
2917
3309
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'spread' &&
|
|
2918
3310
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
2919
3311
|
advance(); // spread
|
|
@@ -2922,9 +3314,29 @@ function parseAsk() {
|
|
|
2922
3314
|
return { type: 'SpreadExpression', collection };
|
|
2923
3315
|
}
|
|
2924
3316
|
|
|
3317
|
+
// `count of <collection>` returns collection count/length
|
|
3318
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'count' &&
|
|
3319
|
+
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
3320
|
+
advance(); // count
|
|
3321
|
+
advance(); // of
|
|
3322
|
+
const collection = parsePrimary();
|
|
3323
|
+
return { type: 'CountOfExpression', object: collection };
|
|
3324
|
+
}
|
|
3325
|
+
|
|
2925
3326
|
const item = tryParseItemExpression();
|
|
2926
3327
|
if (item) return item;
|
|
2927
|
-
|
|
3328
|
+
// v1.0.361 - `new` works anywhere an expression is parsed (assignment
|
|
3329
|
+
// targets, call arguments, member chains). "new" is not a keyword, so this
|
|
3330
|
+
// identifier must be intercepted here; parseAtom would read it as a plain
|
|
3331
|
+
// Identifier and turn "new THREE.Scene()" into "new.THREE.Scene()".
|
|
3332
|
+
let node;
|
|
3333
|
+
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'new' &&
|
|
3334
|
+
peekAt(1).type !== TOKEN.BECOMES && peekAt(1).type !== TOKEN.LPAREN) {
|
|
3335
|
+
advance(); // new
|
|
3336
|
+
node = parseNewExpressionCore();
|
|
3337
|
+
} else {
|
|
3338
|
+
node = parseAtom();
|
|
3339
|
+
}
|
|
2928
3340
|
while (true) {
|
|
2929
3341
|
if (peek().type === TOKEN.LBRACKET) {
|
|
2930
3342
|
advance();
|
|
@@ -2933,7 +3345,7 @@ function parseAsk() {
|
|
|
2933
3345
|
node = { type: 'IndexExpression', object: node, index };
|
|
2934
3346
|
} else if (peek().type === TOKEN.DOT) {
|
|
2935
3347
|
advance();
|
|
2936
|
-
// After ".", any word is a valid JS property/method name
|
|
3348
|
+
// After ".", any word is a valid JS property/method name - including
|
|
2937
3349
|
// words that are PlainScript keywords in other contexts (e.g. crypto
|
|
2938
3350
|
// .update(...), obj .delete()). So accept any word token, not just an
|
|
2939
3351
|
// identifier.
|
|
@@ -2999,6 +3411,21 @@ function parseAsk() {
|
|
|
2999
3411
|
advance(); // position
|
|
3000
3412
|
const idx = consume(TOKEN.NUMBER, 'Expected a number after "at position".').value;
|
|
3001
3413
|
node = { type: 'IndexExpression', object: node, index: { type: 'NumberLiteral', value: idx } };
|
|
3414
|
+
} else if (peek().type === TOKEN.LPAREN) {
|
|
3415
|
+
// Postfix call: invoke any expression - f()(), arr[0](1), mul(6)(7),
|
|
3416
|
+
// obj.method()(x), or an immediately-invoked lambda ((x) -> x + 1)(2).
|
|
3417
|
+
// Only continuation tokens are considered, so the call binds to the
|
|
3418
|
+
// chain (same rules as the existing "." / "[" cases below it).
|
|
3419
|
+
advance(); // (
|
|
3420
|
+
const { separator, args } = parseArgList();
|
|
3421
|
+
consume(TOKEN.RPAREN, 'Expected ")" to close the call.');
|
|
3422
|
+
if (separator) {
|
|
3423
|
+
throw new Error(makeError(
|
|
3424
|
+
'Postfix calls cannot use "to"/"from" arguments.\n\nExample:\n mul(6)(7)',
|
|
3425
|
+
peek()
|
|
3426
|
+
));
|
|
3427
|
+
}
|
|
3428
|
+
node = { type: 'CallExpression', callee: node, args };
|
|
3002
3429
|
} else {
|
|
3003
3430
|
break;
|
|
3004
3431
|
}
|
|
@@ -3006,7 +3433,7 @@ function parseAsk() {
|
|
|
3006
3433
|
return node;
|
|
3007
3434
|
}
|
|
3008
3435
|
|
|
3009
|
-
// v1.1
|
|
3436
|
+
// v1.1 - Item expressions, detected before parseAtom:
|
|
3010
3437
|
// first player from players → players[0]
|
|
3011
3438
|
// last player from players → players[players.length - 1]
|
|
3012
3439
|
// player one from players → players[0]
|
|
@@ -3019,10 +3446,18 @@ function parseAsk() {
|
|
|
3019
3446
|
if (second.type !== TOKEN.IDENTIFIER) return null;
|
|
3020
3447
|
if (third.type !== TOKEN.IDENTIFIER) return null;
|
|
3021
3448
|
|
|
3022
|
-
// v2.4
|
|
3449
|
+
// v2.4 - skip "different from" which is a comparison, not an item expression
|
|
3023
3450
|
if (second.value === 'different' && third.value === 'from') return null;
|
|
3024
3451
|
|
|
3025
|
-
// "first
|
|
3452
|
+
// Natural intent style: "first of <collection>" / "last of <collection>"
|
|
3453
|
+
if ((first.value === 'first' || first.value === 'last') && second.value === 'of') {
|
|
3454
|
+
advance(); // first / last
|
|
3455
|
+
advance(); // of
|
|
3456
|
+
const collection = parsePrimary();
|
|
3457
|
+
return { type: first.value === 'first' ? 'FirstItem' : 'LastItem', collection };
|
|
3458
|
+
}
|
|
3459
|
+
|
|
3460
|
+
// "first from players" / "last from players" - missing noun
|
|
3026
3461
|
if ((first.value === 'first' || first.value === 'last') && second.value === 'from') {
|
|
3027
3462
|
throw new Error(makeError(
|
|
3028
3463
|
`Expected a noun after "${first.value}".\n\nExample:\n ${first.value} player from players`,
|
|
@@ -3030,12 +3465,12 @@ function parseAsk() {
|
|
|
3030
3465
|
));
|
|
3031
3466
|
}
|
|
3032
3467
|
|
|
3033
|
-
if (third.value !== 'from') return null;
|
|
3468
|
+
if (third.value !== 'from' && third.value !== 'of') return null;
|
|
3034
3469
|
|
|
3035
3470
|
if (first.value === 'first') {
|
|
3036
3471
|
advance(); // first
|
|
3037
|
-
advance(); // noun
|
|
3038
|
-
advance(); // from
|
|
3472
|
+
advance(); // noun (e.g. item, player)
|
|
3473
|
+
advance(); // from / of
|
|
3039
3474
|
const collection = parsePrimary();
|
|
3040
3475
|
return { type: 'FirstItem', collection };
|
|
3041
3476
|
}
|
|
@@ -3043,7 +3478,7 @@ function parseAsk() {
|
|
|
3043
3478
|
if (first.value === 'last') {
|
|
3044
3479
|
advance(); // last
|
|
3045
3480
|
advance(); // noun
|
|
3046
|
-
advance(); // from
|
|
3481
|
+
advance(); // from / of
|
|
3047
3482
|
const collection = parsePrimary();
|
|
3048
3483
|
return { type: 'LastItem', collection };
|
|
3049
3484
|
}
|
|
@@ -3072,7 +3507,7 @@ function parseAsk() {
|
|
|
3072
3507
|
if (token.type === TOKEN.TEMPLATE_STRING) { advance(); return { type: 'TemplateLiteral', value: token.value }; }
|
|
3073
3508
|
if (token.type === TOKEN.NUMBER) { advance(); return { type: 'NumberLiteral', value: token.value }; }
|
|
3074
3509
|
if (token.type === TOKEN.BIGINT) { advance(); return { type: 'BigIntLiteral', value: token.value }; }
|
|
3075
|
-
// v2.1.1
|
|
3510
|
+
// v2.1.1 - boolean and null literals
|
|
3076
3511
|
if (token.type === TOKEN.TRUE_KW) { advance(); return { type: 'BooleanLiteral', value: true }; }
|
|
3077
3512
|
if (token.type === TOKEN.FALSE_KW) { advance(); return { type: 'BooleanLiteral', value: false }; }
|
|
3078
3513
|
if (token.type === TOKEN.NULL_KW) { advance(); return { type: 'NullLiteral' }; }
|
|
@@ -3095,7 +3530,7 @@ function parseAsk() {
|
|
|
3095
3530
|
// Bare symbol keyword - treat as Symbol() with no args
|
|
3096
3531
|
return { type: 'CallExpression', name: 'Symbol', args: [] };
|
|
3097
3532
|
}
|
|
3098
|
-
// v2.1.1
|
|
3533
|
+
// v2.1.1 - parenthesised grouping: (a + b) * c
|
|
3099
3534
|
if (token.type === TOKEN.LPAREN) {
|
|
3100
3535
|
advance();
|
|
3101
3536
|
const inner = parseExpression();
|
|
@@ -3105,7 +3540,7 @@ function parseAsk() {
|
|
|
3105
3540
|
if (token.type === TOKEN.LBRACKET) { return parseArrayLiteral(); }
|
|
3106
3541
|
if (token.type === TOKEN.LBRACE) { return parseInlineObjectLiteral(); }
|
|
3107
3542
|
|
|
3108
|
-
// v2.1.1
|
|
3543
|
+
// v2.1.1 - HTTP client prefix form:
|
|
3109
3544
|
// get "<url>" post urlExpr with <body>
|
|
3110
3545
|
// put/patch/delete … optional "headers { … }" and "timeout <ms>" clauses
|
|
3111
3546
|
// The call form get(...) stays an ordinary user/builtin call.
|
|
@@ -3160,7 +3595,7 @@ function parseAsk() {
|
|
|
3160
3595
|
return { type: 'Identifier', name: token.value };
|
|
3161
3596
|
}
|
|
3162
3597
|
|
|
3163
|
-
// v2.1.0
|
|
3598
|
+
// v2.1.0 - query("field") as a value: the HTTP query-string accessor.
|
|
3164
3599
|
// The SQLite block form only applies when "query" stands alone.
|
|
3165
3600
|
if (token.type === TOKEN.QUERY_KW && peekAt(1).type === TOKEN.LPAREN) {
|
|
3166
3601
|
return parseCallExpression();
|
|
@@ -3172,7 +3607,7 @@ function parseAsk() {
|
|
|
3172
3607
|
));
|
|
3173
3608
|
}
|
|
3174
3609
|
|
|
3175
|
-
// v2.1.1
|
|
3610
|
+
// v2.1.1 - "get", "post", "put", "patch" lex as identifiers; "delete" lexes
|
|
3176
3611
|
// as the SQL keyword token. All five introduce an HTTP request when followed
|
|
3177
3612
|
// by a URL value instead of "(".
|
|
3178
3613
|
function httpMethodWord(token) {
|
|
@@ -3191,7 +3626,7 @@ function parseAsk() {
|
|
|
3191
3626
|
].includes(token.type);
|
|
3192
3627
|
}
|
|
3193
3628
|
|
|
3194
|
-
// v2.1.1
|
|
3629
|
+
// v2.1.1 - parse one HTTP request expression after its method word.
|
|
3195
3630
|
//
|
|
3196
3631
|
// get <url> [headers <object>] [timeout <ms>]
|
|
3197
3632
|
// post|put|patch|delete <url> [with <body>] [headers <object>] [timeout <ms>]
|
|
@@ -3253,7 +3688,7 @@ function parseAsk() {
|
|
|
3253
3688
|
return { type: 'ArrayLiteral', elements };
|
|
3254
3689
|
}
|
|
3255
3690
|
|
|
3256
|
-
// v1.2
|
|
3691
|
+
// v1.2 - Inline object literal: { key: value, ... }
|
|
3257
3692
|
// If `isPattern` is true, allows shorthand { x, y } for destructuring
|
|
3258
3693
|
function parseInlineObjectLiteral(isPattern) {
|
|
3259
3694
|
consume(TOKEN.LBRACE);
|
|
@@ -3328,7 +3763,7 @@ function parseAsk() {
|
|
|
3328
3763
|
return { type: 'ObjectLiteral', properties };
|
|
3329
3764
|
}
|
|
3330
3765
|
|
|
3331
|
-
// v2.1.0
|
|
3766
|
+
// v2.1.0 - "key is value" pairs until "done", shared by the mail
|
|
3332
3767
|
// statements. Returns [{ key, value }] and consumes the closing DONE.
|
|
3333
3768
|
function parsePropertyList(contextName) {
|
|
3334
3769
|
const properties = [];
|
|
@@ -3339,7 +3774,7 @@ function parseAsk() {
|
|
|
3339
3774
|
peek()
|
|
3340
3775
|
));
|
|
3341
3776
|
}
|
|
3342
|
-
// v2.4
|
|
3777
|
+
// v2.4 - accept keyword tokens as property names (e.g., "to" in mail transport)
|
|
3343
3778
|
const keyToken = peek();
|
|
3344
3779
|
if (keyToken.type === TOKEN.IDENTIFIER || keyToken.type === TOKEN.TO ||
|
|
3345
3780
|
keyToken.type === TOKEN.FROM || keyToken.type === TOKEN.AND ||
|
|
@@ -3360,7 +3795,7 @@ function parseAsk() {
|
|
|
3360
3795
|
return properties;
|
|
3361
3796
|
}
|
|
3362
3797
|
|
|
3363
|
-
// v2.1.0
|
|
3798
|
+
// v2.1.0 - every <n> <unit>s … done: repeat work on an interval.
|
|
3364
3799
|
// The unit is resolved to milliseconds at parse time; the generator emits
|
|
3365
3800
|
// a plain setInterval with count * unit.
|
|
3366
3801
|
function parseEvery() {
|
|
@@ -3372,7 +3807,7 @@ function parseAsk() {
|
|
|
3372
3807
|
}
|
|
3373
3808
|
if (unitToken.type !== TOKEN.IDENTIFIER || !TIME_UNITS[unitToken.value]) {
|
|
3374
3809
|
throw new Error(makeError(
|
|
3375
|
-
'Expected a time unit after the number in "every".\n\nUnits: seconds, minutes, hours, days\n\nExample:\n every 5 minutes',
|
|
3810
|
+
'Expected a time unit after the number in "every".\n\nUnits: milliseconds, seconds, minutes, hours, days\n\nExample:\n every 5 minutes',
|
|
3376
3811
|
unitToken
|
|
3377
3812
|
));
|
|
3378
3813
|
}
|
|
@@ -3381,7 +3816,37 @@ function parseAsk() {
|
|
|
3381
3816
|
return { type: 'EveryStatement', count, unit, body };
|
|
3382
3817
|
}
|
|
3383
3818
|
|
|
3384
|
-
//
|
|
3819
|
+
// v1.0.361 - every frame … done: a requestAnimationFrame loop whose body runs
|
|
3820
|
+
// once per animation frame. The next frame is scheduled after the body so it
|
|
3821
|
+
// always runs (t is the frame timestamp, like the DOM's rAF callback).
|
|
3822
|
+
function parseEveryFrame() {
|
|
3823
|
+
advance(); // every/each
|
|
3824
|
+
advance(); // frame
|
|
3825
|
+
const body = parseBody('"every frame" block');
|
|
3826
|
+
return { type: 'EveryFrameStatement', body };
|
|
3827
|
+
}
|
|
3828
|
+
|
|
3829
|
+
// v1.0.361 - after <n> <unit>s … done: run work once after a delay.
|
|
3830
|
+
// after 5 seconds
|
|
3831
|
+
// show "butter!" ... done
|
|
3832
|
+
// The delay is a number expression ("after count seconds"); the unit is
|
|
3833
|
+
// consumed from TIME_UNITS so the generator can fold <n>*<unit> directly.
|
|
3834
|
+
function parseAfter() {
|
|
3835
|
+
advance(); // after
|
|
3836
|
+
const delay = parseExpression();
|
|
3837
|
+
const unitToken = peek();
|
|
3838
|
+
if (unitToken.type !== TOKEN.IDENTIFIER || !TIME_UNITS[unitToken.value]) {
|
|
3839
|
+
throw new Error(makeError(
|
|
3840
|
+
'Expected a time unit after the delay in "after".\n\nUnits: milliseconds, seconds, minutes, hours, days\n\nExample:\n after 5 seconds\n show "later"\n done',
|
|
3841
|
+
unitToken
|
|
3842
|
+
));
|
|
3843
|
+
}
|
|
3844
|
+
const unit = TIME_UNITS[advance().value];
|
|
3845
|
+
const body = parseBody('"after" block');
|
|
3846
|
+
return { type: 'AfterStatement', delay, unit, body };
|
|
3847
|
+
}
|
|
3848
|
+
|
|
3849
|
+
// v2.1.0 - websocket server on <port> … done
|
|
3385
3850
|
//
|
|
3386
3851
|
// websocket server on 8081
|
|
3387
3852
|
// when socket connects … done
|
|
@@ -3432,12 +3897,12 @@ function parseAsk() {
|
|
|
3432
3897
|
return { type: 'WebSocketServerStatement', port, connectBody, messageBody, disconnectBody };
|
|
3433
3898
|
}
|
|
3434
3899
|
|
|
3435
|
-
// v2.1.1
|
|
3900
|
+
// v2.1.1 - whatsapp bot … done
|
|
3436
3901
|
//
|
|
3437
3902
|
// whatsapp bot
|
|
3438
3903
|
// auth "session" (optional; session folder name)
|
|
3439
|
-
// login qr
|
|
3440
|
-
// login pairing "2348012345678" (literal)
|
|
3904
|
+
// login qr - or -
|
|
3905
|
+
// login pairing "2348012345678" (literal) - or -
|
|
3441
3906
|
// login pairing phone (any value, e.g. from ask)
|
|
3442
3907
|
//
|
|
3443
3908
|
// on message
|
|
@@ -3468,9 +3933,9 @@ function parseAsk() {
|
|
|
3468
3933
|
));
|
|
3469
3934
|
}
|
|
3470
3935
|
|
|
3471
|
-
// use baileys "<pkg>"
|
|
3472
|
-
// Default is @
|
|
3473
|
-
// so developers can pin a
|
|
3936
|
+
// use baileys "<pkg>" - override the Baileys implementation package.
|
|
3937
|
+
// Default is @whiskeysockets/baileys. Accepts any require-able package
|
|
3938
|
+
// name, so developers can pin a local adapter or another compatible fork.
|
|
3474
3939
|
if (peek().type === TOKEN.USE && peekAt(1).value === 'baileys' && peekAt(2).type === TOKEN.STRING) {
|
|
3475
3940
|
advance(); // use
|
|
3476
3941
|
advance(); // baileys
|
|
@@ -3478,7 +3943,7 @@ function parseAsk() {
|
|
|
3478
3943
|
continue;
|
|
3479
3944
|
}
|
|
3480
3945
|
|
|
3481
|
-
// auth "<folder>"
|
|
3946
|
+
// auth "<folder>" - where WhatsApp session credentials persist.
|
|
3482
3947
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'auth' &&
|
|
3483
3948
|
peekAt(1).type === TOKEN.STRING) {
|
|
3484
3949
|
advance(); // auth
|
|
@@ -3497,7 +3962,7 @@ function parseAsk() {
|
|
|
3497
3962
|
}
|
|
3498
3963
|
if (modeToken.type === TOKEN.IDENTIFIER && modeToken.value === 'pairing') {
|
|
3499
3964
|
advance(); // pairing
|
|
3500
|
-
// v2.1.2
|
|
3965
|
+
// v2.1.2 - the phone may be a string literal (validated here at
|
|
3501
3966
|
// compile time) or any PlainScript value, typically a variable filled by
|
|
3502
3967
|
// `ask`:
|
|
3503
3968
|
//
|
|
@@ -3522,7 +3987,7 @@ function parseAsk() {
|
|
|
3522
3987
|
));
|
|
3523
3988
|
}
|
|
3524
3989
|
|
|
3525
|
-
// on message … done
|
|
3990
|
+
// on message … done - the message handler.
|
|
3526
3991
|
if (peek().type === TOKEN.ON && peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'message') {
|
|
3527
3992
|
advance(); // on
|
|
3528
3993
|
advance(); // message
|
|
@@ -3532,7 +3997,7 @@ function parseAsk() {
|
|
|
3532
3997
|
}
|
|
3533
3998
|
|
|
3534
3999
|
throw new Error(makeError(
|
|
3535
|
-
'A "whatsapp bot" block may only contain an "auth", a "login", a "use baileys", and "on message" statements.\n\nExample:\n whatsapp bot\n auth "session"\n use baileys "@
|
|
4000
|
+
'A "whatsapp bot" block may only contain an "auth", a "login", a "use baileys", and "on message" statements.\n\nExample:\n whatsapp bot\n auth "session"\n use baileys "@whiskeysockets/baileys"\n login qr\n\n on message\n log message\n done\n done',
|
|
3536
4001
|
peek()
|
|
3537
4002
|
));
|
|
3538
4003
|
}
|
|
@@ -3555,7 +4020,7 @@ function parseAsk() {
|
|
|
3555
4020
|
const cleaned = String(raw).replace(/[\s()+\-\.]/g, '');
|
|
3556
4021
|
if (!/^[0-9]+$/.test(cleaned)) {
|
|
3557
4022
|
throw new Error(makeError(
|
|
3558
|
-
`"${raw}" is not a valid phone number for "login pairing".\n\nUse the full international number, digits only
|
|
4023
|
+
`"${raw}" is not a valid phone number for "login pairing".\n\nUse the full international number, digits only - country code first, no "+" and no spaces:\n\nExample:\n login pairing "2348012345678"`,
|
|
3559
4024
|
token
|
|
3560
4025
|
));
|
|
3561
4026
|
}
|
|
@@ -3811,7 +4276,7 @@ function parseAsk() {
|
|
|
3811
4276
|
}
|
|
3812
4277
|
args.push(parseExpression());
|
|
3813
4278
|
}
|
|
3814
|
-
// v1.1
|
|
4279
|
+
// v1.1 - collection expressions: add(item to list) / remove(item from list) / write(data to "file")
|
|
3815
4280
|
if (args.length === 1 && (
|
|
3816
4281
|
(peek().type === TOKEN.IDENTIFIER && (peek().value === 'to' || peek().value === 'from')) ||
|
|
3817
4282
|
peek().type === TOKEN.TO)) {
|
|
@@ -3825,7 +4290,7 @@ function parseAsk() {
|
|
|
3825
4290
|
return { separator, args };
|
|
3826
4291
|
}
|
|
3827
4292
|
|
|
3828
|
-
// v1.1
|
|
4293
|
+
// v1.1 - Build a collection or I/O expression from a "to"/"from" special form.
|
|
3829
4294
|
function buildSpecialCall(name, separator, args, nameToken) {
|
|
3830
4295
|
if (separator === 'to' && name === 'add') {
|
|
3831
4296
|
return { type: 'AddCall', value: args[0], collection: args[1] };
|
|
@@ -3917,17 +4382,55 @@ function parseAsk() {
|
|
|
3917
4382
|
return { type: 'ClassDeclaration', name, superClass, body };
|
|
3918
4383
|
}
|
|
3919
4384
|
|
|
3920
|
-
// new ClassName(args)
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
4385
|
+
// new ClassName(args) - the constructor-call forms:
|
|
4386
|
+
// new Foo → new Foo()
|
|
4387
|
+
// new Foo(1, 2) → new Foo(1, 2)
|
|
4388
|
+
// new window.Thing(1, 2) → new window.Thing(1, 2)
|
|
4389
|
+
// new Foo().bar → new Foo().bar (postfix continues in parsePrimary)
|
|
4390
|
+
// Called with the "new" word already consumed. The callee is parsed as a
|
|
4391
|
+
// plain identifier plus a member chain so "new THREE.Scene(75)" binds the
|
|
4392
|
+
// argument list to the constructor - not to a member call.
|
|
4393
|
+
function parseNewExpressionCore() {
|
|
4394
|
+
const callee = parseNewCallee();
|
|
4395
|
+
let args = [];
|
|
3924
4396
|
if (peek().type === TOKEN.LPAREN) {
|
|
3925
4397
|
advance(); // (
|
|
3926
|
-
const { separator, args } = parseArgList();
|
|
4398
|
+
const { separator, args: parsedArgs } = parseArgList();
|
|
3927
4399
|
consume(TOKEN.RPAREN, 'Expected ")" to close the constructor call.');
|
|
3928
|
-
|
|
4400
|
+
if (separator) {
|
|
4401
|
+
throw new Error(makeError(
|
|
4402
|
+
'Constructor calls cannot use "to"/"from" arguments.\n\nExample:\n new THREE.PerspectiveCamera(75, 1.5, 0.1, 1000)',
|
|
4403
|
+
peek()
|
|
4404
|
+
));
|
|
4405
|
+
}
|
|
4406
|
+
args = parsedArgs;
|
|
3929
4407
|
}
|
|
3930
|
-
return { type: 'NewExpression', callee, args
|
|
4408
|
+
return { type: 'NewExpression', callee, args };
|
|
4409
|
+
}
|
|
4410
|
+
|
|
4411
|
+
// The constructor name after "new": an identifier with an optional member
|
|
4412
|
+
// chain. Deliberately narrower than parsePrimary - a call paren following
|
|
4413
|
+
// the callee belongs to the constructor, so it is never consumed here.
|
|
4414
|
+
function parseNewCallee() {
|
|
4415
|
+
const token = peek();
|
|
4416
|
+
if (token.type !== TOKEN.IDENTIFIER || token.value === 'new') {
|
|
4417
|
+
throw new Error(makeError(
|
|
4418
|
+
'Expected a class name after "new".\n\nExample:\n remember scene as new THREE.Scene()',
|
|
4419
|
+
token
|
|
4420
|
+
));
|
|
4421
|
+
}
|
|
4422
|
+
advance();
|
|
4423
|
+
let node = { type: 'Identifier', name: token.value };
|
|
4424
|
+
while (peek().type === TOKEN.DOT) {
|
|
4425
|
+
advance();
|
|
4426
|
+
const propToken = peek();
|
|
4427
|
+
if (propToken.type === TOKEN.EOF || !/^[A-Za-z_$]/.test(propToken.value)) {
|
|
4428
|
+
throw new Error(makeError('Expected a property name after "." in the constructor.', propToken));
|
|
4429
|
+
}
|
|
4430
|
+
advance();
|
|
4431
|
+
node = { type: 'MemberExpression', object: node, property: propToken.value };
|
|
4432
|
+
}
|
|
4433
|
+
return node;
|
|
3931
4434
|
}
|
|
3932
4435
|
|
|
3933
4436
|
// ── Program ────────────────────────────────────────────────────────────────
|