Skip to content

Instantly share code, notes, and snippets.

@tani
Last active July 4, 2021 15:04
Show Gist options
  • Save tani/993c2eed029cdce54ed9ae85db768c0b to your computer and use it in GitHub Desktop.
Save tani/993c2eed029cdce54ed9ae85db768c0b to your computer and use it in GitHub Desktop.
SPACK is a toy package system for LISP.
;;
;; # SPACK
;;
;; SPACK is a toy package system for LISP.
;;
;; ## Syntax
;;
;; Predefined functions and variables are unprefixed.
;; User-defined functions and variables are prefixed by package
;; declared by `spack/defpackage'. There is a separator `/' between
;; the package and symbol name. `~' is an alias for current package.
;; You can import external symbols into the current package by
;; `spack/import', which takes a package and symbols as arguments.
;;
(defparameter spack/*package* 'spack)
(defmacro spack/defpackage (package)
`(defparameter spack/*package* ',package))
(defun spack/resolve (stream char)
(declare (ignore char))
(read-char stream t nil t)
(let ((sym (read stream t nil t)))
(intern (format nil "~a/~a" spack/*package* sym))))
(set-macro-character #\~ #'spack/resolve)
(defmacro spack/import-functions (package &rest symbols)
(when symbols
`(progn
(setf (fdefinition ',(intern (format nil "~a/~a" spack/*package* (car symbols))))
#',(intern (format nil "~a/~a" package (car symbols))))
(spack/import ,package ,@(cdr symbols)))))
;;
;; Example
;;
(spack/defpackage example-lib)
(defvar ~/pi 3.141592)
(defun ~/sayhello ()
(print ~/pi))
(spack/defpackage example-main)
(spack/import-functions example-lib sayhello)
(~/sayhello)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment