trebol-dev 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.
@@ -0,0 +1,192 @@
1
+ Trebol [
2
+ title: "Where the designer draws each state of a file's machines, kept in a commented block at the end of the file"
3
+ exports: [
4
+ places-of places-last machine-key machine-places set-machine-places rename-places drop-places
5
+ rename-machine-places put-place state-key
6
+ ]
7
+ ]
8
+
9
+ import %machines.trb
10
+
11
+ ; The engine never reads it: it is a comment, written last in the file, in Trebol:
12
+ ;
13
+ ; ; layout [
14
+ ; ; order [charging 40x60 paid 260x60 charging/attempt 24x40]
15
+ ; ; ]
16
+ ;
17
+ ; Each machine is known by the word it is set to (or its place among the file's machines when it has none),
18
+ ; each state by its path from the machine. A state's place is its box's top-left corner, from the top-left
19
+ ; corner of the box it is in, so a state moved takes the states inside it along. A state with no place is
20
+ ; put where there is room; a machine with none is laid out whole.
21
+
22
+ opening: "; layout ["
23
+ closing: "; ]"
24
+
25
+ ; [start end] of the block in the text: from its first line to the end of its last, or none
26
+ block-at: function [text [string!]] [
27
+ s: none
28
+ p: text
29
+ while [p: find p opening] [
30
+ if any [head? p newline = first back p] [s: p]
31
+ p: next p
32
+ ]
33
+ unless s [return none]
34
+ e: s
35
+ while [e: find e newline] [
36
+ e: next e
37
+ line: copy/part e any [find e newline tail e]
38
+ trim/tail line
39
+ if line = closing [return reduce [index? s (index? e) + length? closing]]
40
+ unless #";" = first line [return none]
41
+ ]
42
+ none
43
+ ]
44
+
45
+ places-of: function ["The places in a file's text: [key [path pair ...] ...], empty when it has none" text [string!]] [
46
+ unless span: block-at text [return copy []]
47
+ code: copy/part at text span/1 span/2 - span/1
48
+ lines: collect [
49
+ foreach line split code newline [
50
+ line: next line ; the ;
51
+ if #" " = first line [line: next line]
52
+ keep line
53
+ ]
54
+ ]
55
+ b: attempt [load rejoin collect [foreach l lines [keep l keep newline]]]
56
+ unless all [block? b 'layout = first b block? second b] [return copy []]
57
+ out: copy []
58
+ foreach [key places] second b [
59
+ if all [any [word? key integer? key] block? places] [
60
+ kept: copy []
61
+ foreach [p xy] places [if all [any [word? p path? p] pair? xy] [append kept reduce [state-key p xy]]]
62
+ append out reduce [key kept]
63
+ ]
64
+ ]
65
+ out
66
+ ]
67
+
68
+ ; the text without the block, and the lines around it, ending in one newline (or empty)
69
+ without-block: function [text [string!]] [
70
+ unless span: block-at text [return copy text]
71
+ t: copy text
72
+ remove/part at t span/1 span/2 - span/1
73
+ trim/tail t
74
+ unless empty? t [append t newline]
75
+ t
76
+ ]
77
+
78
+ ; one machine's places as lines of the block, short enough to read
79
+ places-lines: function [key places [block!]] [
80
+ items: collect [foreach [p xy] places [keep rejoin [mold p " " mold xy]]]
81
+ one: rejoin ["; " mold key " [" combine items " " "]"]
82
+ if 100 >= length? one [return reduce [one]]
83
+ out: reduce [rejoin ["; " mold key " ["]]
84
+ line: copy ""
85
+ foreach item items [
86
+ if all [not empty? line 96 < ((length? line) + 2 + length? item)] [
87
+ append out rejoin ["; " line]
88
+ line: copy ""
89
+ ]
90
+ unless empty? line [append line " "]
91
+ append line item
92
+ ]
93
+ unless empty? line [append out rejoin ["; " line]]
94
+ append out "; ]"
95
+ out
96
+ ]
97
+
98
+ combine: function [items [block!] sep [string!]] [
99
+ out: copy ""
100
+ foreach i items [unless empty? out [append out sep] append out i]
101
+ out
102
+ ]
103
+
104
+ write-places: function ["The text with these places as its block, at its end" text [string!] all-places [block!]] [
105
+ t: without-block text
106
+ kept: collect [foreach [key places] all-places [unless empty? places [keep key keep/only places]]]
107
+ if empty? kept [return t]
108
+ unless empty? t [append t newline]
109
+ append t opening
110
+ append t newline
111
+ foreach [key places] kept [foreach l places-lines key places [append t l append t newline]]
112
+ append t closing
113
+ append t newline
114
+ t
115
+ ]
116
+
117
+ places-last: function ["The text with its block of places at its end, if code was written after it" text [string!]] [
118
+ unless span: block-at text [return text]
119
+ rest: copy at text span/2
120
+ either empty? trim rest [text] [write-places text places-of text]
121
+ ]
122
+
123
+ machine-key: function ["The word a machine is known by in the places, or its place among the file's" d [object!] m [object!]] [
124
+ if m/name [return m/name]
125
+ k: 0
126
+ foreach x machines-of d [k: k + 1 if same? x/block m/block [return k]]
127
+ none
128
+ ]
129
+
130
+ machine-places: function ["One machine's places: [path pair ...]" text [string!] key] [
131
+ any [select places-of text key copy []]
132
+ ]
133
+
134
+ set-machine-places: function ["The text with one machine's places as given" text [string!] key places [block!]] [
135
+ all-places: places-of text
136
+ either p: find/skip all-places key 2 [change/only next p places] [append all-places reduce [key places]]
137
+ write-places text all-places
138
+ ]
139
+
140
+ ; a state as the places know it: its name at the top of the machine, its path below
141
+ state-key: function [p [block! path! word!]] [
142
+ b: to block! p
143
+ either 1 = length? b [first b] [to path! b]
144
+ ]
145
+
146
+ ; a path starts with another: the same state, or one inside it
147
+ under?: function [p [path! word!] top [path! word!]] [
148
+ a: to block! p
149
+ b: to block! top
150
+ all [(length? a) >= length? b b = copy/part a length? b]
151
+ ]
152
+
153
+ rename-places: function [
154
+ "The text with a state's places, and those of the states inside it, under a new path"
155
+ text [string!] key old [path! word!] new [path! word!]
156
+ ] [
157
+ old: to path! to block! old
158
+ new: to path! to block! new
159
+ places: machine-places text key
160
+ if empty? places [return text]
161
+ out: copy []
162
+ foreach [p xy] places [
163
+ if under? p old [p: state-key append to block! new skip to block! p length? to block! old]
164
+ append out reduce [p xy]
165
+ ]
166
+ set-machine-places text key out
167
+ ]
168
+
169
+ drop-places: function ["The text without a state's places, and those of the states inside it" text [string!] key path [path! word!]] [
170
+ path: to path! to block! path
171
+ places: machine-places text key
172
+ if empty? places [return text]
173
+ out: copy []
174
+ foreach [p xy] places [unless under? p path [append out reduce [p xy]]]
175
+ set-machine-places text key out
176
+ ]
177
+
178
+ rename-machine-places: function ["The text with a machine's places under a new key, or without them for none" text [string!] old new] [
179
+ all-places: places-of text
180
+ unless p: find/skip all-places old 2 [return text]
181
+ either new [change p new] [remove/part p 2]
182
+ write-places text all-places
183
+ ]
184
+
185
+ put-place: function ["The text with one state's place" text [string!] key path [path! word! block!] xy [pair!]] [
186
+ path: state-key path
187
+ places: machine-places text key
188
+ out: copy []
189
+ foreach [p spot] places [unless (to block! p) = to block! path [append out reduce [p spot]]]
190
+ append out reduce [path xy]
191
+ set-machine-places text key out
192
+ ]
@@ -0,0 +1,22 @@
1
+ Trebol [
2
+ title: "A file with a machine, as the machine tests read and change it"
3
+ exports: [machine-sample]
4
+ ]
5
+
6
+ machine-sample: {repair: machine [
7
+ data [parts: 0]
8
+ initial received
9
+ state received [] ; at the door
10
+ state work [
11
+ initial checking
12
+ state checking []
13
+ state waiting [on-enter [print "parts ordered"]]
14
+ from checking to waiting on need-parts set [parts: event/count]
15
+ from waiting choose [when [data/parts > 2] to ready else to checking]
16
+ ]
17
+ final ready done
18
+ from received to work on start
19
+ from work to ready on fixed
20
+ ]
21
+ send repair 'start
22
+ }
@@ -0,0 +1,170 @@
1
+ Trebol [
2
+ title: "The designer's Try: a machine of the file run in the designer, its events sent with the mouse"
3
+ ]
4
+
5
+ ; Run by app.trb (do), after machines-view.trb, so it shares their words: ms, picked, text, machine-drawing.
6
+ ; Its buttons, data and history take the place of the properties while it runs.
7
+ ; Try makes the chosen machine from its block and shows it running: a button per event it names, usable when
8
+ ; a way out takes it now, the states it is in lit in the drawing, its data and what happened. Its timers run
9
+ ; as they would in the app; a button sends a timeout at once. The fields an event carries (count: 3) are typed
10
+ ; before its button is pressed. Any change to the code stops it.
11
+
12
+ trying: none ; the machine being tried, or none
13
+ try-k: none ; which of the file's machines it is
14
+ try-text: none ; the code it was made from
15
+ try-last: "" ; the state it was in when last shown
16
+ try-sending: false ; an event of the person's on its way: what it does is told by try-send
17
+ try-lines: copy [] ; what happened, the newest first
18
+
19
+ try-log: function [line [string!]] [
20
+ insert try-lines line
21
+ if 50 < length? try-lines [clear at try-lines 51]
22
+ ]
23
+
24
+ state-text: function [m [object!]] [
25
+ s: m/state
26
+ either block? s [form collect [foreach p s [keep form p]]] [form s]
27
+ ]
28
+
29
+ ; the spec as the file writes it, but shared with no tab and saved nowhere
30
+ try-spec: function [m [object!]] [
31
+ spec: engine-block ms m
32
+ words: select first m/nodes 'words
33
+ places: sort/reverse collect [foreach [w i] words [if find [shared on-save] w [keep i]]]
34
+ foreach i places [remove/part at spec i 2]
35
+ spec
36
+ ]
37
+
38
+ try-machine: function ["Makes the chosen machine and starts it" ] [
39
+ k: either picked [picked/1] [1]
40
+ unless m: pick ms k [exit]
41
+ clear try-lines
42
+ r: try [machine try-spec m]
43
+ if error? r [
44
+ msg: r/message
45
+ if find/match msg "machine: " [msg: skip msg 9]
46
+ try-log rejoin ["It does not start: " msg]
47
+ toast first try-lines
48
+ set 'trying none
49
+ show-try
50
+ exit
51
+ ]
52
+ r/on-save: [try-changed]
53
+ r/saver: make object! [snapshot: none]
54
+ set 'trying r
55
+ set 'try-k k
56
+ set 'try-text text
57
+ set 'try-last state-text r
58
+ try-log rejoin ["Started in " try-last]
59
+ show-try
60
+ ]
61
+
62
+ stop-try: function [why [string! none!]] [
63
+ if trying [attempt [machine-engine/stop trying]]
64
+ set 'trying none
65
+ clear try-lines
66
+ if why [try-log why toast why]
67
+ show-try
68
+ ]
69
+
70
+ ; the machine moved by itself: a timer, a run that ended
71
+ try-changed: function [] [
72
+ if any [not trying try-sending] [exit]
73
+ now-in: state-text trying
74
+ if now-in <> try-last [
75
+ try-log rejoin ["By itself: " try-last " to " now-in]
76
+ set 'try-last now-in
77
+ try-ended
78
+ ]
79
+ show-try
80
+ ]
81
+
82
+ try-ended: function [] [
83
+ if trying/done [try-log rejoin ["Ended" either trying/outcome [rejoin [": " trying/outcome]] [""]]]
84
+ ]
85
+
86
+ try-send: function [event [word!]] [
87
+ unless trying [exit]
88
+ set 'try-sending true
89
+ fields: attempt [load/all any [try-fields/value ""]]
90
+ r: try [send trying either all [block? fields not empty? fields] [append reduce [event] fields] [event]]
91
+ set 'try-sending false
92
+ either error? r [try-log rejoin [event ": " r/message]] [
93
+ now-in: state-text trying
94
+ try-log either now-in = try-last [rejoin [event ": stays in " now-in]] [rejoin [event ": " try-last " to " now-in]]
95
+ set 'try-last now-in
96
+ try-ended
97
+ ]
98
+ show-try
99
+ ]
100
+
101
+ ; a timer of the machine goes off now: a state's timeout, or the machine's ttl (key 0)
102
+ try-time: function [key [integer!]] [
103
+ unless all [trying x: select trying/timers key] [exit]
104
+ set 'try-sending true
105
+ machine-engine/drop second x
106
+ r: try [machine-engine/post trying either key = 0 ['expired] ['timeout] key first x]
107
+ set 'try-sending false
108
+ either error? r [try-log rejoin ["timeout: " r/message]] [
109
+ now-in: state-text trying
110
+ try-log rejoin [either key = 0 ["expired: "] ["timeout: "] try-last " to " now-in]
111
+ set 'try-last now-in
112
+ try-ended
113
+ ]
114
+ show-try
115
+ ]
116
+
117
+ ; the events a button is shown for: those its ways name, not the machine's own
118
+ try-events: function [] [
119
+ own: machine-engine/own
120
+ collect [foreach e any [machine-engine/events trying []] [unless find own e [keep e]]]
121
+ ]
122
+
123
+ try-active: function [] [
124
+ ; only in the drawing of the machine tried
125
+ unless all [trying try-k = either picked [picked/1] [1] m: pick ms try-k] [return copy []]
126
+ ; by path: the states of a fragment a state uses are the machine's own to the engine, and come
127
+ ; before the states written after that one, so the engine's ids are not the drawing's
128
+ out: copy []
129
+ foreach id trying/on [
130
+ path: copy []
131
+ while [id] [n: pick trying/nodes id if n/parent [insert path n/name] id: n/parent]
132
+ if x: drawn-state try-k m path [unless find out x [append out x]]
133
+ ]
134
+ out
135
+ ]
136
+
137
+ show-try: function [] [
138
+ ; while it runs, the properties give their room to it
139
+ try-panel/hidden: not trying
140
+ either trying [machine-props/hidden: nothing-chosen/hidden: true] [fill-mprops]
141
+ if trying [
142
+ try-buttons/pane: layout/only collect [
143
+ foreach e try-events [
144
+ keep compose/deep [button small (form e) [try-send (to lit-word! e)]]
145
+ unless all [not trying/done can? trying e] [keep 'disabled]
146
+ ]
147
+ foreach [key x] trying/timers [
148
+ if all [x key >= 0] [
149
+ name: either key = 0 ["Expire"] [rejoin ["Time out " select pick trying/nodes key 'name]]
150
+ keep compose/deep [button small #clock (name) [try-time (key)]]
151
+ ]
152
+ ]
153
+ ]
154
+ ; a field a line
155
+ try-data/text: either trying/data [
156
+ t: copy ""
157
+ foreach [w v] body-of trying/data [
158
+ unless empty? t [append t newline]
159
+ append t rejoin [mold w " " mold :v]
160
+ ]
161
+ t
162
+ ] ["none"]
163
+ ]
164
+ ; the last few: the drawing keeps the room
165
+ try-history/pane: layout/only collect [foreach l copy/part try-lines 12 [keep compose [text small muted (l)]]]
166
+ ; set by path: a trying: in the block would make the word local to this function
167
+ p: make machine-drawing/props [active: try-active]
168
+ p/trying: to logic! trying
169
+ machine-drawing/props: p
170
+ ]