Skip to content

Commit

Permalink
Added experimental module readers for plain and s-expression Javascript.
Browse files Browse the repository at this point in the history
Each language creates a module that defines and exports a single variable, "ans", containing a Javascript !begin block:

  #lang planet untyped/mirrors/javascript/plain
  function sum (a, b) { return a + b; }

  #lang planet untyped/mirrors/javascript/sexp
  (function sum (a b) (return (+ a b)))

The idea is to create a require-like mechanism that allows the programmer to specify dependencies between modules.
Once we have that, we have the basis for an automated JS build process.

And if it works with JS, it can work with CSS too.
  • Loading branch information
Dave Gurnell committed Jun 20, 2009
1 parent 1250e20 commit 3a064b6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
14 changes: 14 additions & 0 deletions javascript/plain/lang/reader.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(module reader syntax/module-reader
#:language `(planet "module.ss" ("untyped" "mirrors.plt" 2) "javascript/plain")
#:read
(lambda ([in (current-input-port)])
(let ([ast (with-syntax-errors (lambda () (parse-program-unit in)))])
(list `(#%module-begin ,@ast))))
#:read-syntax
(lambda ([source-name #f] [in (current-input-port)])
(let ([ast (with-syntax-errors (lambda () (parse-program-unit in)))])
(list `(#%module-begin ,@ast))))
#:whole-body-readers? #t
(require "../../../base.ss")
(require (javascript-in private/compiler/compile
private/syntax/parse)))
31 changes: 31 additions & 0 deletions javascript/plain/module.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#lang scheme/base

(require (for-syntax scheme/base)
"../javascript.ss"
"../struct.ss")

;(define (extract-requires stx accum)
; (let loop ([stx stx] [req-accum null] [stmt-accum null])
; (syntax-case* stx (require) symbolic-identifer=?
; [((require arg ...) req+stmt ...)
; (loop #'(req+stmt ...)
; (cons #'(require arg ...) req-accum)
; stmt-accum)]
; [(stmt req+stmt ...)
; (loop #'(req+stmt ...)
; req-accum
; (cons #'stmt stmt-accum))]
; [() #`(#,@(reverse req-accum)
; #,@(reverse stmt-accum))])))

(define-syntax (module-begin stx)
(syntax-case stx ()
[(module-begin) #'(#%plain-module-begin (begin #f))]
[(module-begin stmt ...)
#'(#%plain-module-begin
(define ans (make-BeginStatement #f (list stmt ...)))
(display (javascript->pretty-string ans))
(provide ans))]))

(provide (rename-out [module-begin #%module-begin])
(except-out (all-from-out scheme/base) #%module-begin))
2 changes: 2 additions & 0 deletions javascript/sexp/lang/reader.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(module reader syntax/module-reader
#:language `(planet "module.ss" ("untyped" "mirrors.plt" 2) "javascript/sexp"))
35 changes: 35 additions & 0 deletions javascript/sexp/module.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#lang scheme/base

(require (for-syntax scheme/base
(planet untyped/unlib:3/debug)
(planet untyped/unlib:3/syntax))
"../javascript.ss")

(define-for-syntax (extract-requires stx)
(let loop ([stx stx] [req-accum null] [stmt-accum null])
(syntax-case* stx (require) symbolic-identifier=?
[((require arg ...) req+stmt ...)
(loop #'(req+stmt ...)
(cons #'(require arg ...) req-accum)
stmt-accum)]
[(stmt req+stmt ...)
(loop #'(req+stmt ...)
req-accum
(cons #'stmt stmt-accum))]
[() #`(#,(reverse req-accum)
#,(reverse stmt-accum))])))

(define-syntax (module-begin stx)
(syntax-case stx ()
[(module-begin) #'(#%plain-module-begin (begin #f))]
[(module-begin req+stmt ...)
(with-syntax ([((require ...) (stmt ...))
(extract-requires #'(req+stmt ...))])
#'(#%plain-module-begin
require ...
(define ans (js stmt ...))
(display (javascript->pretty-string ans))
(provide ans)))]))

(provide (rename-out [module-begin #%module-begin])
(except-out (all-from-out scheme/base) #%module-begin))

0 comments on commit 3a064b6

Please sign in to comment.