Guest User

Untitled

a guest
Jul 12th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.34 KB | None | 0 0
  1. ;; -*- lexical-binding: t -*-
  2.  
  3. ;; this will only work if lexical-binding is t
  4. ;; this does not yet support non-toplevel defun/defvar expressions
  5. ;; we need a
  6. ;; thing that pushes globals to fns/vars
  7. ;; and returns a body with nil in place of them
  8. ;; (labels (P (sexp) (cond ((atom? sexp) ...) ...)))
  9. ;; needs to handle defuns declared in defuns, defvars declared in defuns,
  10. ;; whatever stuff you can do
  11.  
  12. (defmacro defmodule (module-name export-list &rest body)
  13.   (labels ((defun? (sexp) (and (listp sexp)
  14.                    (eq (car sexp) 'defun)))
  15.        (defun-name (sexp) (cadr sexp))
  16.        (defun-args (sexp) (caddr sexp))
  17.        (defun-body (sexp) (cadddr sexp))
  18.        (defvar? (sexp) (and (listp sexp)
  19.                 (eq (car sexp) 'defvar)))
  20.        (defvar-name (sexp) (cadr sexp))
  21.        (defvar-value (sexp) (caddr sexp))
  22.        (export? (sym) (member sym (cdr export-list))))
  23.   (let ((fns nil)
  24.     (vars nil)
  25.     (b nil))
  26.     (dolist (sexp body)
  27.       (cond ((defun? sexp)
  28.          (if (export? (defun-name sexp))
  29.          (push sexp b)
  30.            (push sexp fns)))
  31.         ((defvar? sexp)
  32.          (push sexp vars))
  33.         (t
  34.          (push sexp b))))
  35.     `(let (,@(mapcar (lambda (def) (list (defvar-name def)
  36.                      (defvar-value def)))
  37.              vars))
  38.        (labels (,@(mapcar (lambda (fn) (list (defun-name fn)
  39.                          (defun-args fn)
  40.                          (defun-body fn)))
  41.               fns))
  42.      ,@b)))))
Add Comment
Please, Sign In to add comment