plainscript-lang 0.1.7-rc → 1.0.0-beta

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.
@@ -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 — 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') {
@@ -551,6 +569,45 @@ function parse(tokens) {
551
569
  return { type: 'GoogleOAuthStatement', options: parsePropertyList('"google oauth" block') };
552
570
  }
553
571
 
572
+ // ── v1.0.0 — capability-gap features (all contextual, IOPL-native) ──
573
+
574
+ // define a kind called "Person" with … done → record schema (classes)
575
+ if (token.value === 'define' &&
576
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'a') {
577
+ return parseDefineKind();
578
+ }
579
+
580
+ // load env file "<path>" → apply .env KEY=VALUE pairs to process.env
581
+ if (token.value === 'load' &&
582
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'env' &&
583
+ peekAt(2).type === TOKEN.IDENTIFIER && peekAt(2).value === 'file') {
584
+ advance(); // load
585
+ advance(); // env
586
+ advance(); // file
587
+ const path = consume(TOKEN.STRING,
588
+ 'Expected an env file path string after "load env file".\n\nExample:\n load env file ".env"');
589
+ return { type: 'LoadEnvFileStatement', path: path.value };
590
+ }
591
+
592
+ // test "<name>" … done → native test DSL
593
+ if (token.value === 'test' && peekAt(1).type === TOKEN.STRING) {
594
+ return parseTestStatement();
595
+ }
596
+
597
+ // check … → assertion, only valid inside a test block
598
+ if (token.value === 'check') {
599
+ return parseCheckStatement();
600
+ }
601
+
602
+ // export <name> → mark a top-level symbol for module.exports
603
+ if (token.value === 'export' &&
604
+ peekAt(1).type === TOKEN.IDENTIFIER &&
605
+ peekAt(2).type !== TOKEN.LPAREN) {
606
+ advance(); // export
607
+ const name = advance().value;
608
+ return { type: 'ExportStatement', name };
609
+ }
610
+
554
611
  const expr = parsePrimary();
555
612
 
556
613
  if (peek().type === TOKEN.BECOMES) {
@@ -757,20 +814,150 @@ function parse(tokens) {
757
814
  function parseParamList() {
758
815
  const params = [];
759
816
  if (peek().type === TOKEN.RPAREN) return params;
760
- params.push(consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value);
817
+ const firstName = consume(TOKEN.IDENTIFIER, 'Expected a parameter name.').value;
818
+ const firstParam = { name: firstName };
819
+ if (peek().type === TOKEN.AS) {
820
+ advance(); // as
821
+ const dv = parseDefaultValue();
822
+ if (dv) firstParam.defaultValue = dv;
823
+ }
824
+ params.push(firstParam);
761
825
  while (peek().type === TOKEN.COMMA) {
762
826
  advance();
763
- params.push(consume(TOKEN.IDENTIFIER, 'Expected a parameter name after ",".').value);
827
+ const pName = consume(TOKEN.IDENTIFIER, 'Expected a parameter name after ",".').value;
828
+ const param = { name: pName };
829
+ if (peek().type === TOKEN.AS) {
830
+ advance(); // as
831
+ const dv = parseDefaultValue();
832
+ if (dv) param.defaultValue = dv;
833
+ }
834
+ params.push(param);
764
835
  }
765
836
  return params;
766
837
  }
767
838
 
839
+ function parseDefaultValue() {
840
+ const token = peek();
841
+ if (token.type === TOKEN.STRING) {
842
+ advance();
843
+ return { type: 'StringLiteral', value: token.value };
844
+ }
845
+ if (token.type === TOKEN.NUMBER) {
846
+ advance();
847
+ return { type: 'NumberLiteral', value: token.value };
848
+ }
849
+ if (token.type === TOKEN.TRUE_KW) {
850
+ advance();
851
+ return { type: 'BooleanLiteral', value: true };
852
+ }
853
+ if (token.type === TOKEN.FALSE_KW) {
854
+ advance();
855
+ return { type: 'BooleanLiteral', value: false };
856
+ }
857
+ if (token.type === TOKEN.NULL_KW) {
858
+ advance();
859
+ return { type: 'NullLiteral' };
860
+ }
861
+ return null;
862
+ }
863
+
768
864
  function parseGive() {
769
865
  consume(TOKEN.GIVE);
770
866
  const value = parseExpression();
771
867
  return { type: 'GiveStatement', value };
772
868
  }
773
869
 
870
+ // v1.0.0 — generators: `yield <expr>` (optionally bare `yield`).
871
+ function parseYield() {
872
+ consume(TOKEN.YIELD);
873
+ let value = null;
874
+ if (peek().type !== TOKEN.DONE && peek().type !== TOKEN.EOF) {
875
+ value = parseExpression();
876
+ }
877
+ return { type: 'YieldStatement', value };
878
+ }
879
+
880
+ // v1.0.0 — record kinds: `define a kind called "Person" with … done`
881
+ // The field block reuses the property-list grammar (`age is 0`), so each
882
+ // field gets a default expression just like an object literal.
883
+ function parseDefineKind() {
884
+ advance(); // define
885
+ const art = advance(); // "a" / "an"
886
+ // `define a kind called "Person"` — the word "kind" is optional prose.
887
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'kind') {
888
+ advance(); // kind
889
+ } else if (peek().type === TOKEN.IDENTIFIER && peek().value === 'called') {
890
+ advance(); // (implicit 'kind' omitted)
891
+ } else {
892
+ throw new Error(makeError(
893
+ 'Expected "kind" (or "called") after "define a ' + art.value + '".\n\nExample:\n define a kind called "Person" with\n name is ""\n done',
894
+ peek()
895
+ ));
896
+ }
897
+ const called = peek();
898
+ if (called.type !== TOKEN.IDENTIFIER || called.value !== 'called') {
899
+ throw new Error(makeError(
900
+ 'Expected "called" after "define a kind".\n\nExample:\n define a kind called "Person" with\n name is ""\n done',
901
+ called
902
+ ));
903
+ }
904
+ advance(); // called
905
+ let name;
906
+ if (peek().type === TOKEN.STRING) {
907
+ name = advance().value;
908
+ } else {
909
+ const t = consume(TOKEN.IDENTIFIER, 'Expected a kind name after "kind called".');
910
+ name = t.value;
911
+ }
912
+ consume(TOKEN.WITH, 'Expected "with" after the kind name.\n\nExample:\n define a kind called "Person" with\n name is ""\n done');
913
+ const fields = parsePropertyList('kind "' + name + '" definition');
914
+ return { type: 'DefineKindStatement', name, fields };
915
+ }
916
+
917
+ // v1.0.0 — record constructor (expression): `create a Person with name "Ada" and age 17`
918
+ function parseCreateKind() {
919
+ advance(); // create
920
+ advance(); // a / an
921
+ const kind = consume(TOKEN.IDENTIFIER, 'Expected a kind name after "create a".').value;
922
+ consume(TOKEN.WITH, 'Expected "with" after the kind name.\n\nExample:\n create a Person with name "Ada" and age 17');
923
+ const pairs = [];
924
+ while (true) {
925
+ const key = consume(TOKEN.IDENTIFIER, 'Expected a field name in "create a ' + kind + ' with ...".').value;
926
+ const value = parsePrimary();
927
+ pairs.push({ key, value });
928
+ if (peek().type === TOKEN.AND) {
929
+ advance();
930
+ continue;
931
+ }
932
+ break;
933
+ }
934
+ return { type: 'CreateKindExpression', kind, pairs };
935
+ }
936
+
937
+ // v1.0.0 — native test DSL: `test "name" … done`
938
+ function parseTestStatement() {
939
+ advance(); // test
940
+ const name = consume(TOKEN.STRING, 'Expected a test name string after "test".').value;
941
+ const body = parseBody('"test" block');
942
+ return { type: 'TestStatement', name, body };
943
+ }
944
+
945
+ // v1.0.0 — assertions: `check <a> (equals|is|contains|raises) <b>`
946
+ function parseCheckStatement() {
947
+ advance(); // check
948
+ const a = parseExpression();
949
+ const opToken = peek();
950
+ if (!['equals', 'is', 'contains', 'raises'].includes(opToken.value)) {
951
+ throw new Error(makeError(
952
+ 'Expected "equals", "is", "contains" or "raises" after the value in a "check".\n\nExample:\n check score equals 42',
953
+ opToken
954
+ ));
955
+ }
956
+ const op = advance().value;
957
+ const b = parseExpression();
958
+ return { type: 'CheckStatement', a, op, b };
959
+ }
960
+
774
961
  // for each <item> in <collection> ... done
775
962
  // for every <item> in <collection> ... done (alias — "every" maps to EACH token)
776
963
  function parseForEach() {
@@ -850,6 +1037,22 @@ function parse(tokens) {
850
1037
  return { type: 'NotFoundStatement', body };
851
1038
  }
852
1039
 
1040
+ // IOPL-native — "when \"event\" happens as data … done": event listener.
1041
+ // Must be checked before the "when someone" path.
1042
+ if (peek().type === TOKEN.STRING &&
1043
+ peekAt(1).type === TOKEN.HAPPENS) {
1044
+ const eventStr = advance().value;
1045
+ advance(); // happens
1046
+ let paramName = null;
1047
+ if (peek().type === TOKEN.AS) {
1048
+ advance(); // as
1049
+ paramName = consume(TOKEN.IDENTIFIER,
1050
+ 'Expected a parameter name after "as".\n\nExample:\n when "data.received" happens as data').value;
1051
+ }
1052
+ const body = parseBody('"when happens" block');
1053
+ return { type: 'WhenHappensStatement', event: eventStr, paramName, body };
1054
+ }
1055
+
853
1056
  // v2.1.0 — socket handlers inside a "websocket server" block come first:
854
1057
  // they read "when socket …", not "when someone …".
855
1058
  const whenToken = peek();
@@ -1173,7 +1376,7 @@ function parse(tokens) {
1173
1376
 
1174
1377
  // ── v2.1.1 — error handling, retries and backend middleware ───────────────
1175
1378
 
1176
- // try … [recover [as <name>]] … done
1379
+ // try … [recover [when <err> catches "<ErrorType>"]] … done
1177
1380
  function parseTryStatement() {
1178
1381
  advance(); // try
1179
1382
  const tryBody = [];
@@ -1188,17 +1391,34 @@ function parse(tokens) {
1188
1391
  const stmt = parseStatement();
1189
1392
  if (stmt) tryBody.push(stmt);
1190
1393
  }
1191
- let catchName = null;
1192
- let recoverBody = null;
1193
- if (peek().type !== TOKEN.DONE) {
1394
+ const catches = [];
1395
+ while (peek().type !== TOKEN.DONE) {
1396
+ if (peek().type === TOKEN.EOF) {
1397
+ throw new Error(makeError(
1398
+ 'Expected keyword "done" to close the "try" block before end of file.',
1399
+ peek()
1400
+ ));
1401
+ }
1194
1402
  advance(); // recover
1403
+ let paramName = null;
1404
+ let errorType = null;
1195
1405
  if (peek().type === TOKEN.AS) {
1196
- advance();
1197
- catchName = consume(TOKEN.IDENTIFIER,
1406
+ advance(); // as
1407
+ paramName = consume(TOKEN.IDENTIFIER,
1198
1408
  'Expected a variable name after "recover as".\n\nExample:\n recover as error').value;
1409
+ } else if (peek().type === TOKEN.WHEN) {
1410
+ advance(); // when
1411
+ paramName = consume(TOKEN.IDENTIFIER,
1412
+ 'Expected a variable name after "when" in recover.\n\nExample:\n recover when err catches "TypeError"').value;
1413
+ if (peek().type === TOKEN.CATCHES) {
1414
+ advance(); // catches
1415
+ errorType = consume(TOKEN.STRING,
1416
+ 'Expected an error type string after "catches".\n\nExample:\n recover when err catches "TypeError"').value;
1417
+ }
1199
1418
  }
1200
- recoverBody = [];
1201
- while (peek().type !== TOKEN.DONE) {
1419
+ const recoverBody = [];
1420
+ while (peek().type !== TOKEN.DONE &&
1421
+ !(peek().type === TOKEN.IDENTIFIER && peek().value === 'recover')) {
1202
1422
  if (peek().type === TOKEN.EOF) {
1203
1423
  throw new Error(makeError(
1204
1424
  'Expected keyword "done" to close the "recover" block before end of file.',
@@ -1208,9 +1428,10 @@ function parse(tokens) {
1208
1428
  const stmt = parseStatement();
1209
1429
  if (stmt) recoverBody.push(stmt);
1210
1430
  }
1431
+ catches.push({ param: paramName, errorType, body: recoverBody });
1211
1432
  }
1212
1433
  advance(); // done
1213
- return { type: 'TryStatement', tryBody, catchName, recoverBody };
1434
+ return { type: 'TryStatement', body: tryBody, catches };
1214
1435
  }
1215
1436
 
1216
1437
  // retry <n> times [every <n> seconds] … done
@@ -1438,17 +1659,25 @@ function parse(tokens) {
1438
1659
 
1439
1660
  let alternate = null;
1440
1661
  if (peek().type === TOKEN.OTHERWISE) {
1662
+ // `otherwise if` is an else-if chain (sharing one `done`) ONLY when `if`
1663
+ // sits on the same line as `otherwise`; a newline-separated `if` is a
1664
+ // nested if statement with its own `done`.
1665
+ const otherwiseLine = peek().line;
1441
1666
  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
- ));
1667
+ if (peek().type === TOKEN.IF && peek().line === otherwiseLine) {
1668
+ alternate = [parseElseIfChain()];
1669
+ } else {
1670
+ alternate = [];
1671
+ while (peek().type !== TOKEN.DONE) {
1672
+ if (peek().type === TOKEN.EOF) {
1673
+ throw new Error(makeError(
1674
+ 'Expected keyword "done" before end of file to close the "otherwise" block.',
1675
+ peek()
1676
+ ));
1677
+ }
1678
+ const stmt = parseStatement();
1679
+ if (stmt) alternate.push(stmt);
1449
1680
  }
1450
- const stmt = parseStatement();
1451
- if (stmt) alternate.push(stmt);
1452
1681
  }
1453
1682
  }
1454
1683
 
@@ -1456,6 +1685,50 @@ function parse(tokens) {
1456
1685
  return { type: 'IfStatement', condition, consequent, alternate };
1457
1686
  }
1458
1687
 
1688
+ // `otherwise if <condition> ... [otherwise if ...] [otherwise ...] done`
1689
+ // Parses a single else-if branch; a following `otherwise if` recurses, and a
1690
+ // trailing `otherwise` supplies the final else. The trailing `done` belongs to
1691
+ // the outermost `if` and is consumed there, not here.
1692
+ function parseElseIfChain() {
1693
+ advance(); // consume IF
1694
+ const condition = parseCondition();
1695
+
1696
+ const consequent = [];
1697
+ while (peek().type !== TOKEN.OTHERWISE && peek().type !== TOKEN.DONE) {
1698
+ if (peek().type === TOKEN.EOF) {
1699
+ throw new Error(makeError(
1700
+ 'Expected keyword "done" before end of file to close the "otherwise if" block.',
1701
+ peek()
1702
+ ));
1703
+ }
1704
+ const stmt = parseStatement();
1705
+ if (stmt) consequent.push(stmt);
1706
+ }
1707
+
1708
+ let alternate = null;
1709
+ if (peek().type === TOKEN.OTHERWISE) {
1710
+ const otherwiseLine = peek().line;
1711
+ advance();
1712
+ if (peek().type === TOKEN.IF && peek().line === otherwiseLine) {
1713
+ alternate = [parseElseIfChain()];
1714
+ } else {
1715
+ alternate = [];
1716
+ while (peek().type !== TOKEN.DONE) {
1717
+ if (peek().type === TOKEN.EOF) {
1718
+ throw new Error(makeError(
1719
+ 'Expected keyword "done" before end of file to close the "otherwise" block.',
1720
+ peek()
1721
+ ));
1722
+ }
1723
+ const stmt = parseStatement();
1724
+ if (stmt) alternate.push(stmt);
1725
+ }
1726
+ }
1727
+ }
1728
+
1729
+ return { type: 'IfStatement', condition, consequent, alternate };
1730
+ }
1731
+
1459
1732
  // ── Shared helpers ──────────────────────────────────────────────────────────
1460
1733
 
1461
1734
  function parseBody(context) {
@@ -1525,6 +1798,41 @@ function parse(tokens) {
1525
1798
 
1526
1799
  // primary → itemExpr | atom (postfix)*
1527
1800
  function parsePrimary() {
1801
+ // v1.0.0 — record constructor: `create a Person with name "Ada" and age 17`.
1802
+ // `create` then an article ("a"/"an") then a kind name then "with" pairs.
1803
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'create') {
1804
+ const a1 = peekAt(1);
1805
+ const a2 = peekAt(2);
1806
+ const a3 = peekAt(3);
1807
+ if (a1.type === TOKEN.IDENTIFIER &&
1808
+ (a1.value === 'a' || a1.value === 'an') &&
1809
+ a2.type === TOKEN.IDENTIFIER &&
1810
+ (a3.value === 'with')) {
1811
+ return parseCreateKind();
1812
+ }
1813
+ }
1814
+
1815
+ // v1.0.0 — concurrency combinators: `all of [a(), b()]`, `any of [...]`,
1816
+ // `settled of [...]`. Guard on the identifier + "of" lookahead so plain
1817
+ // property reads like `all of the_list` still work as well as prose.
1818
+ if (peek().type === TOKEN.IDENTIFIER &&
1819
+ (peek().value === 'all' || peek().value === 'any' || peek().value === 'settled') &&
1820
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
1821
+ const comboKw = advance().value; // all | any | settled
1822
+ advance(); // of
1823
+ const items = parseExpression();
1824
+ return { type: 'ConcurrencyExpression', combo: comboKw, items };
1825
+ }
1826
+
1827
+ // v1.0.0 — `spread of <collection>` unfolds an iterable into a new array.
1828
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'spread' &&
1829
+ peekAt(1).type === TOKEN.IDENTIFIER && peekAt(1).value === 'of') {
1830
+ advance(); // spread
1831
+ advance(); // of
1832
+ const collection = parsePrimary();
1833
+ return { type: 'SpreadExpression', collection };
1834
+ }
1835
+
1528
1836
  const item = tryParseItemExpression();
1529
1837
  if (item) return item;
1530
1838
  let node = parseAtom();
@@ -2000,6 +2308,215 @@ function parse(tokens) {
2000
2308
  return cleaned;
2001
2309
  }
2002
2310
 
2311
+ // ── IOPL-native features ──────────────────────────────────────────────────
2312
+
2313
+ // gather each item in list giving expr
2314
+ function parseGatherStatement() {
2315
+ advance(); // gather
2316
+ consume(TOKEN.EACH,
2317
+ 'Expected "each" after "gather".\n\nExample:\n gather each item in names giving item + "!"');
2318
+ const item = consume(TOKEN.IDENTIFIER,
2319
+ 'Expected an item name after "each".').value;
2320
+ consume(TOKEN.IN,
2321
+ `Expected "in" after "${item}".\n\nExample:\n gather each item in names giving item + "!"`);
2322
+ const collection = parseExpression();
2323
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'giving') {
2324
+ advance(); // giving
2325
+ } else {
2326
+ throw new Error(makeError(
2327
+ 'Expected "giving" after the collection expression.\n\nExample:\n gather each item in names giving item + "!"',
2328
+ peek()
2329
+ ));
2330
+ }
2331
+ const body = parseExpression();
2332
+ return { type: 'GatherStatement', item, collection, body };
2333
+ }
2334
+
2335
+ // filter each item in list when condition
2336
+ function parseFilterStatement() {
2337
+ advance(); // filter
2338
+ consume(TOKEN.EACH,
2339
+ 'Expected "each" after "filter".\n\nExample:\n filter each item in numbers when item is above 5');
2340
+ const item = consume(TOKEN.IDENTIFIER,
2341
+ 'Expected an item name after "each".').value;
2342
+ consume(TOKEN.IN,
2343
+ `Expected "in" after "${item}".\n\nExample:\n filter each item in numbers when item is above 5`);
2344
+ const collection = parseExpression();
2345
+ if (peek().type === TOKEN.WHEN ||
2346
+ (peek().type === TOKEN.IDENTIFIER && peek().value === 'when')) {
2347
+ advance(); // when
2348
+ } else {
2349
+ throw new Error(makeError(
2350
+ 'Expected "when" after the collection expression.\n\nExample:\n filter each item in numbers when item is above 5',
2351
+ peek()
2352
+ ));
2353
+ }
2354
+ const condition = parseAndCondition();
2355
+ return { type: 'FilterStatement', item, collection, condition };
2356
+ }
2357
+
2358
+ // total each item in list giving expr
2359
+ function parseTotalStatement() {
2360
+ advance(); // total
2361
+ consume(TOKEN.EACH,
2362
+ 'Expected "each" after "total".\n\nExample:\n total each item in numbers giving running + item');
2363
+ const item = consume(TOKEN.IDENTIFIER,
2364
+ 'Expected an item name after "each".').value;
2365
+ consume(TOKEN.IN,
2366
+ `Expected "in" after "${item}".\n\nExample:\n total each item in numbers giving running + item`);
2367
+ const collection = parseExpression();
2368
+ if (peek().type === TOKEN.IDENTIFIER && peek().value === 'giving') {
2369
+ advance(); // giving
2370
+ } else {
2371
+ throw new Error(makeError(
2372
+ 'Expected "giving" after the collection expression.\n\nExample:\n total each item in numbers giving running + item',
2373
+ peek()
2374
+ ));
2375
+ }
2376
+ const body = parseExpression();
2377
+ return { type: 'TotalStatement', item, collection, body };
2378
+ }
2379
+
2380
+ // match <expr> against ... done
2381
+ // "case1" → show "one"
2382
+ // "case2" → show "two"
2383
+ // otherwise → show "other"
2384
+ // match pattern "regex" in text as name
2385
+ function parseMatchStatement() {
2386
+ advance(); // match
2387
+
2388
+ // Regex match form: match pattern "regex" in text as name
2389
+ if (peek().type === TOKEN.PATTERN_KW) {
2390
+ advance(); // pattern
2391
+ const pattern = consume(TOKEN.STRING,
2392
+ 'Expected a regex pattern string after "pattern".\n\nExample:\n match pattern "^(\\d+)$" in text as result').value;
2393
+ const inToken = peek();
2394
+ if (inToken.type !== TOKEN.IN) {
2395
+ throw new Error(makeError(
2396
+ 'Expected "in" after the pattern string.\n\nExample:\n match pattern "^(\\d+)$" in text as result',
2397
+ inToken
2398
+ ));
2399
+ }
2400
+ advance(); // in
2401
+ const source = parseExpression();
2402
+ const asToken = peek();
2403
+ if (asToken.type !== TOKEN.AS) {
2404
+ throw new Error(makeError(
2405
+ 'Expected "as" after the source expression.\n\nExample:\n match pattern "^(\\d+)$" in text as result',
2406
+ asToken
2407
+ ));
2408
+ }
2409
+ advance(); // as
2410
+ const name = consume(TOKEN.IDENTIFIER,
2411
+ 'Expected a variable name after "as".\n\nExample:\n match pattern "^(\\d+)$" in text as result').value;
2412
+ return { type: 'RegexMatchStatement', pattern, source, name };
2413
+ }
2414
+
2415
+ // Pattern matching form: match expr against ... done
2416
+ const value = parseExpression();
2417
+ consume(TOKEN.AGAINST,
2418
+ 'Expected "after" the expression.\n\nExample:\n match color against\n "red" → show "stop"\n done');
2419
+ const cases = [];
2420
+ let defaultCase = null;
2421
+ while (peek().type !== TOKEN.DONE) {
2422
+ if (peek().type === TOKEN.EOF) {
2423
+ throw new Error(makeError(
2424
+ 'Expected keyword "done" to close the "match" block before end of file.',
2425
+ peek()
2426
+ ));
2427
+ }
2428
+ // otherwise → body
2429
+ if (peek().type === TOKEN.OTHERWISE) {
2430
+ advance(); // otherwise
2431
+ consume(TOKEN.ARROW,
2432
+ 'Expected "->" after "otherwise".\n\nExample:\n otherwise → show "other"');
2433
+ defaultCase = [];
2434
+ while (peek().type !== TOKEN.DONE) {
2435
+ if (peek().type === TOKEN.EOF) {
2436
+ throw new Error(makeError(
2437
+ 'Expected keyword "done" to close the "match" block before end of file.',
2438
+ peek()
2439
+ ));
2440
+ }
2441
+ const stmt = parseStatement();
2442
+ if (stmt) defaultCase.push(stmt);
2443
+ }
2444
+ break;
2445
+ }
2446
+ const test = parseExpression();
2447
+ consume(TOKEN.ARROW,
2448
+ 'Expected "->" after the case expression.\n\nExample:\n "red" → show "stop"');
2449
+ const caseBody = [];
2450
+ while (peek().type !== TOKEN.DONE && peek().type !== TOKEN.OTHERWISE &&
2451
+ !(peek().type === TOKEN.STRING || peek().type === TOKEN.NUMBER ||
2452
+ peek().type === TOKEN.TRUE_KW || peek().type === TOKEN.FALSE_KW ||
2453
+ peek().type === TOKEN.NULL_KW)) {
2454
+ if (peek().type === TOKEN.EOF) {
2455
+ throw new Error(makeError(
2456
+ 'Expected keyword "done" to close the "match" block before end of file.',
2457
+ peek()
2458
+ ));
2459
+ }
2460
+ const stmt = parseStatement();
2461
+ if (stmt) caseBody.push(stmt);
2462
+ }
2463
+ cases.push({ test, body: caseBody });
2464
+ }
2465
+ advance(); // done
2466
+ return { type: 'MatchStatement', value, cases, defaultCase };
2467
+ }
2468
+
2469
+ // emit "event.name" with data
2470
+ function parseEmitStatement() {
2471
+ advance(); // emit
2472
+ const eventToken = peek();
2473
+ const event = consume(TOKEN.STRING,
2474
+ 'Expected an event name string after "emit".\n\nExample:\n emit "data.received" with payload').value;
2475
+ const withToken = peek();
2476
+ if (withToken.type !== TOKEN.WITH) {
2477
+ throw new Error(makeError(
2478
+ 'Expected "with" after the event name.\n\nExample:\n emit "data.received" with payload',
2479
+ withToken
2480
+ ));
2481
+ }
2482
+ advance(); // with
2483
+ const data = parseExpression();
2484
+ return { type: 'EmitStatement', event, data };
2485
+ }
2486
+
2487
+ // stream "file.txt" as line ... done
2488
+ function parseStreamStatement() {
2489
+ advance(); // stream
2490
+ const filename = parseExpression();
2491
+ const asToken = peek();
2492
+ if (asToken.type !== TOKEN.AS) {
2493
+ throw new Error(makeError(
2494
+ 'Expected "as" after the filename.\n\nExample:\n stream "data.txt" as line\n show line\n done',
2495
+ asToken
2496
+ ));
2497
+ }
2498
+ advance(); // as
2499
+ const paramName = consume(TOKEN.IDENTIFIER,
2500
+ 'Expected a variable name after "as".\n\nExample:\n stream "data.txt" as line').value;
2501
+ const body = parseBody('"stream" block');
2502
+ return { type: 'StreamStatement', filename, paramName, body };
2503
+ }
2504
+
2505
+ // run in parallel ... done as results
2506
+ function parseRunParallelStatement() {
2507
+ advance(); // run (already checked by caller)
2508
+ advance(); // in
2509
+ advance(); // parallel
2510
+ const body = parseBody('"run in parallel" block');
2511
+ let resultName = null;
2512
+ if (peek().type === TOKEN.AS) {
2513
+ advance(); // as
2514
+ resultName = consume(TOKEN.IDENTIFIER,
2515
+ 'Expected a variable name after "as".\n\nExample:\n run in parallel\n remember a as wait for slowJob()\n done as results').value;
2516
+ }
2517
+ return { type: 'RunParallelStatement', body, resultName };
2518
+ }
2519
+
2003
2520
  // name(arg, arg, ...)
2004
2521
  // name(value to collection) → v1.1 collection/io forms (add, remove, write)
2005
2522
  function parseCallExpression() {
@@ -1,6 +1,6 @@
1
1
  // Single source of truth for the compiler version.
2
2
  // Consumed by cli.js help/version output.
3
3
 
4
- const VERSION = '0.1.7';
4
+ const VERSION = '1.0.0-beta';
5
5
 
6
6
  module.exports = { VERSION };