Big refactoring of the ParenScript compiler.
[clinton/parenscript.git] / src / compilation-interface.lisp
CommitLineData
9da682ca
RD
1(in-package :parenscript)
2
4a987e2b 3(defun translate-ast (compiled-expr &key (output-stream *standard-output*) (output-spec :javascript) (pretty-print t))
9da682ca
RD
4 "Translates a compiled Parenscript program (compiled with COMPILE-PAREN-FORM)
5to a Javascript string. Outputs to the stream OUTPUT-STREAM in the language given
6by OUTPUT-SPEC, pretty printing if PRETTY-PRINT is non-null.
7
8OUTPUT-SPEC must be :javascript at the moment."
9da682ca
RD
9 (when (not (eql :javascript output-spec))
10 (error "Unsupported output-spec for translation: ~A" output-spec))
4a987e2b
VS
11 (write-string (string-join (ps-print compiled-expr 0)
12 (string #\Newline))
13 output-stream))
9da682ca 14
4a987e2b 15(defun compile-script (script-form &key (output-spec :javascript) (pretty-print t) (output-stream nil) (toplevel-p t))
9da682ca
RD
16 "Compiles the Parenscript form SCRIPT-FORM into the language specified by OUTPUT-SPEC.
17Non-null PRETTY-PRINT values result in a pretty-printed output code. If OUTPUT-STREAM
18is NIL, then the result is a string; otherwise code is output to the OUTPUT-STREAM stream.
9da682ca
RD
19
20This is the main function used by Parenscript users to compile their code to Javascript (and
21potentially other languages)."
22 (macrolet ((with-output-stream ((var) &body body)
23 `(if (null output-stream)
24 (with-output-to-string (,var)
25 ,@body)
26 (let ((,var output-stream))
27 ,@body))))
28 (with-output-stream (stream)
4a987e2b
VS
29 (translate-ast (compile-parenscript-form script-form)
30 :output-stream stream
31 :output-spec output-spec
32 :pretty-print pretty-print))))
a98e58ee 33
b5be3f57 34(defun ps-to-string (expr)
4a987e2b 35 (string-join (ps-print (compile-parenscript-form expr) 0) (string #\Newline)))
b5be3f57 36
4a987e2b 37(defmacro ps (&body body)
a9fce0a7 38 "A macro that returns a Javascript string of the supplied Parenscript forms."
4a987e2b 39 `(ps* '(progn ,@body)))
a9fce0a7 40
4a987e2b 41(defun ps* (&rest body)
a9fce0a7 42 "Return the javascript string representing BODY.
a9fce0a7 43Body is evaluated."
4b5d1808 44 (compile-script `(progn ,@body)))