From 21387ef514e1fa91f8f10f0a5089cc4bc66f3c18 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 5 Oct 2020 21:09:42 -0700 Subject: 6963 - tile: more idiomatic conventional repl --- 305keyboard.subx | 2 +- 400.mu | 2 +- apps/tile/main.mu | 72 +++++++++++++++++++++++++++---------------------------- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/305keyboard.subx b/305keyboard.subx index 01dc9783..079814fa 100644 --- a/305keyboard.subx +++ b/305keyboard.subx @@ -155,7 +155,7 @@ $read-key-from-real-keyboard:end: c3/return # use this in type mode -read-line-from-real-keyboard: # in: (addr stream byte) +read-line-from-real-keyboard: # out: (addr stream byte) # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp diff --git a/400.mu b/400.mu index 9f407565..93e6f03a 100644 --- a/400.mu +++ b/400.mu @@ -168,7 +168,7 @@ sig show-cursor-on-real-screen sig enable-keyboard-immediate-mode sig enable-keyboard-type-mode sig read-key-from-real-keyboard -> result/eax: grapheme -sig read-line-from-real-keyboard in: (addr stream byte) +sig read-line-from-real-keyboard out: (addr stream byte) sig open filename: (addr array byte), write?: boolean, out: (addr handle buffered-file) sig populate-buffered-file-containing contents: (addr array byte), out: (addr handle buffered-file) sig new-buffered-file out: (addr handle buffered-file) diff --git a/apps/tile/main.mu b/apps/tile/main.mu index ad692f63..c4a5f6c0 100644 --- a/apps/tile/main.mu +++ b/apps/tile/main.mu @@ -63,46 +63,44 @@ fn interactive { } fn repl { - enable-keyboard-immediate-mode - var env-storage: environment - var env/esi: (addr environment) <- address env-storage - initialize-environment env - var stack-storage: value-stack - var stack/edi: (addr value-stack) <- address stack-storage - initialize-value-stack stack, 0x10 - print-string-to-real-screen "> " - $repl:loop: { - var key/eax: grapheme <- read-key-from-real-keyboard - print-grapheme-to-real-screen key - compare key, 4 # ctrl-d - break-if-= - compare key, 0xa # newline + { + # prompt + var line-storage: (stream byte 0x100) + var line/ecx: (addr stream byte) <- address line-storage + print-string-to-real-screen "> " + # read + clear-stream line + read-line-from-real-keyboard line + var done?/eax: boolean <- stream-empty? line + compare done?, 0 # false + break-if-!= + # parse + var env-storage: environment + var env/esi: (addr environment) <- address env-storage + initialize-environment env { + var done?/eax: boolean <- stream-empty? line + compare done?, 0 # false break-if-!= - evaluate-environment env, stack - var empty?/eax: boolean <- value-stack-empty? stack - { - compare empty?, 0 # false - break-if-!= - var result/eax: int <- pop-int-from-value-stack stack - print-int32-decimal-to-real-screen result - print-string-to-real-screen "\n" - } - # clear line - var program-ah/eax: (addr handle program) <- get env, program - var program/eax: (addr program) <- lookup *program-ah - var sandbox-ah/eax: (addr handle sandbox) <- get program, sandboxes - var _sandbox/eax: (addr sandbox) <- lookup *sandbox-ah - var sandbox/esi: (addr sandbox) <- copy _sandbox - var line-ah/eax: (addr handle line) <- get sandbox, data - var line/eax: (addr line) <- lookup *line-ah - var cursor-word-ah/esi: (addr handle word) <- get sandbox, cursor-word - initialize-line line, cursor-word-ah - print-string-to-real-screen "> " - loop $repl:loop + var g/eax: grapheme <- read-grapheme line + process env, g + loop } - process env, key + # eval + var stack-storage: value-stack + var stack/edi: (addr value-stack) <- address stack-storage + initialize-value-stack stack, 0x10 + evaluate-environment env, stack + # print + var empty?/eax: boolean <- value-stack-empty? stack + { + compare empty?, 0 # false + break-if-!= + var result/eax: int <- pop-int-from-value-stack stack + print-int32-decimal-to-real-screen result + print-string-to-real-screen "\n" + } + # loop } - enable-keyboard-type-mode } -- cgit 1.4.1-2-gfad0