Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
36 lines (34 sloc) 1.39 KB
(define (toplevel-process expr read-file process-macro process-binding process-expr combine)
(if (and (pair? expr) (eq? (car expr) 'defmacro))
(let ((name (cadr expr))
(args (caddr expr))
(value (car (cdddr expr))))
(process-macro name (list 'lambda args
(macroexpand value))))
(let ((expr (macroexpand expr)))
(cond ((and (pair? expr) (eq? (car expr) 'js-define))
(let* ((name (cadr expr))
(value (caddr expr))
(var-name (jsify-symbol name)))
(process-binding name (macroexpand value))))
((and (pair? expr) (eq? (car expr) 'load))
(let ((more-exprs (read-file (cadr expr))))
(combine (map
(lambda (x) (toplevel-process x read-file process-macro process-binding process-expr combine))
more-exprs))))
(else
(process-expr expr))))))
(define (toplevel-compile expr read-file)
(toplevel-process expr read-file
(lambda (name value-expr)
(add-toplevel-macro name (eval value-expr (interaction-environment)))
"null;\n")
(lambda (name value-expr)
(let ((var-name (jsify-symbol name)))
(add-toplevel-binding name var-name)
(string-append "var " var-name "="
(compile value-expr *toplevel-bindings*) ";null;\n")))
(lambda (expr)
(string-append (compile expr *toplevel-bindings*) ";\n"))
(lambda (results)
(fold-left string-append "" results))))