X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=ecmalisp.lisp;h=1f960b6457659ad32a6551f3309e78bbcb47e1cc;hb=ad19ec082d2d9aa6877cc0bbae9ec3fe3094f489;hp=bbc6340f6a15f6606276dd83b3c1808a405b9ddd;hpb=c9e8ef61e01075a7db5de4f8f493161be8799940;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index bbc6340..1f960b6 100644 --- a/ecmalisp.lisp +++ b/ecmalisp.lisp @@ -383,6 +383,43 @@ ,@body) (cdr ,head)))) + ;; A very simple defstruct built on lists. It supports just slot with + ;; an optional default initform, and it will create a constructor, + ;; predicate and accessors for you. + (defmacro defstruct (name &rest slots) + (unless (symbolp name) + (error "It is not a full defstruct implementation.")) + (let* ((name-string (symbol-name name)) + (slot-descriptions + (mapcar (lambda (sd) + (cond + ((symbolp sd) + (list sd)) + ((and (listp sd) (car sd) (cddr sd)) + sd) + (t + (error "Bad slot accessor.")))) + slots)) + (predicate (intern (concat name-string "P")))) + `(progn + ;; Constructor + (defun ,(intern (concat "MAKE-" name-string)) (&key ,@slot-descriptions) + (list ',name ,@(mapcar #'car slot-descriptions))) + ;; Predicate + (defun ,predicate (x) + (and (consp x) (eq (car x) ',name))) + ;; Slot accessors + ,@(with-collect + (let ((index 1)) + (dolist (slot slot-descriptions) + (let ((name (car slot))) + (collect `(defun ,(intern (concat name-string "-" (string name))) (x) + (unless (,predicate x) + (error ,(concat "The object is not a type " name-string))) + (nth ,index x))) + (incf index))))) + ',name))) + (defun map1 (func list) (with-collect (while list @@ -533,6 +570,14 @@ (return-from every nil))) t) + (defun position (elt sequence) + (let ((pos 0)) + (do-sequence (x seq) + (when (eq elt x) + (return)) + (incf pos)) + pos)) + (defun assoc (x alist) (while alist (if (eql x (caar alist)) @@ -958,7 +1003,9 @@ "()" (prin1-to-string (vector-to-list form))))) ((packagep form) - (concat "#")))) + (concat "#")) + (t + (concat "#")))) (defun write-line (x) (write-string x) @@ -2712,6 +2759,14 @@ (t (error (concat "How should I compile " (prin1-to-string sexp) "?")))))) + +(defvar *compile-print-toplevels* nil) + +(defun truncate-string (string &optional (width 60)) + (let ((n (or (position #\newline string) + (min width (length string))))) + (subseq string 0 n))) + (defun ls-compile-toplevel (sexp &optional multiple-value-p) (let ((*toplevel-compilations* nil)) (cond @@ -2721,6 +2776,12 @@ (cdr sexp)))) (join (remove-if #'null-or-empty-p subs)))) (t + (when *compile-print-toplevels* + (let ((form-string (prin1-to-string sexp))) + (write-string "Compiling ") + (write-string (truncate-string form-string)) + (write-line "..."))) + (let ((code (ls-compile sexp multiple-value-p))) (code (join-trailing (get-toplevel-compilations) (code ";" *newline*)) @@ -2804,8 +2865,9 @@ (read-sequence seq in) seq))) - (defun ls-compile-file (filename output) - (let ((*compiling-file* t)) + (defun ls-compile-file (filename output &key print) + (let ((*compiling-file* t) + (*compile-print-toplevels* print)) (with-open-file (out output :direction :output :if-exists :supersede) (write-string (read-whole-file "prelude.js") out) (let* ((source (read-whole-file filename)) @@ -2824,4 +2886,4 @@ *gensym-counter* 0 *literal-counter* 0 *block-counter* 0) - (ls-compile-file "ecmalisp.lisp" "ecmalisp.js"))) + (ls-compile-file "ecmalisp.lisp" "ecmalisp.js" :print t)))