Non-blocking REPL

Makeshift REPL so that one can evaluate expressions while the game is
running. I have a curses project which used lua to do something
similiar, I'd like to repurpose that at some point. For now, this is
satisfactory.
This commit is contained in:
FroggyGreen 2024-06-29 19:39:01 -05:00
parent 1a7b86f6d1
commit 0c55429241

View File

@ -88,6 +88,57 @@
(end-render-frame))))
(define read-without-blocking
(lambda (port)
(letrec
([iter!
(lambda (read-stack expr)
(let ([chr (peek-char port)])
(cond
[(or (char=? chr #\newline) (eof-object? chr))
(read-char port)
(if (and (null? read-stack) (pair? expr))
(reverse expr)
'())]
[(or (char=? chr #\() (char=? chr #\[))
(read-char port)
(iter! (cons chr read-stack) (cons chr expr))]
[(char=? chr #\))
(read-char port)
(if (and (pair? read-stack) (char=? (car read-stack) #\())
(iter! (cdr read-stack) (cons chr expr))
'())]
[(char=? chr #\])
(read-char port)
(if (and (pair? read-stack) (char=? (car read-stack) #\[))
(iter! (cdr read-stack) (cons chr expr))
'())]
[else
(read-char port)
(iter! read-stack (cons chr expr))])))])
(if (char-ready? port)
(let ([expr-list (iter! '() '())])
(if (pair? expr-list)
(read (open-input-string [list->string expr-list]))))))))
(define eval-print
(lambda (expr port)
(write (eval expr) port)
(write-char #\newline port)
(flush-output-port port)))
(define read-eval-print-without-blocking
(lambda ()
(let ([expr (read-without-blocking (console-input-port))])
(if (pair? expr)
(eval-print expr (console-output-port))))))
(define run-game!
(lambda ()
(letrec* ([lag 0.0] [prev-time-utc (current-time)] [curr-time-utc prev-time-utc]
@ -103,6 +154,9 @@
(lambda ()
(when (and game-running? (window-open?))
;; handle expressions without blocking
(read-eval-print-without-blocking)
;; handle window events
(process-window-input!)