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