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