plainscript-lang 1.0.36 → 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 +251 -19
- package/compiler/bundler.js +4 -4
- package/compiler/cli.js +18 -18
- package/compiler/dependency-detector.js +34 -22
- package/compiler/generator.js +286 -186
- package/compiler/lexer.js +49 -39
- package/compiler/parser.js +552 -181
- package/compiler/sourcemap.js +1 -1
- package/compiler/version.js +1 -1
- package/docs/CAPABILITY_GAP_AUDIT.md +2 -2
- package/docs/FB-PROMPT.md +524 -0
- package/docs/GAME-PROMPT.md +56 -55
- package/docs/PLAINSCRIPT-SPEC.md +16 -15
- package/docs/index.html +13 -6
- package/docs/website/script.js +1 -1
- package/docs/website/style.css +3 -3
- 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 +3 -3
- package/examples/browser-interactive/src/interactive.pln +1 -1
- package/examples/cache-and-jobs.pln +1 -1
- package/examples/cache-schedule.pln +1 -1
- package/examples/canvas-game/README.md +4 -4
- package/examples/canvas-game/src/game.pln +1 -1
- 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 +6 -6
- package/examples/javascript-library/public/index.html +1 -1
- package/examples/javascript-library/src/interop.pln +1 -1
- 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 +2 -2
- package/examples/three-dimensional/src/scene.pln +1 -1
- 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 +7 -9
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,7 +36,7 @@ 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
|
|
|
@@ -51,7 +51,7 @@ const TIME_UNITS = {
|
|
|
51
51
|
day: 24 * 60 * 60 * 1000, days: 24 * 60 * 60 * 1000,
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
// v2.1.0
|
|
54
|
+
// v2.1.0 - split raw SQL into placeholder-free text and ordered parameter
|
|
55
55
|
// names. "{name}" marks a bound parameter; the generator renders "?" for
|
|
56
56
|
// SQLite or "$1…" for PostgreSQL.
|
|
57
57
|
function extractSqlParams(rawSql) {
|
|
@@ -63,7 +63,7 @@ function extractSqlParams(rawSql) {
|
|
|
63
63
|
return { sql, params };
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
// v2.1.1
|
|
66
|
+
// v2.1.1 - parse an upload size limit such as "5 MB", "512 KB", "1GB" or
|
|
67
67
|
// "100B" into a byte count. Returns null when the text is not a valid size.
|
|
68
68
|
const UPLOAD_SIZE_UNITS = { b: 1, kb: 1024, mb: 1024 * 1024, gb: 1024 * 1024 * 1024 };
|
|
69
69
|
function parseUploadSize(text) {
|
|
@@ -107,6 +107,7 @@ function makeError(message, token) {
|
|
|
107
107
|
|
|
108
108
|
function parse(tokens) {
|
|
109
109
|
let pos = 0;
|
|
110
|
+
let _inRepeatCount = false;
|
|
110
111
|
|
|
111
112
|
function peek() { return tokens[pos]; }
|
|
112
113
|
function peekAt(offset) { return tokens[pos + offset] || { type: TOKEN.EOF }; }
|
|
@@ -132,13 +133,49 @@ function parse(tokens) {
|
|
|
132
133
|
// comparison := the single-comparison forms below
|
|
133
134
|
//
|
|
134
135
|
// Node shapes produced by the comparison level:
|
|
135
|
-
// BinaryCondition { type, left, op, right }
|
|
136
|
-
// UnaryCondition { type, left, op }
|
|
137
|
-
// BetweenCondition { type, left, low, high }
|
|
138
|
-
// 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
|
|
139
140
|
// plus LogicalCondition { type, op: "and"|"or", left, right } and
|
|
140
141
|
// { type, op: "not", operand } from the combinator levels.
|
|
141
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
|
+
|
|
142
179
|
// Entry point used by if/while.
|
|
143
180
|
function parseCondition() {
|
|
144
181
|
let left = parseAndCondition();
|
|
@@ -170,121 +207,174 @@ function parse(tokens) {
|
|
|
170
207
|
|
|
171
208
|
function parseComparisonCondition() {
|
|
172
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) {
|
|
173
223
|
|
|
174
224
|
// ── instanceof condition ───────────────────────────────────────────────────
|
|
175
225
|
if (peek().type === TOKEN.INSTANCEOF) {
|
|
176
226
|
advance();
|
|
177
|
-
const right =
|
|
227
|
+
const right = parseBooleanAtom();
|
|
178
228
|
return { type: 'BinaryCondition', left, op: 'instanceof', right };
|
|
179
229
|
}
|
|
180
230
|
|
|
181
|
-
// v2.4
|
|
231
|
+
// v2.4 - "has field" → "x" in obj (contextual: "has" followed by "field")
|
|
182
232
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'has' &&
|
|
183
233
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'field') {
|
|
184
234
|
advance(); // has
|
|
185
235
|
advance(); // field
|
|
186
|
-
const right =
|
|
236
|
+
const right = parseBooleanAtom();
|
|
187
237
|
return { type: 'BinaryCondition', left: right, op: 'in', right: left };
|
|
188
238
|
}
|
|
189
|
-
// v2.4
|
|
239
|
+
// v2.4 - "starts as" → startsWith (contextual: "starts" followed by "as")
|
|
190
240
|
if (peek().type === TOKEN.STARTS && peekAt(1).type === TOKEN.AS) {
|
|
191
241
|
advance(); // starts
|
|
192
242
|
advance(); // as
|
|
193
|
-
const right =
|
|
243
|
+
const right = parseBooleanAtom();
|
|
194
244
|
return { type: 'StringCondition', left, method: 'startsWith', right };
|
|
195
245
|
}
|
|
196
|
-
// v2.4
|
|
246
|
+
// v2.4 - "ends as" → endsWith (contextual: "ends" followed by "as")
|
|
197
247
|
if (peek().type === TOKEN.ENDS && peekAt(1).type === TOKEN.AS) {
|
|
198
248
|
advance(); // ends
|
|
199
249
|
advance(); // as
|
|
200
|
-
const right =
|
|
250
|
+
const right = parseBooleanAtom();
|
|
201
251
|
return { type: 'StringCondition', left, method: 'endsWith', right };
|
|
202
252
|
}
|
|
203
|
-
// v2.4
|
|
253
|
+
// v2.4 - "made of" → includes (contextual: "made" followed by "of")
|
|
204
254
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'made' &&
|
|
205
255
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
206
256
|
advance(); // made
|
|
207
257
|
advance(); // of
|
|
208
|
-
const right =
|
|
258
|
+
const right = parseBooleanAtom();
|
|
209
259
|
return { type: 'StringCondition', left, method: 'includes', right };
|
|
210
260
|
}
|
|
211
|
-
// v2.4
|
|
261
|
+
// v2.4 - "more than" → > (contextual: "more" followed by "than")
|
|
212
262
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'more' &&
|
|
213
263
|
peekAt(1).type === TOKEN.THAN) {
|
|
214
264
|
advance(); // more
|
|
215
265
|
advance(); // than
|
|
216
|
-
const right =
|
|
266
|
+
const right = parseBooleanAtom();
|
|
217
267
|
return { type: 'BinaryCondition', left, op: '>', right };
|
|
218
268
|
}
|
|
219
|
-
// v2.4
|
|
269
|
+
// v2.4 - "fewer than" → < (contextual: "fewer" followed by "than")
|
|
220
270
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'fewer' &&
|
|
221
271
|
peekAt(1).type === TOKEN.THAN) {
|
|
222
272
|
advance(); // fewer
|
|
223
273
|
advance(); // than
|
|
224
|
-
const right =
|
|
274
|
+
const right = parseBooleanAtom();
|
|
225
275
|
return { type: 'BinaryCondition', left, op: '<', right };
|
|
226
276
|
}
|
|
227
|
-
// v2.4
|
|
277
|
+
// v2.4 - "same as" → === (contextual: "same" followed by "as")
|
|
228
278
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'same' &&
|
|
229
279
|
peekAt(1).type === TOKEN.AS) {
|
|
230
280
|
advance(); // same
|
|
231
281
|
advance(); // as
|
|
232
|
-
const right =
|
|
282
|
+
const right = parseBooleanAtom();
|
|
233
283
|
return { type: 'BinaryCondition', left, op: '===', right };
|
|
234
284
|
}
|
|
235
|
-
// v2.4
|
|
285
|
+
// v2.4 - "different from" → !== (contextual: "different" followed by "from")
|
|
236
286
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'different' &&
|
|
237
287
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'from') {
|
|
238
288
|
advance(); // different
|
|
239
289
|
advance(); // from
|
|
240
|
-
const right =
|
|
290
|
+
const right = parseBooleanAtom();
|
|
241
291
|
return { type: 'BinaryCondition', left, op: '!==', right };
|
|
242
292
|
}
|
|
243
293
|
|
|
244
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
|
+
|
|
245
309
|
if (peek().type === TOKEN.CONTAINS) {
|
|
246
310
|
advance();
|
|
247
|
-
const right =
|
|
311
|
+
const right = parseBooleanAtom();
|
|
248
312
|
return { type: 'StringCondition', left, method: 'includes', right };
|
|
249
313
|
}
|
|
250
314
|
|
|
251
315
|
if (peek().type === TOKEN.STARTS) {
|
|
252
316
|
advance();
|
|
253
317
|
consume(TOKEN.WITH, makeError('Expected "with" after "starts". Use: starts with', peek()));
|
|
254
|
-
const right =
|
|
318
|
+
const right = parseBooleanAtom();
|
|
255
319
|
return { type: 'StringCondition', left, method: 'startsWith', right };
|
|
256
320
|
}
|
|
257
321
|
|
|
258
322
|
if (peek().type === TOKEN.ENDS) {
|
|
259
323
|
advance();
|
|
260
324
|
consume(TOKEN.WITH, makeError('Expected "with" after "ends". Use: ends with', peek()));
|
|
261
|
-
const right =
|
|
325
|
+
const right = parseBooleanAtom();
|
|
262
326
|
return { type: 'StringCondition', left, method: 'endsWith', right };
|
|
263
327
|
}
|
|
264
328
|
|
|
265
329
|
if (peek().type === TOKEN.BETWEEN) {
|
|
266
330
|
advance();
|
|
267
|
-
const low =
|
|
331
|
+
const low = parseBooleanAtom();
|
|
268
332
|
consume(TOKEN.AND, makeError('Expected "and" after the lower bound in "between" expression.\n\nExample:\n if x between 1 and 10', peek()));
|
|
269
|
-
const high =
|
|
333
|
+
const high = parseBooleanAtom();
|
|
270
334
|
return { type: 'BetweenCondition', left, low, high };
|
|
271
335
|
}
|
|
272
336
|
|
|
273
337
|
// ── "is ..." comparisons ────────────────────────────────────────────────
|
|
274
338
|
const isToken = peek();
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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();
|
|
279
347
|
|
|
280
|
-
// is
|
|
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
|
+
}
|
|
354
|
+
|
|
355
|
+
// is not empty / is not in <expr> / is not equal to <expr> / is not <expr>
|
|
281
356
|
if (peek().type === TOKEN.NOT) {
|
|
282
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
|
+
}
|
|
283
364
|
if (peek().type === TOKEN.EMPTY) {
|
|
284
365
|
advance();
|
|
285
366
|
return { type: 'UnaryCondition', left, op: 'isNotEmpty' };
|
|
286
367
|
}
|
|
287
|
-
|
|
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();
|
|
288
378
|
return { type: 'BinaryCondition', left, op: '!==', right };
|
|
289
379
|
}
|
|
290
380
|
|
|
@@ -294,17 +384,36 @@ function parse(tokens) {
|
|
|
294
384
|
return { type: 'UnaryCondition', left, op: 'isEmpty' };
|
|
295
385
|
}
|
|
296
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
|
+
|
|
297
406
|
// is above (alias: >)
|
|
298
407
|
if (peek().type === TOKEN.ABOVE) {
|
|
299
408
|
advance();
|
|
300
|
-
const right =
|
|
409
|
+
const right = parseBooleanAtom();
|
|
301
410
|
return { type: 'BinaryCondition', left, op: '>', right };
|
|
302
411
|
}
|
|
303
412
|
|
|
304
413
|
// is below (alias: <)
|
|
305
414
|
if (peek().type === TOKEN.BELOW) {
|
|
306
415
|
advance();
|
|
307
|
-
const right =
|
|
416
|
+
const right = parseBooleanAtom();
|
|
308
417
|
return { type: 'BinaryCondition', left, op: '<', right };
|
|
309
418
|
}
|
|
310
419
|
|
|
@@ -313,35 +422,53 @@ function parse(tokens) {
|
|
|
313
422
|
advance();
|
|
314
423
|
if (peek().type === TOKEN.LEAST) {
|
|
315
424
|
advance();
|
|
316
|
-
const right =
|
|
425
|
+
const right = parseBooleanAtom();
|
|
317
426
|
return { type: 'BinaryCondition', left, op: '>=', right };
|
|
318
427
|
}
|
|
319
428
|
if (peek().type === TOKEN.MOST) {
|
|
320
429
|
advance();
|
|
321
|
-
const right =
|
|
430
|
+
const right = parseBooleanAtom();
|
|
322
431
|
return { type: 'BinaryCondition', left, op: '<=', right };
|
|
323
432
|
}
|
|
324
433
|
throw new Error(makeError('Expected "least" or "most" after "at". Use: is at least / is at most', peek()));
|
|
325
434
|
}
|
|
326
435
|
|
|
327
|
-
// is greater than
|
|
436
|
+
// is greater than [or equal to]
|
|
328
437
|
if (peek().type === TOKEN.GREATER) {
|
|
329
438
|
advance();
|
|
330
439
|
consume(TOKEN.THAN, 'Expected "than" after "greater". Use: is greater than');
|
|
331
|
-
|
|
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();
|
|
332
450
|
return { type: 'BinaryCondition', left, op: '>', right };
|
|
333
451
|
}
|
|
334
452
|
|
|
335
|
-
// is less than
|
|
453
|
+
// is less than [or equal to]
|
|
336
454
|
if (peek().type === TOKEN.LESS) {
|
|
337
455
|
advance();
|
|
338
456
|
consume(TOKEN.THAN, 'Expected "than" after "less". Use: is less than');
|
|
339
|
-
|
|
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();
|
|
340
467
|
return { type: 'BinaryCondition', left, op: '<', right };
|
|
341
468
|
}
|
|
342
469
|
|
|
343
470
|
// is <expr> (equality)
|
|
344
|
-
const right =
|
|
471
|
+
const right = parseBooleanAtom();
|
|
345
472
|
return { type: 'BinaryCondition', left, op: '===', right };
|
|
346
473
|
}
|
|
347
474
|
|
|
@@ -385,6 +512,17 @@ function parse(tokens) {
|
|
|
385
512
|
if (token.type === TOKEN.SHOW) return parseShow();
|
|
386
513
|
if (token.type === TOKEN.PRINT) return parseShow(); // alias for show
|
|
387
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
|
+
}
|
|
388
526
|
if (token.type === TOKEN.IF) return parseIf();
|
|
389
527
|
if (token.type === TOKEN.MAKE) return parseMake();
|
|
390
528
|
if (token.type === TOKEN.DEFINE) return parseMake(); // alias for make
|
|
@@ -392,18 +530,22 @@ function parse(tokens) {
|
|
|
392
530
|
if (token.type === TOKEN.GIVE) return parseGive();
|
|
393
531
|
if (token.type === TOKEN.RETURN) return parseGive(); // alias for give
|
|
394
532
|
if (token.type === TOKEN.GIVE_BACK) return parseGive(); // alias for give
|
|
395
|
-
// v1.0.1
|
|
533
|
+
// v1.0.1 - generators: `yield <expr>` is only meaningful inside a function
|
|
396
534
|
// body; the generator marks the enclosing `make ... done` as a function*.
|
|
397
535
|
if (token.type === TOKEN.YIELD) return parseYield();
|
|
398
536
|
if (token.type === TOKEN.FOR) return parseForEach();
|
|
399
|
-
//
|
|
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
|
|
400
542
|
// lexes as TOKEN.EACH (the "for every" alias), so only the number+unit
|
|
401
543
|
// form is intercepted; "for every item in list" keeps its meaning.
|
|
402
544
|
if (token.type === TOKEN.EACH && peekAt(1).type === TOKEN.NUMBER &&
|
|
403
545
|
peekAt(2).type === TOKEN.IDENTIFIER && TIME_UNITS[peekAt(2).value]) {
|
|
404
546
|
return parseEvery();
|
|
405
547
|
}
|
|
406
|
-
// v1.0.
|
|
548
|
+
// v1.0.361 - every frame … done: requestAnimationFrame loop. "every" lexes
|
|
407
549
|
// as TOKEN.EACH; guard on the following "frame" identifier ("for every
|
|
408
550
|
// item in list" is intercepted by TOKEN.FOR above).
|
|
409
551
|
if (token.type === TOKEN.EACH && peekAt(1).type === TOKEN.IDENTIFIER &&
|
|
@@ -417,9 +559,9 @@ function parse(tokens) {
|
|
|
417
559
|
return parseImport();
|
|
418
560
|
}
|
|
419
561
|
if (token.type === TOKEN.WHEN) return parseWhen();
|
|
420
|
-
// v2.3
|
|
562
|
+
// v2.3 - English-like function declaration: "to add a and b together ... done"
|
|
421
563
|
if (token.type === TOKEN.TO && peekAt(1).type === TOKEN.IDENTIFIER) return parseToFunction();
|
|
422
|
-
// v2.4
|
|
564
|
+
// v2.4 - "list with" and "record with" as expressions
|
|
423
565
|
if (token.type === TOKEN.IDENTIFIER && token.value === 'list' && peekAt(1).type === TOKEN.WITH) return parseListWith();
|
|
424
566
|
if (token.type === TOKEN.IDENTIFIER && token.value === 'record' && peekAt(1).type === TOKEN.WITH) return parseRecordWith(false);
|
|
425
567
|
if (token.type === TOKEN.IDENTIFIER && token.value === 'put' && peekAt(1).type !== TOKEN.LPAREN) return parsePutStatement();
|
|
@@ -441,7 +583,7 @@ function parse(tokens) {
|
|
|
441
583
|
advance();
|
|
442
584
|
return { type: 'DebuggerStatement' };
|
|
443
585
|
}
|
|
444
|
-
// v2.0.1
|
|
586
|
+
// v2.0.1 - OCR capability
|
|
445
587
|
if (token.type === TOKEN.OCR_KW) return parseOcr();
|
|
446
588
|
// v0.6
|
|
447
589
|
if (token.type === TOKEN.WEB) return parseWebApp();
|
|
@@ -451,7 +593,7 @@ function parse(tokens) {
|
|
|
451
593
|
if (token.type === TOKEN.DATABASE_KW) return parseDatabase();
|
|
452
594
|
if (token.type === TOKEN.CONNECT_DB) return parseDatabase(); // alias
|
|
453
595
|
if (token.type === TOKEN.USE_DATABASE) return parseDatabase(); // alias
|
|
454
|
-
// v2.1.0
|
|
596
|
+
// v2.1.0 - query("field"): the HTTP query-string accessor. The SQLite
|
|
455
597
|
// block form keeps priority when "query" stands alone on a line; a
|
|
456
598
|
// call form is always the accessor.
|
|
457
599
|
if (token.type === TOKEN.QUERY_KW && peekAt(1).type === TOKEN.LPAREN) {
|
|
@@ -460,7 +602,7 @@ function parse(tokens) {
|
|
|
460
602
|
if (token.type === TOKEN.QUERY_KW) return parseSqlBlock('query', 'QueryStatement');
|
|
461
603
|
if (token.type === TOKEN.INSERT_KW) return parseSqlBlock('insert', 'InsertStatement');
|
|
462
604
|
if (token.type === TOKEN.UPDATE_KW) return parseSqlBlock('update', 'UpdateStatement');
|
|
463
|
-
// v2.1.1
|
|
605
|
+
// v2.1.1 - delete "<url>" is an HTTP DELETE request; a bare "delete"
|
|
464
606
|
// starting a raw block keeps its SQL meaning.
|
|
465
607
|
if (token.type === TOKEN.DELETE_KW && tokenStartsValue(peekAt(1))) {
|
|
466
608
|
return { type: 'ExpressionStatement', expression: parseHttpCall('delete') };
|
|
@@ -486,7 +628,7 @@ function parse(tokens) {
|
|
|
486
628
|
|
|
487
629
|
// Statements starting with an identifier: call, becomes, index/member becomes
|
|
488
630
|
if (token.type === TOKEN.IDENTIFIER) {
|
|
489
|
-
// v2.5
|
|
631
|
+
// v2.5 - natural string and collection verbs that transform a variable
|
|
490
632
|
// in place: "lowercase title", "uppercase first letter of each word in title",
|
|
491
633
|
// "split title by \" \"", "join title by \" \"", "trim title".
|
|
492
634
|
const TRANSFORM_VERB = token.value === 'lowercase' || token.value === 'uppercase' ||
|
|
@@ -561,7 +703,7 @@ function parse(tokens) {
|
|
|
561
703
|
return { type: 'ExpressionStatement', expression: parsePrimary() };
|
|
562
704
|
}
|
|
563
705
|
|
|
564
|
-
// v1.2
|
|
706
|
+
// v1.2 - bot "<token>" / bot <expr>: creates the polling Telegram bot.
|
|
565
707
|
// Bound to BOT by the generator's `bot` stdlib. Only intercepts when a
|
|
566
708
|
// value follows; bot(...) calls and ordinary identifiers (e.g. a
|
|
567
709
|
// variable named bot) keep their normal meaning.
|
|
@@ -571,7 +713,7 @@ function parse(tokens) {
|
|
|
571
713
|
peekAt(1).type === TOKEN.LBRACKET || peekAt(1).type === TOKEN.LBRACE;
|
|
572
714
|
if (token.value === 'bot' && nextIsValue) return parseBot();
|
|
573
715
|
|
|
574
|
-
// v2.1.0
|
|
716
|
+
// v2.1.0 - allow cors: enables CORS middleware on the current app.
|
|
575
717
|
// Contextual (not a reserved keyword): a variable named "allow" keeps
|
|
576
718
|
// its ordinary meaning.
|
|
577
719
|
if (token.value === 'allow' &&
|
|
@@ -581,7 +723,7 @@ function parse(tokens) {
|
|
|
581
723
|
return { type: 'AllowCorsStatement' };
|
|
582
724
|
}
|
|
583
725
|
|
|
584
|
-
// v2.1.0
|
|
726
|
+
// v2.1.0 - group "<prefix>" ... done: composes routes under a shared
|
|
585
727
|
// path prefix. Contextual like "bot": only intercepted when a quoted
|
|
586
728
|
// prefix follows.
|
|
587
729
|
if (token.value === 'group' && peekAt(1).type === TOKEN.STRING) {
|
|
@@ -591,7 +733,7 @@ function parse(tokens) {
|
|
|
591
733
|
return { type: 'GroupStatement', prefix, body };
|
|
592
734
|
}
|
|
593
735
|
|
|
594
|
-
// v2.1.0
|
|
736
|
+
// v2.1.0 - postgres "<connection>": binds the PostgreSQL driver.
|
|
595
737
|
// Contextual: a variable named "postgres" keeps its meaning unless a
|
|
596
738
|
// connection value follows on the same line.
|
|
597
739
|
if (token.value === 'postgres' && (
|
|
@@ -600,7 +742,7 @@ function parse(tokens) {
|
|
|
600
742
|
return parsePostgres();
|
|
601
743
|
}
|
|
602
744
|
|
|
603
|
-
// v2.2.0
|
|
745
|
+
// v2.2.0 - mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
|
|
604
746
|
// Contextual: a variable named "mongo" or "mongodb" keeps its meaning unless
|
|
605
747
|
// a connection string follows on the same line.
|
|
606
748
|
if ((token.value === 'mongo' || token.value === 'mongodb') && (
|
|
@@ -609,7 +751,7 @@ function parse(tokens) {
|
|
|
609
751
|
return parseMongo();
|
|
610
752
|
}
|
|
611
753
|
|
|
612
|
-
// v2.1.0
|
|
754
|
+
// v2.1.0 - cache "<redis-url>" / cache env("REDIS_URL"): connects the
|
|
613
755
|
// shared Redis client used by cacheGet / cacheSet / cacheDelete.
|
|
614
756
|
// Contextual like "postgres": a variable named "cache" keeps its
|
|
615
757
|
// meaning unless a connection value follows on the same line.
|
|
@@ -621,7 +763,7 @@ function parse(tokens) {
|
|
|
621
763
|
return { type: 'CacheStatement', url };
|
|
622
764
|
}
|
|
623
765
|
|
|
624
|
-
// v2.1.0
|
|
766
|
+
// v2.1.0 - transaction … done: groups database writes atomically.
|
|
625
767
|
// Contextual: "transaction becomes x" and "transaction(...)" keep
|
|
626
768
|
// their ordinary variable/function meanings.
|
|
627
769
|
if (token.value === 'transaction' &&
|
|
@@ -631,7 +773,7 @@ function parse(tokens) {
|
|
|
631
773
|
return { type: 'TransactionStatement', body };
|
|
632
774
|
}
|
|
633
775
|
|
|
634
|
-
// v2.1.0
|
|
776
|
+
// v2.1.0 - mail transport … done: configures the outgoing mailer.
|
|
635
777
|
if (token.value === 'mail' &&
|
|
636
778
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'transport') {
|
|
637
779
|
advance(); // mail
|
|
@@ -639,7 +781,7 @@ function parse(tokens) {
|
|
|
639
781
|
return { type: 'MailTransportStatement', options: parsePropertyList('"mail transport" block') };
|
|
640
782
|
}
|
|
641
783
|
|
|
642
|
-
// v2.1.0
|
|
784
|
+
// v2.1.0 - send mail … done: sends one email through the transport.
|
|
643
785
|
if (token.value === 'send' &&
|
|
644
786
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'mail') {
|
|
645
787
|
advance(); // send
|
|
@@ -647,22 +789,22 @@ function parse(tokens) {
|
|
|
647
789
|
return { type: 'SendMailStatement', fields: parsePropertyList('"send mail" block') };
|
|
648
790
|
}
|
|
649
791
|
|
|
650
|
-
// v2.1.0
|
|
651
|
-
// ("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
|
|
652
794
|
// identifier-form branch only guards against future lexer changes.)
|
|
653
795
|
if (token.value === 'every' && peekAt(1).type === TOKEN.NUMBER &&
|
|
654
796
|
peekAt(2).type === TOKEN.IDENTIFIER && TIME_UNITS[peekAt(2).value]) {
|
|
655
797
|
return parseEvery();
|
|
656
798
|
}
|
|
657
799
|
|
|
658
|
-
// v1.0.
|
|
800
|
+
// v1.0.361 - every frame … done: requestAnimationFrame loop. Identifier
|
|
659
801
|
// mirror of the EACH dispatch above.
|
|
660
802
|
if (token.value === 'every' && peekAt(1).type === TOKEN.IDENTIFIER &&
|
|
661
803
|
peekAt(1).value === 'frame') {
|
|
662
804
|
return parseEveryFrame();
|
|
663
805
|
}
|
|
664
806
|
|
|
665
|
-
// v1.0.
|
|
807
|
+
// v1.0.361 - after <n> <unit>s … done: one-shot delayed execution.
|
|
666
808
|
// Contextual like "retry": "after becomes 5" and after(...) keep their
|
|
667
809
|
// ordinary meanings.
|
|
668
810
|
if (token.value === 'after' &&
|
|
@@ -671,7 +813,7 @@ function parse(tokens) {
|
|
|
671
813
|
return parseAfter();
|
|
672
814
|
}
|
|
673
815
|
|
|
674
|
-
// v2.1.0
|
|
816
|
+
// v2.1.0 - schedule "<cron>" … done: run work on a cron schedule.
|
|
675
817
|
if (token.value === 'schedule' && peekAt(1).type === TOKEN.STRING) {
|
|
676
818
|
advance(); // schedule
|
|
677
819
|
const expression = advance().value; // cron string
|
|
@@ -679,7 +821,7 @@ function parse(tokens) {
|
|
|
679
821
|
return { type: 'ScheduleStatement', expression, body };
|
|
680
822
|
}
|
|
681
823
|
|
|
682
|
-
// v2.1.0
|
|
824
|
+
// v2.1.0 - run background <call>: fire-and-forget execution.
|
|
683
825
|
if (token.value === 'run' &&
|
|
684
826
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'background') {
|
|
685
827
|
advance(); // run
|
|
@@ -700,13 +842,13 @@ function parse(tokens) {
|
|
|
700
842
|
return parseRunParallelStatement();
|
|
701
843
|
}
|
|
702
844
|
|
|
703
|
-
// v2.1.0
|
|
845
|
+
// v2.1.0 - websocket server on <port> … done: realtime endpoint.
|
|
704
846
|
if (token.value === 'websocket' &&
|
|
705
847
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'server') {
|
|
706
848
|
return parseWebSocketServer();
|
|
707
849
|
}
|
|
708
850
|
|
|
709
|
-
// v2.1.1
|
|
851
|
+
// v2.1.1 - whatsapp bot … done: WhatsApp bot runtime (Baileys under
|
|
710
852
|
// the hood). Contextual: a variable named "whatsapp" keeps its meaning;
|
|
711
853
|
// only "whatsapp bot" opens the block.
|
|
712
854
|
if (token.value === 'whatsapp' &&
|
|
@@ -714,7 +856,7 @@ function parse(tokens) {
|
|
|
714
856
|
return parseWhatsAppBot();
|
|
715
857
|
}
|
|
716
858
|
|
|
717
|
-
// v2.1.1
|
|
859
|
+
// v2.1.1 - pair whatsapp "<phone>": starts an on-demand WhatsApp pairing
|
|
718
860
|
// session for the given phone number. Used by hybrid bots where a
|
|
719
861
|
// Telegram command triggers a WhatsApp pairing flow.
|
|
720
862
|
// Accepts a string literal (validated at compile time) or any expression
|
|
@@ -731,7 +873,7 @@ function parse(tokens) {
|
|
|
731
873
|
return { type: 'WhatsAppPairStatement', phone: phoneExpr };
|
|
732
874
|
}
|
|
733
875
|
|
|
734
|
-
// v2.1.1
|
|
876
|
+
// v2.1.1 - log message: prints the normalized message record inside an
|
|
735
877
|
// "on message" handler. Generation rejects it everywhere else with a
|
|
736
878
|
// teaching error.
|
|
737
879
|
if (token.value === 'log' &&
|
|
@@ -741,7 +883,7 @@ function parse(tokens) {
|
|
|
741
883
|
return { type: 'WhatsAppLogStatement' };
|
|
742
884
|
}
|
|
743
885
|
|
|
744
|
-
// v2.14
|
|
886
|
+
// v2.14 - download "<path>": saves the current message's media (image,
|
|
745
887
|
// video, audio, document, sticker, …) to a file, inside an "on message"
|
|
746
888
|
// handler. Generation rejects it everywhere else with a teaching error.
|
|
747
889
|
if (token.value === 'download' && peekAt(1).type === TOKEN.STRING) {
|
|
@@ -750,14 +892,14 @@ function parse(tokens) {
|
|
|
750
892
|
return { type: 'WhatsAppDownloadStatement', filePath };
|
|
751
893
|
}
|
|
752
894
|
|
|
753
|
-
// v2.1.0
|
|
895
|
+
// v2.1.0 - broadcast <expr>: sends to every connected socket.
|
|
754
896
|
if (token.value === 'broadcast') {
|
|
755
897
|
advance(); // broadcast
|
|
756
898
|
const value = parseExpression();
|
|
757
899
|
return { type: 'BroadcastStatement', value };
|
|
758
900
|
}
|
|
759
901
|
|
|
760
|
-
// v2.2.0
|
|
902
|
+
// v2.2.0 - send to <connection> <message>: send to a specific stored connection.
|
|
761
903
|
if (token.value === 'send' &&
|
|
762
904
|
(peekAt(1).type === TOKEN.TO || (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'to'))) {
|
|
763
905
|
advance(); // send
|
|
@@ -767,7 +909,7 @@ function parse(tokens) {
|
|
|
767
909
|
return { type: 'SendToStatement', connection, value };
|
|
768
910
|
}
|
|
769
911
|
|
|
770
|
-
// v2.1.0
|
|
912
|
+
// v2.1.0 - send socket <expr>: replies to one connected socket.
|
|
771
913
|
if (token.value === 'send' &&
|
|
772
914
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'socket') {
|
|
773
915
|
advance(); // send
|
|
@@ -776,7 +918,7 @@ function parse(tokens) {
|
|
|
776
918
|
return { type: 'SendSocketStatement', value };
|
|
777
919
|
}
|
|
778
920
|
|
|
779
|
-
// v2.1.0
|
|
921
|
+
// v2.1.0 - status 404 / status <expr>: sets the HTTP response status.
|
|
780
922
|
// Contextual: "status becomes 404" is still a normal reassignment of a
|
|
781
923
|
// variable named "status", and "status(...)" remains a function call.
|
|
782
924
|
if (token.value === 'status' && peekAt(1).type !== TOKEN.BECOMES &&
|
|
@@ -788,7 +930,7 @@ function parse(tokens) {
|
|
|
788
930
|
return { type: 'StatusStatement', value };
|
|
789
931
|
}
|
|
790
932
|
|
|
791
|
-
// v2.2.0
|
|
933
|
+
// v2.2.0 - redirect to "<url>": sends an HTTP redirect from a route
|
|
792
934
|
// handler. "redirect(...)" calls and "redirect becomes x" stay ordinary.
|
|
793
935
|
if (token.value === 'redirect' && (peekAt(1).value === 'to' || peekAt(1).type === TOKEN.TO)) {
|
|
794
936
|
advance(); // redirect
|
|
@@ -874,7 +1016,7 @@ function parse(tokens) {
|
|
|
874
1016
|
return { type: 'GoogleOAuthStatement', options: parsePropertyList('"google oauth" block') };
|
|
875
1017
|
}
|
|
876
1018
|
|
|
877
|
-
// ── v1.0.1
|
|
1019
|
+
// ── v1.0.1 - capability-gap features (all contextual, IOPL-native) ──
|
|
878
1020
|
|
|
879
1021
|
// define a kind called "Person" with … done → record schema (classes)
|
|
880
1022
|
if ((token.type === TOKEN.DEFINE || (token.type === TOKEN.IDENTIFIER && token.value === 'define')) &&
|
|
@@ -945,7 +1087,19 @@ function parse(tokens) {
|
|
|
945
1087
|
|
|
946
1088
|
// Assignment operators: becomes, is now, set to, change to, or becomes (||=), and becomes (&&=), nullish becomes (??=)
|
|
947
1089
|
let becomeOp = null;
|
|
948
|
-
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) {
|
|
949
1103
|
advance(); // or
|
|
950
1104
|
advance(); // becomes
|
|
951
1105
|
becomeOp = '||=';
|
|
@@ -1039,7 +1193,7 @@ function parse(tokens) {
|
|
|
1039
1193
|
}
|
|
1040
1194
|
|
|
1041
1195
|
// Object literal: next token is IDENTIFIER (except dictionary/map) followed by IS or BE
|
|
1042
|
-
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()) {
|
|
1043
1197
|
const init = parseInlineObjectLiteral(false);
|
|
1044
1198
|
return { type: 'VariableDeclaration', name: target, initializer: init, isLet, isConstant: !isLet };
|
|
1045
1199
|
}
|
|
@@ -1055,11 +1209,11 @@ function parse(tokens) {
|
|
|
1055
1209
|
);
|
|
1056
1210
|
|
|
1057
1211
|
// Object literal: next token is IDENTIFIER followed by IS or BE
|
|
1058
|
-
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()) {
|
|
1059
1213
|
return { type: 'RememberStatement', name: target, value: parseObjectLiteral() };
|
|
1060
1214
|
}
|
|
1061
1215
|
|
|
1062
|
-
// v2.1.0
|
|
1216
|
+
// v2.1.0 - remember <name> as query|insert|update|delete … done
|
|
1063
1217
|
// Captures the SQL result: rows for "query", the run info (changes /
|
|
1064
1218
|
// lastInsertRowid) for the write forms. The call form query("field")
|
|
1065
1219
|
// is the HTTP accessor and still parses as an expression below.
|
|
@@ -1100,7 +1254,7 @@ function parseAsk() {
|
|
|
1100
1254
|
return { type: 'AskStatement', variable };
|
|
1101
1255
|
}
|
|
1102
1256
|
|
|
1103
|
-
// v2.0.1
|
|
1257
|
+
// v2.0.1 - OCR capability.
|
|
1104
1258
|
//
|
|
1105
1259
|
// ocr "<image>" as <variable>
|
|
1106
1260
|
// ocr "<image>" as <variable> using "<lang>"
|
|
@@ -1130,10 +1284,13 @@ function parseAsk() {
|
|
|
1130
1284
|
function parseShow() {
|
|
1131
1285
|
const isPrint = peek().type === TOKEN.PRINT;
|
|
1132
1286
|
const isDisplay = peek().type === TOKEN.DISPLAY;
|
|
1287
|
+
const isLog = peek().type === TOKEN.LOG_KW || (peek().type === TOKEN.IDENTIFIER && peek().value === 'log');
|
|
1133
1288
|
if (isPrint) {
|
|
1134
1289
|
consume(TOKEN.PRINT);
|
|
1135
1290
|
} else if (isDisplay) {
|
|
1136
1291
|
consume(TOKEN.DISPLAY);
|
|
1292
|
+
} else if (isLog) {
|
|
1293
|
+
advance();
|
|
1137
1294
|
} else {
|
|
1138
1295
|
consume(TOKEN.SHOW);
|
|
1139
1296
|
}
|
|
@@ -1207,7 +1364,7 @@ function parseAsk() {
|
|
|
1207
1364
|
return { type: 'FunctionDeclaration', name, params, body };
|
|
1208
1365
|
}
|
|
1209
1366
|
|
|
1210
|
-
// v2.3
|
|
1367
|
+
// v2.3 - English-like function declaration: "to <name> <a> and <b> together ... done"
|
|
1211
1368
|
// Parameters are identifiers joined by "and". Body ends with "together" or "done".
|
|
1212
1369
|
function parseToFunction() {
|
|
1213
1370
|
consume(TOKEN.TO);
|
|
@@ -1241,21 +1398,24 @@ function parseAsk() {
|
|
|
1241
1398
|
}
|
|
1242
1399
|
params.push({ name: advance().value });
|
|
1243
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"
|
|
1244
1404
|
const body = parseBody(`function "${name}"`);
|
|
1245
1405
|
return { type: 'FunctionDeclaration', name, params, body };
|
|
1246
1406
|
}
|
|
1247
1407
|
|
|
1248
|
-
// v2.3
|
|
1408
|
+
// v2.3 - English-like array literal: "list with 1, 2, 3"
|
|
1249
1409
|
// Parse comma-separated elements for "list with" (shared helper)
|
|
1250
1410
|
function parseListWithElements() {
|
|
1251
1411
|
const elements = [];
|
|
1252
1412
|
if (peek().type !== TOKEN.DONE && peek().type !== TOKEN.TOGETHER && peek().type !== TOKEN.EOF &&
|
|
1253
1413
|
peek().type !== TOKEN.FOLDER &&
|
|
1254
1414
|
!(peek().type === TOKEN.IDENTIFIER && (peek().value === 'folder' || peek().value === 'done' || peek().value === 'together'))) {
|
|
1255
|
-
elements.push(
|
|
1415
|
+
elements.push(parseBooleanAtom());
|
|
1256
1416
|
while (peek().type === TOKEN.COMMA) {
|
|
1257
1417
|
advance(); // consume comma
|
|
1258
|
-
elements.push(
|
|
1418
|
+
elements.push(parseBooleanAtom());
|
|
1259
1419
|
}
|
|
1260
1420
|
}
|
|
1261
1421
|
return elements;
|
|
@@ -1273,7 +1433,7 @@ function parseAsk() {
|
|
|
1273
1433
|
return { type: 'ArrayLiteral', elements };
|
|
1274
1434
|
}
|
|
1275
1435
|
|
|
1276
|
-
// v2.3
|
|
1436
|
+
// v2.3 - English-like object literal: "record with name 'Alice' and age 30"
|
|
1277
1437
|
// consumeTerminator: true when used as expression (done closes record), false when standalone statement
|
|
1278
1438
|
function parseRecordWith(consumeTerminator) {
|
|
1279
1439
|
// Accept both "record_with" keyword and contextual "record" + "with"
|
|
@@ -1317,9 +1477,14 @@ function parseAsk() {
|
|
|
1317
1477
|
if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1318
1478
|
while (true) {
|
|
1319
1479
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1320
|
-
|
|
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();
|
|
1321
1484
|
consume(TOKEN.IS, 'Expected "is" between key and value in dictionary/map.');
|
|
1322
|
-
|
|
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();
|
|
1323
1488
|
pairs.push({ key, value });
|
|
1324
1489
|
if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
|
|
1325
1490
|
advance();
|
|
@@ -1341,7 +1506,7 @@ function parseAsk() {
|
|
|
1341
1506
|
if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1342
1507
|
while (true) {
|
|
1343
1508
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1344
|
-
elements.push(
|
|
1509
|
+
elements.push(parseBooleanAtom());
|
|
1345
1510
|
if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
1346
1511
|
advance();
|
|
1347
1512
|
continue;
|
|
@@ -1362,7 +1527,7 @@ function parseAsk() {
|
|
|
1362
1527
|
if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
|
|
1363
1528
|
while (true) {
|
|
1364
1529
|
if (peek().type === TOKEN.DONE || peek().value === 'done') break;
|
|
1365
|
-
elements.push(
|
|
1530
|
+
elements.push(parseBooleanAtom());
|
|
1366
1531
|
if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
|
|
1367
1532
|
advance();
|
|
1368
1533
|
continue;
|
|
@@ -1410,7 +1575,7 @@ function parseAsk() {
|
|
|
1410
1575
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'tuple') {
|
|
1411
1576
|
advance(); // tuple
|
|
1412
1577
|
}
|
|
1413
|
-
const tupleExpr =
|
|
1578
|
+
const tupleExpr = parseBooleanAtom();
|
|
1414
1579
|
if (peek().type === TOKEN.INTO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'into')) {
|
|
1415
1580
|
advance(); // into
|
|
1416
1581
|
}
|
|
@@ -1511,9 +1676,10 @@ function parseAsk() {
|
|
|
1511
1676
|
}
|
|
1512
1677
|
|
|
1513
1678
|
function parseGive() {
|
|
1514
|
-
const
|
|
1515
|
-
const
|
|
1516
|
-
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;
|
|
1517
1683
|
if (isReturn) {
|
|
1518
1684
|
consume(TOKEN.RETURN);
|
|
1519
1685
|
} else if (isGiveBack) {
|
|
@@ -1524,11 +1690,18 @@ function parseAsk() {
|
|
|
1524
1690
|
} else {
|
|
1525
1691
|
consume(TOKEN.GIVE);
|
|
1526
1692
|
}
|
|
1527
|
-
|
|
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
|
+
}
|
|
1528
1701
|
return { type: 'GiveStatement', value };
|
|
1529
1702
|
}
|
|
1530
1703
|
|
|
1531
|
-
// v1.0.1
|
|
1704
|
+
// v1.0.1 - generators: `yield <expr>` (optionally bare `yield`).
|
|
1532
1705
|
function parseYield() {
|
|
1533
1706
|
consume(TOKEN.YIELD);
|
|
1534
1707
|
let value = null;
|
|
@@ -1538,13 +1711,13 @@ function parseAsk() {
|
|
|
1538
1711
|
return { type: 'YieldStatement', value };
|
|
1539
1712
|
}
|
|
1540
1713
|
|
|
1541
|
-
// v1.0.1
|
|
1714
|
+
// v1.0.1 - record kinds: `define a kind called "Person" with … done`
|
|
1542
1715
|
// The field block reuses the property-list grammar (`age is 0`), so each
|
|
1543
1716
|
// field gets a default expression just like an object literal.
|
|
1544
1717
|
function parseDefineKind() {
|
|
1545
1718
|
advance(); // define
|
|
1546
1719
|
const art = advance(); // "a" / "an"
|
|
1547
|
-
// `define a kind called "Person"`
|
|
1720
|
+
// `define a kind called "Person"` - the word "kind" is optional prose.
|
|
1548
1721
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'kind') {
|
|
1549
1722
|
advance(); // kind
|
|
1550
1723
|
} else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'called') {
|
|
@@ -1584,7 +1757,7 @@ function parseAsk() {
|
|
|
1584
1757
|
return { type: 'DefineKindStatement', name, fields, extends: extendsKind };
|
|
1585
1758
|
}
|
|
1586
1759
|
|
|
1587
|
-
// v1.0.1
|
|
1760
|
+
// v1.0.1 - record constructor (expression): `create a Person with name "Ada" and age 17`
|
|
1588
1761
|
function parseCreateKind() {
|
|
1589
1762
|
advance(); // create
|
|
1590
1763
|
advance(); // a / an
|
|
@@ -1604,7 +1777,7 @@ function parseAsk() {
|
|
|
1604
1777
|
return { type: 'CreateKindExpression', kind, pairs };
|
|
1605
1778
|
}
|
|
1606
1779
|
|
|
1607
|
-
// v1.0.1
|
|
1780
|
+
// v1.0.1 - native test DSL: `test "name" … done`
|
|
1608
1781
|
function parseTestStatement() {
|
|
1609
1782
|
advance(); // test
|
|
1610
1783
|
const name = consume(TOKEN.STRING, 'Expected a test name string after "test".').value;
|
|
@@ -1612,10 +1785,13 @@ function parseAsk() {
|
|
|
1612
1785
|
return { type: 'TestStatement', name, body };
|
|
1613
1786
|
}
|
|
1614
1787
|
|
|
1615
|
-
// v1.0.1
|
|
1788
|
+
// v1.0.1 - assertions: `check <a> (equals|is|contains|raises) <b>`
|
|
1616
1789
|
function parseCheckStatement() {
|
|
1617
1790
|
advance(); // check
|
|
1618
|
-
|
|
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();
|
|
1619
1795
|
const opToken = peek();
|
|
1620
1796
|
if (!['equals', 'is', 'contains', 'raises'].includes(opToken.value)) {
|
|
1621
1797
|
throw new Error(makeError(
|
|
@@ -1624,12 +1800,12 @@ function parseAsk() {
|
|
|
1624
1800
|
));
|
|
1625
1801
|
}
|
|
1626
1802
|
const op = advance().value;
|
|
1627
|
-
const b =
|
|
1803
|
+
const b = parseBooleanAtom();
|
|
1628
1804
|
return { type: 'CheckStatement', a, op, b };
|
|
1629
1805
|
}
|
|
1630
1806
|
|
|
1631
1807
|
// for each <item> in <collection> ... done
|
|
1632
|
-
// for every <item> in <collection> ... done (alias
|
|
1808
|
+
// for every <item> in <collection> ... done (alias - "every" maps to EACH token)
|
|
1633
1809
|
function parseForEach() {
|
|
1634
1810
|
consume(TOKEN.FOR);
|
|
1635
1811
|
|
|
@@ -1641,7 +1817,7 @@ function parseAsk() {
|
|
|
1641
1817
|
'Expected an index name after "for index".\n\nExample:\n for index i from 0 to 9\n show i\n done'
|
|
1642
1818
|
).value;
|
|
1643
1819
|
if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
|
|
1644
|
-
// for index <name> in <collection>
|
|
1820
|
+
// for index <name> in <collection> - zero-based index over a list.
|
|
1645
1821
|
advance(); // in
|
|
1646
1822
|
const collection = parseExpression();
|
|
1647
1823
|
const body = parseBody('"for index" loop');
|
|
@@ -1694,6 +1870,54 @@ function parseAsk() {
|
|
|
1694
1870
|
return { type: 'WhileStatement', condition, body };
|
|
1695
1871
|
}
|
|
1696
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
|
+
|
|
1697
1921
|
// Enterprise & Intent-Oriented Module Importing & Exporting
|
|
1698
1922
|
// bring circleArea from "./math.pln"
|
|
1699
1923
|
// bring circleArea and squareArea from "./math.pln"
|
|
@@ -1839,8 +2063,8 @@ function parseAsk() {
|
|
|
1839
2063
|
};
|
|
1840
2064
|
}
|
|
1841
2065
|
|
|
1842
|
-
// use <module>
|
|
1843
|
-
// 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
|
|
1844
2068
|
// <module> may carry a version range: use left-pad@^1.3.0
|
|
1845
2069
|
function parseUse() {
|
|
1846
2070
|
consume(TOKEN.USE);
|
|
@@ -1870,7 +2094,7 @@ function parseAsk() {
|
|
|
1870
2094
|
function parseWhen() {
|
|
1871
2095
|
consume(TOKEN.WHEN);
|
|
1872
2096
|
|
|
1873
|
-
// v2.3
|
|
2097
|
+
// v2.3 - "when <condition> ... done": English-like if statement.
|
|
1874
2098
|
// Detect: when <expr> is ... / when <expr> above ... etc.
|
|
1875
2099
|
// Must come after existing "when" event-handler checks.
|
|
1876
2100
|
// Check if next token starts a condition (not someone, not "nothing", not socket,
|
|
@@ -1882,7 +2106,7 @@ function parseAsk() {
|
|
|
1882
2106
|
(nextToken.type === TOKEN.IDENTIFIER && (nextToken.value === 'nothing' || nextToken.value === 'socket'));
|
|
1883
2107
|
|
|
1884
2108
|
if (!isEventWhen) {
|
|
1885
|
-
// v1.0.
|
|
2109
|
+
// v1.0.361 - when <target> "<event>" happens [as <name>] … done binds a
|
|
1886
2110
|
// DOM event listener: when button "click" happens => addEventListener.
|
|
1887
2111
|
// Detection probes the target expression and commits only when a string
|
|
1888
2112
|
// followed by "happens" is next; otherwise the tokens are replayed and
|
|
@@ -1903,7 +2127,7 @@ function parseAsk() {
|
|
|
1903
2127
|
return { type: 'WhenTargetedStatement', target, event: eventStr, paramName, body };
|
|
1904
2128
|
}
|
|
1905
2129
|
} catch (_e) {
|
|
1906
|
-
// Not a targeted "when"
|
|
2130
|
+
// Not a targeted "when" - replay and fall through to the condition form.
|
|
1907
2131
|
}
|
|
1908
2132
|
pos = savedPos;
|
|
1909
2133
|
|
|
@@ -1927,7 +2151,7 @@ function parseAsk() {
|
|
|
1927
2151
|
return { type: 'IfStatement', condition, consequent, alternate: null };
|
|
1928
2152
|
}
|
|
1929
2153
|
|
|
1930
|
-
// v2.1.1
|
|
2154
|
+
// v2.1.1 - "when nothing matches … done" registers the 404 catch-all.
|
|
1931
2155
|
// It must appear after every route in the source; the generated handler
|
|
1932
2156
|
// keeps that position.
|
|
1933
2157
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'nothing') {
|
|
@@ -1944,7 +2168,7 @@ function parseAsk() {
|
|
|
1944
2168
|
return { type: 'NotFoundStatement', body };
|
|
1945
2169
|
}
|
|
1946
2170
|
|
|
1947
|
-
// IOPL-native
|
|
2171
|
+
// IOPL-native - "when \"event\" happens as data … done": event listener.
|
|
1948
2172
|
// Must be checked before the "when someone" path.
|
|
1949
2173
|
if (peek().type === TOKEN.STRING &&
|
|
1950
2174
|
peekAt(1).type === TOKEN.HAPPENS) {
|
|
@@ -1960,7 +2184,7 @@ function parseAsk() {
|
|
|
1960
2184
|
return { type: 'WhenHappensStatement', event: eventStr, paramName, body };
|
|
1961
2185
|
}
|
|
1962
2186
|
|
|
1963
|
-
// v2.1.0
|
|
2187
|
+
// v2.1.0 - socket handlers inside a "websocket server" block come first:
|
|
1964
2188
|
// they read "when socket …", not "when someone …".
|
|
1965
2189
|
const whenToken = peek();
|
|
1966
2190
|
if (whenToken.type === TOKEN.IDENTIFIER && whenToken.value === 'socket') {
|
|
@@ -1989,7 +2213,7 @@ function parseAsk() {
|
|
|
1989
2213
|
));
|
|
1990
2214
|
}
|
|
1991
2215
|
|
|
1992
|
-
// v2.1.0
|
|
2216
|
+
// v2.1.0 - socket handlers inside a "websocket server" block:
|
|
1993
2217
|
// when socket connects … done
|
|
1994
2218
|
// when socket sends message … done
|
|
1995
2219
|
// when socket disconnects … done
|
|
@@ -2180,7 +2404,7 @@ function parseAsk() {
|
|
|
2180
2404
|
return { type: 'ServeFolderStatement', folder };
|
|
2181
2405
|
}
|
|
2182
2406
|
|
|
2183
|
-
// ── v0.6
|
|
2407
|
+
// ── v0.6 - Express DX ──────────────────────────────────────────────────────
|
|
2184
2408
|
|
|
2185
2409
|
// web app
|
|
2186
2410
|
function parseWebApp() {
|
|
@@ -2224,7 +2448,7 @@ function parseAsk() {
|
|
|
2224
2448
|
|
|
2225
2449
|
// bot "<token>" / bot env("BOT_TOKEN")
|
|
2226
2450
|
// Creates the polling Telegram bot and binds it to BOT (v1.2). The token
|
|
2227
|
-
// argument may be any expression
|
|
2451
|
+
// argument may be any expression - the token literal is never required to
|
|
2228
2452
|
// appear in source, and the runtime falls back to TELEGRAM_BOT_TOKEN.
|
|
2229
2453
|
function parseBot() {
|
|
2230
2454
|
advance(); // consume "bot"
|
|
@@ -2233,7 +2457,7 @@ function parseAsk() {
|
|
|
2233
2457
|
}
|
|
2234
2458
|
|
|
2235
2459
|
// start <port> / run on <port>
|
|
2236
|
-
// start telegram bot
|
|
2460
|
+
// start telegram bot - explicit Telegram startup marker. Polling keeps
|
|
2237
2461
|
// the bot alive, so this form is only for documentation/intent.
|
|
2238
2462
|
function parseStart() {
|
|
2239
2463
|
const isRunOn = peek().type === TOKEN.RUN_ON;
|
|
@@ -2258,14 +2482,14 @@ function parseAsk() {
|
|
|
2258
2482
|
return { type: 'StartStatement', port };
|
|
2259
2483
|
}
|
|
2260
2484
|
|
|
2261
|
-
// ── v0.6
|
|
2485
|
+
// ── v0.6 - SQLite DX ───────────────────────────────────────────────────────
|
|
2262
2486
|
|
|
2263
2487
|
// database "<file>" [using "<driver>"] / connect database "<file>" [using "<driver>"] / use database "<file>" [using "<driver>"]
|
|
2264
2488
|
//
|
|
2265
|
-
// v2.1.1
|
|
2489
|
+
// v2.1.1 - the optional driver selects the SQLite engine without changing
|
|
2266
2490
|
// any other PlainScript database semantics:
|
|
2267
|
-
// "native"
|
|
2268
|
-
// "wasm"
|
|
2491
|
+
// "native" - better-sqlite3 (requires a working native binding)
|
|
2492
|
+
// "wasm" - sql.js WebAssembly build (runs anywhere Node runs)
|
|
2269
2493
|
// The default ("auto") tries native first and falls back to the wasm
|
|
2270
2494
|
// engine when the native binding is unavailable on this platform.
|
|
2271
2495
|
function parseDatabase() {
|
|
@@ -2306,7 +2530,7 @@ function parseAsk() {
|
|
|
2306
2530
|
return { type: 'DatabaseStatement', file, driver };
|
|
2307
2531
|
}
|
|
2308
2532
|
|
|
2309
|
-
// v2.1.0
|
|
2533
|
+
// v2.1.0 - postgres "<connection-string>": binds the PostgreSQL pool to
|
|
2310
2534
|
// "db". SQL statements afterwards compile to async pool queries.
|
|
2311
2535
|
function parsePostgres() {
|
|
2312
2536
|
// "postgres" is contextual: only a declaration when followed by a value.
|
|
@@ -2315,7 +2539,7 @@ function parseAsk() {
|
|
|
2315
2539
|
return { type: 'PostgresStatement', connection };
|
|
2316
2540
|
}
|
|
2317
2541
|
|
|
2318
|
-
// v2.2.0
|
|
2542
|
+
// v2.2.0 - mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
|
|
2319
2543
|
// Usage: mongo "mongodb://localhost:27017" db "mydb"
|
|
2320
2544
|
function parseMongo() {
|
|
2321
2545
|
const token = advance(); // mongo or mongodb
|
|
@@ -2330,10 +2554,10 @@ function parseAsk() {
|
|
|
2330
2554
|
|
|
2331
2555
|
// query/insert/update/delete/execute SQL_BODY DONE
|
|
2332
2556
|
//
|
|
2333
|
-
// v2.1.0
|
|
2557
|
+
// v2.1.0 - the raw SQL may reference PlainScript variables with {name}
|
|
2334
2558
|
// placeholders. They are extracted at parse time and replaced: SQLite gets
|
|
2335
2559
|
// anonymous "?" markers, PostgreSQL numbered "$1…" markers (the generator
|
|
2336
|
-
// decides). Values are always passed as bound parameters
|
|
2560
|
+
// decides). Values are always passed as bound parameters - never spliced
|
|
2337
2561
|
// into the SQL text.
|
|
2338
2562
|
function parseSqlBlock(keyword, nodeType) {
|
|
2339
2563
|
advance(); // consume the keyword token (QUERY_KW, INSERT_KW, etc.)
|
|
@@ -2349,7 +2573,7 @@ function parseAsk() {
|
|
|
2349
2573
|
return { type: nodeType, ...extractSqlParams(rawSql) };
|
|
2350
2574
|
}
|
|
2351
2575
|
|
|
2352
|
-
// ── v2.1.1
|
|
2576
|
+
// ── v2.1.1 - error handling, retries and backend middleware ───────────────
|
|
2353
2577
|
|
|
2354
2578
|
function isHandledBy() {
|
|
2355
2579
|
return peek().type === TOKEN.IDENTIFIER && peek().value === 'handled' &&
|
|
@@ -2417,7 +2641,10 @@ function parseAsk() {
|
|
|
2417
2641
|
const stmt = parseStatement();
|
|
2418
2642
|
if (stmt) recoverBody.push(stmt);
|
|
2419
2643
|
}
|
|
2420
|
-
|
|
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();
|
|
2421
2648
|
catches.push({ param: paramName, errorType, body: recoverBody });
|
|
2422
2649
|
}
|
|
2423
2650
|
let finallyBody = null;
|
|
@@ -2436,7 +2663,7 @@ function parseAsk() {
|
|
|
2436
2663
|
}
|
|
2437
2664
|
advance(); // consume "done" that closes the finally block
|
|
2438
2665
|
}
|
|
2439
|
-
// No final "done" advance here
|
|
2666
|
+
// No final "done" advance here - the enclosing parseBody() consumes it
|
|
2440
2667
|
return { type: 'TryStatement', body: tryBody, catches, finallyBody };
|
|
2441
2668
|
}
|
|
2442
2669
|
|
|
@@ -2510,7 +2737,7 @@ function parseAsk() {
|
|
|
2510
2737
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'allow' && mimes === null) {
|
|
2511
2738
|
advance(); // allow
|
|
2512
2739
|
let elements;
|
|
2513
|
-
// v2.4
|
|
2740
|
+
// v2.4 - accept "list with" as alternative to bracket syntax
|
|
2514
2741
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'list' &&
|
|
2515
2742
|
peekAt(1).type === TOKEN.WITH) {
|
|
2516
2743
|
advance(); // "list"
|
|
@@ -2765,7 +2992,7 @@ function parseAsk() {
|
|
|
2765
2992
|
|
|
2766
2993
|
// ── Expressions ────────────────────────────────────────────────────────────
|
|
2767
2994
|
|
|
2768
|
-
// v2.1.1
|
|
2995
|
+
// v2.1.1 - full arithmetic precedence:
|
|
2769
2996
|
// nullish := additive ("??" additive)*
|
|
2770
2997
|
// additive := term (("+" | "-") term)*
|
|
2771
2998
|
// multiplicative ("term") := power (("*" | "/" | "%") power)*
|
|
@@ -2782,10 +3009,54 @@ function parseAsk() {
|
|
|
2782
3009
|
const alternate = parseExpression();
|
|
2783
3010
|
return { type: 'ConditionalExpression', condition, consequent, alternate };
|
|
2784
3011
|
}
|
|
2785
|
-
|
|
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
|
+
}
|
|
2786
3040
|
return left;
|
|
2787
3041
|
}
|
|
2788
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
|
+
|
|
2789
3060
|
function parseNullish() {
|
|
2790
3061
|
let left = parseAdditive();
|
|
2791
3062
|
while (peek().type === TOKEN.NULLISH_COALESCE) {
|
|
@@ -2816,7 +3087,7 @@ function parseAsk() {
|
|
|
2816
3087
|
peek().type === TOKEN.STAR ||
|
|
2817
3088
|
peek().type === TOKEN.SLASH ||
|
|
2818
3089
|
peek().type === TOKEN.PERCENT ||
|
|
2819
|
-
peek().type === TOKEN.TIMES_WORD ||
|
|
3090
|
+
(!_inRepeatCount && peek().type === TOKEN.TIMES_WORD) ||
|
|
2820
3091
|
peek().type === TOKEN.DIVIDED_BY_WORD
|
|
2821
3092
|
) {
|
|
2822
3093
|
const opToken = advance();
|
|
@@ -2855,7 +3126,7 @@ function parseAsk() {
|
|
|
2855
3126
|
advance();
|
|
2856
3127
|
return { type: 'AwaitExpression', value: parseUnary() };
|
|
2857
3128
|
}
|
|
2858
|
-
// v2.1.1
|
|
3129
|
+
// v2.1.1 - await semantics: "wait for <value>" awaits an async operation.
|
|
2859
3130
|
// The operand binds tightly (a full postfix chain), so
|
|
2860
3131
|
// "wait for loadUser(id) + 1" means "(await loadUser(id)) + 1".
|
|
2861
3132
|
// ("for" lexes as the FOR keyword, not an identifier.)
|
|
@@ -2883,9 +3154,77 @@ function parseAsk() {
|
|
|
2883
3154
|
return parsePrimary();
|
|
2884
3155
|
}
|
|
2885
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
|
+
|
|
2886
3221
|
// primary → itemExpr | atom (postfix)*
|
|
2887
3222
|
function parsePrimary() {
|
|
2888
|
-
//
|
|
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)
|
|
2889
3228
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'record' &&
|
|
2890
3229
|
peekAt(1).type === TOKEN.WITH) {
|
|
2891
3230
|
return parseRecordWith(true);
|
|
@@ -2926,7 +3265,7 @@ function parseAsk() {
|
|
|
2926
3265
|
advance(); // of
|
|
2927
3266
|
const left = parsePrimary();
|
|
2928
3267
|
if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) advance();
|
|
2929
|
-
const right =
|
|
3268
|
+
const right = parseBooleanAtom();
|
|
2930
3269
|
return { type: 'SetAlgebraExpression', op, left, right };
|
|
2931
3270
|
}
|
|
2932
3271
|
|
|
@@ -2940,7 +3279,7 @@ function parseAsk() {
|
|
|
2940
3279
|
return { type: 'CollectionReflectExpression', accessor, target };
|
|
2941
3280
|
}
|
|
2942
3281
|
|
|
2943
|
-
// v1.0.1
|
|
3282
|
+
// v1.0.1 - record constructor: `create a Person with name "Ada" and age 17`.
|
|
2944
3283
|
// `create` then an article ("a"/"an") then a kind name then "with" pairs.
|
|
2945
3284
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'create') {
|
|
2946
3285
|
const a1 = peekAt(1);
|
|
@@ -2954,7 +3293,7 @@ function parseAsk() {
|
|
|
2954
3293
|
}
|
|
2955
3294
|
}
|
|
2956
3295
|
|
|
2957
|
-
// v1.0.1
|
|
3296
|
+
// v1.0.1 - concurrency combinators: `all of [a(), b()]`, `any of [...]`,
|
|
2958
3297
|
// `settled of [...]`. Guard on the identifier + "of" lookahead so plain
|
|
2959
3298
|
// property reads like `all of the_list` still work as well as prose.
|
|
2960
3299
|
if (peek().type === TOKEN.IDENTIFIER &&
|
|
@@ -2966,7 +3305,7 @@ function parseAsk() {
|
|
|
2966
3305
|
return { type: 'ConcurrencyExpression', combo: comboKw, items };
|
|
2967
3306
|
}
|
|
2968
3307
|
|
|
2969
|
-
// v1.0.1
|
|
3308
|
+
// v1.0.1 - `spread of <collection>` unfolds an iterable into a new array.
|
|
2970
3309
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'spread' &&
|
|
2971
3310
|
peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
|
|
2972
3311
|
advance(); // spread
|
|
@@ -2975,9 +3314,18 @@ function parseAsk() {
|
|
|
2975
3314
|
return { type: 'SpreadExpression', collection };
|
|
2976
3315
|
}
|
|
2977
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
|
+
|
|
2978
3326
|
const item = tryParseItemExpression();
|
|
2979
3327
|
if (item) return item;
|
|
2980
|
-
// v1.0.
|
|
3328
|
+
// v1.0.361 - `new` works anywhere an expression is parsed (assignment
|
|
2981
3329
|
// targets, call arguments, member chains). "new" is not a keyword, so this
|
|
2982
3330
|
// identifier must be intercepted here; parseAtom would read it as a plain
|
|
2983
3331
|
// Identifier and turn "new THREE.Scene()" into "new.THREE.Scene()".
|
|
@@ -2997,7 +3345,7 @@ function parseAsk() {
|
|
|
2997
3345
|
node = { type: 'IndexExpression', object: node, index };
|
|
2998
3346
|
} else if (peek().type === TOKEN.DOT) {
|
|
2999
3347
|
advance();
|
|
3000
|
-
// After ".", any word is a valid JS property/method name
|
|
3348
|
+
// After ".", any word is a valid JS property/method name - including
|
|
3001
3349
|
// words that are PlainScript keywords in other contexts (e.g. crypto
|
|
3002
3350
|
// .update(...), obj .delete()). So accept any word token, not just an
|
|
3003
3351
|
// identifier.
|
|
@@ -3063,6 +3411,21 @@ function parseAsk() {
|
|
|
3063
3411
|
advance(); // position
|
|
3064
3412
|
const idx = consume(TOKEN.NUMBER, 'Expected a number after "at position".').value;
|
|
3065
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 };
|
|
3066
3429
|
} else {
|
|
3067
3430
|
break;
|
|
3068
3431
|
}
|
|
@@ -3070,7 +3433,7 @@ function parseAsk() {
|
|
|
3070
3433
|
return node;
|
|
3071
3434
|
}
|
|
3072
3435
|
|
|
3073
|
-
// v1.1
|
|
3436
|
+
// v1.1 - Item expressions, detected before parseAtom:
|
|
3074
3437
|
// first player from players → players[0]
|
|
3075
3438
|
// last player from players → players[players.length - 1]
|
|
3076
3439
|
// player one from players → players[0]
|
|
@@ -3083,10 +3446,18 @@ function parseAsk() {
|
|
|
3083
3446
|
if (second.type !== TOKEN.IDENTIFIER) return null;
|
|
3084
3447
|
if (third.type !== TOKEN.IDENTIFIER) return null;
|
|
3085
3448
|
|
|
3086
|
-
// v2.4
|
|
3449
|
+
// v2.4 - skip "different from" which is a comparison, not an item expression
|
|
3087
3450
|
if (second.value === 'different' && third.value === 'from') return null;
|
|
3088
3451
|
|
|
3089
|
-
// "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
|
|
3090
3461
|
if ((first.value === 'first' || first.value === 'last') && second.value === 'from') {
|
|
3091
3462
|
throw new Error(makeError(
|
|
3092
3463
|
`Expected a noun after "${first.value}".\n\nExample:\n ${first.value} player from players`,
|
|
@@ -3094,12 +3465,12 @@ function parseAsk() {
|
|
|
3094
3465
|
));
|
|
3095
3466
|
}
|
|
3096
3467
|
|
|
3097
|
-
if (third.value !== 'from') return null;
|
|
3468
|
+
if (third.value !== 'from' && third.value !== 'of') return null;
|
|
3098
3469
|
|
|
3099
3470
|
if (first.value === 'first') {
|
|
3100
3471
|
advance(); // first
|
|
3101
|
-
advance(); // noun
|
|
3102
|
-
advance(); // from
|
|
3472
|
+
advance(); // noun (e.g. item, player)
|
|
3473
|
+
advance(); // from / of
|
|
3103
3474
|
const collection = parsePrimary();
|
|
3104
3475
|
return { type: 'FirstItem', collection };
|
|
3105
3476
|
}
|
|
@@ -3107,7 +3478,7 @@ function parseAsk() {
|
|
|
3107
3478
|
if (first.value === 'last') {
|
|
3108
3479
|
advance(); // last
|
|
3109
3480
|
advance(); // noun
|
|
3110
|
-
advance(); // from
|
|
3481
|
+
advance(); // from / of
|
|
3111
3482
|
const collection = parsePrimary();
|
|
3112
3483
|
return { type: 'LastItem', collection };
|
|
3113
3484
|
}
|
|
@@ -3136,7 +3507,7 @@ function parseAsk() {
|
|
|
3136
3507
|
if (token.type === TOKEN.TEMPLATE_STRING) { advance(); return { type: 'TemplateLiteral', value: token.value }; }
|
|
3137
3508
|
if (token.type === TOKEN.NUMBER) { advance(); return { type: 'NumberLiteral', value: token.value }; }
|
|
3138
3509
|
if (token.type === TOKEN.BIGINT) { advance(); return { type: 'BigIntLiteral', value: token.value }; }
|
|
3139
|
-
// v2.1.1
|
|
3510
|
+
// v2.1.1 - boolean and null literals
|
|
3140
3511
|
if (token.type === TOKEN.TRUE_KW) { advance(); return { type: 'BooleanLiteral', value: true }; }
|
|
3141
3512
|
if (token.type === TOKEN.FALSE_KW) { advance(); return { type: 'BooleanLiteral', value: false }; }
|
|
3142
3513
|
if (token.type === TOKEN.NULL_KW) { advance(); return { type: 'NullLiteral' }; }
|
|
@@ -3159,7 +3530,7 @@ function parseAsk() {
|
|
|
3159
3530
|
// Bare symbol keyword - treat as Symbol() with no args
|
|
3160
3531
|
return { type: 'CallExpression', name: 'Symbol', args: [] };
|
|
3161
3532
|
}
|
|
3162
|
-
// v2.1.1
|
|
3533
|
+
// v2.1.1 - parenthesised grouping: (a + b) * c
|
|
3163
3534
|
if (token.type === TOKEN.LPAREN) {
|
|
3164
3535
|
advance();
|
|
3165
3536
|
const inner = parseExpression();
|
|
@@ -3169,7 +3540,7 @@ function parseAsk() {
|
|
|
3169
3540
|
if (token.type === TOKEN.LBRACKET) { return parseArrayLiteral(); }
|
|
3170
3541
|
if (token.type === TOKEN.LBRACE) { return parseInlineObjectLiteral(); }
|
|
3171
3542
|
|
|
3172
|
-
// v2.1.1
|
|
3543
|
+
// v2.1.1 - HTTP client prefix form:
|
|
3173
3544
|
// get "<url>" post urlExpr with <body>
|
|
3174
3545
|
// put/patch/delete … optional "headers { … }" and "timeout <ms>" clauses
|
|
3175
3546
|
// The call form get(...) stays an ordinary user/builtin call.
|
|
@@ -3224,7 +3595,7 @@ function parseAsk() {
|
|
|
3224
3595
|
return { type: 'Identifier', name: token.value };
|
|
3225
3596
|
}
|
|
3226
3597
|
|
|
3227
|
-
// v2.1.0
|
|
3598
|
+
// v2.1.0 - query("field") as a value: the HTTP query-string accessor.
|
|
3228
3599
|
// The SQLite block form only applies when "query" stands alone.
|
|
3229
3600
|
if (token.type === TOKEN.QUERY_KW && peekAt(1).type === TOKEN.LPAREN) {
|
|
3230
3601
|
return parseCallExpression();
|
|
@@ -3236,7 +3607,7 @@ function parseAsk() {
|
|
|
3236
3607
|
));
|
|
3237
3608
|
}
|
|
3238
3609
|
|
|
3239
|
-
// v2.1.1
|
|
3610
|
+
// v2.1.1 - "get", "post", "put", "patch" lex as identifiers; "delete" lexes
|
|
3240
3611
|
// as the SQL keyword token. All five introduce an HTTP request when followed
|
|
3241
3612
|
// by a URL value instead of "(".
|
|
3242
3613
|
function httpMethodWord(token) {
|
|
@@ -3255,7 +3626,7 @@ function parseAsk() {
|
|
|
3255
3626
|
].includes(token.type);
|
|
3256
3627
|
}
|
|
3257
3628
|
|
|
3258
|
-
// v2.1.1
|
|
3629
|
+
// v2.1.1 - parse one HTTP request expression after its method word.
|
|
3259
3630
|
//
|
|
3260
3631
|
// get <url> [headers <object>] [timeout <ms>]
|
|
3261
3632
|
// post|put|patch|delete <url> [with <body>] [headers <object>] [timeout <ms>]
|
|
@@ -3317,7 +3688,7 @@ function parseAsk() {
|
|
|
3317
3688
|
return { type: 'ArrayLiteral', elements };
|
|
3318
3689
|
}
|
|
3319
3690
|
|
|
3320
|
-
// v1.2
|
|
3691
|
+
// v1.2 - Inline object literal: { key: value, ... }
|
|
3321
3692
|
// If `isPattern` is true, allows shorthand { x, y } for destructuring
|
|
3322
3693
|
function parseInlineObjectLiteral(isPattern) {
|
|
3323
3694
|
consume(TOKEN.LBRACE);
|
|
@@ -3392,7 +3763,7 @@ function parseAsk() {
|
|
|
3392
3763
|
return { type: 'ObjectLiteral', properties };
|
|
3393
3764
|
}
|
|
3394
3765
|
|
|
3395
|
-
// v2.1.0
|
|
3766
|
+
// v2.1.0 - "key is value" pairs until "done", shared by the mail
|
|
3396
3767
|
// statements. Returns [{ key, value }] and consumes the closing DONE.
|
|
3397
3768
|
function parsePropertyList(contextName) {
|
|
3398
3769
|
const properties = [];
|
|
@@ -3403,7 +3774,7 @@ function parseAsk() {
|
|
|
3403
3774
|
peek()
|
|
3404
3775
|
));
|
|
3405
3776
|
}
|
|
3406
|
-
// v2.4
|
|
3777
|
+
// v2.4 - accept keyword tokens as property names (e.g., "to" in mail transport)
|
|
3407
3778
|
const keyToken = peek();
|
|
3408
3779
|
if (keyToken.type === TOKEN.IDENTIFIER || keyToken.type === TOKEN.TO ||
|
|
3409
3780
|
keyToken.type === TOKEN.FROM || keyToken.type === TOKEN.AND ||
|
|
@@ -3424,7 +3795,7 @@ function parseAsk() {
|
|
|
3424
3795
|
return properties;
|
|
3425
3796
|
}
|
|
3426
3797
|
|
|
3427
|
-
// v2.1.0
|
|
3798
|
+
// v2.1.0 - every <n> <unit>s … done: repeat work on an interval.
|
|
3428
3799
|
// The unit is resolved to milliseconds at parse time; the generator emits
|
|
3429
3800
|
// a plain setInterval with count * unit.
|
|
3430
3801
|
function parseEvery() {
|
|
@@ -3445,7 +3816,7 @@ function parseAsk() {
|
|
|
3445
3816
|
return { type: 'EveryStatement', count, unit, body };
|
|
3446
3817
|
}
|
|
3447
3818
|
|
|
3448
|
-
// v1.0.
|
|
3819
|
+
// v1.0.361 - every frame … done: a requestAnimationFrame loop whose body runs
|
|
3449
3820
|
// once per animation frame. The next frame is scheduled after the body so it
|
|
3450
3821
|
// always runs (t is the frame timestamp, like the DOM's rAF callback).
|
|
3451
3822
|
function parseEveryFrame() {
|
|
@@ -3455,7 +3826,7 @@ function parseAsk() {
|
|
|
3455
3826
|
return { type: 'EveryFrameStatement', body };
|
|
3456
3827
|
}
|
|
3457
3828
|
|
|
3458
|
-
// v1.0.
|
|
3829
|
+
// v1.0.361 - after <n> <unit>s … done: run work once after a delay.
|
|
3459
3830
|
// after 5 seconds
|
|
3460
3831
|
// show "butter!" ... done
|
|
3461
3832
|
// The delay is a number expression ("after count seconds"); the unit is
|
|
@@ -3475,7 +3846,7 @@ function parseAsk() {
|
|
|
3475
3846
|
return { type: 'AfterStatement', delay, unit, body };
|
|
3476
3847
|
}
|
|
3477
3848
|
|
|
3478
|
-
// v2.1.0
|
|
3849
|
+
// v2.1.0 - websocket server on <port> … done
|
|
3479
3850
|
//
|
|
3480
3851
|
// websocket server on 8081
|
|
3481
3852
|
// when socket connects … done
|
|
@@ -3526,12 +3897,12 @@ function parseAsk() {
|
|
|
3526
3897
|
return { type: 'WebSocketServerStatement', port, connectBody, messageBody, disconnectBody };
|
|
3527
3898
|
}
|
|
3528
3899
|
|
|
3529
|
-
// v2.1.1
|
|
3900
|
+
// v2.1.1 - whatsapp bot … done
|
|
3530
3901
|
//
|
|
3531
3902
|
// whatsapp bot
|
|
3532
3903
|
// auth "session" (optional; session folder name)
|
|
3533
|
-
// login qr
|
|
3534
|
-
// login pairing "2348012345678" (literal)
|
|
3904
|
+
// login qr - or -
|
|
3905
|
+
// login pairing "2348012345678" (literal) - or -
|
|
3535
3906
|
// login pairing phone (any value, e.g. from ask)
|
|
3536
3907
|
//
|
|
3537
3908
|
// on message
|
|
@@ -3562,9 +3933,9 @@ function parseAsk() {
|
|
|
3562
3933
|
));
|
|
3563
3934
|
}
|
|
3564
3935
|
|
|
3565
|
-
// use baileys "<pkg>"
|
|
3566
|
-
// Default is @
|
|
3567
|
-
// 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.
|
|
3568
3939
|
if (peek().type === TOKEN.USE && peekAt(1).value === 'baileys' && peekAt(2).type === TOKEN.STRING) {
|
|
3569
3940
|
advance(); // use
|
|
3570
3941
|
advance(); // baileys
|
|
@@ -3572,7 +3943,7 @@ function parseAsk() {
|
|
|
3572
3943
|
continue;
|
|
3573
3944
|
}
|
|
3574
3945
|
|
|
3575
|
-
// auth "<folder>"
|
|
3946
|
+
// auth "<folder>" - where WhatsApp session credentials persist.
|
|
3576
3947
|
if (peek().type === TOKEN.IDENTIFIER && peek().value === 'auth' &&
|
|
3577
3948
|
peekAt(1).type === TOKEN.STRING) {
|
|
3578
3949
|
advance(); // auth
|
|
@@ -3591,7 +3962,7 @@ function parseAsk() {
|
|
|
3591
3962
|
}
|
|
3592
3963
|
if (modeToken.type === TOKEN.IDENTIFIER && modeToken.value === 'pairing') {
|
|
3593
3964
|
advance(); // pairing
|
|
3594
|
-
// v2.1.2
|
|
3965
|
+
// v2.1.2 - the phone may be a string literal (validated here at
|
|
3595
3966
|
// compile time) or any PlainScript value, typically a variable filled by
|
|
3596
3967
|
// `ask`:
|
|
3597
3968
|
//
|
|
@@ -3616,7 +3987,7 @@ function parseAsk() {
|
|
|
3616
3987
|
));
|
|
3617
3988
|
}
|
|
3618
3989
|
|
|
3619
|
-
// on message … done
|
|
3990
|
+
// on message … done - the message handler.
|
|
3620
3991
|
if (peek().type === TOKEN.ON && peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'message') {
|
|
3621
3992
|
advance(); // on
|
|
3622
3993
|
advance(); // message
|
|
@@ -3626,7 +3997,7 @@ function parseAsk() {
|
|
|
3626
3997
|
}
|
|
3627
3998
|
|
|
3628
3999
|
throw new Error(makeError(
|
|
3629
|
-
'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',
|
|
3630
4001
|
peek()
|
|
3631
4002
|
));
|
|
3632
4003
|
}
|
|
@@ -3649,7 +4020,7 @@ function parseAsk() {
|
|
|
3649
4020
|
const cleaned = String(raw).replace(/[\s()+\-\.]/g, '');
|
|
3650
4021
|
if (!/^[0-9]+$/.test(cleaned)) {
|
|
3651
4022
|
throw new Error(makeError(
|
|
3652
|
-
`"${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"`,
|
|
3653
4024
|
token
|
|
3654
4025
|
));
|
|
3655
4026
|
}
|
|
@@ -3905,7 +4276,7 @@ function parseAsk() {
|
|
|
3905
4276
|
}
|
|
3906
4277
|
args.push(parseExpression());
|
|
3907
4278
|
}
|
|
3908
|
-
// v1.1
|
|
4279
|
+
// v1.1 - collection expressions: add(item to list) / remove(item from list) / write(data to "file")
|
|
3909
4280
|
if (args.length === 1 && (
|
|
3910
4281
|
(peek().type === TOKEN.IDENTIFIER && (peek().value === 'to' || peek().value === 'from')) ||
|
|
3911
4282
|
peek().type === TOKEN.TO)) {
|
|
@@ -3919,7 +4290,7 @@ function parseAsk() {
|
|
|
3919
4290
|
return { separator, args };
|
|
3920
4291
|
}
|
|
3921
4292
|
|
|
3922
|
-
// v1.1
|
|
4293
|
+
// v1.1 - Build a collection or I/O expression from a "to"/"from" special form.
|
|
3923
4294
|
function buildSpecialCall(name, separator, args, nameToken) {
|
|
3924
4295
|
if (separator === 'to' && name === 'add') {
|
|
3925
4296
|
return { type: 'AddCall', value: args[0], collection: args[1] };
|
|
@@ -4011,14 +4382,14 @@ function parseAsk() {
|
|
|
4011
4382
|
return { type: 'ClassDeclaration', name, superClass, body };
|
|
4012
4383
|
}
|
|
4013
4384
|
|
|
4014
|
-
// new ClassName(args)
|
|
4385
|
+
// new ClassName(args) - the constructor-call forms:
|
|
4015
4386
|
// new Foo → new Foo()
|
|
4016
4387
|
// new Foo(1, 2) → new Foo(1, 2)
|
|
4017
4388
|
// new window.Thing(1, 2) → new window.Thing(1, 2)
|
|
4018
4389
|
// new Foo().bar → new Foo().bar (postfix continues in parsePrimary)
|
|
4019
4390
|
// Called with the "new" word already consumed. The callee is parsed as a
|
|
4020
4391
|
// plain identifier plus a member chain so "new THREE.Scene(75)" binds the
|
|
4021
|
-
// argument list to the constructor
|
|
4392
|
+
// argument list to the constructor - not to a member call.
|
|
4022
4393
|
function parseNewExpressionCore() {
|
|
4023
4394
|
const callee = parseNewCallee();
|
|
4024
4395
|
let args = [];
|
|
@@ -4038,7 +4409,7 @@ function parseAsk() {
|
|
|
4038
4409
|
}
|
|
4039
4410
|
|
|
4040
4411
|
// The constructor name after "new": an identifier with an optional member
|
|
4041
|
-
// chain. Deliberately narrower than parsePrimary
|
|
4412
|
+
// chain. Deliberately narrower than parsePrimary - a call paren following
|
|
4042
4413
|
// the callee belongs to the constructor, so it is never consumed here.
|
|
4043
4414
|
function parseNewCallee() {
|
|
4044
4415
|
const token = peek();
|