[racket] How to to make a program that will take a simple racket file, parse it with the reader in a way that I can expand it?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sat Jun 15 01:21:35 EDT 2013

There are a lot of details to get right in processing a module.  To really
get them all right, use get-module-code from syntax/modcode.  Here's an
example that produces expanded syntax rather than a compiled bytecode
expression, if that's what you want:

#lang racket
(require racket/runtime-path syntax/modcode)
(define-runtime-path code "code.rkt")
(syntax->datum
  (get-module-code code
    #:compile expand))

Put your file to expand in "code.rkt" in the same directory as that
program.  Obviously you can change the code to produce some other path
instead, as needed, but define-runtime-path worked for a quick example.


Carl Eastlund


On Fri, Jun 14, 2013 at 4:32 PM, Vincent St-Amour <stamourv at ccs.neu.edu>wrote:

> Ah, right, sorry about that. My previous example works at the REPL, but
> not in a module. This should work in both cases:
>
>     (parameterize ([read-accept-reader #t]
>                    [current-namespace (make-base-namespace)])
>       (expand (read-syntax fichier-programme in)))
>
>
> Vincent
>
>
> At Fri, 14 Jun 2013 16:23:18 -0400,
> Andre Mayers wrote:
> >
> > Thank you for your answer, it works partially.
> >
> > The read-syntax returns
> >       '(module fichier-programme racket
> >          (#%module-begin
> >            (define aa 7)
> >            (let ((bb 3)) (+ aa bb))))
> >
> > but expand cannot expand it. The error message is
> >       <unsaved editor>::1: module: unbound identifier;
> >       also, no #%app syntax transformer is bound in: module
> >
> > -----Message d'origine-----
> > De : Vincent St-Amour [mailto:stamourv at ccs.neu.edu]
> > Envoyé : 14 juin 2013 16:02
> > À : Andre Mayers
> > Cc : 'Racket'
> > Objet : Re: [racket] How to to make a program that will take a simple
> racket file, parse it with the reader in a way that I can expand it?
> >
> > The following should work:
> >
> >     (parameterize ([read-accept-reader #t])
> >       (expand (read-syntax fichier-programme in)))
> >
> > If you don't need to manipulate the expansion programmatically, you can
> use DrRacket's Macro Stepper instead.
> >
> > Vincent
> >
> >
> >
> > At Fri, 14 Jun 2013 15:48:40 -0400,
> > Andre Mayers wrote:
> > >
> > > [1  <multipart/alternative (7bit)>]
> > > [1.1  <text/plain; iso-8859-1 (quoted-printable)>] I want to make a
> > > little program that will take a simple racket file, parse it with the
> > > reader in a way that I can expand it.
> > >
> > > For example, I have the file programme.rkt that have the following
> line :
> > >
> > >                 #lang racket
> > >
> > >                 (define aa 7)
> > >
> > >                 (let ( [bb 3])
> > >
> > >                   (+ aa bb))
> > >
> > >
> > >
> > > I would like to do something like
> > >
> > >                 (define fichier-programme "C:\\fichier-programme.rkt")
> > >
> > >                 (define in (open-input-file fichier-programme))
> > >
> > >                 (expand (read-syntax fichier-programme in))
> > >
> > >                 (close-input-port in)
> > >
> > >
> > >
> > >
> > >
> > > André Mayers, Ph.D., M. Ps.
> > >
> > > professeur agrégé
> > >
> > > Département d'informatique
> > >
> > > Université de Sherbrooke
> > >
> > > Sherbrooke (Québec) Canada J1K 2R1
> > >
> > > tél: +1 819-821-8000 poste 62041
> > >
> > > fax: +1 819-821-8200
> > >
> > > andre.mayers at usherbrooke.ca
> > >
> > > http://info.usherbrooke.ca/AndreMayers
> > >
> > >
> > >
> > >
> > >
> > > De : users-bounces at racket-lang.org
> > > [mailto:users-bounces at racket-lang.org] De la part de Matt Jadud Envoyé
> > > : 14 juin 2013 14:38 À : Sam Tobin-Hochstadt Cc : Racket Objet : Re:
> > > [racket] Racket Blogging - How To??
> > >
> > >
> > >
> > > I had no idea!
> > >
> > >
> > >
> > > I take it this is my Jekyll replacement? Thank you!
> > >
> > >
> > >
> > > Cheers,
> > >
> > > Matt
> > >
> > >
> > >
> > >
> > >
> > > On Fri, Jun 14, 2013 at 11:31 AM, Sam Tobin-Hochstadt
> > > <samth at ccs.neu.edu>
> > > wrote:
> > >
> > > On Fri, Jun 14, 2013 at 11:24 AM, Ray Racine <ray.racine at gmail.com>
> wrote:
> > > > So I'm thinking about blogging about some aspect (positive) of
> > > > Racket
> > > coding
> > > > this weekend.  Not sure what the options are these days.
> > > >
> > > > A) Use a blogging site of choice + cut/paste into some online code
> > > > formatter, enscriptem etc..
> > > > B) Use Scribble to generate html -> host generated html on gitpages
> > > > or
> > > self
> > > > host.
> > > > C) I recall seeing at least 1 or 2 posts on the list about some
> > > > Racket
> > > > project(s) which facilitate Racket blogging.
> > > >
> > > > Recommendations?
> > >
> > > Frog: https://github.com/greghendershott/frog
> > >
> > > Sam
> > > ____________________
> > >   Racket Users list:
> > >   http://lists.racket-lang.org/users
> > >
> > >
> > >
> > > [1.2  <text/html; iso-8859-1 (quoted-printable)>]
> > >
> > > [2  <text/plain; us-ascii (7bit)>]
> > > ____________________
> > >   Racket Users list:
> > >   http://lists.racket-lang.org/users
> >
> >
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130615/811fb7bd/attachment-0001.html>

Posted on the users mailing list.