;;; Utils
+#+common-lisp
(defmacro while (condition &body body)
`(do ()
((not ,condition))
(or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
(defun skip-whitespaces (stream)
- (loop for ch = (%peek-char stream)
- while (and ch (whitespacep ch))
- do (%read-char stream)))
+ (let (ch)
+ (setq ch (%peek-char stream))
+ (while (and ch (whitespacep ch))
+ (%read-char stream)
+ (setq ch (%peek-char stream)))))
(defun terminalp (ch)
- (or (null ch) (whitespacep ch) (char= #\) ch)))
+ (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
(defun read-until (stream func)
- (let ((string ""))
- (loop for ch = (%peek-char stream)
- until (funcall func ch)
- do (setq string (concat string (string ch)))
- do (%read-char stream))
+ (let ((string "")
+ (ch))
+ (setq ch (%peek-char stream))
+ (while (not (funcall func ch))
+ (setq string (concat string (string ch)))
+ (%read-char stream)
+ (setq ch (%peek-char stream)))
string))
(defun skip-whitespaces-and-comments (stream)
(%read-char stream)
(ecase (%read-char stream)
(#\'
- (list 'function (ls-read stream)))))
+ (list 'function (ls-read stream)))
+ (#\+
+ (let ((feature (read-until stream #'terminalp)))
+ (cond
+ ((string= feature "common-lisp")
+ (ls-read stream);ignore
+ (ls-read stream))
+ ((string= feature "lispstrack")
+ (ls-read stream))
+ (t
+ (error "Unknown reader form.")))))))
(t
(let ((string (read-until stream #'terminalp)))
(if (every #'digit-char-p string)
`(push (list ',name (lambda (env fenv ,@args) ,@body))
*compilations*))
+(defvar *toplevel-compilations*)
+
(define-compilation if (condition true false)
(concat "("
(ls-compile condition env fenv)
;;; Literals
-(defvar *literals* '())
-
(defun literal->js (sexp)
(cond
((null sexp) "undefined")
(let ((counter 0))
(defun literal (form)
(let ((var (concat "l" (integer-to-string (incf counter)))))
- (push (cons var (literal->js form)) *literals*)
+ (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
var)))
(define-compilation quote (sexp)
(defvar *eval-when-compilations*)
(define-compilation eval-when-compile (&rest body)
- (setq *eval-when-compilations* "")
(eval (cons 'progn body))
- (if (string= *eval-when-compilations* "")
- nil
- *eval-when-compilations*))
+ nil)
(defmacro define-transformation (name args form)
`(define-compilation ,name ,args
(define-compilation code-char (x)
(concat "String.fromCharCode( " (ls-compile x env fenv) ")"))
-
-(defmacro with-eval-when-compilation (&body body)
- `(setq *eval-when-compilations*
- (concat *eval-when-compilations* (progn ,@body))))
-
(defun %compile-defvar (name)
(push (make-var-binding name) *env*)
- (with-eval-when-compilation
- (concat "var " (lookup-variable name *env*))))
+ (push (concat "var " (lookup-variable name *env*)) *toplevel-compilations*))
(defun %compile-defun (name)
(push (make-func-binding name) *fenv*)
- (with-eval-when-compilation
- (concat "var " (lookup-variable name *fenv*))))
+ (push (concat "var " (lookup-variable name *fenv*)) *toplevel-compilations*))
(defun %compile-defmacro (name lambda)
(push (cons name (cons 'macro lambda)) *fenv*))
(compile-funcall (car sexp) (cdr sexp) env fenv)))))))
(defun ls-compile-toplevel (sexp)
- (setq *literals* nil)
+ (setq *toplevel-compilations* nil)
(let ((code (ls-compile sexp)))
(prog1
- (concat (join (mapcar (lambda (lit)
- (concat "var " (car lit) " = " (cdr lit) ";
+ (concat (join (mapcar (lambda (x)(concat x ";
"))
- *literals*)
+ *toplevel-compilations*)
"")
code)
- (setq *literals* nil))))
+ (setq *toplevel-compilations* nil))))
+#+common-lisp
(defun ls-compile-file (filename output)
(with-open-file (in filename)
(with-open-file (out output :direction :output :if-exists :supersede)
for compilation = (ls-compile-toplevel x)
when compilation do (write-line (concat compilation "; ") out)))))
-
;;; Testing
(defun compile-test ()
(ls-compile-file "test.lisp" "test.js"))