7761 - baremetal/shell: read input from keyboard

This commit is contained in:
Kartik K. Agaram 2021-02-20 22:37:44 -08:00
parent c7c8eb0cb4
commit 684c096203
3 changed files with 72 additions and 8 deletions

View File

@ -6,17 +6,17 @@ type gap-buffer {
right: grapheme-stack
}
fn initialize-gap-buffer _self: (addr gap-buffer) {
fn initialize-gap-buffer _self: (addr gap-buffer), max-word-size: int {
var self/esi: (addr gap-buffer) <- copy _self
var left/eax: (addr grapheme-stack) <- get self, left
initialize-grapheme-stack left, 0x10/max-word-size
initialize-grapheme-stack left, max-word-size
var right/eax: (addr grapheme-stack) <- get self, right
initialize-grapheme-stack right, 0x10/max-word-size
initialize-grapheme-stack right, max-word-size
}
# just for tests
fn initialize-gap-buffer-with self: (addr gap-buffer), s: (addr array byte) {
initialize-gap-buffer self
initialize-gap-buffer self, 0x10/max-word-size
var stream-storage: (stream byte 0x10/max-word-size)
var stream/ecx: (addr stream byte) <- address stream-storage
write stream, s
@ -280,7 +280,7 @@ fn gap-buffer-equal? _self: (addr gap-buffer), s: (addr array byte) -> _/eax: bo
fn test-gap-buffer-equal-from-end {
var _g: gap-buffer
var g/esi: (addr gap-buffer) <- address _g
initialize-gap-buffer g
initialize-gap-buffer g, 0x10
#
var c/eax: grapheme <- copy 0x61/a
add-grapheme-at-gap g, c
@ -294,7 +294,7 @@ fn test-gap-buffer-equal-from-end {
fn test-gap-buffer-equal-from-middle {
var _g: gap-buffer
var g/esi: (addr gap-buffer) <- address _g
initialize-gap-buffer g
initialize-gap-buffer g, 0x10
#
var c/eax: grapheme <- copy 0x61/a
add-grapheme-at-gap g, c
@ -309,7 +309,7 @@ fn test-gap-buffer-equal-from-middle {
fn test-gap-buffer-equal-from-start {
var _g: gap-buffer
var g/esi: (addr gap-buffer) <- address _g
initialize-gap-buffer g
initialize-gap-buffer g, 0x10
#
var c/eax: grapheme <- copy 0x61/a
add-grapheme-at-gap g, c
@ -327,7 +327,7 @@ fn test-gap-buffer-equal-fails {
# g = "aaa"
var _g: gap-buffer
var g/esi: (addr gap-buffer) <- address _g
initialize-gap-buffer g
initialize-gap-buffer g, 0x10
var c/eax: grapheme <- copy 0x61/a
add-grapheme-at-gap g, c
add-grapheme-at-gap g, c

17
baremetal/shell/main.mu Normal file
View File

@ -0,0 +1,17 @@
# Experimental Mu shell
# A Lisp with indent-sensitivity and infix, no macros. Commas are ignored.
fn main {
var sandbox-storage: sandbox
var sandbox/esi: (addr sandbox) <- address sandbox-storage
initialize-sandbox sandbox
{
render-sandbox 0/screen, sandbox, 2/x, 2/y
var key/eax: byte <- read-key 0/keyboard
compare key, 0
loop-if-=
# no way to quit right now; just reboot
edit-sandbox sandbox, key
loop
}
}

View File

@ -0,0 +1,47 @@
type sandbox {
data: (handle gap-buffer)
}
fn initialize-sandbox _self: (addr sandbox) {
var self/esi: (addr sandbox) <- copy _self
var data-ah/eax: (addr handle gap-buffer) <- get self, data
allocate data-ah
var data/eax: (addr gap-buffer) <- lookup *data-ah
initialize-gap-buffer data, 0x1000/4KB
}
## some helpers for tests
fn initialize-sandbox-with _self: (addr sandbox), s: (addr array byte) {
var self/esi: (addr sandbox) <- copy _self
var data-ah/eax: (addr handle gap-buffer) <- get self, data
allocate data-ah
var data/eax: (addr gap-buffer) <- lookup *data-ah
initialize-gap-buffer-with data, s
}
fn allocate-sandbox-with _out: (addr handle sandbox), s: (addr array byte) {
var out/eax: (addr handle sandbox) <- copy _out
allocate out
var out-addr/eax: (addr sandbox) <- lookup *out
initialize-sandbox-with out-addr, s
}
fn add-grapheme-to-sandbox _self: (addr sandbox), c: grapheme {
var self/esi: (addr sandbox) <- copy _self
var data-ah/eax: (addr handle gap-buffer) <- get self, data
var data/eax: (addr gap-buffer) <- lookup *data-ah
add-grapheme-at-gap data, c
}
fn render-sandbox screen: (addr screen), _self: (addr sandbox), x: int, y: int {
var self/esi: (addr sandbox) <- copy _self
var data-ah/eax: (addr handle gap-buffer) <- get self, data
var data/eax: (addr gap-buffer) <- lookup *data-ah
var dummy/eax: int <- render-gap-buffer screen, data, x, y, 1/true
}
fn edit-sandbox self: (addr sandbox), key: byte {
var g/edx: grapheme <- copy key
add-grapheme-to-sandbox self, g
}