Reader module affect by using `require`?

We can define a reader module language by

; literal/main.rkt
(module reader racket
  (provide (rename-out [literal-read read]
                       [literal-read-syntax read-syntax]))
  (define (literal-read in) ...)
  (define (literal-read-syntax src in) ...))

and using it by #lang literal, but if I just want (require literal) to get the same effect?

What do you mean by "the same effect"? It's not possible for the (require literal) form to affect the result of reading the rest of the file because reading the whole file happens before expansion. If you want (require literal) to affect the results of calling read then you can set various parameters that control the reader.

3 Likes

I want I can write

#lang racket/base
(require literal)

not

#lang literal

because I want it able to work with different base languages like

#lang racket
(require literal)
; or
#lang nanopass
(require literal)
; or
#lang racket/gui
(require literal)

because I want it able to work with different base languages like

#lang racket
(require literal)
; or
#lang nanopass
(require literal)
; or
#lang racket/gui
(require literal)

One way to do this sort of thing is to make a "meta-language" like exact-decimal or at-exp. The syntax/module-reader provides help, particularly through the function make-meta-reader.

Then you could write your examples as

#lang literal racket
; or
#lang literal nanopass
; or
#lang literal racket/gui

-Philip

3 Likes

This is good enough, but what if I want a mixed result? For example, we write

#lang ext1 racket ; get ext1
#lang ext2 racket ; get ext2

But I want ext1 and ext2 work in the same time

Meta languages appear to be chainable, FWIW.

#lang at-exp exact-decimal racket

1.0
@values{abc}

(You can also swap their positions. In this example, their ordering shouldn't matter)

5 Likes

Oh wow! That is awesome