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,244 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part five: snapshot and resume, can, the graph of the states,
2
+ // and on-save.
3
+ export const SNAPSHOT = String.raw `
4
+ ; --- snapshot: the machine as plain data, which machine/from resumes ---
5
+
6
+ ; A state as the block of the names from the top down to it, and back.
7
+ names-of: function [m id] [collect [foreach x next chain m 1 id [keep select node m x 'name]]]
8
+
9
+ id-of: function [m names] [
10
+ id: 1
11
+ foreach w names [
12
+ unless id: child m id w [do make error! rejoin ["machine/from: no state " mold to path! names suggest w state-names m]]
13
+ ]
14
+ id
15
+ ]
16
+
17
+ ; An object from a block of field: value, the values taken as they are.
18
+ object-of: function [body] [
19
+ o: make object! append collect [foreach [f v] body [keep to set-word! f]] [none]
20
+ foreach [f v] body [set in o to word! f :v]
21
+ o
22
+ ]
23
+
24
+ snapshot: function [m] [
25
+ timers: copy [] tasks: copy [] ttl: none
26
+ foreach [key x] m/timers [
27
+ if x [
28
+ case [
29
+ key = 0 [ttl: third x]
30
+ key > 0 [append/only timers names-of m key append timers third x]
31
+ true [
32
+ w: second x
33
+ append/only tasks names-of m negate key
34
+ append/only tasks case [
35
+ object? w [snapshot w]
36
+ block? w [
37
+ reduce [
38
+ 'outcomes copy third x 'data copy fourth x
39
+ 'machines collect [foreach c w [keep/only either c/done [none] [snapshot c]]]
40
+ ]
41
+ ]
42
+ true ['run]
43
+ ]
44
+ ]
45
+ ]
46
+ ]
47
+ ]
48
+ reduce [
49
+ 'on collect [foreach id m/on [keep/only names-of m id]]
50
+ 'data body-of m/data
51
+ 'history collect [foreach [k v] m/history [if v [keep/only names-of m k keep/only names-of m v]]]
52
+ 'results collect [foreach [k v] m/results [keep/only names-of m k keep :v]]
53
+ 'deferred collect [foreach e m/deferred [keep/only reduce [first e body-of second e]]]
54
+ 'timers timers 'ttl ttl 'tasks tasks
55
+ 'done m/done 'outcome m/outcome 'last m/last
56
+ ]
57
+ ]
58
+
59
+ ; Puts a machine made from its spec where the snapshot left it: no on-enter runs again, the
60
+ ; timers go off when they were going to, a run starts over and a machine invoked resumes.
61
+ resume: function [m saved] [
62
+ restore m saved
63
+ unless m/done [
64
+ if t: select saved 'ttl [clock-at m 0 t 'expired]
65
+ foreach [names t] select saved 'timers [clock-at m id-of m names t 'timeout]
66
+ foreach [names w] select saved 'tasks [revive m names w]
67
+ ]
68
+ m/state: where m
69
+ m
70
+ ]
71
+
72
+ ; The plain part of a snapshot: states, data, history, results, the events held.
73
+ restore: function [m saved] [
74
+ unless block? :saved [do make error! "machine/from: a snapshot is a block"]
75
+ foreach names select saved 'on [append m/on id-of m names]
76
+ foreach [f v] select saved 'data [
77
+ unless in m/data to word! f [do make error! rejoin ["machine/from: data has no " to word! f suggest to word! f words-of m/data]]
78
+ set in m/data to word! f :v
79
+ ]
80
+ foreach [k v] select saved 'history [put m/history id-of m k id-of m v]
81
+ foreach [k v] select saved 'results [put m/results id-of m k :v]
82
+ foreach e select saved 'deferred [append/only m/deferred reduce [first e object-of second e]]
83
+ m/done: true = select saved 'done
84
+ m/outcome: select saved 'outcome
85
+ m/last: select saved 'last
86
+ m/state: where m
87
+ ]
88
+
89
+ ; The work of a state as the snapshot left it: a run starts over, a machine invoked resumes.
90
+ revive: function [m names w] [
91
+ id: id-of m names
92
+ n: node m id
93
+ case [
94
+ n/run [work m id]
95
+ all [n/invoke n/for block? :w] [
96
+ s: serial m
97
+ kids: copy []
98
+ machines: select w 'machines
99
+ repeat i length? machines [
100
+ if c: pick machines i [
101
+ c: resumed n c
102
+ c/caller: reduce [m id s i]
103
+ append kids c
104
+ ]
105
+ ]
106
+ put m/timers negate id reduce [s kids copy select w 'outcomes copy select w 'data length? kids]
107
+ ]
108
+ all [n/invoke block? :w] [
109
+ c: resumed n w
110
+ s: serial m
111
+ c/caller: reduce [m id s]
112
+ put m/timers negate id reduce [s c]
113
+ ]
114
+ ]
115
+ ]
116
+
117
+ ; Forgets the states, history and results, before a snapshot is put in.
118
+ clean: function [m] [
119
+ stop m
120
+ m/on: copy []
121
+ m/history: make map! []
122
+ m/results: make map! []
123
+ clear m/queue
124
+ ]
125
+
126
+ ; The machine back where a snapshot of it left it, in place: what it was doing
127
+ ; is stopped, and nothing it does on entering a state runs again. A form in
128
+ ; steps goes back with it. A copy of a shared machine asks the owner's tab.
129
+ rewind: function [m saved] [
130
+ unless m/owner [
131
+ share-ask m/channel mold/all reduce ['rewind saved]
132
+ return m
133
+ ]
134
+ clean m
135
+ m/done: false
136
+ m/outcome: m/error: m/active: m/last: none
137
+ resume m saved
138
+ saved m
139
+ m
140
+ ]
141
+
142
+ ; Whether a state the machine is in, or one around it, has a way out on an event.
143
+ can: function [m name] [
144
+ foreach leaf leaves m [
145
+ id: leaf
146
+ while [all [id id <> 1]] [
147
+ n: node m id
148
+ foreach t select node m n/parent 'transitions [
149
+ if all [t/source = id t/events find t/events name] [return true]
150
+ ]
151
+ id: n/parent
152
+ ]
153
+ ]
154
+ false
155
+ ]
156
+
157
+ ; --- graph: the states and ways as plain data, for tools that draw them ---
158
+
159
+ key-of: function [m id] [
160
+ names: names-of m id
161
+ either 1 = length? names [first names] [to path! names]
162
+ ]
163
+
164
+ graph: function [m] [
165
+ states: copy []
166
+ foreach n next m/nodes [
167
+ parent: node m n/parent
168
+ append states key-of m n/id
169
+ append/only states reduce [
170
+ 'kind case [n/final ['final] n/parallel ['parallel] true ['state]]
171
+ 'initial n/id = parent/initial
172
+ 'on on? m n/id
173
+ 'history n/history
174
+ 'outcome n/outcome
175
+ 'timeout :n/timeout
176
+ 'run either n/run [copy/deep n/run] [none]
177
+ 'invoke either block? :n/invoke [graph make-machine n/invoke] [:n/invoke]
178
+ 'for n/for
179
+ 'in :n/in
180
+ 'defer copy n/defer
181
+ ]
182
+ ]
183
+ ways: copy []
184
+ foreach n m/nodes [
185
+ foreach t n/transitions [
186
+ way: reduce ['from key-of m t/source]
187
+ case [
188
+ t/target [append way reduce ['to key-of m t/target]]
189
+ t/select [append way reduce ['select copy/deep t/select]]
190
+ true [append way 'choose]
191
+ ]
192
+ append way reduce ['on either t/events [copy t/events] [none]]
193
+ append way reduce ['where either t/guard [copy/deep t/guard] [none]]
194
+ append way reduce ['set either t/set [copy/deep t/set] [none]]
195
+ if t/choices [
196
+ branches: copy []
197
+ foreach c t/choices [
198
+ append/only branches reduce [
199
+ either t/select ['value] ['when] copy/deep first c
200
+ 'to key-of m second c
201
+ 'set either third c [copy/deep third c] [none]
202
+ ]
203
+ ]
204
+ if t/other [
205
+ append/only branches reduce ['else none 'to key-of m t/other 'set either t/other-set [copy/deep t/other-set] [none]]
206
+ ]
207
+ append way reduce ['branches branches]
208
+ ]
209
+ append/only ways way
210
+ ]
211
+ ]
212
+ reduce [
213
+ 'state m/state
214
+ 'data collect [foreach v any [m/data-spec []] [if set-word? :v [keep to word! v]]]
215
+ 'ttl m/ttl
216
+ 'shared m/shared
217
+ 'done m/done
218
+ 'states states
219
+ 'ways ways
220
+ ]
221
+ ]
222
+
223
+ ; The machine of an invoke, resumed from its own snapshot.
224
+ resumed: function [n saved] [
225
+ spec: n/invoke
226
+ unless block? spec [spec: get spec]
227
+ c: make-machine spec
228
+ fresh c
229
+ resume c saved
230
+ ]
231
+
232
+ ; After each event, the on-save of the outermost machine, with its snapshot.
233
+ saved: function [m] [
234
+ top: m
235
+ while [top/caller] [top: first top/caller]
236
+ if any [top/busy not top/owner] [exit]
237
+ if top/on-save [
238
+ top/saver/snapshot: snapshot top
239
+ do top/on-save
240
+ ]
241
+ if top/channel [tell top false]
242
+ ]
243
+
244
+ `;
@@ -0,0 +1 @@
1
+ export declare const SPEC: string;
@@ -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
  `;