eval-when special form
[clinton/parenscript.git] / src / compilation-interface.lisp
1 (in-package :parenscript)
2
3 (defmacro with-new-compilation-environment ((var) &body body)
4 `(let* ((,var (make-basic-compilation-environment))
5 (*compilation-environment* ,var))
6 ,@body))
7
8
9 (defun translate-ast (compiled-expr
10 &key
11 (comp-env *compilation-environment*)
12 (output-stream *standard-output*)
13 (output-spec :javascript)
14 (pretty-print t))
15 "Translates a compiled Parenscript program (compiled with COMPILE-PAREN-FORM)
16 to a Javascript string. Outputs to the stream OUTPUT-STREAM in the language given
17 by OUTPUT-SPEC, pretty printing if PRETTY-PRINT is non-null.
18
19 OUTPUT-SPEC must be :javascript at the moment."
20 (declare (ignore pretty-print) (ignore comp-env))
21 (when (not (eql :javascript output-spec))
22 (error "Unsupported output-spec for translation: ~A" output-spec))
23 (when (eql :javascript output-spec)
24 (write-string (string-join
25 (js-to-statement-strings compiled-expr 0)
26 (string #\Newline))
27 output-stream)))
28
29 (defun compile-script (script-form
30 &key
31 (output-spec :javascript)
32 (pretty-print t)
33 (output-stream nil)
34 (toplevel-p t)
35 (comp-env (make-basic-compilation-environment)))
36 "Compiles the Parenscript form SCRIPT-FORM into the language specified by OUTPUT-SPEC.
37 Non-null PRETTY-PRINT values result in a pretty-printed output code. If OUTPUT-STREAM
38 is NIL, then the result is a string; otherwise code is output to the OUTPUT-STREAM stream.
39 COMP-ENV is the compilation environment in which to compile the form.
40
41 This is the main function used by Parenscript users to compile their code to Javascript (and
42 potentially other languages)."
43 (macrolet ((with-output-stream ((var) &body body)
44 `(if (null output-stream)
45 (with-output-to-string (,var)
46 ,@body)
47 (let ((,var output-stream))
48 ,@body))))
49 (with-output-stream (stream)
50 (let* ((*compilation-environment* comp-env)
51 (compiled
52 (if toplevel-p
53 (compile-parenscript-form
54 comp-env
55 (compile-parenscript-form comp-env script-form :toplevel-p t))
56 (compile-parenscript-form comp-env script-form :toplevel-p nil))))
57 (translate-ast
58 compiled
59 ; (compile-script-form script-form :comp-env comp-env)
60 :comp-env comp-env
61 :output-stream stream
62 :output-spec output-spec
63 :pretty-print pretty-print)))))
64
65 (defun compile-script-file (source-file
66 &key
67 (output-spec :javascript)
68 (comp-env (or *compilation-environment*
69 (make-basic-compilation-environment)))
70 (pretty-print t)
71 (output-stream *standard-output*))
72 "Compiles the given Parenscript source file and outputs the results
73 to the given output stream."
74 (setf (comp-env-compiling-toplevel-p comp-env) t)
75 (error "NOT IMPLEMENTED."))
76
77
78
79
80 ;(defun compile-script-file (script-src-file
81 ; &key
82 ; (output-spec :javascript)
83 ; (output-stream *standard-out*)
84 ; (comp-env *compilation-environment*))
85
86
87 ;;; SEXPs -> Javascript string functionality
88 (defmacro script (&body body)
89 "A macro that returns a Javascript string of the supplied Parenscript forms."
90 `(js* '(progn ,@body)))
91
92 (defmacro script* (&body body)
93 "Return the javascript string representing BODY.
94
95 Body is evaluated."
96 `(compile-script (progn ,@body)))
97
98 ;; DEPRECATED
99 (defmacro js (&body body)
100 "A macro that returns a javascript string of the supplied Parenscript forms."
101 `(script ,@body))
102
103 (defmacro js* (&body body)
104 `(script* ,@body))
105
106 (defun js-to-string (expr)
107 "Given an AST node, compiles it to a Javascript string."
108 (string-join
109 (js-to-statement-strings (compile-script-form expr) 0)
110 (string #\Newline)))
111
112 (defun js-to-line (expr)
113 "Given an AST node, compiles it to a Javascript string."
114 (string-join
115 (js-to-statement-strings (compile-script-form expr) 0) " "))
116
117
118 ;;; old file compilation functions:
119 (defun compile-parenscript-file-to-string (source-file
120 &key
121 (log-stream nil)
122 (comment nil)
123 (eval-forms-p nil))
124 "Compile SOURCE-FILE (a parenscript file) to a javascript string. (in-package ...) forms
125 behave as expected and all other forms are evaluated according to the value of
126 EVAL-FORMS-P. If the result of the evaluation is not nil then it's compiled with
127 js:js* and written to the output."
128 (with-output-to-string (output)
129 (with-open-file (input source-file :direction :input)
130 (flet ((read-form ()
131 (read input nil))
132 (log-message (&rest args)
133 (when log-stream
134 (apply #'format log-stream args))))
135 (let ((*package* *package*))
136 (loop for form = (read-form)
137 while form do
138 (if (or (not (listp form))
139 (not (eq (car form) 'cl:in-package)))
140 (progn
141 (log-message "Processing form:~%~S~%" form)
142 (when comment
143 (princ "/*" output)
144 (print form output)
145 (terpri output)
146 (princ "*/" output)
147 (terpri output))
148 (when eval-forms-p
149 (setf form (eval form)))
150 (log-message "After evaluation:~%~S~%" form)
151 (when form
152 (let ((compiled (js:js* form)))
153 (log-message "Compiled into:~%~A~%~%" compiled)
154 (write-string compiled output)
155 (terpri output)
156 (terpri output))))
157 (when (and (listp form)
158 (eq (car form) 'cl:in-package))
159 (log-message "Setting package to: ~S~%" (cadr form))
160 (setf *package* (find-package (cadr form)))))))))))
161
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")
170 source-file)))
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)))