On Monday, March 20, 2017 at 7:25:35 PM UTC+1, Matthew Butterick wrote:
> On Mar 20, 2017, at 2:59 AM, Jan Hondebrink <jrhond...@gmail.com> wrote:
> 
> Testing top-interaction with expression: (#%top-interaction + 1 2)
> . +: unbound identifier;
> also, no #%app syntax transformer is bound in: +
> 
> So when you pass an expression through to the racket/base #%module-begin, it 
> can use the racket/base bindings, but if you pass an expression through to 
> the racket/base #%top-interaction it cannot. How can you make 
> #%top-interaction use the racket/base bindings?
> 
> 
> 
> 
> When you start a source file with this line:
> 
> 
> #lang s-exp "ME-expander.rkt"
> 
> 
> You are telling Racket to 1) parse the code into expressions using the 
> default `s-exp` (aka Racket-style) reader and 2) use "ME-expander.rkt" as the 
> expander module.
> 
> 
> The expander module supplies the initial set of bindings for the code. 
> Conversely, if the expander doesn't provide a certain binding, the code can't 
> use it, and you get an unbound-identifier error.
> 
> 
> Even though "ME-expander.rkt" is written with `#lang racket/base`, it does 
> not automatically export the bindings from `racket/base`. If that's the 
> behavior you want, you need to add this to "ME-expander.rkt":
> 
> 
> (provide (all-from-out racket/base))
> 
> 
> Though you have to be a little careful if you plan to override bindings from 
> `racket/base` with your own (say, `#%top-interaction` and `#%module-begin`). 
> In that case, you can omit those bindings with `except-out`:
> 
> 
> (provide (except-out (all-from-out racket/base) #%top-interaction 
> #%module-begin))

Thanks for the explanation! It turns out that I don't need "ME-expander.rkt" to 
export any bindings apart from #%module-begin and #%top-interaction. I 
overcomplicated Greg's suggestion to supply my own #%top-interaction by 
thinking it needed to pass its result "up the chain" to the racket/base 
#%top-interaction. Just as Beautiful Racket taught me to do with #%module-begin.

But for #%top-interaction it is not necessary. In "ME-expander.rkt" the 
following works:

#lang racket/base

(require (for-syntax racket/base))

(define-syntax (ME-module-begin stx)
  (syntax-case stx ()
    [(_ s-expressions ...)
     #'(#%module-begin (apply values
                              (for/list ([expr (list 's-expressions ...)])
                                (m-eval expr global-environment)))
                       )]))

(define-syntax (#%top-interaction stx)
  #`(m-eval '#,(cdr (syntax-e stx)) global-environment))

(provide (rename-out (ME-module-begin #%module-begin))
         #%top-interaction)

(define (m-eval exp env)
  (...

---
So let me try and formulate my understanding if this: 
when using #lang s-expr "ME-expander.rkt" in a source file:
- The code in the file (DrRacket definition window) is caught by ME-expander's 
ME-module-begin which wraps the transformed code in (#%module-begin ...) which 
is then evaluated using the bindings from racket/base + the bindings in 
ME-expander (although I'm still a bit shady on how *exactly* that works)
- Anything typed in the REPL window is caught by ME-expander's 
#%top-interaction which just transforms code without wrapping it in a special 
form, which is then evaluated using the bindings from racket/base + the 
bindings in ME-expander.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to