plainscript-lang 0.1.7 → 1.0.0-latest

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 (49) hide show
  1. package/README.md +98 -40
  2. package/compiler/bundler.js +1 -1
  3. package/compiler/cli.js +166 -57
  4. package/compiler/generator.js +878 -19
  5. package/compiler/lexer.js +31 -2
  6. package/compiler/parser.js +668 -24
  7. package/compiler/version.js +1 -1
  8. package/docs/CAPABILITY_GAP_AUDIT.md +439 -0
  9. package/docs/PLINJS_SPEC.md +124 -24
  10. package/docs/index.html +555 -2220
  11. package/docs/website/script.js +889 -133
  12. package/docs/website/style.css +652 -1113
  13. package/examples/auth-api.ps +56 -0
  14. package/examples/{cache-and-jobs.pln → cache-and-jobs.ps} +1 -1
  15. package/examples/concurrency.ps +49 -0
  16. package/examples/{database.pln → database.ps} +1 -1
  17. package/examples/{deployment.pln → deployment.ps} +1 -1
  18. package/examples/{email-send.pln → email-send.ps} +1 -1
  19. package/examples/error-handling.ps +54 -0
  20. package/examples/file-uploads.ps +39 -0
  21. package/examples/files-and-config.ps +47 -0
  22. package/examples/http-client.ps +44 -0
  23. package/examples/modules/app.ps +16 -0
  24. package/examples/modules/math.ps +20 -0
  25. package/examples/record-kinds.ps +43 -0
  26. package/examples/{rest-api.pln → rest-api.ps} +1 -1
  27. package/examples/{scheduled-tasks.pln → scheduled-tasks.ps} +1 -1
  28. package/examples/search-and-filter.ps +34 -0
  29. package/examples/sessions-and-cookies.ps +54 -0
  30. package/examples/{websocket-server.pln → websocket-server.ps} +1 -1
  31. package/package.json +2 -2
  32. package/docs/website/index.html +0 -1500
  33. /package/examples/{arrays.pln → arrays.ps} +0 -0
  34. /package/examples/{day2.pln → day2.ps} +0 -0
  35. /package/examples/{day3.pln → day3.ps} +0 -0
  36. /package/examples/{expressions.pln → expressions.ps} +0 -0
  37. /package/examples/football-backend/{app.pln → app.ps} +0 -0
  38. /package/examples/{hello.pln → hello.ps} +0 -0
  39. /package/examples/id-verification/{app.pln → app.ps} +0 -0
  40. /package/examples/{loops.pln → loops.ps} +0 -0
  41. /package/examples/{objects.pln → objects.ps} +0 -0
  42. /package/examples/{rest-api-fetch.pln → rest-api-fetch.ps} +0 -0
  43. /package/examples/{server.pln → server.ps} +0 -0
  44. /package/examples/{start.pln → start.ps} +0 -0
  45. /package/examples/{stdlib.pln → stdlib.ps} +0 -0
  46. /package/examples/{telegram-bot.pln → telegram-bot.ps} +0 -0
  47. /package/examples/{web-app.pln → web-app.ps} +0 -0
  48. /package/examples/whatsapp-bot/{pairing.pln → pairing.ps} +0 -0
  49. /package/examples/whatsapp-bot/{qr.pln → qr.ps} +0 -0
@@ -8,6 +8,7 @@ const STATEMENT_KEYWORDS = [
8
8
  'for', 'while', '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
+ 'gather', 'filter', 'total', 'match', 'emit', 'stream', 'run',
11
12
  ];
12
13
 
13
14
  // Number words used by the numbered item expression (v1.1):
@@ -268,6 +269,9 @@ function parse(tokens) {
268
269
  if (token.type === TOKEN.IF) return parseIf();
269
270
  if (token.type === TOKEN.MAKE) return parseMake();
270
271
  if (token.type === TOKEN.GIVE) return parseGive();
272
+ // v1.0.0-latest — generators: `yield <expr>` is only meaningful inside a function
273
+ // body; the generator marks the enclosing `make ... done` as a function*.
274
+ if (token.type === TOKEN.YIELD) return parseYield();
271
275
  if (token.type === TOKEN.FOR) return parseForEach();
272
276
  // v2.1.0 — every <n> <unit>s … done: interval scheduling. "every" also
273
277
  // lexes as TOKEN.EACH (the "for every" alias), so only the number+unit
@@ -310,6 +314,14 @@ function parse(tokens) {
310
314
  if (token.type === TOKEN.DELETE_KW) return parseSqlBlock('delete', 'DeleteStatement');
311
315
  if (token.type === TOKEN.EXECUTE_KW) return parseSqlBlock('execute', 'ExecuteStatement');
312
316
 
317
+ // IOPL-native features
318
+ if (token.type === TOKEN.GATHER) return parseGatherStatement();
319
+ if (token.type === TOKEN.FILTER_KW) return parseFilterStatement();
320
+ if (token.type === TOKEN.TOTAL) return parseTotalStatement();
321
+ if (token.type === TOKEN.MATCH) return parseMatchStatement();
322
+ if (token.type === TOKEN.EMIT) return parseEmitStatement();
323
+ if (token.type === TOKEN.STREAM) return parseStreamStatement();
324
+
313
325
  if (token.type === TOKEN.EOF) return null;
314
326
 
315
327
  // Statements starting with an identifier: call, becomes, index/member becomes
@@ -422,6 +434,12 @@ function parse(tokens) {
422
434
  return { type: 'RunBackgroundStatement', call };
423
435
  }
424
436
 
437
+ // run in parallel ... done as <name>: worker threads with a result name.
438
+ if (token.value === 'run' &&
439
+ peekAt(1).type === TOKEN.IN && peekAt(2).type === TOKEN.PARALLEL) {
440
+ return parseRunParallelStatement();
441
+ }
442
+
425
443
  // v2.1.0 — websocket server on <port> … done: realtime endpoint.
426
444
  if (token.value === 'websocket' &&
427
445
  peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'server') {
@@ -474,6 +492,15 @@ function parse(tokens) {
474
492
  return { type: 'StatusStatement', value };
475
493
  }
476
494
 
495
+ // v2.2.0 — redirect to "<url>": sends an HTTP redirect from a route
496
+ // handler. "redirect(...)" calls and "redirect becomes x" stay ordinary.
497
+ if (token.value === 'redirect' && peekAt(1).value === 'to') {
498
+ advance(); // redirect
499
+ advance(); // to
500
+ const url = parseExpression();
501
+ return { type: 'RedirectStatement', url };
502
+ }
503
+
477
504
  // ── v2.1.1 statements (all contextual, following the v2.1.0 pattern) ──
478
505
 
479
506
  // try … [recover [as <name>]] … done: deterministic error handling.
@@ -551,6 +578,68 @@ function parse(tokens) {
551
578
  return { type: 'GoogleOAuthStatement', options: parsePropertyList('"google oauth" block') };
552
579
  }
553
580
 
581
+ // ── v1.0.0-latest — capability-gap features (all contextual, IOPL-native) ──
582
+
583
+ // define a kind called "Person" with … done → record schema (classes)
584
+ if (token.value === 'define' &&
585
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'a') {
586
+ return parseDefineKind();
587
+ }
588
+
589
+ // load env file "<path>" → apply .env KEY=VALUE pairs to process.env
590
+ if (token.value === 'load' &&
591
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'env' &&
592
+ peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'file') {
593
+ advance(); // load
594
+ advance(); // env
595
+ advance(); // file
596
+ const path = consume(TOKEN.STRING,
597
+ 'Expected an env file path string after "load env file".\n\nExample:\n load env file ".env"');
598
+ return { type: 'LoadEnvFileStatement', path: path.value };
599
+ }
600
+
601
+ // test "<name>" … done → native test DSL
602
+ if (token.value === 'test' && peekAt(1).type === TOKEN.STRING) {
603
+ return parseTestStatement();
604
+ }
605
+
606
+ // check … → assertion, only valid inside a test block
607
+ if (token.value === 'check') {
608
+ return parseCheckStatement();
609
+ }
610
+
611
+ // export <name> → mark a top-level symbol for module.exports
612
+ if (token.value === 'export' &&
613
+ peekAt(1).type === TOKEN.IDENTIFIER &&
614
+ peekAt(2).type !== TOKEN.LPAREN) {
615
+ advance(); // export
616
+ const name = advance().value;
617
+ return { type: 'ExportStatement', name };
618
+ }
619
+
620
+ // throw <value> → raise an error for the caller's try/recover to catch.
621
+ // "throw(...)" calls and "throw becomes x" keep their ordinary meaning.
622
+ if (token.value === 'throw' &&
623
+ peekAt(1).type !== TOKEN.LPAREN && peekAt(1).type !== TOKEN.BECOMES) {
624
+ advance(); // throw
625
+ const value = parseExpression();
626
+ return { type: 'ThrowStatement', value };
627
+ }
628
+
629
+ // break / continue → loop control flow. Contextual keywords: a variable
630
+ // literally named break/continue is rejected by the teaching guard in
631
+ // generateStatement when not inside a loop.
632
+ if (token.value === 'break' &&
633
+ peekAt(1).type !== TOKEN.BECOMES && peekAt(1).type !== TOKEN.LPAREN) {
634
+ advance(); // break
635
+ return { type: 'BreakStatement' };
636
+ }
637
+ if (token.value === 'continue' &&
638
+ peekAt(1).type !== TOKEN.BECOMES && peekAt(1).type !== TOKEN.LPAREN) {
639
+ advance(); // continue
640
+ return { type: 'ContinueStatement' };
641
+ }
642
+
554
643
  const expr = parsePrimary();
555
644
 
556
645
  if (peek().type === TOKEN.BECOMES) {
@@ -757,26 +846,197 @@ function parse(tokens) {
757
846
  function parseParamList() {
758
847
  const params = [];
759
848
  if (peek().type === TOKEN.RPAREN) return params;
760
- params.push(consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value);
849
+ const firstName = consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value;
850
+ const firstParam = { name: firstName };
851
+ if (peek().type === TOKEN.AS) {
852
+ advance(); // as
853
+ const dv = parseDefaultValue();
854
+ if (dv) firstParam.defaultValue = dv;
855
+ }
856
+ params.push(firstParam);
761
857
  while (peek().type === TOKEN.COMMA) {
762
858
  advance();
763
- params.push(consume(TOKEN.IDENTIFIER, 'Expected a parameter name after ",".').value);
859
+ const pName = consume(TOKEN.IDENTIFIER, 'Expected a parameter name after ",".').value;
860
+ const param = { name: pName };
861
+ if (peek().type === TOKEN.AS) {
862
+ advance(); // as
863
+ const dv = parseDefaultValue();
864
+ if (dv) param.defaultValue = dv;
865
+ }
866
+ params.push(param);
764
867
  }
765
868
  return params;
766
869
  }
767
870
 
871
+ function parseDefaultValue() {
872
+ const token = peek();
873
+ if (token.type === TOKEN.STRING) {
874
+ advance();
875
+ return { type: 'StringLiteral', value: token.value };
876
+ }
877
+ if (token.type === TOKEN.NUMBER) {
878
+ advance();
879
+ return { type: 'NumberLiteral', value: token.value };
880
+ }
881
+ if (token.type === TOKEN.TRUE_KW) {
882
+ advance();
883
+ return { type: 'BooleanLiteral', value: true };
884
+ }
885
+ if (token.type === TOKEN.FALSE_KW) {
886
+ advance();
887
+ return { type: 'BooleanLiteral', value: false };
888
+ }
889
+ if (token.type === TOKEN.NULL_KW) {
890
+ advance();
891
+ return { type: 'NullLiteral' };
892
+ }
893
+ return null;
894
+ }
895
+
768
896
  function parseGive() {
769
897
  consume(TOKEN.GIVE);
770
898
  const value = parseExpression();
771
899
  return { type: 'GiveStatement', value };
772
900
  }
773
901
 
902
+ // v1.0.0-latest — generators: `yield <expr>` (optionally bare `yield`).
903
+ function parseYield() {
904
+ consume(TOKEN.YIELD);
905
+ let value = null;
906
+ if (peek().type !== TOKEN.DONE && peek().type !== TOKEN.EOF) {
907
+ value = parseExpression();
908
+ }
909
+ return { type: 'YieldStatement', value };
910
+ }
911
+
912
+ // v1.0.0-latest — record kinds: `define a kind called "Person" with … done`
913
+ // The field block reuses the property-list grammar (`age is 0`), so each
914
+ // field gets a default expression just like an object literal.
915
+ function parseDefineKind() {
916
+ advance(); // define
917
+ const art = advance(); // "a" / "an"
918
+ // `define a kind called "Person"` — the word "kind" is optional prose.
919
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'kind') {
920
+ advance(); // kind
921
+ } else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'called') {
922
+ advance(); // (implicit 'kind' omitted)
923
+ } else {
924
+ throw new Error(makeError(
925
+ 'Expected "kind" (or "called") after "define a ' + art.value + '".\n\nExample:\n define a kind called "Person" with\n name is ""\n done',
926
+ peek()
927
+ ));
928
+ }
929
+ const called = peek();
930
+ if (called.type !== TOKEN.IDENTIFIER || called.value !== 'called') {
931
+ throw new Error(makeError(
932
+ 'Expected "called" after "define a kind".\n\nExample:\n define a kind called "Person" with\n name is ""\n done',
933
+ called
934
+ ));
935
+ }
936
+ advance(); // called
937
+ let name;
938
+ if (peek().type === TOKEN.STRING) {
939
+ name = advance().value;
940
+ } else {
941
+ const t = consume(TOKEN.IDENTIFIER, 'Expected a kind name after "kind called".');
942
+ name = t.value;
943
+ }
944
+ consume(TOKEN.WITH, 'Expected "with" after the kind name.\n\nExample:\n define a kind called "Person" with\n name is ""\n done');
945
+ const fields = parsePropertyList('kind "' + name + '" definition');
946
+ return { type: 'DefineKindStatement', name, fields };
947
+ }
948
+
949
+ // v1.0.0-latest — record constructor (expression): `create a Person with name "Ada" and age 17`
950
+ function parseCreateKind() {
951
+ advance(); // create
952
+ advance(); // a / an
953
+ const kind = consume(TOKEN.IDENTIFIER, 'Expected a kind name after "create a".').value;
954
+ consume(TOKEN.WITH, 'Expected "with" after the kind name.\n\nExample:\n create a Person with name "Ada" and age 17');
955
+ const pairs = [];
956
+ while (true) {
957
+ const key = consume(TOKEN.IDENTIFIER, 'Expected a field name in "create a ' + kind + ' with ...".').value;
958
+ const value = parsePrimary();
959
+ pairs.push({ key, value });
960
+ if (peek().type === TOKEN.AND) {
961
+ advance();
962
+ continue;
963
+ }
964
+ break;
965
+ }
966
+ return { type: 'CreateKindExpression', kind, pairs };
967
+ }
968
+
969
+ // v1.0.0-latest — native test DSL: `test "name" … done`
970
+ function parseTestStatement() {
971
+ advance(); // test
972
+ const name = consume(TOKEN.STRING, 'Expected a test name string after "test".').value;
973
+ const body = parseBody('"test" block');
974
+ return { type: 'TestStatement', name, body };
975
+ }
976
+
977
+ // v1.0.0-latest — assertions: `check <a> (equals|is|contains|raises) <b>`
978
+ function parseCheckStatement() {
979
+ advance(); // check
980
+ const a = parseExpression();
981
+ const opToken = peek();
982
+ if (!['equals', 'is', 'contains', 'raises'].includes(opToken.value)) {
983
+ throw new Error(makeError(
984
+ 'Expected "equals", "is", "contains" or "raises" after the value in a "check".\n\nExample:\n check score equals 42',
985
+ opToken
986
+ ));
987
+ }
988
+ const op = advance().value;
989
+ const b = parseExpression();
990
+ return { type: 'CheckStatement', a, op, b };
991
+ }
992
+
774
993
  // for each <item> in <collection> ... done
775
994
  // for every <item> in <collection> ... done (alias — "every" maps to EACH token)
776
995
  function parseForEach() {
777
996
  consume(TOKEN.FOR);
997
+
998
+ // for index <name> from <start> to <end> [by <step>] ... done
999
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'index') {
1000
+ advance(); // index
1001
+ const name = consume(
1002
+ TOKEN.IDENTIFIER,
1003
+ 'Expected an index name after "for index".\n\nExample:\n for index i from 0 to 9\n show i\n done'
1004
+ ).value;
1005
+ if (peek().type === TOKEN.IN || (peek().type === TOKEN.IDENTIFIER && peek().value === 'in')) {
1006
+ // for index <name> in <collection> — zero-based index over a list.
1007
+ advance(); // in
1008
+ const collection = parseExpression();
1009
+ const body = parseBody('"for index" loop');
1010
+ return { type: 'ForIndexStatement', name, start: null, end: collection, step: null, over: collection, body };
1011
+ }
1012
+ if (peek().type !== TOKEN.IDENTIFIER || peek().value !== 'from') {
1013
+ throw new Error(makeError(
1014
+ 'Expected "from" or "in" after the "for index" name.\n\nExample:\n for index i from 0 to 9\n for index i in players',
1015
+ peek()
1016
+ ));
1017
+ }
1018
+ advance(); // from
1019
+ const start = parseExpression();
1020
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'to') {
1021
+ advance(); // to
1022
+ } else {
1023
+ throw new Error(makeError(
1024
+ 'Expected "to" between the start and end of a "for index" loop.\n\nExample:\n for index i from 0 to 9',
1025
+ peek()
1026
+ ));
1027
+ }
1028
+ const end = parseExpression();
1029
+ let step = null;
1030
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'by') {
1031
+ advance(); // by
1032
+ step = parseExpression();
1033
+ }
1034
+ const body = parseBody('"for index" loop');
1035
+ return { type: 'ForIndexStatement', name, start, end, step, over: null, body };
1036
+ }
1037
+
778
1038
  consume(TOKEN.EACH,
779
- 'Expected "each" or "every" after "for".\n\nExample:\n for each item in players\n show item\n done');
1039
+ 'Expected "each", "every" or "index" after "for".\n\nExample:\n for each item in players\n show item\n done');
780
1040
  const item = consume(TOKEN.IDENTIFIER, 'Expected an item name after "each" or "every".').value;
781
1041
  consume(TOKEN.IN, `Expected "in" after "${item}".\n\nExample:\n for each item in players`);
782
1042
  const collection = parseExpression();
@@ -792,12 +1052,43 @@ function parse(tokens) {
792
1052
  return { type: 'WhileStatement', condition, body };
793
1053
  }
794
1054
 
795
- // import "./file.pln"
1055
+ // import "./file.ps"
1056
+ // import { name1, name2 } from "./file.ps" (named imports — binds only the
1057
+ // listed symbols; the whole file is still bundled as a dependency)
796
1058
  function parseImport() {
797
1059
  consume(TOKEN.IMPORT);
1060
+
1061
+ // Named-import form: import { a, b } from "./file.ps"
1062
+ if (peek().type === TOKEN.LBRACE) {
1063
+ advance(); // {
1064
+ const names = [];
1065
+ while (true) {
1066
+ names.push(consume(
1067
+ TOKEN.IDENTIFIER,
1068
+ 'Expected an exported name inside the import braces.\n\nExample:\n import { helper } from "./util.ps"'
1069
+ ).value);
1070
+ if (peek().type === TOKEN.COMMA) { advance(); continue; }
1071
+ break;
1072
+ }
1073
+ consume(TOKEN.RBRACE,
1074
+ 'Expected "}" to close the import braces.\n\nExample:\n import { helper } from "./util.ps"');
1075
+ // The "from" is a plain identifier; tolerate its optional presence so
1076
+ // both "import { x } from "./f.ps"" and "import { x } "./f.ps"" read
1077
+ // naturally, but require it for the documented form.
1078
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'from') {
1079
+ advance(); // from
1080
+ }
1081
+ const filePath = consume(
1082
+ TOKEN.STRING,
1083
+ 'Expected a file path string after the import.\n\nExample:\n import { helper } from "./util.ps"'
1084
+ ).value;
1085
+ return { type: 'ImportStatement', path: filePath, names };
1086
+ }
1087
+
1088
+ // Whole-module form: import "./file.ps"
798
1089
  const filePath = consume(
799
1090
  TOKEN.STRING,
800
- 'Expected a file path string after "import".\n\nExample:\n import "./math.pln"'
1091
+ 'Expected a file path string after "import".\n\nExample:\n import "./math.ps"'
801
1092
  ).value;
802
1093
  return { type: 'ImportStatement', path: filePath };
803
1094
  }
@@ -850,6 +1141,22 @@ function parse(tokens) {
850
1141
  return { type: 'NotFoundStatement', body };
851
1142
  }
852
1143
 
1144
+ // IOPL-native — "when \"event\" happens as data … done": event listener.
1145
+ // Must be checked before the "when someone" path.
1146
+ if (peek().type === TOKEN.STRING &&
1147
+ peekAt(1).type === TOKEN.HAPPENS) {
1148
+ const eventStr = advance().value;
1149
+ advance(); // happens
1150
+ let paramName = null;
1151
+ if (peek().type === TOKEN.AS) {
1152
+ advance(); // as
1153
+ paramName = consume(TOKEN.IDENTIFIER,
1154
+ 'Expected a parameter name after "as".\n\nExample:\n when "data.received" happens as data').value;
1155
+ }
1156
+ const body = parseBody('"when happens" block');
1157
+ return { type: 'WhenHappensStatement', event: eventStr, paramName, body };
1158
+ }
1159
+
853
1160
  // v2.1.0 — socket handlers inside a "websocket server" block come first:
854
1161
  // they read "when socket …", not "when someone …".
855
1162
  const whenToken = peek();
@@ -1173,7 +1480,7 @@ function parse(tokens) {
1173
1480
 
1174
1481
  // ── v2.1.1 — error handling, retries and backend middleware ───────────────
1175
1482
 
1176
- // try … [recover [as <name>]] … done
1483
+ // try … [recover [when <err> catches "<ErrorType>"]] … done
1177
1484
  function parseTryStatement() {
1178
1485
  advance(); // try
1179
1486
  const tryBody = [];
@@ -1188,17 +1495,34 @@ function parse(tokens) {
1188
1495
  const stmt = parseStatement();
1189
1496
  if (stmt) tryBody.push(stmt);
1190
1497
  }
1191
- let catchName = null;
1192
- let recoverBody = null;
1193
- if (peek().type !== TOKEN.DONE) {
1498
+ const catches = [];
1499
+ while (peek().type !== TOKEN.DONE) {
1500
+ if (peek().type === TOKEN.EOF) {
1501
+ throw new Error(makeError(
1502
+ 'Expected keyword "done" to close the "try" block before end of file.',
1503
+ peek()
1504
+ ));
1505
+ }
1194
1506
  advance(); // recover
1507
+ let paramName = null;
1508
+ let errorType = null;
1195
1509
  if (peek().type === TOKEN.AS) {
1196
- advance();
1197
- catchName = consume(TOKEN.IDENTIFIER,
1510
+ advance(); // as
1511
+ paramName = consume(TOKEN.IDENTIFIER,
1198
1512
  'Expected a variable name after "recover as".\n\nExample:\n recover as error').value;
1513
+ } else if (peek().type === TOKEN.WHEN) {
1514
+ advance(); // when
1515
+ paramName = consume(TOKEN.IDENTIFIER,
1516
+ 'Expected a variable name after "when" in recover.\n\nExample:\n recover when err catches "TypeError"').value;
1517
+ if (peek().type === TOKEN.CATCHES) {
1518
+ advance(); // catches
1519
+ errorType = consume(TOKEN.STRING,
1520
+ 'Expected an error type string after "catches".\n\nExample:\n recover when err catches "TypeError"').value;
1521
+ }
1199
1522
  }
1200
- recoverBody = [];
1201
- while (peek().type !== TOKEN.DONE) {
1523
+ const recoverBody = [];
1524
+ while (peek().type !== TOKEN.DONE &&
1525
+ !(peek().type === TOKEN.IDENTIFIER && peek().value === 'recover')) {
1202
1526
  if (peek().type === TOKEN.EOF) {
1203
1527
  throw new Error(makeError(
1204
1528
  'Expected keyword "done" to close the "recover" block before end of file.',
@@ -1208,9 +1532,10 @@ function parse(tokens) {
1208
1532
  const stmt = parseStatement();
1209
1533
  if (stmt) recoverBody.push(stmt);
1210
1534
  }
1535
+ catches.push({ param: paramName, errorType, body: recoverBody });
1211
1536
  }
1212
1537
  advance(); // done
1213
- return { type: 'TryStatement', tryBody, catchName, recoverBody };
1538
+ return { type: 'TryStatement', body: tryBody, catches };
1214
1539
  }
1215
1540
 
1216
1541
  // retry <n> times [every <n> seconds] … done
@@ -1438,17 +1763,25 @@ function parse(tokens) {
1438
1763
 
1439
1764
  let alternate = null;
1440
1765
  if (peek().type === TOKEN.OTHERWISE) {
1766
+ // `otherwise if` is an else-if chain (sharing one `done`) ONLY when `if`
1767
+ // sits on the same line as `otherwise`; a newline-separated `if` is a
1768
+ // nested if statement with its own `done`.
1769
+ const otherwiseLine = peek().line;
1441
1770
  advance();
1442
- alternate = [];
1443
- while (peek().type !== TOKEN.DONE) {
1444
- if (peek().type === TOKEN.EOF) {
1445
- throw new Error(makeError(
1446
- 'Expected keyword "done" before end of file to close the "otherwise" block.',
1447
- peek()
1448
- ));
1771
+ if (peek().type === TOKEN.IF && peek().line === otherwiseLine) {
1772
+ alternate = [parseElseIfChain()];
1773
+ } else {
1774
+ alternate = [];
1775
+ while (peek().type !== TOKEN.DONE) {
1776
+ if (peek().type === TOKEN.EOF) {
1777
+ throw new Error(makeError(
1778
+ 'Expected keyword "done" before end of file to close the "otherwise" block.',
1779
+ peek()
1780
+ ));
1781
+ }
1782
+ const stmt = parseStatement();
1783
+ if (stmt) alternate.push(stmt);
1449
1784
  }
1450
- const stmt = parseStatement();
1451
- if (stmt) alternate.push(stmt);
1452
1785
  }
1453
1786
  }
1454
1787
 
@@ -1456,6 +1789,50 @@ function parse(tokens) {
1456
1789
  return { type: 'IfStatement', condition, consequent, alternate };
1457
1790
  }
1458
1791
 
1792
+ // `otherwise if <condition> ... [otherwise if ...] [otherwise ...] done`
1793
+ // Parses a single else-if branch; a following `otherwise if` recurses, and a
1794
+ // trailing `otherwise` supplies the final else. The trailing `done` belongs to
1795
+ // the outermost `if` and is consumed there, not here.
1796
+ function parseElseIfChain() {
1797
+ advance(); // consume IF
1798
+ const condition = parseCondition();
1799
+
1800
+ const consequent = [];
1801
+ while (peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.DONE) {
1802
+ if (peek().type === TOKEN.EOF) {
1803
+ throw new Error(makeError(
1804
+ 'Expected keyword "done" before end of file to close the "otherwise if" block.',
1805
+ peek()
1806
+ ));
1807
+ }
1808
+ const stmt = parseStatement();
1809
+ if (stmt) consequent.push(stmt);
1810
+ }
1811
+
1812
+ let alternate = null;
1813
+ if (peek().type === TOKEN.OTHERWISE) {
1814
+ const otherwiseLine = peek().line;
1815
+ advance();
1816
+ if (peek().type === TOKEN.IF && peek().line === otherwiseLine) {
1817
+ alternate = [parseElseIfChain()];
1818
+ } else {
1819
+ alternate = [];
1820
+ while (peek().type !== TOKEN.DONE) {
1821
+ if (peek().type === TOKEN.EOF) {
1822
+ throw new Error(makeError(
1823
+ 'Expected keyword "done" before end of file to close the "otherwise" block.',
1824
+ peek()
1825
+ ));
1826
+ }
1827
+ const stmt = parseStatement();
1828
+ if (stmt) alternate.push(stmt);
1829
+ }
1830
+ }
1831
+ }
1832
+
1833
+ return { type: 'IfStatement', condition, consequent, alternate };
1834
+ }
1835
+
1459
1836
  // ── Shared helpers ──────────────────────────────────────────────────────────
1460
1837
 
1461
1838
  function parseBody(context) {
@@ -1525,6 +1902,41 @@ function parse(tokens) {
1525
1902
 
1526
1903
  // primary → itemExpr | atom (postfix)*
1527
1904
  function parsePrimary() {
1905
+ // v1.0.0-latest — record constructor: `create a Person with name "Ada" and age 17`.
1906
+ // `create` then an article ("a"/"an") then a kind name then "with" pairs.
1907
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'create') {
1908
+ const a1 = peekAt(1);
1909
+ const a2 = peekAt(2);
1910
+ const a3 = peekAt(3);
1911
+ if (a1.type === TOKEN.IDENTIFIER &&
1912
+ (a1.value === 'a' || a1.value === 'an') &&
1913
+ a2.type === TOKEN.IDENTIFIER &&
1914
+ (a3.value === 'with')) {
1915
+ return parseCreateKind();
1916
+ }
1917
+ }
1918
+
1919
+ // v1.0.0-latest — concurrency combinators: `all of [a(), b()]`, `any of [...]`,
1920
+ // `settled of [...]`. Guard on the identifier + "of" lookahead so plain
1921
+ // property reads like `all of the_list` still work as well as prose.
1922
+ if (peek().type === TOKEN.IDENTIFIER &&
1923
+ (peek().value === 'all' || peek().value === 'any' || peek().value === 'settled') &&
1924
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
1925
+ const comboKw = advance().value; // all | any | settled
1926
+ advance(); // of
1927
+ const items = parseExpression();
1928
+ return { type: 'ConcurrencyExpression', combo: comboKw, items };
1929
+ }
1930
+
1931
+ // v1.0.0-latest — `spread of <collection>` unfolds an iterable into a new array.
1932
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'spread' &&
1933
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
1934
+ advance(); // spread
1935
+ advance(); // of
1936
+ const collection = parsePrimary();
1937
+ return { type: 'SpreadExpression', collection };
1938
+ }
1939
+
1528
1940
  const item = tryParseItemExpression();
1529
1941
  if (item) return item;
1530
1942
  let node = parseAtom();
@@ -1536,8 +1948,31 @@ function parse(tokens) {
1536
1948
  node = { type: 'IndexExpression', object: node, index };
1537
1949
  } else if (peek().type === TOKEN.DOT) {
1538
1950
  advance();
1539
- const property = consume(TOKEN.IDENTIFIER, 'Expected a property name after ".".').value;
1951
+ // After ".", any word is a valid JS property/method name — including
1952
+ // words that are PlainScript keywords in other contexts (e.g. crypto
1953
+ // .update(...), obj .delete()). So accept any word token, not just an
1954
+ // identifier.
1955
+ const propToken = peek();
1956
+ if (propToken.type === TOKEN.EOF || !/^[A-Za-z_$]/.test(propToken.value)) {
1957
+ throw new Error(makeError('Expected a property name after ".".', propToken));
1958
+ }
1959
+ advance();
1960
+ const property = propToken.value;
1540
1961
  node = { type: 'MemberExpression', object: node, property };
1962
+ // Method call: object.method(args). Only member access may introduce
1963
+ // a call here; plain `name(args)` is handled in parseAtom.
1964
+ if (peek().type === TOKEN.LPAREN) {
1965
+ advance(); // (
1966
+ const { separator, args } = parseArgList();
1967
+ consume(TOKEN.RPAREN, 'Expected ")" to close the method call.');
1968
+ if (separator) {
1969
+ throw new Error(makeError(
1970
+ 'Method calls cannot use "to"/"from" arguments.\n\nExample:\n mrz.parse(line)',
1971
+ peek()
1972
+ ));
1973
+ }
1974
+ node = { type: 'CallExpression', callee: node, args };
1975
+ }
1541
1976
  } else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'of') {
1542
1977
  if (node.type !== 'Identifier') {
1543
1978
  throw new Error(makeError(
@@ -2000,6 +2435,215 @@ function parse(tokens) {
2000
2435
  return cleaned;
2001
2436
  }
2002
2437
 
2438
+ // ── IOPL-native features ──────────────────────────────────────────────────
2439
+
2440
+ // gather each item in list giving expr
2441
+ function parseGatherStatement() {
2442
+ advance(); // gather
2443
+ consume(TOKEN.EACH,
2444
+ 'Expected "each" after "gather".\n\nExample:\n gather each item in names giving item + "!"');
2445
+ const item = consume(TOKEN.IDENTIFIER,
2446
+ 'Expected an item name after "each".').value;
2447
+ consume(TOKEN.IN,
2448
+ `Expected "in" after "${item}".\n\nExample:\n gather each item in names giving item + "!"`);
2449
+ const collection = parseExpression();
2450
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'giving') {
2451
+ advance(); // giving
2452
+ } else {
2453
+ throw new Error(makeError(
2454
+ 'Expected "giving" after the collection expression.\n\nExample:\n gather each item in names giving item + "!"',
2455
+ peek()
2456
+ ));
2457
+ }
2458
+ const body = parseExpression();
2459
+ return { type: 'GatherStatement', item, collection, body };
2460
+ }
2461
+
2462
+ // filter each item in list when condition
2463
+ function parseFilterStatement() {
2464
+ advance(); // filter
2465
+ consume(TOKEN.EACH,
2466
+ 'Expected "each" after "filter".\n\nExample:\n filter each item in numbers when item is above 5');
2467
+ const item = consume(TOKEN.IDENTIFIER,
2468
+ 'Expected an item name after "each".').value;
2469
+ consume(TOKEN.IN,
2470
+ `Expected "in" after "${item}".\n\nExample:\n filter each item in numbers when item is above 5`);
2471
+ const collection = parseExpression();
2472
+ if (peek().type === TOKEN.WHEN ||
2473
+ (peek().type === TOKEN.IDENTIFIER && peek().value === 'when')) {
2474
+ advance(); // when
2475
+ } else {
2476
+ throw new Error(makeError(
2477
+ 'Expected "when" after the collection expression.\n\nExample:\n filter each item in numbers when item is above 5',
2478
+ peek()
2479
+ ));
2480
+ }
2481
+ const condition = parseAndCondition();
2482
+ return { type: 'FilterStatement', item, collection, condition };
2483
+ }
2484
+
2485
+ // total each item in list giving expr
2486
+ function parseTotalStatement() {
2487
+ advance(); // total
2488
+ consume(TOKEN.EACH,
2489
+ 'Expected "each" after "total".\n\nExample:\n total each item in numbers giving running + item');
2490
+ const item = consume(TOKEN.IDENTIFIER,
2491
+ 'Expected an item name after "each".').value;
2492
+ consume(TOKEN.IN,
2493
+ `Expected "in" after "${item}".\n\nExample:\n total each item in numbers giving running + item`);
2494
+ const collection = parseExpression();
2495
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'giving') {
2496
+ advance(); // giving
2497
+ } else {
2498
+ throw new Error(makeError(
2499
+ 'Expected "giving" after the collection expression.\n\nExample:\n total each item in numbers giving running + item',
2500
+ peek()
2501
+ ));
2502
+ }
2503
+ const body = parseExpression();
2504
+ return { type: 'TotalStatement', item, collection, body };
2505
+ }
2506
+
2507
+ // match <expr> against ... done
2508
+ // "case1" → show "one"
2509
+ // "case2" → show "two"
2510
+ // otherwise → show "other"
2511
+ // match pattern "regex" in text as name
2512
+ function parseMatchStatement() {
2513
+ advance(); // match
2514
+
2515
+ // Regex match form: match pattern "regex" in text as name
2516
+ if (peek().type === TOKEN.PATTERN_KW) {
2517
+ advance(); // pattern
2518
+ const pattern = consume(TOKEN.STRING,
2519
+ 'Expected a regex pattern string after "pattern".\n\nExample:\n match pattern "^(\\d+)$" in text as result').value;
2520
+ const inToken = peek();
2521
+ if (inToken.type !== TOKEN.IN) {
2522
+ throw new Error(makeError(
2523
+ 'Expected "in" after the pattern string.\n\nExample:\n match pattern "^(\\d+)$" in text as result',
2524
+ inToken
2525
+ ));
2526
+ }
2527
+ advance(); // in
2528
+ const source = parseExpression();
2529
+ const asToken = peek();
2530
+ if (asToken.type !== TOKEN.AS) {
2531
+ throw new Error(makeError(
2532
+ 'Expected "as" after the source expression.\n\nExample:\n match pattern "^(\\d+)$" in text as result',
2533
+ asToken
2534
+ ));
2535
+ }
2536
+ advance(); // as
2537
+ const name = consume(TOKEN.IDENTIFIER,
2538
+ 'Expected a variable name after "as".\n\nExample:\n match pattern "^(\\d+)$" in text as result').value;
2539
+ return { type: 'RegexMatchStatement', pattern, source, name };
2540
+ }
2541
+
2542
+ // Pattern matching form: match expr against ... done
2543
+ const value = parseExpression();
2544
+ consume(TOKEN.AGAINST,
2545
+ 'Expected "after" the expression.\n\nExample:\n match color against\n "red" → show "stop"\n done');
2546
+ const cases = [];
2547
+ let defaultCase = null;
2548
+ while (peek().type !== TOKEN.DONE) {
2549
+ if (peek().type === TOKEN.EOF) {
2550
+ throw new Error(makeError(
2551
+ 'Expected keyword "done" to close the "match" block before end of file.',
2552
+ peek()
2553
+ ));
2554
+ }
2555
+ // otherwise → body
2556
+ if (peek().type === TOKEN.OTHERWISE) {
2557
+ advance(); // otherwise
2558
+ consume(TOKEN.ARROW,
2559
+ 'Expected "->" after "otherwise".\n\nExample:\n otherwise → show "other"');
2560
+ defaultCase = [];
2561
+ while (peek().type !== TOKEN.DONE) {
2562
+ if (peek().type === TOKEN.EOF) {
2563
+ throw new Error(makeError(
2564
+ 'Expected keyword "done" to close the "match" block before end of file.',
2565
+ peek()
2566
+ ));
2567
+ }
2568
+ const stmt = parseStatement();
2569
+ if (stmt) defaultCase.push(stmt);
2570
+ }
2571
+ break;
2572
+ }
2573
+ const test = parseExpression();
2574
+ consume(TOKEN.ARROW,
2575
+ 'Expected "->" after the case expression.\n\nExample:\n "red" → show "stop"');
2576
+ const caseBody = [];
2577
+ while (peek().type !== TOKEN.DONE && peek().type !== TOKEN.OTHERWISE &&
2578
+ !(peek().type === TOKEN.STRING || peek().type === TOKEN.NUMBER ||
2579
+ peek().type === TOKEN.TRUE_KW || peek().type === TOKEN.FALSE_KW ||
2580
+ peek().type === TOKEN.NULL_KW)) {
2581
+ if (peek().type === TOKEN.EOF) {
2582
+ throw new Error(makeError(
2583
+ 'Expected keyword "done" to close the "match" block before end of file.',
2584
+ peek()
2585
+ ));
2586
+ }
2587
+ const stmt = parseStatement();
2588
+ if (stmt) caseBody.push(stmt);
2589
+ }
2590
+ cases.push({ test, body: caseBody });
2591
+ }
2592
+ advance(); // done
2593
+ return { type: 'MatchStatement', value, cases, defaultCase };
2594
+ }
2595
+
2596
+ // emit "event.name" with data
2597
+ function parseEmitStatement() {
2598
+ advance(); // emit
2599
+ const eventToken = peek();
2600
+ const event = consume(TOKEN.STRING,
2601
+ 'Expected an event name string after "emit".\n\nExample:\n emit "data.received" with payload').value;
2602
+ const withToken = peek();
2603
+ if (withToken.type !== TOKEN.WITH) {
2604
+ throw new Error(makeError(
2605
+ 'Expected "with" after the event name.\n\nExample:\n emit "data.received" with payload',
2606
+ withToken
2607
+ ));
2608
+ }
2609
+ advance(); // with
2610
+ const data = parseExpression();
2611
+ return { type: 'EmitStatement', event, data };
2612
+ }
2613
+
2614
+ // stream "file.txt" as line ... done
2615
+ function parseStreamStatement() {
2616
+ advance(); // stream
2617
+ const filename = parseExpression();
2618
+ const asToken = peek();
2619
+ if (asToken.type !== TOKEN.AS) {
2620
+ throw new Error(makeError(
2621
+ 'Expected "as" after the filename.\n\nExample:\n stream "data.txt" as line\n show line\n done',
2622
+ asToken
2623
+ ));
2624
+ }
2625
+ advance(); // as
2626
+ const paramName = consume(TOKEN.IDENTIFIER,
2627
+ 'Expected a variable name after "as".\n\nExample:\n stream "data.txt" as line').value;
2628
+ const body = parseBody('"stream" block');
2629
+ return { type: 'StreamStatement', filename, paramName, body };
2630
+ }
2631
+
2632
+ // run in parallel ... done as results
2633
+ function parseRunParallelStatement() {
2634
+ advance(); // run (already checked by caller)
2635
+ advance(); // in
2636
+ advance(); // parallel
2637
+ const body = parseBody('"run in parallel" block');
2638
+ let resultName = null;
2639
+ if (peek().type === TOKEN.AS) {
2640
+ advance(); // as
2641
+ resultName = consume(TOKEN.IDENTIFIER,
2642
+ 'Expected a variable name after "as".\n\nExample:\n run in parallel\n remember a as wait for slowJob()\n done as results').value;
2643
+ }
2644
+ return { type: 'RunParallelStatement', body, resultName };
2645
+ }
2646
+
2003
2647
  // name(arg, arg, ...)
2004
2648
  // name(value to collection) → v1.1 collection/io forms (add, remove, write)
2005
2649
  function parseCallExpression() {