summaryrefslogtreecommitdiff
path: root/chickadee/scripting/script.scm
blob: 1e0727829bc3a2b2adf463f008eeb8d256fa2f3f (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
;;; Chickadee Game Toolkit
;;; Copyright © 2017 David Thompson <dthompson2@worcester.edu>
;;;
;;; Chickadee 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.
;;;
;;; Chickadee 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 this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (chickadee scripting script)
  #:use-module (ice-9 format)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:export (script?
            script-cancelled?
            script-running?
            script-complete?
            spawn-script
            script
            cancel-script
            yield
            join)
  #:replace (yield))

(define-record-type <script>
  (make-script status children joined)
  script?
  (status script-status set-script-status!)
  (children script-children set-script-children!)
  (joined script-joined set-script-joined!))

(define current-script (make-parameter #f))

(define (display-script script port)
  (format port "<script status: ~a>" (script-status script)))

(set-record-type-printer! <script> display-script)

(define (script-cancelled? script)
  "Return #t if SCRIPT has been cancelled."
  (eq? 'cancelled (script-status script)))

(define (script-running? script)
  "Return #t if SCRIPT has not yet terminated or been cancelled."
  (eq? 'running (script-status script)))

(define (script-complete? script)
  "Return #t if SCRIPT has terminated."
  (eq? 'complete (script-status script)))

(define (script-terminated? script)
  "Return #t if SCRIPT is in either the completed or cancelled state."
  (or (script-complete? script) (script-cancelled? script)))

(define (cancel-script script)
  "Prevent further execution of SCRIPT."
  (set-script-status! script 'cancelled)
  (for-each cancel-script (script-children script))
  (resume-joined-scripts script))

(define (add-join! script cont)
  "Add CONT to the list of continuations waiting for SCRIPT to finish."
  (set-script-joined! script (cons cont (script-joined script))))

(define (resume-joined-scripts script)
  "Resume all scripts waiting on SCRIPT to terminate."
  (for-each (lambda (cont) (cont))
            (script-joined script)))

(define script-prompt (make-prompt-tag 'script))

(define (spawn-script thunk)
  "Apply THUNK as a script."
  (let ((script (make-script 'running '() '())))
    (define (handler cont callback . args)
      (define (resume . args)
        ;; Call the continuation that resumes the script, unless the
        ;; script has been cancelled in the meanwhile.
        (unless (script-cancelled? script)
          (call-with-prompt script-prompt
            (lambda () (apply cont args))
            handler)))
      (when (procedure? callback)
        (apply callback resume args)))
    (define task
      (let ((dynamic-state (current-dynamic-state)))
        (lambda ()
          (with-dynamic-state
           dynamic-state
           (lambda ()
             (current-script script)
             (thunk)
             (set-script-status! script 'complete)
             (resume-joined-scripts script))))))
    ;; Register child script with parent.  Cancelling the parent will
    ;; cause all children to be cancelled as well.
    (when (script? (current-script))
      (set-script-children! (current-script)
                            (cons script (script-children (current-script)))))
    ;; Start the script.
    (call-with-prompt script-prompt task handler)
    script))

(define-syntax-rule (script body ...)
  "Evaluate BODY in a script."
  (spawn-script (lambda () body ...)))

(define (yield handler)
  "Suspend the current script and pass its continuation to the
procedure HANDLER."
  (abort-to-prompt script-prompt handler))

(define (join script)
  "Suspend the current script until SCRIPT has terminated."
  (unless (script-terminated? script)
    (yield (lambda (cont)
             (add-join! script cont)))))