reindent
[jscl.git] / src / toplevel.lisp
1 ;;; toplevel.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; This program is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
10 ;;
11 ;; This program is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;; General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 (defun eval (x)
21   (js-eval (ls-compile-toplevel x t)))
22
23 (defvar * nil)
24 (defvar ** nil)
25 (defvar *** nil)
26 (defvar / nil)
27 (defvar // nil)
28 (defvar /// nil)
29 (defvar + nil)
30 (defvar ++ nil)
31 (defvar +++ nil)
32 (defvar - nil)
33
34 (defun eval-interactive (x)
35   (setf - x)
36   (let ((results (multiple-value-list (eval x))))
37     (setf /// //
38           // /
39           / results
40           *** **
41           ** *
42           * (car results)))
43   (setf +++ ++
44         ++ +
45         + -)
46   (values-list /))
47
48 (export '(&body &key &optional &rest * ** *** *gensym-counter* *package* + ++
49           +++ - / // /// 1+ 1- < <= = = > >= and append apply aref
50           arrayp assoc atom block boundp butlast caar cadddr caddr
51           cadr car car case catch cdar cdddr cddr cdr cdr char
52           char-code char= code-char cond cons consp constantly
53           copy-list decf declaim defconstant define-setf-expander
54           define-symbol-macro defmacro defparameter defun defvar
55           digit-char digit-char-p disassemble do do* documentation
56           dolist dotimes ecase eq eql equal error eval every export expt
57           fdefinition find-package find-symbol first flet format fourth fset
58           funcall function functionp gensym get-setf-expansion
59           get-universal-time go identity if in-package incf integerp
60           intern keywordp labels lambda last length let let* list
61           list* list-all-packages listp loop make-array make-package
62           make-symbol mapcar member minusp mod multiple-value-bind
63           multiple-value-call multiple-value-list multiple-value-prog1
64           nconc nil not nreconc nth nthcdr null numberp or
65           package-name package-use-list packagep parse-integer plusp
66           prin1-to-string print proclaim prog1 prog2 progn psetq push
67           quote remove remove-if remove-if-not return return-from
68           revappend reverse rplaca rplacd second set setf setq some
69           string string-upcase string= stringp subseq symbol-function
70           symbol-name symbol-package symbol-plist symbol-value symbolp
71           t tagbody third throw truncate unless unwind-protect values
72           values-list variable warn when write-line write-string zerop))
73
74 (setq *package* *user-package*)
75
76 (js-eval "var lisp")
77 (%js-vset "lisp" (new))
78 (%js-vset "lisp.read" #'ls-read-from-string)
79 (%js-vset "lisp.print" #'prin1-to-string)
80 (%js-vset "lisp.eval" #'eval)
81 (%js-vset "lisp.compile" (lambda (s) (ls-compile-toplevel s t)))
82 (%js-vset "lisp.evalString" (lambda (str) (eval (ls-read-from-string str))))
83 (%js-vset "lisp.evalInput" (lambda (str) (eval-interactive (ls-read-from-string str))))
84 (%js-vset "lisp.compileString" (lambda (str) (ls-compile-toplevel (ls-read-from-string str) t)))
85
86 ;; Set the initial global environment to be equal to the host global
87 ;; environment at this point of the compilation.
88 (eval-when-compile
89   (toplevel-compilation
90    (ls-compile `(setq *environment* ',*environment*))))
91
92 (eval-when-compile
93   (toplevel-compilation
94    (ls-compile
95     `(progn
96        ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(cdr s))))
97                  *literal-symbols*)
98        (setq *literal-symbols* ',*literal-symbols*)
99        (setq *variable-counter* ,*variable-counter*)
100        (setq *gensym-counter* ,*gensym-counter*)
101        (setq *block-counter* ,*block-counter*)))))
102
103 (eval-when-compile
104   (toplevel-compilation
105    (ls-compile
106     `(setq *literal-counter* ,*literal-counter*))))