summaryrefslogtreecommitdiff
path: root/2d/repl
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-01-09 19:11:09 -0500
committerDavid Thompson <dthompson2@worcester.edu>2014-01-09 19:11:09 -0500
commit13c70844ee8d54778569298bc0a6867b7cda0c37 (patch)
treedb610d15525425b21924a9290b49cee10471f265 /2d/repl
parentf9f18fe45e60660ae51025d2b35be2874ac1f41a (diff)
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.
Diffstat (limited to '2d/repl')
-rw-r--r--2d/repl/repl.scm37
1 files changed, 34 insertions, 3 deletions
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.