1 (in-package :parenscript
)
3 (defmacro with-new-compilation-environment
((var) &body body
)
4 `(let* ((,var
(make-basic-compilation-environment))
5 (*compilation-environment
* ,var
))
8 (defun translate-ast (compiled-expr
10 (comp-env *compilation-environment
*)
11 (output-stream *standard-output
*)
12 (output-spec :javascript
)
14 "Translates a compiled Parenscript program (compiled with COMPILE-PAREN-FORM)
15 to a Javascript string. Outputs to the stream OUTPUT-STREAM in the language given
16 by OUTPUT-SPEC, pretty printing if PRETTY-PRINT is non-null.
18 OUTPUT-SPEC must be :javascript at the moment."
19 (declare (ignore comp-env
))
20 (when (not (eql :javascript output-spec
))
21 (error "Unsupported output-spec for translation: ~A" output-spec
))
22 (when (eql :javascript output-spec
)
23 ; (if (not pretty-print)
24 ; (js-translate compiled-expr :statement output-stream)
25 (write-string (string-join
26 (ps-js::js-to-statement-strings compiled-expr
0)
30 (defun non-nil-comp-env ()
31 "Returns a sane compilation environment. Either the one currently bound or a new
33 (or *compilation-environment
*
34 (make-basic-compilation-environment)))
37 (defun compile-script (script-form
39 (output-spec :javascript
)
43 (comp-env (non-nil-comp-env)))
44 "Compiles the Parenscript form SCRIPT-FORM into the language specified by OUTPUT-SPEC.
45 Non-null PRETTY-PRINT values result in a pretty-printed output code. If OUTPUT-STREAM
46 is NIL, then the result is a string; otherwise code is output to the OUTPUT-STREAM stream.
47 COMP-ENV is the compilation environment in which to compile the form.
49 This is the main function used by Parenscript users to compile their code to Javascript (and
50 potentially other languages)."
51 (macrolet ((with-output-stream ((var) &body body
)
52 `(if (null output-stream
)
53 (with-output-to-string (,var
)
55 (let ((,var output-stream
))
57 ;; we might want to bind this rather than set it
58 (setf (comp-env-compiling-toplevel-p comp-env
) toplevel-p
)
59 (with-output-stream (stream)
60 (let* ((*compilation-environment
* comp-env
)
61 (compiled (let ((first-result (compile-parenscript-form comp-env script-form
)))
65 (setf (comp-env-compiling-toplevel-p comp-env
) nil
)
66 (compile-parenscript-form comp-env first-result
))))))
69 ; (compile-script-form script-form :comp-env comp-env)
72 :output-spec output-spec
73 :pretty-print pretty-print
)))))
75 (defun compile-script-file (source-file
77 (output-spec :javascript
)
78 (comp-env (non-nil-comp-env))
80 (output-stream *standard-output
*))
81 "Compiles the given Parenscript source file and outputs the results
82 to the given output stream."
83 (setf (comp-env-compiling-toplevel-p comp-env
) t
)
84 (with-open-file (input source-file
:direction
:input
)
85 (let ((end-read-form '#:unique
))
87 (parenscript.reader
:read input nil end-read-form
)))
88 (macrolet ((with-output-stream ((var) &body body
)
89 `(if (null output-stream
)
90 (with-output-to-string (,var
)
92 (let ((,var output-stream
))
94 (let* ((*compilation-environment
* comp-env
)
96 (do ((form (read-form) (read-form))
98 ((eql form end-read-form
)
100 (setf (comp-env-compiling-toplevel-p comp-env
) nil
)
101 (compile-parenscript-form
103 `(progn ,@(nreverse compiled-forms
)))))
104 (let ((tl-compiled-form
105 (compile-parenscript-form comp-env form
)))
106 (push tl-compiled-form compiled-forms
)))))
107 (with-output-stream (output)
111 :output-stream output
112 :output-spec output-spec
113 :pretty-print pretty-print
))))))))
115 ;(defun compile-script-asdf-component (component
117 ; (output-spec :javascript)
119 ; (output-to-stream t)
120 ; (output-stream *standard-output*)
121 ; output-to-files ;; currently ignored
122 ; (comp-env (non-nil-comp-env)))
123 ; "Compiles any ASDF:COMPONENT and its dependencies "
125 (defun compile-script-system (system
127 (output-spec :javascript
)
130 (output-stream *standard-output
*)
131 output-to-files
;; currently ignored
132 (comp-env (non-nil-comp-env)))
133 "Compiles a collection of parenscripts as described by an ASDF system into files or
134 a specified output stream."
135 (asdf:operate
'asdf
::parenscript-compile-op system
136 :output-spec output-spec
137 :pretty-print pretty-print
138 ; :output-to-stream t
139 :output-stream output-stream
145 ;(defun compile-script-system-component (system-designator
147 ;(defun compile-script-file (script-src-file
149 ; (output-spec :javascript)
150 ; (output-stream *standard-out*)
151 ; (comp-env *compilation-environment*))
154 ;;; old file compilation functions:
155 (defun compile-parenscript-file-to-string (source-file)
156 "Compile SOURCE-FILE (a parenscript file) to a javascript string. (in-package ...) forms
157 behave as expected and all other forms are evaluated according to the value of
158 EVAL-FORMS-P. If the result of the evaluation is not nil then it's compiled with
159 js:js* and written to the output."
160 (compile-script-file source-file
:output-stream nil
))
162 (defun compile-parenscript-file (source-file &rest args
&key destination-file
&allow-other-keys
)
163 "Compile SOURCE-FILE (a parenscript file) to a javascript file with
164 compile-parenscript-file-to-string. When DESTINATION-FILE is omitted,
165 then it will be named the same as SOURCE-FILE but with js extension."
166 (setf args
(copy-list args
))
167 (remf args
:destination-file
)
168 (unless destination-file
169 (setf destination-file
(merge-pathnames (make-pathname :type
"js")
171 (with-open-file (output destination-file
:if-exists
:supersede
:direction
:output
)
172 (write-string (apply #'compile-parenscript-file-to-string source-file args
) output
)))
174 (defun ps-to-string (expr)
175 "Given an AST node, compiles it to a Javascript string."
177 (ps-js::js-to-statement-strings
(compile-script-form expr
) 0)
180 ;;; SEXPs -> Javascript string functionality
181 (defmacro script
(&body body
)
182 "A macro that returns a Javascript string of the supplied Parenscript forms."
183 `(script* '(progn ,@body
)))
185 (defun script* (&rest body
)
186 "Return the javascript string representing BODY.
188 (compile-script `(progn ,@body
)))
191 (defmacro ps
(&body body
)
194 (defmacro ps
* (&body body
)
197 (defmacro js
(&body body
)
200 (defmacro js
* (&body body
)