[racket] Custom language using syntax/module-reader with #:wrapper1

103 views
Skip to first unread message

Nick Sivo

unread,
May 29, 2012, 7:44:57 PM5/29/12
to us...@racket-lang.org
Hi,

I'm trying to implement arc as a racket language extension. To become
familiar with racket's reader infrastructure, I thought I'd start with
something simple:

==================
kogir/arc/lang/reader.rkt:
==================
#lang s-exp syntax/module-reader
(planet kogir/arc/language)
#:wrapper1 arc-read-wrapper

(require (planet kogir/arc/reader))

==============
kogir/arc/reader.rkt:
==============
#lang racket/base

(provide make-arc-readtable
arc-read-wrapper)

(define (read-square-brackets ch port src line col pos)
(datum->syntax
#f
`(lambda (_)
,(read-syntax/recursive src port #\[ #f))
(let-values ([(l c p) (port-next-location port)])
(list src line col pos (and pos (- p pos))))))

(define (make-arc-readtable)
(make-readtable (current-readtable)
#\[ 'terminating-macro read-square-brackets))

(define (arc-read-wrapper thunk read-syntax)
(parameterize ([current-readtable (make-arc-readtable)])
(thunk)))

================
kogir/arc/language.rkt:
================
#lang racket

(provide (all-defined-out)
(all-from-out racket))

I expected it to give me a language very similar to racket, but with
arc's [ _ ] lambda syntax sugar:
[+ _ 1] => (lambda (_) (+ _ 1))

When I test in Dr. Racket, it works for reading files, but not in the
interaction window:

======
test.arc:
======
#lang planet kogir/arc

(define test [+ _ 1])

---------------------------------------------------------------------------------------

Welcome to DrRacket, version 5.2.1 [3m].
Language: planet kogir/arc [custom]; memory limit: 128 MB.
> test
#<procedure:test>
> (test 1)
2
> [+ _ 1]
. _: wildcard not allowed as an expression in: _

What pieces am I missing to make the Dr. Racket interaction window
support the new syntax?

Also, is it correct in kogir/arc/reader.rkt to use
`(lambda (_)
,(read-syntax/recursive src port #\[ #f)
or should I use
#`(lambda (_)
#,(read-syntax/recursive src port #\[ #f)

Both appear to work?

Any pointers you might have would be awesome. I apologize in advance
if I missed something obvious.

Thanks,
Nick
____________________
Racket Users list:
http://lists.racket-lang.org/users

Matthias Felleisen

unread,
May 30, 2012, 12:18:57 PM5/30/12
to Nick Sivo, us...@racket-lang.org

Nick Sivo

unread,
May 30, 2012, 6:15:51 PM5/30/12
to Matthias Felleisen, us...@racket-lang.org
Thanks for the ideas, Matthias.

I don't currently provide my own #%top-interaction. After reading the
documentation and running some tests, it appears #%top-interaction is
called too late - after the line in the interaction window has already
been read with the wrong readtable.

Eventually I discovered syntax/module-reader's #:language-info:
keyword and added a runtime configuration that replaces the
current-readtable. It *appears* to work, but if anyone knows of a
"more correct/idomatic" way to do it I'm all ears.

Cheers,
Nick

Reply all
Reply to author
Forward
0 new messages