trebol 0.1.2 → 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 (256) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +10040 -7519
  3. package/dist/lib/core/boot.js +10 -7
  4. package/dist/lib/core/eval.js +2 -2
  5. package/dist/lib/core/machine/checks.d.ts +1 -0
  6. package/dist/lib/core/machine/checks.js +250 -0
  7. package/dist/lib/core/machine/events.d.ts +1 -0
  8. package/dist/lib/core/machine/events.js +365 -0
  9. package/dist/lib/core/machine/running.d.ts +1 -0
  10. package/dist/lib/core/machine/running.js +180 -0
  11. package/dist/lib/core/machine/shared.d.ts +1 -0
  12. package/dist/lib/core/machine/shared.js +123 -0
  13. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  14. package/dist/lib/core/machine/snapshot.js +244 -0
  15. package/dist/lib/core/machine/spec.d.ts +1 -0
  16. package/dist/lib/core/machine/spec.js +274 -0
  17. package/dist/lib/core/machine/words.d.ts +1 -0
  18. package/dist/lib/core/machine/words.js +98 -0
  19. package/dist/lib/core/machine.d.ts +1 -0
  20. package/dist/lib/core/machine.js +17 -0
  21. package/dist/lib/core/mezz.js +31 -0
  22. package/dist/lib/core/natives/control.js +1 -1
  23. package/dist/lib/core/natives/data.js +6 -1
  24. package/dist/lib/core/natives/series.js +49 -9
  25. package/dist/lib/core/natives/text.js +0 -1
  26. package/dist/lib/host/browser/calendar.d.ts +34 -0
  27. package/dist/lib/host/browser/calendar.js +268 -0
  28. package/dist/lib/host/browser/count.d.ts +15 -0
  29. package/dist/lib/host/browser/count.js +48 -0
  30. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  31. package/dist/lib/host/browser/dialogs.js +443 -0
  32. package/dist/lib/host/browser/drag.d.ts +70 -0
  33. package/dist/lib/host/browser/drag.js +268 -0
  34. package/dist/lib/host/browser/drawings.d.ts +30 -0
  35. package/dist/lib/host/browser/drawings.js +176 -0
  36. package/dist/lib/host/browser/events.d.ts +16 -0
  37. package/dist/lib/host/browser/events.js +125 -0
  38. package/dist/lib/host/browser/faces.d.ts +35 -0
  39. package/dist/lib/host/browser/faces.js +131 -0
  40. package/dist/lib/host/browser/fields.d.ts +16 -0
  41. package/dist/lib/host/browser/fields.js +322 -0
  42. package/dist/lib/host/browser/keys.d.ts +29 -0
  43. package/dist/lib/host/browser/keys.js +151 -0
  44. package/dist/lib/host/browser/lookup.d.ts +25 -0
  45. package/dist/lib/host/browser/lookup.js +275 -0
  46. package/dist/lib/host/browser/menus.d.ts +20 -0
  47. package/dist/lib/host/browser/menus.js +149 -0
  48. package/dist/lib/host/browser/mount.d.ts +92 -0
  49. package/dist/lib/host/browser/mount.js +448 -0
  50. package/dist/lib/host/browser/orbits.d.ts +90 -0
  51. package/dist/lib/host/browser/orbits.js +379 -0
  52. package/dist/lib/host/browser/page.d.ts +13 -0
  53. package/dist/lib/host/browser/page.js +35 -0
  54. package/dist/lib/host/browser/richtext.d.ts +19 -0
  55. package/dist/lib/host/browser/richtext.js +379 -0
  56. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  57. package/dist/lib/host/browser/row-faces.js +74 -0
  58. package/dist/lib/host/browser/screens.d.ts +37 -0
  59. package/dist/lib/host/browser/screens.js +140 -0
  60. package/dist/lib/host/browser/swipe.d.ts +31 -0
  61. package/dist/lib/host/browser/swipe.js +178 -0
  62. package/dist/lib/host/browser/sync.d.ts +24 -0
  63. package/dist/lib/host/browser/sync.js +450 -0
  64. package/dist/lib/host/browser/tables.d.ts +65 -0
  65. package/dist/lib/host/browser/tables.js +505 -0
  66. package/dist/lib/host/browser/times.d.ts +14 -0
  67. package/dist/lib/host/browser/times.js +114 -0
  68. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  69. package/dist/lib/host/browser/toolbars.js +129 -0
  70. package/dist/lib/host/browser/trees.d.ts +20 -0
  71. package/dist/lib/host/browser/trees.js +235 -0
  72. package/dist/lib/host/browser/uploads.d.ts +29 -0
  73. package/dist/lib/host/browser/uploads.js +370 -0
  74. package/dist/lib/host/browser/widget.d.ts +13 -0
  75. package/dist/lib/host/browser/widget.js +105 -0
  76. package/dist/lib/host/browser.d.ts +25 -0
  77. package/dist/lib/host/browser.js +84 -4812
  78. package/dist/lib/host/devtools/console.d.ts +9 -0
  79. package/dist/lib/host/devtools/console.js +104 -0
  80. package/dist/lib/host/devtools/faces.d.ts +25 -0
  81. package/dist/lib/host/devtools/faces.js +169 -0
  82. package/dist/lib/host/devtools/log.d.ts +3 -0
  83. package/dist/lib/host/devtools/log.js +32 -0
  84. package/dist/lib/host/devtools/machines.d.ts +34 -0
  85. package/dist/lib/host/devtools/machines.js +212 -0
  86. package/dist/lib/host/devtools/read.d.ts +13 -0
  87. package/dist/lib/host/devtools/read.js +52 -0
  88. package/dist/lib/host/devtools/state.d.ts +90 -0
  89. package/dist/lib/host/devtools/state.js +123 -0
  90. package/dist/lib/host/devtools.d.ts +15 -19
  91. package/dist/lib/host/devtools.js +39 -427
  92. package/dist/lib/host/fonts.js +4 -3
  93. package/dist/lib/host/icons.js +4 -0
  94. package/dist/lib/host/index.d.ts +3 -1
  95. package/dist/lib/host/index.js +107 -4
  96. package/dist/lib/host/locale.d.ts +18 -0
  97. package/dist/lib/host/locale.js +75 -5
  98. package/dist/lib/host/markdown.d.ts +4 -0
  99. package/dist/lib/host/markdown.js +8 -0
  100. package/dist/lib/host/modules.js +118 -19
  101. package/dist/lib/host/motion.d.ts +32 -0
  102. package/dist/lib/host/motion.js +190 -0
  103. package/dist/lib/host/net.js +5 -2
  104. package/dist/lib/host/palette.d.ts +17 -0
  105. package/dist/lib/host/palette.js +131 -0
  106. package/dist/lib/host/scheduler.d.ts +2 -0
  107. package/dist/lib/host/scheduler.js +4 -0
  108. package/dist/lib/host/share.d.ts +31 -0
  109. package/dist/lib/host/share.js +134 -0
  110. package/dist/lib/host/theme/classic.d.ts +1 -0
  111. package/dist/lib/host/theme/classic.js +120 -0
  112. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  113. package/dist/lib/host/theme/cupertino.js +65 -0
  114. package/dist/lib/host/theme/data.d.ts +1 -0
  115. package/dist/lib/host/theme/data.js +109 -0
  116. package/dist/lib/host/theme/devtools.d.ts +1 -0
  117. package/dist/lib/host/theme/devtools.js +166 -0
  118. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  119. package/dist/lib/host/theme/dialogs.js +63 -0
  120. package/dist/lib/host/theme/fluent.d.ts +1 -0
  121. package/dist/lib/host/theme/fluent.js +64 -0
  122. package/dist/lib/host/theme/forms.d.ts +1 -0
  123. package/dist/lib/host/theme/forms.js +239 -0
  124. package/dist/lib/host/theme/fusion.d.ts +1 -0
  125. package/dist/lib/host/theme/fusion.js +66 -0
  126. package/dist/lib/host/theme/layout.d.ts +1 -0
  127. package/dist/lib/host/theme/layout.js +166 -0
  128. package/dist/lib/host/theme/media.d.ts +1 -0
  129. package/dist/lib/host/theme/media.js +106 -0
  130. package/dist/lib/host/theme/navigation.d.ts +1 -0
  131. package/dist/lib/host/theme/navigation.js +36 -0
  132. package/dist/lib/host/theme/panes.d.ts +1 -0
  133. package/dist/lib/host/theme/panes.js +99 -0
  134. package/dist/lib/host/theme/popups.d.ts +1 -0
  135. package/dist/lib/host/theme/popups.js +149 -0
  136. package/dist/lib/host/theme/responsive.d.ts +1 -0
  137. package/dist/lib/host/theme/responsive.js +103 -0
  138. package/dist/lib/host/theme/toasts.d.ts +1 -0
  139. package/dist/lib/host/theme/toasts.js +62 -0
  140. package/dist/lib/host/theme.js +19 -1384
  141. package/dist/lib/ui/dialect/events.trb +276 -0
  142. package/dist/lib/ui/dialect/faces.trb +135 -0
  143. package/dist/lib/ui/dialect/layout.trb +283 -0
  144. package/dist/lib/ui/dialect/palette.trb +87 -0
  145. package/dist/lib/ui/dialect/problems.trb +174 -0
  146. package/dist/lib/ui/dialect/reactions.trb +198 -0
  147. package/dist/lib/ui/dialect/rows.trb +208 -0
  148. package/dist/lib/ui/dialect/screens.trb +293 -0
  149. package/dist/lib/ui/dialect/steps.trb +203 -0
  150. package/dist/lib/ui/dialect/validation.trb +137 -0
  151. package/dist/lib/ui/dialect/view.trb +110 -0
  152. package/dist/lib/ui/dialect/walking.trb +98 -0
  153. package/dist/lib/ui/dialect/words.trb +70 -0
  154. package/dist/lib/ui/i18n.trb +17 -0
  155. package/dist/lib/ui/test.trb +3 -556
  156. package/dist/lib/ui/testing/actions.trb +421 -0
  157. package/dist/lib/ui/testing/faces.trb +148 -0
  158. package/dist/lib/ui/testing/server.trb +73 -0
  159. package/dist/lib/ui/ui.trb +25 -1675
  160. package/dist/trebol.js +4463 -1891
  161. package/package.json +1 -1
  162. package/src/core/boot.ts +9 -6
  163. package/src/core/eval.ts +2 -2
  164. package/src/core/machine/checks.ts +251 -0
  165. package/src/core/machine/events.ts +366 -0
  166. package/src/core/machine/running.ts +181 -0
  167. package/src/core/machine/shared.ts +124 -0
  168. package/src/core/machine/snapshot.ts +245 -0
  169. package/src/core/machine/spec.ts +275 -0
  170. package/src/core/machine/words.ts +99 -0
  171. package/src/core/machine.ts +20 -0
  172. package/src/core/mezz.ts +31 -0
  173. package/src/core/natives/control.ts +1 -1
  174. package/src/core/natives/data.ts +6 -1
  175. package/src/core/natives/parse.ts +1 -1
  176. package/src/core/natives/series.ts +47 -11
  177. package/src/core/natives/text.ts +0 -1
  178. package/src/host/browser/calendar.ts +264 -0
  179. package/src/host/browser/count.ts +48 -0
  180. package/src/host/browser/dialogs.ts +439 -0
  181. package/src/host/browser/drag.ts +278 -0
  182. package/src/host/browser/drawings.ts +178 -0
  183. package/src/host/browser/events.ts +117 -0
  184. package/src/host/browser/faces.ts +143 -0
  185. package/src/host/browser/fields.ts +315 -0
  186. package/src/host/browser/keys.ts +145 -0
  187. package/src/host/browser/lookup.ts +262 -0
  188. package/src/host/browser/menus.ts +146 -0
  189. package/src/host/browser/mount.ts +420 -0
  190. package/src/host/browser/orbits.ts +418 -0
  191. package/src/host/browser/page.ts +43 -0
  192. package/src/host/browser/richtext.ts +346 -0
  193. package/src/host/browser/row-faces.ts +75 -0
  194. package/src/host/browser/screens.ts +142 -0
  195. package/src/host/browser/swipe.ts +168 -0
  196. package/src/host/browser/sync.ts +354 -0
  197. package/src/host/browser/tables.ts +461 -0
  198. package/src/host/browser/times.ts +115 -0
  199. package/src/host/browser/toolbars.ts +126 -0
  200. package/src/host/browser/trees.ts +205 -0
  201. package/src/host/browser/uploads.ts +367 -0
  202. package/src/host/browser/widget.ts +103 -0
  203. package/src/host/browser.ts +82 -4612
  204. package/src/host/devtools/console.ts +102 -0
  205. package/src/host/devtools/faces.ts +160 -0
  206. package/src/host/devtools/log.ts +34 -0
  207. package/src/host/devtools/machines.ts +208 -0
  208. package/src/host/devtools/read.ts +58 -0
  209. package/src/host/devtools/state.ts +148 -0
  210. package/src/host/devtools.ts +39 -442
  211. package/src/host/fonts.ts +4 -3
  212. package/src/host/icons.ts +4 -0
  213. package/src/host/index.ts +107 -4
  214. package/src/host/locale.ts +71 -5
  215. package/src/host/markdown.ts +9 -0
  216. package/src/host/modules.ts +117 -21
  217. package/src/host/motion.ts +204 -0
  218. package/src/host/net.ts +4 -2
  219. package/src/host/palette.ts +139 -0
  220. package/src/host/scheduler.ts +5 -0
  221. package/src/host/share.ts +168 -0
  222. package/src/host/theme/classic.ts +121 -0
  223. package/src/host/theme/cupertino.ts +66 -0
  224. package/src/host/theme/data.ts +110 -0
  225. package/src/host/theme/devtools.ts +167 -0
  226. package/src/host/theme/dialogs.ts +64 -0
  227. package/src/host/theme/fluent.ts +65 -0
  228. package/src/host/theme/forms.ts +240 -0
  229. package/src/host/theme/fusion.ts +67 -0
  230. package/src/host/theme/layout.ts +167 -0
  231. package/src/host/theme/media.ts +107 -0
  232. package/src/host/theme/navigation.ts +37 -0
  233. package/src/host/theme/panes.ts +100 -0
  234. package/src/host/theme/popups.ts +150 -0
  235. package/src/host/theme/responsive.ts +104 -0
  236. package/src/host/theme/toasts.ts +63 -0
  237. package/src/host/theme.ts +19 -1384
  238. package/src/ui/dialect/events.trb +276 -0
  239. package/src/ui/dialect/faces.trb +135 -0
  240. package/src/ui/dialect/layout.trb +283 -0
  241. package/src/ui/dialect/palette.trb +87 -0
  242. package/src/ui/dialect/problems.trb +174 -0
  243. package/src/ui/dialect/reactions.trb +198 -0
  244. package/src/ui/dialect/rows.trb +208 -0
  245. package/src/ui/dialect/screens.trb +293 -0
  246. package/src/ui/dialect/steps.trb +203 -0
  247. package/src/ui/dialect/validation.trb +137 -0
  248. package/src/ui/dialect/view.trb +110 -0
  249. package/src/ui/dialect/walking.trb +98 -0
  250. package/src/ui/dialect/words.trb +70 -0
  251. package/src/ui/i18n.trb +17 -0
  252. package/src/ui/test.trb +3 -556
  253. package/src/ui/testing/actions.trb +421 -0
  254. package/src/ui/testing/faces.trb +148 -0
  255. package/src/ui/testing/server.trb +73 -0
  256. package/src/ui/ui.trb +25 -1675
@@ -0,0 +1,274 @@
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
+ export const SPEC = String.raw `
4
+ ; The engine of machine: states are objects in a block, known by their place
5
+ ; in it (the root is 1), so a machine holds no loops of references and molds.
6
+ machine-engine: make object! [
7
+
8
+ words: [data initial state final parallel from no on-enter on-exit on-activity on-save timeout ttl defer run invoke shared]
9
+
10
+ ; the events a machine sends itself
11
+ own: [error timeout expired done]
12
+
13
+ ; send on a socket, when the host has sockets: send itself is redefined below
14
+ socket: either value? 'send [:send] [none]
15
+
16
+ ; where the last mistake was, for trebol check
17
+ failed: none
18
+
19
+ fail: function [message [string!] place [block!]] [
20
+ set 'failed place
21
+ e: make error! rejoin ["machine: " message]
22
+ e/near: copy/part place 4
23
+ e/at: loaded-at place
24
+ do e
25
+ ]
26
+
27
+ node: function [m id] [pick m/nodes id]
28
+
29
+ ; --- did you mean: the word near enough to one that is not known (strat: start) ---
30
+
31
+ ; Optimal string alignment distance: letters put in, taken out, changed, or two neighbours swapped.
32
+ distance: function [a [string!] b [string!]] [
33
+ n: length? b
34
+ before: none
35
+ prev: copy []
36
+ repeat j n + 1 [append prev j - 1]
37
+ repeat i length? a [
38
+ row: reduce [i]
39
+ repeat j n [
40
+ d: min min (pick prev j + 1) + 1 (last row) + 1 (pick prev j) + either (pick a i) = (pick b j) [0] [1]
41
+ if all [i > 1 j > 1 (pick a i) = (pick b j - 1) (pick a i - 1) = (pick b j)] [
42
+ d: min d (pick before j - 1) + 1
43
+ ]
44
+ append row d
45
+ ]
46
+ before: prev
47
+ prev: row
48
+ ]
49
+ last prev
50
+ ]
51
+
52
+ ; The word near enough to w to be what it meant, or none: one letter off up to four letters, two longer;
53
+ ; nothing for one or two letters, which are near too many.
54
+ nearest: function [w words [block!]] [
55
+ t: form w
56
+ if 3 > length? t [return none]
57
+ most: either 4 < length? t [2] [1]
58
+ best: none
59
+ foreach c words [
60
+ d: distance t form c
61
+ if all [d > 0 d <= most] [best: c most: d - 1]
62
+ ]
63
+ best
64
+ ]
65
+
66
+ ; "; did you mean start?" when one of the words is near enough; "" otherwise.
67
+ suggest: function [w words [block!]] [
68
+ either best: nearest w words [rejoin ["; did you mean " best "?"]] [""]
69
+ ]
70
+
71
+ state-names: function [m] [collect [foreach n next m/nodes [keep n/name]]]
72
+
73
+ data-fields: function [m] [collect [foreach v any [m/data-spec []] [if set-word? :v [keep to word! v]]]]
74
+
75
+ add-node: function [m name parent] [
76
+ n: make object! [
77
+ id: name: parent: initial: enter: exit: activity: outcome: place: timeout: run: invoke: with: for: in: none
78
+ children: copy [] transitions: copy [] defer: copy [] history: true final: false parallel: false
79
+ ]
80
+ append m/nodes n
81
+ n/id: length? m/nodes n/name: name n/parent: parent
82
+ n
83
+ ]
84
+
85
+ child: function [m id name] [
86
+ foreach c (select node m id 'children) [if name = select node m c 'name [return c]]
87
+ none
88
+ ]
89
+
90
+ ; A target: a name or a path, looked up from the scope, then from each level above.
91
+ find-state: function [m scope name] [
92
+ names: either path? name [to block! name] [reduce [name]]
93
+ while [scope] [
94
+ id: scope
95
+ foreach w names [unless id: child m id w [break]]
96
+ if id [return id]
97
+ scope: select node m scope 'parent
98
+ ]
99
+ none
100
+ ]
101
+
102
+ ; A hook, guard or set block of the spec: bound to the machine's data and event.
103
+ code: function [m spec what] [
104
+ b: second spec
105
+ unless block? :b [fail rejoin [what " takes a block"] spec]
106
+ bind b m/scope
107
+ append/only m/code spec
108
+ b
109
+ ]
110
+
111
+ ; Reads the words of one level (the machine or a state) into node n.
112
+ level: function [m n spec] [
113
+ froms: copy []
114
+ while [not tail? spec] [
115
+ here: spec
116
+ w: first spec
117
+ unless word? :w [fail rejoin [mold :w " is not a word of machine"] here]
118
+ spec: next spec
119
+ switch/default w [
120
+ data [
121
+ if n/id <> 1 [fail "data goes at the top of the machine" here]
122
+ unless block? first spec [fail "data takes a block" here]
123
+ m/data-spec: first spec
124
+ spec: next spec
125
+ ]
126
+ initial [
127
+ unless word? first spec [fail "initial takes the name of a state" here]
128
+ n/initial: here
129
+ spec: next spec
130
+ ]
131
+ state final parallel [
132
+ name: first spec
133
+ unless word? :name [fail rejoin [w " takes a name"] here]
134
+ if child m n/id name [fail rejoin ["two states called " name] here]
135
+ c: add-node m name n/id
136
+ c/place: here
137
+ append n/children c/id
138
+ spec: next spec
139
+ c/parallel: w = 'parallel
140
+ if w = 'final [
141
+ c/final: true
142
+ if all [word? first spec not find words first spec] [
143
+ c/outcome: first spec
144
+ spec: next spec
145
+ ]
146
+ ]
147
+ if block? first spec [
148
+ level m c first spec
149
+ spec: next spec
150
+ ]
151
+ if all [c/final not empty? c/children] [fail rejoin ["final " name " has states inside"] here]
152
+ if c/parallel [
153
+ if 2 > length? c/children [fail rejoin ["parallel " name " needs two states or more"] here]
154
+ foreach k c/children [
155
+ if select node m k 'final [fail rejoin ["parallel " name " holds states, not final ones"] here]
156
+ ]
157
+ ]
158
+ ]
159
+ shared [
160
+ if n/id <> 1 [fail "shared goes at the top of the machine" here]
161
+ unless word? first spec [fail "shared takes a name, the same in every tab" here]
162
+ m/shared: first spec
163
+ spec: next spec
164
+ ]
165
+ on-save [
166
+ if n/id <> 1 [fail "on-save goes at the top of the machine" here]
167
+ b: code m here w
168
+ ; snapshot, inside it, is the machine as it is after the event
169
+ m/saver: make object! [snapshot: none]
170
+ bind b m/saver
171
+ m/on-save: b
172
+ spec: next spec
173
+ ]
174
+ on-enter on-exit on-activity [
175
+ if n/id = 1 [fail rejoin [w " goes inside a state"] here]
176
+ b: code m here w
177
+ switch w [on-enter [n/enter: b] on-exit [n/exit: b] on-activity [n/activity: b]]
178
+ spec: next spec
179
+ ]
180
+ timeout [
181
+ if n/id = 1 [fail "timeout goes inside a state; the whole machine takes ttl" here]
182
+ if n/final [fail "a final state takes no timeout" here]
183
+ v: first spec
184
+ unless any [number? :v time? :v all [path? :v 'data = first v 2 = length? v]] [
185
+ fail "timeout takes seconds, a time or data/field" here
186
+ ]
187
+ n/timeout: v
188
+ spec: next spec
189
+ ]
190
+ run invoke [
191
+ if n/id = 1 [fail rejoin [w " goes inside a state"] here]
192
+ if n/final [fail rejoin ["a final state takes no " w] here]
193
+ if any [n/run n/invoke] [fail "a state runs one thing: one run or one invoke" here]
194
+ either w = 'run [
195
+ n/run: code m here 'run
196
+ spec: next spec
197
+ ] [
198
+ v: first spec
199
+ unless any [block? :v word? :v path? :v] [fail "invoke takes the block of a machine, or a word for one" here]
200
+ ; a machine written here is made now, so its mistakes show at once
201
+ if block? v [inner: make-machine v]
202
+ n/invoke: v
203
+ spec: next spec
204
+ ; one machine for each item of a block of the data
205
+ if 'for = first spec [
206
+ unless all [word? second spec 'in = third spec] [
207
+ fail "invoke ... for takes a word, in and data/field" spec
208
+ ]
209
+ v: fourth spec
210
+ unless all [path? :v 'data = first v 2 = length? v] [
211
+ fail "invoke ... for item in takes data/field" spec
212
+ ]
213
+ n/for: second spec
214
+ n/in: v
215
+ spec: skip spec 4
216
+ ]
217
+ if 'with = first spec [
218
+ n/with: code m spec 'with
219
+ if block? v [
220
+ fields: collect [foreach x any [inner/data-spec []] [if set-word? :x [keep to word! x]]]
221
+ foreach x n/with [
222
+ if all [set-word? :x not find fields to word! x] [
223
+ fail rejoin ["the machine invoked in " n/name " has no data " to word! x suggest to word! x fields] spec
224
+ ]
225
+ ]
226
+ ]
227
+ spec: skip spec 2
228
+ ]
229
+ ]
230
+ ]
231
+ ttl [
232
+ if n/id <> 1 [fail "ttl goes at the top of the machine" here]
233
+ v: first spec
234
+ unless any [number? :v time? :v] [fail "ttl takes seconds or a time" here]
235
+ m/ttl: v
236
+ spec: next spec
237
+ ]
238
+ defer [
239
+ e: first spec
240
+ names: case [
241
+ word? :e [reduce [e]]
242
+ all [block? :e not empty? e parse e [some word!]] [e]
243
+ true [fail "defer takes the name of an event or a block of them" here]
244
+ ]
245
+ foreach w names [
246
+ if find own w [fail rejoin [w " is the machine's own event; it cannot wait"] here]
247
+ ]
248
+ append n/defer names
249
+ spec: next spec
250
+ ]
251
+ no [
252
+ unless 'history = first spec [fail "no takes history" here]
253
+ n/history: false
254
+ spec: next spec
255
+ ]
256
+ from [spec: transition m n here]
257
+ ] [
258
+ fail rejoin [w " is not a word of machine" suggest w words] here
259
+ ]
260
+ ]
261
+ if all [n/parallel n/initial] [fail rejoin ["parallel " n/name " has no initial: all its states start at once"] n/initial]
262
+ if all [not empty? n/children none? n/initial not n/parallel] [
263
+ fail either n/id = 1 ["the machine has no initial state"] [rejoin ["state " n/name " has no initial"]]
264
+ any [n/place spec]
265
+ ]
266
+ if n/initial [
267
+ place: n/initial
268
+ unless n/initial: child m n/id second place [
269
+ fail rejoin ["no state " second place " here" suggest second place state-names m] place
270
+ ]
271
+ ]
272
+ ]
273
+
274
+ `;
@@ -0,0 +1 @@
1
+ export declare const WORDS: string;
@@ -0,0 +1,98 @@
1
+ // Machines in Trebol (see machine.ts). The words a program uses: machine, snapshot, send, in?, graph-of, can?
2
+ // and restart.
3
+ export const WORDS = String.raw `
4
+ machine: function [
5
+ "Makes a state machine from a block and starts it in its initial state."
6
+ spec [block!]
7
+ /from {Resumes it where a snapshot of it left it, instead}
8
+ saved [block!] "What snapshot gave"
9
+ ] [
10
+ e: machine-engine
11
+ m: e/make-machine spec
12
+ e/fresh m
13
+ case [
14
+ m/shared [e/share-start m all [from saved]]
15
+ from [e/resume m saved]
16
+ true [e/start m]
17
+ ]
18
+ ]
19
+
20
+ snapshot: function [
21
+ "The state of a machine as a block of plain data, which machine/from resumes."
22
+ m [object!]
23
+ ] [
24
+ ; a copy of a shared machine has only what the owner told it
25
+ either m/owner [machine-engine/snapshot m] [copy/deep m/seen]
26
+ ]
27
+
28
+ send: function [
29
+ "Sends an event to a machine and gives the state it is in then, or sends a value on a socket."
30
+ m [object! handle!] "A machine, or a socket"
31
+ event [any-value!] "The event: a word, or a block with the word and its fields; for a socket, the value"
32
+ ] [
33
+ if handle? m [return machine-engine/socket m :event]
34
+ unless any [word? :event block? :event] [
35
+ do make error! "send: the event is a word, or a block that starts with one"
36
+ ]
37
+ either word? event [name: event ev: make object! []] [
38
+ name: first event
39
+ unless word? :name [do make error! "send: the block starts with the name of the event"]
40
+ ev: make object! next event
41
+ ]
42
+ if find machine-engine/own name [do make error! rejoin ["send: " name " is the machine's own event"]]
43
+ ; an event no way of the machine names is a mistake: it would be lost
44
+ if all [known: machine-engine/events m not find known name] [
45
+ do make error! rejoin ["send: the machine has no way on " name machine-engine/suggest name known]
46
+ ]
47
+ unless m/owner [
48
+ ; a copy: the owner's tab runs it, and its answer brings the new state
49
+ share-ask m/channel mold/all reduce ['event name body-of ev]
50
+ return m/state
51
+ ]
52
+ append/only m/queue reduce [name ev]
53
+ unless m/busy [machine-engine/run m]
54
+ m/state
55
+ ]
56
+
57
+ in?: function [
58
+ "Whether a machine is in a state now: its name, or a path of names from the top."
59
+ m [object!]
60
+ name [word! path!]
61
+ ] [
62
+ e: machine-engine
63
+ either word? name [
64
+ foreach id m/on [if name = select e/node m id 'name [return true]]
65
+ false
66
+ ] [
67
+ id: 1
68
+ foreach w to block! name [unless id: e/child m id w [return false]]
69
+ e/on? m id
70
+ ]
71
+ ]
72
+
73
+ graph-of: function [
74
+ "The states of a machine and the ways between them, as plain data; of a block, the machine it would make, run not."
75
+ machine [object! block!] "A machine, or the block of one"
76
+ ] [
77
+ e: machine-engine
78
+ e/graph either block? machine [e/make-machine machine] [machine]
79
+ ]
80
+
81
+ can?: function [
82
+ "Whether a machine has a way out on an event from where it is now; what where would say is not asked."
83
+ m [object!]
84
+ event [word!]
85
+ ] [
86
+ machine-engine/can m event
87
+ ]
88
+
89
+ restart: function [
90
+ "Starts a machine again from its initial state, with its data as it was made."
91
+ m [object!]
92
+ ] [
93
+ either m/owner [machine-engine/again m] [
94
+ share-ask m/channel mold/all [restart]
95
+ m
96
+ ]
97
+ ]
98
+ `;
@@ -0,0 +1 @@
1
+ export declare const MACHINE: string;
@@ -0,0 +1,17 @@
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
+ // The Trebol code is in machine/, a file per part, joined here in order.
10
+ import { SPEC } from "./machine/spec.js";
11
+ import { CHECKS } from "./machine/checks.js";
12
+ import { RUNNING } from "./machine/running.js";
13
+ import { EVENTS } from "./machine/events.js";
14
+ import { SNAPSHOT } from "./machine/snapshot.js";
15
+ import { SHARED } from "./machine/shared.js";
16
+ import { WORDS } from "./machine/words.js";
17
+ export const MACHINE = `\n${[SPEC, CHECKS, RUNNING, EVENTS, SNAPSHOT, SHARED, WORDS].map(part => part.slice(1)).join('')}`;
@@ -357,4 +357,35 @@ suffix?: func [
357
357
  ]
358
358
  ]
359
359
  ]
360
+ iban?: func [
361
+ "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."
362
+ text [string!]
363
+ /local s n r letter digit alnum
364
+ ][
365
+ letter: charset [#"A" - #"Z"]
366
+ digit: charset [#"0" - #"9"]
367
+ alnum: union letter digit
368
+ s: uppercase replace/all copy text " " ""
369
+ to logic! all [
370
+ parse s [2 letter 2 digit some alnum]
371
+ n: select [
372
+ "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
373
+ "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
374
+ "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
375
+ "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
376
+ "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
377
+ "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
378
+ ] copy/part s 2
379
+ n = length? s
380
+ (
381
+ ; the four first characters go to the end, each letter counts as 10 to 35, and the number
382
+ ; they make leaves 1 when divided by 97
383
+ r: 0
384
+ foreach c append copy skip s 4 copy/part s 4 [
385
+ r: either find letter c [mod r * 100 + (to integer! c) - 55 97] [mod r * 10 + (to integer! c) - 48 97]
386
+ ]
387
+ r = 1
388
+ )
389
+ ]
390
+ ]
360
391
  `;
@@ -3,7 +3,7 @@
3
3
  // Each one is a small state machine over `f.phase`: phase 0 is the first
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
- import { K, kindName } from "../kinds.js";
6
+ import { K } from "../kinds.js";
7
7
  import * as I64 from "../int64.js";
8
8
  import { NONE, UNSET, TRUE, block, context } from "../values.js";
9
9
  import { bindSeries, copyDeep, setWord } from "../ctx.js";
@@ -242,8 +242,13 @@ native('spec-of', '["A copy of a function\'s spec block." value [any-function!]]
242
242
  const spec = c.t === K.Function ? p.specBlock : p.specBlock;
243
243
  return value({ t: K.Block, s: copyDeep(spec.s, spec.i), i: 0 });
244
244
  });
245
- native('body-of', '["A copy of a function\'s body block, or none for a native." value [any-function!]]', (_t, f) => {
245
+ 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) => {
246
246
  const c = underOp(f.args[0]);
247
+ if (c.t === K.Object || c.t === K.Error) {
248
+ const out = [];
249
+ c.ctx.keys.forEach((k, i) => out.push(wordOf(K.SetWord, k), c.ctx.vals[i]));
250
+ return value(blockOf(K.Block, out));
251
+ }
247
252
  return value(c.t === K.Function ? { t: K.Block, s: copyDeep(c.p.body), i: 0 } : NONE);
248
253
  });
249
254
  native('words-of', '["The words of an object or map, or the arguments, refinements and local words of a function." value [any-object! map! any-function!]]', (_t, f) => {
@@ -3,13 +3,14 @@
3
3
  // Blocks hold cells, strings code points and binaries bytes; the helpers in
4
4
  // series.ts hide the difference. Searching is case-insensitive unless /case,
5
5
  // as `=` is.
6
- import { K, isAnyBlock, kindBit } from "../kinds.js";
6
+ import { K, isAnyBlock, isAnyString, kindBit } from "../kinds.js";
7
+ import { bitHas } from "../bitset.js";
7
8
  import * as I64 from "../int64.js";
8
9
  import { ownMeta, NONE, TRUE, integer, char, logic, strictEqual, } from "../values.js";
9
10
  import { bindSeries, copyDeep } from "../ctx.js";
10
11
  import { isEqual, compare, foldCase } from "../compare.js";
11
12
  import { pick } from "../access.js";
12
- import { context, time, sameValue, string } from "../values.js";
13
+ import { context, time, sameValue, string, bitset } from "../values.js";
13
14
  import { dateDiff } from "./math.js";
14
15
  import { sourceOf } from "../mold.js";
15
16
  import { splice, itemsFor, lengthOf, at, clampIndex, } from "../series.js";
@@ -256,7 +257,7 @@ function findIn(s, v, o) {
256
257
  }
257
258
  return { at: -1, len };
258
259
  }
259
- 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) => {
260
+ 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) => {
260
261
  const x = f.args[0];
261
262
  if (x.t === K.None)
262
263
  return value(NONE);
@@ -264,6 +265,10 @@ native('find', '["Finds a value in a series; gives the series where it is, or no
264
265
  const v = f.args[1];
265
266
  return value(logic(v.t === K.Datatype && (x.bits & kindBit(v.k)) !== 0n));
266
267
  }
268
+ // In a bitset, whether it holds the character, the code point or every one of a text or block
269
+ // (R3); a letter holds either case unless /case.
270
+ if (x.t === K.Bitset)
271
+ return value(logic(bitsetHolds(x, f.args[1], used(f.args[5]))));
267
272
  const s = x;
268
273
  const a = f.args;
269
274
  const o = {
@@ -475,6 +480,22 @@ native('map-each', "[\"A block of what the block gives for each value of a serie
475
480
  }
476
481
  return eachNext(f, st, data) ?? value({ t: K.Block, s: { data: st.out }, i: 0 });
477
482
  });
483
+ function bitsetHolds(b, v, cased) {
484
+ const upper = (n) => { const u = String.fromCodePoint(n).toUpperCase(); return u.length === 1 || u.codePointAt(0) > 0xffff ? u.codePointAt(0) : n; };
485
+ const one = (n) => bitHas(b, n) || (!cased && (bitHas(b, foldCase(n)) || bitHas(b, upper(n))));
486
+ switch (v.t) {
487
+ case K.Char: return one(v.v);
488
+ case K.Integer: {
489
+ const n = I64.toNumber(v.v);
490
+ return Number.isSafeInteger(n) && n >= 0 && bitHas(b, n);
491
+ }
492
+ case K.Block: return v.s.data.slice(v.i).every(x => bitsetHolds(b, x, cased));
493
+ default:
494
+ if (isAnyString(v.t))
495
+ return v.s.data.slice(v.i).every(one);
496
+ return false;
497
+ }
498
+ }
478
499
  // --- set operations ----------------------------------------------------------------
479
500
  function itemEq(t, cased) {
480
501
  if (isAnyBlock(t))
@@ -493,7 +514,26 @@ function distinct(list, eq) {
493
514
  function setResult(like, list) {
494
515
  return { t: like.t, s: { data: list }, i: 0 };
495
516
  }
496
- function setOp(f, two, pick) {
517
+ // Of two bitsets, the one holding what op says of being in each. A complemented bitset
518
+ // (neg) holds what its bits do not, so each side is flipped before op and the result after.
519
+ function bitsetOp(a, b, op) {
520
+ const neg = op(a.neg ? 1 : 0, b.neg ? 1 : 0) !== 0;
521
+ const n = Math.max(a.s.data.length, b.s.data.length);
522
+ const bytes = [];
523
+ for (let i = 0; i < n; i++) {
524
+ const x = (a.s.data[i] ?? 0) ^ (a.neg ? 0xff : 0);
525
+ const y = (b.s.data[i] ?? 0) ^ (b.neg ? 0xff : 0);
526
+ bytes.push((op(x, y) ^ (neg ? 0xff : 0)) & 0xff);
527
+ }
528
+ return bitset(bytes, neg);
529
+ }
530
+ function setOp(f, two, pick, bits) {
531
+ if (two && (f.args[0].t === K.Bitset || f.args[1].t === K.Bitset)) {
532
+ const [x, y] = f.args;
533
+ if (x.t !== K.Bitset || y.t !== K.Bitset || bits === undefined)
534
+ return fail('script', 'bad-set', 'a bitset goes only with another bitset');
535
+ return value(bitsetOp(x, y, bits));
536
+ }
497
537
  const a = f.args[0];
498
538
  const b = two ? f.args[1] : null;
499
539
  if (b !== null && isAnyBlock(a.t) !== isAnyBlock(b.t))
@@ -504,16 +544,16 @@ function setOp(f, two, pick) {
504
544
  return value(setResult(a, pick(xs, ys, eq)));
505
545
  }
506
546
  native('unique', '["The values of the series, each once." set [series!] /case]', (_t, f) => setOp(f, false, (a, _b, eq) => distinct(a, eq)));
507
- 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)));
508
- native('intersect', '["The values that are in both series." a [series!] b [series!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct(a.filter(x => b.some(y => eq(x, y))), eq)));
547
+ 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));
548
+ native('intersect', '["The values that are in both series." a [series! bitset!] b [series! bitset!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct(a.filter(x => b.some(y => eq(x, y))), eq), (x, y) => x & y));
509
549
  // Of two dates, the time between them (R3).
510
- 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) => {
550
+ 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) => {
511
551
  const [a, b] = f.args;
512
552
  if (a.t === K.Date || b.t === K.Date) {
513
553
  if (a.t !== K.Date || b.t !== K.Date)
514
554
  return fail('script', 'bad-set', 'difference takes two dates or two series');
515
555
  return value(time(dateDiff(a.v, b.v)));
516
556
  }
517
- 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));
557
+ 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);
518
558
  });
519
- native('exclude', '["The values of the first series that are not in the second." a [series!] b [series!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct(a.filter(x => !b.some(y => eq(x, y))), eq)));
559
+ native('exclude', '["The values of the first series that are not in the second." a [series! bitset!] b [series! bitset!] /case]', (_t, f) => setOp(f, true, (a, b, eq) => distinct(a.filter(x => !b.some(y => eq(x, y))), eq), (x, y) => x & ~y));
@@ -9,7 +9,6 @@ import { ownMeta, NONE, TRUE, FALSE, integer, decimal, percent, money, char, log
9
9
  import { getWord, setWord } from "../ctx.js";
10
10
  import { foldCase, toDec } from "../compare.js";
11
11
  import { load, LoadError } from "../lexer.js";
12
- import { form } from "../mold.js";
13
12
  import { typesOf } from "../func.js";
14
13
  import { textOf, utf8, at } from "../series.js";
15
14
  import { T } from "../eval.js";
@@ -0,0 +1,34 @@
1
+ import { type Cell, type ObjectCell } from '../../core/index.ts';
2
+ export interface Calendar {
3
+ face: ObjectCell;
4
+ opener: HTMLButtonElement;
5
+ box: HTMLElement;
6
+ month: HTMLElement;
7
+ at: Date;
8
+ range: HTMLElement | null;
9
+ from: Date | null;
10
+ }
11
+ export declare let calendar: Calendar | null;
12
+ export declare const DAY_MS = 86400000;
13
+ /** A date! cell as a UTC date at midnight, or null. */
14
+ export declare function utcDate(v: Cell): Date | null;
15
+ export declare function isoDay(d: Date): string;
16
+ /** The two days of a date-range's value, or null. */
17
+ export declare function rangeOf(v: Cell): [Date, Date] | null;
18
+ /** A range of dates as it shows on screen: 3 Nov 2026 – 10 Nov 2026. */
19
+ export declare function rangeText(v: Cell): string;
20
+ /** The argument of a rule of a face, as min or max, or none. */
21
+ export declare function ruleArg(face: ObjectCell, rule: string): Cell;
22
+ /** The date argument of a min or max rule of a face, or null. */
23
+ export declare function dateRule(face: ObjectCell, rule: string): Date | null;
24
+ export declare function openCalendar(face: ObjectCell, opener: HTMLButtonElement): void;
25
+ export declare function closeCalendar(refocus?: boolean): void;
26
+ export declare function outsideCalendar(ev: PointerEvent): void;
27
+ /** Draws the month of the focused day; the head stays. */
28
+ export declare function renderCalendar(): void;
29
+ /** With the first day of a range picked, marks the days between it and the one pointed or focused. */
30
+ export declare function spanTo(d: Date): void;
31
+ export declare function addMonths(d: Date, n: number): Date;
32
+ export declare function focusDay(): void;
33
+ export declare function pickDay(d: Date): void;
34
+ export declare function calendarKey(ev: KeyboardEvent): void;