summaryrefslogtreecommitdiff
path: root/starling/repl.scm
blob: 551e2adda69aab43498c8d728bcc8132cdd46700 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
;;; 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 data array-list)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics font)
  #:use-module (chickadee graphics path)
  #: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-lines #:accessor log-lines)
  (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-lines repl) line))
            (match (string-split s #\newline)
              ;; Drop trailing newlines
              ((lines ... "") lines)
              (lines lines))))

(define-method (flush-log (repl <repl>))
  (let ((log (log-lines 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 (print-backtrace (repl <repl>) stack)
  (let loop ((i (- (stack-length stack) 1)))
    (when (>= i 0)
      (let ((frame (stack-ref stack i)))
        (match (frame-source frame)
          ((_ file line . column)
           (format #t "~d: In ~a:~%  ~d:~d ~a~%"
                   i file line column
                   (frame-procedure-name frame)))
          (#f
           (format #t "~d: In unknown file:~%  ~a~%"
                   i
                   (frame-procedure-name frame))))
        (loop (- i 1))))))

(define-method (eval-user-text (repl <repl>))
  (let ((result *unspecified*))
    (let/ec cancel
      (with-exception-handler
          (lambda (exception)
            (print repl
                   (with-output-to-string
                     (lambda ()
                       (print-exception (current-output-port) #f
                                        (exception-kind exception)
                                        (exception-args exception))
                       (newline)
                       (let ((tag (match (fluid-ref %stacks)
                                    ((_ . tag) tag)
                                    (_ 0))))
                         (print-backtrace repl (make-stack #t 3 tag))))))
            (cancel))
        (lambda ()
          (print repl
                 (with-output-to-string
                   (lambda ()
                     (start-stack 'repl-stack
                                  (set! result
                                    (eval-string (user-text repl)
                                                 #:module (module repl))))))))))
    result))

(define-method (eval-and-print (repl <repl>))
  (print repl (text (& repl prompt)))
  (let ((result (eval-user-text repl)))
    ;;(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 <canvas>
                 #:painter (with-style ((fill-color (make-color 0 0 0 0.7)))
                             (fill
                              (rectangle (vec2 0.0 0.0)
                                         (vec2-x res)
                                         (vec2-y res)))))
               (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-lines 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)))