Commit a2ca76a2 authored by Le BigMac's avatar Le BigMac
Browse files

FN, STRUCT, STRUCT*, DEFINER*, method/function SETF, less warnings

parent 9503346b
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
2020-02-02  Le BigMac <1boi@protonmail.com>

	* definer.lisp:
	Summary:
	1. Converted `(name symbol)` -> `name` for methods and functions, so that setf definitions work.
	2. Defined `fn' alias for `function', I think it's good to have something as short, convenient and intuitive for something used so often.
	3. Uncommented 'struct definition -> makes it easy to convert already existing defstructs.
	4. Renamed structure to struct*, but made an alias to it for compatibility.
	5. Added (declare (ignore prefix-options)) nearly everywhere due to the warnings.
	6. Added :ignore-prefix-options to definer for the reasons of point 5 (this time to avoid warnings also in client code), and definer* for a shorthand to that.

	* definer.asd  
	Added FN, STRUCT, STRUCT*, DEFINER* to exports.
 
2013-09-30  author  <author@paniscia.local>

	* definer.asd:
+10 −4
Original line number Diff line number Diff line
@@ -4,9 +4,9 @@

;;; See the file COPYING for copyright and licensing information.

(defpackage "IT.UNIMIB.DISCO.MA.CL.EXT.SYNTAX.DEFINER" (:use "COMMON-LISP")
(defpackage "IT.UNIMIB.DISCO.MA.CL.EXT.SYNTAX.DEFINER"
  (:use "COMMON-LISP")
  (:nicknames  "CL.EXT.SYNTAX.DEFINER" "DEFINER" #-lispworks "DEF" "DEFN")

  (:documentation "The CL Extensions Definer Package.

The package contains a simple extension to Common Lisp by introducing
@@ -31,6 +31,12 @@ scheme.")
   "PARAMETER"
   "CONSTANT"

   "FUNCTION"
   "FN"                                 ; FUNCTION alias

   "STRUCT"
   "STRUCT*"                            ; class-like struct definitions

   "MACRO"
   "GENERIC"
   "METHOD"
@@ -40,6 +46,6 @@ scheme.")
   "MODIFY-MACRO"

   "DEFINER"
   ))
   "DEFINER*"))                         ; DEFINER with :ignore-prefix-var t

;;; end of file -- definer-pkg.lisp --
+2 −4
Original line number Diff line number Diff line
@@ -15,8 +15,6 @@
The DEFINER library adds a simple macro DEF to Common Lisp that
replaces the various 'def*' forms in the language.  It is a simple
hack, but it adds some elegance to the language.  Of course, it comes
with its own way to be extended."
)

with its own way to be extended.")

;;;; end of file -- definer.asd --
+73 −54
Original line number Diff line number Diff line
@@ -131,6 +131,8 @@ function BUILD-DEFINITION-FORM.
;;; Now you say:
;;;
;;;	(def function foo (x) (+ x 42))
;;; or
;;;	(def fn foo (x) (+ x 42))
;;;
;;; and
;;;
@@ -145,16 +147,24 @@ function BUILD-DEFINITION-FORM.
;;;	(def method bar :before ((x symbol)) ...)

(defmethod build-definition-form ((what (eql 'function))
                                  (name symbol)
                                  name
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defun ,name ,@definition-forms))

(defmethod build-definition-form ((what (eql 'fn))
                                  name
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defun ,name ,@definition-forms))

(defmethod build-definition-form ((what (eql 'macro))
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defmacro ,name ,@definition-forms))


@@ -162,22 +172,16 @@ function BUILD-DEFINITION-FORM.
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defun ,name ,@definition-forms))


(defmethod build-definition-form ((what (eql 'method))
                                  (name symbol)
                                  name
                                  definition-forms
                                  &optional prefix-options)
  (if (listp (first definition-forms))
      `(defmethod ,name ,@definition-forms)
      (let ((arglist-pos (position-if #'listp definition-forms)))
        (if arglist-pos
            `(defmethod ,name
                        ,@(subseq definition-forms 0 arglist-pos)
                        ,@(subseq definition-forms arglist-pos))
            (error 'program-error)))))

  (declare (ignore prefix-options))
  `(defmethod ,name ,@definition-forms))

;;; Classes
;;; Now you say:
@@ -188,6 +192,7 @@ function BUILD-DEFINITION-FORM.
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defclass ,name ,@definition-forms))


@@ -207,38 +212,35 @@ function BUILD-DEFINITION-FORM.
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defvar ,name ,@definition-forms))

(defmethod build-definition-form ((what (eql 'parameter))
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defparameter ,name ,@definition-forms))

(defmethod build-definition-form ((what (eql 'constant))
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defconstant ,name ,@definition-forms))



;;; Structures.
;;; These are the straightforward definitions.
#|
(defmethod build-definition-form ((what (eql 'structure))

;;; This is the straightforward definition.
(defmethod build-definition-form ((what (eql 'struct))
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  `(defstruct ,name ,@definition-forms))


(defmethod build-definition-form ((what (eql 'structure))
                                  (name cons)
                                  definition-forms
                                  &optional prefix-options)
  `(defstruct ,name ,@definition-forms))
|#
;;; ... but this is more interesting: let's have a similar syntax for
;;; Classes and Structures.
;;; Now you say:
@@ -247,24 +249,27 @@ function BUILD-DEFINITION-FORM.
;;;	     (:documentation "Foo structure")
;;;	     (:print-function print-foo))

(defmethod build-definition-form ((what (eql 'structure))
(defmethod build-definition-form ((what (eql 'struct*))
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (declare (ignore prefix-options))
  (destructuring-bind (slots &rest options)
      definition-forms
    (if options
        ;; The standard DEFSTRUCT options, but :DOCUMENTATION is
        ;; special.
        (let ((doc (find :documentation options :key #'first))
              (opts (remove :documentation options :key #'first))
              )
              (opts (remove :documentation options :key #'first)))
          `(defstruct (,name ,@opts) ,@(when doc (list (second doc))) ,@slots))
        `(defstruct ,name ,@slots))))




;; redirect to 'struct, so as not to break compatibility
(defmethod build-definition-form ((what (eql 'structure))
                                  (name symbol)
                                  definition-forms
                                  &optional prefix-options)
  (build-definition-form 'struct* name definition-forms prefix-options))

;;; Now let's define a meta "def" processor.
;;; The EVAL-WHEN is needed to ensure that the definition is ready for
@@ -275,20 +280,35 @@ function BUILD-DEFINITION-FORM.
                                    (for-what symbol)
                                    definition-forms
                                    &optional prefix-options)
    (destructuring-bind ((&key
    (declare (ignore prefix-options))
    (destructuring-bind
        ((&key
            ((:type-of-naming-form name-type) 'symbol) ; Non evaluated
            ((:name name-var) 'name)                   ; Non evaluated
            ((:body-name def-form-var) 'definition-forms) ; Non evaluated
                          )
            ((:ignore-prefix-options ignore-prefix-var) nil))
         &body definer-forms)
        definition-forms
      `(defmethod build-definition-form ((what (eql ',for-what))
                                         (,name-var ,name-type)
                                         ,def-form-var
                                         &optional prefix-options)
         ,@definer-forms)))
  )
         ,@(append
            (when ignore-prefix-var (list `(declare (ignore prefix-options))))
            definer-forms)))))

(eval-when (:compile-toplevel :execute :load-toplevel)
  (defmethod build-definition-form ((what (eql 'definer*))
                                    (for-what symbol)
                                    definition-forms
                                    &optional prefix-options)
    (build-definition-form
     'definer
     for-what
     (cons (append (first definition-forms)
                   (list :ignore-prefix-options t))
           (rest definition-forms))
     prefix-options)))

;;; ... and here is a couple of (real!) example.

@@ -298,7 +318,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;;	(def type buffer (x) `(vector character ,x))

(def definer type (:name type-name)
(def definer* type (:name type-name)
  `(deftype ,type-name ,@definition-forms))


@@ -307,7 +327,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;;     (def package "FOO" (:use "CL") (:export "BAR"))

(def definer package (:name package-name)
(def definer* package (:name package-name)
  `(defpackage ,package-name ,@definition-forms))


@@ -316,7 +336,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;;	(def condition my-error (simple-error) ...)

(def definer condition (:name condition-name :body-name cnd-def-body)
(def definer* condition (:name condition-name :body-name cnd-def-body)
  `(define-condition ,condition-name ,@cnd-def-body))


@@ -327,7 +347,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;; Works for both "short" and "long" forms.

(def definer method-combination (:name mcn :body-name mc-def-body)
(def definer* method-combination (:name mcn :body-name mc-def-body)
  `(define-method-combination ,mcn ,@mc-def-body))


@@ -339,7 +359,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;; Works for both "short" and "long" forms.

(def definer setf (:name accessor-fn :body-name defsetf-def-body)
(def definer* setf (:name accessor-fn :body-name defsetf-def-body)
  `(defsetf ,accessor-fn ,@defsetf-def-body))


@@ -350,7 +370,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;; Works for both "short" and "long" forms.

(def definer setf-expander (:name accessor-fn :body-name setfexp-def-body)
(def definer* setf-expander (:name accessor-fn :body-name setfexp-def-body)
  `(define-setf-expander ,accessor-fn ,@setfexp-def-body))


@@ -362,7 +382,7 @@ function BUILD-DEFINITION-FORM.
;;;
;;; Works for both "short" and "long" forms.

(def definer modify-macro (:name mm-name :body-name mm-def-body)
(def definer* modify-macro (:name mm-name :body-name mm-def-body)
  `(define-modify-macro ,mm-name ,@mm-def-body))


@@ -375,7 +395,7 @@ function BUILD-DEFINITION-FORM.
;;; PREFIX-OPTIONS parameter.

(defmethod build-definition-form ((what list)
                                  (name t)
                                  name
                                  definition-forms
                                  &optional prefix-options)
  (build-definition-form (first (last what))
@@ -389,10 +409,9 @@ function BUILD-DEFINITION-FORM.
  (nth-value 0 (ignore-errors (subtypep x t))))


#|
;;; The next ones are commented because I am experimenting with different
;;; syntax.

#|
(defmethod build-definition-form :around ((what (eql 'function))
                                          (name symbol)
                                          definition-forms
+1 −3
Original line number Diff line number Diff line
@@ -15,8 +15,6 @@
The DEFINER library adds a simple macro DEF to Common Lisp that
replaces the various 'def*' forms in the language.  It is a simple
hack, but it adds some elegance to the language.  Of course, it comes
with its own way to be extended."
  )

with its own way to be extended.")

;;;; end of file -- definer.system --