X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcompiler-codegen.lisp;h=674ace6f38414af97168e0e9d5e596fe1ffe3a43;hb=99252fafa2c1bd2933787ca25cc16f5fd19364eb;hp=fbb3952909c86b0d5d04228e203db8a47b45c8cc;hpb=afb6a14b45bf0734ce9b06d3c299036ad8792d40;p=jscl.git diff --git a/src/compiler-codegen.lisp b/src/compiler-codegen.lisp index fbb3952..674ace6 100644 --- a/src/compiler-codegen.lisp +++ b/src/compiler-codegen.lisp @@ -22,6 +22,8 @@ ;;; It is intended to be used with the new compiler. However, it is ;;; quite independent so it has been integrated early in JSCL. +(/debug "loading compiler-codegen.lisp!") + (defvar *js-output* t) ;;; Two seperate functions are needed for escaping strings: @@ -152,7 +154,7 @@ (js-format ",") (js-identifier arg))) (js-format ")") - (js-stmt `(group ,@body))) + (js-stmt `(group ,@body) t)) (defun check-lvalue (x) (unless (or (symbolp x) @@ -211,6 +213,9 @@ (let ((op1 (car args)) (op2 (cadr args))) (case op + ;; Transactional compatible operator + (code + (js-format "~a" (apply #'code args))) ;; Function call (call (js-expr (car args)) @@ -357,82 +362,92 @@ ((and (consp form) (eq (car form) 'progn)) (destructuring-bind (&body body) (cdr form) (cond - ((null body) '(empty)) - ((null (cdr body)) (car body)) - (t `(group ,@(cdr form)))))) + ((null body) + nil) + ((null (cdr body)) + (js-expand-stmt (car body))) + (t + `(group ,@(cdr form)))))) (t form))) (defun js-stmt (form &optional parent) (let ((form (js-expand-stmt form))) (flet ((js-stmt (x) (js-stmt x form))) - (if (atom form) - (progn - (js-expr form) - (js-format ";")) - (case (car form) - (empty - (unless (and (consp parent) (eq (car parent) 'group)) - (js-format ";"))) - (label - (destructuring-bind (label &body body) (cdr form) - (js-identifier label) - (js-format ":") - (js-stmt `(progn ,@body)))) - (break - (destructuring-bind (label) (cdr form) - (js-format "break ") - (js-identifier label) + (cond + ((null form) + (unless (or (and (consp parent) (eq (car parent) 'group)) + (null parent)) + (js-format ";"))) + ((atom form) + (progn + (js-expr form) + (js-format ";"))) + (t + (case (car form) + (code + (js-format "~a" (apply #'code (cdr form)))) + (label + (destructuring-bind (label &body body) (cdr form) + (js-identifier label) + (js-format ":") + (js-stmt `(progn ,@body)))) + (break + (destructuring-bind (label) (cdr form) + (js-format "break ") + (js-identifier label) + (js-format ";"))) + (return + (destructuring-bind (value) (cdr form) + (js-format "return ") + (js-expr value) (js-format ";"))) - (return - (destructuring-bind (value) (cdr form) - (js-format "return ") - (js-expr value) - (js-format ";"))) - (var - (flet ((js-var (spec) - (destructuring-bind (variable &optional initial) - (ensure-list spec) - (js-identifier variable) - (when initial - (js-format "=") - (js-expr initial))))) - (destructuring-bind (var &rest vars) (cdr form) - (let ((*js-operator-precedence* 12)) - (js-format "var ") - (js-var var) - (dolist (var vars) - (js-format ",") - (js-var var)) - (js-format ";"))))) - (if - (destructuring-bind (condition true &optional false) (cdr form) - (js-format "if (") - (js-expr condition) - (js-format ") ") - (js-stmt true) - (when false - (js-format " else ") - (js-stmt false)))) - (group - (let ((in-group-p (and (consp parent) (eq (car parent) 'group)))) - (unless in-group-p (js-format "{")) - (mapc #'js-stmt (cdr form)) - (unless in-group-p (js-format "}")))) - (while - (destructuring-bind (condition &body body) (cdr form) - (js-format "while (") - (js-expr condition) - (js-format ")") - (js-stmt `(progn ,@body)))) - (throw - (destructuring-bind (object) (cdr form) - (js-format "throw ") - (js-expr object) - (js-format ";"))) - (t - (js-expr form) - (js-format ";"))))))) + (var + (flet ((js-var (spec) + (destructuring-bind (variable &optional initial) + (ensure-list spec) + (js-identifier variable) + (when initial + (js-format "=") + (js-expr initial))))) + (destructuring-bind (var &rest vars) (cdr form) + (let ((*js-operator-precedence* 12)) + (js-format "var ") + (js-var var) + (dolist (var vars) + (js-format ",") + (js-var var)) + (js-format ";"))))) + (if + (destructuring-bind (condition true &optional false) (cdr form) + (js-format "if (") + (js-expr condition) + (js-format ") ") + (js-stmt true) + (when false + (js-format " else ") + (js-stmt false)))) + (group + (let ((in-group-p + (or (null parent) + (and (consp parent) (eq (car parent) 'group))))) + (unless in-group-p (js-format "{")) + (mapc #'js-stmt (cdr form)) + (unless in-group-p (js-format "}")))) + (while + (destructuring-bind (condition &body body) (cdr form) + (js-format "while (") + (js-expr condition) + (js-format ")") + (js-stmt `(progn ,@body)))) + (throw + (destructuring-bind (object) (cdr form) + (js-format "throw ") + (js-expr object) + (js-format ";"))) + (t + (js-expr form) + (js-format ";")))))))) (defun js (&rest stmts) (mapc #'js-stmt stmts)