plainscript-lang 1.0.3-beta → 1.0.3

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.
Files changed (73) hide show
  1. package/README.md +97 -120
  2. package/compiler/bundler.js +83 -12
  3. package/compiler/cli.js +62 -11
  4. package/compiler/dependency-detector.js +11 -0
  5. package/compiler/generator.js +280 -15
  6. package/compiler/lexer.js +2 -0
  7. package/compiler/parser.js +410 -28
  8. package/compiler/sourcemap.js +142 -0
  9. package/compiler/version.js +1 -1
  10. package/docs/CAPABILITY_GAP_AUDIT.md +17 -15
  11. package/docs/PLAINSCRIPT-SPEC.md +41 -39
  12. package/docs/index.html +60 -33
  13. package/docs/website/script.js +168 -19
  14. package/docs/website/style.css +187 -41
  15. package/examples/ai-providers.pln +1 -1
  16. package/examples/arrays.pln +1 -1
  17. package/examples/async-errors.pln +1 -1
  18. package/examples/auth-api.pln +1 -1
  19. package/examples/auth-sessions.pln +1 -1
  20. package/examples/basics.pln +1 -1
  21. package/examples/bots.pln +1 -1
  22. package/examples/cache-and-jobs.pln +1 -1
  23. package/examples/cache-schedule.pln +1 -1
  24. package/examples/collections.pln +1 -1
  25. package/examples/concurrency.pln +1 -1
  26. package/examples/conditions.pln +1 -1
  27. package/examples/database.pln +1 -1
  28. package/examples/day2.pln +1 -1
  29. package/examples/day3.pln +1 -1
  30. package/examples/deployment.pln +1 -1
  31. package/examples/email-send.pln +1 -1
  32. package/examples/error-handling.pln +1 -1
  33. package/examples/expressions.pln +1 -1
  34. package/examples/file-uploads.pln +1 -1
  35. package/examples/files-and-config.pln +1 -1
  36. package/examples/files.pln +1 -1
  37. package/examples/football-backend/app.pln +1 -1
  38. package/examples/functions.pln +1 -1
  39. package/examples/hello.pln +1 -1
  40. package/examples/http-client.pln +1 -1
  41. package/examples/http.pln +1 -1
  42. package/examples/id-verification/app.pln +1 -1
  43. package/examples/json.pln +1 -1
  44. package/examples/loops.pln +1 -1
  45. package/examples/modules/app.pln +1 -1
  46. package/examples/modules/main.pln +1 -1
  47. package/examples/modules/math.pln +1 -1
  48. package/examples/mongo.pln +33 -0
  49. package/examples/objects.pln +1 -1
  50. package/examples/ocr.pln +1 -1
  51. package/examples/packages.pln +1 -1
  52. package/examples/record-kinds.pln +1 -1
  53. package/examples/records.pln +1 -1
  54. package/examples/rest-api-fetch.pln +1 -1
  55. package/examples/rest-api.pln +1 -1
  56. package/examples/scheduled-tasks.pln +1 -1
  57. package/examples/search-and-filter.pln +1 -1
  58. package/examples/server.pln +1 -1
  59. package/examples/sessions-and-cookies.pln +1 -1
  60. package/examples/sqlite.pln +1 -1
  61. package/examples/start.pln +1 -1
  62. package/examples/stdlib.pln +1 -1
  63. package/examples/string-verbs.pln +16 -0
  64. package/examples/telegram-ai-bot.pln +1 -1
  65. package/examples/telegram-bot.pln +1 -1
  66. package/examples/testing.pln +1 -1
  67. package/examples/web-api.pln +1 -1
  68. package/examples/web-app.pln +1 -1
  69. package/examples/websocket-server.pln +1 -1
  70. package/examples/websocket.pln +1 -1
  71. package/examples/whatsapp-bot/pairing.pln +1 -1
  72. package/examples/whatsapp-bot/qr.pln +1 -1
  73. package/package.json +3 -2
@@ -347,6 +347,18 @@ function parse(tokens) {
347
347
 
348
348
  function parseStatement() {
349
349
  const token = peek();
350
+ const startLine = token ? token.line : undefined;
351
+ const startCol = token ? token.col : undefined;
352
+ const node = parseStatementCore();
353
+ if (node && typeof node === 'object' && node.line === undefined && startLine !== undefined) {
354
+ node.line = startLine;
355
+ node.col = startCol;
356
+ }
357
+ return node;
358
+ }
359
+
360
+ function parseStatementCore() {
361
+ const token = peek();
350
362
 
351
363
  // "define a kind called ..." - must check before DEFINE token dispatch
352
364
  if ((token.type === TOKEN.DEFINE || (token.type === TOKEN.IDENTIFIER && token.value === 'define')) &&
@@ -357,7 +369,7 @@ function parse(tokens) {
357
369
  // "load env file" - must check before LOAD token dispatch
358
370
  if ((token.type === TOKEN.LOAD || (token.type === TOKEN.IDENTIFIER && token.value === 'load')) &&
359
371
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'env' &&
360
- peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'file') {
372
+ (peekAt(2).type === TOKEN.IDENTIFIER || peekAt(2).type === TOKEN.FILE_KW) && peekAt(2).value === 'file') {
361
373
  advance(); // load
362
374
  advance(); // env
363
375
  advance(); // file
@@ -391,15 +403,18 @@ function parse(tokens) {
391
403
  }
392
404
  if (token.type === TOKEN.WHILE) return parseWhile();
393
405
  if (token.type === TOKEN.USE) return parseUse();
394
- if (token.type === TOKEN.IMPORT) return parseImport();
395
- if (token.type === TOKEN.INCLUDE) return parseImport(); // alias for import
396
- if (token.type === TOKEN.LOAD) return parseImport(); // alias for import
406
+ if (token.type === TOKEN.IMPORT || token.type === TOKEN.INCLUDE || token.type === TOKEN.LOAD ||
407
+ (token.type === TOKEN.IDENTIFIER && token.value === 'bring')) {
408
+ return parseImport();
409
+ }
397
410
  if (token.type === TOKEN.WHEN) return parseWhen();
398
411
  // v2.3 — English-like function declaration: "to add a and b together ... done"
399
412
  if (token.type === TOKEN.TO && peekAt(1).type === TOKEN.IDENTIFIER) return parseToFunction();
400
413
  // v2.4 — "list with" and "record with" as expressions
401
414
  if (token.type === TOKEN.IDENTIFIER && token.value === 'list' && peekAt(1).type === TOKEN.WITH) return parseListWith();
402
415
  if (token.type === TOKEN.IDENTIFIER && token.value === 'record' && peekAt(1).type === TOKEN.WITH) return parseRecordWith(false);
416
+ if (token.type === TOKEN.IDENTIFIER && token.value === 'put' && peekAt(1).type !== TOKEN.LPAREN) return parsePutStatement();
417
+ if (token.type === TOKEN.IDENTIFIER && token.value === 'unpack') return parseUnpackStatement();
403
418
  if (token.type === TOKEN.LIST_WITH) return parseListWith();
404
419
  if (token.type === TOKEN.RECORD_WITH) return parseRecordWith(false);
405
420
  if (token.type === TOKEN.LISTEN) return parseListen();
@@ -462,6 +477,54 @@ function parse(tokens) {
462
477
 
463
478
  // Statements starting with an identifier: call, becomes, index/member becomes
464
479
  if (token.type === TOKEN.IDENTIFIER) {
480
+ // v2.5 — natural string and collection verbs that transform a variable
481
+ // in place: "lowercase title", "uppercase first letter of each word in title",
482
+ // "split title by \" \"", "join title by \" \"", "trim title".
483
+ const TRANSFORM_VERB = token.value === 'lowercase' || token.value === 'uppercase' ||
484
+ token.value === 'trim' || token.value === 'split' || token.value === 'join';
485
+ if (TRANSFORM_VERB && peekAt(1).type === TOKEN.IDENTIFIER) {
486
+ // "uppercase first letter of each word in <target>"
487
+ if (token.value === 'uppercase' &&
488
+ peekAt(1).value === 'first' && peekAt(2).value === 'letter' &&
489
+ peekAt(3).value === 'of' && peekAt(4).type === TOKEN.EACH && peekAt(4).value === 'each' &&
490
+ peekAt(5).value === 'word') {
491
+ for (let t = 0; t < 6; t++) advance(); // uppercase first letter of each word
492
+ const connector = peek();
493
+ if (connector.type !== TOKEN.IN && !(connector.type === TOKEN.IDENTIFIER && connector.value === 'in')) {
494
+ throw new Error(makeError(
495
+ 'Expected "in <name>" after "uppercase first letter of each word".\n\nExample:\n uppercase first letter of each word in title',
496
+ connector
497
+ ));
498
+ }
499
+ advance(); // in
500
+ const target = peek();
501
+ if (target.type !== TOKEN.IDENTIFIER) {
502
+ throw new Error(makeError('Expected a variable name after "in".', target));
503
+ }
504
+ advance();
505
+ return { type: 'CapitalizeWordsStatement', target: target.value };
506
+ }
507
+
508
+ // "lowercase title" / "uppercase title" / "trim title"
509
+ if (token.value === 'lowercase' || token.value === 'uppercase' || token.value === 'trim') {
510
+ const kind = token.value;
511
+ advance(); // verb
512
+ const target = advance().value;
513
+ return { type: 'StringTransformStatement', kind, target };
514
+ }
515
+
516
+ // "split title by <sep>" / "join list by <sep>"
517
+ if ((token.value === 'split' || token.value === 'join') &&
518
+ peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'by') {
519
+ const kind = token.value;
520
+ advance(); // verb
521
+ const target = advance().value;
522
+ advance(); // by
523
+ const separator = parseExpression();
524
+ return { type: kind === 'split' ? 'SplitStatement' : 'JoinStatement', target, separator };
525
+ }
526
+ }
527
+
465
528
  // switch ... against ... done
466
529
  if (token.value === 'switch' && (peekAt(1).type === TOKEN.IDENTIFIER || tokenStartsValue(peekAt(1)))) {
467
530
  return parseSwitchStatement();
@@ -525,6 +588,15 @@ function parse(tokens) {
525
588
  return parsePostgres();
526
589
  }
527
590
 
591
+ // v2.2.0 — mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
592
+ // Contextual: a variable named "mongo" or "mongodb" keeps its meaning unless
593
+ // a connection string follows on the same line.
594
+ if ((token.value === 'mongo' || token.value === 'mongodb') && (
595
+ peekAt(1).type === TOKEN.STRING || peekAt(1).type === TOKEN.IDENTIFIER ||
596
+ peekAt(1).type === TOKEN.LPAREN || peekAt(1).type === TOKEN.TEMPLATE_STRING)) {
597
+ return parseMongo();
598
+ }
599
+
528
600
  // v2.1.0 — cache "<redis-url>" / cache env("REDIS_URL"): connects the
529
601
  // shared Redis client used by cacheGet / cacheSet / cacheDelete.
530
602
  // Contextual like "postgres": a variable named "cache" keeps its
@@ -631,6 +703,16 @@ function parse(tokens) {
631
703
  return { type: 'BroadcastStatement', value };
632
704
  }
633
705
 
706
+ // v2.2.0 — send to <connection> <message>: send to a specific stored connection.
707
+ if (token.value === 'send' &&
708
+ (peekAt(1).type === TOKEN.TO || (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'to'))) {
709
+ advance(); // send
710
+ advance(); // to
711
+ const connection = parseExpression();
712
+ const value = parseExpression();
713
+ return { type: 'SendToStatement', connection, value };
714
+ }
715
+
634
716
  // v2.1.0 — send socket <expr>: replies to one connected socket.
635
717
  if (token.value === 'send' &&
636
718
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'socket') {
@@ -749,7 +831,7 @@ function parse(tokens) {
749
831
  // load env file "<path>" → apply .env KEY=VALUE pairs to process.env
750
832
  if ((token.type === TOKEN.LOAD || (token.type === TOKEN.IDENTIFIER && token.value === 'load')) &&
751
833
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'env' &&
752
- peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'file') {
834
+ (peekAt(2).type === TOKEN.IDENTIFIER || peekAt(2).type === TOKEN.FILE_KW) && peekAt(2).value === 'file') {
753
835
  advance(); // load
754
836
  advance(); // env
755
837
  advance(); // file
@@ -768,13 +850,11 @@ function parse(tokens) {
768
850
  return parseCheckStatement();
769
851
  }
770
852
 
771
- // export <name> → mark a top-level symbol for module.exports
772
- if (token.value === 'export' &&
773
- peekAt(1).type === TOKEN.IDENTIFIER &&
774
- peekAt(2).type !== TOKEN.LPAREN) {
775
- advance(); // export
776
- const name = advance().value;
777
- return { type: 'ExportStatement', name };
853
+ // export / share / expose → mark top-level symbol(s) or re-export from module
854
+ if ((token.value === 'export' || token.value === 'share' || token.value === 'expose' ||
855
+ token.type === TOKEN.SHARE || token.type === TOKEN.EXPOSE) &&
856
+ peekAt(1).type !== TOKEN.LPAREN && peekAt(1).type !== TOKEN.BECOMES) {
857
+ return parseExport();
778
858
  }
779
859
 
780
860
  // throw <value> → raise an error for the caller's try/recover to catch.
@@ -890,9 +970,10 @@ function parse(tokens) {
890
970
  target = parseInlineObjectLiteral(true); // returns InlineObjectLiteral with properties, destructuring mode
891
971
  target.type = 'ObjectPattern';
892
972
  } else {
893
- // Allow "back" (TOKEN.BACK) as a variable name (it's a keyword for "give back" but valid as identifier)
973
+ // Allow contextual keywords as variable names in remember declarations
894
974
  let nameToken = peek();
895
- if (nameToken.type === TOKEN.BACK) {
975
+ const REMEMBER_KEYWORDS = new Set([TOKEN.BACK, TOKEN.TOTAL, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
976
+ if (REMEMBER_KEYWORDS.has(nameToken.type)) {
896
977
  advance();
897
978
  target = nameToken.value;
898
979
  } else {
@@ -903,6 +984,12 @@ function parse(tokens) {
903
984
  }
904
985
  }
905
986
 
987
+ // Object literal: next token is IDENTIFIER (except dictionary/map) followed by IS or BE
988
+ if (peek().type === TOKEN.IDENTIFIER && peek().value !== 'dictionary' && peek().value !== 'map' && (peekAt(1).type === TOKEN.IS || peekAt(1).type === TOKEN.BE)) {
989
+ const init = parseInlineObjectLiteral(false);
990
+ return { type: 'VariableDeclaration', name: target, initializer: init, isLet, isConstant: !isLet };
991
+ }
992
+
906
993
  // "let" uses "is" or "be" for simple vars, but also accepts "as" for both simple and destructuring
907
994
  // "remember" always uses "as"
908
995
  const isDestructuring = target && typeof target === 'object' && (target.type === 'ArrayPattern' || target.type === 'ObjectPattern');
@@ -1169,6 +1256,122 @@ function parseAsk() {
1169
1256
  return { type: 'InlineObjectLiteral', properties };
1170
1257
  }
1171
1258
 
1259
+ function parseDictionaryWith() {
1260
+ advance(); // dictionary or map
1261
+ consume(TOKEN.WITH, 'Expected "with" after dictionary/map.');
1262
+ const pairs = [];
1263
+ if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
1264
+ while (true) {
1265
+ if (peek().type === TOKEN.DONE || peek().value === 'done') break;
1266
+ const key = parseExpression();
1267
+ consume(TOKEN.IS, 'Expected "is" between key and value in dictionary/map.');
1268
+ const value = parseExpression();
1269
+ pairs.push({ key, value });
1270
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
1271
+ advance();
1272
+ continue;
1273
+ }
1274
+ break;
1275
+ }
1276
+ }
1277
+ if (peek().type === TOKEN.DONE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'done')) {
1278
+ advance(); // done
1279
+ }
1280
+ return { type: 'DictionaryLiteral', pairs };
1281
+ }
1282
+
1283
+ function parseSetWith() {
1284
+ advance(); // set
1285
+ consume(TOKEN.WITH, 'Expected "with" after "set".');
1286
+ const elements = [];
1287
+ if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
1288
+ while (true) {
1289
+ if (peek().type === TOKEN.DONE || peek().value === 'done') break;
1290
+ elements.push(parseExpression());
1291
+ if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
1292
+ advance();
1293
+ continue;
1294
+ }
1295
+ break;
1296
+ }
1297
+ }
1298
+ if (peek().type === TOKEN.DONE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'done')) {
1299
+ advance(); // done
1300
+ }
1301
+ return { type: 'SetLiteral', elements };
1302
+ }
1303
+
1304
+ function parseTupleWith() {
1305
+ advance(); // tuple
1306
+ consume(TOKEN.WITH, 'Expected "with" after "tuple".');
1307
+ const elements = [];
1308
+ if (peek().type !== TOKEN.DONE && peek().value !== 'done') {
1309
+ while (true) {
1310
+ if (peek().type === TOKEN.DONE || peek().value === 'done') break;
1311
+ elements.push(parseExpression());
1312
+ if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
1313
+ advance();
1314
+ continue;
1315
+ }
1316
+ break;
1317
+ }
1318
+ }
1319
+ if (peek().type === TOKEN.DONE || (peek().type === TOKEN.IDENTIFIER && peek().value === 'done')) {
1320
+ advance(); // done
1321
+ }
1322
+ return { type: 'TupleLiteral', elements };
1323
+ }
1324
+
1325
+ function parseTupleCall() {
1326
+ advance(); // tuple
1327
+ consume(TOKEN.LPAREN, 'Expected "(" after tuple.');
1328
+ const elements = [];
1329
+ if (peek().type !== TOKEN.RPAREN) {
1330
+ while (true) {
1331
+ elements.push(parseExpression());
1332
+ if (peek().type === TOKEN.COMMA) { advance(); continue; }
1333
+ break;
1334
+ }
1335
+ }
1336
+ consume(TOKEN.RPAREN, 'Expected ")" to close tuple.');
1337
+ return { type: 'TupleLiteral', elements };
1338
+ }
1339
+
1340
+ function parsePutStatement() {
1341
+ advance(); // put
1342
+ const key = parseNullish();
1343
+ consume(TOKEN.AS, 'Expected "as" between key and value in "put".');
1344
+ const value = parseNullish();
1345
+ if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
1346
+ advance(); // in
1347
+ } else {
1348
+ consume(TOKEN.IN, 'Expected "in" before map variable in "put".');
1349
+ }
1350
+ const mapVar = parsePrimary();
1351
+ return { type: 'PutStatement', mapVar, key, value };
1352
+ }
1353
+
1354
+ function parseUnpackStatement() {
1355
+ advance(); // unpack
1356
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'tuple') {
1357
+ advance(); // tuple
1358
+ }
1359
+ const tupleExpr = parseExpression();
1360
+ if (peek().type === TOKEN.INTO || (peek().type === TOKEN.IDENTIFIER && peek().value === 'into')) {
1361
+ advance(); // into
1362
+ }
1363
+ const variables = [];
1364
+ while (peek().type === TOKEN.IDENTIFIER) {
1365
+ variables.push(advance().value);
1366
+ if (peek().type === TOKEN.COMMA || peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) {
1367
+ advance();
1368
+ continue;
1369
+ }
1370
+ break;
1371
+ }
1372
+ return { type: 'UnpackStatement', tupleExpr, variables };
1373
+ }
1374
+
1172
1375
  function parseParamList() {
1173
1376
  const params = [];
1174
1377
  if (peek().type === TOKEN.RPAREN) return params;
@@ -1202,7 +1405,15 @@ function parseAsk() {
1202
1405
  return pattern;
1203
1406
  }
1204
1407
  // Regular parameter
1205
- const name = consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value;
1408
+ // Contextual keywords (back/reply/respond/send back/file) are valid
1409
+ // identifier names when used as a parameter, mirroring `remember`.
1410
+ const PARAM_KEYWORDS = new Set([TOKEN.BACK, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
1411
+ let name;
1412
+ if (PARAM_KEYWORDS.has(peek().type)) {
1413
+ name = advance().value;
1414
+ } else {
1415
+ name = consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value;
1416
+ }
1206
1417
  const param = { name };
1207
1418
  if (peek().type === TOKEN.AS) {
1208
1419
  advance(); // as
@@ -1429,13 +1640,19 @@ function parseAsk() {
1429
1640
  return { type: 'WhileStatement', condition, body };
1430
1641
  }
1431
1642
 
1432
- // import "./file.pln"
1433
- // import { name1, name2 } from "./file.pln" (named imports — binds only the
1434
- // listed symbols; the whole file is still bundled as a dependency)
1643
+ // Enterprise & Intent-Oriented Module Importing & Exporting
1644
+ // bring circleArea from "./math.pln"
1645
+ // bring circleArea and squareArea from "./math.pln"
1646
+ // bring all from "./math.pln" as math
1647
+ // bring express from "express"
1648
+ // bring { get, post } from "axios"
1649
+ // import { circleArea, squareArea } from "./math.pln"
1650
+ // import all as math from "./math.pln"
1651
+ // import "./file.pln"
1435
1652
  function parseImport() {
1436
- consume(TOKEN.IMPORT);
1653
+ advance(); // consume keyword (import / bring / include / load)
1437
1654
 
1438
- // Named-import form: import { a, b } from "./file.pln"
1655
+ // Form 1: import { a, b } from "path"
1439
1656
  if (peek().type === TOKEN.LBRACE) {
1440
1657
  advance(); // {
1441
1658
  const names = [];
@@ -1447,12 +1664,8 @@ function parseAsk() {
1447
1664
  if (peek().type === TOKEN.COMMA) { advance(); continue; }
1448
1665
  break;
1449
1666
  }
1450
- consume(TOKEN.RBRACE,
1451
- 'Expected "}" to close the import braces.\n\nExample:\n import { helper } from "./util.pln"');
1452
- // The "from" is a plain identifier; tolerate its optional presence so
1453
- // both "import { x } from "./f.pln"" and "import { x } "./f.pln"" read
1454
- // naturally, but require it for the documented form.
1455
- if (peek().type === TOKEN.IDENTIFIER && peek().value === 'from') {
1667
+ consume(TOKEN.RBRACE, 'Expected "}" to close the import braces.');
1668
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1456
1669
  advance(); // from
1457
1670
  }
1458
1671
  const filePath = consume(
@@ -1462,7 +1675,50 @@ function parseAsk() {
1462
1675
  return { type: 'ImportStatement', path: filePath, names };
1463
1676
  }
1464
1677
 
1465
- // Whole-module form: import "./file.pln"
1678
+ // Form 2: bring all from "path" as math / import all as math from "path" / import * as math from "path"
1679
+ if (peek().type === TOKEN.ALL || (peek().type === TOKEN.IDENTIFIER && peek().value === 'all') || peek().type === TOKEN.TIMES) {
1680
+ advance(); // all or *
1681
+ let namespace = null;
1682
+ if (peek().type === TOKEN.AS) {
1683
+ advance(); // as
1684
+ namespace = consume(TOKEN.IDENTIFIER, 'Expected a namespace identifier after "as".').value;
1685
+ }
1686
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1687
+ advance(); // from
1688
+ }
1689
+ const filePath = consume(TOKEN.STRING, 'Expected a file path or package string after "from".').value;
1690
+ if (peek().type === TOKEN.AS && !namespace) {
1691
+ advance(); // as
1692
+ namespace = consume(TOKEN.IDENTIFIER, 'Expected a namespace identifier after "as".').value;
1693
+ }
1694
+ return { type: 'ImportStatement', path: filePath, namespace };
1695
+ }
1696
+
1697
+ // Form 3: bring symbol from "path" / bring a and b from "path" / bring express from "express"
1698
+ if ((peek().type === TOKEN.IDENTIFIER || peek().type === TOKEN.PACKAGE) && peekAt(1).type !== TOKEN.STRING) {
1699
+ const names = [];
1700
+ const importLine = peek().line;
1701
+ let defaultImport = null;
1702
+ while ((peek().type === TOKEN.IDENTIFIER || peek().type === TOKEN.PACKAGE) && peek().line === importLine) {
1703
+ if (peek().type === TOKEN.FROM || peek().value === 'from') break;
1704
+ names.push(advance().value);
1705
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
1706
+ advance();
1707
+ }
1708
+ }
1709
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1710
+ advance(); // from
1711
+ }
1712
+ if (peek().type === TOKEN.STRING) {
1713
+ const filePath = advance().value;
1714
+ if (names.length === 1) {
1715
+ defaultImport = names[0];
1716
+ }
1717
+ return { type: 'ImportStatement', path: filePath, names, defaultImport };
1718
+ }
1719
+ }
1720
+
1721
+ // Form 4: Whole-module form: import "./file.pln"
1466
1722
  const filePath = consume(
1467
1723
  TOKEN.STRING,
1468
1724
  'Expected a file path string after "import".\n\nExample:\n import "./math.pln"'
@@ -1470,6 +1726,65 @@ function parseAsk() {
1470
1726
  return { type: 'ImportStatement', path: filePath };
1471
1727
  }
1472
1728
 
1729
+ // Enterprise & Intent-Oriented Exporting
1730
+ // export circleArea
1731
+ // export circleArea and squareArea
1732
+ // export { circleArea, squareArea }
1733
+ // export all from "./math.pln"
1734
+ // export circleArea and squareArea from "./math.pln"
1735
+ function parseExport() {
1736
+ advance(); // export / share / expose
1737
+
1738
+ // Form 1: export all from "./math.pln" / export * from "./math.pln"
1739
+ if (peek().type === TOKEN.ALL || (peek().type === TOKEN.IDENTIFIER && peek().value === 'all') || peek().type === TOKEN.TIMES) {
1740
+ advance(); // all or *
1741
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1742
+ advance(); // from
1743
+ }
1744
+ const fromPath = consume(TOKEN.STRING, 'Expected a file path string after "from".').value;
1745
+ return { type: 'ExportStatement', exportAll: true, fromPath };
1746
+ }
1747
+
1748
+ // Form 2: export { a, b } [from "./math.pln"]
1749
+ if (peek().type === TOKEN.LBRACE) {
1750
+ advance(); // {
1751
+ const names = [];
1752
+ while (true) {
1753
+ names.push(consume(TOKEN.IDENTIFIER, 'Expected an identifier inside export braces.').value);
1754
+ if (peek().type === TOKEN.COMMA) { advance(); continue; }
1755
+ break;
1756
+ }
1757
+ consume(TOKEN.RBRACE, 'Expected "}" to close export braces.');
1758
+ let fromPath = null;
1759
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1760
+ advance(); // from
1761
+ fromPath = consume(TOKEN.STRING, 'Expected a file path string after "from".').value;
1762
+ }
1763
+ return { type: 'ExportStatement', names, fromPath };
1764
+ }
1765
+
1766
+ // Form 3: export a and b [from "./math.pln"] OR export a
1767
+ const names = [];
1768
+ const exportLine = peek().line;
1769
+ while (peek().type === TOKEN.IDENTIFIER && peek().value !== 'from' && peek().value !== 'export' && peek().value !== 'share' && peek().value !== 'expose' && peek().line === exportLine) {
1770
+ names.push(advance().value);
1771
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and') || peek().type === TOKEN.COMMA) {
1772
+ advance();
1773
+ }
1774
+ }
1775
+ let fromPath = null;
1776
+ if (peek().type === TOKEN.FROM || (peek().type === TOKEN.IDENTIFIER && peek().value === 'from')) {
1777
+ advance(); // from
1778
+ fromPath = consume(TOKEN.STRING, 'Expected a file path string after "from".').value;
1779
+ }
1780
+ return {
1781
+ type: 'ExportStatement',
1782
+ name: names.length === 1 ? names[0] : null,
1783
+ names: names.length > 0 ? names : null,
1784
+ fromPath,
1785
+ };
1786
+ }
1787
+
1473
1788
  // use <module> — side-effect or canonical binding
1474
1789
  // use <module> as <name> — bind the package to a custom variable name
1475
1790
  // <module> may carry a version range: use left-pad@^1.3.0
@@ -1686,6 +2001,7 @@ function parseAsk() {
1686
2001
 
1687
2002
  // reply <expr> / respond <expr> / send back <expr>
1688
2003
  // reply json\n <key> is <val>\n...\ndone
2004
+ // reply file "<path>"
1689
2005
  function parseReply() {
1690
2006
  const isRespond = peek().type === TOKEN.RESPOND;
1691
2007
  const isSendBack = peek().type === TOKEN.SEND_BACK;
@@ -1714,6 +2030,12 @@ function parseAsk() {
1714
2030
  advance(); // consume DONE
1715
2031
  return { type: 'ReplyJsonStatement', properties };
1716
2032
  }
2033
+ if (peek().type === TOKEN.FILE_KW) {
2034
+ advance(); // consume file
2035
+ const filePath = consume(TOKEN.STRING,
2036
+ 'Expected a file path string after "reply file".\n\nExample:\n route get "/"\n reply file "public/index.html"\n done').value;
2037
+ return { type: 'ReplyFileStatement', filePath };
2038
+ }
1717
2039
  const value = parseExpression();
1718
2040
 
1719
2041
  // reply <value> with buttons ... done → Telegram inline keyboard block.
@@ -1914,6 +2236,19 @@ function parseAsk() {
1914
2236
  return { type: 'PostgresStatement', connection };
1915
2237
  }
1916
2238
 
2239
+ // v2.2.0 — mongo "<connection-string>" [db "<name>"]: binds the MongoDB client.
2240
+ // Usage: mongo "mongodb://localhost:27017" db "mydb"
2241
+ function parseMongo() {
2242
+ const token = advance(); // mongo or mongodb
2243
+ const connection = parseExpression();
2244
+ let dbName = null;
2245
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'db') {
2246
+ advance(); // db
2247
+ dbName = parseExpression();
2248
+ }
2249
+ return { type: 'MongoStatement', connection, dbName };
2250
+ }
2251
+
1917
2252
  // query/insert/update/delete/execute SQL_BODY DONE
1918
2253
  //
1919
2254
  // v2.1.0 — the raw SQL may reference PlainScript variables with {name}
@@ -2480,6 +2815,51 @@ function parseAsk() {
2480
2815
  peekAt(1).type === TOKEN.WITH) {
2481
2816
  return parseListWith();
2482
2817
  }
2818
+ if (peek().type === TOKEN.IDENTIFIER && (peek().value === 'dictionary' || peek().value === 'map') &&
2819
+ peekAt(1).type === TOKEN.WITH) {
2820
+ return parseDictionaryWith();
2821
+ }
2822
+ if ((peek().type === TOKEN.EMPTY || (peek().type === TOKEN.IDENTIFIER && peek().value === 'empty')) &&
2823
+ (peekAt(1).value === 'dictionary' || peekAt(1).value === 'map' || peekAt(1).value === 'set')) {
2824
+ const typeStr = peekAt(1).value;
2825
+ advance(); advance();
2826
+ if (typeStr === 'set') return { type: 'SetLiteral', elements: [] };
2827
+ return { type: 'DictionaryLiteral', pairs: [] };
2828
+ }
2829
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'set') {
2830
+ if (peekAt(1).type === TOKEN.WITH) return parseSetWith();
2831
+ if (peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'from') {
2832
+ advance(); advance();
2833
+ const iterable = parseExpression();
2834
+ return { type: 'SetFromExpression', iterable };
2835
+ }
2836
+ }
2837
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'tuple') {
2838
+ if (peekAt(1).type === TOKEN.WITH) return parseTupleWith();
2839
+ if (peekAt(1).type === TOKEN.LPAREN) return parseTupleCall();
2840
+ }
2841
+
2842
+ // Set Algebra: union of a and b / intersection of a and b / difference of a and b
2843
+ if (peek().type === TOKEN.IDENTIFIER &&
2844
+ (peek().value === 'union' || peek().value === 'intersection' || peek().value === 'intersect' || peek().value === 'difference') &&
2845
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
2846
+ const op = advance().value;
2847
+ advance(); // of
2848
+ const left = parsePrimary();
2849
+ if (peek().type === TOKEN.AND || (peek().type === TOKEN.IDENTIFIER && peek().value === 'and')) advance();
2850
+ const right = parseExpression();
2851
+ return { type: 'SetAlgebraExpression', op, left, right };
2852
+ }
2853
+
2854
+ // Collection reflection: keys of d / values of d / entries of d / size of d
2855
+ if (peek().type === TOKEN.IDENTIFIER &&
2856
+ (peek().value === 'keys' || peek().value === 'values' || peek().value === 'entries' || peek().value === 'size') &&
2857
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
2858
+ const accessor = advance().value;
2859
+ advance(); // of
2860
+ const target = parsePrimary();
2861
+ return { type: 'CollectionReflectExpression', accessor, target };
2862
+ }
2483
2863
 
2484
2864
  // v1.0.1 — record constructor: `create a Person with name "Ada" and age 17`.
2485
2865
  // `create` then an article ("a"/"an") then a kind name then "with" pairs.
@@ -2716,7 +3096,9 @@ function parseAsk() {
2716
3096
  return { type: 'ImportMetaExpression' };
2717
3097
  }
2718
3098
 
2719
- if (token.type === TOKEN.IDENTIFIER || token.type === TOKEN.BACK) {
3099
+ // Keywords that can also be used as identifiers in expression context
3100
+ const IDENTIFIER_KEYWORDS = new Set([TOKEN.BACK, TOKEN.REPLY, TOKEN.RESPOND, TOKEN.SEND_BACK, TOKEN.FILE_KW]);
3101
+ if (token.type === TOKEN.IDENTIFIER || IDENTIFIER_KEYWORDS.has(token.type)) {
2720
3102
  if (peekAt(1).type === TOKEN.USES || peekAt(1).type === TOKEN.FILLS) {
2721
3103
  const callee = { type: 'Identifier', name: token.value };
2722
3104
  advance();