##// END OF EJS Templates
Properly handle stringly-indexed arrays
naryl -
r16:6eb15d3f default
parent child Browse files
Show More
@@ -1,9 +1,6 b''
1
2 * string array keys as addition to number keys
3 * arr[] notation (i.e. with empty index)
4
1
5 * Windows GUI (for the compiler)
2 * Windows GUI (for the compiler)
6 * Save-load game in slots
3 * Save-load game in slots
7 * Resizable frames
4 * Resizable frames
8 * Build Istreblenie
5 * Build Istreblenie
9 ** modifying it to suit compiler specifics No newline at end of file
6 ** modifying it to suit compiler specifics
@@ -1,236 +1,287 b''
1
1
2 (in-package sugar-qsp)
2 (in-package sugar-qsp)
3
3
4 ;;; API deals with DOM manipulation and some bookkeeping for the
4 ;;; API deals with DOM manipulation and some bookkeeping for the
5 ;;; intrinsics, namely variables
5 ;;; intrinsics, namely variables
6 ;;; API is an implementation detail and has no QSP documentation. It
6 ;;; API is an implementation detail and has no QSP documentation. It
7 ;;; doesn't call intrinsics
7 ;;; doesn't call intrinsics
8
8
9 (setf (root api) (ps:create))
9 (setf (root api) (ps:create))
10
10
11 ;;; Utils
11 ;;; Utils
12
12
13 (defm (root api make-act-html) (title img)
13 (defm (root api make-act-html) (title img)
14 (+ "<a class='qsp-act' href='#' onclick='SugarQSP.acts[\"" title "\"].act();'>"
14 (+ "<a class='qsp-act' href='#' onclick='SugarQSP.acts[\"" title "\"].act();'>"
15 title
15 title
16 "</a>"))
16 "</a>"))
17
17
18 (defm (root api make-menu-item-html) (num title img loc)
18 (defm (root api make-menu-item-html) (num title img loc)
19 (+ "<a href='#' onclick='SugarQSP.api.runMenu(" num ", \"" loc "\")();'>"
19 (+ "<a href='#' onclick='SugarQSP.api.runMenu(" num ", \"" loc "\")();'>"
20 "<img src='" img "'>"
20 "<img src='" img "'>"
21 title
21 title
22 "</a>"))
22 "</a>"))
23
23
24 ;; To be used in saving game
24 ;; To be used in saving game
25 (defm (root api stash-state) ()
25 (defm (root api stash-state) (args)
26 (setf (root state-stash)
26 (setf (root state-stash)
27 (*j-s-o-n.stringify
27 (*j-s-o-n.stringify
28 (ps:create vars (root vars)
28 (ps:create vars (root vars)
29 objs (root objs)
29 objs (root objs)
30 loc-args args
31 main-html (ps:@
32 (document.get-element-by-id :qsp-main)
33 inner-h-t-m-l)
34 stat-html (ps:@
35 (document.get-element-by-id :qsp-stat)
36 inner-h-t-m-l)
30 next-location (root current-location))))
37 next-location (root current-location))))
31 (values))
38 (values))
32
39
40 (defm (root api unstash-state) ()
41 (let ((data (*j-s-o-n.parse (root state-stash))))
42 (this.clear-act)
43 (setf (root vars) (ps:@ data vars))
44 (loop :for k :in (*object.keys (root vars))
45 :do (*object.set-prototype-of (ps:getprop (root vars) k)
46 (root api *var prototype)))
47 (setf (root objs) (ps:@ data objs))
48 (setf (root current-location) (ps:@ data next-location))
49 (setf (ps:@ (document.get-element-by-id :qsp-main) inner-h-t-m-l)
50 (ps:@ data main-html))
51 (setf (ps:@ (document.get-element-by-id :qsp-stat) inner-h-t-m-l)
52 (ps:@ data stat-html))
53 (funcall (root locs (root current-location)) (ps:@ data loc-args))
54 (this.update-objs)
55 (values)))
56
33 (defm (root api state-to-base64) ()
57 (defm (root api state-to-base64) ()
34 (btoa (encode-u-r-i-component (root state-stash))))
58 (btoa (encode-u-r-i-component (root state-stash))))
35
59
36 (defm (root api base64-to-state) (data)
60 (defm (root api base64-to-state) (data)
37 (setf (root state-stash) (decode-u-r-i-component (atob data)))
61 (setf (root state-stash) (decode-u-r-i-component (atob data))))
38 (let ((data (*j-s-o-n.parse (root state-stash))))
39 (this.clear-id :qsp-main)
40 (this.clear-id :qsp-stat)
41 (this.clear-act)
42 (setf (root vars) (ps:@ data vars))
43 (setf (root objs) (ps:@ data objs))
44 (setf (root current-location) (ps:@ data next-location))
45 (funcall (root locs (root current-location)))
46 (this.update-objs)
47 (values)))
48
62
49 ;;; Misc
63 ;;; Misc
50
64
51 (defm (root api clear-id) (id)
65 (defm (root api clear-id) (id)
52 (setf (ps:chain document (get-element-by-id id) inner-text) ""))
66 (setf (ps:chain document (get-element-by-id id) inner-text) ""))
53
67
54 (defm (root api get-id) (id)
68 (defm (root api get-id) (id)
55 (if (var "USEHTML" 0)
69 (if (var "USEHTML" 0 :num)
56 (ps:chain (document.get-element-by-id id) inner-h-t-m-l)
70 (ps:chain (document.get-element-by-id id) inner-h-t-m-l)
57 (ps:chain (document.get-element-by-id id) inner-text)))
71 (ps:chain (document.get-element-by-id id) inner-text)))
58
72
59 (defm (root api set-id) (id contents)
73 (defm (root api set-id) (id contents)
60 (if (var "USEHTML" 0)
74 (if (var "USEHTML" 0 :num)
61 (setf (ps:chain (document.get-element-by-id id) inner-h-t-m-l) contents)
75 (setf (ps:chain (document.get-element-by-id id) inner-h-t-m-l) contents)
62 (setf (ps:chain (document.get-element-by-id id) inner-text) contents)))
76 (setf (ps:chain (document.get-element-by-id id) inner-text) contents)))
63
77
64 (defm (root api append-id) (id contents)
78 (defm (root api append-id) (id contents)
65 (if (var "USEHTML" 0)
79 (if (var "USEHTML" 0 :num)
66 (incf (ps:chain (document.get-element-by-id id) inner-h-t-m-l) contents)
80 (incf (ps:chain (document.get-element-by-id id) inner-h-t-m-l) contents)
67 (incf (ps:chain (document.get-element-by-id id) inner-text) contents)))
81 (incf (ps:chain (document.get-element-by-id id) inner-text) contents)))
68
82
69 ;;; Function calls
83 ;;; Function calls
70
84
71 (defm (root api init-args) (args)
85 (defm (root api init-args) (args)
72 (dotimes (i (length args))
86 (dotimes (i (length args))
73 (if (numberp (elt args i))
87 (let ((arg (elt args i)))
74 (set (var args i) (elt args i))
88 (if (numberp arg)
75 (set (var $args i) (elt args i)))))
89 (this.set-var args i :num arg)
90 (this.set-var args i :str arg)))))
76
91
77 (defm (root api get-result) ()
92 (defm (root api get-result) ()
78 (if (not (equal "" (var $result 0)))
93 (if (not (equal "" (var result 0 :str)))
79 (var $result 0)
94 (var result 0 :str)
80 (var result 0)))
95 (var result 0 :num)))
81
96
82 ;;; Text windows
97 ;;; Text windows
83
98
84 (defm (root api key-to-id) (key)
99 (defm (root api key-to-id) (key)
85 (case key
100 (case key
86 (:main "qsp-main")
101 (:main "qsp-main")
87 (:stat "qsp-stat")
102 (:stat "qsp-stat")
88 (:objs "qsp-objs")
103 (:objs "qsp-objs")
89 (:acts "qsp-acts")
104 (:acts "qsp-acts")
90 (:input "qsp-input")
105 (:input "qsp-input")
91 (:dropdown "qsp-dropdown")
106 (:dropdown "qsp-dropdown")
92 (t (report-error "Internal error!"))))
107 (t (report-error "Internal error!"))))
93
108
94 (defm (root api get-frame) (key)
109 (defm (root api get-frame) (key)
95 (document.get-element-by-id (this.key-to-id key)))
110 (document.get-element-by-id (this.key-to-id key)))
96
111
97 (defm (root api add-text) (key text)
112 (defm (root api add-text) (key text)
98 (this.append-id (this.key-to-id key) text))
113 (this.append-id (this.key-to-id key) text))
99
114
100 (defm (root api get-text) (key)
115 (defm (root api get-text) (key)
101 (this.get-id (this.key-to-id key)))
116 (this.get-id (this.key-to-id key)))
102
117
103 (defm (root api clear-text) (key)
118 (defm (root api clear-text) (key)
104 (this.clear-id (this.key-to-id key)))
119 (this.clear-id (this.key-to-id key)))
105
120
106 (defm (root api newline) (key)
121 (defm (root api newline) (key)
107 (let ((div (this.get-frame key)))
122 (let ((div (this.get-frame key)))
108 (ps:chain div (append-child (document.create-element "br")))))
123 (ps:chain div (append-child (document.create-element "br")))))
109
124
110 (defm (root api enable-frame) (key enable)
125 (defm (root api enable-frame) (key enable)
111 (let ((clss (ps:getprop (this.get-frame key) 'class-list)))
126 (let ((clss (ps:getprop (this.get-frame key) 'class-list)))
112 (setf clss.style.display (if enable "block" "none"))
127 (setf clss.style.display (if enable "block" "none"))
113 (values)))
128 (values)))
114
129
115 ;;; Actions
130 ;;; Actions
116
131
117 (defm (root api add-act) (title img act)
132 (defm (root api add-act) (title img act)
118 (setf (ps:getprop (root acts) title)
133 (setf (ps:getprop (root acts) title)
119 (ps:create :img img :act act))
134 (ps:create :img img :act act))
120 (this.update-acts))
135 (this.update-acts))
121
136
122 (defm (root api del-act) (title)
137 (defm (root api del-act) (title)
123 (delete (ps:getprop (root acts) title))
138 (delete (ps:getprop (root acts) title))
124 (this.update-acts))
139 (this.update-acts))
125
140
126 (defm (root api clear-act) ()
141 (defm (root api clear-act) ()
127 (setf (root acts) (ps:create))
142 (setf (root acts) (ps:create))
128 (this.clear-id "qsp-acts"))
143 (this.clear-id "qsp-acts"))
129
144
130 (defm (root api update-acts) ()
145 (defm (root api update-acts) ()
131 (this.clear-id "qsp-acts")
146 (this.clear-id "qsp-acts")
132 (ps:for-in (title (root acts))
147 (ps:for-in (title (root acts))
133 (let ((obj (ps:getprop (root acts) title)))
148 (let ((obj (ps:getprop (root acts) title)))
134 (this.append-id "qsp-acts"
149 (this.append-id "qsp-acts"
135 (this.make-act-html title (ps:getprop obj :img))))))
150 (this.make-act-html title (ps:getprop obj :img))))))
136
151
137 ;;; Variables
152 ;;; Variable class
153
154 (defm (root api *var) (name)
155 ;; From strings to numbers
156 (setf this.indexes (ps:create))
157 ;; From numbers to {num: 0, str: ""} objects
158 (setf this.values (list))
159 (values))
160
161 (defm (root api *var prototype new-value) ()
162 (ps:create :num 0 :str ""))
138
163
139 (defm (root api var-slot) (name)
164 (defm (root api *var prototype index-num) (index)
140 (if (= (ps:@ name 0) #\$)
165 (let ((num-index
141 :str
166 (if (stringp index)
142 :num))
167 (if (in index this.indexes)
168 (ps:getprop this.indexes index)
169 (let ((n (length this.values)))
170 (setf (ps:getprop this.indexes index) n)
171 n))
172 index)))
173 (unless (in num-index this.values)
174 (setf (elt this.values num-index) (this.new-value)))
175 num-index))
176
177 (defm (root api *var prototype get) (index slot)
178 (unless (or index (= 0 index))
179 (setf index (1- (length this.values))))
180 (ps:getprop this.values (this.index-num index) slot))
181
182 (defm (root api *var prototype set) (index slot value)
183 (unless (or index (= 0 index))
184 (setf index (length store)))
185 (case slot
186 (:num (setf value (ps:chain *number (parse-int value))))
187 (:str (setf value (ps:chain value (to-string)))))
188 (setf (ps:getprop this.values (this.index-num index) slot) value)
189 (values))
190
191 (defm (root api *var prototype kill) (index)
192 (setf (elt this.values (this.index-num index)) (this.new-value)))
193
194 ;;; Variables
143
195
144 (defm (root api var-real-name) (name)
196 (defm (root api var-real-name) (name)
145 (if (= (ps:@ name 0) #\$)
197 (if (= (ps:@ name 0) #\$)
146 (ps:chain name (substr 1))
198 (values (ps:chain name (substr 1)) :str)
147 name))
199 (values name :num)))
148
200
149 (defm (root api ensure-var) (name index)
201 (defm (root api ensure-var) (name)
150 (let ((store (this.var-ref name)))
202 (let ((store (this.var-ref name)))
151 (unless store
203 (unless store
152 (setf store (ps:create))
204 (setf store (ps:new (this.-var name)))
153 (setf (ps:getprop (root vars) name) store)))
205 (setf (ps:getprop (root vars) name) store))
154 (unless (in index store)
206 store))
155 (setf (elt store index) (ps:create :num 0 :str "")))
156 (values))
157
207
158 (defm (root api var-ref) (name)
208 (defm (root api var-ref) (name)
159 (let ((var-name (this.var-real-name name))
209 (let ((local-store (this.current-local-frame)))
160 (local-store (this.current-local-frame)))
210 (cond ((in name local-store)
161 (cond ((in var-name local-store)
211 (ps:getprop local-store name))
162 (ps:getprop local-store))
212 ((in name (root vars))
163 ((in var-name (root vars))
213 (ps:getprop (root vars) name))
164 (ps:getprop (root vars) var-name))
165 (t nil))))
214 (t nil))))
166
215
167 (defm (root api get-var) (name index)
216 (defm (root api get-var) (name index slot)
168 (this.ensure-var name index)
217 (ps:chain (this.ensure-var name) (get index slot)))
169 (let ((store (this.var-ref name)))
170 (ps:getprop store index (this.var-slot name))))
171
218
172 (defm (root api set-var) (name index value)
219 (defm (root api set-var) (name index slot value)
173 (this.ensure-var name index)
220 (ps:chain (this.ensure-var name) (set index slot value))
174 (let ((store (this.var-ref name)))
221 (values))
175 (setf (ps:getprop store index (this.var-slot name)) value)
176 (values)))
177
222
178 (defm (root api get-array) (name)
223 (defm (root api get-array) (name)
179 (ps:getprop (root vars) name))
224 (this.var-ref name))
180
225
181 (defm (root api set-array) (name value)
226 (defm (root api set-array) (name value)
182 (setf (ps:getprop (root vars) name) value))
227 (let ((store (this.var-ref name)))
228 (setf (ps:@ store values) (ps:@ value values))
229 (setf (ps:@ store indexes) (ps:@ value indexes)))
230 (values))
183
231
184 (defm (root api kill-var) (name &optional index)
232 (defm (root api kill-var) (name &optional index)
185 (if index
233 (if (and index (not (= 0 index)))
186 (ps:delete (ps:getprop (root vars) name index))
234 (ps:chain (ps:getprop (root vars) name) (kill index))
187 (ps:delete (ps:getprop (root vars) name)))
235 (ps:delete (ps:getprop (root vars) name)))
188 (values))
236 (values))
189
237
190 (defm (root api array-size) (name)
238 (defm (root api array-size) (name)
191 (ps:getprop (root vars) (this.var-real-name name) 'length))
239 (ps:getprop (this.var-ref name) 'length))
240
241 ;;; Locals
242
243 (defm (root api push-local-frame) ()
244 (ps:chain (root locals) (push (ps:create)))
245 (values))
246
247 (defm (root api pop-local-frame) ()
248 (ps:chain (root locals) (pop))
249 (values))
250
251 (defm (root api current-local-frame) ()
252 (elt (root locals) (1- (length (root locals)))))
253
254 (defm (root api new-local) (name)
255 (let ((frame (this.current-local-frame)))
256 (unless (in name frame)
257 (setf (ps:getprop frame name) (ps:create)))
258 (values)))
192
259
193 ;;; Objects
260 ;;; Objects
194
261
195 (defm (root api update-objs) ()
262 (defm (root api update-objs) ()
196 (let ((elt (document.get-element-by-id "qsp-objs")))
263 (let ((elt (document.get-element-by-id "qsp-objs")))
197 (setf elt.inner-h-t-m-l "<ul>")
264 (setf elt.inner-h-t-m-l "<ul>")
198 (loop :for obj :in (root objs)
265 (loop :for obj :in (root objs)
199 :do (incf elt.inner-h-t-m-l (+ "<li>" obj)))
266 :do (incf elt.inner-h-t-m-l (+ "<li>" obj)))
200 (incf elt.inner-h-t-m-l "</ul>")))
267 (incf elt.inner-h-t-m-l "</ul>")))
201
268
202 ;;; Menu
269 ;;; Menu
203
270
204 (defm (root api menu) (menu-data)
271 (defm (root api menu) (menu-data)
205 (let ((elt (document.get-element-by-id "qsp-dropdown"))
272 (let ((elt (document.get-element-by-id "qsp-dropdown"))
206 (i 0))
273 (i 0))
207 (setf elt.inner-h-t-m-l "")
274 (setf elt.inner-h-t-m-l "")
208 (loop :for item :in menu-data
275 (loop :for item :in menu-data
209 :do (incf i)
276 :do (incf i)
210 :do (incf elt.inner-h-t-m-l (this.make-menu-item-html i item.text item.icon item.loc)))
277 :do (incf elt.inner-h-t-m-l (this.make-menu-item-html i item.text item.icon item.loc)))
211 (setf elt.style.display "block")))
278 (setf elt.style.display "block")))
212
279
213 ;;; Content
280 ;;; Content
214
281
215 (defm (root api clean-audio) ()
282 (defm (root api clean-audio) ()
216 (loop :for k :in (*object.keys (root playing))
283 (loop :for k :in (*object.keys (root playing))
217 :for v := (ps:getprop (root playing) k)
284 :for v := (ps:getprop (root playing) k)
218 :do (when (ps:@ v ended)
285 :do (when (ps:@ v ended)
219 (ps:delete (ps:@ (root playing) k)))))
286 (ps:delete (ps:@ (root playing) k)))))
220
287
221 ;;; Locals
222
223 (defm (root api push-local-frame) ()
224 (ps:chain (root locals) (push (ps:create))))
225
226 (defm (root api pop-local-frame) ()
227 (ps:chain (root locals) (pop)))
228
229 (defm (root api current-local-frame) ()
230 (elt (root locals) (1- (length (root locals)))))
231
232 (defm (root api new-local) (name)
233 (let ((frame (this.current-local-frame))
234 (var-name (this.var-real-name name)))
235 (unless (in var-name frame)
236 (setf (ps:getprop frame var-name) (ps:create)))))
@@ -1,323 +1,324 b''
1
1
2 (in-package sugar-qsp)
2 (in-package sugar-qsp)
3
3
4 ;;;; Functions and procedures defined by the QSP language.
4 ;;;; Functions and procedures defined by the QSP language.
5 ;;;; They can call api and deal with locations and other data directly.
5 ;;;; They can call api and deal with locations and other data directly.
6 ;;;; Except vars. Use VAR and SET or GET-VAR and SET-VAR api calls.
6 ;;;; Except vars. Use VAR and SET or GET-VAR and SET-VAR api calls.
7
7
8 (setf (root lib) (ps:create))
8 (setf (root lib) (ps:create))
9
9
10 ;;; 1loc
10 ;;; 1loc
11
11
12 (defm (root lib goto) (target &rest args)
12 (defm (root lib goto) (target args)
13 (api-call clear-text :main)
13 (api-call clear-text :main)
14 (apply (root lib xgoto) target args))
14 (funcall (root lib xgoto) target (or args (list)))
15 (values))
15
16
16 (defm (root lib xgoto) (target &rest args)
17 (defm (root lib xgoto) (target args)
17 (api-call clear-act)
18 (api-call clear-act)
18 (api-call init-args args)
19 (setf (root current-location) (ps:chain target (to-upper-case)))
19 (setf (root current-location) (ps:chain target (to-upper-case)))
20 (api-call stash-state)
20 (api-call stash-state args)
21 (funcall (ps:getprop (root locs) (root current-location))))
21 (funcall (ps:getprop (root locs) (root current-location))
22 (or args (list)))
23 (values))
22
24
23 ;;; 2var
25 ;;; 2var
24
26
25 ;;; 3expr
27 ;;; 3expr
26
28
27 ;;; 4code
29 ;;; 4code
28
30
29 (defm (root lib rand) (a &optional (b 1))
31 (defm (root lib rand) (a &optional (b 1))
30 (let ((min (min a b))
32 (let ((min (min a b))
31 (max (max a b)))
33 (max (max a b)))
32 (+ min (ps:chain *math (random (- max min))))))
34 (+ min (ps:chain *math (random (- max min))))))
33
35
34 ;;; 5arrays
36 ;;; 5arrays
35
37
36 (defm (root lib copyarr) (to from start count)
38 (defm (root lib copyarr) (to from start count)
37 (ps:for ((i start))
39 (multiple-value-bind (to-name to-slot)
38 ((< i (min (api-call array-size from)
40 (api-call var-real-name to)
39 (+ start count))))
41 (multiple-value-bind (from-name from-slot)
40 ((incf i))
42 (api-call var-real-name from)
41 (api-call set-var to (+ start i)
43 (ps:for ((i start))
42 (api-call get-var from (+ start i)))))
44 ((< i (min (api-call array-size from-name)
45 (+ start count))))
46 ((incf i))
47 (api-call set-var to-name (+ start i) to-slot
48 (api-call get-var from-name (+ start i) from-slot))))))
43
49
44 (defm (root lib arrpos) (name value &optional (start 0))
50 (defm (root lib arrpos) (name value &optional (start 0))
45 (ps:for ((i start)) ((< i (api-call array-size name))) ((incf i))
51 (multiple-value-bind (real-name slot)
46 (when (eq (api-call get-var name i) value)
52 (api-call var-real-name name)
47 (return i)))
53 (ps:for ((i start)) ((< i (api-call array-size name))) ((incf i))
54 (when (eq (api-call get-var real-name i slot) value)
55 (return i))))
48 -1)
56 -1)
49
57
50 (defm (root lib arrcomp) (name pattern &optional (start 0))
58 (defm (root lib arrcomp) (name pattern &optional (start 0))
51 (ps:for ((i start)) ((< i (api-call array-size name))) ((incf i))
59 (multiple-value-bind (real-name slot)
52 (when (funcall (ps:getprop (api-call get-var name i) 'match) pattern)
60 (api-call var-real-name name)
53 (return i)))
61 (ps:for ((i start)) ((< i (api-call array-size name))) ((incf i))
62 (when (funcall (ps:getprop (api-call get-var real-name i slot) 'match) pattern)
63 (return i))))
54 -1)
64 -1)
55
65
56 ;;; 6str
66 ;;; 6str
57
67
58 (defm (root lib instr) (s subs &optional (start 1))
68 (defm (root lib instr) (s subs &optional (start 1))
59 (+ start (ps:chain s (substring (- start 1)) (search subs))))
69 (+ start (ps:chain s (substring (- start 1)) (search subs))))
60
70
61 (defm (root lib isnum) (s)
71 (defm (root lib isnum) (s)
62 (if (is-na-n s)
72 (if (is-na-n s)
63 0
73 0
64 -1))
74 -1))
65
75
66 (defm (root lib strcomp) (s pattern)
76 (defm (root lib strcomp) (s pattern)
67 (if (s.match pattern)
77 (if (s.match pattern)
68 -1
78 -1
69 0))
79 0))
70
80
71 (defm (root lib strfind) (s pattern group)
81 (defm (root lib strfind) (s pattern group)
72 (let* ((re (ps:new (*reg-exp pattern)))
82 (let* ((re (ps:new (*reg-exp pattern)))
73 (match (re.exec s)))
83 (match (re.exec s)))
74 (match.group group)))
84 (match.group group)))
75
85
76 (defm (root lib strpos) (s pattern &optional (group 0))
86 (defm (root lib strpos) (s pattern &optional (group 0))
77 (let* ((re (ps:new (*reg-exp pattern)))
87 (let* ((re (ps:new (*reg-exp pattern)))
78 (match (re.exec s))
88 (match (re.exec s))
79 (found (match.group group)))
89 (found (match.group group)))
80 (if found
90 (if found
81 (s.search found)
91 (s.search found)
82 0)))
92 0)))
83
93
84 ;;; 7if
94 ;;; 7if
85
95
86 ;; Has to be a function because it always evaluates all three of its
96 ;; Has to be a function because it always evaluates all three of its
87 ;; arguments
97 ;; arguments
88 (defm (root lib iif) (cond-expr then-expr else-expr)
98 (defm (root lib iif) (cond-expr then-expr else-expr)
89 (if cond-expr then-expr else-expr))
99 (if cond-expr then-expr else-expr))
90
100
91 ;;; 8sub
101 ;;; 8sub
92
102
93 (defm (root lib gosub) (target &rest args)
103 (defm (root lib gosub) (target &rest args)
94 (conserving-vars (__funcall args result)
104 (funcall (ps:getprop (root locs) target) args)
95 (api-call init-args args)
105 (values))
96 (funcall (ps:getprop (root locs) target))
97 (values)))
98
106
99 (defm (root lib func) (target &rest args)
107 (defm (root lib func) (target &rest args)
100 (conserving-vars (__funcall args result)
108 (funcall (ps:getprop (root locs) target) args))
101 (api-call init-args args)
102 (funcall (ps:getprop (root locs) target))
103 (api-call get-result)))
104
109
105 ;;; 9loops
110 ;;; 9loops
106
111
107 ;;; 10dynamic
112 ;;; 10dynamic
108
113
109 (defm (root lib dyneval) (block &rest args)
114 (defm (root lib dynamic) (block &rest args)
110 (conserving-vars (__funcall args result)
115 (funcall block args)
111 (api-call init-args args)
116 (values))
112 (funcall block)
113 (api-call get-result)))
114
117
115 (defm (root lib dynamic) (&rest args)
118 (defm (root lib dyneval) (block &rest args)
116 (conserving-vars (__funcall args result)
119 (funcall block args))
117 (api-call init-args args)
118 (funcall block)
119 (values)))
120
120
121 ;;; 11main
121 ;;; 11main
122
122
123 (defm (root lib main-p) (s)
123 (defm (root lib main-p) (s)
124 (api-call add-text :main s)
124 (api-call add-text :main s)
125 (values))
125 (values))
126
126
127 (defm (root lib main-pl) (s)
127 (defm (root lib main-pl) (s)
128 (api-call add-text :main s)
128 (api-call add-text :main s)
129 (api-call newline :main)
129 (api-call newline :main)
130 (values))
130 (values))
131
131
132 (defm (root lib main-nl) (s)
132 (defm (root lib main-nl) (s)
133 (api-call newline :main)
133 (api-call newline :main)
134 (api-call add-text :main s)
134 (api-call add-text :main s)
135 (values))
135 (values))
136
136
137 (defm (root lib maintxt) (s)
137 (defm (root lib maintxt) (s)
138 (api-call get-text :main)
138 (api-call get-text :main)
139 (values))
139 (values))
140
140
141 ;; For clarity (it leaves a lib.desc() call in JS)
141 ;; For clarity (it leaves a lib.desc() call in JS)
142 (defm (root lib desc) (s)
142 (defm (root lib desc) (s)
143 "")
143 "")
144
144
145 (defm (root lib main-clear) ()
145 (defm (root lib main-clear) ()
146 (api-call clear-text :main)
146 (api-call clear-text :main)
147 (values))
147 (values))
148
148
149 ;;; 12stat
149 ;;; 12stat
150
150
151 (defm (root lib stat-p) (s)
151 (defm (root lib stat-p) (s)
152 (api-call add-text :stat s)
152 (api-call add-text :stat s)
153 (values))
153 (values))
154
154
155 (defm (root lib stat-pl) (s)
155 (defm (root lib stat-pl) (s)
156 (api-call add-text :stat s)
156 (api-call add-text :stat s)
157 (api-call newline :stat)
157 (api-call newline :stat)
158 (values))
158 (values))
159
159
160 (defm (root lib stat-nl) (s)
160 (defm (root lib stat-nl) (s)
161 (api-call newline :stat)
161 (api-call newline :stat)
162 (api-call add-text :stat s)
162 (api-call add-text :stat s)
163 (values))
163 (values))
164
164
165 (defm (root lib stattxt) (s)
165 (defm (root lib stattxt) (s)
166 (api-call get-text :stat)
166 (api-call get-text :stat)
167 (values))
167 (values))
168
168
169 (defm (root lib stat-clear) ()
169 (defm (root lib stat-clear) ()
170 (api-call clear-text :stat)
170 (api-call clear-text :stat)
171 (values))
171 (values))
172
172
173 (defm (root lib cls) ()
173 (defm (root lib cls) ()
174 (funcall (root lib stat-clear))
174 (funcall (root lib stat-clear))
175 (funcall (root lib main-clear))
175 (funcall (root lib main-clear))
176 (funcall (root lib cla))
176 (funcall (root lib cla))
177 (funcall (root lib cmdclear))
177 (funcall (root lib cmdclear))
178 (values))
178 (values))
179
179
180 ;;; 13diag
180 ;;; 13diag
181
181
182 ;;; 14act
182 ;;; 14act
183
183
184 (defm (root lib curacts) ()
184 (defm (root lib curacts) ()
185 (let ((acts (root acts)))
185 (let ((acts (root acts)))
186 (lambda ()
186 (lambda ()
187 (setf (root acts) acts)
187 (setf (root acts) acts)
188 (values))))
188 (values))))
189
189
190 ;;; 15objs
190 ;;; 15objs
191
191
192 (defm (root lib addobj) (name)
192 (defm (root lib addobj) (name)
193 (ps:chain (root objs) (push name))
193 (ps:chain (root objs) (push name))
194 (api-call update-objs)
194 (api-call update-objs)
195 (values))
195 (values))
196
196
197 (defm (root lib delobj) (name)
197 (defm (root lib delobj) (name)
198 (let ((index (ps:chain (root objs) (index-of name))))
198 (let ((index (ps:chain (root objs) (index-of name))))
199 (when (> index -1)
199 (when (> index -1)
200 (funcall (root lib killobj) (1+ index))))
200 (funcall (root lib killobj) (1+ index))))
201 (values))
201 (values))
202
202
203 (defm (root lib killobj) (&optional (num nil))
203 (defm (root lib killobj) (&optional (num nil))
204 (if (eq nil num)
204 (if (eq nil num)
205 (setf (root objs) (list))
205 (setf (root objs) (list))
206 (ps:chain (root objs) (splice (1- num) 1)))
206 (ps:chain (root objs) (splice (1- num) 1)))
207 (api-call update-objs)
207 (api-call update-objs)
208 (values))
208 (values))
209
209
210 ;;; 16menu
210 ;;; 16menu
211
211
212 (defm (root lib menu) (menu-name)
212 (defm (root lib menu) (menu-name)
213 (let ((menu-data (list)))
213 (let ((menu-data (list)))
214 (loop :for item :in (api-call get-array (api-call var-real-name menu-name))
214 (loop :for item :in (api-call get-array (api-call var-real-name menu-name))
215 :do (cond ((string= item "")
215 :do (cond ((string= item "")
216 (break))
216 (break))
217 ((string= item "-:-")
217 ((string= item "-:-")
218 (ps:chain menu-data (push :delimiter)))
218 (ps:chain menu-data (push :delimiter)))
219 (t
219 (t
220 (let* ((tokens (ps:chain item (split ":"))))
220 (let* ((tokens (ps:chain item (split ":"))))
221 (when (= (length tokens) 2)
221 (when (= (length tokens) 2)
222 (tokens.push ""))
222 (tokens.push ""))
223 (let* ((text (ps:chain tokens (splice 0 (- tokens.length 2)) (join ":")))
223 (let* ((text (ps:chain tokens (splice 0 (- tokens.length 2)) (join ":")))
224 (loc (ps:getprop tokens (- tokens.length 2)))
224 (loc (ps:getprop tokens (- tokens.length 2)))
225 (icon (ps:getprop tokens (- tokens.length 1))))
225 (icon (ps:getprop tokens (- tokens.length 1))))
226 (ps:chain menu-data
226 (ps:chain menu-data
227 (push (ps:create text text
227 (push (ps:create text text
228 loc loc
228 loc loc
229 icon icon))))))))
229 icon icon))))))))
230 (api-call menu menu-data)
230 (api-call menu menu-data)
231 (values)))
231 (values)))
232
232
233 ;;; 17sound
233 ;;; 17sound
234
234
235 (defm (root lib play) (filename &optional (volume 100))
235 (defm (root lib play) (filename &optional (volume 100))
236 (let ((audio (ps:new (*audio filename))))
236 (let ((audio (ps:new (*audio filename))))
237 (setf (ps:getprop (root playing) filename) audio)
237 (setf (ps:getprop (root playing) filename) audio)
238 (setf (ps:@ audio volume) (* volume 0.01))
238 (setf (ps:@ audio volume) (* volume 0.01))
239 (ps:chain audio (play))))
239 (ps:chain audio (play))))
240
240
241 (defm (root lib close) (filename)
241 (defm (root lib close) (filename)
242 (funcall (root playing filename) stop)
242 (funcall (root playing filename) stop)
243 (ps:delete (root playing filename)))
243 (ps:delete (root playing filename)))
244
244
245 (defm (root lib closeall) ()
245 (defm (root lib closeall) ()
246 (loop :for k :in (*object.keys (root playing))
246 (loop :for k :in (*object.keys (root playing))
247 :for v := (ps:getprop (root playing) k)
247 :for v := (ps:getprop (root playing) k)
248 :do (funcall v stop))
248 :do (funcall v stop))
249 (setf (root playing) (ps:create)))
249 (setf (root playing) (ps:create)))
250
250
251 ;;; 18img
251 ;;; 18img
252
252
253 (defm (root lib refint) ())
253 (defm (root lib refint) ())
254
254
255 (defm (root lib view) ())
255 (defm (root lib view) ())
256
256
257 ;;; 19input
257 ;;; 19input
258
258
259 (defm (root lib showinput) ())
259 (defm (root lib showinput) ())
260
260
261 (defm (root lib usertxt) ())
261 (defm (root lib usertxt) ())
262
262
263 (defm (root lib cmdclear) ())
263 (defm (root lib cmdclear) ())
264
264
265 (defm (root lib input) ())
265 (defm (root lib input) ())
266
266
267 ;;; 20time
267 ;;; 20time
268
268
269 ;; I wonder if there's a better solution than busy-wait
269 ;; I wonder if there's a better solution than busy-wait
270 (defm (root lib wait) (msec)
270 (defm (root lib wait) (msec)
271 (let* ((now (ps:new (*date)))
271 (let* ((now (ps:new (*date)))
272 (exit-time (+ (funcall now.get-time) msec)))
272 (exit-time (+ (funcall now.get-time) msec)))
273 (loop :while (< (funcall now.get-time) exit-time))))
273 (loop :while (< (funcall now.get-time) exit-time))))
274
274
275 (defm (root lib msecscount) ())
275 (defm (root lib msecscount) ())
276
276
277 (defm (root lib settimer) ())
277 (defm (root lib settimer) ())
278
278
279 ;;; 21local
279 ;;; 21local
280
280
281 ;;; misc
281 ;;; misc
282
282
283 (defm (root lib rgb) ())
283 (defm (root lib rgb) ())
284
284
285 (defm (root lib openqst) ())
285 (defm (root lib openqst) ())
286
286
287 (defm (root lib addqst) ())
287 (defm (root lib addqst) ())
288
288
289 (defm (root lib killqst) ())
289 (defm (root lib killqst) ())
290
290
291 (defm (root lib opengame) (&optional filename)
291 (defm (root lib opengame) (&optional filename)
292 (let ((element (document.create-element :input)))
292 (let ((element (document.create-element :input)))
293 (element.set-attribute :type :file)
293 (element.set-attribute :type :file)
294 (element.set-attribute :id :qsp-opengame)
294 (element.set-attribute :id :qsp-opengame)
295 (element.set-attribute :tabindex -1)
295 (element.set-attribute :tabindex -1)
296 (element.set-attribute "aria-hidden" t)
296 (element.set-attribute "aria-hidden" t)
297 (setf element.style.display :block)
297 (setf element.style.display :block)
298 (setf element.style.visibility :hidden)
298 (setf element.style.visibility :hidden)
299 (setf element.style.position :fixed)
299 (setf element.style.position :fixed)
300 (setf element.onchange
300 (setf element.onchange
301 (lambda (event)
301 (lambda (event)
302 (let* ((file (elt event.target.files 0))
302 (let* ((file (elt event.target.files 0))
303 (reader (ps:new (*file-reader))))
303 (reader (ps:new (*file-reader))))
304 (setf reader.onload
304 (setf reader.onload
305 (lambda (ev)
305 (lambda (ev)
306 (block nil
306 (block nil
307 (let ((target ev.current-target))
307 (let ((target ev.current-target))
308 (unless target.result
308 (unless target.result
309 (return))
309 (return))
310 (api-call base64-to-state target.result)))))
310 (api-call base64-to-state target.result)
311 (api-call unstash-state)))))
311 (reader.read-as-text file))))
312 (reader.read-as-text file))))
312 (document.body.append-child element)
313 (document.body.append-child element)
313 (element.click)
314 (element.click)
314 (document.body.remove-child element)))
315 (document.body.remove-child element)))
315
316
316 (defm (root lib savegame) (&optional filename)
317 (defm (root lib savegame) (&optional filename)
317 (let ((element (document.create-element :a)))
318 (let ((element (document.create-element :a)))
318 (element.set-attribute :href (+ "data:text/plain;charset=utf-8," (api-call state-to-base64)))
319 (element.set-attribute :href (+ "data:text/plain;charset=utf-8," (api-call state-to-base64)))
319 (element.set-attribute :download "savegame.sav")
320 (element.set-attribute :download "savegame.sav")
320 (setf element.style.display :none)
321 (setf element.style.display :none)
321 (document.body.append-child element)
322 (document.body.append-child element)
322 (element.click)
323 (element.click)
323 (document.body.remove-child element)))
324 (document.body.remove-child element)))
@@ -1,34 +1,35 b''
1
1
2 (in-package sugar-qsp)
2 (in-package sugar-qsp)
3
3
4 (setf (root)
4 (setf (root)
5 (ps:create
5 (ps:create
6 ;;; Game session state
6 ;;; Game session state
7 ;; Variables
7 ;; Variables
8 vars (ps:create)
8 vars (ps:create)
9 ;; Inventory (objects)
9 ;; Inventory (objects)
10 objs (list)
10 objs (list)
11 ;;; Transient state
11 ;;; Transient state
12 ;; Savegame data
12 ;; Savegame data
13 state-stash (ps:create)
13 state-stash (ps:create)
14 ;; List of audio files being played
14 ;; List of audio files being played
15 playing (ps:create)
15 playing (ps:create)
16 ;; Local variables stack (starts with an empty frame)
16 ;; Local variables stack (starts with an empty frame)
17 locals (list)
17 locals (list)
18 ;;; Game data
18 ;;; Game data
19 ;; ACTions
19 ;; ACTions
20 acts (ps:create)
20 acts (ps:create)
21 ;; Locations
21 ;; Locations
22 locs (ps:create)))
22 locs (ps:create)))
23
23
24 ;; Launch the game from the first location
24 ;; Launch the game from the first location
25 (setf window.onload
25 (setf window.onload
26 (lambda ()
26 (lambda ()
27 (funcall (ps:getprop (root locs)
27 (funcall (ps:getprop (root locs)
28 (ps:chain *object (keys (root locs)) 0)))
28 (ps:chain *object (keys (root locs)) 0))
29 (list))
29 (values)))
30 (values)))
30
31
31 ;; Close the dropdown on any click
32 ;; Close the dropdown on any click
32 (setf window.onclick
33 (setf window.onclick
33 (lambda (event)
34 (lambda (event)
34 (setf (ps:@ (api-call get-frame :dropdown) style display) "none")))
35 (setf (ps:@ (api-call get-frame :dropdown) style display) "none")))
@@ -1,586 +1,587 b''
1
1
2 (in-package sugar-qsp)
2 (in-package sugar-qsp)
3
3
4 ;;;; Parses TXT source to an intermediate representation
4 ;;;; Parses TXT source to an intermediate representation
5
5
6 ;;; Utility
6 ;;; Utility
7
7
8 (defun remove-nth (list nth)
8 (defun remove-nth (list nth)
9 (append (subseq list 0 nth)
9 (append (subseq list 0 nth)
10 (subseq list (1+ nth))))
10 (subseq list (1+ nth))))
11
11
12 (defun not-quote (char)
12 (defun not-quote (char)
13 (not (eql #\' char)))
13 (not (eql #\' char)))
14
14
15
15
16 (defun not-doublequote (char)
16 (defun not-doublequote (char)
17 (not (eql #\" char)))
17 (not (eql #\" char)))
18
18
19 (defun not-brace (char)
19 (defun not-brace (char)
20 (not (eql #\} char)))
20 (not (eql #\} char)))
21
21
22 (defun not-integer (string)
22 (defun not-integer (string)
23 (when (find-if-not #'digit-char-p string)
23 (when (find-if-not #'digit-char-p string)
24 t))
24 t))
25
25
26 (defun not-newline (char)
26 (defun not-newline (char)
27 (not (eql #\newline char)))
27 (not (eql #\newline char)))
28
28
29 (defun id-any-char (char)
29 (defun id-any-char (char)
30 (and
30 (and
31 (not (digit-char-p char))
31 (not (digit-char-p char))
32 (not (eql #\newline char))
32 (not (eql #\newline char))
33 (not (find char " !:&=<>+-*/,'\"()[]{}"))))
33 (not (find char " !:&=<>+-*/,'\"()[]{}"))))
34
34
35 (defun intern-first (list)
35 (defun intern-first (list)
36 (list* (intern (string-upcase (first list)))
36 (list* (intern (string-upcase (first list)))
37 (rest list)))
37 (rest list)))
38
38
39 (eval-when (:compile-toplevel :load-toplevel :execute)
39 (eval-when (:compile-toplevel :load-toplevel :execute)
40 (defun remove-nil (list)
40 (defun remove-nil (list)
41 (remove nil list)))
41 (remove nil list)))
42
42
43 (defun binop-rest (list)
43 (defun binop-rest (list)
44 (destructuring-bind (ws1 operator ws2 operand2)
44 (destructuring-bind (ws1 operator ws2 operand2)
45 list
45 list
46 (declare (ignore ws1 ws2))
46 (declare (ignore ws1 ws2))
47 (list (intern (string-upcase operator)) operand2)))
47 (list (intern (string-upcase operator)) operand2)))
48
48
49 (defun do-binop% (left-op other-ops)
49 (defun do-binop% (left-op other-ops)
50 (if (null other-ops)
50 (if (null other-ops)
51 left-op
51 left-op
52 (destructuring-bind ((operator right-op) &rest rest-ops)
52 (destructuring-bind ((operator right-op) &rest rest-ops)
53 other-ops
53 other-ops
54 (if (and (listp left-op)
54 (if (and (listp left-op)
55 (eq (first left-op)
55 (eq (first left-op)
56 operator))
56 operator))
57 (do-binop% (append left-op (list right-op)) rest-ops)
57 (do-binop% (append left-op (list right-op)) rest-ops)
58 (do-binop% (list operator left-op right-op) rest-ops)))))
58 (do-binop% (list operator left-op right-op) rest-ops)))))
59
59
60 (defun do-binop (list)
60 (defun do-binop (list)
61 (destructuring-bind (left-op rest-ops)
61 (destructuring-bind (left-op rest-ops)
62 list
62 list
63 (do-binop% left-op
63 (do-binop% left-op
64 (mapcar #'binop-rest rest-ops))))
64 (mapcar #'binop-rest rest-ops))))
65
65
66 (p:defrule line-continuation (and #\_ #\newline)
66 (p:defrule line-continuation (and #\_ #\newline)
67 (:constant nil))
67 (:constant nil))
68
68
69 (p:defrule text-spaces (+ (or #\space #\tab line-continuation))
69 (p:defrule text-spaces (+ (or #\space #\tab line-continuation))
70 (:text t))
70 (:text t))
71
71
72 (p:defrule spaces (+ (or #\space #\tab line-continuation))
72 (p:defrule spaces (+ (or #\space #\tab line-continuation))
73 (:constant nil))
73 (:constant nil))
74
74
75 (p:defrule spaces? (* (or #\space #\tab line-continuation))
75 (p:defrule spaces? (* (or #\space #\tab line-continuation))
76 (:constant nil))
76 (:constant nil))
77
77
78 (p:defrule colon #\:
78 (p:defrule colon #\:
79 (:constant nil))
79 (:constant nil))
80
80
81 (p:defrule alphanumeric (alphanumericp character))
81 (p:defrule alphanumeric (alphanumericp character))
82
82
83 (p:defrule not-newline (not-newline character))
83 (p:defrule not-newline (not-newline character))
84
84
85 (p:defrule squote-esc "''"
85 (p:defrule squote-esc "''"
86 (:lambda (list)
86 (:lambda (list)
87 (p:text (elt list 0))))
87 (p:text (elt list 0))))
88
88
89 (p:defrule dquote-esc "\"\""
89 (p:defrule dquote-esc "\"\""
90 (:lambda (list)
90 (:lambda (list)
91 (p:text (elt list 0))))
91 (p:text (elt list 0))))
92
92
93 (p:defrule sstring-chars (+ (and (and (p:! "<<") (p:! (p:~ "\"exec:")))
93 (p:defrule sstring-chars (+ (and (and (p:! "<<") (p:! (p:~ "\"exec:")))
94 (or squote-esc (not-quote character))))
94 (or squote-esc (not-quote character))))
95 (:lambda (list)
95 (:lambda (list)
96 (p:text (mapcar #'second list))))
96 (p:text (mapcar #'second list))))
97
97
98 (p:defrule dstring-chars (+ (and (and (p:! "<<") (p:! (p:~ "'exec:")))
98 (p:defrule dstring-chars (+ (and (and (p:! "<<") (p:! (p:~ "'exec:")))
99 (or dquote-esc (not-doublequote character))))
99 (or dquote-esc (not-doublequote character))))
100 (:lambda (list)
100 (:lambda (list)
101 (p:text (mapcar #'second list))))
101 (p:text (mapcar #'second list))))
102
102
103 ;;; Identifiers
103 ;;; Identifiers
104
104
105 ;; From the official docs
105 ;; From the official docs
106 (defparameter *keywords* '(act addlib addobj addqst and arrcomp arrpos arrsize cla clear *clear close clr *clr cls cmdclear cmdclr copyarr counter countobj curacts curloc debug delact dellib delobj desc disablescroll disablesubex dynamic dyneval else elseif end exit fcolor fname freelib fsize func getobj gosub goto gs gt if iif inclib input instr isnum isplay jump killall killobj killqst killvar lcase lcolor len let loc local maintxt max menu mid min mod msecscount msg nl *nl no nosave obj onactsel ongload ongsave onnewloc onobjadd onobjdel onobjsel opengame openqst or p *p pl *pl play qspver rand refint replace rgb rnd savegame selact selobj set settimer showacts showinput showobjs showstat stattxt str strcomp strfind strpos trim ucase unsel unselect usercom user_text usrtxt val view wait xgoto xgt))
106 (defparameter *keywords* '(act addlib addobj addqst and arrcomp arrpos arrsize cla clear *clear close clr *clr cls cmdclear cmdclr copyarr counter countobj curacts curloc debug delact dellib delobj desc disablescroll disablesubex dynamic dyneval else elseif end exit fcolor fname freelib fsize func getobj gosub goto gs gt if iif inclib input instr isnum isplay jump killall killobj killqst killvar lcase lcolor len let loc local maintxt max menu mid min mod msecscount msg nl *nl no nosave obj onactsel ongload ongsave onnewloc onobjadd onobjdel onobjsel opengame openqst or p *p pl *pl play qspver rand refint replace rgb rnd savegame selact selobj set settimer showacts showinput showobjs showstat stattxt str strcomp strfind strpos trim ucase unsel unselect usercom user_text usrtxt val view wait xgoto xgt))
107
107
108 (defun trim-$ (str)
108 (defun trim-$ (str)
109 (if (char= #\$ (elt str 0))
109 (if (char= #\$ (elt str 0))
110 (subseq str 1)
110 (subseq str 1)
111 str))
111 str))
112
112
113 (defun qsp-keyword-p (id)
113 (defun qsp-keyword-p (id)
114 (member (intern (trim-$ (string-upcase id))) *keywords*))
114 (member (intern (trim-$ (string-upcase id))) *keywords*))
115
115
116 (defun not-qsp-keyword-p (id)
116 (defun not-qsp-keyword-p (id)
117 (not (member (intern (trim-$ (string-upcase id))) *keywords*)))
117 (not (member (intern (trim-$ (string-upcase id))) *keywords*)))
118
118
119 (p:defrule qsp-keyword (qsp-keyword-p identifier-raw))
119 (p:defrule qsp-keyword (qsp-keyword-p identifier-raw))
120
120
121 (p:defrule id-first (id-any-char character))
121 (p:defrule id-first (id-any-char character))
122 (p:defrule id-next (or (id-any-char character)
122 (p:defrule id-next (or (id-any-char character)
123 (digit-char-p character)))
123 (digit-char-p character)))
124 (p:defrule identifier-raw (and id-first (* id-next))
124 (p:defrule identifier-raw (and id-first (* id-next))
125 (:lambda (list)
125 (:lambda (list)
126 (intern (string-upcase (p:text list)))))
126 (intern (string-upcase (p:text list)))))
127
127
128 (p:defrule identifier (not-qsp-keyword-p identifier-raw))
128 (p:defrule identifier (not-qsp-keyword-p identifier-raw))
129
129
130 ;;; Strings
130 ;;; Strings
131
131
132 (p:defrule qsp-string (or normal-string brace-string))
132 (p:defrule qsp-string (or normal-string brace-string))
133
133
134 (p:defrule normal-string (or sstring dstring)
134 (p:defrule normal-string (or sstring dstring)
135 (:lambda (str)
135 (:lambda (str)
136 (list* 'str (or str (list "")))))
136 (list* 'str (or str (list "")))))
137
137
138 (p:defrule sstring (and #\' (* (or string-interpol
138 (p:defrule sstring (and #\' (* (or string-interpol
139 sstring-exec
139 sstring-exec
140 sstring-chars))
140 sstring-chars))
141 #\')
141 #\')
142 (:function second))
142 (:function second))
143
143
144 (p:defrule dstring (and #\" (* (or string-interpol
144 (p:defrule dstring (and #\" (* (or string-interpol
145 dstring-exec
145 dstring-exec
146 dstring-chars))
146 dstring-chars))
147 #\")
147 #\")
148 (:function second))
148 (:function second))
149
149
150 (p:defrule string-interpol (and "<<" expression ">>")
150 (p:defrule string-interpol (and "<<" expression ">>")
151 (:function second))
151 (:function second))
152
152
153 (p:defrule sstring-exec-body (+ (or squote-esc (not-doublequote character)))
153 (p:defrule sstring-exec-body (+ (or squote-esc (not-doublequote character)))
154 (:text t))
154 (:text t))
155
155
156 (p:defrule dstring-exec-body (+ (or dquote-esc (not-quote character)))
156 (p:defrule dstring-exec-body (+ (or dquote-esc (not-quote character)))
157 (:text t))
157 (:text t))
158
158
159 (p:defrule sstring-exec (and (p:~ "\"exec:") sstring-exec-body #\")
159 (p:defrule sstring-exec (and (p:~ "\"exec:") sstring-exec-body #\")
160 (:lambda (list)
160 (:lambda (list)
161 (list* 'exec (p:parse 'exec-body (second list)))))
161 (list* 'exec (p:parse 'exec-body (second list)))))
162
162
163 (p:defrule dstring-exec (and (p:~ "'exec:") dstring-exec-body #\')
163 (p:defrule dstring-exec (and (p:~ "'exec:") dstring-exec-body #\')
164 (:lambda (list)
164 (:lambda (list)
165 (list* 'exec (p:parse 'exec-body (second list)))))
165 (list* 'exec (p:parse 'exec-body (second list)))))
166
166
167 (p:defrule brace-string (and #\{ before-statement block-body #\})
167 (p:defrule brace-string (and #\{ before-statement block-body #\})
168 (:lambda (list)
168 (:lambda (list)
169 (list* 'qspblock (third list))))
169 (list* 'qspblock (third list))))
170
170
171 ;;; Location
171 ;;; Location
172
172
173 (p:defrule sugar-qsp-grammar (and (* (or spaces #\newline))
173 (p:defrule sugar-qsp-grammar (and (* (or spaces #\newline))
174 (* location))
174 (* location))
175 (:function second))
175 (:function second))
176
176
177 (p:defrule location (and location-header block-body location-end)
177 (p:defrule location (and location-header block-body location-end)
178 (:destructure (header body end)
178 (:destructure (header body end)
179 (declare (ignore end))
179 (declare (ignore end))
180 `(location (,header) ,@body)))
180 `(location (,header) ,@body)))
181
181
182 (p:defrule location-header (and #\#
182 (p:defrule location-header (and #\#
183 (+ not-newline)
183 (+ not-newline)
184 (and #\newline spaces? before-statement))
184 (and #\newline spaces? before-statement))
185 (:destructure (spaces1 name spaces2)
185 (:destructure (spaces1 name spaces2)
186 (declare (ignore spaces1 spaces2))
186 (declare (ignore spaces1 spaces2))
187 (string-upcase (string-trim " " (p:text name)))))
187 (string-upcase (string-trim " " (p:text name)))))
188
188
189 (p:defrule location-end (and #\- (* not-newline) #\newline before-statement)
189 (p:defrule location-end (and #\- (* not-newline) #\newline before-statement)
190 (:constant nil))
190 (:constant nil))
191
191
192 ;;; Block body
192 ;;; Block body
193
193
194 (p:defrule newline-block-body (and #\newline spaces? block-body)
194 (p:defrule newline-block-body (and #\newline spaces? block-body)
195 (:function third))
195 (:function third))
196
196
197 (p:defrule block-body (* statement)
197 (p:defrule block-body (* statement)
198 (:function remove-nil))
198 (:function remove-nil))
199
199
200 ;; Just for <a href="exec:...'>
200 ;; Just for <a href="exec:...'>
201 ;; Explicitly called from that rule's production
201 ;; Explicitly called from that rule's production
202 (p:defrule exec-body (and before-statement line-body)
202 (p:defrule exec-body (and before-statement line-body)
203 (:function second))
203 (:function second))
204
204
205 (p:defrule line-body (and inline-statement (* next-inline-statement))
205 (p:defrule line-body (and inline-statement (* next-inline-statement))
206 (:lambda (list)
206 (:lambda (list)
207 (list* (first list) (second list))))
207 (list* (first list) (second list))))
208
208
209 (p:defrule before-statement (* (or #\newline spaces))
209 (p:defrule before-statement (* (or #\newline spaces))
210 (:constant nil))
210 (:constant nil))
211
211
212 (p:defrule statement-end (or statement-end-real statement-end-block-close))
212 (p:defrule statement-end (or statement-end-real statement-end-block-close))
213
213
214 (p:defrule statement-end-real (and (or #\newline
214 (p:defrule statement-end-real (and (or #\newline
215 (and #\& spaces? (p:& statement%)))
215 (and #\& spaces? (p:& statement%)))
216 before-statement)
216 before-statement)
217 (:constant nil))
217 (:constant nil))
218
218
219 (p:defrule statement-end-block-close (or (p:& #\}))
219 (p:defrule statement-end-block-close (or (p:& #\}))
220 (:constant nil))
220 (:constant nil))
221
221
222 (p:defrule inline-statement (and statement% spaces?)
222 (p:defrule inline-statement (and statement% spaces?)
223 (:function first))
223 (:function first))
224
224
225 (p:defrule next-inline-statement (and #\& spaces? inline-statement)
225 (p:defrule next-inline-statement (and #\& spaces? inline-statement)
226 (:function third))
226 (:function third))
227
227
228 (p:defrule not-a-non-statement (and (p:! (p:~ "elseif"))
228 (p:defrule not-a-non-statement (and (p:! (p:~ "elseif"))
229 (p:! (p:~ "else"))
229 (p:! (p:~ "else"))
230 (p:! (p:~ "end"))))
230 (p:! (p:~ "end"))))
231
231
232 (p:defrule statement (and inline-statement statement-end)
232 (p:defrule statement (and inline-statement statement-end)
233 (:function first))
233 (:function first))
234
234
235 (p:defrule statement% (and not-a-non-statement
235 (p:defrule statement% (and not-a-non-statement
236 (or label comment string-output
236 (or label comment string-output
237 block non-returning-intrinsic local
237 block non-returning-intrinsic local
238 assignment expression-output))
238 assignment expression-output))
239 (:function second))
239 (:function second))
240
240
241 (p:defrule expr-stopper (or comment block non-returning-intrinsic))
241 (p:defrule expr-stopper (or comment block non-returning-intrinsic))
242
242
243 (p:defrule string-output qsp-string
243 (p:defrule string-output qsp-string
244 (:lambda (string)
244 (:lambda (string)
245 (list 'main-pl string)))
245 (list 'main-pl string)))
246
246
247 (p:defrule expression-output expression
247 (p:defrule expression-output expression
248 (:lambda (list)
248 (:lambda (list)
249 (list 'main-pl list)))
249 (list 'main-pl list)))
250
250
251 (p:defrule label (and colon identifier)
251 (p:defrule label (and colon identifier)
252 (:lambda (list)
252 (:lambda (list)
253 (intern (string (second list)) :keyword)))
253 (intern (string (second list)) :keyword)))
254
254
255 (p:defrule comment (and #\! (* (or qsp-string brace-comment text-spaces not-newline)))
255 (p:defrule comment (and #\! (* (or qsp-string brace-comment text-spaces not-newline)))
256 (:constant nil))
256 (:constant nil))
257
257
258 (p:defrule brace-comment (and #\{ (* (not-brace character)) #\})
258 (p:defrule brace-comment (and #\{ (* (not-brace character)) #\})
259 (:constant nil))
259 (:constant nil))
260
260
261 (p:defrule local (and (p:~ "local") spaces variable (p:? (and spaces? #\= spaces? expression)))
261 (p:defrule local (and (p:~ "local") spaces variable (p:? (and spaces? #\= spaces? expression)))
262 (:lambda (list)
262 (:lambda (list)
263 (list* 'local (third list)
263 (list* 'local (third list)
264 (when (fourth list)
264 (when (fourth list)
265 (list (fourth (fourth list)))))))
265 (list (fourth (fourth list)))))))
266
266
267 ;;; Blocks
267 ;;; Blocks
268
268
269 (p:defrule block (or block-act block-if))
269 (p:defrule block (or block-act block-if))
270
270
271 (p:defrule block-if (and block-if-head block-if-body)
271 (p:defrule block-if (and block-if-head block-if-body)
272 (:destructure (head body)
272 (:destructure (head body)
273 `(qspcond (,@head ,@(first body))
273 `(qspcond (,@head ,@(first body))
274 ,@(rest body))))
274 ,@(rest body))))
275
275
276 (p:defrule block-if-head (and (p:~ "if") spaces expression spaces? colon spaces?)
276 (p:defrule block-if-head (and (p:~ "if") spaces expression spaces? colon spaces?)
277 (:function remove-nil)
277 (:function remove-nil)
278 (:function cdr))
278 (:function cdr))
279
279
280 (p:defrule block-if-body (or block-if-ml block-if-sl)
280 (p:defrule block-if-body (or block-if-ml block-if-sl)
281 (:destructure (if-body elseifs else &rest ws)
281 (:destructure (if-body elseifs else &rest ws)
282 (declare (ignore ws))
282 (declare (ignore ws))
283 `(,(remove-nil if-body) ,@elseifs ,@(when else `((else ,@else))))))
283 `(,(remove-nil if-body) ,@elseifs ,@(when else `((else ,@else))))))
284
284
285 (p:defrule block-if-sl (and line-body
285 (p:defrule block-if-sl (and line-body
286 (p:? block-if-elseif-inline)
286 (p:? block-if-elseif-inline)
287 (p:? block-if-else-inline)
287 (p:? block-if-else-inline)
288 spaces?))
288 spaces?))
289
289
290 (p:defrule block-if-ml (and (and #\newline spaces?)
290 (p:defrule block-if-ml (and (and #\newline spaces?)
291 block-body
291 block-body
292 (p:? block-if-elseif)
292 (p:? block-if-elseif)
293 (p:? block-if-else)
293 (p:? block-if-else)
294 block-if-end)
294 block-if-end)
295 (:lambda (list)
295 (:lambda (list)
296 (cdr list)))
296 (cdr list)))
297
297
298 (p:defrule block-if-elseif-inline (and block-if-elseif-head line-body (p:? block-if-elseif-inline))
298 (p:defrule block-if-elseif-inline (and block-if-elseif-head line-body (p:? block-if-elseif-inline))
299 (:destructure (head statements elseif)
299 (:destructure (head statements elseif)
300 `((,@(rest head) ,@(remove-nil statements)) ,@elseif)))
300 `((,@(rest head) ,@(remove-nil statements)) ,@elseif)))
301
301
302 (p:defrule block-if-elseif (and block-if-elseif-head statement-end block-body (p:? block-if-elseif))
302 (p:defrule block-if-elseif (and block-if-elseif-head statement-end block-body (p:? block-if-elseif))
303 (:destructure (head ws statements elseif)
303 (:destructure (head ws statements elseif)
304 (declare (ignore ws))
304 (declare (ignore ws))
305 `((,@(rest head) ,@(remove-nil statements)) ,@elseif)))
305 `((,@(rest head) ,@(remove-nil statements)) ,@elseif)))
306
306
307 (p:defrule block-if-elseif-head (and (p:~ "elseif") spaces expression spaces? colon spaces?)
307 (p:defrule block-if-elseif-head (and (p:~ "elseif") spaces expression spaces? colon spaces?)
308 (:function remove-nil)
308 (:function remove-nil)
309 (:function intern-first))
309 (:function intern-first))
310
310
311 (p:defrule block-if-else-inline (and block-if-else-head line-body)
311 (p:defrule block-if-else-inline (and block-if-else-head line-body)
312 (:function second))
312 (:function second))
313
313
314 (p:defrule block-if-else (and block-if-else-head #\newline spaces? block-body)
314 (p:defrule block-if-else (and block-if-else-head #\newline spaces? block-body)
315 (:function fourth))
315 (:function fourth))
316
316
317 (p:defrule block-if-else-head (and (p:~ "else") spaces?)
317 (p:defrule block-if-else-head (and (p:~ "else") spaces?)
318 (:constant nil))
318 (:constant nil))
319
319
320 (p:defrule block-if-end (and (p:~ "end")
320 (p:defrule block-if-end (and (p:~ "end")
321 (p:? (and spaces (p:~ "if"))))
321 (p:? (and spaces (p:~ "if"))))
322 (:constant nil))
322 (:constant nil))
323
323
324 (p:defrule block-act (and block-act-head (or block-act-ml block-act-sl))
324 (p:defrule block-act (and block-act-head (or block-act-ml block-act-sl))
325 (:lambda (list)
325 (:lambda (list)
326 (apply #'append list)))
326 (apply #'append list)))
327
327
328 (p:defrule block-act-sl line-body)
328 (p:defrule block-act-sl line-body)
329
329
330 (p:defrule block-act-ml (and newline-block-body block-act-end)
330 (p:defrule block-act-ml (and newline-block-body block-act-end)
331 (:lambda (list)
331 (:lambda (list)
332 (apply #'list* (butlast list))))
332 (apply #'list* (butlast list))))
333
333
334 (p:defrule block-act-head (and (p:~ "act") spaces qsp-string spaces?
334 (p:defrule block-act-head (and (p:~ "act") spaces qsp-string spaces?
335 (p:? block-act-head-img)
335 (p:? block-act-head-img)
336 colon spaces?)
336 colon spaces?)
337 (:lambda (list)
337 (:lambda (list)
338 (intern-first (list (first list)
338 (intern-first (list (first list)
339 (third list)
339 (third list)
340 (or (fifth list) '(str ""))))))
340 (or (fifth list) '(str ""))))))
341
341
342 (p:defrule block-act-head-img (and #\, spaces? qsp-string spaces?)
342 (p:defrule block-act-head-img (and #\, spaces? qsp-string spaces?)
343 (:lambda (list)
343 (:lambda (list)
344 (or (third list) "")))
344 (or (third list) "")))
345
345
346 (p:defrule block-act-end (and (p:~ "end"))
346 (p:defrule block-act-end (and (p:~ "end"))
347 (:constant nil))
347 (:constant nil))
348
348
349 ;;; Calls
349 ;;; Calls
350
350
351 (p:defrule first-argument (and expression spaces?)
351 (p:defrule first-argument (and expression spaces?)
352 (:function first))
352 (:function first))
353 (p:defrule next-argument (and "," spaces? expression)
353 (p:defrule next-argument (and "," spaces? expression)
354 (:function third))
354 (:function third))
355 (p:defrule arguments (or parenthesized-arguments plain-arguments no-arguments))
355 (p:defrule arguments (or parenthesized-arguments plain-arguments no-arguments))
356 (p:defrule parenthesized-arguments (and spaces? #\( base-arguments #\))
356 (p:defrule parenthesized-arguments (and spaces? #\( base-arguments #\))
357 (:function third))
357 (:function third))
358 (p:defrule plain-arguments (and spaces base-arguments)
358 (p:defrule plain-arguments (and spaces base-arguments)
359 (:function second))
359 (:function second))
360 (p:defrule no-arguments (or (and spaces? (p:& #\newline))
360 (p:defrule no-arguments (or (and spaces? (p:& #\newline))
361 (and spaces? (p:& #\&))
361 (and spaces? (p:& #\&))
362 spaces?)
362 spaces?)
363 (:constant nil))
363 (:constant nil))
364 (p:defrule base-arguments (or (and first-argument (* next-argument)) spaces?)
364 (p:defrule base-arguments (or (and first-argument (* next-argument)) spaces?)
365 (:lambda (list)
365 (:lambda (list)
366 (if (null list)
366 (if (null list)
367 nil
367 nil
368 (list* (first list) (second list)))))
368 (list* (first list) (second list)))))
369
369
370 ;;; Intrinsics
370 ;;; Intrinsics
371
371
372 (defmacro defintrinsics ((rule-name returning-rule-name non-returning-rule-name) &body clauses)
372 (defmacro defintrinsics ((rule-name returning-rule-name non-returning-rule-name) &body clauses)
373 `(progn
373 `(progn
374 ,@(loop :for clause :in clauses
374 ,@(loop :for clause :in clauses
375 :collect `(defintrinsic ,@clause))
375 :collect `(defintrinsic ,@clause))
376 (p:defrule ,returning-rule-name (or ,@(remove-nil
376 (p:defrule ,returning-rule-name (or ,@(remove-nil
377 (mapcar (lambda (clause)
377 (mapcar (lambda (clause)
378 (when (second clause)
378 (when (second clause)
379 (alexandria:symbolicate
379 (alexandria:symbolicate
380 'intrinsic- (first clause))))
380 'intrinsic- (first clause))))
381 clauses))))
381 clauses))))
382 (p:defrule ,non-returning-rule-name (or ,@(remove-nil
382 (p:defrule ,non-returning-rule-name (or ,@(remove-nil
383 (mapcar (lambda (clause)
383 (mapcar (lambda (clause)
384 (unless (second clause)
384 (unless (second clause)
385 (alexandria:symbolicate
385 (alexandria:symbolicate
386 'intrinsic- (first clause))))
386 'intrinsic- (first clause))))
387 clauses))))
387 clauses))))
388 (p:defrule ,rule-name (or ,returning-rule-name ,non-returning-rule-name))))
388 (p:defrule ,rule-name (or ,returning-rule-name ,non-returning-rule-name))))
389
389
390 (defmacro defintrinsic (sym returning &optional (min-arity 0) (max-arity 10) &rest names)
390 (defmacro defintrinsic (sym returning &optional (min-arity 0) (max-arity 10) &rest names)
391 (declare (ignore returning))
391 (declare (ignore returning))
392 (setf names
392 (setf names
393 (if names
393 (if names
394 (mapcar #'string-upcase names)
394 (mapcar #'string-upcase names)
395 (list (string sym))))
395 (list (string sym))))
396 `(p:defrule ,(alexandria:symbolicate 'intrinsic- sym)
396 `(p:defrule ,(alexandria:symbolicate 'intrinsic- sym)
397 (and (p:? #\$) (or ,@(loop :for name :in names :collect `(p:~ ,name)))
397 (and (p:? #\$) (or ,@(loop :for name :in names :collect `(p:~ ,name)))
398 arguments)
398 arguments)
399 (:destructure (dollar name arguments)
399 (:destructure (dollar name arguments)
400 (declare (ignore dollar))
400 (declare (ignore dollar))
401 (unless (<= ,min-arity (length arguments) ,max-arity)
401 (unless (<= ,min-arity (length arguments) ,max-arity)
402 (error "Intrinsic ~A expects between ~A and ~A arguments but ~A were provided:~%~S"
402 (error "Intrinsic ~A expects between ~A and ~A arguments but ~A were provided:~%~S"
403 name ,min-arity ,max-arity (length arguments) arguments))
403 name ,min-arity ,max-arity (length arguments) arguments))
404 (list* ',sym arguments))))
404 (list* ',sym arguments))))
405
405
406 (defintrinsics (intrinsic returning-intrinsic non-returning-intrinsic)
406 (defintrinsics (intrinsic returning-intrinsic non-returning-intrinsic)
407 ;; Transitions
407 ;; Transitions
408 (goto nil 0 10 "gt" "goto")
408 (goto nil 0 10 "gt" "goto")
409 (xgoto nil 0 10 "xgt" "xgoto")
409 (xgoto nil 0 10 "xgt" "xgoto")
410 ;; Variables
410 ;; Variables
411 (killvar nil 0 2)
411 (killvar nil 0 2)
412 ;; Expressions
412 ;; Expressions
413 (obj t 1 1)
413 (obj t 1 1)
414 (loc t 1 1)
414 (loc t 1 1)
415 (no t 1 1)
415 (no t 1 1)
416 ;; Basic
416 ;; Basic
417 (qspver t 0 0)
417 (qspver t 0 0)
418 (curloc t 0 0)
418 (curloc t 0 0)
419 (rand t 1 2)
419 (rand t 1 2)
420 (rnd t 0 0)
420 (rnd t 0 0)
421 (qspmax t 1 10 "max")
421 (qspmax t 1 10 "max")
422 (qspmin t 1 10 "min")
422 (qspmin t 1 10 "min")
423 ;; Arrays
423 ;; Arrays
424 (killall nil 0 0)
424 (killall nil 0 0)
425 (copyarr nil 2 4)
425 (copyarr nil 2 4)
426 (arrsize t 1 1)
426 (arrsize t 1 1)
427 (arrpos t 2 3)
427 (arrpos t 2 3)
428 (arrcomp t 2 3)
428 (arrcomp t 2 3)
429 ;; Strings
429 ;; Strings
430 (len t 1 1)
430 (len t 1 1)
431 (mid t 2 3)
431 (mid t 2 3)
432 (ucase t 1 1)
432 (ucase t 1 1)
433 (lcase t 1 1)
433 (lcase t 1 1)
434 (trim t 1 1)
434 (trim t 1 1)
435 (replace t 2 3)
435 (replace t 2 3)
436 (instr t 2 3)
436 (instr t 2 3)
437 (isnum t 1 1)
437 (isnum t 1 1)
438 (val t 1 1)
438 (val t 1 1)
439 (qspstr t 1 1 "str")
439 (qspstr t 1 1 "str")
440 (strcomp t 2 2)
440 (strcomp t 2 2)
441 (strfind t 2 3)
441 (strfind t 2 3)
442 (strpos t 2 3)
442 (strpos t 2 3)
443 ;; IF
443 ;; IF
444 (iif t 2 3)
444 (iif t 2 3)
445 ;; Subs
445 ;; Subs
446 (gosub nil 1 10 "gosub" "gs")
446 (gosub nil 1 10 "gosub" "gs")
447 (func t 1 10)
447 (func t 1 10)
448 (exit nil 0 0)
448 (exit nil 0 0)
449 ;; Jump
449 ;; Jump
450 (jump nil 1 1)
450 (jump nil 1 1)
451 ;; Dynamic
451 ;; Dynamic
452 (dynamic nil 1 10)
452 (dynamic nil 1 10)
453 (dyneval t 1 10)
453 (dyneval t 1 10)
454 ;; Main window
454 ;; Main window
455 (main-pl nil 1 1 "*pl")
455 (main-pl nil 1 1 "*pl")
456 (main-nl nil 0 1 "*nl")
456 (main-nl nil 0 1 "*nl")
457 (main-p nil 1 1 "*p")
457 (main-p nil 1 1 "*p")
458 (maintxt t 0 0)
458 (maintxt t 0 0)
459 (desc t 1 1)
459 (desc t 1 1)
460 (main-clear nil 0 0 "*clear" "*clr")
460 (main-clear nil 0 0 "*clear" "*clr")
461 ;; Aux window
461 ;; Aux window
462 (showstat nil 1 1)
462 (showstat nil 1 1)
463 (stat-pl nil 1 1 "pl")
463 (stat-pl nil 1 1 "pl")
464 (stat-nl nil 0 1 "nl")
464 (stat-nl nil 0 1 "nl")
465 (stat-p nil 1 1 "p")
465 (stat-p nil 1 1 "p")
466 (stattxt t 0 0)
466 (stattxt t 0 0)
467 (stat-clear nil 0 0 "clear" "clr")
467 (stat-clear nil 0 0 "clear" "clr")
468 (cls nil 0 0)
468 (cls nil 0 0)
469 ;; Dialog
469 ;; Dialog
470 (msg nil 1 1)
470 (msg nil 1 1)
471 ;; Acts
471 ;; Acts
472 (showacts nil 1 1)
472 (showacts nil 1 1)
473 (delact nil 1 1 "delact" "del act")
473 (delact nil 1 1 "delact" "del act")
474 (curacts t 0 0)
474 (curacts t 0 0)
475 (cla nil 0 0)
475 (cla nil 0 0)
476 ;; Objects
476 ;; Objects
477 (showobjs nil 1 1)
477 (showobjs nil 1 1)
478 (addobj nil 1 3 "addobj" "add obj")
478 (addobj nil 1 3 "addobj" "add obj")
479 (delobj nil 1 1 "delobj" "del obj")
479 (delobj nil 1 1 "delobj" "del obj")
480 (killobj nil 0 1)
480 (killobj nil 0 1)
481 (countobj t 0 0)
481 (countobj t 0 0)
482 (getobj t 1 1)
482 (getobj t 1 1)
483 ;; Menu
483 ;; Menu
484 (menu nil 1 1)
484 (menu nil 1 1)
485 ;; Sound
485 ;; Sound
486 (play nil 1 2)
486 (play nil 1 2)
487 (isplay t 1 1)
487 (isplay t 1 1)
488 (close nil 1 1)
488 (close nil 1 1)
489 (closeall nil 0 0 "close all")
489 (closeall nil 0 0 "close all")
490 ;; Images
490 ;; Images
491 (refint nil 0 0)
491 (refint nil 0 0)
492 (view nil 0 1)
492 (view nil 0 1)
493 ;; Fonts
493 ;; Fonts
494 (rgb t 3 3)
494 (rgb t 3 3)
495 ;; Input
495 ;; Input
496 (showinput nil 1 1)
496 (showinput nil 1 1)
497 (usertxt t 0 0 "user_text" "usrtxt")
497 (usertxt t 0 0 "user_text" "usrtxt")
498 (cmdclear nil 0 0 "cmdclear" "cmdclr")
498 (cmdclear nil 0 0 "cmdclear" "cmdclr")
499 (input t 1 1)
499 (input t 1 1)
500 ;; Files
500 ;; Files
501 (openqst nil 1 1)
501 (openqst nil 1 1)
502 (addqst nil 1 1 "addqst" "addlib" "inclib")
502 (addqst nil 1 1 "addqst" "addlib" "inclib")
503 (killqst nil 1 1 "killqst" "dellib" "freelib")
503 (killqst nil 1 1 "killqst" "dellib" "freelib")
504 (opengame nil 0 0)
504 (opengame nil 0 0)
505 (savegame nil 0 0)
505 (savegame nil 0 0)
506 ;; Real time
506 ;; Real time
507 (wait nil 1 1)
507 (wait nil 1 1)
508 (msecscount t 0 0)
508 (msecscount t 0 0)
509 (settimer nil 1 1))
509 (settimer nil 1 1))
510
510
511 ;;; Expression
511 ;;; Expression
512
512
513 (p:defrule expression or-expr)
513 (p:defrule expression or-expr)
514
514
515 (p:defrule or-expr (and and-expr (* (and spaces? or-op spaces? and-expr)))
515 (p:defrule or-expr (and and-expr (* (and spaces? or-op spaces? and-expr)))
516 (:function do-binop))
516 (:function do-binop))
517
517
518 (p:defrule and-expr (and eq-expr (* (and spaces? and-op spaces? eq-expr)))
518 (p:defrule and-expr (and eq-expr (* (and spaces? and-op spaces? eq-expr)))
519 (:function do-binop))
519 (:function do-binop))
520
520
521 (p:defrule eq-expr (and sum-expr (* (and spaces? (or "<=" ">=" "=<" "=>" "<>"
521 (p:defrule eq-expr (and sum-expr (* (and spaces? (or "<=" ">=" "=<" "=>" "<>"
522 "=" "<" ">" "!")
522 "=" "<" ">" "!")
523 spaces? cat-expr)))
523 spaces? cat-expr)))
524 (:function do-binop))
524 (:function do-binop))
525
525
526 (p:defrule sum-expr (and mod-expr (* (and spaces? (or #\+ #\-) spaces? mod-expr)))
526 (p:defrule sum-expr (and mod-expr (* (and spaces? (or #\+ #\-) spaces? mod-expr)))
527 (:function do-binop))
527 (:function do-binop))
528
528
529 (p:defrule mod-expr (and mul-expr (* (and spaces? (p:~ "mod") spaces? mul-expr)))
529 (p:defrule mod-expr (and mul-expr (* (and spaces? (p:~ "mod") spaces? mul-expr)))
530 (:function do-binop))
530 (:function do-binop))
531
531
532 (p:defrule mul-expr (and unary-expr (* (and spaces? (or #\* #\/) spaces? unary-expr)))
532 (p:defrule mul-expr (and unary-expr (* (and spaces? (or #\* #\/) spaces? unary-expr)))
533 (:function do-binop))
533 (:function do-binop))
534
534
535 (p:defrule unary-expr (and (p:? (or #\+ #\-)) atom-expr)
535 (p:defrule unary-expr (and (p:? (or #\+ #\-)) atom-expr)
536 (:lambda (list)
536 (:lambda (list)
537 (let ((expr (remove-nil list)))
537 (let ((expr (remove-nil list)))
538 (if (= 1 (length expr))
538 (if (= 1 (length expr))
539 (first expr)
539 (first expr)
540 (intern-first expr)))))
540 (intern-first expr)))))
541
541
542 (p:defrule atom-expr (and (or variable literal returning-intrinsic paren-expr) spaces?)
542 (p:defrule atom-expr (and (or variable literal returning-intrinsic paren-expr) spaces?)
543 (:function first))
543 (:function first))
544
544
545 (p:defrule paren-expr (and #\( spaces? expression spaces? #\))
545 (p:defrule paren-expr (and #\( spaces? expression spaces? #\))
546 (:function third))
546 (:function third))
547
547
548 (p:defrule or-op (p:~ "or")
548 (p:defrule or-op (p:~ "or")
549 (:constant "or"))
549 (:constant "or"))
550
550
551 (p:defrule and-op (p:~ "and")
551 (p:defrule and-op (p:~ "and")
552 (:constant "and"))
552 (:constant "and"))
553
553
554 ;;; Variables
554 ;;; Variables
555
555
556 (p:defrule variable (and identifier (p:? array-index))
556 (p:defrule variable (and identifier (p:? array-index))
557 (:destructure (id idx)
557 (:destructure (id idx)
558 (list 'var id (or idx 0))))
558 (if (char= #\$ (elt (string id) 0))
559 (list 'var (intern (subseq (string-upcase id) 1)) (or idx 0) :str)
560 (list 'var id (or idx 0) :num))))
559
561
560 (p:defrule array-index (and #\[ spaces? (p:? expression) spaces? #\])
562 (p:defrule array-index (and #\[ spaces? (p:? expression) spaces? #\])
561 (:lambda (list)
563 (:function third))
562 (or (third list) :end)))
563
564
564 (p:defrule assignment (or kw-assignment plain-assignment op-assignment)
565 (p:defrule assignment (or kw-assignment plain-assignment op-assignment)
565 (:destructure (var eq expr)
566 (:destructure (var eq expr)
566 (declare (ignore eq))
567 (declare (ignore eq))
567 (list 'set var expr)))
568 (list 'set var expr)))
568
569
569 (p:defrule kw-assignment (and (or (p:~ "let") (p:~ "set")) spaces? (or op-assignment plain-assignment))
570 (p:defrule kw-assignment (and (or (p:~ "let") (p:~ "set")) spaces? (or op-assignment plain-assignment))
570 (:function third))
571 (:function third))
571
572
572 (p:defrule op-assignment (and variable spaces? (or "+" "-" "*" "/") #\= spaces? expression)
573 (p:defrule op-assignment (and variable spaces? (or "+" "-" "*" "/") #\= spaces? expression)
573 (:destructure (var ws1 op eq ws2 expr)
574 (:destructure (var ws1 op eq ws2 expr)
574 (declare (ignore ws1 ws2))
575 (declare (ignore ws1 ws2))
575 (list var eq (intern-first (list op var expr)))))
576 (list var eq (intern-first (list op var expr)))))
576
577
577 (p:defrule plain-assignment (and variable spaces? #\= spaces? expression)
578 (p:defrule plain-assignment (and variable spaces? #\= spaces? expression)
578 (:function remove-nil))
579 (:function remove-nil))
579
580
580 ;;; Non-string literals
581 ;;; Non-string literals
581
582
582 (p:defrule literal (or qsp-string brace-string number))
583 (p:defrule literal (or qsp-string brace-string number))
583
584
584 (p:defrule number (+ (or #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
585 (p:defrule number (+ (or #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
585 (:lambda (list)
586 (:lambda (list)
586 (parse-integer (p:text list))))
587 (parse-integer (p:text list))))
@@ -1,181 +1,193 b''
1
1
2 (in-package sugar-qsp)
2 (in-package sugar-qsp)
3
3
4 ;;;; Parenscript macros which make the parser's intermediate
4 ;;;; Parenscript macros which make the parser's intermediate
5 ;;;; representation directly compilable by Parenscript
5 ;;;; representation directly compilable by Parenscript
6 ;;;; Some utility macros for other .ps sources too.
6 ;;;; Some utility macros for other .ps sources too.
7
7
8 ;;; Utils
8 ;;; Utils
9
9
10 (ps:defpsmacro defm (path args &body body)
10 (ps:defpsmacro defm (path args &body body)
11 `(setf ,path (lambda ,args ,@body)))
11 `(setf ,path (lambda ,args ,@body)))
12
12
13 (ps:defpsmacro root (&rest path)
13 (ps:defpsmacro root (&rest path)
14 `(ps:@ *sugar-q-s-p ,@path))
14 `(ps:@ *sugar-q-s-p ,@path))
15
15
16 (ps:defpsmacro in (key obj)
16 (ps:defpsmacro in (key obj)
17 `(ps:chain ,obj (has-own-property ,key)))
17 `(ps:chain ,obj (has-own-property ,key)))
18
18
19 (ps:defpsmacro with-frame (&body body)
20 `(progn
21 (api-call push-local-frame)
22 (unwind-protect
23 ,@body
24 (api-call pop-local-frame))))
25
19 ;;; Common
26 ;;; Common
20
27
21 (defmacro defpsintrinsic (name)
28 (defmacro defpsintrinsic (name)
22 `(ps:defpsmacro ,name (&rest args)
29 `(ps:defpsmacro ,name (&rest args)
23 `(funcall (root lib ,',name)
30 `(funcall (root lib ,',name)
24 ,@args)))
31 ,@args)))
25
32
26 (defmacro defpsintrinsics (() &rest names)
33 (defmacro defpsintrinsics (() &rest names)
27 `(progn ,@(loop :for name :in names
34 `(progn ,@(loop :for name :in names
28 :collect `(defpsintrinsic ,name))))
35 :collect `(defpsintrinsic ,name))))
29
36
30 (defpsintrinsics ()
37 (defpsintrinsics ()
31 rand copyarr arrpos arrcomp instr isnum strcomp strfind strpos iif gosub func dynamic dyneval main-p main-pl main-nl maintxt main-clear stat-p stat-pl stat-nl stattxt stat-clear cls curacts addobj delobj killobj menu play isplay close closeall refint view rgb showinput usertxt cmdclear input openqst addqst killqst opengame savegame wait msecscount settimer)
38 rand copyarr arrpos arrcomp instr isnum strcomp strfind strpos iif gosub func dynamic dyneval main-p main-pl main-nl maintxt main-clear stat-p stat-pl stat-nl stattxt stat-clear cls curacts addobj delobj killobj menu play isplay close closeall refint view rgb showinput usertxt cmdclear input openqst addqst killqst opengame savegame wait msecscount settimer)
32
39
33 (ps:defpsmacro api-call (func &rest args)
40 (ps:defpsmacro api-call (func &rest args)
34 `(funcall (root api ,func) ,@args))
41 `(funcall (root api ,func) ,@args))
35
42
36 (ps:defpsmacro label-block (&body body)
43 (ps:defpsmacro label-block ((&key (locals t)) &body body)
37 (let ((has-labels (some #'keywordp body)))
44 (let ((has-labels (some #'keywordp body)))
38 `(block nil
45 `(block nil
39 ,@(when has-labels
46 ,@(when has-labels
40 '((defvar __labels)))
47 '((defvar __labels)))
41 (api-call push-local-frame)
48 ,@(if locals
42 (unwind-protect
49 `((with-frame
43 (tagbody
50 (tagbody
44 ,@body)
51 ,@body)))
45 (api-call pop-local-frame))
52 `((tagbody
46 (values))))
53 ,@body))))))
47
54
48 (ps:defpsmacro str (&rest forms)
55 (ps:defpsmacro str (&rest forms)
49 (cond ((zerop (length forms))
56 (cond ((zerop (length forms))
50 "")
57 "")
51 ((and (= 1 (length forms))
58 ((and (= 1 (length forms))
52 (stringp (first forms)))
59 (stringp (first forms)))
53 (first forms))
60 (first forms))
54 (t
61 (t
55 `(& ,@forms))))
62 `(& ,@forms))))
56
63
57 ;;; 1loc
64 ;;; 1loc
58
65
59 (ps:defpsmacro location ((name) &body body)
66 (ps:defpsmacro location ((name) &body body)
60 `(setf (root locs ,name)
67 `(setf (root locs ,name)
61 (lambda ()
68 (lambda (args)
62 (label-block
69 (label-block ()
63 ,@body))))
70 (api-call init-args args)
71 ,@body
72 (api-call get-result)))))
64
73
65 (ps:defpsmacro goto (target &rest args)
74 (ps:defpsmacro goto (target &rest args)
66 `(progn
75 `(progn
67 (funcall (root lib goto) ,target ,@args)
76 (funcall (root lib goto) ,target ,args)
68 (exit)))
77 (exit)))
69
78
70 (ps:defpsmacro xgoto (target &rest args)
79 (ps:defpsmacro xgoto (target &rest args)
71 `(progn
80 `(progn
72 (funcall (root lib xgoto) ,target ,@args)
81 (funcall (root lib xgoto) ,target ,args)
73 (exit)))
82 (exit)))
74
83
75 (ps:defpsmacro desc (target)
84 (ps:defpsmacro desc (target)
76 (declare (ignore target))
85 (declare (ignore target))
77 (report-error "DESC is not supported"))
86 (report-error "DESC is not supported"))
78
87
79 ;;; 2var
88 ;;; 2var
80
89
81 (ps:defpsmacro var (name index)
90 (ps:defpsmacro var (name index slot)
82 `(api-call get-var ,(string name) ,index))
91 `(api-call get-var ,(string name) ,index ,slot))
83
92
84 (ps:defpsmacro set ((var vname vindex) value)
93 (ps:defpsmacro set ((var vname vindex vslot) value)
85 (assert (eq var 'var))
94 (assert (eq var 'var))
86 `(api-call set-var ,(string vname) ,vindex ,value))
95 `(api-call set-var ,(string vname) ,vindex ,vslot ,value))
87
96
88 ;;; 3expr
97 ;;; 3expr
89
98
90 (ps:defpsmacro <> (op1 op2)
99 (ps:defpsmacro <> (op1 op2)
91 `(not (equal ,op1 ,op2)))
100 `(not (equal ,op1 ,op2)))
92
101
93 (ps:defpsmacro ! (op1 op2)
102 (ps:defpsmacro ! (op1 op2)
94 `(not (equal ,op1 ,op2)))
103 `(not (equal ,op1 ,op2)))
95
104
96 ;;; 4code
105 ;;; 4code
97
106
98 (ps:defpsmacro exec (&body body)
107 (ps:defpsmacro exec (&body body)
99 (format nil "javascript: ~{~A~^~%~}" (mapcar #'ps:ps* body)))
108 (format nil "javascript: ~{~A~^~%~}" (mapcar #'ps:ps* body)))
100
109
101 ;;; 5arrays
110 ;;; 5arrays
102
111
103 ;;; 6str
112 ;;; 6str
104
113
105 (ps:defpsmacro & (&rest args)
114 (ps:defpsmacro & (&rest args)
106 `(ps:chain "" (concat ,@args)))
115 `(ps:chain "" (concat ,@args)))
107
116
108 ;;; 7if
117 ;;; 7if
109
118
110 (ps:defpsmacro qspcond (&rest clauses)
119 (ps:defpsmacro qspcond (&rest clauses)
111 `(cond ,@(loop :for clause :in clauses
120 `(cond ,@(loop :for clause :in clauses
112 :collect (list (first clause)
121 :collect (list (first clause)
113 `(tagbody ,@(rest clause))))))
122 `(tagbody
123 ,@(rest clause))))))
114
124
115 ;;; 8sub
125 ;;; 8sub
116
126
117 ;;; 9loops
127 ;;; 9loops
118 ;;; Yep, that's a huge kludge since Javascript doesn't support arbitrary labels
128 ;;; Yep, that's a huge kludge since Javascript doesn't support arbitrary labels
119
129
120 (ps:defpsmacro jump (target)
130 (ps:defpsmacro jump (target)
121 `(return-from ,(intern (string-upcase (second target)))
131 `(return-from ,(intern (string-upcase (second target)))
122 (funcall (ps:getprop __labels ,target))))
132 (funcall (ps:getprop __labels ,target))))
123
133
124 (ps:defpsmacro tagbody (&body body)
134 (ps:defpsmacro tagbody (&body body)
125 (let ((funcs (list nil :__nil)))
135 (let ((funcs (list nil :__nil)))
126 (dolist (form body)
136 (dolist (form body)
127 (cond ((keywordp form)
137 (cond ((keywordp form)
128 (setf (first funcs) (reverse (first funcs)))
138 (setf (first funcs) (reverse (first funcs)))
129 (push form funcs)
139 (push form funcs)
130 (push nil funcs))
140 (push nil funcs))
131 (t
141 (t
132 (push form (first funcs)))))
142 (push form (first funcs)))))
133 (setf (first funcs) (reverse (first funcs)))
143 (setf (first funcs) (reverse (first funcs)))
134 (setf funcs (reverse funcs))
144 (setf funcs (reverse funcs))
135 (if (= 2 (length funcs))
145 (if (= 2 (length funcs))
136 `(progn
146 `(progn
137 ,@body)
147 ,@body)
138 `(progn
148 `(progn
139 (setf ,@(loop :for f :on funcs :by #'cddr
149 (setf ,@(loop :for f :on funcs :by #'cddr
140 :append (list `(ps:@ __labels ,(first f))
150 :append `((ps:@ __labels ,(first f))
141 `(block ,(intern (string-upcase (string (first f))))
151 (block ,(intern (string-upcase (string (first f))))
142 ,@(second f)
152 ,@(second f)
143 ,@(when (third f)
153 ,@(when (third f)
144 `((funcall
154 `((funcall
145 (ps:getprop __labels ,(third f)))))))))
155 (ps:getprop __labels ,(third f)))))))))
146 (jump (str "__nil"))))))
156 (jump (str "__nil"))))))
147
157
148 ;;; 10dynamic
158 ;;; 10dynamic
149
159
150 (ps:defpsmacro qspblock (&body body)
160 (ps:defpsmacro qspblock (&body body)
151 `(lambda ()
161 `(lambda (args)
152 (label-block
162 (label-block ()
153 ,@body)))
163 (api-call init-args args)
164 ,@body
165 (api-call get-result))))
154
166
155 ;;; 11main
167 ;;; 11main
156
168
157 (ps:defpsmacro act (name img &body body)
169 (ps:defpsmacro act (name img &body body)
158 `(api-call add-act ,name ,img
170 `(api-call add-act ,name ,img
159 (lambda ()
171 (lambda ()
160 (label-block
172 (label-block ()
161 ,@body))))
173 ,@body))))
162
174
163 ;;; 12aux
175 ;;; 12aux
164
176
165 ;;; 13diag
177 ;;; 13diag
166
178
167 ;;; 14act
179 ;;; 14act
168
180
169 ;;; 15objs
181 ;;; 15objs
170
182
171 ;;; 16menu
183 ;;; 16menu
172
184
173 ;;; 17sound
185 ;;; 17sound
174
186
175 ;;; 18img
187 ;;; 18img
176
188
177 ;;; 19input
189 ;;; 19input
178
190
179 ;;; 20time
191 ;;; 20time
180
192
181 ;;; 21local
193 ;;; 21local
General Comments 0
You need to be logged in to leave comments. Login now