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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
;; convert2.lsp -- insert XLISP syntax definitions to Nyquist manual AND SAL syntax
;; definitions into xlisp.mss
(defun assert (co) (if co t (error "assertion error")))
(defun alpha-char-p (c)
(let ((cc (char-code c)))
(or (and (>= cc (char-code #\a))
(<= cc (char-code #\z)))
(and (>= cc (char-code #\A))
(<= cc (char-code #\Z))))))
(defun get-begin-end-token (begin-end)
(open-paren)
(let ((env-name (get-token))
(close-tok (get-token)))
(cond ((paren-match close-tok)
(pop paren-stack)
(return (strcat begin-end "(" env-name ")")))
(t
(display "get-begin-end-token failed" begin-end env-name close-tok)))))
(defun get-token ()
(prog ((token (read-char *inf*))
(next-char (peek-char nil *inf*)))
(if (not token) (return token))
(if (and token (not (alpha-char-p token)) (not (eql token #\@)))
(return (string token)))
(setf token (string token))
(while (and next-char (alpha-char-p next-char))
(setf token (strcat token (string (read-char *inf*))))
(setf next-char (peek-char nil *inf*)))
(if (or (string= token "@begin") (string= token "@end"))
(return (get-begin-end-token token))
(return token))))
(defun convert (infile outfile)
(setf *next-tokens* nil)
(setf paren-stack nil)
(let ((inf (open infile))
(outf (open outfile :direction :output)))
(process inf outf)
(close inf)
(close outf)))
;; note: "<" has been omitted here to allow parsing of "<" as an operator
;; in XLISP documentation. "<" is not commonly used as scribe bracket, but
;; that could cause problems in some cases because this is not a full
;; scribe parser.
(defun is-open-paren (tok)
(member tok '("(" "{" "[") :test 'equal))
(defun open-paren ()
(let ((tok (get-token)))
(cond ((is-open-paren tok)
(push tok paren-stack))
(t
(display "open-paren got a surprise" tok)))))
; (push tok *next-tokens*)
; ;; if no open paren, then fake open and close
; (push #\( paren-stack)
; (push #\) *next-tokens*)))))
(defun close-paren-p (tok)
(paren-match tok))
(defun paren-match (p2)
(let ((p1 (car paren-stack)))
(or (and (equal p2 ")")
(equal p1 "("))
(and (equal p2 "]")
(equal p1 "["))
(and (equal p2 "}")
(equal p1 "{"))
(and (equal p2 ">")
(equal p1 "<")))))
(defun starts-with-symbol-char (tok)
(let ((c (char tok 0)))
(or (alpha-char-p c)
(digit-char-p c)
(eql c #\-)
(eql c #\+)
(eql c #\*)
(eql c #\=)
(eql c #\/)
(eql c #\>)
(eql c #\<))))
(defun get-fn-name ()
(setf *token-list* (cdr *token-list*))
(let ((fn-name ""))
(while (and *token-list*
(or (starts-with-symbol-char (car *token-list*))
(equal (car *token-list*) "@i") ; allow c@i(xx)r
(equal (car *token-list*) "(")
(equal (car *token-list*) ")")))
(setf fn-name (strcat fn-name (car *token-list*)))
(setf *token-list* (cdr *token-list*)))
fn-name))
(defun get-symbol()
(let ((s ""))
(while (and *token-list*
(starts-with-symbol-char (car *token-list*)))
(setf s (strcat s (car *token-list*)))
(setf *token-list* (cdr *token-list*)))
s))
;; GET-ARG - *token-list* starts with open bracket (after @i). Get the
;; tokens between this and the close bracket.
(defun get-arg ()
(let (arg)
(push (car *token-list*) paren-stack)
(setf *token-list* (cdr *token-list*)) ;; go to parameter name
(while paren-stack
(if (close-paren-p (car *token-list*)) (pop paren-stack))
(push (car *token-list*) arg)
(setf *token-list* (cdr *token-list*)))
;; take cdr to drop the close bracket
(reverse (cdr arg))))
(defun get-args ()
(prog (args arg)
loop
(cond ((and *token-list* (cdr *token-list*) (cddr *token-list*)
(equal (car *token-list*) "@i"))
(setf *token-list* (cdr *token-list*))
(push (get-arg) args))
((and (equal (car *token-list*) ".")
(equal (cadr *token-list*) ".")
(equal (caddr *token-list*) "."))
(setf *token-list* (cdddr *token-list*))
(push '("...") args))
((and *token-list* (cddr *token-list*)
(equal (car *token-list*) "&")
(equal (cadr *token-list*) "key")
(equal (caddr *token-list*) " "))
(push '("&key ") args)
(setf *token-list* (cdddr *token-list*)))
((and *token-list* (cdr *token-list*)
(equal (car *token-list*) ":"))
(setf arg '(":"))
(setf *token-list* (cdr *token-list*)) ; skip ":"
(push (get-symbol) arg) ;; keyword
(setf arg (reverse arg))
(push arg args))
(*token-list*
(push (list :meta (car *token-list*)) args)
(setf *token-list* (cdr *token-list*)))
((null *token-list*)
(return (reverse args))))
(go loop)))
(defun write-list-of-args (args)
(let (need-space)
(dolist (arg args)
(cond ((equal arg '("..."))
(setf need-space t)
(format *outf* "@r(...)"))
((and (consp arg) (equal (car arg) :meta))
(setf need-space nil)
(format *outf* (cadr arg)))
((and (consp arg) (or (equal (car arg) ":")
(equal (car arg) "&key ")))
(setf need-space nil)
(format *outf* "@t(")
(write-list-of-tokens arg)
(format *outf* ")"))
(t
;; insert space between consecutive args
(if need-space (format *outf* "@t( )"))
(setf need-space t)
(format *outf* "@t(@i(")
(write-list-of-tokens arg)
(format *outf* "))"))))))
(defun write-sal-args (args)
(let (need-comma)
(dolist (arg args)
(cond ((equal arg '("..."))
(format *outf* "@r(...)"))
((and (consp arg) (equal (car arg) :meta)
(or (equal (cadr arg) "[")
(equal (cadr arg) "]")))
(format *outf* (cadr arg)))
((and (consp arg) (equal (car arg) :meta)) nil) ;; o.w. ignore meta
((and (consp arg) (equal (car arg) "&key ")))
((and (consp arg) (equal (car arg) ":")) ;; must be a keyword parm
;; assumes this is not the first parameter
(format *outf* ", ~A: @i(~A)"
(cadr arg) (cadr arg)))
(t
(format *outf* "~A@i(" (if need-comma ", " ""))
(setf need-comma t)
(write-list-of-tokens arg)
(format *outf* ")"))))))
(defun write-list-of-tokens (toks)
(dolist (tok toks)
(format *outf* "~A" tok)))
;; this is a variable if there are no args and if there is no
;; back-to-back open/close paren pair as in foo().
(defun is-variable-check (args)
(prog ()
loop
(cond ((null (cdr args))
(return t))
((and (equal (car args) '(:meta "("))
(equal (cadr args) '(:meta ")")))
(return nil))
((= (length (car args)) 1)
(return nil)))
(setf args (cdr args))
(go loop)))
(defun get-balanced-token-list (tok)
(let (token-list)
(push tok paren-stack)
(push tok token-list)
(while (and tok paren-stack)
(setf tok (get-token))
(if (is-open-paren tok) (push tok paren-stack)
(if (close-paren-p tok) (pop paren-stack)))
(push tok token-list))
(setf token-list (reverse token-list))))
(defun process-codef ()
(let (fn-name args save-tokens)
(setf *token-list* (get-balanced-token-list (get-token)))
;; now we have a list of tokens including brackets
(display "process-codef" *token-list*)
(setf save-tokens *token-list*)
(setf fn-name (get-fn-name))
(setf args (get-args))
(setf is-var (is-variable-check args))
(display "parse" fn-name args is-var)
(cond (is-var
(format *outf* "@codef")
(write-list-of-tokens save-tokens))
(t
(format *outf* "@codef")
(write-list-of-tokens *token-list*)
(format *outf* " @c{[sal]}@*\n@altdef{@code[(~A" fn-name)
(write-list-of-args args)
(format *outf* "] @c{[lisp]}}")))))
(defun exclude-from-sal (name)
(or (equal name "*")
(equal name "/")
(equal name "+")
(equal name "-")
(equal name "cond")
(equal name "case")
(equal name "let")
(equal name "let*")
(equal name "prog")
(equal name "prog*")
(equal name "flet")
(equal name "labels")
(equal name "macrolet")
(equal name "defun")
(equal name "defmacro")
(equal name "do")
(equal name "do*")
(equal name "dolist")
(equal name "dotimes")
(equal name "return")
(equal name "loop")
(equal name "progv")
(equal name "clean-up")
(equal name "top-level")
(equal name "continue")
(equal name "errset")
(equal name "baktrace")
(equal name "evalhook")
(equal name "1+")
(equal name "1-")
(equal name "<")
(equal name "<=")
(equal name ">")
(equal name ">=")
(equal name "=")
(equal name "/=")
(equal name "print")
(equal name "load")))
(defun process-fdescription ()
(let (function-name args save-tokens (tok (get-token)) has-sal)
(format *outf* "@begin(fdescription)")
(while (and tok (not (equal tok "@end(fdescription)")))
(cond ((equal tok "(")
(setf *token-list* (get-balanced-token-list tok))
(assert (equal (first *token-list*) "("))
(setf function-name (get-fn-name))
(setf save-tokens *token-list*)
(setf args (get-args))
(display "process-fdescription" save-tokens args)
(setf has-sal (not (exclude-from-sal function-name)))
(cond (has-sal
(format *outf* "@begin(fgroup)@xlcode{~A(" function-name)
(write-sal-args args)
(format *outf* ")} @c{[sal]}\n\n ")))
(format *outf* "@xlcode{(~A" function-name)
;(setf save-tokens (reverse save-tokens))
;(cond ((equal (car save-tokens) ")")
; (setf save-tokens (cons "@xlcode{)}" (cdr save-tokens))))
; (t
; (display "MISSING CLOSE PAREN" save-tokens)))
;(setf save-tokens (reverse save-tokens))
;(write-list-of-tokens save-tokens)
(write-list-of-args args)
(format *outf* "} @c{[lisp]}")
(setf tok (get-token))
(format *outf* tok)
(display "process-fdescription" function-name args)
(while (not (equal tok "\n"))
(setf tok (get-token))
(format *outf* tok))
(cond (has-sal
(format *outf* "@end(fgroup)\n")
(setf has-sal nil)))
(setf tok (get-token)))
((equal tok "@begin(pdescription)")
(format *outf* tok)
(scan-to "@end(pdescription)")
(setf tok (get-token)))
((equal (char tok 0) #\@)
(format *outf* tok)
(cond ((equal (setf tok (get-token)) "(")
(write-list-of-tokens (get-balanced-token-list tok))
(setf tok (get-token)))))
(t
(format *outf* tok)
(setf tok (get-token)))))
(if tok (format *outf* tok))))
(defun scan-to (stop-tok)
(prog (tok)
loop
(setf tok (get-token))
(format *outf* "~A" tok)
;; handle nested pdescriptions
(if (equal tok "@begin(pdescription)")
(scan-to "@end(pdescription)"))
(if (equal tok stop-tok) (return))
(go loop)))
(defun process (inf outf)
(setf *inf* inf)
(setf *outf* outf)
(prog (tok)
loop
(setf tok (get-token))
(cond ((null tok)
(return 'done))
((string= tok "@codef")
(process-codef))
((string= tok "@begin(fdescription)")
(process-fdescription))
(t
(format *outf* "~A" tok)))
(go loop)))
(defun l () (load "convert2.lsp"))
;(convert "xltest.mss" "xltest.out.mss")
(convert "../../xlisp/xlisp-no-sal.mss" "xlisp-out.mss")
|