From db0363462f42b05561853bd295fb4d39909a9f6c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 13 Oct 2021 23:14:20 -0700 Subject: [PATCH] primitive: read line from keyboard Blocking. --- 516read-line.mu | 20 ++++++++++++++++++++ apps/rpn.mu | 20 +------------------- 2 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 516read-line.mu diff --git a/516read-line.mu b/516read-line.mu new file mode 100644 index 00000000..1bb79c5a --- /dev/null +++ b/516read-line.mu @@ -0,0 +1,20 @@ +# read line from keyboard into stream while also echoing to screen +# abort on stream overflow +fn read-line-from-keyboard keyboard: (addr keyboard), out: (addr stream byte), screen: (addr screen), fg: int, bg: int { + clear-stream out + { + draw-cursor screen, 0x20/space + var key/eax: byte <- read-key keyboard + compare key, 0xa/newline + break-if-= + compare key, 0 + loop-if-= + var key2/eax: int <- copy key + append-byte out, key2 + var c/eax: code-point <- copy key2 + draw-code-point-at-cursor-over-full-screen screen, c, fg bg + loop + } + # clear cursor + draw-code-point-at-cursor-over-full-screen screen, 0x20/space, fg bg +} diff --git a/apps/rpn.mu b/apps/rpn.mu index 4fd8ccb6..7d98d549 100644 --- a/apps/rpn.mu +++ b/apps/rpn.mu @@ -19,28 +19,10 @@ fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) var in-storage: (stream byte 0x80) var in/esi: (addr stream byte) <- address in-storage var y/ecx: int <- copy 0 - var space/edx: code-point <- copy 0x20 # read-eval-print loop { - # print prompt var x/eax: int <- draw-text-rightward screen, "> ", 0/x, 0x80/xmax, y, 3/fg/cyan, 0/bg - # read line from keyboard - clear-stream in - { - draw-cursor screen, space - var key/eax: byte <- read-key keyboard - compare key, 0xa/newline - break-if-= - compare key, 0 - loop-if-= - var key2/eax: int <- copy key - append-byte in, key2 - var c/eax: code-point <- copy key2 - draw-code-point-at-cursor-over-full-screen screen, c, 0xf/fg, 0/bg - loop - } - # clear cursor - draw-code-point-at-cursor-over-full-screen screen, space, 3/fg/never-used, 0/bg + read-line-from-keyboard keyboard, in, screen, 0xf/fg 0/bg # parse and eval var out/eax: int <- simplify in # print