[plt-scheme] ajax and plt

From: Noel Welsh (noelwelsh at gmail.com)
Date: Fri Jul 10 04:18:35 EDT 2009

On Wed, Jul 8, 2009 at 9:39 PM, Marco Monteiro<masm at acm.org> wrote:
> I want to port the compiler to PLT-Scheme. My idea is to somehow
> reuser the module and macro system of PLT. I'm just starting with PLT,
> so I need advice about the best way to do this.
> What I was thinking about was that I could write a module using #lang sines,
> for example. The forms in this module would be expanded to the core
> syntactic
> forms in PLT-Scheme (the ones in chapter two of the reference manual) by
> PLT-Scheme.
> Then the compiler takes these and generates Javascript (maybe using
> javascript.plt)
> and serve it with the web server or maybe put it in a file.

Yes, that's the way to do it.

Basically you want to define your own macros for define etc. which
expand into the kernel syntax (as you said). Then you probably want to
implement your own #%module-begin form to compile kernel syntax into
JS (after expanding the forms within the module).

Below is a really simple example of a language that compiles into JS
expressions. Call one file unajoy.ss and the other unajoy-test.ss and
put them in the same directory. Note that I don't implement
#%module-begin; I reuse PLT Scheme's.



#lang scheme/base

(require (planet untyped/mirrors/javascript/javascript))

(define-syntax js-define
  (syntax-rules ()
    [(js-define (name arg ...) expr0 expr ...)
      (js (function name (arg ...) ,expr0 ,expr ...)))]
    [(js-define name value)
      (js (var (name value))))]))

(define-syntax js-if
  (syntax-rules ()
    [(js-if test true false)
     (js (? test true false))]
    [(js-if test true)
     (js (? test true undefined))]))

(define-syntax js-fn-begin
  (syntax-rules ()
    [(js-fn-begin expr)
     (js (return ,expr))]
    [(js-fn-begin expr0 expr

 (rename-out [js-define define]
             [js-if if]))



#lang s-exp "unajoy.ss"

(define foo 42)

(define (bar x y)
  (if (> x 0)
      (+ x y)
      (- y x)))

(define (baz x y)
  (define z 0)
  (+ x y z))

Posted on the users mailing list.