Report unexpected errors in the reader
[jscl.git] / lispstrack.lisp
index 59e1c85..46c07c6 100644 (file)
   (skip-whitespaces-and-comments stream)
   (let ((ch (%peek-char stream)))
     (cond
+      ((null ch)
+       (error "Unspected EOF"))
       ((char= ch #\))
        (%read-char stream)
        nil)
       ((char= ch #\.)
        (%read-char stream)
-       (skip-whitespaces-and-comments stream)
        (prog1 (ls-read stream)
+         (skip-whitespaces-and-comments stream)
          (unless (char= (%read-char stream) #\))
            (error "')' was expected."))))
       (t
   (let ((string "")
         (ch nil))
     (setq ch (%read-char stream))
-    (while (not (char= ch #\"))
-      (when (char= ch #\\)
+    (while (not (eql ch #\"))
+      (when (null ch)
+        (error "Unexpected EOF"))
+      (when (eql ch #\\)
         (setq ch (%read-char stream)))
       (setq string (concat string (string ch)))
       (setq ch (%read-char stream)))
 
 (define-transformation let (bindings &rest body)
   (let ((bindings (mapcar #'ensure-list bindings)))
-    `((lambda ,(mapcar 'car bindings) ,@body)
-      ,@(mapcar 'cadr bindings))))
+    `((lambda ,(mapcar #'car bindings) ,@body)
+      ,@(mapcar #'cadr bindings))))
 
 ;;; A little backquote implementation without optimizations of any
 ;;; kind for lispstrack.
   (compile-bool (concat "(" (ls-compile x env fenv) "===" (ls-compile nil env fenv) ")")))
 
 (define-compilation cons (x y)
-  (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}"))
+  (concat "({car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "})"))
 
 (define-compilation consp (x)
   (compile-bool
            "; return (typeof tmp == 'object' && 'car' in tmp);})()")))
 
 (define-compilation car (x)
-  (concat "(" (ls-compile x env fenv) ").car"))
+  (concat "(function () { var tmp = " (ls-compile x env fenv)
+          "; return tmp === " (ls-compile nil nil nil) "? "
+          (ls-compile nil nil nil)
+          ": tmp.car; })()"))
 
 (define-compilation cdr (x)
-  (concat "(" (ls-compile x env fenv) ").cdr"))
+  (concat "(function () { var tmp = " (ls-compile x env fenv)
+          "; return tmp === " (ls-compile nil nil nil) "? "
+          (ls-compile nil nil nil)
+          ": tmp.cdr; })()"))
 
 (define-compilation setcar (x new)
   (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")"))
                 "})()" *newline*))))
 
 (define-compilation js-eval (string)
-  (concat "eval(" (ls-compile string env fenv)  ")"))
-
+  (concat "eval.apply(window, [" (ls-compile string env fenv)  "])"))
 
 (define-compilation error (string)
   (concat "(function (){ throw " (ls-compile string env fenv) ";" "return 0;})()"))
   (and (symbolp x) (eq (binding-type (lookup-function x *fenv*)) 'macro)))
 
 (defun ls-macroexpand-1 (form env fenv)
-  (when (macrop (car form))
-    (let ((binding (lookup-function (car form) *env*)))
-      (if (eq (binding-type binding) 'macro)
-          (apply (eval (binding-translation binding)) (cdr form))
-          form))))
+  (if (macrop (car form))
+      (let ((binding (lookup-function (car form) *env*)))
+        (if (eq (binding-type binding) 'macro)
+            (apply (eval (binding-translation binding)) (cdr form))
+            form))
+      form))
 
 (defun compile-funcall (function args env fenv)
   (cond