define-toplevel-macro defines a macro that expands into one or more Coalton top-level forms.
Syntax
(define-toplevel-macro ⟨name⟩ ⟨lambda-list⟩
⟨docstring⟩?
⟨declaration⟩*
⟨lisp-body⟩+)
Semantics
define-toplevel-macrois a Common Lisp top-level form (used outsidecoalton-toplevel).⟨lambda-list⟩is a standard Common Lisp macro destructuring lambda list. (When writing macros in a package that uses#:coaltonrather than#:cl, qualify lambda-list keywords such ascl:&optional,cl:&rest,cl:&body, andcl:&key.)- The macro body executes as Common Lisp at compile time and returns a Coalton top-level form.
- When
⟨name⟩is invoked insidecoalton-toplevel,coalton-codegen,pprint-coalton-codegen, or related wrapper forms, the parser expands it and parses the result as a top-level form. - To emit multiple declarations, attributes, or definitions from a single macro invocation, wrap the returned forms in
(coalton:progn ...).
Examples
;; Emit both a declaration and a definition
(define-toplevel-macro define-constant (name type value)
"Declare and define a top-level constant."
`(progn
(declare ,name ,type)
(define ,name ,value)))
;; Emit a transparent wrapper type together with an Into instance
(define-toplevel-macro define-tagged-id (name underlying-type)
`(progn
(repr :transparent)
(derive Eq Hash Show)
(define-type ,name
(,name ,underlying-type))
(define-instance (Into ,name ,underlying-type)
(define (into (,name x)) x))))
(coalton-toplevel
(define-constant default-port U16 8080)
(define-tagged-id UserId U64))
Other notes
define-toplevel-macrorecords:toplevelas the symbol’s:coalton-repl-contextproperty for editor integration, REPL support, and documentation generation.- If a macro is meant to be called directly at the Common Lisp top level (outside
coalton-toplevel) and expand into(coalton-toplevel ...), define it withcl:defmacroinstead. - See Macros for a comparison of expression macros, top-level macros, and Common Lisp macros.