Projects : gbw-signer : gbw-signer_usrbin
1 | ;;;; Hygienic packages for gscm |
2 | ;;; J. Welsh, November 2017 |
3 | |
4 | ;;; Usage: |
5 | ;; |
6 | ;; ;; mypkg.scm |
7 | ;; (lambda (yourpkg) |
8 | ;; (define square (yourpkg 'square)) |
9 | ;; (define (foo x) (write x) (newline)) |
10 | ;; (define (bar x) (square (+ x 1))) |
11 | ;; (export foo bar)) |
12 | ;; |
13 | ;; ;; yourpkg.scm |
14 | ;; (lambda () |
15 | ;; (define (square x) (* x x)) |
16 | ;; (export square)) |
17 | ;; |
18 | ;; ;; myprog.scm |
19 | ;; (define + 'weird-custom-thing) |
20 | ;; (load "pkg.scm") |
21 | ;; (define mypkg (load-pkg "mypkg.scm" (load-pkg "yourpkg.scm"))) |
22 | ;; (import mypkg foo bar) |
23 | ;; (foo (bar 5)) |
24 | ;; |
25 | ;; $ gscm myprog.scm |
26 | ;; 36 |
27 | ;; |
28 | ;; Ideally, "import" would work internally too, but the current compiler has a |
29 | ;; quirk that prevents macros expanding to internal definitions. |
30 | |
31 | (define (load-pkg path . args) |
32 | ;; The package is closed in the immutable gscm environment (r5rs plus |
33 | ;; extensions), protecting it from top-level redefinitions. Dependencies can |
34 | ;; be injected via args. |
35 | (apply (eval (with-input-from-file path read) |
36 | (gales-scheme-environment)) |
37 | args)) |
38 | |
39 | (define-syntax import |
40 | (syntax-rules () |
41 | ((import pkg sym) |
42 | (define sym (or (pkg 'sym) |
43 | (error "import: not found in package" '(pkg sym))))) |
44 | ((import pkg sym . tail) |
45 | (begin (import pkg sym) |
46 | (import pkg . tail))))) |
47 | |
48 | (define-syntax export |
49 | (syntax-rules () |
50 | ((export sym) |
51 | (lambda (tmp-1cb5c81c936f8103) |
52 | (and (eq? tmp-1cb5c81c936f8103 'sym) sym))) |
53 | ((export sym . tail) |
54 | (lambda (tmp-1cb5c81c936f8103) |
55 | (if (eq? tmp-1cb5c81c936f8103 'sym) sym |
56 | ((export . tail) tmp-1cb5c81c936f8103)))))) |