srfi-89.scm 5.6 KB
Newer Older
wanderer's avatar
wanderer committed
1 2 3 4
;;; (srfi srfi-89) --- Guile srfi-89 implementation.

;; Copyright (C) 2020 Martin Becze <mjbecze@riseup.net>
;;
5
;; This file is part of guile-srfi-89.
wanderer's avatar
wanderer committed
6
;;
7
;; guile-srfi-89 is free software: you can redistribute it and/or modify
wanderer's avatar
wanderer committed
8 9 10 11
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
wanderer's avatar
wanderer committed
12
;; guile-srfi-89 is distributed in the hope that it will be useful, but
wanderer's avatar
wanderer committed
13 14 15 16 17 18 19 20 21
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with guile-json. If not, see https://www.gnu.org/licenses/.

;;; Commentary:

wanderer's avatar
wanderer committed
22
;; A hygenic implementation srfi-89 for Guile
wanderer's avatar
wanderer committed
23 24 25

;;; Code:

wanderer's avatar
init  
wanderer committed
26
(library (srfi srfi-89)
wanderer's avatar
wanderer committed
27
  (export define* lambda*)
wanderer's avatar
init  
wanderer committed
28
  (import (rnrs)
29
	  (rnrs syntax-case (6))
wanderer's avatar
init  
wanderer committed
30 31
	  (prefix (guile) guile:)))

wanderer's avatar
wanderer committed
32
(guile:cond-expand-provide (guile:current-module) '(srfi-89))
wanderer's avatar
init  
wanderer committed
33 34

(define-syntax with-syntax*
wanderer's avatar
wanderer committed
35 36 37 38 39 40 41 42 43
  (syntax-rules ()
    ((_ ((i v)) e1 e2 ...)
     (with-syntax ((i v))
       e1 e2 ...))
    ((_ ((i v) i1 i2 ...) e1 e2 ...)
     (with-syntax ((i v))
       (with-syntax* (i1 i2 ...)
		     e1 e2 ...)))))

44
(define (remove-keywords ls)
wanderer's avatar
wanderer committed
45 46
  (cond
   ((null? ls) ls)
47
   ((guile:keyword? (car ls)) (remove-keywords (cddr ls)))
wanderer's avatar
wanderer committed
48 49 50
   (else ls)))

(define-syntax lambda*
wanderer's avatar
init  
wanderer committed
51
  (lambda (x)
wanderer's avatar
wanderer committed
52 53
    (define (generate-temporary)
      (car (generate-temporaries '(_))))
wanderer's avatar
init  
wanderer committed
54

wanderer's avatar
wanderer committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    (define (kword->sym x keyword)
      (datum->syntax x (guile:keyword->symbol (syntax->datum keyword))))

    (define (required-positional? var)
      (identifier? var))

    (define (optional-positional? var)
      (syntax-case var ()
	((var exp)
	 (identifier? #'var) #t)
	(else #f)))

    (define (positional? var)
      (or (required-positional? var)
	  (optional-positional? var)))

    (define (generate-key-error-message x)
      (string-append "key " (symbol->string (syntax->datum x)) " is required"))

    (define (zip a b)
      (syntax-case (list a b) ()
	((() ()) #'())
	(((a) (b)) #'((a b)))
	(((a a1 ...) (b b1 ...))
	 (with-syntax (((r ...) (zip #'(a1 ...) #'(b1 ...))))
	   #'((a b) r ...)))))

    (define (keyword? x)
      (guile:keyword? (syntax->datum x)))
wanderer's avatar
init  
wanderer committed
84

wanderer's avatar
wanderer committed
85 86
    ;; <named> --> <required named> |  <optional named>
    (define (parse-named ctx x)
wanderer's avatar
init  
wanderer committed
87
      (syntax-case x ()
wanderer's avatar
wanderer committed
88 89 90 91 92 93 94
	;; <required named> --> ( <keyword> <variable> )
	(((key var) n ...)
	 (keyword? #'key)
	 (with-syntax* ((k (kword->sym ctx #'key))
			(((named ...) (vars ...) r) (parse-named ctx #'(n ...)))
			(error-message (generate-key-error-message #'k)))

95
		       #'(((k (guile:error error-message)) named ...) (var vars ...) r)))
wanderer's avatar
wanderer committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	;; <optional named> --> ( <keyword> <variable> <expression> )
	(((key var exp) n ...)
	 (keyword? #'key)
	 (with-syntax* ((k (kword->sym ctx #'key))
			(((named ...) (vars ...) r) (parse-named ctx #'(n ...))))

		       #'(((k exp) named ...) (var vars ...) r)))
	((rest ...)
	 #'(() () (rest ...)))))

    ;; <positional section> --> <required positional>* <optional positional>*
    (define (parse-pos x)
      (syntax-case x ()
	(() #'(() () ()))
	((var r ...)
	 (if (required-positional? #'var)
	     (with-syntax ((((pos ...) opts rest) (parse-pos #'(r ...))))
	       #'((var pos ...) opts rest))
	     (parse-pos-optional x)))))

    ;; <optional positional> --> ( <variable> <expression> )
    (define (parse-pos-optional x)
      (syntax-case x ()
	(((var exp) r ...)
	 (identifier? #'var)
	 (with-syntax (((pos (opts ...) rest) (parse-pos-optional #'(r ...))))
	   #'(pos ((var exp) opts ...) rest)))
	((rest ...)
	 #'(() () (rest ...)))))
wanderer's avatar
init  
wanderer committed
125

wanderer's avatar
wanderer committed
126 127 128 129 130 131
    (syntax-case x ()
      ((_ () body ...) #'(lambda () body ...))
      ((_ (formal formal1 ... . rest) body ...)
       (if (positional? #'formal)
	   ;; <extended def formals> --> <positional section> <named section>? <rest section>
	   (with-syntax* ((((pos ...) (opt ...) r1) (parse-pos #'(formal formal1 ...)))
132
			  ((((named exp) ...) vars remainder) (parse-named x #'r1))
wanderer's avatar
wanderer committed
133 134
			  ((wrapper ...) (zip #'vars #'(named ...)))
			  (temp-rest (generate-temporary))
135 136 137 138
			  ((sanitizeded-rest ...) ((lambda ()
						     (if (identifier? #'rest)
							 #'((rest (remove-keywords temp-rest)))
							 #'())))))
139 140 141 142 143 144 145 146 147 148

			 (if (null? (syntax->datum #'remainder))
			     #'(guile:lambda* (pos ...
						 #:optional opt ...
						 #:key (named exp) ...
						 #:rest temp-rest)
					    (let (sanitizeded-rest ...
								   wrapper ...)
					      body ...))
			     #'(guile:syntax-error "Invalid parameters" remainder)))
wanderer's avatar
wanderer committed
149 150
	   ;; <extended def formals> --> <named section>? <positional section> <rest section>
	   (with-syntax* (((((named exp) ...) vars r1) (parse-named x #'(formal formal1 ...)))
151
			  (((pos ...) (opt ...) remainder) (parse-pos #'r1))
wanderer's avatar
wanderer committed
152 153
			  ((wrapper ...) (zip #'vars #'(named ...)))
			  (temp-rest (generate-temporary))
154 155 156 157
			  ((rest ...) ((lambda ()
					 (if (identifier? #'rest)
					     #'(#:rest rest)
					     #'())))))
158 159 160 161 162 163 164 165 166 167 168 169

			 (if (null? (syntax->datum #'remainder))
			     #'(guile:lambda* (#:key (named exp) ... #:rest temp-rest)
					    (let (wrapper ...)
					      (apply
					       (guile:lambda* (pos ...
								   #:optional opt ...
								   rest ...
								   )
							      body ...)
					       (remove-keywords temp-rest))))
			     #'(guile:syntax-error "Invalid parameters" remainder))))))))
wanderer's avatar
wanderer committed
170 171 172 173 174 175 176 177 178 179

(define-syntax define*
  (syntax-rules ()
    ;; define* ( <variable> <extended def formals> ) <body>
    ((_ (var formals ... . rest) body ... )
     (define var
       (lambda* (formals ... . rest) body ...)))
    ;; define* <variable> <expression>
    ((_ var exp)
     (define var exp))))