From 13c70844ee8d54778569298bc0a6867b7cda0c37 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 9 Jan 2014 19:11:09 -0500 Subject: Factor REPL out of main loop. * 2d/game.scm (start-game-loop): Don't spawn REPL. (update-and-render, tick): Don't run REPL. (run-repl-thunk, run-repl): Move. * 2d/repl/repl.scm: (run-repl-thunk, flush-repl): Add. (flush-repl): Schedule REPL to be run within main loop. --- 2d/repl/repl.scm | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to '2d/repl') diff --git a/2d/repl/repl.scm b/2d/repl/repl.scm index df0b8fe..6b0a69b 100644 --- a/2d/repl/repl.scm +++ b/2d/repl/repl.scm @@ -29,9 +29,14 @@ #:use-module (system repl common) #:use-module (system repl command) #:use-module (ice-9 control) - #:use-module (2d mvars) + #:use-module (srfi srfi-2) + #:use-module (2d agenda) #:use-module (2d game) - #:export (repl-input-mvar repl-output-mvar start-repl run-repl)) + #:use-module (2d mvars) + #:export (repl-input-mvar + repl-output-mvar + start-repl + run-repl)) ;;; @@ -127,12 +132,38 @@ ;;; -;;; The repl +;;; guile-2d REPL stuff ;;; (define repl-input-mvar (new-empty-mvar)) (define repl-output-mvar (new-empty-mvar)) +(define (run-repl-thunk thunk input output error stack) + "Run THUNK with the given REPL STACK. I/O is redirected to the given +INPUT, OUTPUT, and ERROR ports." + (put-mvar + repl-output-mvar + (with-input-from-port input + (lambda () + (with-output-to-port output + (lambda () + (with-error-to-port error + (lambda () + (with-fluids ((*repl-stack* stack)) + (thunk)))))))))) + +(define (flush-repl) + "Execute a thunk from the REPL is there is one." + (unless (mvar-empty? repl-input-mvar) + (and-let* ((vals (try-take-mvar repl-input-mvar))) + (apply run-repl-thunk vals)))) + +(schedule-interval flush-repl 5) + +;;; +;;; The repl +;;; + (define* (start-repl #:optional (lang (current-language)) #:key debug) ;; ,language at the REPL will update the current-language. Make ;; sure that it does so in a new dynamic scope. -- cgit v1.2.3