2011/09/25

scan-file*

(defun remove-from-keyword-args (args &rest keywords)
(loop for (a b) on args by #'cddr
unless (member a keywords :test #'eq)
append (list a b)))

(series::defS scan-file* (name &rest args-for-open &key (reader #'read-line) &allow-other-keys)
"like scan-file. accept options for open."
(series::fragl ((name) (reader) (args-for-open)) ; args
((items t)) ; rets
((items t) ; aux
(lastcons cons (list nil))
(lst list))
() ; alt
((setq lst lastcons) ; prolog
(with-open-stream (f (apply #'open name :direction
:input (remove-from-keyword-args args-for-open :reader)))
(cl:let ((done (list nil)))
(loop
(cl:let ((item (cl:funcall reader f nil done)))
(when (eq item done)
(return nil))
(setq lastcons (setf (cdr lastcons) (cons item nil)))))))
(setq lst (cdr lst)))
((if (null lst) (go series::end)) ; body
(setq items (car lst))
(setq lst (cdr lst)))
() ; epilog
() ; wraprs
:context) ; impure
:optimizer
(series::apply-literal-frag
(cl:let ((file (series::new-var 'file)))
`((((reader)) ; args
((items t)) ; rets
((items t) (done t (list nil))) ; aux
() ; alt
() ; prolog
((if (eq (setq items (cl:funcall reader ,file nil done)) done) ; body
(go series::end)))
() ; epilog
((#'(lambda (code) ; wraprs
(list 'with-open-file
'(,file ,name :direction :input ,@(remove-from-keyword-args args-for-open :reader))
code)) :loop))
:context) ; impure
,reader)))) ; これは何?

0 件のコメント: