[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
02/14: combinators: Add 'define-compile-time-procedure'.
From: |
guix-commits |
Subject: |
02/14: combinators: Add 'define-compile-time-procedure'. |
Date: |
Mon, 20 Dec 2021 10:24:10 -0500 (EST) |
civodul pushed a commit to branch master
in repository guix.
commit ddf9345dfec208611261ab06052de47fe8873f88
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Sat Dec 18 17:54:23 2021 +0100
combinators: Add 'define-compile-time-procedure'.
* guix/combinators.scm (procedure-call-location): New syntax parameter.
(define-compile-time-procedure): New macro.
---
guix/combinators.scm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 48 insertions(+), 2 deletions(-)
diff --git a/guix/combinators.scm b/guix/combinators.scm
index 88ad09d..261d6bb 100644
--- a/guix/combinators.scm
+++ b/guix/combinators.scm
@@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès
<ludo@gnu.org>
+;;; Copyright © 2012-2017, 2021 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
;;; Copyright © 2020 Arun Isaac <arunisaac@systemreboot.net>
;;;
@@ -24,7 +24,9 @@
#:export (fold2
fold-tree
fold-tree-leaves
- compile-time-value))
+ compile-time-value
+ procedure-call-location
+ define-compile-time-procedure))
;;; Commentary:
;;;
@@ -100,4 +102,48 @@ evaluate to a simple datum."
(_ #`'#,(datum->syntax s val)))))))
v))))
+(define-syntax-parameter procedure-call-location
+ (lambda (s)
+ (syntax-violation 'procedure-call-location
+ "'procedure-call-location' may only be used \
+within 'define-compile-time-procedure'"
+ s)))
+
+(define-syntax-rule (define-compile-time-procedure (proc (arg pred) ...)
+ body ...)
+ "Define PROC as a macro such that, if every actual argument in a \"call\"
+matches PRED, then BODY is evaluated at macro-expansion time. BODY must
+return a single value in a type that has read syntax--e.g., numbers, strings,
+lists, etc.
+
+BODY can refer to 'procedure-call-location', which is bound to a source
+property alist corresponding to the call site.
+
+This macro is meant to be used primarily for small procedures that validate or
+process its arguments in a way that may be equally well performed at
+macro-expansion time."
+ (define-syntax proc
+ (lambda (s)
+ (define loc
+ #`(identifier-syntax
+ '#,(datum->syntax #'s (syntax-source s))))
+
+ (syntax-case s ()
+ ((_ arg ...)
+ (and (pred (syntax->datum #'arg)) ...)
+ (let ((arg (syntax->datum #'arg)) ...)
+ (syntax-parameterize ((procedure-call-location
+ (identifier-syntax (syntax-source s))))
+ body ...)))
+ ((_ actual (... ...))
+ #`((lambda (arg ...)
+ (syntax-parameterize ((procedure-call-location #,loc))
+ body ...))
+ actual (... ...)))
+ (id
+ (identifier? #'id)
+ #`(lambda (arg ...)
+ (syntax-parameterize ((procedure-call-location #,loc))
+ body ...)))))))
+
;;; combinators.scm ends here
- 05/14: gnu: vlang: Update to 0.2.4., (continued)
- 05/14: gnu: vlang: Update to 0.2.4., guix-commits, 2021/12/20
- 12/14: gnu: bpytop: Fix build., guix-commits, 2021/12/20
- 06/14: gnu: Add udpcast, guix-commits, 2021/12/20
- 10/14: doc: Fix wrong home-shepherd-configuration data type., guix-commits, 2021/12/20
- 14/14: doc: Remove extra closing paren., guix-commits, 2021/12/20
- 01/14: build-system/meson: Define build variables when cross-compiling., guix-commits, 2021/12/20
- 04/14: gnu: rust-rayon@1.5: Update dependencies., guix-commits, 2021/12/20
- 13/14: gnu: Add tkrev., guix-commits, 2021/12/20
- 08/14: gnu: rcs: Install man page for rcsfreeze., guix-commits, 2021/12/20
- 11/14: gnu: ghc-microlens: Update home page., guix-commits, 2021/12/20
- 02/14: combinators: Add 'define-compile-time-procedure'.,
guix-commits <=
- 07/14: doc: cookbook: Simplify inputs in examples., guix-commits, 2021/12/20
- 03/14: services: static-networking: Sanitize <network-address> values., guix-commits, 2021/12/20
- 09/14: gnu: Add rcshist., guix-commits, 2021/12/20