Migrate js!bool and js!selfcall
[jscl.git] / src / compiler.lisp
index 3b3d0c9..b4e1eaf 100644 (file)
@@ -18,6 +18,8 @@
 
 ;;;; Compiler
 
+(/debug "loading compiler.lisp!")
+
 ;;; Translate the Lisp code to Javascript. It will compile the special
 ;;; forms. Some primitive functions are compiled as special forms
 ;;; too. The respective real functions are defined in the target (see
@@ -48,7 +50,7 @@
 ;;; Wrap X with a Javascript code to convert the result from
 ;;; Javascript generalized booleans to T or NIL.
 (defun js!bool (x)
-  `(code "(" ,x "?" ,(ls-compile t) ": " ,(ls-compile nil) ")"))
+  `(if ,x ,(ls-compile t) ,(ls-compile nil)))
 
 ;;; Concatenate the arguments and wrap them with a self-calling
 ;;; Javascript anonymous function. It is used to make some Javascript
 ;;; It could be defined as function, but we could do some
 ;;; preprocessing in the future.
 (defmacro js!selfcall (&body body)
-  ``(code "(function(){" ,*newline*
-          (code ,,@body)
-          ,*newline*
-          "})()"))
+  ``(call (function nil (code ,,@body))))
 
 ;;; Like CODE, but prefix each line with four spaces. Two versions
 ;;; of this function are available, because the Ecmalisp version is
 
 (defun gvarname (symbol)
   (declare (ignore symbol))
-  (format nil "v~d" (incf *variable-counter*)))
+  (code "v" (incf *variable-counter*)))
 
 (defun translate-variable (symbol)
   (awhen (lookup-in-lexenv symbol *environment* 'variable)
 (defvar *literal-counter* 0)
 
 (defun genlit ()
-  (format nil "l~d" (incf *literal-counter*)))
+  (code "l" (incf *literal-counter*)))
 
 (defun dump-symbol (symbol)
   #-jscl
         ,@(mapcar (lambda (form)
                     `(code "vs = " ,(ls-compile form t) ";"
                            "if (typeof vs === 'object' && 'multiple-value' in vs)"
-                           (code "args = args.concat(vs);" )
-                           "else"
+                           (code " args = args.concat(vs);" )
+                           " else "
                            (code "args.push(vs);" )))
                   forms))
       "args[1] = args.length-2;"
       (cond
         ((floatp x) (push (float-to-string x) fargs))
         ((numberp x) (push (integer-to-string x) fargs))
-        (t (let ((v (format nil "x~d" (incf counter))))
+        (t (let ((v (code "x" (incf counter))))
              (push v fargs)
              (push `(code "var " ,v " = " ,(ls-compile x) ";"
-                          "if (typeof " v " !== 'number') throw 'Not a number!';"
-                          )
+                          "if (typeof " ,v " !== 'number') throw 'Not a number!';")
                    prelude)))))
     (js!selfcall
       `(code ,@(reverse prelude))
 (define-builtin %write-string (x)
   `(code "lisp.write(" ,x ")"))
 
+(define-builtin /debug (x)
+  `(code "console.log(xstring(" ,x "))"))
+
 
 ;;; Storage vectors. They are used to implement arrays and (in the
 ;;; future) structures.
 " *newline*)
           ";" ,*newline*))))
 
-(defun ls-compile (sexp &optional multiple-value-p)
+(defun ls-compile* (sexp &optional multiple-value-p)
   (multiple-value-bind (sexp expandedp) (!macroexpand-1 sexp)
     (when expandedp
-      (return-from ls-compile (ls-compile sexp multiple-value-p)))
+      (return-from ls-compile* (ls-compile sexp multiple-value-p)))
     ;; The expression has been macroexpanded. Now compile it!
     (let ((*multiple-value-p* multiple-value-p))
       (cond
         (t
          (error "How should I compile `~S'?" sexp))))))
 
+(defun ls-compile (sexp &optional multiple-value-p)
+  `(code "(" ,(ls-compile* sexp multiple-value-p) ")"))
+
 
 (defvar *compile-print-toplevels* nil)