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,167 @@
1
+ Trebol [
2
+ title: "The designer's fragments: listed with the machines, drawn on their own, seen inside the states that use them"
3
+ ]
4
+
5
+ ; Run by app.trb (do), after machines-view.trb, so it shares its words. A fragment is edited on its own
6
+ ; drawing, as a machine is. A state that uses one is drawn with its name; opened, the fragment's states
7
+ ; and ways are drawn inside it as they are on the fragment's drawing, to be seen and not changed there:
8
+ ; they are not written in that state. Their ids say so: u<k>.<state>:<id>, v<k>.<state>:<id> for ways,
9
+ ; one :<id> more for each fragment used inside another.
10
+
11
+ opened: [] ; ids of the states that use a fragment and are shown open: ["s1.3" ...]
12
+
13
+ toggle-open: function ["Opens or closes what a state that uses a fragment shows" id [string!]] [
14
+ either find opened id [remove find opened id] [append opened id]
15
+ draw-machine
16
+ fill-mprops
17
+ ]
18
+
19
+ ; read again, only the states that still use a fragment stay open
20
+ prune-opened: function [] [
21
+ still: collect [
22
+ foreach id opened [
23
+ p: canvas-place id
24
+ n: all [p m: pick ms p/1 p/2 <= length? m/nodes machine-node m p/2]
25
+ if all [n n/uses] [keep id]
26
+ ]
27
+ ]
28
+ set 'opened still
29
+ ]
30
+
31
+ open-fragment: function ["Chooses the drawing of a fragment, by name" name] [
32
+ if string? name [name: to word! name]
33
+ k: 0
34
+ foreach m ms [
35
+ k: k + 1
36
+ if all [m/kind = 'fragment m/name = name] [
37
+ set 'picked reduce [k 1 'machine]
38
+ machine-tree/selected: machine-node-of tree-id picked machine-tree/data
39
+ mark-machine
40
+ draw-machine
41
+ fill-mprops
42
+ exit
43
+ ]
44
+ ]
45
+ ]
46
+
47
+ inside-id: function [prefix [string!] parent [string!] id [integer!]] [either id = 1 [parent] [rejoin ["u" prefix ":" id]]]
48
+
49
+ ; The nodes and ways a fragment adds inside the state that uses it, for graph-data: [nodes ways].
50
+ ; owner is the state's id in the drawing; seen keeps a fragment that uses itself from going on forever.
51
+ fragment-inside: function [owner [string!] prefix [string!] parent [string!] f [object!] seen [block!]] [
52
+ nodes: copy [] ways: copy []
53
+ if find seen f/name [return reduce [nodes ways]]
54
+ seen: append copy seen f/name
55
+ places: machine-places text machine-key d f
56
+ foreach n next f/nodes [
57
+ path: machine-path f n/id
58
+ xy: none
59
+ foreach [s spot] places [if path = to block! s [xy: spot]]
60
+ p: machine-node f n/parent
61
+ append nodes make map! reduce [
62
+ 'x either xy [xy/x] [none]
63
+ 'y either xy [xy/y] [none]
64
+ 'id inside-id prefix parent n/id
65
+ 'name form n/name
66
+ 'kind form n/kind
67
+ 'parent inside-id prefix parent n/parent
68
+ 'initial to logic! p/initial = n/name
69
+ 'uses either n/uses [form n/uses] [none]
70
+ 'fixed true
71
+ 'owner owner
72
+ ]
73
+ if all [n/uses g: fragment-named ms n/uses] [
74
+ more: fragment-inside owner rejoin [prefix ":" n/id] inside-id prefix parent n/id g seen
75
+ append nodes more/1
76
+ append ways more/2
77
+ ]
78
+ ]
79
+ foreach w f/ways [
80
+ if src: way-source f w [
81
+ append ways make map! reduce [
82
+ 'id rejoin ["v" prefix ":" w/id]
83
+ 'from inside-id prefix parent src
84
+ 'to collect [foreach t way-targets f w [if t [keep inside-id prefix parent t]]]
85
+ 'label way-label w
86
+ 'events either w/events [form w/events] [""]
87
+ 'fixed true
88
+ 'owner owner
89
+ ]
90
+ ]
91
+ ]
92
+ reduce [nodes ways]
93
+ ]
94
+
95
+ ; --- the properties ------------------------------------------------------------------------------
96
+
97
+ fragment-props-code: function [m [object!]] [
98
+ used: (length? uses-of ms m/name) / 2
99
+ compose/deep [
100
+ code (rejoin [either m/name [rejoin [m/name ": "]] [""] "fragment"])
101
+ text small muted (rejoin [
102
+ "A piece of machine made once, which states of machines use. "
103
+ either used = 0 ["No state uses it yet."] [rejoin [used either used = 1 [" state uses it."] [" states use it."]]]
104
+ ])
105
+ field "name" "the word it is set to" with [value: (either m/name [form m/name] [""])] on-change [machine-act 'machine-name value]
106
+ (code-field "blanks" "what each use fills in, with the types it takes: work [block!] check [block!]" 'spec fragment-spec-value d m)
107
+ button #trash "Delete" danger [machine-act 'delete-machine none]
108
+ ]
109
+ ]
110
+
111
+ use-props-code: function [m [object!] n [object!]] [
112
+ names: collect [foreach f fragments-of ms [if all [f/name not same? f m] [keep form f/name]]]
113
+ if empty? names [return copy []]
114
+ out: compose/deep [
115
+ choice "use" [(append copy ["(none)"] names)] with [value: (either n/uses [form n/uses] ["(none)"])]
116
+ on-change [machine-act 'use value]
117
+ ]
118
+ if n/uses [
119
+ f: fragment-named ms n/uses
120
+ help: either f [rejoin ["the blanks of " n/uses ": " fragment-spec-value d f]] ["blank: value for each blank"]
121
+ append out code-field "with" help 'use-with use-with-value d m n/id
122
+ append out compose/deep [
123
+ row [
124
+ button #eye (either find opened rejoin ["s" picked/1 "." n/id] ["Close"] ["Open"]) small
125
+ [toggle-open (rejoin ["s" picked/1 "." n/id])]
126
+ button #edit (rejoin ["Edit " n/uses]) small [open-fragment (form n/uses)]
127
+ ]
128
+ ]
129
+ ]
130
+ out
131
+ ]
132
+
133
+ ; --- trying -------------------------------------------------------------------------------------
134
+
135
+ drawn-state: function [
136
+ "The id in the drawing of a state the engine names by its path: inside a state that uses a fragment, the fragment's state when it is open, else the state itself"
137
+ k [integer!] m [object!] path [block!]
138
+ ] [
139
+ id: 1
140
+ while [not empty? path] [
141
+ unless c: state-child m id first path [break]
142
+ id: c
143
+ path: next path
144
+ n: machine-node m id
145
+ if all [n/uses not empty? path] [
146
+ owner: rejoin ["s" k "." id]
147
+ unless find opened owner [return owner]
148
+ prefix: rejoin [k "." id]
149
+ f: m
150
+ seen: copy []
151
+ while [all [n/uses not empty? path f: fragment-named ms n/uses not find seen f/name]] [
152
+ append seen f/name
153
+ fid: 1
154
+ while [all [not empty? path c: state-child f fid first path]] [
155
+ fid: c
156
+ path: next path
157
+ n: machine-node f fid
158
+ prefix: rejoin [prefix ":" fid]
159
+ if n/uses [break]
160
+ ]
161
+ if any [empty? path not n/uses] [break]
162
+ ]
163
+ return rejoin ["u" prefix]
164
+ ]
165
+ ]
166
+ either id = 1 [none] [rejoin ["s" k "." id]]
167
+ ]
@@ -0,0 +1,187 @@
1
+ Trebol [
2
+ title: "Fragments in the designer: pieces of machine made once, used in states, and the edits they take"
3
+ exports: [
4
+ engine-block fragment-problem late-use-problem fragment-before-machines machine-head rename-machine delete-machine-text new-fragment-code
5
+ set-use use-with-value set-use-with fragment-spec-value set-fragment-spec uses-of
6
+ ]
7
+ ]
8
+
9
+ import %model.trb
10
+ import %machines.trb
11
+ import %machine-edits.trb
12
+
13
+ ; The file is never run, so the words a machine's use names are not set: to make it as the engine does,
14
+ ; for its problems or to try it, its block is copied and bound to the fragments written before it, made
15
+ ; from their text. A fragment written after the machine is not made yet when the file runs.
16
+ engine-block: function [
17
+ "A copy of a machine's block, its use words bound to the fragments written before it in the file"
18
+ ms [block!] m [object!]
19
+ ] [
20
+ frs: fragments-before ms m
21
+ b: copy/deep m/block
22
+ if empty? frs [return b]
23
+ ctx: make object! append collect [foreach f frs [keep to set-word! f/name]] none
24
+ foreach f frs [
25
+ body: copy/deep f/block
26
+ bind body ctx
27
+ set in ctx f/name attempt [fragment copy/deep f/spec body]
28
+ ]
29
+ bind b ctx
30
+ ]
31
+
32
+ fragments-before: function ["The fragments written before a machine" ms [block!] m [object!]] [
33
+ collect [foreach x ms [if same? x m [break] if x/kind = 'fragment [keep x]]]
34
+ ]
35
+
36
+ late-use-problem: function [
37
+ "The problem of a machine that uses a fragment written after it: [line:col message], or none"
38
+ d [object!] ms [block!] m [object!]
39
+ ] [
40
+ before: collect [foreach f fragments-before ms m [keep f/name]]
41
+ foreach n next m/nodes [
42
+ if all [n/uses not find before n/uses fragment-named ms n/uses] [
43
+ at: none
44
+ foreach k keys-of d/spots [
45
+ v: select d/spots k
46
+ if all [same? v/2 n/block v/3 = (n/use-at + 1)] [at: k]
47
+ ]
48
+ return reduce [at rejoin ["state " n/name " uses " n/uses ", a fragment written after the machine: write it before"]]
49
+ ]
50
+ ]
51
+ none
52
+ ]
53
+
54
+ fragment-before-machines: function [
55
+ "The text with a fragment's code put before the first machine that is no fragment, or at the end when there is none"
56
+ d [object!] ms [block!] code [string!]
57
+ ] [
58
+ first-m: none
59
+ foreach m ms [if m/kind <> 'fragment [first-m: m break]]
60
+ if all [first-m h: machine-head d first-m] [
61
+ pos: first span-of d h/1 h/2
62
+ ; the statement of the file that holds it, with the words set before it
63
+ top: none
64
+ repeat i length? d/values [if (first span-of d d/values i) <= pos [top: i]]
65
+ if top [
66
+ while [all [top > 1 set-word? pick d/values top - 1]] [top: top - 1]
67
+ at-i: line-start d/text first span-of d d/values top
68
+ return splice d/text at-i 0 rejoin [code newline]
69
+ ]
70
+ ]
71
+ t: copy d/text
72
+ unless any [empty? t newline = last t] [append t newline]
73
+ unless empty? t [append t newline]
74
+ append t code
75
+ ]
76
+
77
+ fragment-problem: function ["What is wrong with a fragment's blanks, or none" f [object!]] [
78
+ e: try [fragment copy/deep f/spec copy/deep f/block]
79
+ either error? e [e] [none]
80
+ ]
81
+
82
+ uses-of: function ["The states of the machines that use a fragment: [machine node ...]" ms [block!] name [word!]] [
83
+ collect [foreach m ms [foreach n next m/nodes [if n/uses = name [keep m keep n]]]]
84
+ ]
85
+
86
+ ; --- the head of a machine: repair: machine [...] or retry: fragment [...] [...] ---------------------------
87
+
88
+ machine-head: function [
89
+ "Where a machine is written in the file: [block index], the index of its word (repair:) or of machine itself"
90
+ d [object!] m [object!]
91
+ ] [
92
+ foreach [blk sps own] d/blocks [
93
+ repeat i length? blk [
94
+ if same? pick blk i m/block [
95
+ h: either m/kind = 'fragment [i - 2] [i - 1]
96
+ if all [h > 1 set-word? pick blk h - 1] [h: h - 1]
97
+ return reduce [blk h]
98
+ ]
99
+ ]
100
+ ]
101
+ none
102
+ ]
103
+
104
+ rename-machine: function [
105
+ "The text with a machine or fragment set to another word; the states that use a fragment use it by its new name"
106
+ d [object!] ms [block!] m [object!] v [word!]
107
+ ] [
108
+ unless at: machine-head d m [return none]
109
+ if all [m/kind = 'fragment any [find collect [foreach x ms [if x/name [keep x/name]]] v]] [return none]
110
+ blk: at/1 h: at/2
111
+ changes: either set-word? pick blk h [
112
+ value-change d blk h rejoin [v ":"]
113
+ ] [
114
+ s: span-of d blk h
115
+ reduce [s/x 0 rejoin [v ": "]]
116
+ ]
117
+ if all [m/kind = 'fragment m/name] [
118
+ foreach [x n] uses-of ms m/name [append changes value-change d n/block n/use-at + 1 form v]
119
+ ]
120
+ apply-changes d/text changes
121
+ ]
122
+
123
+ delete-machine-text: function ["The text without a machine or fragment" d [object!] m [object!]] [
124
+ unless at: machine-head d m [return none]
125
+ i: at/2 + either set-word? pick at/1 at/2 [1] [0]
126
+ cut d/text range-span d at/1 at/2 i + either m/kind = 'fragment [2] [1]
127
+ ]
128
+
129
+ new-fragment-code: function ["The code of a new fragment, named so that no machine or fragment has its name" ms [block!]] [
130
+ names: collect [foreach m ms [if m/name [keep m/name]]]
131
+ name: 'fragment-1
132
+ k: 1
133
+ while [find names name] [k: k + 1 name: to word! rejoin ["fragment-" k]]
134
+ rejoin [name ": fragment [] [" newline " initial start" newline " state start []" newline "]" newline]
135
+ ]
136
+
137
+ ; --- the edits of a state that uses one ----------------------------------------------------------------
138
+
139
+ set-use: function [
140
+ "The text with a state using a fragment by name, or using none for none"
141
+ d [object!] m [object!] id [integer!] name
142
+ ] [
143
+ n: machine-node m id
144
+ case [
145
+ all [none? name n/use-at] [apply-changes d/text cut-change d n/block n/use-at either n/with-at [n/with-at + 1] [n/use-at + 1]]
146
+ none? name [d/text]
147
+ n/use-at [apply-changes d/text value-change d n/block n/use-at + 1 form name]
148
+ true [set-state-word d m id 'use form name]
149
+ ]
150
+ ]
151
+
152
+ use-with-value: function ["What a state gives the blanks of the fragment it uses, as written inside with's block, or none" d [object!] m [object!] id [integer!]] [
153
+ n: machine-node m id
154
+ unless all [n/with-at block? pick n/block n/with-at + 1] [return none]
155
+ s: span-of d n/block n/with-at + 1
156
+ copy/part at d/text s/x + 1 s/y - 2
157
+ ]
158
+
159
+ set-use-with: function [
160
+ "The text with what a state gives the blanks of its fragment: work: [charge] check: [data/ok]; none or empty takes with out"
161
+ d [object!] m [object!] id [integer!] code
162
+ ] [
163
+ n: machine-node m id
164
+ empty: any [none? code empty? trim copy code]
165
+ case [
166
+ all [empty n/with-at] [apply-changes d/text cut-change d n/block n/with-at n/with-at + 1]
167
+ empty [d/text]
168
+ n/with-at [apply-changes d/text value-change d n/block n/with-at + 1 rejoin ["[" trim copy code "]"]]
169
+ n/use-at [
170
+ s: span-of d n/block n/use-at + 1
171
+ splice d/text s/x + s/y 0 rejoin [" with [" trim copy code "]"]
172
+ ]
173
+ true [none]
174
+ ]
175
+ ]
176
+
177
+ ; --- the blanks of a fragment ----------------------------------------------------------------------------
178
+
179
+ fragment-spec-value: function ["A fragment's blanks as written inside its spec block" d [object!] m [object!]] [
180
+ s: block-span d m/spec
181
+ copy/part at d/text s/x + 1 s/y - 2
182
+ ]
183
+
184
+ set-fragment-spec: function ["The text with a fragment's blanks: work [block!] check [block!]" d [object!] m [object!] code [string!]] [
185
+ s: block-span d m/spec
186
+ splice d/text s/x s/y rejoin ["[" trim copy code "]"]
187
+ ]
@@ -0,0 +1,157 @@
1
+ Trebol [
2
+ title: "The designer's machine canvas: what the drawing's bar, drags, double clicks and menus do"
3
+ ]
4
+
5
+ ; Run by app.trb (do), after machines-view.trb and machine-try.trb, so it shares their words. The drawing
6
+ ; (<tb-machine-graph>) tells what the person did, by the ids of the structure ("s1.4"); each handler makes the
7
+ ; edit, chooses what it made, and asks the drawing to let the person type its name there.
8
+
9
+ edit-next: none ; the id whose name the drawing opens for typing when it is drawn next
10
+
11
+ canvas-place: function ["[k id what] of an id of the drawing, or none" id] [
12
+ unless string? id [return none]
13
+ what: select [#"m" machine #"s" state #"w" way #"u" state #"v" state] first id
14
+ parts: split next id #"."
15
+ ; what is drawn inside a state from the fragment it uses is that state's: u1.3:2 is s1.3
16
+ if all [what 2 = length? parts find "uv" first id] [parts/2: first split parts/2 #":"]
17
+ all [what 2 = length? parts attempt [reduce [to integer! parts/1 to integer! parts/2 what]]]
18
+ ]
19
+
20
+ ; what is chosen, by the name of a state of the k-th machine, once the text is read again
21
+ pick-state-named: function [k [integer!] name] [
22
+ foreach n select pick ms k 'nodes [if n/name = name [set 'picked reduce [k n/id 'state]]]
23
+ ]
24
+
25
+ ; The drawing sends, with an edit made where the person pointed, where every state of the machine is drawn
26
+ ; now, by id: [id x y ...]. They are written first, so that the states it put where there was room stay there;
27
+ ; the edit is made on that text, in one change for Undo.
28
+ keep-drawn: function [k [integer!] drawn] [
29
+ unless all [block? drawn not empty? drawn m: pick ms k] [return text]
30
+ out: copy []
31
+ foreach [id x y] drawn [
32
+ p: canvas-place id
33
+ if all [p p/1 = k p/3 = 'state p/2 > 1 machine-node m p/2 number? x number? y] [
34
+ append out reduce [state-key machine-path m p/2 as-pair to integer! round x to integer! round y]
35
+ ]
36
+ ]
37
+ set-machine-places text machine-key d m out
38
+ ]
39
+
40
+ ; the path a state named so has, in a state (or the machine, 1)
41
+ path-in: function [m [object!] at [integer!] name [word!]] [
42
+ append either at = 1 [copy []] [machine-path m at] name
43
+ ]
44
+
45
+ xy-of: function [v] [if all [block? v 2 = length? v number? v/1 number? v/2] [as-pair to integer! round v/1 to integer! round v/2]]
46
+
47
+ canvas-add: function ["detail: [kind parent] or [kind parent [x y] drawn], x y from the top-left corner of parent" detail [block!]] [
48
+ kind: to word! first detail
49
+ if kind = 'machine [machine-add 'machine exit]
50
+ unless all [p: canvas-place second detail pick ms p/1] [exit]
51
+ k: p/1
52
+ t: keep-drawn k pick detail 4
53
+ d0: design-of t
54
+ m: pick machines-of d0 k
55
+ at: p/2
56
+ if 'final = select machine-node m at 'kind [at: select machine-node m at 'parent]
57
+ name: fresh-state-name m select [state state final done parallel together] kind
58
+ unless t: add-state d0 m at name kind [exit]
59
+ if xy: xy-of pick detail 3 [t: put-place t machine-key d0 m path-in m at name xy]
60
+ edit t
61
+ pick-state-named k name
62
+ set 'edit-next tree-id picked
63
+ redraw
64
+ ]
65
+
66
+ canvas-connect: function [detail [block!]] [
67
+ machine-connect detail
68
+ if all [picked picked/3 = 'way] [
69
+ set 'edit-next tree-id picked
70
+ redraw
71
+ ]
72
+ ]
73
+
74
+ canvas-grow: function ["detail: from, or [from [x y] drawn], x y from the top-left corner of the box from is in" detail] [
75
+ unless block? detail [detail: reduce [detail]]
76
+ unless all [p: canvas-place first detail pick ms p/1] [exit]
77
+ k: p/1
78
+ t: keep-drawn k pick detail 3
79
+ d0: design-of t
80
+ m: pick machines-of d0 k
81
+ name: fresh-state-name m 'state
82
+ unless t: grow-state d0 m p/2 name 'go [
83
+ toast "A region of a parallel state has no ways of its own: draw one inside it"
84
+ exit
85
+ ]
86
+ if xy: xy-of pick detail 2 [t: put-place t machine-key d0 m path-in m select machine-node m p/2 'parent name xy]
87
+ edit t
88
+ pick-state-named k name
89
+ set 'edit-next tree-id picked
90
+ redraw
91
+ ]
92
+
93
+ canvas-rename: function [detail [block!]] [
94
+ unless all [p: canvas-place first detail m: pick ms p/1] [exit]
95
+ typed: second detail
96
+ t: either p/3 = 'way [
97
+ rename-way-events d m pick m/ways p/2 typed
98
+ ] [
99
+ v: attempt [load typed]
100
+ case [
101
+ not word? :v [toast "A state's name is one word, as checking or on-hold" none]
102
+ find state-names m v [toast rejoin ["A state is already called " v] none]
103
+ find machine-keywords v [toast rejoin [v " is a word of machine: call the state something else"] none]
104
+ true [rename-state d m p/2 v]
105
+ ]
106
+ ]
107
+ unless t [exit]
108
+ edit t
109
+ set 'picked p
110
+ redraw
111
+ ]
112
+
113
+ canvas-remove: function [id] [
114
+ unless all [p: canvas-place id m: pick ms p/1] [exit]
115
+ t: switch p/3 [
116
+ state [delete-state d m p/2]
117
+ way [delete-way d m pick m/ways p/2]
118
+ ]
119
+ unless t [exit]
120
+ edit t
121
+ set 'picked none
122
+ redraw
123
+ ]
124
+
125
+ canvas-initial: function [id] [
126
+ unless all [p: canvas-place id m: pick ms p/1] [exit]
127
+ unless t: set-initial d m p/2 [exit]
128
+ edit t
129
+ set 'picked p
130
+ redraw
131
+ ]
132
+
133
+ canvas-move: function ["detail: [id into] or [id into [x y] drawn], x y from the top-left corner of into" detail [block!]] [
134
+ unless all [p: canvas-place first detail pick ms p/1 into: canvas-place second detail] [exit]
135
+ k: p/1
136
+ t: keep-drawn k pick detail 4
137
+ d0: design-of t
138
+ m: pick machines-of d0 k
139
+ name: select machine-node m p/2 'name
140
+ unless into/2 = select machine-node m p/2 'parent [
141
+ unless t: move-state d0 m p/2 into/2 [
142
+ toast "It cannot go there"
143
+ exit
144
+ ]
145
+ ]
146
+ if xy: xy-of pick detail 3 [t: put-place t machine-key d0 m path-in m into/2 name xy]
147
+ edit t
148
+ pick-state-named k name
149
+ redraw
150
+ ]
151
+
152
+ ; a state dragged in its own box, or the whole machine tidied: where every state is drawn now
153
+ canvas-placed: function ["detail: [id x y ...]" drawn [block!]] [
154
+ unless all [not empty? drawn p: canvas-place first drawn] [exit]
155
+ t: keep-drawn p/1 drawn
156
+ if t <> text [edit t redraw]
157
+ ]