https://github.com/akkartik/mu/blob/main/shell/main.mu
1
2
3
4 fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
5 var globals-storage: global-table
6 var globals/edi: (addr global-table) <- address globals-storage
7 initialize-globals globals
8 var sandbox-storage: sandbox
9 var sandbox/esi: (addr sandbox) <- address sandbox-storage
10 initialize-sandbox sandbox, 1/with-screen
11 load-state data-disk, sandbox, globals
12 $main:loop: {
13
14
15
16 render-globals screen, globals
17 render-sandbox screen, sandbox, 0x55/sandbox-left-margin, 0/sandbox-top-margin, 0x80/screen-width, 0x2f/screen-height-without-menu
18 {
19 var key/eax: byte <- read-key keyboard
20 compare key, 0
21 loop-if-=
22
23 {
24 compare key, 0x12/ctrl-r
25 break-if-!=
26 var tmp/eax: (addr handle cell) <- copy 0
27 var nil: (handle cell)
28 tmp <- address nil
29 allocate-pair tmp
30
31
32 var real-keyboard: (handle cell)
33 tmp <- address real-keyboard
34 allocate-keyboard tmp
35
36 var args: (handle cell)
37 tmp <- address args
38 new-pair tmp, real-keyboard, nil
39
40 var real-screen: (handle cell)
41 tmp <- address real-screen
42 allocate-screen tmp
43
44 tmp <- address args
45 new-pair tmp, real-screen, *tmp
46
47 var main: (handle cell)
48 tmp <- address main
49 new-symbol tmp, "main"
50
51 tmp <- address args
52 new-pair tmp, main, *tmp
53
54 clear-screen screen
55 set-cursor-position screen, 0, 0
56
57 var out: (handle cell)
58 var out-ah/ecx: (addr handle cell) <- address out
59 evaluate tmp, out-ah, nil, globals, 0/trace, 0/no-fake-screen, 0/no-fake-keyboard, 0/call-number
60 {
61 var tmp/eax: byte <- read-key keyboard
62 compare tmp, 0
63 loop-if-=
64 }
65
66 loop $main:loop
67 }
68
69 edit-sandbox sandbox, key, globals, data-disk, screen, 1/tweak-real-screen
70 }
71 loop
72 }
73 }
74
75
76 fn load-state data-disk: (addr disk), _sandbox: (addr sandbox), globals: (addr global-table) {
77 var sandbox/eax: (addr sandbox) <- copy _sandbox
78 var data-ah/eax: (addr handle gap-buffer) <- get sandbox, data
79 var _data/eax: (addr gap-buffer) <- lookup *data-ah
80 var data/esi: (addr gap-buffer) <- copy _data
81
82 var s-storage: (stream byte 0x1000)
83 var s/ebx: (addr stream byte) <- address s-storage
84 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "loading sectors from data disk", 3/fg, 0/bg
85 move-cursor-to-left-margin-of-next-line 0/screen
86 load-sectors data-disk, 0/lba, 8/sectors, s
87
88
89 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "parsing", 3/fg, 0/bg
90 move-cursor-to-left-margin-of-next-line 0/screen
91 load-gap-buffer-from-stream data, s
92 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " into gap buffer", 3/fg, 0/bg
93 move-cursor-to-left-margin-of-next-line 0/screen
94 clear-stream s
95
96 var initial-root-storage: (handle cell)
97 var initial-root/ecx: (addr handle cell) <- address initial-root-storage
98 read-cell data, initial-root, 0/no-trace
99 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " into s-expressions", 3/fg, 0/bg
100 move-cursor-to-left-margin-of-next-line 0/screen
101 clear-gap-buffer data
102
103 {
104 var initial-root-addr/eax: (addr cell) <- lookup *initial-root
105 compare initial-root-addr, 0
106 break-if-!=
107 return
108 }
109
110 var globals-literal-storage: (handle cell)
111 var globals-literal-ah/eax: (addr handle cell) <- address globals-literal-storage
112 new-symbol globals-literal-ah, "globals"
113 var globals-literal/eax: (addr cell) <- lookup *globals-literal-ah
114 var globals-cell-storage: (handle cell)
115 var globals-cell-ah/edx: (addr handle cell) <- address globals-cell-storage
116 lookup-symbol globals-literal, globals-cell-ah, *initial-root, 0/no-globals, 0/no-trace, 0/no-screen, 0/no-keyboard
117 var globals-cell/eax: (addr cell) <- lookup *globals-cell-ah
118 {
119 compare globals-cell, 0
120 break-if-=
121 load-globals globals-cell-ah, globals
122 }
123
124 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "loading sandbox", 3/fg, 0/bg
125 var sandbox-literal-storage: (handle cell)
126 var sandbox-literal-ah/eax: (addr handle cell) <- address sandbox-literal-storage
127 new-symbol sandbox-literal-ah, "sandbox"
128 var sandbox-literal/eax: (addr cell) <- lookup *sandbox-literal-ah
129 var sandbox-cell-storage: (handle cell)
130 var sandbox-cell-ah/edx: (addr handle cell) <- address sandbox-cell-storage
131 lookup-symbol sandbox-literal, sandbox-cell-ah, *initial-root, 0/no-globals, 0/no-trace, 0/no-screen, 0/no-keyboard
132 var sandbox-cell/eax: (addr cell) <- lookup *sandbox-cell-ah
133 {
134 compare sandbox-cell, 0
135 break-if-=
136
137 print-cell sandbox-cell-ah, s, 0/no-trace
138
139 load-gap-buffer-from-stream data, s
140 }
141 }
142
143
144
145
146
147 fn store-state data-disk: (addr disk), sandbox: (addr sandbox), globals: (addr global-table) {
148 compare data-disk, 0/no-disk
149 {
150 break-if-!=
151 return
152 }
153 var stream-storage: (stream byte 0x1000)
154 var stream/edi: (addr stream byte) <- address stream-storage
155 write stream, "(\n"
156 write-globals stream, globals
157 write-sandbox stream, sandbox
158 write stream, ")\n"
159 store-sectors data-disk, 0/lba, 8/sectors, stream
160 }