renaming and refactoring
[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 (comp-env (make-basic-compilation-environment)))
35 "Compiles the Parenscript form SCRIPT-FORM into the language specified by OUTPUT-SPEC.
36 Non-null PRETTY-PRINT values result in a pretty-printed output code. If OUTPUT-STREAM
37 is NIL, then the result is a string; otherwise code is output to the OUTPUT-STREAM stream.
38 COMP-ENV is the compilation environment in which to compile the form.
39
40 This is the main function used by Parenscript users to compile their code to Javascript (and
41 potentially other languages)."
42 (macrolet ((with-output-stream ((var) &body body)
43 `(if (null output-stream)
44 (with-output-to-string (,var)
45 ,@body)
46 (let ((,var output-stream))
47 ,@body))))
48 (with-output-stream (stream)
49 (let ((*compilation-environment* comp-env))
50 (translate-ast (compile-script-form script-form :comp-env comp-env)
51 :comp-env comp-env
52 :output-stream stream
53 :output-spec output-spec
54 :pretty-print pretty-print)))))
55
56 ;;; SEXPs -> Javascript string functionality
57 (defmacro script (&body body)
58 "A macro that returns a Javascript string of the supplied Parenscript forms."
59 `(js* '(progn ,@body)))
60
61 (defmacro script* (&body body)
62 "Return the javascript string representing BODY.
63
64 Body is evaluated."
65 `(compile-script (progn ,@body)))
66
67 ;; DEPRECATED
68 (defmacro js (&body body)
69 "A macro that returns a javascript string of the supplied Parenscript forms."
70 `(script ,@body))
71
72 (defmacro js* (&body body)
73 `(script* ,@body))
74
75 (defun js-to-string (expr)
76 "Given an AST node, compiles it to a Javascript string."
77 (string-join
78 (js-to-statement-strings (compile-script-form expr) 0)
79 (string #\Newline)))
80
81 (defun js-to-line (expr)
82 "Given an AST node, compiles it to a Javascript string."
83 (string-join
84 (js-to-statement-strings (compile-script-form expr) 0) " "))
85
86 (defun compile-parenscript-file-to-string (source-file
87 &key
88 (log-stream nil)
89 (comment nil)
90 (eval-forms-p nil))
91 "Compile SOURCE-FILE (a parenscript file) to a javascript string. (in-package ...) forms
92 behave as expected and all other forms are evaluated according to the value of
93 EVAL-FORMS-P. If the result of the evaluation is not nil then it's compiled with
94 js:js* and written to the output."
95 (with-output-to-string (output)
96 (with-open-file (input source-file :direction :input)
97 (flet ((read-form ()
98 (read input nil))
99 (log-message (&rest args)
100 (when log-stream
101 (apply #'format log-stream args))))
102 (let ((*package* *package*))
103 (loop for form = (read-form)
104 while form do
105 (if (or (not (listp form))
106 (not (eq (car form) 'cl:in-package)))
107 (progn
108 (log-message "Processing form:~%~S~%" form)
109 (when comment
110 (princ "/*" output)
111 (print form output)
112 (terpri output)
113 (princ "*/" output)
114 (terpri output))
115 (when eval-forms-p
116 (setf form (eval form)))
117 (log-message "After evaluation:~%~S~%" form)
118 (when form
119 (let ((compiled (js:js* form)))
120 (log-message "Compiled into:~%~A~%~%" compiled)
121 (write-string compiled output)
122 (terpri output)
123 (terpri output))))
124 (when (and (listp form)
125 (eq (car form) 'cl:in-package))
126 (log-message "Setting package to: ~S~%" (cadr form))
127 (setf *package* (find-package (cadr form)))))))))))
128
129 (defun compile-parenscript-file (source-file &rest args &key destination-file &allow-other-keys)
130 "Compile SOURCE-FILE (a parenscript file) to a javascript file with
131 compile-parenscript-file-to-string. When DESTINATION-FILE is omitted,
132 then it will be named the same as SOURCE-FILE but with js extension."
133 (setf args (copy-list args))
134 (remf args :destination-file)
135 (unless destination-file
136 (setf destination-file (merge-pathnames (make-pathname :type "js")
137 source-file)))
138 (with-open-file (output destination-file :if-exists :supersede :direction :output)
139 (write-string (apply #'compile-parenscript-file-to-string source-file args) output)))