Modified the printer so that PS and PS-INLINE compile and print
[clinton/parenscript.git] / src / compilation-interface.lisp
dissimilarity index 94%
index 7780eec..e148901 100644 (file)
@@ -1,44 +1,35 @@
-(in-package :parenscript)
-
-(defun translate-ast (compiled-expr &key (output-stream *standard-output*) (output-spec :javascript) (pretty-print t))
-  "Translates a compiled Parenscript program (compiled with COMPILE-PAREN-FORM)
-to a Javascript string.  Outputs to the stream OUTPUT-STREAM in the language given
-by OUTPUT-SPEC, pretty printing if PRETTY-PRINT is non-null.
-
-OUTPUT-SPEC must be :javascript at the moment."
-  (when (not (eql :javascript output-spec))
-    (error "Unsupported output-spec for translation: ~A" output-spec))
-  (write-string (string-join (ps-print compiled-expr 0)
-                             (string #\Newline))
-                output-stream))
-
-(defun compile-script (script-form &key (output-spec :javascript) (pretty-print t) (output-stream nil) (toplevel-p t))
-  "Compiles the Parenscript form SCRIPT-FORM into the language specified by OUTPUT-SPEC.
-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.
-
-This is the main function used by Parenscript users to compile their code to Javascript (and
-potentially other languages)."
-  (macrolet ((with-output-stream ((var) &body body)
-              `(if (null output-stream)
-                (with-output-to-string (,var)
-                  ,@body)
-                (let ((,var output-stream))
-                  ,@body))))
-    (with-output-stream (stream)
-      (translate-ast (compile-parenscript-form script-form)
-                     :output-stream stream
-                     :output-spec output-spec
-                     :pretty-print pretty-print))))
-
-(defun ps-to-string (expr)
-  (string-join (ps-print (compile-parenscript-form expr) 0) (string #\Newline)))
-
-(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)))
+(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)))))