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,476 @@
1
+ Trebol [
2
+ title: "The designer's machines: the structure, the properties and the code of the machines a file makes"
3
+ ]
4
+
5
+ ; Run by app.trb (do), so it shares the app's words: d, text, edit, code-view. The header's Edit switch
6
+ ; shows either the screens of a file or its machines; both write the same text, through the same Undo.
7
+ ; What is chosen is kept by place, [k id what]: the k-th machine of the file, and the id of one of its
8
+ ; nodes (what = 'machine or 'state) or of one of its ways (what = 'way).
9
+
10
+ editing: "Screens" ; or "Machines": what the window edits
11
+ ms: [] ; the machines of the design, read again with it
12
+ picked: none ; [k id what], or none
13
+ keep-mprops: false ; a property being typed in: the panel stays, so the typing goes on
14
+
15
+ picked-machine: does [all [picked pick ms picked/1]]
16
+ picked-node: does [all [m: picked-machine picked/3 <> 'way machine-node m picked/2]]
17
+ picked-way: does [all [m: picked-machine picked/3 = 'way pick m/ways picked/2]]
18
+
19
+ show-editing: function [what [string!]] [
20
+ set 'editing what
21
+ machines?: what = "Machines"
22
+ add-button/hidden: structure/hidden: machines?
23
+ machine-side/hidden: not machines?
24
+ preview-pane/hidden: any [machines? mode-f/value = "Code"]
25
+ machine-pane/hidden: any [not machines? mode-f/value = "Code"]
26
+ unless machines? [stop-try none]
27
+ redraw
28
+ ]
29
+
30
+ ; --- the structure -----------------------------------------------------------------------------
31
+
32
+ ; the title at the top of the properties, its words on lines short enough for the panel, so it never scrolls
33
+ title-lines: function ["A title broken into lines of at most 26 characters, between words" t [string!]] [
34
+ out: copy ""
35
+ line: 0
36
+ foreach w split t #" " [
37
+ case [
38
+ line = 0 []
39
+ line + 1 + (length? w) > 26 [append out newline line: 0]
40
+ true [append out " " line: line + 1]
41
+ ]
42
+ append out w
43
+ line: line + length? w
44
+ ]
45
+ out
46
+ ]
47
+
48
+ state-title: function [m [object!] n [object!]] [
49
+ t: rejoin [n/kind " " n/name]
50
+ if n/uses [append t rejoin [" uses " n/uses]]
51
+ p: all [n/parent machine-node m n/parent]
52
+ if all [p p/initial = n/name] [append t " (initial)"]
53
+ t
54
+ ]
55
+
56
+ way-title: function [m [object!] w [object!]] [
57
+ to-names: collect [foreach [b k] w/targets [keep mold pick b k]]
58
+ t: either w/kind = 'to [rejoin ["to " first to-names]] [rejoin [form any [w/kind "from"] " " form to-names]]
59
+ l: way-label w
60
+ if all [not empty? l w/kind = 'to] [append t rejoin [" on " l]]
61
+ if all [w/events w/kind <> 'to] [append t rejoin [" on " form w/events]]
62
+ t
63
+ ]
64
+
65
+ node-tree: function [m [object!] k [integer!] id [integer!]] [
66
+ n: machine-node m id
67
+ node: make map! reduce [
68
+ 'id rejoin [either id = 1 ["m"] ["s"] k "." id]
69
+ 'name either id = 1 [rejoin [either m/name [rejoin [m/name ": "]] [""] m/kind]] [state-title m n]
70
+ ]
71
+ kids: collect [
72
+ foreach c n/children [keep node-tree m k c]
73
+ ; the ways out of a state hang from it; one that names no state here, from its level
74
+ foreach w m/ways [
75
+ src: way-source m w
76
+ if any [src = id all [none? src w/level = id]] [
77
+ keep make map! reduce ['id rejoin ["w" k "." w/id] 'name way-title m w]
78
+ ]
79
+ ]
80
+ ]
81
+ unless empty? kids [node/children: kids node/expanded: true]
82
+ node
83
+ ]
84
+
85
+ tree-id: function [p [block!]] [rejoin [select [machine "m" state "s" way "w"] p/3 p/1 "." p/2]]
86
+
87
+ pick-machine: function ["Chooses what a row of the machines' structure is, or of the drawing" id [string!]] [
88
+ unless p: canvas-place id [exit]
89
+ set 'picked p
90
+ machine-tree/selected: machine-node-of id machine-tree/data
91
+ mark-machine
92
+ draw-machine
93
+ fill-mprops
94
+ ]
95
+
96
+ redraw-machines: function [] [
97
+ set 'ms either d [machines-of d] [copy []]
98
+ prune-opened
99
+ if all [picked any [not pick ms picked/1 not either picked/3 = 'way [picked-way] [picked-node]]] [set 'picked none]
100
+ k: 0
101
+ machine-tree/data: collect [foreach m ms [k: k + 1 keep node-tree m k 1]]
102
+ machine-tree/selected: all [picked machine-node-of tree-id picked machine-tree/data]
103
+ machine-none/hidden: not machine-tree/hidden: empty? ms
104
+ show-machine-problems
105
+ if editing = "Machines" [
106
+ mark-machine
107
+ draw-machine
108
+ either keep-mprops [set 'keep-mprops false] [fill-mprops]
109
+ ]
110
+ ]
111
+
112
+ ; what the engine says is wrong with each machine: the first mistake it finds, where it is written
113
+ machine-problems-of: function [] [
114
+ collect [
115
+ foreach m ms [
116
+ if all [m/kind <> 'fragment late: late-use-problem d ms m] [
117
+ keep make map! reduce ['at any [late/1 ""] 'message rejoin [either m/name [rejoin [m/name ": "]] [""] late/2]]
118
+ continue
119
+ ]
120
+ e: either m/kind = 'fragment [fragment-problem m] [try [machine-engine/make-machine engine-block ms m]]
121
+ if error? e [
122
+ msg: e/message
123
+ if find/match msg "machine: " [msg: skip msg 9]
124
+ keep make map! reduce ['at any [e/at ""] 'message rejoin [either m/name [rejoin [m/name ": "]] [""] msg]]
125
+ ]
126
+ ]
127
+ ]
128
+ ]
129
+
130
+ show-machine-problems: function [] [
131
+ machine-problems/data: machine-problems-of
132
+ machine-problems/hidden: empty? machine-problems/data
133
+ if d [
134
+ n: (length? problems/data) + length? machine-problems/data
135
+ problem-count/text: either n = 0 ["No problems"] [rejoin [n either n = 1 [" problem"] [" problems"]]]
136
+ ]
137
+ ]
138
+
139
+ machine-node-of: function [id list [block!]] [
140
+ foreach n list [
141
+ if n/id = id [return n]
142
+ if n/children [if found: machine-node-of id n/children [return found]]
143
+ ]
144
+ none
145
+ ]
146
+
147
+ ; --- the drawing ---------------------------------------------------------------------------------
148
+
149
+ graph-data: function ["What the drawing of the k-th machine is given: its nodes and ways, by the structure's ids" k [integer!]] [
150
+ m: pick ms k
151
+ places: machine-places text machine-key d m
152
+ inner: copy [] inner-ways: copy []
153
+ nodes: collect [
154
+ foreach n m/nodes [
155
+ p: all [n/parent machine-node m n/parent]
156
+ ; where it is drawn, from the box it is in, when the file says
157
+ xy: none
158
+ if n/id > 1 [
159
+ path: machine-path m n/id
160
+ foreach [s spot] places [if path = to block! s [xy: spot]]
161
+ ]
162
+ keep make map! reduce [
163
+ 'x either xy [xy/x] [none]
164
+ 'y either xy [xy/y] [none]
165
+ 'id rejoin [either n/id = 1 ["m"] ["s"] k "." n/id]
166
+ 'name form n/name
167
+ 'kind form n/kind
168
+ 'parent either p [rejoin [either p/id = 1 ["m"] ["s"] k "." p/id]] [none]
169
+ 'initial to logic! all [p p/initial = n/name]
170
+ 'uses either n/uses [form n/uses] [none]
171
+ ]
172
+ ; opened, a state that uses a fragment shows it inside, to be seen there
173
+ id: rejoin ["s" k "." n/id]
174
+ if all [n/uses find opened id f: fragment-named ms n/uses] [
175
+ more: fragment-inside id rejoin [k "." n/id] id f either m/name [reduce [m/name]] [copy []]
176
+ append inner more/1
177
+ append inner-ways more/2
178
+ ]
179
+ ]
180
+ ]
181
+ append nodes inner
182
+ ways: collect [
183
+ foreach w m/ways [
184
+ if src: way-source m w [
185
+ keep make map! reduce [
186
+ 'id rejoin ["w" k "." w/id]
187
+ 'from rejoin ["s" k "." src]
188
+ 'to collect [foreach t way-targets m w [if t [keep rejoin ["s" k "." t]]]]
189
+ 'label way-label w
190
+ 'events either w/events [form w/events] [""]
191
+ ]
192
+ ]
193
+ ]
194
+ ]
195
+ append ways inner-ways
196
+ make map! reduce ['name either m/name [form m/name] [none] 'kind form m/kind 'nodes nodes 'ways ways]
197
+ ]
198
+
199
+ ; the chosen machine, or the first; none when the file makes none
200
+ draw-machine: function [] [
201
+ if all [trying text <> try-text] [stop-try "The code changed: try it again."]
202
+ k: either picked [picked/1] [1]
203
+ p: make machine-drawing/props [
204
+ graph: either pick ms k [graph-data k] [none]
205
+ selected: either picked [tree-id picked] [none]
206
+ active: try-active
207
+ editing: edit-next
208
+ canUndo: can-undo? hist
209
+ canRedo: can-redo? hist
210
+ ]
211
+ ; set by path: a trying: in the block would make the word local to this function
212
+ p/trying: to logic! trying
213
+ machine-drawing/props: p
214
+ set 'edit-next none
215
+ ]
216
+
217
+ ; a drag in the drawing from one state onto another: a new way between them, chosen
218
+ machine-connect: function [ids [block!]] [
219
+ a: split next first ids #"."
220
+ b: split next second ids #"."
221
+ k: to integer! a/1
222
+ if k <> to integer! b/1 [exit]
223
+ unless m: pick ms k [exit]
224
+ from: to integer! a/2
225
+ to-id: to integer! b/2
226
+ unless t: add-way d m from to-id 'go [exit]
227
+ edit t
228
+ ; the way is the last one of its level that leaves from there to there
229
+ foreach w select pick ms k 'ways [
230
+ if all [from = way-source pick ms k w find way-targets pick ms k w to-id] [set 'picked reduce [k w/id 'way]]
231
+ ]
232
+ redraw
233
+ ]
234
+
235
+ ; what is chosen, marked in the code
236
+ picked-span: function [] [
237
+ unless m: picked-machine [return none]
238
+ switch picked/3 [
239
+ machine [block-span d m/block]
240
+ state [n: picked-node range-span d n/holder n/from n/to]
241
+ way [w: picked-way range-span d w/block w/from w/to]
242
+ ]
243
+ ]
244
+
245
+ mark-machine: function [] [
246
+ s: picked-span
247
+ code-view/props: make code-view/props [mark: either s [reduce [s/x s/y]] [none]]
248
+ ]
249
+
250
+ ; the cursor put in the code: the innermost state or way written around it
251
+ machine-caret: function [at [integer!]] [
252
+ spots: copy [] ; [k id what span ...]: every machine, state and way, where it is written
253
+ k: 0
254
+ foreach m ms [
255
+ k: k + 1
256
+ append spots reduce [k 1 'machine block-span d m/block]
257
+ foreach n next m/nodes [append spots reduce [k n/id 'state range-span d n/holder n/from n/to]]
258
+ foreach w m/ways [append spots reduce [k w/id 'way range-span d w/block w/from w/to]]
259
+ ]
260
+ best: none
261
+ foreach [k id what s] spots [
262
+ if all [at >= s/x at <= (s/x + s/y) any [not best s/y <= best/4/y]] [best: reduce [k id what s]]
263
+ ]
264
+ if all [best any [not picked (copy/part best 3) <> picked]] [
265
+ set 'picked copy/part best 3
266
+ machine-tree/selected: machine-node-of tree-id picked machine-tree/data
267
+ mark-machine
268
+ draw-machine
269
+ fill-mprops
270
+ ]
271
+ ]
272
+
273
+ ; --- adding --------------------------------------------------------------------------------------
274
+
275
+ new-machine-code: function [] [
276
+ names: collect [foreach m ms [if m/name [keep m/name]]]
277
+ name: 'machine-1
278
+ k: 1
279
+ while [find names name] [k: k + 1 name: to word! rejoin ["machine-" k]]
280
+ rejoin [name ": machine [" newline " initial start" newline " state start []" newline "]" newline]
281
+ ]
282
+
283
+ machine-add: function ["What the Add menu of the machines does" item [word!]] [
284
+ if item = 'fragment [
285
+ code: new-fragment-code ms
286
+ edit fragment-before-machines d ms code
287
+ k: 1
288
+ foreach m ms [if m/name = to word! first split code #":" [break] k: k + 1]
289
+ set 'picked reduce [k 1 'machine]
290
+ redraw
291
+ exit
292
+ ]
293
+ if item = 'machine [
294
+ t: copy text
295
+ unless any [empty? t newline = last t] [append t newline]
296
+ unless empty? t [append t newline]
297
+ edit append t new-machine-code
298
+ set 'picked reduce [length? ms 1 'machine]
299
+ redraw
300
+ exit
301
+ ]
302
+ unless m: picked-machine [exit]
303
+ ; inside the chosen state, or the level of the chosen way
304
+ at: either w: picked-way [w/level] [picked/2]
305
+ if 'final = select machine-node m at 'kind [at: select machine-node m at 'parent]
306
+ base: select [state state final done parallel together] item
307
+ name: fresh-state-name m base
308
+ if t: add-state d m at name item [
309
+ k: picked/1
310
+ edit t
311
+ foreach n select pick ms k 'nodes [if n/name = name [set 'picked reduce [k n/id 'state]]]
312
+ redraw
313
+ ]
314
+ ]
315
+
316
+ ; --- the properties ------------------------------------------------------------------------------
317
+
318
+ code-field: function [label [string!] help [string!] what value] [
319
+ compose/deep [
320
+ text (label)
321
+ widget <trebol-code-small> [] with [value: (any [value ""])] on-change [machine-act (to lit-word! what) value]
322
+ text small muted (help)
323
+ ]
324
+ ]
325
+
326
+ state-choices: function [m [object!]] [
327
+ collect [foreach n next m/nodes [keep form to path! machine-path m n/id]]
328
+ ]
329
+
330
+ machine-props-code: function [] [
331
+ m: picked-machine
332
+ switch picked/3 [
333
+ machine [
334
+ if m/kind = 'fragment [return fragment-props-code m]
335
+ compose/deep [
336
+ code (rejoin [either m/name [rejoin [m/name ": "]] [""] "machine"])
337
+ text small muted "Drag states onto the drawing from its bar, and from the dots around a state to another to make a way."
338
+ field "name" "the word it is set to" with [value: (either m/name [form m/name] [""])] on-change [machine-act 'machine-name value]
339
+ (code-field "data" "its fields and their first values: parts: 0" 'data state-word-value d m 1 'data)
340
+ button #trash "Delete" danger [machine-act 'delete-machine none]
341
+ ]
342
+ ]
343
+ state [
344
+ n: picked-node
345
+ p: machine-node m n/parent
346
+ out: compose/deep [
347
+ code (title-lines state-title m n)
348
+ text small muted (form to path! machine-path m n/id)
349
+ field "name" "word" with [value: (form n/name)] on-change [machine-act 'name value]
350
+ ]
351
+ if p/kind <> 'parallel [
352
+ append out compose/deep [
353
+ check "initial: its level starts here" with [value: (p/initial = n/name)] on-change [machine-act 'initial value]
354
+ ]
355
+ ]
356
+ if n/kind = 'final [
357
+ append out compose/deep [
358
+ field "outcome" "the word the machine ends with" with [value: (either n/outcome [form pick n/holder n/outcome] [""])]
359
+ on-change [machine-act 'outcome value]
360
+ ]
361
+ ]
362
+ append out use-props-code m n
363
+ append out code-field "on-enter" "runs when the machine comes into it" 'on-enter state-word-value d m n/id 'on-enter
364
+ append out code-field "on-exit" "runs when the machine leaves it" 'on-exit state-word-value d m n/id 'on-exit
365
+ if n/kind <> 'final [
366
+ append out compose/deep [
367
+ field "timeout" "seconds, a time (0:15) or data/field" with [value: (any [state-word-value d m n/id 'timeout ""])]
368
+ on-change [machine-act 'timeout value]
369
+ ]
370
+ append out code-field "run" "work that takes a while: done or error when it ends" 'run state-word-value d m n/id 'run
371
+ append out compose/deep [
372
+ field "defer" "events kept until a state takes them" with [value: (any [state-word-value d m n/id 'defer ""])]
373
+ on-change [machine-act 'defer value]
374
+ ]
375
+ ]
376
+ unless empty? n/children [
377
+ if n/kind = 'state [
378
+ append out compose/deep [
379
+ check "history: comes back to the state it left" with [value: (not select n/words 'no)] on-change [machine-act 'history value]
380
+ ]
381
+ ]
382
+ ]
383
+ append out compose/deep [
384
+ row [
385
+ button #arrow-right "Add a way" menu [(collect [foreach n next m/nodes [keep form to path! machine-path m n/id keep to word! rejoin ["state-" n/id]]])]
386
+ on-menu [machine-act 'way-to event/picked]
387
+ button #trash "Delete" danger [machine-act 'delete none]
388
+ ]
389
+ ]
390
+ out
391
+ ]
392
+ way [
393
+ w: picked-way
394
+ out: compose/deep [
395
+ code (title-lines rejoin ["from " w/source " " way-title m w])
396
+ field "on" "the events it is taken on: start, or [start go]" with [value: (either w/events [form w/events] [""])]
397
+ on-change [machine-act 'events value]
398
+ ]
399
+ if w/kind = 'to [
400
+ append out compose/deep [
401
+ choice "to" [(state-choices m)] with [value: (form to path! machine-path m any [first way-targets m w 1])]
402
+ on-change [machine-act 'target value]
403
+ ]
404
+ ]
405
+ if w/branches [
406
+ append out compose/deep [
407
+ text small muted (rejoin [either w/kind = 'select ["select"] ["choose"] ": " (length? w/choices) / 3 " branches, in the code"])
408
+ ]
409
+ ]
410
+ if w/events [append out code-field "where" "a condition: the way is taken only when it holds" 'where way-code d w 'where]
411
+ append out code-field "set" "the data it changes when taken: parts: event/count" 'set way-code d w 'set
412
+ append out [button #trash "Delete" danger [machine-act 'delete-way none]]
413
+ out
414
+ ]
415
+ ]
416
+ ]
417
+
418
+ fill-mprops: function [] [
419
+ if any [editing <> "Machines" not try-panel/hidden] [exit]
420
+ machine-props/hidden: not picked
421
+ nothing-chosen/hidden: to logic! picked
422
+ props/hidden: true
423
+ if picked [machine-props/pane: layout/only machine-props-code]
424
+ ]
425
+
426
+ ; what a control of the machine's properties did
427
+ machine-act: function [what [word!] value] [
428
+ unless m: picked-machine [exit]
429
+ n: picked-node
430
+ w: picked-way
431
+ id: picked/2
432
+ if find [name machine-name data outcome on-enter on-exit timeout run defer events where set spec use-with] what [set 'keep-mprops true]
433
+ t: switch what [
434
+ name [all [string? value v: attempt [load value] word? v rename-state d m id v]]
435
+ initial [either value [set-initial d m id] [none]]
436
+ outcome [
437
+ v: all [string? value attempt [load value]]
438
+ s: all [n/outcome span-of d n/holder n/outcome]
439
+ case [
440
+ all [s any [none? v v = []]] [splice d/text s/x - 1 s/y + 1 ""]
441
+ not word? v [none]
442
+ s [splice d/text s/x s/y form v]
443
+ true [s: span-of d n/holder n/from + 1 splice d/text s/x + s/y 0 rejoin [" " v]]
444
+ ]
445
+ ]
446
+ data on-enter on-exit run [set-state-word d m id what value]
447
+ timeout defer [set-state-word d m id what value]
448
+ history [set-state-word d m id 'no either value [none] ["history"]]
449
+ way-to [add-way d m id to integer! skip form value 6 'go]
450
+ delete [r: delete-state d m id set 'picked none r]
451
+ machine-name [
452
+ v: all [string? value attempt [load value]]
453
+ r: all [word? v rename-machine d ms m v]
454
+ if r [r: rename-machine-places r machine-key d m v]
455
+ r
456
+ ]
457
+ delete-machine [
458
+ r: delete-machine-text d m
459
+ if r [r: rename-machine-places r machine-key d m none]
460
+ set 'picked none
461
+ r
462
+ ]
463
+ spec [all [string? value set-fragment-spec d m value]]
464
+ use [set-use d m id either value = "(none)" [none] [to word! value]]
465
+ use-with [set-use-with d m id value]
466
+ events [
467
+ v: any [attempt [load/all any [value ""]] copy []]
468
+ if all [block? v 1 = length? v block? first v] [v: first v]
469
+ either all [block? v parse v [any word!]] [set-way-events d m w v] [none]
470
+ ]
471
+ target [set-way-target d m w find-machine-state m 1 load value]
472
+ where set [set-way-code d m w what value]
473
+ delete-way [r: delete-way d m w set 'picked none r]
474
+ ]
475
+ either t [edit t] [set 'keep-mprops false]
476
+ ]