https://github.com/akkartik/mu/blob/main/shell/global.mu
1 type global-table {
2 data: (handle array global)
3 final-index: int
4 render-list: (handle array int)
5
6 }
7
8 type global {
9 name: (handle array byte)
10 input: (handle gap-buffer)
11 value: (handle cell)
12 trace: (handle trace)
13 }
14
15 fn initialize-globals _self: (addr global-table) {
16 var self/esi: (addr global-table) <- copy _self
17 compare self, 0
18 {
19 break-if-!=
20 abort "initialize globals"
21 return
22 }
23 var data-ah/eax: (addr handle array global) <- get self, data
24 populate data-ah, 0x80
25 initialize-primitives self
26 var render-ah/eax: (addr handle array int) <- get self, render-list
27 populate render-ah, 0x20/render-size
28 }
29
30 fn load-globals in: (addr handle cell), self: (addr global-table) {
31 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "loading globals:", 3/fg, 0/bg
32 var remaining-ah/esi: (addr handle cell) <- copy in
33 {
34 var _remaining/eax: (addr cell) <- lookup *remaining-ah
35 var remaining/ebx: (addr cell) <- copy _remaining
36 var done?/eax: boolean <- nil? remaining
37 compare done?, 0/false
38 break-if-!=
39 var curr-ah/eax: (addr handle cell) <- get remaining, left
40 var _curr/eax: (addr cell) <- lookup *curr-ah
41 var curr/ecx: (addr cell) <- copy _curr
42 remaining-ah <- get remaining, right
43 var name-ah/eax: (addr handle cell) <- get curr, left
44 var name/eax: (addr cell) <- lookup *name-ah
45 var name-data-ah/eax: (addr handle stream byte) <- get name, text-data
46 var _name-data/eax: (addr stream byte) <- lookup *name-data-ah
47 var name-data/edx: (addr stream byte) <- copy _name-data
48 rewind-stream name-data
49 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " ", 3/fg 0/bg
50 draw-stream-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, name-data, 3/fg, 0/bg
51 var value-ah/eax: (addr handle cell) <- get curr, right
52 var value/eax: (addr cell) <- lookup *value-ah
53 var value-data-ah/eax: (addr handle stream byte) <- get value, text-data
54 var _value-data/eax: (addr stream byte) <- lookup *value-data-ah
55 var value-data/ecx: (addr stream byte) <- copy _value-data
56 var value-gap-buffer-storage: (handle gap-buffer)
57 var value-gap-buffer-ah/edi: (addr handle gap-buffer) <- address value-gap-buffer-storage
58 allocate value-gap-buffer-ah
59 var value-gap-buffer/eax: (addr gap-buffer) <- lookup *value-gap-buffer-ah
60 initialize-gap-buffer value-gap-buffer, 0x40000/256KB
61 load-gap-buffer-from-stream value-gap-buffer, value-data
62 load-lexical-scope value-gap-buffer-ah, self
63 loop
64 }
65 move-cursor-to-left-margin-of-next-line 0/screen
66 }
67
68 fn write-globals out: (addr stream byte), _self: (addr global-table) {
69 var self/esi: (addr global-table) <- copy _self
70 compare self, 0
71 {
72 break-if-!=
73 abort "write globals"
74 return
75 }
76 write out, " (globals . (\n"
77 var data-ah/eax: (addr handle array global) <- get self, data
78 var data/eax: (addr array global) <- lookup *data-ah
79 var final-index/edx: (addr int) <- get self, final-index
80 var curr-index/ecx: int <- copy 1/skip-0
81 {
82 compare curr-index, *final-index
83 break-if->
84 var curr-offset/ebx: (offset global) <- compute-offset data, curr-index
85 var curr/ebx: (addr global) <- index data, curr-offset
86 var curr-value-ah/edx: (addr handle cell) <- get curr, value
87 var curr-value/eax: (addr cell) <- lookup *curr-value-ah
88 var curr-type/eax: (addr int) <- get curr-value, type
89 {
90 compare *curr-type, 4/primitive-function
91 break-if-=
92 compare *curr-type, 5/screen
93 break-if-=
94 compare *curr-type, 6/keyboard
95 break-if-=
96 write out, " ("
97 var curr-name-ah/eax: (addr handle array byte) <- get curr, name
98 var curr-name/eax: (addr array byte) <- lookup *curr-name-ah
99 write out, curr-name
100 write out, " . ["
101 var curr-input-ah/eax: (addr handle gap-buffer) <- get curr, input
102 var curr-input/eax: (addr gap-buffer) <- lookup *curr-input-ah
103 {
104 compare curr-input, 0
105 break-if-!=
106 abort "null gap buffer"
107 }
108 append-gap-buffer curr-input, out
109 write out, "])\n"
110 }
111 curr-index <- increment
112 loop
113 }
114 write out, " ))\n"
115 }
116
117
118 fn render-globals screen: (addr screen), _self: (addr global-table), show-cursor?: boolean {
119 clear-rect screen, 0/xmin, 0/ymin, 0x55/xmax, 0x2f/ymax=screen-height-without-menu, 0xdc/bg=green-bg
120 var self/esi: (addr global-table) <- copy _self
121 compare self, 0
122 {
123 break-if-!=
124 abort "render globals"
125 return
126 }
127 var data-ah/eax: (addr handle array global) <- get self, data
128 var _data/eax: (addr array global) <- lookup *data-ah
129 var data/ebx: (addr array global) <- copy _data
130 var curr-index/edx: int <- copy 0
131 var y1: int
132 copy-to y1, 1/padding-top
133 var y2: int
134 copy-to y2, 1/padding-top
135 $render-globals:loop: {
136 compare curr-index, 0x20/render-size
137 break-if->=
138 {
139 compare y1, 0x2f/ymax
140 break-if-<
141 compare y2, 0x2f/ymax
142 break-if-<
143 break $render-globals:loop
144 }
145 $render-globals:iter: {
146 var cursor-in-current-line?: boolean
147 {
148 compare show-cursor?, 0/false
149 break-if-=
150 compare curr-index, 0
151 break-if-!=
152 copy-to cursor-in-current-line?, 1/true
153 }
154 var render-list-ah/eax: (addr handle array int) <- get self, render-list
155 var render-list/eax: (addr array int) <- lookup *render-list-ah
156 var global-id-a/eax: (addr int) <- index render-list, curr-index
157 var global-id/eax: int <- copy *global-id-a
158 compare global-id, 0
159 break-if-=
160 var global-offset/edx: (offset global) <- compute-offset data, global-id
161 var curr/edx: (addr global) <- index data, global-offset
162 {
163 var render-image?/eax: boolean <- render-image? curr, cursor-in-current-line?
164 compare render-image?, 0/false
165 break-if-=
166 var y/ecx: int <- render-image-definition screen, curr, 0/x y1
167 y <- increment
168 copy-to y1, y
169 break $render-globals:iter
170 }
171 var curr-input-ah/eax: (addr handle gap-buffer) <- get curr, input
172 var _curr-input/eax: (addr gap-buffer) <- lookup *curr-input-ah
173 var curr-input/ebx: (addr gap-buffer) <- copy _curr-input
174 compare curr-input, 0
175 break-if-=
176 var curr-trace-ah/eax: (addr handle trace) <- get curr, trace
177 var _curr-trace/eax: (addr trace) <- lookup *curr-trace-ah
178 var curr-trace/edx: (addr trace) <- copy _curr-trace
179 $render-globals:render-global: {
180 var x/eax: int <- copy 0
181 var y/ecx: int <- copy y1
182 compare y, y2
183 {
184 break-if->=
185 x, y <- render-gap-buffer-wrapping-right-then-down screen, curr-input, 1/padding-left, y1, 0x2a/xmax, 0x2f/ymax, cursor-in-current-line?, 7/fg=definition, 0xc5/bg=blue-bg
186 y <- increment
187 y <- render-trace screen, curr-trace, 1/padding-left, y, 0x2a/xmax, 0x2f/ymax, 0/no-cursor
188 y <- increment
189 copy-to y1, y
190 break $render-globals:render-global
191 }
192 x, y <- render-gap-buffer-wrapping-right-then-down screen, curr-input, 0x2b/xmin, y2, 0x54/xmax, 0x2f/ymax, cursor-in-current-line?, 7/fg=definition, 0xc5/bg=blue-bg
193 y <- increment
194 y <- render-trace screen, curr-trace, 0x2b/xmin, y, 0x54/xmax, 0x2f/ymax, 0/no-cursor
195 y <- increment
196 copy-to y2, y
197 }
198 }
199 curr-index <- increment
200 loop
201 }
202
203 render-primitives screen, 1/xmin=padding-left, 0x55/xmax, 0x2f/ymax
204 }
205
206 fn render-globals-menu screen: (addr screen), _self: (addr global-table) {
207 var _width/eax: int <- copy 0
208 var height/ecx: int <- copy 0
209 _width, height <- screen-size screen
210 var width/edx: int <- copy _width
211 var y/ecx: int <- copy height
212 y <- decrement
213 var height/ebx: int <- copy y
214 height <- increment
215 clear-rect screen, 0/x, y, width, height, 0xc5/bg=blue-bg
216 set-cursor-position screen, 0/x, y
217 draw-text-rightward-from-cursor screen, " ^r ", width, 0/fg, 0x5c/bg=menu-highlight
218 draw-text-rightward-from-cursor screen, " run main ", width, 7/fg, 0xc5/bg=blue-bg
219 draw-text-rightward-from-cursor screen, " ^s ", width, 0/fg, 0x5c/bg=menu-highlight
220 draw-text-rightward-from-cursor screen, " run sandbox ", width, 7/fg, 0xc5/bg=blue-bg
221 draw-text-rightward-from-cursor screen, " ^g ", width, 0/fg, 0x5c/bg=menu-highlight
222 draw-text-rightward-from-cursor screen, " go to ", width, 7/fg, 0xc5/bg=blue-bg
223 draw-text-rightward-from-cursor screen, " ^a ", width, 0/fg, 0x5c/bg=menu-highlight
224 draw-text-rightward-from-cursor screen, " << ", width, 7/fg, 0xc5/bg=blue-bg
225 draw-text-rightward-from-cursor screen, " ^b ", width, 0/fg, 0x5c/bg=menu-highlight
226 draw-text-rightward-from-cursor screen, " <word ", width, 7/fg, 0xc5/bg=blue-bg
227 draw-text-rightward-from-cursor screen, " ^f ", width, 0/fg, 0x5c/bg=menu-highlight
228 draw-text-rightward-from-cursor screen, " word> ", width, 7/fg, 0xc5/bg=blue-bg
229 draw-text-rightward-from-cursor screen, " ^e ", width, 0/fg, 0x5c/bg=menu-highlight
230 draw-text-rightward-from-cursor screen, " >> ", width, 7/fg, 0xc5/bg=blue-bg
231 }
232
233 fn edit-globals _self: (addr global-table), key: code-point-utf8 {
234 var self/esi: (addr global-table) <- copy _self
235
236 {
237 compare key, 0x13/ctrl-s
238 break-if-!=
239
240 refresh-cursor-definition self
241 return
242 }
243 var cursor-index/ecx: int <- cursor-global self
244 compare cursor-index, 0
245 {
246 break-if-!=
247 return
248 }
249 var data-ah/eax: (addr handle array global) <- get self, data
250 var data/eax: (addr array global) <- lookup *data-ah
251 var cursor-offset/ecx: (offset global) <- compute-offset data, cursor-index
252 var curr-global/eax: (addr global) <- index data, cursor-offset
253 var curr-editor-ah/eax: (addr handle gap-buffer) <- get curr-global, input
254 var curr-editor/eax: (addr gap-buffer) <- lookup *curr-editor-ah
255 edit-gap-buffer curr-editor, key
256 }
257
258 fn create-empty-global _self: (addr global-table), name-stream: (addr stream byte), capacity: int {
259 var self/esi: (addr global-table) <- copy _self
260 var final-index-addr/ecx: (addr int) <- get self, final-index
261 increment *final-index-addr
262 var render-list-ah/eax: (addr handle array int) <- get self, render-list
263 var render-list/eax: (addr array int) <- lookup *render-list-ah
264 slide-down render-list, 0/start 0x1f/penultimate, 1/target
265 var curr-index/ecx: int <- copy *final-index-addr
266 var dest/eax: (addr int) <- index render-list, 0
267 copy-to *dest, curr-index
268 var data-ah/eax: (addr handle array global) <- get self, data
269 var data/eax: (addr array global) <- lookup *data-ah
270 var curr-offset/ecx: (offset global) <- compute-offset data, curr-index
271 var curr/esi: (addr global) <- index data, curr-offset
272 var curr-name-ah/eax: (addr handle array byte) <- get curr, name
273 stream-to-array name-stream, curr-name-ah
274 var curr-input-ah/eax: (addr handle gap-buffer) <- get curr, input
275 allocate curr-input-ah
276 var curr-input/eax: (addr gap-buffer) <- lookup *curr-input-ah
277 initialize-gap-buffer curr-input, capacity
278 var trace-ah/eax: (addr handle trace) <- get curr, trace
279 allocate trace-ah
280 var trace/eax: (addr trace) <- lookup *trace-ah
281 initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
282 }
283
284 fn refresh-cursor-definition _self: (addr global-table) {
285 var self/esi: (addr global-table) <- copy _self
286 var render-list-ah/eax: (addr handle array int) <- get self, render-list
287 var render-list/eax: (addr array int) <- lookup *render-list-ah
288 var cursor-index/edx: (addr int) <- index render-list, 0
289 refresh-definition self, *cursor-index
290 }
291
292 fn refresh-definition _self: (addr global-table), _index: int {
293 var self/esi: (addr global-table) <- copy _self
294 var data-ah/eax: (addr handle array global) <- get self, data
295 var data/eax: (addr array global) <- lookup *data-ah
296 var index/ebx: int <- copy _index
297 var offset/ebx: (offset global) <- compute-offset data, index
298 var curr-global/ebx: (addr global) <- index data, offset
299 var curr-input-ah/edx: (addr handle gap-buffer) <- get curr-global, input
300 var curr-trace-ah/eax: (addr handle trace) <- get curr-global, trace
301 var curr-trace/eax: (addr trace) <- lookup *curr-trace-ah
302 clear-trace curr-trace
303 var curr-value-ah/edi: (addr handle cell) <- get curr-global, value
304 var definitions-created-storage: (stream int 0x10)
305 var definitions-created/ecx: (addr stream int) <- address definitions-created-storage
306 read-and-evaluate-and-save-gap-buffer-to-globals curr-input-ah, curr-value-ah, self, definitions-created, curr-trace, 0/no-screen, 0/no-keyboard
307 }
308
309 fn assign-or-create-global _self: (addr global-table), name: (addr array byte), value: (handle cell), index-updated: (addr int), trace: (addr trace) {
310 var self/esi: (addr global-table) <- copy _self
311 compare self, 0
312 {
313 break-if-!=
314 abort "assign global"
315 }
316 var curr-index/ecx: int <- find-symbol-name-in-globals self, name
317 {
318 compare curr-index, -1/not-found
319 break-if-!=
320 var final-index-addr/eax: (addr int) <- get self, final-index
321 increment *final-index-addr
322 curr-index <- copy *final-index-addr
323 var arr-ah/eax: (addr handle array int) <- get self, render-list
324 var arr/eax: (addr array int) <- lookup *arr-ah
325 slide-down arr, 0/start 0x1e/penultimate, 1/target
326 var dest/eax: (addr int) <- index arr, 0
327 copy-to *dest, curr-index
328 }
329 var data-ah/eax: (addr handle array global) <- get self, data
330 var data/eax: (addr array global) <- lookup *data-ah
331 var curr-offset/esi: (offset global) <- compute-offset data, curr-index
332 var curr/esi: (addr global) <- index data, curr-offset
333 var curr-name-ah/eax: (addr handle array byte) <- get curr, name
334 copy-array-object name, curr-name-ah
335 var curr-value-ah/eax: (addr handle cell) <- get curr, value
336 copy-handle value, curr-value-ah
337 var index-updated/edi: (addr int) <- copy index-updated
338 copy-to *index-updated, curr-index
339 var trace-ah/eax: (addr handle trace) <- get curr, trace
340 allocate trace-ah
341 var trace/eax: (addr trace) <- lookup *trace-ah
342 initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
343 }
344
345 fn bump-global _globals: (addr global-table), global-id: int {
346 var globals/esi: (addr global-table) <- copy _globals
347 var render-list-ah/eax: (addr handle array int) <- get globals, render-list
348 var render-list/eax: (addr array int) <- lookup *render-list-ah
349 var idx/ecx: int <- find-slide-down-slot-in-array render-list, global-id
350 {
351 compare idx, 0
352 break-if-!=
353 return
354 }
355 slide-down render-list, 0/start idx, 1/target
356 var dest/eax: (addr int) <- index render-list, 0
357 var val/ecx: int <- copy global-id
358 copy-to *dest, val
359 }
360
361 fn cursor-global _globals: (addr global-table) -> _/ecx: int {
362 var globals/esi: (addr global-table) <- copy _globals
363 var render-list-ah/eax: (addr handle array int) <- get globals, render-list
364 var render-list/eax: (addr array int) <- lookup *render-list-ah
365 var dest/eax: (addr int) <- index render-list, 0
366 return *dest
367 }
368
369 fn lookup-symbol-in-globals _sym: (addr cell), out: (addr handle cell), _globals: (addr global-table), trace: (addr trace), inner-screen-var: (addr handle cell), inner-keyboard-var: (addr handle cell) {
370 var sym/eax: (addr cell) <- copy _sym
371 var sym-name-ah/eax: (addr handle stream byte) <- get sym, text-data