trebol 0.1.1 → 0.2.0

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 (300) hide show
  1. package/README.md +12 -1
  2. package/dist/cli.js +17097 -12238
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +12 -8
  12. package/dist/lib/core/eval.js +2 -2
  13. package/dist/lib/core/func.d.ts +4 -0
  14. package/dist/lib/core/machine/checks.d.ts +1 -0
  15. package/dist/lib/core/machine/checks.js +250 -0
  16. package/dist/lib/core/machine/events.d.ts +1 -0
  17. package/dist/lib/core/machine/events.js +365 -0
  18. package/dist/lib/core/machine/running.d.ts +1 -0
  19. package/dist/lib/core/machine/running.js +180 -0
  20. package/dist/lib/core/machine/shared.d.ts +1 -0
  21. package/dist/lib/core/machine/shared.js +123 -0
  22. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  23. package/dist/lib/core/machine/snapshot.js +244 -0
  24. package/dist/lib/core/machine/spec.d.ts +1 -0
  25. package/dist/lib/core/machine/spec.js +274 -0
  26. package/dist/lib/core/machine/words.d.ts +1 -0
  27. package/dist/lib/core/machine/words.js +98 -0
  28. package/dist/lib/core/machine.d.ts +1 -0
  29. package/dist/lib/core/machine.js +17 -0
  30. package/dist/lib/core/mezz.js +31 -0
  31. package/dist/lib/core/natives/control.js +2 -2
  32. package/dist/lib/core/natives/data.js +6 -1
  33. package/dist/lib/core/natives/series.js +59 -9
  34. package/dist/lib/core/natives/text.js +3 -1
  35. package/dist/lib/host/browser/calendar.d.ts +34 -0
  36. package/dist/lib/host/browser/calendar.js +268 -0
  37. package/dist/lib/host/browser/count.d.ts +15 -0
  38. package/dist/lib/host/browser/count.js +48 -0
  39. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  40. package/dist/lib/host/browser/dialogs.js +443 -0
  41. package/dist/lib/host/browser/drag.d.ts +70 -0
  42. package/dist/lib/host/browser/drag.js +268 -0
  43. package/dist/lib/host/browser/drawings.d.ts +30 -0
  44. package/dist/lib/host/browser/drawings.js +176 -0
  45. package/dist/lib/host/browser/events.d.ts +16 -0
  46. package/dist/lib/host/browser/events.js +125 -0
  47. package/dist/lib/host/browser/faces.d.ts +35 -0
  48. package/dist/lib/host/browser/faces.js +131 -0
  49. package/dist/lib/host/browser/fields.d.ts +16 -0
  50. package/dist/lib/host/browser/fields.js +322 -0
  51. package/dist/lib/host/browser/keys.d.ts +29 -0
  52. package/dist/lib/host/browser/keys.js +151 -0
  53. package/dist/lib/host/browser/lookup.d.ts +25 -0
  54. package/dist/lib/host/browser/lookup.js +275 -0
  55. package/dist/lib/host/browser/menus.d.ts +20 -0
  56. package/dist/lib/host/browser/menus.js +149 -0
  57. package/dist/lib/host/browser/mount.d.ts +92 -0
  58. package/dist/lib/host/browser/mount.js +448 -0
  59. package/dist/lib/host/browser/orbits.d.ts +90 -0
  60. package/dist/lib/host/browser/orbits.js +379 -0
  61. package/dist/lib/host/browser/page.d.ts +13 -0
  62. package/dist/lib/host/browser/page.js +35 -0
  63. package/dist/lib/host/browser/richtext.d.ts +19 -0
  64. package/dist/lib/host/browser/richtext.js +379 -0
  65. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  66. package/dist/lib/host/browser/row-faces.js +74 -0
  67. package/dist/lib/host/browser/screens.d.ts +37 -0
  68. package/dist/lib/host/browser/screens.js +140 -0
  69. package/dist/lib/host/browser/swipe.d.ts +31 -0
  70. package/dist/lib/host/browser/swipe.js +178 -0
  71. package/dist/lib/host/browser/sync.d.ts +24 -0
  72. package/dist/lib/host/browser/sync.js +450 -0
  73. package/dist/lib/host/browser/tables.d.ts +65 -0
  74. package/dist/lib/host/browser/tables.js +505 -0
  75. package/dist/lib/host/browser/times.d.ts +14 -0
  76. package/dist/lib/host/browser/times.js +114 -0
  77. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  78. package/dist/lib/host/browser/toolbars.js +129 -0
  79. package/dist/lib/host/browser/trees.d.ts +20 -0
  80. package/dist/lib/host/browser/trees.js +235 -0
  81. package/dist/lib/host/browser/uploads.d.ts +29 -0
  82. package/dist/lib/host/browser/uploads.js +370 -0
  83. package/dist/lib/host/browser/widget.d.ts +13 -0
  84. package/dist/lib/host/browser/widget.js +105 -0
  85. package/dist/lib/host/browser.d.ts +25 -0
  86. package/dist/lib/host/browser.js +129 -3964
  87. package/dist/lib/host/devlog.d.ts +11 -0
  88. package/dist/lib/host/devlog.js +13 -0
  89. package/dist/lib/host/devtools/console.d.ts +9 -0
  90. package/dist/lib/host/devtools/console.js +104 -0
  91. package/dist/lib/host/devtools/faces.d.ts +25 -0
  92. package/dist/lib/host/devtools/faces.js +169 -0
  93. package/dist/lib/host/devtools/log.d.ts +3 -0
  94. package/dist/lib/host/devtools/log.js +32 -0
  95. package/dist/lib/host/devtools/machines.d.ts +34 -0
  96. package/dist/lib/host/devtools/machines.js +212 -0
  97. package/dist/lib/host/devtools/read.d.ts +13 -0
  98. package/dist/lib/host/devtools/read.js +52 -0
  99. package/dist/lib/host/devtools/state.d.ts +90 -0
  100. package/dist/lib/host/devtools/state.js +123 -0
  101. package/dist/lib/host/devtools.d.ts +19 -0
  102. package/dist/lib/host/devtools.js +238 -0
  103. package/dist/lib/host/fonts/OFL.txt +93 -0
  104. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  105. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  106. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  107. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  108. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  109. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  110. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  111. package/dist/lib/host/fonts.d.ts +11 -0
  112. package/dist/lib/host/fonts.js +30 -0
  113. package/dist/lib/host/icons.js +4 -0
  114. package/dist/lib/host/index.d.ts +5 -1
  115. package/dist/lib/host/index.js +110 -5
  116. package/dist/lib/host/js.d.ts +5 -0
  117. package/dist/lib/host/js.js +130 -0
  118. package/dist/lib/host/locale.d.ts +18 -0
  119. package/dist/lib/host/locale.js +75 -5
  120. package/dist/lib/host/markdown.d.ts +6 -0
  121. package/dist/lib/host/markdown.js +18 -0
  122. package/dist/lib/host/modules.d.ts +4 -0
  123. package/dist/lib/host/modules.js +157 -20
  124. package/dist/lib/host/motion.d.ts +32 -0
  125. package/dist/lib/host/motion.js +190 -0
  126. package/dist/lib/host/net.js +14 -3
  127. package/dist/lib/host/orbit.d.ts +121 -0
  128. package/dist/lib/host/orbit.js +374 -0
  129. package/dist/lib/host/palette.d.ts +17 -0
  130. package/dist/lib/host/palette.js +131 -0
  131. package/dist/lib/host/rows.d.ts +14 -0
  132. package/dist/lib/host/rows.js +29 -0
  133. package/dist/lib/host/scheduler.d.ts +8 -0
  134. package/dist/lib/host/scheduler.js +64 -36
  135. package/dist/lib/host/share.d.ts +31 -0
  136. package/dist/lib/host/share.js +134 -0
  137. package/dist/lib/host/theme/classic.d.ts +1 -0
  138. package/dist/lib/host/theme/classic.js +120 -0
  139. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  140. package/dist/lib/host/theme/cupertino.js +65 -0
  141. package/dist/lib/host/theme/data.d.ts +1 -0
  142. package/dist/lib/host/theme/data.js +109 -0
  143. package/dist/lib/host/theme/devtools.d.ts +1 -0
  144. package/dist/lib/host/theme/devtools.js +166 -0
  145. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  146. package/dist/lib/host/theme/dialogs.js +63 -0
  147. package/dist/lib/host/theme/fluent.d.ts +1 -0
  148. package/dist/lib/host/theme/fluent.js +64 -0
  149. package/dist/lib/host/theme/forms.d.ts +1 -0
  150. package/dist/lib/host/theme/forms.js +239 -0
  151. package/dist/lib/host/theme/fusion.d.ts +1 -0
  152. package/dist/lib/host/theme/fusion.js +66 -0
  153. package/dist/lib/host/theme/layout.d.ts +1 -0
  154. package/dist/lib/host/theme/layout.js +166 -0
  155. package/dist/lib/host/theme/media.d.ts +1 -0
  156. package/dist/lib/host/theme/media.js +106 -0
  157. package/dist/lib/host/theme/navigation.d.ts +1 -0
  158. package/dist/lib/host/theme/navigation.js +36 -0
  159. package/dist/lib/host/theme/panes.d.ts +1 -0
  160. package/dist/lib/host/theme/panes.js +99 -0
  161. package/dist/lib/host/theme/popups.d.ts +1 -0
  162. package/dist/lib/host/theme/popups.js +149 -0
  163. package/dist/lib/host/theme/responsive.d.ts +1 -0
  164. package/dist/lib/host/theme/responsive.js +103 -0
  165. package/dist/lib/host/theme/toasts.d.ts +1 -0
  166. package/dist/lib/host/theme/toasts.js +62 -0
  167. package/dist/lib/host/theme.js +19 -1179
  168. package/dist/lib/ui/dialect/events.trb +276 -0
  169. package/dist/lib/ui/dialect/faces.trb +135 -0
  170. package/dist/lib/ui/dialect/layout.trb +283 -0
  171. package/dist/lib/ui/dialect/palette.trb +87 -0
  172. package/dist/lib/ui/dialect/problems.trb +174 -0
  173. package/dist/lib/ui/dialect/reactions.trb +198 -0
  174. package/dist/lib/ui/dialect/rows.trb +208 -0
  175. package/dist/lib/ui/dialect/screens.trb +293 -0
  176. package/dist/lib/ui/dialect/steps.trb +203 -0
  177. package/dist/lib/ui/dialect/validation.trb +137 -0
  178. package/dist/lib/ui/dialect/view.trb +110 -0
  179. package/dist/lib/ui/dialect/walking.trb +98 -0
  180. package/dist/lib/ui/dialect/words.trb +70 -0
  181. package/dist/lib/ui/i18n.trb +23 -0
  182. package/dist/lib/ui/test.trb +12 -0
  183. package/dist/lib/ui/testing/actions.trb +421 -0
  184. package/dist/lib/ui/testing/faces.trb +148 -0
  185. package/dist/lib/ui/testing/server.trb +73 -0
  186. package/dist/lib/ui/ui.trb +25 -1234
  187. package/dist/trebol.js +4882 -1088
  188. package/editors/vscode/README.md +15 -0
  189. package/editors/vscode/language-configuration.json +28 -0
  190. package/editors/vscode/package.json +42 -0
  191. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  192. package/package.json +7 -3
  193. package/src/core/boot.ts +10 -7
  194. package/src/core/eval.ts +2 -2
  195. package/src/core/func.ts +4 -0
  196. package/src/core/machine/checks.ts +251 -0
  197. package/src/core/machine/events.ts +366 -0
  198. package/src/core/machine/running.ts +181 -0
  199. package/src/core/machine/shared.ts +124 -0
  200. package/src/core/machine/snapshot.ts +245 -0
  201. package/src/core/machine/spec.ts +275 -0
  202. package/src/core/machine/words.ts +99 -0
  203. package/src/core/machine.ts +20 -0
  204. package/src/core/mezz.ts +31 -0
  205. package/src/core/natives/control.ts +2 -2
  206. package/src/core/natives/data.ts +6 -1
  207. package/src/core/natives/parse.ts +1 -1
  208. package/src/core/natives/series.ts +55 -11
  209. package/src/core/natives/text.ts +2 -1
  210. package/src/host/browser/calendar.ts +264 -0
  211. package/src/host/browser/count.ts +48 -0
  212. package/src/host/browser/dialogs.ts +439 -0
  213. package/src/host/browser/drag.ts +278 -0
  214. package/src/host/browser/drawings.ts +178 -0
  215. package/src/host/browser/events.ts +117 -0
  216. package/src/host/browser/faces.ts +143 -0
  217. package/src/host/browser/fields.ts +315 -0
  218. package/src/host/browser/keys.ts +145 -0
  219. package/src/host/browser/lookup.ts +262 -0
  220. package/src/host/browser/menus.ts +146 -0
  221. package/src/host/browser/mount.ts +420 -0
  222. package/src/host/browser/orbits.ts +418 -0
  223. package/src/host/browser/page.ts +43 -0
  224. package/src/host/browser/richtext.ts +346 -0
  225. package/src/host/browser/row-faces.ts +75 -0
  226. package/src/host/browser/screens.ts +142 -0
  227. package/src/host/browser/swipe.ts +168 -0
  228. package/src/host/browser/sync.ts +354 -0
  229. package/src/host/browser/tables.ts +461 -0
  230. package/src/host/browser/times.ts +115 -0
  231. package/src/host/browser/toolbars.ts +126 -0
  232. package/src/host/browser/trees.ts +205 -0
  233. package/src/host/browser/uploads.ts +367 -0
  234. package/src/host/browser/widget.ts +103 -0
  235. package/src/host/browser.ts +128 -3747
  236. package/src/host/devlog.ts +16 -0
  237. package/src/host/devtools/console.ts +102 -0
  238. package/src/host/devtools/faces.ts +160 -0
  239. package/src/host/devtools/log.ts +34 -0
  240. package/src/host/devtools/machines.ts +208 -0
  241. package/src/host/devtools/read.ts +58 -0
  242. package/src/host/devtools/state.ts +148 -0
  243. package/src/host/devtools.ts +238 -0
  244. package/src/host/fonts/OFL.txt +93 -0
  245. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  246. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  247. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  248. package/src/host/fonts/inter-greek.woff2 +0 -0
  249. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  250. package/src/host/fonts/inter-latin.woff2 +0 -0
  251. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  252. package/src/host/fonts.ts +40 -0
  253. package/src/host/icons.ts +4 -0
  254. package/src/host/index.ts +110 -5
  255. package/src/host/js.ts +127 -0
  256. package/src/host/locale.ts +71 -5
  257. package/src/host/markdown.ts +18 -0
  258. package/src/host/modules.ts +150 -22
  259. package/src/host/motion.ts +204 -0
  260. package/src/host/net.ts +13 -3
  261. package/src/host/orbit.ts +432 -0
  262. package/src/host/palette.ts +139 -0
  263. package/src/host/rows.ts +30 -0
  264. package/src/host/scheduler.ts +67 -36
  265. package/src/host/share.ts +168 -0
  266. package/src/host/theme/classic.ts +121 -0
  267. package/src/host/theme/cupertino.ts +66 -0
  268. package/src/host/theme/data.ts +110 -0
  269. package/src/host/theme/devtools.ts +167 -0
  270. package/src/host/theme/dialogs.ts +64 -0
  271. package/src/host/theme/fluent.ts +65 -0
  272. package/src/host/theme/forms.ts +240 -0
  273. package/src/host/theme/fusion.ts +67 -0
  274. package/src/host/theme/layout.ts +167 -0
  275. package/src/host/theme/media.ts +107 -0
  276. package/src/host/theme/navigation.ts +37 -0
  277. package/src/host/theme/panes.ts +100 -0
  278. package/src/host/theme/popups.ts +150 -0
  279. package/src/host/theme/responsive.ts +104 -0
  280. package/src/host/theme/toasts.ts +63 -0
  281. package/src/host/theme.ts +19 -1179
  282. package/src/ui/dialect/events.trb +276 -0
  283. package/src/ui/dialect/faces.trb +135 -0
  284. package/src/ui/dialect/layout.trb +283 -0
  285. package/src/ui/dialect/palette.trb +87 -0
  286. package/src/ui/dialect/problems.trb +174 -0
  287. package/src/ui/dialect/reactions.trb +198 -0
  288. package/src/ui/dialect/rows.trb +208 -0
  289. package/src/ui/dialect/screens.trb +293 -0
  290. package/src/ui/dialect/steps.trb +203 -0
  291. package/src/ui/dialect/validation.trb +137 -0
  292. package/src/ui/dialect/view.trb +110 -0
  293. package/src/ui/dialect/walking.trb +98 -0
  294. package/src/ui/dialect/words.trb +70 -0
  295. package/src/ui/i18n.trb +23 -0
  296. package/src/ui/test.trb +12 -0
  297. package/src/ui/testing/actions.trb +421 -0
  298. package/src/ui/testing/faces.trb +148 -0
  299. package/src/ui/testing/server.trb +73 -0
  300. package/src/ui/ui.trb +25 -1234
@@ -0,0 +1,275 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part one: reading the block of a machine into states, one
2
+ // level at a time, and finding the words it meant.
3
+
4
+ export const SPEC = String.raw`
5
+ ; The engine of machine: states are objects in a block, known by their place
6
+ ; in it (the root is 1), so a machine holds no loops of references and molds.
7
+ machine-engine: make object! [
8
+
9
+ words: [data initial state final parallel from no on-enter on-exit on-activity on-save timeout ttl defer run invoke shared]
10
+
11
+ ; the events a machine sends itself
12
+ own: [error timeout expired done]
13
+
14
+ ; send on a socket, when the host has sockets: send itself is redefined below
15
+ socket: either value? 'send [:send] [none]
16
+
17
+ ; where the last mistake was, for trebol check
18
+ failed: none
19
+
20
+ fail: function [message [string!] place [block!]] [
21
+ set 'failed place
22
+ e: make error! rejoin ["machine: " message]
23
+ e/near: copy/part place 4
24
+ e/at: loaded-at place
25
+ do e
26
+ ]
27
+
28
+ node: function [m id] [pick m/nodes id]
29
+
30
+ ; --- did you mean: the word near enough to one that is not known (strat: start) ---
31
+
32
+ ; Optimal string alignment distance: letters put in, taken out, changed, or two neighbours swapped.
33
+ distance: function [a [string!] b [string!]] [
34
+ n: length? b
35
+ before: none
36
+ prev: copy []
37
+ repeat j n + 1 [append prev j - 1]
38
+ repeat i length? a [
39
+ row: reduce [i]
40
+ repeat j n [
41
+ d: min min (pick prev j + 1) + 1 (last row) + 1 (pick prev j) + either (pick a i) = (pick b j) [0] [1]
42
+ if all [i > 1 j > 1 (pick a i) = (pick b j - 1) (pick a i - 1) = (pick b j)] [
43
+ d: min d (pick before j - 1) + 1
44
+ ]
45
+ append row d
46
+ ]
47
+ before: prev
48
+ prev: row
49
+ ]
50
+ last prev
51
+ ]
52
+
53
+ ; The word near enough to w to be what it meant, or none: one letter off up to four letters, two longer;
54
+ ; nothing for one or two letters, which are near too many.
55
+ nearest: function [w words [block!]] [
56
+ t: form w
57
+ if 3 > length? t [return none]
58
+ most: either 4 < length? t [2] [1]
59
+ best: none
60
+ foreach c words [
61
+ d: distance t form c
62
+ if all [d > 0 d <= most] [best: c most: d - 1]
63
+ ]
64
+ best
65
+ ]
66
+
67
+ ; "; did you mean start?" when one of the words is near enough; "" otherwise.
68
+ suggest: function [w words [block!]] [
69
+ either best: nearest w words [rejoin ["; did you mean " best "?"]] [""]
70
+ ]
71
+
72
+ state-names: function [m] [collect [foreach n next m/nodes [keep n/name]]]
73
+
74
+ data-fields: function [m] [collect [foreach v any [m/data-spec []] [if set-word? :v [keep to word! v]]]]
75
+
76
+ add-node: function [m name parent] [
77
+ n: make object! [
78
+ id: name: parent: initial: enter: exit: activity: outcome: place: timeout: run: invoke: with: for: in: none
79
+ children: copy [] transitions: copy [] defer: copy [] history: true final: false parallel: false
80
+ ]
81
+ append m/nodes n
82
+ n/id: length? m/nodes n/name: name n/parent: parent
83
+ n
84
+ ]
85
+
86
+ child: function [m id name] [
87
+ foreach c (select node m id 'children) [if name = select node m c 'name [return c]]
88
+ none
89
+ ]
90
+
91
+ ; A target: a name or a path, looked up from the scope, then from each level above.
92
+ find-state: function [m scope name] [
93
+ names: either path? name [to block! name] [reduce [name]]
94
+ while [scope] [
95
+ id: scope
96
+ foreach w names [unless id: child m id w [break]]
97
+ if id [return id]
98
+ scope: select node m scope 'parent
99
+ ]
100
+ none
101
+ ]
102
+
103
+ ; A hook, guard or set block of the spec: bound to the machine's data and event.
104
+ code: function [m spec what] [
105
+ b: second spec
106
+ unless block? :b [fail rejoin [what " takes a block"] spec]
107
+ bind b m/scope
108
+ append/only m/code spec
109
+ b
110
+ ]
111
+
112
+ ; Reads the words of one level (the machine or a state) into node n.
113
+ level: function [m n spec] [
114
+ froms: copy []
115
+ while [not tail? spec] [
116
+ here: spec
117
+ w: first spec
118
+ unless word? :w [fail rejoin [mold :w " is not a word of machine"] here]
119
+ spec: next spec
120
+ switch/default w [
121
+ data [
122
+ if n/id <> 1 [fail "data goes at the top of the machine" here]
123
+ unless block? first spec [fail "data takes a block" here]
124
+ m/data-spec: first spec
125
+ spec: next spec
126
+ ]
127
+ initial [
128
+ unless word? first spec [fail "initial takes the name of a state" here]
129
+ n/initial: here
130
+ spec: next spec
131
+ ]
132
+ state final parallel [
133
+ name: first spec
134
+ unless word? :name [fail rejoin [w " takes a name"] here]
135
+ if child m n/id name [fail rejoin ["two states called " name] here]
136
+ c: add-node m name n/id
137
+ c/place: here
138
+ append n/children c/id
139
+ spec: next spec
140
+ c/parallel: w = 'parallel
141
+ if w = 'final [
142
+ c/final: true
143
+ if all [word? first spec not find words first spec] [
144
+ c/outcome: first spec
145
+ spec: next spec
146
+ ]
147
+ ]
148
+ if block? first spec [
149
+ level m c first spec
150
+ spec: next spec
151
+ ]
152
+ if all [c/final not empty? c/children] [fail rejoin ["final " name " has states inside"] here]
153
+ if c/parallel [
154
+ if 2 > length? c/children [fail rejoin ["parallel " name " needs two states or more"] here]
155
+ foreach k c/children [
156
+ if select node m k 'final [fail rejoin ["parallel " name " holds states, not final ones"] here]
157
+ ]
158
+ ]
159
+ ]
160
+ shared [
161
+ if n/id <> 1 [fail "shared goes at the top of the machine" here]
162
+ unless word? first spec [fail "shared takes a name, the same in every tab" here]
163
+ m/shared: first spec
164
+ spec: next spec
165
+ ]
166
+ on-save [
167
+ if n/id <> 1 [fail "on-save goes at the top of the machine" here]
168
+ b: code m here w
169
+ ; snapshot, inside it, is the machine as it is after the event
170
+ m/saver: make object! [snapshot: none]
171
+ bind b m/saver
172
+ m/on-save: b
173
+ spec: next spec
174
+ ]
175
+ on-enter on-exit on-activity [
176
+ if n/id = 1 [fail rejoin [w " goes inside a state"] here]
177
+ b: code m here w
178
+ switch w [on-enter [n/enter: b] on-exit [n/exit: b] on-activity [n/activity: b]]
179
+ spec: next spec
180
+ ]
181
+ timeout [
182
+ if n/id = 1 [fail "timeout goes inside a state; the whole machine takes ttl" here]
183
+ if n/final [fail "a final state takes no timeout" here]
184
+ v: first spec
185
+ unless any [number? :v time? :v all [path? :v 'data = first v 2 = length? v]] [
186
+ fail "timeout takes seconds, a time or data/field" here
187
+ ]
188
+ n/timeout: v
189
+ spec: next spec
190
+ ]
191
+ run invoke [
192
+ if n/id = 1 [fail rejoin [w " goes inside a state"] here]
193
+ if n/final [fail rejoin ["a final state takes no " w] here]
194
+ if any [n/run n/invoke] [fail "a state runs one thing: one run or one invoke" here]
195
+ either w = 'run [
196
+ n/run: code m here 'run
197
+ spec: next spec
198
+ ] [
199
+ v: first spec
200
+ unless any [block? :v word? :v path? :v] [fail "invoke takes the block of a machine, or a word for one" here]
201
+ ; a machine written here is made now, so its mistakes show at once
202
+ if block? v [inner: make-machine v]
203
+ n/invoke: v
204
+ spec: next spec
205
+ ; one machine for each item of a block of the data
206
+ if 'for = first spec [
207
+ unless all [word? second spec 'in = third spec] [
208
+ fail "invoke ... for takes a word, in and data/field" spec
209
+ ]
210
+ v: fourth spec
211
+ unless all [path? :v 'data = first v 2 = length? v] [
212
+ fail "invoke ... for item in takes data/field" spec
213
+ ]
214
+ n/for: second spec
215
+ n/in: v
216
+ spec: skip spec 4
217
+ ]
218
+ if 'with = first spec [
219
+ n/with: code m spec 'with
220
+ if block? v [
221
+ fields: collect [foreach x any [inner/data-spec []] [if set-word? :x [keep to word! x]]]
222
+ foreach x n/with [
223
+ if all [set-word? :x not find fields to word! x] [
224
+ fail rejoin ["the machine invoked in " n/name " has no data " to word! x suggest to word! x fields] spec
225
+ ]
226
+ ]
227
+ ]
228
+ spec: skip spec 2
229
+ ]
230
+ ]
231
+ ]
232
+ ttl [
233
+ if n/id <> 1 [fail "ttl goes at the top of the machine" here]
234
+ v: first spec
235
+ unless any [number? :v time? :v] [fail "ttl takes seconds or a time" here]
236
+ m/ttl: v
237
+ spec: next spec
238
+ ]
239
+ defer [
240
+ e: first spec
241
+ names: case [
242
+ word? :e [reduce [e]]
243
+ all [block? :e not empty? e parse e [some word!]] [e]
244
+ true [fail "defer takes the name of an event or a block of them" here]
245
+ ]
246
+ foreach w names [
247
+ if find own w [fail rejoin [w " is the machine's own event; it cannot wait"] here]
248
+ ]
249
+ append n/defer names
250
+ spec: next spec
251
+ ]
252
+ no [
253
+ unless 'history = first spec [fail "no takes history" here]
254
+ n/history: false
255
+ spec: next spec
256
+ ]
257
+ from [spec: transition m n here]
258
+ ] [
259
+ fail rejoin [w " is not a word of machine" suggest w words] here
260
+ ]
261
+ ]
262
+ if all [n/parallel n/initial] [fail rejoin ["parallel " n/name " has no initial: all its states start at once"] n/initial]
263
+ if all [not empty? n/children none? n/initial not n/parallel] [
264
+ fail either n/id = 1 ["the machine has no initial state"] [rejoin ["state " n/name " has no initial"]]
265
+ any [n/place spec]
266
+ ]
267
+ if n/initial [
268
+ place: n/initial
269
+ unless n/initial: child m n/id second place [
270
+ fail rejoin ["no state " second place " here" suggest second place state-names m] place
271
+ ]
272
+ ]
273
+ ]
274
+
275
+ `;
@@ -0,0 +1,99 @@
1
+ // Machines in Trebol (see machine.ts). The words a program uses: machine, snapshot, send, in?, graph-of, can?
2
+ // and restart.
3
+
4
+ export const WORDS = String.raw`
5
+ machine: function [
6
+ "Makes a state machine from a block and starts it in its initial state."
7
+ spec [block!]
8
+ /from {Resumes it where a snapshot of it left it, instead}
9
+ saved [block!] "What snapshot gave"
10
+ ] [
11
+ e: machine-engine
12
+ m: e/make-machine spec
13
+ e/fresh m
14
+ case [
15
+ m/shared [e/share-start m all [from saved]]
16
+ from [e/resume m saved]
17
+ true [e/start m]
18
+ ]
19
+ ]
20
+
21
+ snapshot: function [
22
+ "The state of a machine as a block of plain data, which machine/from resumes."
23
+ m [object!]
24
+ ] [
25
+ ; a copy of a shared machine has only what the owner told it
26
+ either m/owner [machine-engine/snapshot m] [copy/deep m/seen]
27
+ ]
28
+
29
+ send: function [
30
+ "Sends an event to a machine and gives the state it is in then, or sends a value on a socket."
31
+ m [object! handle!] "A machine, or a socket"
32
+ event [any-value!] "The event: a word, or a block with the word and its fields; for a socket, the value"
33
+ ] [
34
+ if handle? m [return machine-engine/socket m :event]
35
+ unless any [word? :event block? :event] [
36
+ do make error! "send: the event is a word, or a block that starts with one"
37
+ ]
38
+ either word? event [name: event ev: make object! []] [
39
+ name: first event
40
+ unless word? :name [do make error! "send: the block starts with the name of the event"]
41
+ ev: make object! next event
42
+ ]
43
+ if find machine-engine/own name [do make error! rejoin ["send: " name " is the machine's own event"]]
44
+ ; an event no way of the machine names is a mistake: it would be lost
45
+ if all [known: machine-engine/events m not find known name] [
46
+ do make error! rejoin ["send: the machine has no way on " name machine-engine/suggest name known]
47
+ ]
48
+ unless m/owner [
49
+ ; a copy: the owner's tab runs it, and its answer brings the new state
50
+ share-ask m/channel mold/all reduce ['event name body-of ev]
51
+ return m/state
52
+ ]
53
+ append/only m/queue reduce [name ev]
54
+ unless m/busy [machine-engine/run m]
55
+ m/state
56
+ ]
57
+
58
+ in?: function [
59
+ "Whether a machine is in a state now: its name, or a path of names from the top."
60
+ m [object!]
61
+ name [word! path!]
62
+ ] [
63
+ e: machine-engine
64
+ either word? name [
65
+ foreach id m/on [if name = select e/node m id 'name [return true]]
66
+ false
67
+ ] [
68
+ id: 1
69
+ foreach w to block! name [unless id: e/child m id w [return false]]
70
+ e/on? m id
71
+ ]
72
+ ]
73
+
74
+ graph-of: function [
75
+ "The states of a machine and the ways between them, as plain data; of a block, the machine it would make, run not."
76
+ machine [object! block!] "A machine, or the block of one"
77
+ ] [
78
+ e: machine-engine
79
+ e/graph either block? machine [e/make-machine machine] [machine]
80
+ ]
81
+
82
+ can?: function [
83
+ "Whether a machine has a way out on an event from where it is now; what where would say is not asked."
84
+ m [object!]
85
+ event [word!]
86
+ ] [
87
+ machine-engine/can m event
88
+ ]
89
+
90
+ restart: function [
91
+ "Starts a machine again from its initial state, with its data as it was made."
92
+ m [object!]
93
+ ] [
94
+ either m/owner [machine-engine/again m] [
95
+ share-ask m/channel mold/all [restart]
96
+ m
97
+ ]
98
+ ]
99
+ `;
@@ -0,0 +1,20 @@
1
+ // Machines: statecharts written as a dialect, `machine [...]`, and the engine
2
+ // that runs them, all in Trebol. Evaluated into lib after the mezzanines by
3
+ // `makeRuntime`, so every program has them, with or without a UI.
4
+ //
5
+ // The semantics follow UML statecharts: a transition leaves the states up to
6
+ // the one the source and the target share, innermost first, and enters down
7
+ // to the target, outermost first; each level runs only its own hooks.
8
+
9
+ //
10
+ // The Trebol code is in machine/, a file per part, joined here in order.
11
+
12
+ import { SPEC } from './machine/spec.ts';
13
+ import { CHECKS } from './machine/checks.ts';
14
+ import { RUNNING } from './machine/running.ts';
15
+ import { EVENTS } from './machine/events.ts';
16
+ import { SNAPSHOT } from './machine/snapshot.ts';
17
+ import { SHARED } from './machine/shared.ts';
18
+ import { WORDS } from './machine/words.ts';
19
+
20
+ export const MACHINE = `\n${[SPEC, CHECKS, RUNNING, EVENTS, SNAPSHOT, SHARED, WORDS].map(part => part.slice(1)).join('')}`;
package/src/core/mezz.ts CHANGED
@@ -358,4 +358,35 @@ suffix?: func [
358
358
  ]
359
359
  ]
360
360
  ]
361
+ iban?: func [
362
+ "True if the text is an IBAN (ISO 13616) of a country that has them, of that country's length and with the right check digits. Spaces may separate its groups."
363
+ text [string!]
364
+ /local s n r letter digit alnum
365
+ ][
366
+ letter: charset [#"A" - #"Z"]
367
+ digit: charset [#"0" - #"9"]
368
+ alnum: union letter digit
369
+ s: uppercase replace/all copy text " " ""
370
+ to logic! all [
371
+ parse s [2 letter 2 digit some alnum]
372
+ n: select [
373
+ "AD" 24 "AE" 23 "AL" 28 "AT" 20 "AZ" 28 "BA" 20 "BE" 16 "BG" 22 "BH" 22 "BI" 27 "BR" 29 "BY" 28 "CH" 21 "CR" 22 "CY" 28
374
+ "CZ" 24 "DE" 22 "DJ" 27 "DK" 18 "DO" 28 "EE" 20 "EG" 29 "ES" 24 "FI" 18 "FK" 18 "FO" 18 "FR" 27 "GB" 22 "GE" 22 "GI" 23
375
+ "GL" 18 "GR" 27 "GT" 28 "HN" 28 "HR" 21 "HU" 28 "IE" 22 "IL" 23 "IQ" 23 "IS" 26 "IT" 27 "JO" 30 "KW" 30 "KZ" 20 "LB" 28
376
+ "LC" 32 "LI" 21 "LT" 20 "LU" 20 "LV" 21 "LY" 25 "MC" 27 "MD" 24 "ME" 22 "MK" 19 "MN" 20 "MR" 27 "MT" 31 "MU" 30 "NI" 28
377
+ "NL" 18 "NO" 15 "OM" 23 "PK" 24 "PL" 28 "PS" 29 "PT" 25 "QA" 29 "RO" 24 "RS" 22 "RU" 33 "SA" 24 "SC" 31 "SD" 18 "SE" 24
378
+ "SI" 19 "SK" 24 "SM" 27 "SO" 23 "ST" 25 "SV" 28 "TL" 23 "TN" 24 "TR" 26 "UA" 29 "VA" 22 "VG" 24 "XK" 20 "YE" 30
379
+ ] copy/part s 2
380
+ n = length? s
381
+ (
382
+ ; the four first characters go to the end, each letter counts as 10 to 35, and the number
383
+ ; they make leaves 1 when divided by 97
384
+ r: 0
385
+ foreach c append copy skip s 4 copy/part s 4 [
386
+ r: either find letter c [mod r * 100 + (to integer! c) - 55 97] [mod r * 10 + (to integer! c) - 48 97]
387
+ ]
388
+ r = 1
389
+ )
390
+ ]
391
+ ]
361
392
  `;
@@ -4,7 +4,7 @@
4
4
  // call with the arguments; later phases receive the value of the block or
5
5
  // expression the native asked the evaluator for.
6
6
 
7
- import { K, kindName } from '../kinds.ts';
7
+ import { K } from '../kinds.ts';
8
8
  import * as I64 from '../int64.ts';
9
9
  import { NONE, UNSET, TRUE, block, context, type Cell, type BlockCell, type Ctx, type WordCell } from '../values.ts';
10
10
  import { bindSeries, copyDeep, setWord } from '../ctx.ts';
@@ -311,7 +311,7 @@ native('until', '["Evaluates the block until it gives true." body [block!]]', (_
311
311
  });
312
312
 
313
313
  native('break', '["Leaves the loop it is in." /return value [any-value!]]', (_t, f) =>
314
- ({ k: 'throw', e: { kind: T.Break, v: f.args[0]!.t === K.Logic ? f.args[1]! : NONE, name: null } }));
314
+ ({ k: 'throw', e: { kind: T.Break, v: f.args[0]!.t === K.Logic ? f.args[1]! : UNSET, name: null } }));
315
315
 
316
316
  native('continue', '["Goes on with the next turn of the loop it is in." ]', () => ({ k: 'throw', e: { kind: T.Continue, v: UNSET, name: null } }));
317
317
 
@@ -236,8 +236,13 @@ native('spec-of', '["A copy of a function\'s spec block." value [any-function!]]
236
236
  return value({ t: K.Block, s: copyDeep(spec.s, spec.i), i: 0 });
237
237
  });
238
238
 
239
- native('body-of', '["A copy of a function\'s body block, or none for a native." value [any-function!]]', (_t, f) => {
239
+ native('body-of', '["A copy of a function\'s body block, or none for a native; for an object, its fields as field: value." value [any-function! any-object!]]', (_t, f) => {
240
240
  const c = underOp(f.args[0]!);
241
+ if (c.t === K.Object || c.t === K.Error) {
242
+ const out: Cell[] = [];
243
+ c.ctx.keys.forEach((k, i) => out.push(wordOf(K.SetWord, k), c.ctx.vals[i]!));
244
+ return value(blockOf(K.Block, out));
245
+ }
241
246
  return value(c.t === K.Function ? { t: K.Block, s: copyDeep(((c as OpaqueCell).p as FuncDef).body), i: 0 } : NONE);
242
247
  });
243
248
 
@@ -17,7 +17,7 @@ import { intern, canon, spell } from '../symbols.ts';
17
17
  import * as I64 from '../int64.ts';
18
18
  import {
19
19
  NONE, TRUE, logic, strictEqual,
20
- type Cell, type BlockCell, type StringCell, type WordCell, type Series, type BitsetCell,
20
+ type Cell, type BlockCell, type WordCell, type Series, type BitsetCell,
21
21
  } from '../values.ts';
22
22
  import { getWord, setWord } from '../ctx.ts';
23
23
  import { isEqual, foldCase } from '../compare.ts';
@@ -4,7 +4,8 @@
4
4
  // series.ts hide the difference. Searching is case-insensitive unless /case,
5
5
  // as `=` is.
6
6
 
7
- import { K, isAnyBlock, kindBit, type Kind } from '../kinds.ts';
7
+ import { K, isAnyBlock, isAnyString, kindBit, type Kind } from '../kinds.ts';
8
+ import { bitHas } from '../bitset.ts';
8
9
  import * as I64 from '../int64.ts';
9
10
  import {
10
11
  ownMeta,
@@ -14,8 +15,9 @@ import {
14
15
  import { bindSeries, copyDeep } from '../ctx.ts';
15
16
  import { isEqual, compare, foldCase } from '../compare.ts';
16
17
  import { pick } from '../access.ts';
17
- import { context, time, sameValue } from '../values.ts';
18
+ import { context, time, sameValue, string, bitset, type BitsetCell } from '../values.ts';
18
19
  import { dateDiff } from './math.ts';
20
+ import { sourceOf } from '../mold.ts';
19
21
  import {
20
22
  splice, itemsFor, lengthOf, at, clampIndex,
21
23
  type SeriesCell, type Item,
@@ -90,6 +92,11 @@ native('at', '["The series at a position, counted from where it is." series [ser
90
92
  native('index?', '["The position of the series, from 1." series [series!]]', (_t, f) => value(integer((f.args[0] as SeriesCell).i + 1)));
91
93
  native('head?', '["True if the series is at its head." series [series!]]', (_t, f) => value(logic((f.args[0] as SeriesCell).i === 0)));
92
94
  native('tail?', '["True if the series is at its tail." series [series!]]', (_t, f) => value(logic(lengthOf(f.args[0] as SeriesCell) === 0)));
95
+ native('loaded-at', '["Where the value at the position of a block was loaded from, as file:line:col; none if it was made at run time." block [any-block!]]', (_t, f) => {
96
+ const b = f.args[0] as BlockCell;
97
+ const at = sourceOf(b.s, b.i);
98
+ return value(at === null ? NONE : string(at));
99
+ });
93
100
 
94
101
  // --- editing ---------------------------------------------------------------------
95
102
 
@@ -153,6 +160,8 @@ native('copy', '["A copy of the series, map, object or bitset." value [series! m
153
160
  const b = s as BlockCell;
154
161
  const part: Series<Cell> = { data: b.s.data.slice(from, end) };
155
162
  if (b.s.nl) part.nl = b.s.nl.slice(from, end);
163
+ if (b.s.pos) part.pos = b.s.pos.slice(from, end);
164
+ if (b.s.file !== undefined) part.file = b.s.file;
156
165
  return value({ t: b.t, s: deep ? copyDeep(part) : part, i: 0 });
157
166
  }
158
167
  return value({ t: s.t, s: { data: (s.s.data as number[]).slice(from, end) }, i: 0 } as StringCell);
@@ -262,13 +271,16 @@ function findIn(s: SeriesCell, v: Cell, o: FindOpts): { at: number; len: number
262
271
  return { at: -1, len };
263
272
  }
264
273
 
265
- native('find', '["Finds a value in a series; gives the series where it is, or none." series [series! none! typeset!] value [any-type!] /part "Look only this far" length [integer! series!] /only "A block is one value" /case "Capitals matter" /skip "Look only at every so many" size [integer!] /last "Find the last one" /tail "Give the series just after it" /match "Only where the series is" /reverse "Look back from where the series is" /any "In a string, * is any run of characters and ? any one" /same "In a block, the very same value (same?), not an equal one"]', (_t, f) => {
274
+ native('find', '["Finds a value in a series; gives the series where it is, or none." series [series! none! typeset! bitset!] value [any-type!] /part "Look only this far" length [integer! series!] /only "A block is one value" /case "Capitals matter" /skip "Look only at every so many" size [integer!] /last "Find the last one" /tail "Give the series just after it" /match "Only where the series is" /reverse "Look back from where the series is" /any "In a string, * is any run of characters and ? any one" /same "In a block, the very same value (same?), not an equal one"]', (_t, f) => {
266
275
  const x = f.args[0]!;
267
276
  if (x.t === K.None) return value(NONE);
268
277
  if (x.t === K.Typeset) {
269
278
  const v = f.args[1]!;
270
279
  return value(logic(v.t === K.Datatype && (x.bits & kindBit(v.k)) !== 0n));
271
280
  }
281
+ // In a bitset, whether it holds the character, the code point or every one of a text or block
282
+ // (R3); a letter holds either case unless /case.
283
+ if (x.t === K.Bitset) return value(logic(bitsetHolds(x, f.args[1]!, used(f.args[5]!))));
272
284
  const s = x as SeriesCell;
273
285
  const a = f.args;
274
286
  const o: FindOpts = {
@@ -482,6 +494,19 @@ native('map-each', "[\"A block of what the block gives for each value of a serie
482
494
  return eachNext(f, st, data) ?? value({ t: K.Block, s: { data: st.out }, i: 0 });
483
495
  });
484
496
 
497
+ function bitsetHolds(b: BitsetCell, v: Cell, cased: boolean): boolean {
498
+ const upper = (n: number): number => { const u = String.fromCodePoint(n).toUpperCase(); return u.length === 1 || u.codePointAt(0)! > 0xffff ? u.codePointAt(0)! : n; };
499
+ const one = (n: number): boolean => bitHas(b, n) || (!cased && (bitHas(b, foldCase(n)) || bitHas(b, upper(n))));
500
+ switch (v.t) {
501
+ case K.Char: return one(v.v);
502
+ case K.Integer: { const n = I64.toNumber(v.v); return Number.isSafeInteger(n) && n >= 0 && bitHas(b, n); }
503
+ case K.Block: return (v.s.data as Cell[]).slice(v.i).every(x => bitsetHolds(b, x, cased));
504
+ default:
505
+ if (isAnyString(v.t)) return ((v as StringCell).s.data as number[]).slice((v as StringCell).i).every(one);
506
+ return false;
507
+ }
508
+ }
509
+
485
510
  // --- set operations ----------------------------------------------------------------
486
511
 
487
512
  function itemEq(t: Kind, cased: boolean): (a: Item, b: Item) => boolean {
@@ -500,7 +525,26 @@ function setResult(like: SeriesCell, list: Item[]): Cell {
500
525
  return { t: like.t, s: { data: list }, i: 0 } as Cell;
501
526
  }
502
527
 
503
- function setOp(f: NativeFrame, two: boolean, pick: (a: Item[], b: Item[], eq: (x: Item, y: Item) => boolean) => Item[]): Step {
528
+ // Of two bitsets, the one holding what op says of being in each. A complemented bitset
529
+ // (neg) holds what its bits do not, so each side is flipped before op and the result after.
530
+ function bitsetOp(a: BitsetCell, b: BitsetCell, op: (x: number, y: number) => number): Cell {
531
+ const neg = op(a.neg ? 1 : 0, b.neg ? 1 : 0) !== 0;
532
+ const n = Math.max(a.s.data.length, b.s.data.length);
533
+ const bytes: number[] = [];
534
+ for (let i = 0; i < n; i++) {
535
+ const x = (a.s.data[i] ?? 0) ^ (a.neg ? 0xff : 0);
536
+ const y = (b.s.data[i] ?? 0) ^ (b.neg ? 0xff : 0);
537
+ bytes.push((op(x, y) ^ (neg ? 0xff : 0)) & 0xff);
538
+ }
539
+ return bitset(bytes, neg);
540
+ }
541
+
542
+ function setOp(f: NativeFrame, two: boolean, pick: (a: Item[], b: Item[], eq: (x: Item, y: Item) => boolean) => Item[], bits?: (x: number, y: number) => number): Step {
543
+ if (two && (f.args[0]!.t === K.Bitset || f.args[1]!.t === K.Bitset)) {
544
+ const [x, y] = f.args as [Cell, Cell];
545
+ if (x.t !== K.Bitset || y.t !== K.Bitset || bits === undefined) return fail('script', 'bad-set', 'a bitset goes only with another bitset');
546
+ return value(bitsetOp(x, y, bits));
547
+ }
504
548
  const a = f.args[0] as SeriesCell;
505
549
  const b = two ? f.args[1] as SeriesCell : null;
506
550
  if (b !== null && isAnyBlock(a.t) !== isAnyBlock(b.t)) return fail('script', 'bad-set', 'both series must be of the same kind');
@@ -511,17 +555,17 @@ function setOp(f: NativeFrame, two: boolean, pick: (a: Item[], b: Item[], eq: (x
511
555
  }
512
556
 
513
557
  native('unique', '["The values of the series, each once." set [series!] /case]', (_t, f) => setOp(f, false, (a, _b, eq) => distinct(a, eq)));
514
- native('union', '["The values that are in either series, each once." a [series!] b [series!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct([...a, ...b], eq)));
515
- native('intersect', '["The values that are in both series." a [series!] b [series!] /case]', (_t, f) =>
516
- setOp(f, true, (a, b, eq) => distinct(a.filter(x => b.some(y => eq(x, y))), eq)));
558
+ native('union', '["The values that are in either series, each once." a [series! bitset!] b [series! bitset!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct([...a, ...b], eq), (x, y) => x | y));
559
+ native('intersect', '["The values that are in both series." a [series! bitset!] b [series! bitset!] /case]', (_t, f) =>
560
+ setOp(f, true, (a, b, eq) => distinct(a.filter(x => b.some(y => eq(x, y))), eq), (x, y) => x & y));
517
561
  // Of two dates, the time between them (R3).
518
- native('difference', '["The values that are in one series but not the other; of two dates, the time between them." a [series! date!] b [series! date!] /case]', (_t, f) => {
562
+ native('difference', '["The values that are in one series but not the other; of two dates, the time between them." a [series! date! bitset!] b [series! date! bitset!] /case]', (_t, f) => {
519
563
  const [a, b] = f.args as [Cell, Cell];
520
564
  if (a.t === K.Date || b.t === K.Date) {
521
565
  if (a.t !== K.Date || b.t !== K.Date) return fail('script', 'bad-set', 'difference takes two dates or two series');
522
566
  return value(time(dateDiff(a.v, b.v)));
523
567
  }
524
- return setOp(f, true, (xs, ys, eq) => distinct([...xs.filter(x => !ys.some(y => eq(x, y))), ...ys.filter(y => !xs.some(x => eq(x, y)))], eq));
568
+ return setOp(f, true, (xs, ys, eq) => distinct([...xs.filter(x => !ys.some(y => eq(x, y))), ...ys.filter(y => !xs.some(x => eq(x, y)))], eq), (x, y) => x ^ y);
525
569
  });
526
- native('exclude', '["The values of the first series that are not in the second." a [series!] b [series!] /case]', (_t, f) =>
527
- setOp(f, true, (a, b, eq) => distinct(a.filter(x => !b.some(y => eq(x, y))), eq)));
570
+ native('exclude', '["The values of the first series that are not in the second." a [series! bitset!] b [series! bitset!] /case]', (_t, f) =>
571
+ setOp(f, true, (a, b, eq) => distinct(a.filter(x => !b.some(y => eq(x, y))), eq), (x, y) => x & ~y));
@@ -15,7 +15,6 @@ import {
15
15
  import { getWord, setWord } from '../ctx.ts';
16
16
  import { foldCase, toDec } from '../compare.ts';
17
17
  import { load, LoadError } from '../lexer.ts';
18
- import { form } from '../mold.ts';
19
18
  import { typesOf } from '../func.ts';
20
19
  import { textOf, utf8, at, type SeriesCell } from '../series.ts';
21
20
  import { T, type Feed, type Step } from '../eval.ts';
@@ -145,6 +144,8 @@ function toKind(k: Kind, v: Cell): Cell | string {
145
144
  if (v.t === K.Binary) return { t: k, s: { data: codePoints(new TextDecoder().decode(new Uint8Array(v.s.data.slice(v.i)))) }, i: 0 };
146
145
  if (isAnyWord(v.t)) return { t: k, s: { data: codePoints(spell((v as WordCell).sym)) }, i: 0 };
147
146
  if (v.t === K.Char) return { t: k, s: { data: [v.v] }, i: 0 };
147
+ // the text itself, so to string! <a> is "a", as in R3; form gives "<a>"
148
+ if (isAnyString(v.t)) return { t: k, s: { data: (v as StringCell).s.data.slice((v as StringCell).i) }, i: 0 };
148
149
  return { t: k, s: { data: codePoints(textOf(v)) }, i: 0 };
149
150
  }
150
151
  case K.Binary: