Added compile-time constants

* (compile-time-const.scm) : added a small utility for compile time constants based on eval and syntax-case.
1 files changed, 28 insertions(+), 0 deletions(-)

A => compile-time-const.scm
A => compile-time-const.scm +28 -0
@@ 0,0 1,28 @@ 
+;; (compile-time-const expr)
+;; eval's expr in the current module and replaces itself with the
+;; result QUOTED. 
+;;
+;; Example repl session:
+;; > ,expand (define (test) (compile-time-eval (curent-time)))
+;; $1 = (define (test) 1600362567)
+
+(define-syntax compile-time-const
+  (lambda (stx)
+    (syntax-case stx ()
+      ((_ expr)
+       (let ((thing (eval (syntax->datum #'expr) (current-module))))
+         (display (list? thing))
+           (datum->syntax stx `(quote ,thing)))))))
+
+
+;; define-inline-const. Like compile-time-const, but
+;; binds it to an identifier macro to inline it wherever it is
+;; used. In guile 3 you probably don't need this, since declarative
+;; modules will most certainly inline constants.
+(define-syntax define-inline-const
+  (lambda (stx)
+  (syntax-case stx ()
+    ((_ name val)
+     (let ((thing (eval (syntax->datum #'val) (current-module))))
+       (with-syntax ((val (datum->syntax stx thing)))
+         #'(define-syntax name (identifier-syntax val))))))))