trebol-dev 0.1.1 → 0.3.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,492 @@
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; or,
113
+ ; when it is made, its warnings (a state no way leads to yet), each starting with warning:
114
+ machine-problems-of: function [] [
115
+ collect [
116
+ foreach m ms [
117
+ named: either m/name [rejoin [m/name ": "]] [""]
118
+ if all [m/kind <> 'fragment late: late-use-problem d ms m] [
119
+ keep make map! reduce ['at any [late/1 ""] 'message rejoin [named late/2]]
120
+ continue
121
+ ]
122
+ e: either m/kind = 'fragment [fragment-problem m] [try [machine-engine/make-machine engine-block ms m]]
123
+ if error? e [
124
+ msg: e/message
125
+ if find/match msg "machine: " [msg: skip msg 9]
126
+ keep make map! reduce ['at any [e/at ""] 'message rejoin [named msg]]
127
+ ]
128
+ if object? e [
129
+ foreach [p msg] e/warnings [
130
+ keep make map! reduce ['at any [loaded-at p ""] 'message rejoin ["warning: " named msg] 'warning true]
131
+ ]
132
+ ]
133
+ ]
134
+ ]
135
+ ]
136
+
137
+ ; "No problems", "2 problems", "1 problem, 1 warning", "No problems, 2 warnings"
138
+ problem-count-text: function [n [integer!] w [integer!]] [
139
+ t: copy either n = 0 ["No problems"] [rejoin [n either n = 1 [" problem"] [" problems"]]]
140
+ if w > 0 [append t rejoin [", " w either w = 1 [" warning"] [" warnings"]]]
141
+ t
142
+ ]
143
+
144
+ show-machine-problems: function [] [
145
+ machine-problems/data: machine-problems-of
146
+ machine-problems/hidden: empty? machine-problems/data
147
+ if d [
148
+ w: 0
149
+ foreach p machine-problems/data [if select p 'warning [w: w + 1]]
150
+ n: (length? problems/data) + (length? machine-problems/data) - w
151
+ problem-count/text: problem-count-text n w
152
+ ]
153
+ ]
154
+
155
+ machine-node-of: function [id list [block!]] [
156
+ foreach n list [
157
+ if n/id = id [return n]
158
+ if n/children [if found: machine-node-of id n/children [return found]]
159
+ ]
160
+ none
161
+ ]
162
+
163
+ ; --- the drawing ---------------------------------------------------------------------------------
164
+
165
+ graph-data: function ["What the drawing of the k-th machine is given: its nodes and ways, by the structure's ids" k [integer!]] [
166
+ m: pick ms k
167
+ places: machine-places text machine-key d m
168
+ inner: copy [] inner-ways: copy []
169
+ nodes: collect [
170
+ foreach n m/nodes [
171
+ p: all [n/parent machine-node m n/parent]
172
+ ; where it is drawn, from the box it is in, when the file says
173
+ xy: none
174
+ if n/id > 1 [
175
+ path: machine-path m n/id
176
+ foreach [s spot] places [if path = to block! s [xy: spot]]
177
+ ]
178
+ keep make map! reduce [
179
+ 'x either xy [xy/x] [none]
180
+ 'y either xy [xy/y] [none]
181
+ 'id rejoin [either n/id = 1 ["m"] ["s"] k "." n/id]
182
+ 'name form n/name
183
+ 'kind form n/kind
184
+ 'parent either p [rejoin [either p/id = 1 ["m"] ["s"] k "." p/id]] [none]
185
+ 'initial to logic! all [p p/initial = n/name]
186
+ 'uses either n/uses [form n/uses] [none]
187
+ ]
188
+ ; opened, a state that uses a fragment shows it inside, to be seen there
189
+ id: rejoin ["s" k "." n/id]
190
+ if all [n/uses find opened id f: fragment-named ms n/uses] [
191
+ more: fragment-inside id rejoin [k "." n/id] id f either m/name [reduce [m/name]] [copy []]
192
+ append inner more/1
193
+ append inner-ways more/2
194
+ ]
195
+ ]
196
+ ]
197
+ append nodes inner
198
+ ways: collect [
199
+ foreach w m/ways [
200
+ if src: way-source m w [
201
+ keep make map! reduce [
202
+ 'id rejoin ["w" k "." w/id]
203
+ 'from rejoin ["s" k "." src]
204
+ 'to collect [foreach t way-targets m w [if t [keep rejoin ["s" k "." t]]]]
205
+ 'label way-label w
206
+ 'events either w/events [form w/events] [""]
207
+ ]
208
+ ]
209
+ ]
210
+ ]
211
+ append ways inner-ways
212
+ make map! reduce ['name either m/name [form m/name] [none] 'kind form m/kind 'nodes nodes 'ways ways]
213
+ ]
214
+
215
+ ; the chosen machine, or the first; none when the file makes none
216
+ draw-machine: function [] [
217
+ if all [trying text <> try-text] [stop-try "The code changed: try it again."]
218
+ k: either picked [picked/1] [1]
219
+ p: make machine-drawing/props [
220
+ graph: either pick ms k [graph-data k] [none]
221
+ selected: either picked [tree-id picked] [none]
222
+ active: try-active
223
+ editing: edit-next
224
+ canUndo: can-undo? hist
225
+ canRedo: can-redo? hist
226
+ ]
227
+ ; set by path: a trying: in the block would make the word local to this function
228
+ p/trying: to logic! trying
229
+ machine-drawing/props: p
230
+ set 'edit-next none
231
+ ]
232
+
233
+ ; a drag in the drawing from one state onto another: a new way between them, chosen
234
+ machine-connect: function [ids [block!]] [
235
+ a: split next first ids #"."
236
+ b: split next second ids #"."
237
+ k: to integer! a/1
238
+ if k <> to integer! b/1 [exit]
239
+ unless m: pick ms k [exit]
240
+ from: to integer! a/2
241
+ to-id: to integer! b/2
242
+ unless t: add-way d m from to-id 'go [exit]
243
+ edit t
244
+ ; the way is the last one of its level that leaves from there to there
245
+ foreach w select pick ms k 'ways [
246
+ 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]]
247
+ ]
248
+ redraw
249
+ ]
250
+
251
+ ; what is chosen, marked in the code
252
+ picked-span: function [] [
253
+ unless m: picked-machine [return none]
254
+ switch picked/3 [
255
+ machine [block-span d m/block]
256
+ state [n: picked-node range-span d n/holder n/from n/to]
257
+ way [w: picked-way range-span d w/block w/from w/to]
258
+ ]
259
+ ]
260
+
261
+ mark-machine: function [] [
262
+ s: picked-span
263
+ code-view/props: make code-view/props [mark: either s [reduce [s/x s/y]] [none]]
264
+ ]
265
+
266
+ ; the cursor put in the code: the innermost state or way written around it
267
+ machine-caret: function [at [integer!]] [
268
+ spots: copy [] ; [k id what span ...]: every machine, state and way, where it is written
269
+ k: 0
270
+ foreach m ms [
271
+ k: k + 1
272
+ append spots reduce [k 1 'machine block-span d m/block]
273
+ foreach n next m/nodes [append spots reduce [k n/id 'state range-span d n/holder n/from n/to]]
274
+ foreach w m/ways [append spots reduce [k w/id 'way range-span d w/block w/from w/to]]
275
+ ]
276
+ best: none
277
+ foreach [k id what s] spots [
278
+ if all [at >= s/x at <= (s/x + s/y) any [not best s/y <= best/4/y]] [best: reduce [k id what s]]
279
+ ]
280
+ if all [best any [not picked (copy/part best 3) <> picked]] [
281
+ set 'picked copy/part best 3
282
+ machine-tree/selected: machine-node-of tree-id picked machine-tree/data
283
+ mark-machine
284
+ draw-machine
285
+ fill-mprops
286
+ ]
287
+ ]
288
+
289
+ ; --- adding --------------------------------------------------------------------------------------
290
+
291
+ new-machine-code: function [] [
292
+ names: collect [foreach m ms [if m/name [keep m/name]]]
293
+ name: 'machine-1
294
+ k: 1
295
+ while [find names name] [k: k + 1 name: to word! rejoin ["machine-" k]]
296
+ rejoin [name ": machine [" newline " initial start" newline " state start []" newline "]" newline]
297
+ ]
298
+
299
+ machine-add: function ["What the Add menu of the machines does" item [word!]] [
300
+ if item = 'fragment [
301
+ code: new-fragment-code ms
302
+ edit fragment-before-machines d ms code
303
+ k: 1
304
+ foreach m ms [if m/name = to word! first split code #":" [break] k: k + 1]
305
+ set 'picked reduce [k 1 'machine]
306
+ redraw
307
+ exit
308
+ ]
309
+ if item = 'machine [
310
+ t: copy text
311
+ unless any [empty? t newline = last t] [append t newline]
312
+ unless empty? t [append t newline]
313
+ edit append t new-machine-code
314
+ set 'picked reduce [length? ms 1 'machine]
315
+ redraw
316
+ exit
317
+ ]
318
+ unless m: picked-machine [exit]
319
+ ; inside the chosen state, or the level of the chosen way
320
+ at: either w: picked-way [w/level] [picked/2]
321
+ if 'final = select machine-node m at 'kind [at: select machine-node m at 'parent]
322
+ base: select [state state final done parallel together] item
323
+ name: fresh-state-name m base
324
+ if t: add-state d m at name item [
325
+ k: picked/1
326
+ edit t
327
+ foreach n select pick ms k 'nodes [if n/name = name [set 'picked reduce [k n/id 'state]]]
328
+ redraw
329
+ ]
330
+ ]
331
+
332
+ ; --- the properties ------------------------------------------------------------------------------
333
+
334
+ code-field: function [label [string!] help [string!] what value] [
335
+ compose/deep [
336
+ text (label)
337
+ widget <trebol-code-small> [] with [value: (any [value ""])] on-change [machine-act (to lit-word! what) value]
338
+ text small muted (help)
339
+ ]
340
+ ]
341
+
342
+ state-choices: function [m [object!]] [
343
+ collect [foreach n next m/nodes [keep form to path! machine-path m n/id]]
344
+ ]
345
+
346
+ machine-props-code: function [] [
347
+ m: picked-machine
348
+ switch picked/3 [
349
+ machine [
350
+ if m/kind = 'fragment [return fragment-props-code m]
351
+ compose/deep [
352
+ code (rejoin [either m/name [rejoin [m/name ": "]] [""] "machine"])
353
+ text small muted "Drag states onto the drawing from its bar, and from the dots around a state to another to make a way."
354
+ field "name" "the word it is set to" with [value: (either m/name [form m/name] [""])] on-change [machine-act 'machine-name value]
355
+ (code-field "data" "its fields and their first values: parts: 0" 'data state-word-value d m 1 'data)
356
+ button #trash "Delete" danger [machine-act 'delete-machine none]
357
+ ]
358
+ ]
359
+ state [
360
+ n: picked-node
361
+ p: machine-node m n/parent
362
+ out: compose/deep [
363
+ code (title-lines state-title m n)
364
+ text small muted (form to path! machine-path m n/id)
365
+ field "name" "word" with [value: (form n/name)] on-change [machine-act 'name value]
366
+ ]
367
+ if p/kind <> 'parallel [
368
+ append out compose/deep [
369
+ check "initial: its level starts here" with [value: (p/initial = n/name)] on-change [machine-act 'initial value]
370
+ ]
371
+ ]
372
+ if n/kind = 'final [
373
+ append out compose/deep [
374
+ field "outcome" "the word the machine ends with" with [value: (either n/outcome [form pick n/holder n/outcome] [""])]
375
+ on-change [machine-act 'outcome value]
376
+ ]
377
+ ]
378
+ append out use-props-code m n
379
+ append out code-field "on-enter" "runs when the machine comes into it" 'on-enter state-word-value d m n/id 'on-enter
380
+ append out code-field "on-exit" "runs when the machine leaves it" 'on-exit state-word-value d m n/id 'on-exit
381
+ if n/kind <> 'final [
382
+ append out compose/deep [
383
+ field "timeout" "seconds, a time (0:15) or data/field" with [value: (any [state-word-value d m n/id 'timeout ""])]
384
+ on-change [machine-act 'timeout value]
385
+ ]
386
+ 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
387
+ append out compose/deep [
388
+ field "defer" "events kept until a state takes them" with [value: (any [state-word-value d m n/id 'defer ""])]
389
+ on-change [machine-act 'defer value]
390
+ ]
391
+ ]
392
+ unless empty? n/children [
393
+ if n/kind = 'state [
394
+ append out compose/deep [
395
+ check "history: comes back to the state it left" with [value: (not select n/words 'no)] on-change [machine-act 'history value]
396
+ ]
397
+ ]
398
+ ]
399
+ append out compose/deep [
400
+ row [
401
+ 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]]])]
402
+ on-menu [machine-act 'way-to event/picked]
403
+ button #trash "Delete" danger [machine-act 'delete none]
404
+ ]
405
+ ]
406
+ out
407
+ ]
408
+ way [
409
+ w: picked-way
410
+ out: compose/deep [
411
+ code (title-lines rejoin ["from " w/source " " way-title m w])
412
+ field "on" "the events it is taken on: start, or [start go]" with [value: (either w/events [form w/events] [""])]
413
+ on-change [machine-act 'events value]
414
+ ]
415
+ if w/kind = 'to [
416
+ append out compose/deep [
417
+ choice "to" [(state-choices m)] with [value: (form to path! machine-path m any [first way-targets m w 1])]
418
+ on-change [machine-act 'target value]
419
+ ]
420
+ ]
421
+ if w/branches [
422
+ append out compose/deep [
423
+ text small muted (rejoin [either w/kind = 'select ["select"] ["choose"] ": " (length? w/choices) / 3 " branches, in the code"])
424
+ ]
425
+ ]
426
+ if w/events [append out code-field "where" "a condition: the way is taken only when it holds" 'where way-code d w 'where]
427
+ append out code-field "set" "the data it changes when taken: parts: event/count" 'set way-code d w 'set
428
+ append out [button #trash "Delete" danger [machine-act 'delete-way none]]
429
+ out
430
+ ]
431
+ ]
432
+ ]
433
+
434
+ fill-mprops: function [] [
435
+ if any [editing <> "Machines" not try-panel/hidden] [exit]
436
+ machine-props/hidden: not picked
437
+ nothing-chosen/hidden: to logic! picked
438
+ props/hidden: true
439
+ if picked [machine-props/pane: layout/only machine-props-code]
440
+ ]
441
+
442
+ ; what a control of the machine's properties did
443
+ machine-act: function [what [word!] value] [
444
+ unless m: picked-machine [exit]
445
+ n: picked-node
446
+ w: picked-way
447
+ id: picked/2
448
+ 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]
449
+ t: switch what [
450
+ name [all [string? value v: attempt [load value] word? v rename-state d m id v]]
451
+ initial [either value [set-initial d m id] [none]]
452
+ outcome [
453
+ v: all [string? value attempt [load value]]
454
+ s: all [n/outcome span-of d n/holder n/outcome]
455
+ case [
456
+ all [s any [none? v v = []]] [splice d/text s/x - 1 s/y + 1 ""]
457
+ not word? v [none]
458
+ s [splice d/text s/x s/y form v]
459
+ true [s: span-of d n/holder n/from + 1 splice d/text s/x + s/y 0 rejoin [" " v]]
460
+ ]
461
+ ]
462
+ data on-enter on-exit run [set-state-word d m id what value]
463
+ timeout defer [set-state-word d m id what value]
464
+ history [set-state-word d m id 'no either value [none] ["history"]]
465
+ way-to [add-way d m id to integer! skip form value 6 'go]
466
+ delete [r: delete-state d m id set 'picked none r]
467
+ machine-name [
468
+ v: all [string? value attempt [load value]]
469
+ r: all [word? v rename-machine d ms m v]
470
+ if r [r: rename-machine-places r machine-key d m v]
471
+ r
472
+ ]
473
+ delete-machine [
474
+ r: delete-machine-text d m
475
+ if r [r: rename-machine-places r machine-key d m none]
476
+ set 'picked none
477
+ r
478
+ ]
479
+ spec [all [string? value set-fragment-spec d m value]]
480
+ use [set-use d m id either value = "(none)" [none] [to word! value]]
481
+ use-with [set-use-with d m id value]
482
+ events [
483
+ v: any [attempt [load/all any [value ""]] copy []]
484
+ if all [block? v 1 = length? v block? first v] [v: first v]
485
+ either all [block? v parse v [any word!]] [set-way-events d m w v] [none]
486
+ ]
487
+ target [set-way-target d m w find-machine-state m 1 load value]
488
+ where set [set-way-code d m w what value]
489
+ delete-way [r: delete-way d m w set 'picked none r]
490
+ ]
491
+ either t [edit t] [set 'keep-mprops false]
492
+ ]