program-saver/messing-with-defun.lisp
2025-01-21 14:20:13 +03:00

46 lines
1.7 KiB
Common Lisp

;; This is just proof-of-concept.
;; This "library" is not meant to be seriously used - but it *can* be used
;;
;; The idea is this: what if you could, actually develop purely from the repl?
;; This idea comes from my (admittedly very briefly) playing with the interlisp medley
;; project. Essentially, you write things into the repl. Function definitions, packages,
;; whatever. Then you call, say, (save-definitions) or some function, and
;; everything you've defined gets saved into a file.
;; Obviously very possible, but I wanted to implement it myself.
;; whether this is a sane way to develop a program, or if this is desirable
;; in any way whatsoever, is left as an exercise for the reader.
(defpackage :program-saver
(:use :cl)
(:shadow :defun :defparameter :defvar))
(in-package :program-saver)
;; essentially, we shadow defun, defparameter etc.
;; and replace them with identical ones that add the definition
;; to a global list.
;; I should probably add some more stuff here -
;; everything is just saved as if they're all in the same file.
(defparameter *definitions* nil)
(defmacro defun (name lambda-list &body body)
(push (list 'defun name lambda-list body) *definitions*)
`(cl:defun ,name ,lambda-list ,@body))
(defmacro defvar (name val)
(push (list 'defvar name val) *definitions*)
`(cl:defvar ,name ,val))
(defmacro defparameter (name val)
(push (list 'defparameter name val) *definitions*)
`(cl:defparameter ,name ,val))
(cl:defun reset-defs ()
(setf *definitions* nil))
(cl:defun save-definitions (fname)
(with-open-file (out fname :direction :output)
(dolist (i (reverse *definitions*))
(print i out))))
; now just (use-package :program-saver)