Modified the printer so that PS and PS-INLINE compile and print
[clinton/parenscript.git] / src / compilation-interface.lisp
dissimilarity index 89%
index bb05fa2..e148901 100644 (file)
@@ -1,27 +1,35 @@
-(in-package :parenscript)
-
-(defun compile-script (script-form &key (output-stream nil))
-  "Compiles the Parenscript form SCRIPT-FORM into Javascript.
-Non-null PRETTY-PRINT values result in a pretty-printed output code.
-If OUTPUT-STREAM is NIL, then the result is a string; otherwise code
-is output to the OUTPUT-STREAM stream."
-  (parenscript-print (compile-parenscript-form script-form :expecting :statement) output-stream))
-
-(defmacro ps (&body body)
-  "A macro that returns a Javascript string of the supplied Parenscript forms."
-  `(ps* '(progn ,@body)))
-
-(defun ps* (&rest body)
-  "Return the javascript string representing BODY.
-Body is evaluated."
-  (compile-script `(progn ,@body)))
-
-(defun ps-inline* (form)
-  (concatenate 'string
-               "javascript:"
-               (remove #\Newline
-                       (parenscript-print (compile-parenscript-form form :expecting :statement))
-                       :from-end t)))
-
-(defmacro ps-inline (&body body)
-  `(ps-inline* '(progn ,@body)))
+(in-package :parenscript)
+
+(defmacro ps (&body body)
+  "Given Parenscript forms (an implicit progn), compiles those forms
+to a JavaScript string at macro-expansion time."
+  `(concatenate 'string ,@(parenscript-print (compile-parenscript-form `(progn ,@body) :expecting :statement))))
+
+(defmacro ps-doc (&body body)
+  "Expands Parenscript forms in a clean environment."
+  (let ((*ps-gensym-counter* 0)
+        (*ps-special-variables* nil))
+     (macroexpand-1 `(ps ,@body))))
+
+(defun ps1* (ps-form)
+  (apply #'concatenate 'string
+         (mapcar (lambda (x)
+                   (if (stringp x)
+                       x
+                       (eval x)))
+                 (parenscript-print (compile-parenscript-form ps-form :expecting :statement)))))
+
+(defun ps* (&rest body)
+  "Compiles BODY to a JavaScript string.
+Body is evaluated."
+  (ps1* `(progn ,@body)))
+
+(defvar *js-inline-string-delimiter* #\"
+  "Controls the string delimiter char used when compiling Parenscript in ps-inline.")
+
+(defun ps-inline* (form &optional (*js-string-delimiter* *js-inline-string-delimiter*))
+  (concatenate 'string "javascript:" (ps1* form)))
+
+(defmacro ps-inline (form &optional (string-delimiter '*js-inline-string-delimiter*))
+  `(let ((*js-string-delimiter* ,string-delimiter))
+     (concatenate 'string "javascript:" ,@(parenscript-print (compile-parenscript-form form :expecting :statement)))))