-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.scm
192 lines (170 loc) · 6.81 KB
/
commands.scm
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
;;; ezd - easy drawing for X11 displays.
;;;
;;; Command parsing and definition.
;* Copyright 1990-1993 Digital Equipment Corporation
;* All Rights Reserved
;*
;* Permission to use, copy, and modify this software and its documentation is
;* hereby granted only under the following terms and conditions. Both the
;* above copyright notice and this permission notice must appear in all copies
;* of the software, derivative works or modified versions, and any portions
;* thereof, and both notices must appear in supporting documentation.
;*
;* Users of this software agree to the terms and conditions set forth herein,
;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
;* right and license under any changes, enhancements or extensions made to the
;* core functions of the software, including but not limited to those affording
;* compatibility with other hardware or software environments, but excluding
;* applications which incorporate this software. Users further agree to use
;* their best efforts to return to Digital any such changes, enhancements or
;* extensions that they make and inform Digital of noteworthy uses of this
;* software. Correspondence should be provided to Digital at:
;*
;* Director of Licensing
;* Western Research Laboratory
;* Digital Equipment Corporation
;* 250 University Avenue
;* Palo Alto, California 94301
;*
;* This software may be distributed (but not offered for sale or transferred
;* for compensation) to third parties, provided such third parties agree to
;* abide by the terms and conditions of this notice.
;*
;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
;* SOFTWARE.
;;; COMMAND PARSING.
;;; Helper functions for command argument parsing.
(define (acons x y) (if (eq? #f y) #f (cons x y)))
(define (matched-args x y)
(define (match-list x y)
(if (null? x)
'()
(cons (if (pair? y) (car y) '())
(match-list (cdr x) (if (pair? y) (cdr y) '())))))
(if (= (length x) 1) (car y) (match-list x y)))
(define (rest-args x y)
(if (or (null? x) (null? y)) y (rest-args (cdr x) (cdr y))))
;;; Command parsing is handled by the following procedure. Its arguments are
;;; a template describing the command arguments and the actual command
;;; arguments. It returns either a list of parsed arguments or #F. The
;;; template is a list composed of any of the following elements that are
;;; matched as noted:
;;;
;;; MATCHES WHEN TRUE:
;;;
;;; (OPTIONAL <template>) #t
;;;
;;; (REPEAT <template>) #t
;;;
;;; (OR <template>...) #t
;;;
;;; *REST* #t
;;;
;;; <a procedure> (<a procedure> argument)
;;;
;;; <any other obj> (EQUAL? <any other obj> argument)
;;;
;;; When the command can be parsed, the result is a list of arguments
;;; generated by the matches to the template. Template items contribute to
;;; the result as follows:
;;; RETURNS ON A MATCH:
;;;
;;; (OPTIONAL <template>) either #f when the <template> could not be
;;; matched, or the item it matched (<template>
;;; consists of one item), or a list of items
;;; matched.
;;;
;;; (REPEAT <template>) a list of items matched (<template> consists
;;; of one item), or a list of lists of items
;;; matched.
;;;
;;; (OR <template>...) the result of the first <template> that matches
;;; or #f when no <template> matched.
;;;
;;; *REST* a list of the remaining arguments.
;;;
;;; <a procedure> the argument.
;;;
;;; <any other obj> no value is returned.
(define (arg-parse template args)
(if (not (null? template))
(let ((x (car template)))
(cond ((and (pair? x) (eq? (car x) 'optional))
(let ((match (arg-parse (append (cdr x) (cdr template))
args)))
(if (not (eq? match #f))
(acons (matched-args (cdr x) args)
(arg-parse (cdr template)
(rest-args (cdr x) args)))
(acons #f (arg-parse (cdr template) args)))))
((and (pair? x) (eq? (car x) 'repeat))
(let loop ((found '()) (args args))
(let ((match (arg-parse
(append (cdr x) '(*rest*)) args)))
(if (not (eq? match #f))
(loop (append found
(list (matched-args (cdr x)
match)))
(rest-args (cdr x) match))
(acons found
(arg-parse (cdr template) args))))))
((and (pair? x) (eq? (car x) 'or))
(let loop ((tl (cdr x)))
(if (null? tl)
#f
(let ((match (arg-parse
(append (car tl) '(*rest*))
args)))
(if (not (eq? match #f))
(acons (matched-args (car tl) match)
(arg-parse (cdr template)
(rest-args (car tl) match)))
(loop (cdr tl)))))))
((eq? x '*rest*) args)
((null? args) #f)
((procedure? x)
(let ((arg (car args)))
(if (x arg)
(acons arg (arg-parse (cdr template) (cdr args)))
#f)))
((equal? x (car args))
(arg-parse (cdr template) (cdr args)))
(else #f)))
(if (null? args) '() #f)))
;;; Generally useful predicates for command decoding.
(define (non-negative? x) (and (number? x) (>= x 0)))
(define (non-zero? x) (and (number? x) (not (= x 0))))
(define (positive-number? x) (and (number? x) (> x 0)))
;(define (any? x) #t)
(define (dash? x) (eq? x 'dash))
;;; ezd commands are defined by calls to the following procedure. The caller
;;; provides the command name (a symbol), the argument parsing template, a
;;; string describing the correct form of the command, and the action procedure
;;; that is to be called when the command is successfully parsed.
(define ezd-commands '())
(define (define-ezd-command template description action)
(let* ((command (car template))
(x (assoc command ezd-commands)))
(if x (set! ezd-commands (delete x ezd-commands)))
(set! ezd-commands
(cons (list command template description action) ezd-commands))
command))
;;; Errors in ezd commands are reported by calling the procedure EZD-ERROR.
;;; This will result in either the message being logged to the stderr-port, or
;;; the Scheme error handler error being called.
(define in-read-eval-draw #f)
(define (ezd-error id form . args)
(if (not in-read-eval-draw) (apply error id form args))
(apply format (current-error-port) form args)
(newline (current-error-port))
#f)
;;; Module initialization procedure.
(define (commands-module-init)
(set! in-read-eval-draw #f)
#t)