Fixed PS-INLINE (thanks to Olof-Joachim Frahm <Olof.Frahm@web.de> for the patch).
[parenscript.git] / src / compilation-interface.lisp
blobea436416c0ff92a2ff46bcb9bbea61e7cf436789
1 (in-package "PARENSCRIPT")
3 (defparameter *js-target-version* 1.3)
5 (defvar *parenscript-stream* nil)
7 (defmacro ps (&body body)
8 "Given Parenscript forms (an implicit progn), compiles those forms
9 to a JavaScript string at macro-expansion time."
10 (let ((printed-forms (parenscript-print
11 (compile-statement `(progn ,@body))
12 nil)))
13 (if (and (not (cdr printed-forms))
14 (stringp (car printed-forms)))
15 (car printed-forms)
16 (let ((s (gensym)))
17 `(with-output-to-string (,s)
18 ,@(mapcar (lambda (x) `(write-string ,x ,s)) printed-forms))))))
20 (defmacro ps-to-stream (stream &body body)
21 (let ((printed-forms (parenscript-print
22 (compile-statement `(progn ,@body))
23 nil)))
24 `(let ((*parenscript-stream* ,stream))
25 ,@(mapcar (lambda (x) `(write-string ,x *parenscript-stream*)) printed-forms))))
27 (defun ps* (&rest body)
28 "Compiles BODY to a JavaScript string.
29 Body is evaluated."
30 (let ((*psw-stream* (or *parenscript-stream*
31 (make-string-output-stream))))
32 (parenscript-print (compile-statement `(progn ,@body)) t)
33 (unless *parenscript-stream*
34 (get-output-stream-string *psw-stream*))))
36 (defmacro ps-doc (&body body)
37 "Expands Parenscript forms in a clean environment."
38 (let ((*ps-gensym-counter* 0)
39 (*ps-special-variables* nil))
40 (macroexpand-1 `(ps ,@body))))
42 (defun ps-doc* (&rest body)
43 (let ((*ps-gensym-counter* 0)
44 (*ps-special-variables* nil))
45 (apply #'ps* body)))
47 (defvar *js-inline-string-delimiter* #\"
48 "Controls the string delimiter char used when compiling Parenscript in ps-inline.")
50 (defun ps-inline* (form &optional (*js-string-delimiter* *js-inline-string-delimiter*))
51 (concatenate 'string "javascript:" (ps* form)))
53 (defmacro+ps ps-inline (form &optional (string-delimiter *js-inline-string-delimiter*))
54 `(concatenate 'string "javascript:"
55 ,@(let ((*js-string-delimiter* string-delimiter))
56 (parenscript-print (compile-statement form) nil))))
58 (defvar *ps-read-function* #'read)
60 (defun ps-compile-stream (stream)
61 (let ((*ps-compilation-level* :toplevel)
62 (eof '#:eof))
63 (ps* (cons 'progn
64 (loop for form = (funcall *ps-read-function* stream nil eof)
65 until (eq form eof)
66 collect form)))))
68 (defun ps-compile-file (source-file)
69 (with-open-file (stream source-file :direction :input)
70 (ps-compile-stream stream)))