Skip to content

Instantly share code, notes, and snippets.

@samdphillips
Created September 1, 2024 03:51
Show Gist options
  • Save samdphillips/baa4d71105e18644749d8aeebf92d845 to your computer and use it in GitHub Desktop.
Save samdphillips/baa4d71105e18644749d8aeebf92d845 to your computer and use it in GitHub Desktop.
Keyword bridge for R6RS
#lang racket/base
;; install in a kw-example collect
(require (for-syntax racket/base
racket/string
syntax/parse)
racket/format
syntax/parse/define)
(provide define-kwish
test)
(begin-for-syntax
(define-syntax-class maybe-kwish
#:attributes (translate)
(pattern id
#:declare id id
#:do [(define id-string
(symbol->string (syntax-e #'id)))]
#:when (string-suffix? id-string ":")
#:attr
translate
#`#,(string->keyword
(substring id-string 0 (sub1 (string-length id-string)))))
(pattern e
#:attr translate #'e)))
(define-syntax-parse-rule (kw-rewrite vs:maybe-kwish ...)
(vs.translate ...))
(define-syntax-parse-rule (define-kwish name:id orig-name:id)
(define-syntax-rule (name . rest) (kw-rewrite orig-name . rest)))
(define (test #:greeting [greeting "hello"]
#:location [location "world"])
(~a greeting " " location))
#!r6rs
(library (test-kw)
(export)
(import (rnrs base (6))
(only (rnrs io simple (6)) display newline)
(kw-example kw))
(define (displayln x) (display x) (newline))
(displayln (test))
(define-kwish kw-test test)
(displayln (kw-test greeting: "hola"
location: "mundo")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment