summaryrefslogtreecommitdiff
path: root/starling/repl.scm
blob: b972b5f54e2922b1950da7eb85c7eb6691f0a35f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; Starling Game Engine
;;; Copyright © 2018 David Thompson <davet@gnu.org>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Starling.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; REPL node for in-engine live hacking.
;;
;;; Code:

(define-module (starling repl)
  #:use-module (chickadee array-list)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics font)
  #:use-module (chickadee math rect)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (ice-9 control)
  #:use-module (ice-9 eval-string)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (starling ring-buffer)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #:use-module (starling scene)
  #:export (<repl>
            open-repl))

(define-class <repl> (<scene-2d>)
  (scene-mux #:getter scene-mux #:init-keyword #:scene-mux)
  (overlay-scene #:accessor overlay-scene #:init-keyword #:overlay-scene)
  (lines #:accessor lines #:init-form '())
  (log #:accessor log)
  (user-text #:accessor user-text #:init-form "")
  (module #:accessor module #:init-form (resolve-module '(guile-user))))

(define-method (open-repl repl)
  (set! (overlay-scene repl) (current-scene (scene-mux repl)))
  (push-scene (scene-mux repl) repl))

(define-method (close-repl (repl <repl>))
  (pop-scene (scene-mux repl)))

(define-method (modify-user-text (repl <repl>) new-text)
  (set! (user-text repl) new-text)
  (set! (text (& repl prompt))
    (format #f "~s> ~a" (module-name (module repl)) new-text)))

(define-method (backward-delete (repl <repl>))
  (let ((text (user-text repl)))
    (modify-user-text repl
                      (substring text 0 (max (- (string-length text) 1) 0)))))

(define-method (print (repl <repl>) s)
  (for-each (lambda (line)
              (ring-buffer-put! (log repl) line))
            (match (string-split s #\newline)
              ;; Drop trailing newlines
              ((lines ... "") lines)
              (lines lines))))

(define-method (flush-log (repl <repl>))
  (let ((log (log repl)))
    (let loop ((i 0)
               (labels (lines repl)))
      (when (< i (ring-buffer-length log))
        (match labels
          ((label . rest)
           (set! (text label) (ring-buffer-ref log i))
           (loop (+ i 1) rest)))))))

(define-method (eval-user-text (repl <repl>))
  (let ((result *unspecified*))
    (values (with-output-to-string
              (lambda ()
                (set! result
                  (let/ec cancel
                    (start-stack 'repl-stack
                                 (with-exception-handler (lambda (exception)
                                                           ;;(backtrace)
                                                           (display exception)
                                                           (cancel *unspecified*))
                                   (lambda ()
                                     (eval-string (user-text repl)
                                                  #:module (module repl)))))))))
            result)))

(define-method (eval-and-print (repl <repl>))
  (call-with-values (lambda () (eval-user-text repl))
    (lambda (output result)
      (print repl (text (& repl prompt)))
      (print repl output)
      (unless (unspecified? result)
        (print repl (with-output-to-string
                      (lambda ()
                        (display "=> ")
                        (write result)))))
      (flush-log repl)
      (modify-user-text repl ""))))

(define-method (on-boot (repl <repl>))
  (let* ((res (resolution (car (cameras repl))))
         (font (default-font))
         (line-height (font-line-height font))
         (left-margin 6.0)
         (bottom-margin 6.0)
         (nlines (- (inexact->exact
                     (floor
                      (/ (vec2-y res) line-height)))
                    1))
         (line-nodes
          (map (lambda (i)
                 (make <label>
                   #:rank 9
                   #:font font
                   #:position (vec2 left-margin
                                    (- (vec2-y res)
                                       (* i line-height)))
                   #:vertical-align 'top))
               (iota nlines))))
    (attach-to repl
               (make <filled-rect>
                 #:region (make-rect 0.0 0.0 (vec2-x res) (vec2-y res))
                 #:color (make-color 0.0 0.0 0.0 0.7))
               (make <label>
                 #:rank 9
                 #:name 'prompt
                 #:position (vec2 left-margin bottom-margin)))
    (apply attach-to repl line-nodes)
    (set! (lines repl) line-nodes)
    (set! (log repl) (make-ring-buffer nlines))
    (modify-user-text repl "")))

(define-method (update (repl <repl>) dt)
  (update-tree (overlay-scene repl) dt)
  (next-method))

(define-method (render (repl <repl>) alpha)
  (render-tree (overlay-scene repl) alpha)
  (next-method))

(define-method (on-key-press (repl <repl>) key modifiers repeat?)
  (match key
    ('escape (close-repl repl))
    ('backspace (backward-delete repl))
    ('return (eval-and-print repl))
    (_ #f)))

(define-method (on-text-input (repl <repl>) text)
  (modify-user-text repl (string-append (user-text repl) text)))