ECL/Wrapping constants

From formgames wiki
< ECL
Jump to: navigation, search

NOTE: This is just a list of personal bookmarks and notes made during my experiments with ECL. For more detailed explanations see ECL's Homepage and its official Documentation!

Example

DEFCONSTANT has to be wrapped into a LET block:

Example:

(ffi:clines "#define SOME_CONSTANT 123")
(let () (defconstant +some-constant+ (ffi:c-inline () () :int "SOME_CONSTANT" :one-liner t)))

Usage:

(format t "+some-constant+: ~a~%" +some-constant+)

Output:

+some-constant+: 123


Using DEFCONSTANT on the top-level

(ffi:clines "#define SOME_CONSTANT 123")
(defconstant +some-constant+ (ffi:c-inline () () :int "SOME_CONSTANT" :one-liner t))

results in an error being thrown:

;;; Error:
;;;   in file rhino-lisp-api.lisp, position 42
;;;   at (DEFCONSTANT +SOME-CONSTANT+ ...)
;;;   * The form (PROGN (SI:*MAKE-CONSTANT '+SOME-CONSTANT+ (FFI:C-INLINE NIL NIL :INT "SOME_CONSTANT" :ONE-LINER T)) (SI::REGISTER-GLOBAL '+SOME-CONSTANT+)) was not evaluated successfully.
;;; Error detected:
;;; The special form c-inline cannot be used in the interpreter: (NIL NIL INT SOME_CONSTANT ONE-LINER T)An error occurred during initialization:
COMPILE-FILE-ERROR while
compiling #<cl-source-file "rhino-lisp-api" "rhino-lisp-api">.
NMAKE : fatal error U1077: 'C:\Users\dietrich\home\cs\lang\lisp\ecl\64\git\install\ecl.exe' : return code '0x1'
Stop.


Documentation / References


Other examples from contrib/sockets/sockets.lisp

(eval-when (:compile-toplevel :execute)
  (defmacro c-constant (c-name)
    `(c-inline () () :int ,c-name :one-liner t))
  (defmacro define-c-constants (&rest args)
    `(let () ; Prevents evaluation of constant value form
       ,@(loop
	    for (lisp-name c-name) on args by #'cddr
	    collect `(defconstant ,lisp-name (c-constant ,c-name))))))
(define-c-constants
  +af-inet+ "AF_INET"
  +af-local+ #-sun4sol2 "AF_LOCAL" #+sun4sol2 "AF_UNIX"
  +eagain+ "EAGAIN"
  +eintr+ "EINTR")
(defmacro define-socket-condition (symbol name)
  `(let () ; Prevents evaluation of constant value at compilation time
     (defconstant ,symbol (c-constant ,(symbol-name symbol)))
     (define-condition ,name (socket-error)
       ((symbol :reader socket-error-symbol :initform (quote ,symbol))))
     (export ',name)
     (push (cons ,symbol (quote ,name)) *conditions-for-errno*)))
(defmacro define-name-service-condition (symbol name)
  `(let ()
     (defconstant ,symbol (c-constant ,(symbol-name symbol)))
     (define-condition ,name (name-service-error)
       ((symbol :reader name-service-error-symbol :initform (quote ,symbol))))
     (push (cons ,symbol (quote ,name)) *conditions-for-name-service-errno*)
     (export (quote ,symbol))))