Add lisp-implementation-version
[jscl.git] / src / toplevel.lisp
index 04423d3..9d6594b 100644 (file)
 ;; You should have received a copy of the GNU General Public License
 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
 
+(/debug "loading toplevel.lisp!")
 
 (defun eval (x)
-  (js-eval (ls-compile-toplevel x t)))
+  (js-eval (compile-toplevel x t)))
 
 (defvar * nil)
 (defvar ** nil)
 
 (setq *package* *user-package*)
 
-;;; Set some external entry point to the Lisp implementation to the
-;;; console. It would not be necessary when FFI is finished.
-(js-eval "var lisp")
-(%js-vset "lisp" (new))
-(%js-vset "lisp.read" #'ls-read-from-string)
-(%js-vset "lisp.print" #'prin1-to-string)
-(%js-vset "lisp.eval" #'eval)
-(%js-vset "lisp.compile" (lambda (s) (ls-compile-toplevel s t)))
-(%js-vset "lisp.evalString" (lambda (str) (eval (ls-read-from-string str))))
-(%js-vset "lisp.evalInput" (lambda (str) (eval-interactive (ls-read-from-string str))))
-(%js-vset "lisp.compileString" (lambda (str) (ls-compile-toplevel (ls-read-from-string str) t)))
+(defvar *root* (%js-vref "window"))
+
+
+(defun load-history ()
+  (#j:jqconsole:SetHistory (#j:JSON:parse (#j:localStorage:getItem "jqhist"))))
+
+(defun save-history ()
+  (#j:localStorage:setItem "jqhist" (#j:JSON:stringify (#j:jqconsole:GetHistory))))
+
+(defun toplevel ()
+  (let ((prompt (format nil "~a> " (package-name *package*))))
+    (#j:jqconsole:Write prompt "jqconsole-prompt"))
+  (flet ((process-input (input)
+           (let* ((form (read-from-string input))
+                  (successp nil)
+                  result)
+             ;; Capture errors. We evaluate the form and set successp
+             ;; to T. However, if a non-local exist happens, we cancel
+             ;; it, so it is not propagated more.
+             (block nil
+               (unwind-protect
+                    (progn
+                      (setq result (multiple-value-list (eval-interactive form)))
+                      (setq successp t))
+                 (return)))
+
+             (if successp
+                 (dolist (x result)
+                   (#j:jqconsole:Write (format nil "~S~%" x) "jqconsole-return"))
+                 (#j:jqconsole:Write (format nil "Error occurred~%") "jqconsole-error"))
+             
+             (save-history)) 
+           (toplevel)))
+    (#j:jqconsole:Prompt t #'process-input)))
+
+
+(defun init (&rest args)
+  (#j:jqconsole:RegisterMatching "(" ")" "parents")
+  (load-history)
+  (toplevel))
+
+(#j:window:addEventListener "load" #'init)