X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=ecmalisp.lisp;h=1f960b6457659ad32a6551f3309e78bbcb47e1cc;hb=ad19ec082d2d9aa6877cc0bbae9ec3fe3094f489;hp=202997bcb49f1b2b35989e44ae7ef3a49d438219;hpb=c9f9630e9e82f5c8d28c1701ebc3b0f2ba3a2355;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index 202997b..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 @@ -1159,8 +1196,8 @@ (values nil index))))) #+ecmalisp -(defun parse-integer (string &key junk-allowed) - (!parse-integer string junk-allowed)) +(defun parse-integer (string) + (!parse-integer string nil)) (defvar *eof* (gensym)) (defun ls-read (stream) @@ -2726,10 +2763,9 @@ (defvar *compile-print-toplevels* nil) (defun truncate-string (string &optional (width 60)) - (let ((size (length string)) - (n (or (position #\newline string) - (min width (length string))))) - (subseq string 0 n))) + (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))