(ero "initializing mu.. (takes ~5s)") ;; profiler (http://arclanguage.org/item?id=11556) ; Keeping this right on top as a reminder to profile before guessing at why my ; program is slow. (mac proc (name params . body) `(def ,name ,params ,@body nil)) (mac filter-log (msg f x) `(ret x@ ,x (prn ,msg (,f x@)))) (= times* (table)) (mac deftimed (name args . body) `(do (def ,(sym (string name "_core")) ,args ,@body) (def ,name ,args (let t0 (msec) (ret ans ,(cons (sym (string name "_core")) args) (update-time ,(string name) t0)))))) (proc update-time(name t0) ; call directly in recursive functions (or= times*.name (list 0 0)) (with ((a b) times*.name timing (- (msec) t0)) (= times*.name (list (+ a timing) (+ b 1))))) (def print-times() (prn (current-process-milliseconds)) (prn "gc " (current-gc-milliseconds)) (each (name time) (tablist times*) (prn name " " time))) ;; what happens when our virtual machine starts up (= initialization-fns* (queue)) (def reset () (each f (as cons initialization-fns*) (f))) (mac on-init body `(enq (fn () ,@body) initialization-fns*)) ;; persisting and checking traces for each test (= traces* (queue)) (= trace-dir* ".traces/") (ensure-dir trace-dir*) (= curr-trace-file* nil) (on-init (awhen curr-trace-file* (tofile (+ trace-dir* it) (each (label trace) (as cons traces*) (pr label ": " trace)))) (= curr-trace-file* nil) (= traces* (queue))) (def new-trace (filename) (prn "== @filename") ;? ) (= curr-trace-file* filename)) (= dump-trace* nil) (def trace (label . args) (when (or (is dump-trace* t) (and dump-trace* (is label "-")) (and dump-trace* (pos label dump-trace*!whitelist)) (and dump-trace* (no dump-trace*!whitelist) (~pos label dump-trace*!blacklist))) (apply prn label ": " args)) (enq (list label (apply tostring:prn args)) traces*) (car args)) (on-init (wipe dump-trace*)) (redef tr args ; why am I still returning to prn when debugging? Will this help? (do1 nil (apply trace "-" args))) (def tr2 (msg arg) (tr msg arg) arg) (def check-trace-contents (msg expected-contents) (unless (trace-contents-match expected-contents) (prn "F - " msg) (prn " trace contents") (print-trace-contents-mismatch expected-contents))) (def trace-contents-match (expected-contents) (each (label msg) (as cons traces*) (when (and expected-contents (is label expected-contents.0.0) (posmatch expected-contents.0.1 msg)) (pop expected-contents))) (no expected-contents)) (def print-trace-contents-mismatch (expected-contents) (each (label msg) (as cons traces*) (whenlet (expected-label expected-msg) expected-contents.0 (if (and (is label expected-label) (posmatch expected-msg msg)) (do (pr " * ") (pop expected-contents)) (pr " ")) (pr label ": " msg))) (prn " couldn't find") (each (expected-label expected-msg) expected-contents (prn " ! " expected-label ": " expected-msg))) (def check-trace-doesnt-contain (msg (label unexpected-contents)) (when (some (fn ((l s)) (and (is l label) (posmatch unexpected-contents msg))) (as cons traces*)) (prn "F - " msg) (prn " trace contents") (each (l msg) (as cons traces*) (if (and (is l label) (posmatch unexpected-contents msg)) (pr " X ") (pr " ")) (pr label ": " msg)))) ;; virtual machine state ; things that a future assembler will need separate memory for: ; code; types; args channel ; at compile time: mapping names to locations (on-init (= type* (table)) ; name -> type info (= memory* (table)) ; address -> value (make this a vector?) (= function* (table)) ; name -> [instructions] ; transforming mu programs (= location* (table)) ; function -> {name -> index into default-space} (= next-space-generator* (table)) ; function -> name of function generating next space ; each function's next space will usually always come from a single function (= next-routine-id* 0) (= continuation* (table)) ) (on-init (= type* (obj ; Each type must be scalar or array, sum or product or primitive type (obj size 1) ; implicitly scalar and primitive type-address (obj size 1 address t elem '(type)) type-array (obj array t elem '(type)) type-array-address (obj size 1 address t elem '(type-array)) location (obj size 1 address t elem '(location)) ; assume it points to an atom integer (obj size 1) boolean (obj size 1) boolean-address (obj size 1 address t elem '(boolean)) byte (obj size 1) byte-address (obj size 1 address t elem '(byte)) string (obj array t elem '(byte)) ; inspired by Go ; an address contains the location of a specific type string-address (obj size 1 address t elem '(string)) string-address-address (obj size 1 address t elem '(string-address)) string-address-array (obj array t elem '(string-address)) string-address-array-address (obj size 1 address t elem '(string-address-array)) string-address-array-address-address (obj size 1 address t elem '(string-address-array-address)) ; 'character' will be of larger size when mu supports unicode ; we're currently undisciplined about mixing 'byte' and 'character' ; realistic test of indiscipline in general character (obj size 1) ; int32 like a Go rune character-address (obj size 1 address t elem '(character)) ; a buffer makes it easy to append to a string/array ; todo: make this generic ; data isn't a 'real' array: its length is stored outside it, ; so for example, 'print-string' won't work on it. buffer (obj size 2 and-record t elems '((integer) (string-address)) fields '(length data)) buffer-address (obj size 1 address t elem '(buffer)) ; a stream makes it easy to read from a string/array stream (obj size 2 and-record t elems '((integer) (string-address)) fields '(pointer data)) stream-address (obj size 1 address t elem '(stream)) ; isolating function calls space (obj array t elem '(location)) ; by convention index 0 points to outer space space-address (obj size 1 address t elem '(space)) ; arrays consist of an integer length followed by that many ; elements, all of the same type integer-array (obj array t elem '(integer)) integer-array-address (obj size 1 address t elem '(integer-array)) integer-array-address-address (obj size 1 address t elem '(integer-array-address)) integer-address (obj size 1 address t elem '(integer)) ; pointer to int integer-address-address (obj size 1 address t elem '(integer-address)) ; and-records consist of a multiple fields of different types integer-boolean-pair (obj size 2 and-record t elems '((integer) (boolean)) fields '(int bool)) integer-boolean-pair-address (obj size 1 address t elem '(integer-boolean-pair)) integer-boolean-pair-array (obj array t elem '(integer-boolean-pair)) integer-boolean-pair-array-address (obj size 1 address t elem '(integer-boolean-pair-array)) integer-integer-pair (obj size 2 and-record t elems '((integer) (integer))) integer-integer-pair-address (obj size 1 address t elem '(integer-integer-pair)) integer-point-pair (obj size 2 and-record t elems '((integer) (integer-integer-pair))) integer-point-pair-address (obj size 1 address t elem '(integer-point-pair)) integer-point-pair-address-address (obj size 1 address t elem '(integer-point-pair-address)) ; tagged-values are the foundation of dynamic types tagged-value (obj size 2 and-record t elems '((type) (location)) fields '(type payload)) tagged-value-address (obj size 1 address t elem '(tagged-value)) tagged-value-array (obj array t elem '(tagged-value)) tagged-value-array-address (obj size 1 address t elem '(tagged-value-array)) tagged-value-array-address-address (obj size 1 address t elem '(tagged-value-array-address)) ; heterogeneous lists list (obj size 2 and-record t elems '((tagged-value) (list-address)) fields '(car cdr)) list-address (obj size 1 address t elem '(list)) list-address-address (obj size 1 address t elem '(list-address)) ; parallel routines use channels to synchronize channel (obj size 3 and-record t elems '((integer) (integer) (tagged-value-array-address)) fields '(first-full first-free circular-buffer)) ; be careful of accidental copies to channels channel-address (obj size 1 address t elem '(channel)) ; opaque pointer to a call stack ; todo: save properly in allocated memory continuation (obj size 1) ; editor line (obj array t elem '(character)) line-address (obj size 1 address t elem '(line)) line-address-address (obj size 1 address t elem '(line-address)) screen (obj array t elem '(line-address)) screen-address (obj size 1 address t elem '(screen)) ; fake screen terminal (obj size 5 and-record t elems '((integer) (integer) (integer) (integer) (string-address)) fields '(num-rows num-cols cursor-row cursor-col data)) terminal-address (obj size 1 address t elem '(terminal)) ; fake keyboard keyboard (obj size 2 and-record t elems '((integer) (string-address)) fields '(index data)) keyboard-address (obj size 1 address t elem '(keyboard)) ))) ;; managing concurrent routines (on-init ;? (prn "-- resetting memory allocation") (= Memory-allocated-until 1000) (= Allocation-chunk 100000)) ; routine = runtime state for a serial thread of execution (def make-routine (fn-name . args) (let curr-alloc Memory-allocated-until ;? (prn "-- allocating routine: @curr-alloc") (++ Memory-allocated-until Allocation-chunk) (annotate 'routine (obj alloc curr-alloc alloc-max Memory-allocated-until call-stack (list (obj fn-name fn-name pc 0 args args caller-arg-idx 0)))) ; other fields we use in routine: ; sleep: conditions ; limit: number of cycles this routine can use ; running-since: start of the clock for counting cycles this routine has used ; todo: do memory management in mu )) (defextend empty (x) (isa x 'routine) (no rep.x!call-stack)) (def stack (routine) ((rep routine) 'call-stack)) (def push-stack (routine op) (push (obj fn-name op pc 0 caller-arg-idx 0 t0 (msec)) rep.routine!call-stack)) (def pop-stack (routine) ;? (update-time label.routine (msec)) ;? 1 (pop rep.routine!call-stack)) (def top (routine) stack.routine.0) (def label (routine) (whenlet stack stack.routine (or= stack.0!label (label2 stack)))) (def label2 (stack) (string:intersperse "/" (map [_ 'fn-name] stack)));)) (def body (routine) (function* stack.routine.0!fn-name)) (mac pc (routine (o idx 0)) ; assignable `((((rep ,routine) 'call-stack) ,idx) 'pc)) (mac caller-arg-idx (routine (o idx 0)) ; assignable `((((rep ,routine) 'call-stack) ,idx) 'caller-arg-idx)) (mac caller-args (routine) ; assignable `((((rep ,routine) 'call-stack) 0) 'args)) (mac caller-operands (routine) ; assignable `((((rep ,routine) 'call-stack) 0) 'caller-operands)) (mac caller-results (routine) ; assignable `((((rep ,routine) 'call-stack) 0) 'caller-results)) (mac results (routine) ; assignable `((((rep ,routine) 'call-stack) 0) 'results)) (mac reply-args (routine) ; assignable `((((rep ,routine) 'call-stack) 0) 'reply-args)) (def waiting-for-exact-cycle? (routine) (is 'until rep.routine!sleep.0)) (def ready-to-wake-up (routine) (assert no.routine*) (case rep.routine!sleep.0 until (> curr-cycle* rep.routine!sleep.1) until-location-changes (~is rep.routine!sleep.2 (memory* rep.routine!sleep.1)) until-routine-done (find [and _ (is rep._!id rep.routine!sleep.1)] completed-routines*) )) (on-init (= running-routines* (queue)) ; simple round-robin scheduler ; set of sleeping routines; don't modify routines while they're in this table (= sleeping-routines* (table)) (= completed-routines* nil) ; audit trail (= routine* nil) (= abort-routine* (parameter nil)) (= curr-cycle* 0) (= scheduling-interval* 500) (= scheduler-switch-table* nil) ; hook into scheduler for debugging ) ; like arc's 'point' but you can also call ((abort-routine*)) in nested calls (mac routine-mark body (w/uniq (g p) `(ccc (fn (,g) (parameterize abort-routine* (fn ((o ,p)) (,g ,p)) ,@body))))) (def run fn-names (freeze function*) ;? (prn function*!main) ;? 1 (load-system-functions) (apply run-more fn-names)) ; assume we've already frozen; throw on a few more routines and continue scheduling (def run-more fn-names (each it fn-names (enq make-routine.it running-routines*)) (while (~empty running-routines*) (= routine* deq.running-routines*) (when rep.routine*!limit ; start the clock if it wasn't already running (or= rep.routine*!running-since curr-cycle*)) (trace "schedule" label.routine*) (routine-mark (run-for-time-slice scheduling-interval*)) (update-scheduler-state))) ; prepare next iteration of round-robin scheduler ; ; state before: routine* running-routines* sleeping-routines* ; state after: running-routines* (with next routine to run at head) sleeping-routines* ; ; responsibilities: ; add routine* to either running-routines* or sleeping-routines* or completed-routines* ; wake up any necessary sleeping routines (which might be waiting for a ; particular time or for a particular memory location to change) ; detect termination: all non-helper routines completed ; detect deadlock: kill all sleeping routines when none can be woken (def update-scheduler-state () (when routine* ;? (prn "update scheduler state: " routine*) (if rep.routine*!sleep (do (trace "schedule" "pushing " label.routine* " to sleep queue") ; keep the clock ticking at rep.routine*!running-since (set sleeping-routines*.routine*)) rep.routine*!error (do (trace "schedule" "done with dead routine " label.routine*) ;? (tr rep.routine*) (push routine* completed-routines*)) empty.routine* (do (trace "schedule" "done with routine " label.routine*) (push routine* completed-routines*)) (no rep.routine*!limit) (do (trace "schedule" "scheduling " label.routine* " for further processing") (enq routine* running-routines*)) (> rep.routine*!limit 0) (do (trace "schedule" "scheduling " label.routine* " for further processing (limit)") ; stop the clock and debit the time on it from the routine (-- rep.routine*!limit (- curr-cycle* rep.routine*!running-since)) (wipe rep.routine*!running-since) (if (<= rep.routine*!limit 0) (do (trace "schedule" "routine ran out of time") (push routine* completed-routines*)) (enq routine* running-routines*))) :else (err "illegal scheduler state")) (= routine* nil)) (each (routine _) routine-canon.sleeping-routines* (when (aand rep.routine!limit (<= it (- curr-cycle* rep.routine!running-since))) (trace "schedule" "routine timed out") (wipe sleeping-routines*.routine) (push routine completed-routines*) ;? (tr completed-routines*) )) (each (routine _) routine-canon.sleeping-routines* (when (ready-to-wake-up routine) (trace "schedule" "waking up " label.routine) (wipe sleeping-routines*.routine) ; do this before modifying routine (wipe rep.routine!sleep) (++ pc.routine) (enq routine running-routines*))) ; optimization for simulated time (when (empty running-routines*) (whenlet exact-sleeping-routines (keep waiting-for-exact-cycle? keys.sleeping-routines*) (let next-wakeup-cycle (apply min (map [rep._!sleep 1] exact-sleeping-routines)) (= curr-cycle* (+ 1 next-wakeup-cycle))) (trace "schedule" "skipping to cycle " curr-cycle*) (update-scheduler-state))) (when (and (or (~empty running-routines*) (~empty sleeping-routines*)) (all [rep._ 'helper] (as cons running-routines*)) (all [rep._ 'helper] keys.sleeping-routines*)) (trace "schedule" "just helpers left; stopping everything") (until (empty running-routines*) (push (deq running-routines*) completed-routines*)) (each (routine _) sleeping-routines* ;? (prn " " label.routine) ;? 0 (wipe sleeping-routines*.routine) (push routine completed-routines*))) (detect-deadlock) ) (def detect-deadlock () (when (and (empty running-routines*) (~empty sleeping-routines*) (~some 'literal (map (fn(_) rep._!sleep.1) keys.sleeping-routines*))) (each (routine _) sleeping-routines* (wipe sleeping-routines*.routine) (= rep.routine!error "deadlock detected") (push routine completed-routines*)))) (def die (msg) (tr "die: " msg) (= rep.routine*!error msg) (iflet abort-continuation (abort-routine*) (abort-continuation))) ;; running a single routine ; value of an arg or oarg, stripping away all metadata ; wish I could have this flag an error when arg is incorrectly formed (mac v (operand) ; for value `((,operand 0) 0)) ; routines consist of instrs ; instrs consist of oargs, op and args (def parse-instr (instr) (iflet delim (pos '<- instr) (do (when (atom (instr (+ delim 1))) (err "operator not tokenized in @instr; maybe you need to freeze functions*?")) (list (cut instr 0 delim) ; oargs (v (instr (+ delim 1))) ; op (cut instr (+ delim 2)))) ; args (list nil (v car.instr) cdr.instr))) (def metadata (operand) cdr.operand) (def ty (operand) (cdr operand.0)) (def literal? (operand) (unless (acons ty.operand) (err "no type in operand @operand")) (in ty.operand.0 'literal 'offset 'fn)) (def typeinfo (operand) (or (type* ty.operand.0) (err "unknown type @(tostring prn.operand)"))) ; operand accessors (def nondummy (operand) ; precondition for helpers below (~is '_ operand)) ; just for convenience, 'new' instruction sometimes takes a raw string and ; allocates just enough space to store it (def not-raw-string (operand) (~isa operand 'string)) (def address? (operand) (or (is ty.operand.0 'location) typeinfo.operand!address)) ($:require "charterm/main.rkt") ($:require graphics/graphics) ;? ($:require "terminal-color/terminal-color/main.rkt") ;? 1 (= Viewport nil) ; http://rosettacode.org/wiki/Terminal_control/Coloured_text#Racket ($:define (tput . xs) (system (apply ~a 'tput " " (add-between xs " "))) (void)) ($:define (foreground color) (tput 'setaf color)) ($:define (background color) (tput 'setab color)) ($:define (reset) (tput 'sgr0)) (= new-string-foo* nil) (= last-print* 0) ; run instructions from 'routine*' for 'time-slice' (def run-for-time-slice (time-slice) (point return (for ninstrs 0 (< ninstrs time-slice) (++ ninstrs) (if (empty body.routine*) (err "@stack.routine*.0!fn-name not defined")) ; falling out of end of function = implicit reply (while (>= pc.routine* (len body.routine*)) (pop-stack routine*) (if empty.routine* (return ninstrs)) (when (pos '<- (body.routine* pc.routine*)) (die "No results returned: @(tostring:pr (body.routine* pc.routine*))")) (++ pc.routine*)) (++ curr-cycle*) (when (no ($.current-charterm)) (let curr (seconds) (when (~is curr last-print*) (prn curr " " curr-cycle* " " len.running-routines*) (= last-print* curr)))) ;? (trace "run" "-- " int-canon.memory*) ;? 1 ;? (trace "run" curr-cycle*) (trace "run" label.routine* " " pc.routine* ": " (body.routine* pc.routine*)) ;? (trace "run" routine*) (when (atom (body.routine* pc.routine*)) ; label ;? (tr "label") ;? 1 (when (aand scheduler-switch-table* (alref it (body.routine* pc.routine*))) (++ pc.routine*) (trace "run" label.routine* " " pc.routine* ": " "context-switch forced " abort-routine*) ((abort-routine*))) (++ pc.routine*) (continue)) (let (oarg op arg) (parse-instr (body.routine* pc.routine*)) ;? (tr op) ;? 1 (let results (case op ; arithmetic add (+ (m arg.0) (m arg.1)) subtract (- (m arg.0) (m arg.1)) multiply (* (m arg.0) (m arg.1)) divide (/ (real (m arg.0)) (m arg.1)) divide-with-remainder (list (trunc:/ (m arg.0) (m arg.1)) (mod (m arg.0) (m arg.1))) ; boolean and (and (m arg.0) (m arg.1)) or (or (m arg.0) (m arg.1)) not (not (m arg.0)) ; comparison equal ;? (do (prn (m arg.0) " vs " (m arg.1)) (is (m arg.0) (m arg.1)) ;? ) not-equal (~is (m arg.0) (m arg.1)) less-than (< (m arg.0) (m arg.1)) greater-than (> (m arg.0) (m arg.1)) lesser-or-equal (<= (m arg.0) (m arg.1)) greater-or-equal (>= (m arg.0) (m arg.1)) ; control flow jump (do (= pc.routine* (+ 1 pc.routine* (v arg.0))) (continue)) jump-if (when (m arg.0) (= pc.routine* (+ 1 pc.routine* (v arg.1))) (continue)) jump-unless ; convenient helper (unless (m arg.0) (= pc.routine* (+ 1 pc.routine* (v arg.1))) (continue)) ; data management: scalars, arrays, and-records (structs) copy (m arg.0) get (with (operand (canonize arg.0) idx (v arg.1)) (assert (iso '(offset) (ty arg.1)) "record index @arg.1 must have type 'offset'") (assert (< -1 idx (len typeinfo.operand!elems)) "@idx is out of bounds of record @operand") (m `((,(apply + v.operand (map (fn(x) (sizeof `((_ ,@x)))) (firstn idx typeinfo.operand!elems))) ,@typeinfo.operand!elems.idx) (raw)))) get-address (with (operand (canonize arg.0) idx (v arg.1)) (assert (iso '(offset) (ty arg.1)) "record index @arg.1 must have type 'offset'") (assert (< -1 idx (len typeinfo.operand!elems)) "@idx is out of bounds of record @operand") (apply + v.operand (map (fn(x) (sizeof `((_ ,@x)))) (firstn idx typeinfo.operand!elems)))) index (withs (operand (canonize arg.0) elemtype typeinfo.operand!elem idx (m arg.1)) ;? (write arg.0) ;? (pr " => ") ;? (write operand) ;? (prn) (unless (< -1 idx array-len.operand) (die "@idx is out of bounds of array @operand")) (m `((,(+ v.operand 1 ; for array size (* idx (sizeof `((_ ,@elemtype))))) ,@elemtype) (raw)))) index-address (withs (operand (canonize arg.0) elemtype typeinfo.operand!elem idx (m arg.1)) (unless (< -1 idx array-len.operand) (die "@idx is out of bounds of array @operand")) (+ v.operand 1 ; for array size (* idx (sizeof `((_ ,@elemtype)))))) new (if (isa arg.0 'string) ; special-case: allocate space for a literal string (new-string arg.0) (let type (v arg.0) (assert (iso '(literal) (ty arg.0)) "new: second arg @arg.0 must be literal") (if (no type*.type) (err "no such type @type")) ; todo: initialize memory. currently racket does it for us (if type*.type!array (new-array type (m arg.1)) (new-scalar type)))) sizeof (sizeof `((_ ,(m arg.0)))) length (let base arg.0 (if (or typeinfo.base!array address?.base) array-len.base -1)) ; tagged-values require one primitive save-type (annotate 'record `(,((ty arg.0) 0) ,(m arg.0))) ; code points for characters character-to-integer ($.char->integer (m arg.0)) integer-to-character ($.integer->char (m arg.0)) ; multiprocessing fork ; args: fn globals-table args ... (let routine (apply make-routine (m arg.0) (map m (nthcdr 3 arg))) (= rep.routine!id ++.next-routine-id*) (= rep.routine!globals (when (len> arg 1) (m arg.1))) (= rep.routine!limit (when (len> arg 2) (m arg.2))) (enq routine running-routines*) rep.routine!id) fork-helper ; args: fn globals-table args ... (let routine (apply make-routine (m arg.0) (map m (nthcdr 3 arg))) (= rep.routine!id ++.next-routine-id*) (set rep.routine!helper) (= rep.routine!globals (when (len> arg 1) (m arg.1))) (= rep.routine!limit (when (len> arg 2) (m arg.2))) (enq routine running-routines*) rep.routine!id) sleep (do (case (v arg.0) for-some-cycles (let wakeup-time (+ curr-cycle* (v arg.1)) (trace "run" label.routine* " " pc.routine* ": " "sleeping until " wakeup-time) (= rep.routine*!sleep `(until ,wakeup-time))) until-location-changes (= rep.routine*!sleep `(until-location-changes ,(addr arg.1) ,(m arg.1))) until-routine-done (= rep.routine*!sleep `(until-routine-done ,(m arg.1))) ; else (die "badly formed 'sleep' call @(tostring:prn (body.routine* pc.routine*))") ) ((abort-routine*))) assert (unless (m arg.0) (die (v arg.1))) ; other routines will be able to look at the error status assert-false (when (m arg.0) (die (v arg.1))) ; cursor-based (text mode) interaction cursor-mode ;(do1 nil (system "/bin/stty -F /dev/tty raw")) (do1 nil (if (no ($.current-charterm)) ($.open-charterm))) retro-mode ;(do1 nil (system "/bin/stty -F /dev/tty sane")) (do1 nil (if ($.current-charterm) ($.close-charterm))) clear-host-screen (do1 nil (pr "\e[m\e[2J\e[;H")) clear-line-on-host (do1 nil (pr "\e[2K")) cursor-on-host (do1 nil (pr (+ "\e[" (m arg.0) ";" (m arg.1) "H"))) cursor-on-host-to-next-line (do1 nil (pr "\r\n")) cursor-up-on-host (do1 nil (pr (+ "\e[" (aif (len> arg 0) (or m arg.0) 1) "A"))) cursor-down-on-host (do1 nil (pr (+ "\e[" (aif (len> arg 0) (or m arg.0) 1) "B"))) cursor-right-on-host (do1 nil (pr (+ "\e[" (aif (len> arg 0) (or m arg.0) 1) "C"))) cursor-left-on-host (do1 nil (pr (+ "\e[" (aif (len> arg 0) (or m arg.0) 1) "D"))) print-character-to-host (do1 nil (assert (in (type:m arg.0) 'char 'sym) (rep (m arg.0))) ;? (write (m arg.0)) (pr " => ") (prn (type (m arg.0))) (if (no ($.current-charterm)) (pr (m arg.0)) (caselet x (m arg.0) ; todo: test these exceptions #\newline (pr "\r\n") #\backspace ; backspace doesn't clear after moving the cursor (pr "\b \b") ctrl-c (do ($.close-charterm) (die "interrupted")) ;else (if (and (len> arg 2) (m arg.2)) (do ($.foreground (m arg.1)) ($.background (m arg.2)) (pr x) ($.reset)) (and (len> arg 1) (m arg.1)) (do ($.foreground (m arg.1)) (pr x) ($.reset)) :else (pr x)))) ) read-key-from-host (if ($.current-charterm) (and ($.charterm-byte-ready?) (r