https://github.com/akkartik/mu/blob/main/shell/main.mu
  1 # Experimental Mu shell
  2 # A Lisp with indent-sensitivity and infix.
  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     # globals layout: 1 char padding, 41 code, 1 padding, 41 code, 1 padding =  85
 14     # sandbox layout: 1 padding, 41 code, 1 padding                          =  43
 15     #                                                                  total = 128 chars
 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       # ctrl-r
 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         # (main 0/real-screen 0/real-keyboard)
 31         # We're using the fact that 'screen' and 'keyboard' in this function are always 0.
 32         var real-keyboard: (handle cell)
 33         tmp <- address real-keyboard
 34         allocate-keyboard tmp
 35         # args = cons(real-keyboard, nil)
 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         #  args = cons(real-screen, args)
 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         # args = cons(main, args)
 51         tmp <- address args
 52         new-pair tmp, main, *tmp
 53         # clear real screen
 54         clear-screen screen
 55         set-cursor-position screen, 0, 0
 56         # run
 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       # no way to quit right now; just reboot
 69       edit-sandbox sandbox, key, globals, data-disk, screen, 1/tweak-real-screen
 70     }
 71     loop
 72   }
 73 }
 74 
 75 # Gotcha: some saved state may not load.
 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   # data-disk -> stream
 82   var s-storage: (stream byte 0x1000)  # space for 8/sectors
 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 #?   draw-stream-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, s, 7/fg, 0xc5/bg=blue-bg
 88   # stream -> gap-buffer
 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   # read: gap-buffer -> cell
 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   # load globals from assoc(initial-root, 'globals)
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   # sandbox = assoc(initial-root, 'sandbox)
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     # print: cell -> stream
137     print-cell sandbox-cell-ah, s, 0/no-trace
138     # stream -> gap-buffer
139     load-gap-buffer-from-stream data, s
140   }
141 }
142 
143 # Save state as an alist of alists:
144 #   ((globals . ((a . (fn ...))
145 #                ...))
146 #    (sandbox . ...))
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)  # space enough for 8/sectors
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 }