inject symbols pasted by andyjpb on Tue Jul 9 14:58:29 2019

mac-mod.scm:

-----
(module mac-mod
	(definer
	  user)

(import chicken scheme)
(use-for-syntax matchable)


(define-syntax definer
  (ir-macro-transformer
    (lambda (stx inject compare)
      (match stx
	((definer NAME)
	 (let* ((NAME*        (inject NAME))
		(NAME*        (strip-syntax NAME))
		(syntax-NAME* (symbol-append 'syntax- NAME*)))
	   `(begin
	      (define-for-syntax ,syntax-NAME* "my-object-at-syntax-time")
	      (define            ,NAME*        "my-object-at-run-time"))))))))

(define-syntax user
  (ir-macro-transformer
    (lambda (stx inject compare)
      (match stx
	((user NEW-NAME OLD-NAME)
	 (let* ((NEW-NAME*        (inject NEW-NAME))
		(NEW-NAME*        (strip-syntax NEW-NAME))
		(syntax-NEW-NAME* (symbol-append 'syntax- NEW-NAME*))
		(OLD-NAME*        (inject OLD-NAME))
		(OLD-NAME*        (strip-syntax OLD-NAME))
		(syntax-OLD-NAME* (symbol-append 'syntax- OLD-NAME*))
		(object           (eval syntax-OLD-NAME*)))
	   `(begin
	      (define-for-syntax ,syntax-NEW-NAME* ,syntax-OLD-NAME*)
	      (define            ,NEW-NAME*        ,OLD-NAME*))))))))

)

;;; csc -feature compiling-extension -setup-mode -s -d2 mac-mod.scm -j mac-mod
;;; csc -feature compiling-extension -setup-mode -s mac-mod.import.scm -d0
;;; csc -feature compiling-extension -setup-mode -c -d2 mac-mod.scm -unit mac-mod -j mac-mod
-----


mac-prog.scm:

-----
(use mac-mod)

(definer thingy)

(printf "Got ~S at run-time\n" thingy)

(begin-for-syntax
  (printf "Got ~S at compile-time\n" syntax-thingy))

(user new-thingy thingy)

(printf "Got ~S at run-time\n" new-thingy)

(begin-for-syntax
  (printf "Got ~S at compile-time\n" syntax-new-thingy))

;;; csc mac-prog.scm
-----


mac-prog-mod.scm

-----
$ more mac-prog-mod.scm
(module mac-prog-mod
        ()

(import chicken scheme)
(use mac-mod)

(definer thingy)

(printf "Got ~S at run-time\n" thingy)

(begin-for-syntax
  (printf "Got ~S at compile-time\n" syntax-thingy))

(user new-thingy thingy)

(printf "Got ~S at run-time\n" new-thingy)

(begin-for-syntax
  (printf "Got ~S at compile-time\n" syntax-new-thingy))

)

;;; csc mac-prog-mod.scm

;;; csc -feature compiling-extension -setup-mode -s -d2 mac-prog-mod.scm -j mac-prog-mod
;;; csc -feature compiling-extension -setup-mode -s mac-prog-mod.import.scm -d0
;;; csc -feature compiling-extension -setup-mode -c -d2 mac-prog-mod.scm -unit mac-prog-mod -j mac-prog-mod
-----


Compile the module:

$ csc -feature compiling-extension -setup-mode -s -d2 mac-mod.scm -j mac-mod
$ csc -feature compiling-extension -setup-mode -s mac-mod.import.scm -d0
$ csc -feature compiling-extension -setup-mode -c -d2 mac-mod.scm -unit mac-mod -j mac-mod


Compile the program that uses the module in the top-level:

$ csc mac-prog.scm


Compile the module that uses the module:

$ csc mac-prog-mod.scm

->

-----
Got "my-object-at-syntax-time" at compile-time

Error: during expansion of (user ...) - unbound variable: mac-prog-mod#syntax-thingy

        Call history:

        <syntax>          (##core#set! thingy19 "my-object-at-run-time")
        <syntax>          (printf "Got ~S at run-time\n" thingy)
        <syntax>          (##core#let ((out28 ##sys#standard-output)) (##sys#check-output-port out28 #t (quote printf)) (##sys......
        <syntax>          (##core#begin (##sys#check-output-port out28 #t (quote printf)) (##sys#print "Got " #f out28) (##sys......
        <syntax>          (##sys#check-output-port out28 #t (quote printf))
        <syntax>          (quote printf)
        <syntax>          (##core#quote printf)
        <syntax>          (##sys#print "Got " #f out28)
        <syntax>          (##sys#print thingy #t out28)
        <syntax>          (##sys#print " at run-time\n" #f out28)
        <syntax>          (begin-for-syntax (printf "Got ~S at compile-time\n" syntax-thingy))
        <syntax>          (##core#elaborationtimeonly (##core#begin (printf "Got ~S at compile-time\n" syntax-thingy)))
        <syntax>          (##core#begin (printf "Got ~S at compile-time\n" syntax-thingy))
        <syntax>          (printf "Got ~S at compile-time\n" syntax-thingy)
        <eval>    (printf "Got ~S at compile-time\n" syntax-thingy)
        <syntax>          (user new-thingy thingy)      <--

Error: shell command terminated with non-zero exit status 17920: '/usr/local/chicken-4.9.0/bin/chicken' 'mac-prog-mod.scm' -output-file 'mac-prog-mod.c'
-----

r-t + c-t added by Bunny351 on Tue Jul 9 20:02:53 2019


(module c-t-r (definer user c-t)
(import scheme (chicken base) (chicken syntax))
(import-for-syntax (srfi 69))

(define-for-syntax c-t-info (make-hash-table))

(define-syntax definer
  (syntax-rules ()
    ((_ name)
     (definer name "r-t-object"))
    ((_ name val)
     (begin
       (define name val)
       (register-entity name)))))

(define-syntax register-entity
  (er-macro-transformer
    (lambda (x r c)
      (let ((name (strip-syntax (cadr x)))
            (val (if (pair? (cddr x)) 
                     (hash-table-ref c-t-info (caddr x))
                     "c-t-object")))
        (hash-table-set! c-t-info name val)
        `(,(r 'void))))))

(define-syntax c-t
  (er-macro-transformer
    (lambda (x r c)
      (let ((name (strip-syntax (cadr x))))
        `(,(r 'quote) ,(hash-table-ref c-t-info name))))))

(define-syntax user
  (syntax-rules ()
    ((_ new old)
     (begin
       (definer new old)
       (register-entity new old)))))

)

; csc -J c-t-r.scm -s
; csc -s c-t-r.import.scm

----

(module main ()
(import scheme (chicken base))
(import c-t-r)
(import (chicken format))

(definer thingy)

(printf "Got ~S at run-time\n" thingy)

(printf "Got ~S at compile-time\n" (c-t thingy))

(user new-thingy thingy)

(printf "Got ~S at run-time\n" new-thingy)

(printf "Got ~S at compile-time\n" (c-t new-thingy))
)