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,251 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part two: the ways out of a state, and the checks of a
2
+ // machine made: every state reached, no choose that hangs, the events it takes, its data.
3
+
4
+ export const CHECKS = String.raw`
5
+ ; from a to b [on e | on [e f] | on error] [where [...]] [set [...]]
6
+ ; from a choose [on e] [when [...] to b [set [...]] ... else to c [set [...]]]
7
+ ; from a select [...] [on e] [value to b [set [...]] ... else to c [set [...]]]
8
+ transition: function [m n here] [
9
+ t: make object! [
10
+ source: target: events: guard: set: choices: other: other-set: select: place: none
11
+ ]
12
+ t/place: here
13
+ spec: next here
14
+ unless any [word? first spec] [fail "from takes the name of a state" here]
15
+ t/source: first spec
16
+ spec: next spec
17
+ switch/default first spec [
18
+ to [
19
+ t/target: second spec
20
+ unless any [word? :t/target path? :t/target] [fail "to takes the name of a state" spec]
21
+ spec: skip spec 2
22
+ ]
23
+ choose [t/choices: copy [] spec: next spec]
24
+ select [
25
+ t/select: code m spec 'select
26
+ t/choices: copy []
27
+ spec: skip spec 2
28
+ ]
29
+ ] [fail "from takes to, choose or select after the name of a state" here]
30
+ if 'on = first spec [
31
+ e: second spec
32
+ case [
33
+ word? :e [t/events: reduce [e]]
34
+ all [block? :e not empty? e parse e [some word!]] [t/events: e]
35
+ true [fail "on takes the name of an event or a block of them" spec]
36
+ ]
37
+ spec: skip spec 2
38
+ ]
39
+ what: either t/select ['select] ['choose]
40
+ if t/choices [
41
+ unless block? first spec [
42
+ fail either t/select ["select takes a block of value to a state"] ["choose takes a block of when ... to"] here
43
+ ]
44
+ branches: first spec
45
+ spec: next spec
46
+ while [not tail? branches] [
47
+ at: branches
48
+ guard: none
49
+ case [
50
+ 'else = first branches [branches: next branches]
51
+ t/select [
52
+ guard: reduce [first branches]
53
+ foreach c t/choices [
54
+ if guard/1 = first c/1 [fail rejoin ["select has two branches for " mold guard/1] at]
55
+ ]
56
+ branches: next branches
57
+ ]
58
+ true [
59
+ unless 'when = first branches [fail "choose takes when [...] to a state, or else to a state" at]
60
+ guard: code m branches 'when
61
+ branches: skip branches 2
62
+ ]
63
+ ]
64
+ unless 'to = first branches [fail rejoin ["a branch of " what " takes to and a state"] at]
65
+ target: second branches
66
+ unless any [word? :target path? :target] [fail "to takes the name of a state" branches]
67
+ branches: skip branches 2
68
+ sets: none
69
+ if 'set = first branches [
70
+ sets: code m branches 'set
71
+ branches: skip branches 2
72
+ ]
73
+ either guard [
74
+ append/only t/choices reduce [guard target sets]
75
+ ] [
76
+ if t/other [fail rejoin [what " has two else"] at]
77
+ t/other: target t/other-set: sets
78
+ ]
79
+ ]
80
+ ]
81
+ if 'where = first spec [
82
+ unless t/events [fail "where goes after on; a transition with no event that may wait for a condition is a choose" spec]
83
+ t/guard: code m spec 'where
84
+ spec: skip spec 2
85
+ ]
86
+ if 'set = first spec [
87
+ t/set: code m spec 'set
88
+ spec: skip spec 2
89
+ ]
90
+ append n/transitions t
91
+ spec
92
+ ]
93
+
94
+ resolve: function [m n t] [
95
+ if n/parallel [fail rejoin ["the states of parallel " n/name " have no ways between them"] t/place]
96
+ unless id: child m n/id t/source [fail rejoin ["no state " t/source " here" suggest t/source state-names m] t/place]
97
+ t/source: id
98
+ if t/target [t/target: target m n t/target t/place]
99
+ if t/choices [
100
+ foreach c t/choices [poke c 2 target m n second c t/place]
101
+ if t/other [t/other: target m n t/other t/place]
102
+ ]
103
+ if t/events [
104
+ if all [find t/events 'timeout not has? m id [timeout]] [
105
+ fail rejoin ["on timeout leaves " second t/place ", which has no timeout"] t/place
106
+ ]
107
+ if all [find t/events 'done not has? m id [run invoke]] [
108
+ fail rejoin ["on done leaves " second t/place ", which has no run or invoke"] t/place
109
+ ]
110
+ if find t/events 'expired [
111
+ unless m/ttl [fail "on expired needs ttl at the top of the machine" t/place]
112
+ unless all [t/target select node m t/target 'final] [
113
+ fail "on expired goes to a final state" t/place
114
+ ]
115
+ ]
116
+ ]
117
+ if all [none? t/events none? t/choices] [
118
+ foreach o n/transitions [
119
+ if all [o <> t o/source = id none? o/events none? o/choices] [
120
+ fail rejoin ["two transitions with no event leave " second t/place] t/place
121
+ ]
122
+ ]
123
+ ]
124
+ ]
125
+
126
+ ; Whether a state or one inside it has one of the fields.
127
+ has?: function [m id fields] [
128
+ n: node m id
129
+ foreach f fields [if select n f [return true]]
130
+ foreach c n/children [if has? m c fields [return true]]
131
+ false
132
+ ]
133
+
134
+ ; Every state can be reached: from the initial ones, by the ways out of states that can.
135
+ check-reach: function [m] [
136
+ reach: make map! []
137
+ mark: function [id] [
138
+ if any [none? id select reach id] [exit]
139
+ put reach id true
140
+ n: node m id
141
+ if n/parent [mark n/parent]
142
+ either n/parallel [foreach c n/children [mark c]] [mark n/initial]
143
+ ]
144
+ mark 1
145
+ more: true
146
+ while [more] [
147
+ more: false
148
+ before: length? reach
149
+ foreach n m/nodes [
150
+ foreach t n/transitions [
151
+ if select reach t/source [
152
+ mark t/target
153
+ mark t/other
154
+ foreach c any [t/choices []] [mark second c]
155
+ ]
156
+ ]
157
+ ]
158
+ more: before < length? reach
159
+ ]
160
+ foreach n next m/nodes [
161
+ unless select reach n/id [fail rejoin ["no way leads to state " n/name] n/place]
162
+ ]
163
+ ]
164
+
165
+ ; A choose with no event and no else, out of a state with no other way out, may keep the machine there for good.
166
+ check-hang: function [m] [
167
+ foreach n m/nodes [
168
+ foreach t n/transitions [
169
+ if all [t/choices none? t/events none? t/other none? t/select] [
170
+ s: node m t/source
171
+ up: next chain m 1 t/source
172
+ out: false
173
+ foreach id up [if select node m id 'parallel [out: true]]
174
+ unless empty? s/children [out: true]
175
+ foreach o m/nodes [
176
+ foreach x o/transitions [
177
+ if all [x <> t find up x/source] [out: true]
178
+ ]
179
+ ]
180
+ unless out [
181
+ fail rejoin ["the machine may stay in " s/name " for good: give the choose an else, or the state another way out"] t/place
182
+ ]
183
+ ]
184
+ ]
185
+ ]
186
+ ]
187
+
188
+ ; The events some way or defer of the machine names; none if it takes any (ttl, on-activity).
189
+ events: function [m] [
190
+ if m/ttl [return none]
191
+ out: copy []
192
+ foreach n m/nodes [
193
+ if n/activity [return none]
194
+ append out n/defer
195
+ foreach t n/transitions [append out any [t/events []]]
196
+ ]
197
+ unique out
198
+ ]
199
+
200
+ ; Every timeout is taken by a transition on timeout from its state or one around it.
201
+ check-timeouts: function [m] [
202
+ foreach n m/nodes [
203
+ if n/timeout [
204
+ up: chain m 1 n/id
205
+ taken: false
206
+ foreach o m/nodes [
207
+ foreach t o/transitions [
208
+ if all [t/events find t/events 'timeout find up t/source] [taken: true]
209
+ ]
210
+ ]
211
+ unless taken [fail rejoin ["state " n/name " has a timeout and no transition on timeout"] n/place]
212
+ ]
213
+ ]
214
+ ]
215
+
216
+ target: function [m n name place] [
217
+ any [find-state m n/id name fail rejoin ["no state " mold name suggest either path? name [last name] [name] state-names m] place]
218
+ ]
219
+
220
+ ; The data a block reads or writes, for data/x where data has no x.
221
+ check-data: function [m] [
222
+ fields: data-fields m
223
+ walk: function [b sets] [
224
+ while [not tail? b] [
225
+ v: first b
226
+ case [
227
+ all [sets set-word? :v not find fields to word! v] [
228
+ fail rejoin ["data has no " to word! v suggest to word! v fields] b
229
+ ]
230
+ all [any [path? :v set-path? :v get-path? :v] 'data = first v] [
231
+ if all [word? second v not find fields second v] [
232
+ fail rejoin ["data has no " second v suggest second v fields] b
233
+ ]
234
+ ]
235
+ any [block? :v paren? :v] [walk v false]
236
+ ]
237
+ b: next b
238
+ ]
239
+ ]
240
+ foreach place m/code [walk second place 'set = first place]
241
+ foreach n m/nodes [
242
+ if all [path? n/timeout not find fields second n/timeout] [
243
+ fail rejoin ["data has no " second n/timeout suggest second n/timeout fields] n/place
244
+ ]
245
+ if all [n/in not find fields second n/in] [
246
+ fail rejoin ["data has no " second n/in suggest second n/in fields] n/place
247
+ ]
248
+ ]
249
+ ]
250
+
251
+ `;
@@ -0,0 +1,366 @@
1
+ // Machines in Trebol (see machine.ts). The engine, part four: run and invoke, events and how a transition moves
2
+ // the machine, one step at a time.
3
+
4
+ export const EVENTS = String.raw`
5
+ ; --- run and invoke: work of a state (key -id), its end sent back as done or error ---
6
+
7
+ work: function [m id] [
8
+ n: node m id
9
+ case [
10
+ n/run [
11
+ s: serial m
12
+ put m/timers negate id reduce [s spawn compose/only [machine-engine/worked (m) (id) (s) (n/run)]]
13
+ ]
14
+ n/invoke [
15
+ spec: n/invoke
16
+ unless block? spec [spec: get spec]
17
+ unless block? :spec [
18
+ do make error! rejoin ["machine: invoke in " n/name " takes the block of a machine: " mold :spec]
19
+ ]
20
+ s: serial m
21
+ either n/for [
22
+ items: get in m/data second n/in
23
+ unless block? :items [
24
+ do make error! rejoin ["machine: invoke in " n/name " goes over a block: data/" second n/in " is " mold :items]
25
+ ]
26
+ ; [serial children outcomes data left]: the outcomes and data in the order of the items
27
+ x: reduce [s copy [] array length? items array length? items length? items]
28
+ put m/timers negate id x
29
+ either empty? items [
30
+ post-event m 'done make object! [outcomes: copy [] data: copy []] negate id s
31
+ ] [
32
+ repeat i length? items [
33
+ ; the word of for, seen only by with
34
+ it: make object! reduce [to set-word! n/for none]
35
+ set in it n/for pick items i
36
+ c: invoked m n spec all [n/with bind copy/deep n/with it]
37
+ c/caller: reduce [m id s i]
38
+ append second x c
39
+ ]
40
+ foreach c second x [start c]
41
+ ]
42
+ ] [
43
+ c: invoked m n spec n/with
44
+ c/caller: reduce [m id s]
45
+ put m/timers negate id reduce [s c]
46
+ start c
47
+ ]
48
+ ]
49
+ ]
50
+ ]
51
+
52
+ ; A machine made for invoke, its data seeded by with.
53
+ invoked: function [m n spec with] [
54
+ c: make-machine spec
55
+ if c/shared [do make error! rejoin ["machine: the machine invoked in " n/name " cannot be shared"]]
56
+ fresh c
57
+ if with [
58
+ foreach [f v] values m reduce [with] [
59
+ unless in c/data f [do make error! rejoin ["machine: the machine invoked in " n/name " has no data " f suggest f words-of c/data]]
60
+ set in c/data f :v
61
+ ]
62
+ ]
63
+ c
64
+ ]
65
+
66
+ ; The task of a run: the value it gives comes back as done, a failure as error.
67
+ worked: function [m id s b] [
68
+ r: try b
69
+ x: select m/timers negate id
70
+ if all [x s = first x] [poke x 2 none]
71
+ either error? :r [
72
+ post-event m 'error r negate id s
73
+ ] [
74
+ post-event m 'done make object! [value: :r] negate id s
75
+ ]
76
+ ]
77
+
78
+ ; A machine invoked that is done tells the one that invoked it.
79
+ returned: function [c] [
80
+ if all [c/done x: c/caller] [
81
+ c/caller: none
82
+ ev: make object! [outcome: c/outcome data: c/data]
83
+ if fourth x [ev: make ev [item: fourth x]]
84
+ post-event first x 'done ev negate second x third x
85
+ ]
86
+ ]
87
+
88
+ serial: function [m] [m/serial: m/serial + 1]
89
+
90
+ arm-ttl: function [m] [
91
+ if m/ttl [
92
+ disarm m 0
93
+ clock m 0 m/ttl 'expired
94
+ ]
95
+ ]
96
+
97
+ stop: function [m] [
98
+ foreach [key x] m/timers [if x [drop second x]]
99
+ m/timers: make map! []
100
+ clear m/deferred
101
+ ]
102
+
103
+ post: function [m name key serial] [post-event m name make object! [] key serial]
104
+
105
+ post-event: function [m name ev key serial] [
106
+ append/only m/queue reduce [name ev key serial]
107
+ unless m/busy [run m]
108
+ ]
109
+
110
+ ; A timer that went off: its timeout is taken from its own state, or from one around it.
111
+ ring: function [m name key serial] [
112
+ x: select m/timers key
113
+ unless all [x serial = first x] [exit]
114
+ ; a machine of a fan-out that ends: the state is done when all of them are
115
+ if all [name = 'done block? second x in e: m/scope/event 'item] [
116
+ poke third x e/item e/outcome
117
+ poke fourth x e/item e/data
118
+ poke x 5 (pick x 5) - 1
119
+ if 0 < pick x 5 [exit]
120
+ m/scope/event: make object! [outcomes: third x data: fourth x]
121
+ ]
122
+ put m/timers key none
123
+ case [
124
+ name = 'timeout [
125
+ if t: find-from m key 'timeout true none [go m t]
126
+ ]
127
+ name = 'expired [
128
+ unless deliver m 'expired [
129
+ m/outcome: 'expired
130
+ m/done: true
131
+ stop m
132
+ exit
133
+ ]
134
+ ]
135
+ name = 'done [
136
+ if t: find-from m negate key 'done true none [go m t]
137
+ ]
138
+ name = 'error [
139
+ ; routed as a failure of the state that ran it
140
+ m/active: negate key
141
+ do m/scope/event
142
+ ]
143
+ ]
144
+ drain m
145
+ ]
146
+
147
+ ; An event goes to every innermost state: each takes its own way out, looking no further
148
+ ; out than its parallel; when none does, the states around the parallels may.
149
+ deliver: function [m name] [
150
+ taken: false
151
+ foreach id leaves m [
152
+ if on? m id [
153
+ if t: find-from m id name true region-of m id [go m t taken: true]
154
+ ]
155
+ ]
156
+ if taken [return true]
157
+ foreach id leaves m [
158
+ if t: find-from m id name true none [go m t return true]
159
+ ]
160
+ false
161
+ ]
162
+
163
+ ; The first event held by defer that the machine can take now.
164
+ redeliver: function [m] [
165
+ pos: m/deferred
166
+ while [not tail? pos] [
167
+ e: first pos
168
+ m/scope/event: second e
169
+ if deliver m first e [
170
+ remove pos
171
+ return true
172
+ ]
173
+ pos: next pos
174
+ ]
175
+ false
176
+ ]
177
+
178
+ deferring?: function [m name] [
179
+ foreach id head insert copy m/on 1 [if find select node m id 'defer name [return true]]
180
+ false
181
+ ]
182
+
183
+ remember: function [m id] [
184
+ if p: select node m id 'parent [put m/history p id]
185
+ ]
186
+
187
+ ; Enters the states of path, the first one down to the last, and then what is inside the
188
+ ; last: its history or initial state, or every state of a parallel.
189
+ enter-down: function [m path] [
190
+ id: first path
191
+ rest: next path
192
+ enter m id
193
+ remember m id
194
+ n: node m id
195
+ case [
196
+ n/parallel [
197
+ foreach c n/children [
198
+ put m/results c none
199
+ enter-down m either all [not tail? rest c = first rest] [rest] [reduce [c]]
200
+ ]
201
+ ]
202
+ not tail? rest [enter-down m rest]
203
+ not empty? n/children [
204
+ unless n/history [put m/history id none]
205
+ enter-down m reduce [any [select m/history id n/initial]]
206
+ ]
207
+ ]
208
+ ]
209
+
210
+ ; Leaves every state on inside state id, innermost first.
211
+ exit-below: function [m id] [
212
+ foreach c reverse copy select node m id 'children [
213
+ if on? m c [
214
+ exit-below m c
215
+ leave m c
216
+ remember m c
217
+ ]
218
+ ]
219
+ ]
220
+
221
+ ; Every right side of a set, worked out before any state is left.
222
+ values: function [m sets] [
223
+ out: copy []
224
+ foreach b sets [
225
+ pos: b
226
+ while [not tail? pos] [
227
+ unless set-word? first pos [fail "set takes field: value pairs" pos]
228
+ f: to word! first pos
229
+ v: do/next next pos 'pos
230
+ append out reduce [f :v]
231
+ ]
232
+ ]
233
+ out
234
+ ]
235
+
236
+ ; The states left and entered are those below the one the source and the target share;
237
+ ; a parallel is never that one, so going between its states leaves it and enters it again.
238
+ go: function [m t] [
239
+ r: m/taken
240
+ dest: first r
241
+ sets: copy []
242
+ if t/set [append/only sets t/set]
243
+ if second r [append/only sets second r]
244
+ vals: values m sets
245
+ pivot: lca m t/source dest
246
+ while [select node m pivot 'parallel] [pivot: select node m pivot 'parent]
247
+ exit-below m pivot
248
+ m/active: pivot
249
+ foreach [f v] vals [set in m/data f :v]
250
+ path: next chain m pivot dest
251
+ unless empty? path [enter-down m path]
252
+ ]
253
+
254
+ ; A state with nowhere to go ends: a leaf gives its outcome, a state of a parallel how it ended.
255
+ sink: function [m id] [
256
+ n: node m id
257
+ if empty? n/children [
258
+ m/last: any [n/outcome n/name]
259
+ if all [n/outcome none? m/outcome none? region-of m id] [m/outcome: n/outcome]
260
+ ]
261
+ leave m id
262
+ case [
263
+ n/parent = 1 [
264
+ m/on: reduce [id]
265
+ m/done: true
266
+ stop m
267
+ ]
268
+ select node m n/parent 'parallel [put m/results id m/last]
269
+ true [put m/history n/parent none]
270
+ ]
271
+ ]
272
+
273
+ ; One move with no event, if there is one: a way out with no event, or a state that ends.
274
+ settle: function [m] [
275
+ foreach id leaves m [
276
+ if on? m id [
277
+ if t: find-from m id none false none [go m t return true]
278
+ unless find-from m id true false none [sink m id return true]
279
+ ]
280
+ ]
281
+ foreach id copy m/on [
282
+ if all [select node m id 'parallel t: find-from m id none false none] [go m t return true]
283
+ ]
284
+ false
285
+ ]
286
+
287
+ drain: function [m] [
288
+ while [not m/done] [
289
+ unless any [settle m redeliver m] [exit]
290
+ ]
291
+ ]
292
+
293
+ begin: function [m] [
294
+ arm-ttl m
295
+ enter-down m reduce [select node m 1 'initial]
296
+ drain m
297
+ ]
298
+
299
+ ; An event from send: one that no transition takes waits if a defer holds it.
300
+ event: function [m name] [
301
+ unless deliver m name [
302
+ either deferring? m name [
303
+ append/only m/deferred reduce [name m/scope/event]
304
+ ] [
305
+ foreach id leaves m [hook m id 'activity]
306
+ ]
307
+ ]
308
+ drain m
309
+ unless m/done [arm-ttl m]
310
+ ]
311
+
312
+ ; One step, the start, an event or a timer; a failure on the way in routes to on error.
313
+ step: function [m e] [
314
+ name: first e
315
+ m/scope/event: second e
316
+ r: try [
317
+ case [
318
+ none? name [begin m]
319
+ third e [ring m name third e fourth e]
320
+ true [event m name]
321
+ ]
322
+ none
323
+ ]
324
+ if error? :r [
325
+ if m/exiting [m/exiting: false do r]
326
+ m/error: r
327
+ m/scope/event: r
328
+ unless t: find-from m m/active 'error true none [do r]
329
+ go m t
330
+ drain m
331
+ ]
332
+ ]
333
+
334
+ ; A word or a path for one state on; a block of paths when there are several.
335
+ where: function [m] [
336
+ out: collect [
337
+ foreach leaf leaves m [
338
+ names: collect [foreach id next chain m 1 leaf [keep select node m id 'name]]
339
+ keep either 1 = length? names [first names] [to path! names]
340
+ ]
341
+ ]
342
+ case [
343
+ empty? out [none]
344
+ 1 = length? out [first out]
345
+ true [out]
346
+ ]
347
+ ]
348
+
349
+ run: function [m] [
350
+ m/busy: true
351
+ r: try [
352
+ while [not empty? m/queue] [
353
+ e: take m/queue
354
+ unless m/done [step m e]
355
+ ]
356
+ none
357
+ ]
358
+ m/busy: false
359
+ m/state: where m
360
+ if error? :r [clear m/queue do r]
361
+ saved m
362
+ returned m
363
+ m
364
+ ]
365
+
366
+ `;