Changeset 1458

Show
Ignore:
Timestamp:
03/13/05 22:43:46 (4 years ago)
Author:
manuel
Message:

radiosendung, einchecken fuer nachher, eigentlich geht alles

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/bknr/src/js/js2.lisp

    r1457 r1458  
    7070;;; void 
    7171;;; function 
     72;;; case 
     73;;; default 
     74;;; do 
     75;;; for 
     76;;; switch 
     77;;; while 
     78;;; with 
     79;;; throw 
    7280;;; 
    7381;;; TODO: 
    74 ;;; case 
    7582;;; catch 
    76 ;;; default 
    77 ;;; do 
    7883;;; finally 
    79 ;;; for 
    80 ;;; switch 
    81 ;;; throw 
    8284;;; try 
    83 ;;; while 
    84 ;;; with 
    8585 
    8686;;; Punctuators 
     
    360360;;; variables 
    361361 
    362 (defclass variable (expression) 
     362(defclass js-variable (expression) 
    363363  ()) 
    364364 
    365 (defmethod js-to-strings ((v variable) start-form) 
     365(defmethod js-to-strings ((v js-variable) start-form) 
    366366  (list (symbol-to-js (value v)))) 
    367367 
     
    633633               :collect nil))) 
    634634 
    635 (defclass slot-value (expression) 
     635(defclass js-slot-value (expression) 
    636636  ((object :initarg :object 
    637637           :accessor sv-object) 
     
    640640 
    641641(define-js-compiler-macro slot-value (obj slot) 
    642   (make-instance 'slot-value :object (js-compile-to-expression obj) 
     642  (make-instance 'js-slot-value :object (js-compile-to-expression obj) 
    643643                 :slot (js-compile-to-symbol slot))) 
    644644 
    645 (defmethod js-to-strings ((sv slot-value) start-pos) 
     645(defmethod js-to-strings ((sv js-slot-value) start-pos) 
    646646  (append-to-last (js-to-strings (sv-object sv) start-pos) 
    647647                  (format nil ".~A" (symbol-to-js (sv-slot sv))))) 
     
    704704 
    705705(defmethod expression-precedence ((if js-if)) 
    706   (declare (ignore if)) 
    707706  (gethash 'if *op-precedence-hash*)) 
    708707 
     
    757756 
    758757(define-js-single-op return statement) 
     758(define-js-single-op throw statement) 
    759759(define-js-single-op delete) 
    760760(define-js-single-op void) 
     
    786786 
    787787(defmethod expression-precedence ((setf js-setf)) 
    788   (declare (ignore setf)) 
    789788  (gethash '= *op-precedence-hash*)) 
    790789 
     
    829828;;; iteration 
    830829 
     830(defclass js-for (statement) 
     831  ((vars :initarg :vars :accessor for-vars) 
     832   (steps :initarg :steps :accessor for-steps) 
     833   (check :initarg :check :accessor for-check) 
     834   (body :initarg :body :accessor for-body))) 
     835 
     836(defun make-for-vars (decls) 
     837  (loop for decl in decls 
     838        for var = (if (atom decl) decl (first decl)) 
     839        for init = (if (atom decl) nil (second decl)) 
     840        collect (make-instance 'js-defvar :names (list (js-compile-to-symbol var)) 
     841                               :value (js-compile-to-expression init)))) 
     842 
     843(defun make-for-steps (decls) 
     844  (loop for decl in decls 
     845        when (= (length decl) 3) 
     846        collect (js-compile-to-expression (third decl)))) 
     847 
     848(define-js-compiler-macro do (decls termination &rest body) 
     849  (let ((vars (make-for-vars decls)) 
     850        (steps (make-for-steps decls)) 
     851        (check (js-compile-to-expression (list 'not (first termination)))) 
     852        (body (js-compile-to-body (cons 'progn body) :indent "  "))) 
     853    (make-instance 'js-for 
     854                   :vars vars 
     855                   :steps steps 
     856                   :check check 
     857                   :body body))) 
     858 
     859(defun strings-length (string-list) 
     860  (reduce #'max (mapcar #'length string-list) :initial-value most-negative-fixnum)) 
     861 
     862(defmethod js-to-statement-strings ((for js-for) start-pos) 
     863  (let* ((init (dwim-join (mapcar #'(lambda (x) 
     864                                      (dwim-join (list (list (symbol-to-js (first (var-names x)))) 
     865                                                       (js-to-strings (var-value x) 
     866                                                                      (+ start-pos 2))) 
     867                                                 (- 80 start-pos 2) 
     868                                                 :join-after " =")) 
     869                                  (for-vars for)) 
     870                          (- 80 start-pos 2) 
     871                          :start "var " :join-after ",")) 
     872         #+nil 
     873         (init-len (strings-length init)) 
     874         (check (js-to-strings (for-check for) (+ start-pos 2))) 
     875         #+nil 
     876         (check-len (strings-length check)) 
     877         (steps (dwim-join (mapcar #'(lambda (x) 
     878                                       (js-to-strings x (- start-pos 2))) 
     879                                   (for-steps for)) 
     880                           (- 80 start-pos 2) 
     881                           :join-after ",")) 
     882         (header (dwim-join (list init check steps) 
     883                            (- 80 start-pos 2) 
     884                            :start "for (" :end ") {" 
     885                            :join-after ";")) 
     886         (body (js-to-statement-strings (for-body for) (+ start-pos 2)))) 
     887    (nconc header body (list "}")))) 
     888 
     889  (let ((fun-header (dwim-join (mapcar #'(lambda (x) (list (symbol-to-js x))) 
     890                                       (d-args defun)) 
     891                               (- 80 start-pos 2) 
     892                               :start (format nil "function ~A(" 
     893                                              (symbol-to-js (d-name defun))) 
     894                               :end ") {" :join-after ",")) 
     895        (fun-body (js-to-statement-strings (d-body defun) (+ start-pos 2)))) 
     896    (nconc fun-header fun-body (list "}")))) 
     897 
     898(defclass for-each (statement) 
     899  ((name :initarg :name :accessor fe-name) 
     900   (value :initarg :value :accessor fe-value) 
     901   (body :initarg :value :accessor fe-body))) 
     902 
     903(define-js-compiler-macro do-each (decl &rest body) 
     904  (make-instance 'for-each :name (js-compile-to-symbol (first decl)) 
     905                 :value (js-compile-to-expression (second decl)) 
     906                 :body (js-compile-to-body (cons 'progn body) :indent "  "))) 
     907 
     908(defmethod js-to-statement-strings ((fe for-each) start-pos) 
     909  (let ((header (dwim-join (list (list (symbol-to-js (fe-name fe)) " in ") 
     910                                 (js-to-strings (fe-value fe) (+ start-pos 2))) 
     911                           :start "for (var " 
     912                           :end ") {")) 
     913        (body (js-to-statement-strings (fe-body fe) (+ start-pos 2)))) 
     914    (nconc header body (list "}")))) 
     915 
     916(defclass js-while (statement) 
     917  ((check :initarg :check :accessor while-check) 
     918   (body :initarg :body :accessor while-body))) 
     919 
     920(define-js-compiler-macro while (check &rest body) 
     921  (make-instance 'js-while 
     922                 :check (js-compile-to-expression check) 
     923                 :body (js-compile-to-body (cons 'progn body) :indent "  "))) 
     924 
     925(defmethod js-to-statement-strings ((while js-while) start-pos) 
     926  (let ((header (dwim-join (list (js-to-strings (while-check while) (+ start-pos 2))) 
     927                           (- 80 start-pos 2) 
     928                           :start "while (" 
     929                           :end ") {")) 
     930        (body (js-to-statement-strings (while-body while) (+ start-pos 2)))) 
     931    (nconc header body (list "}")))) 
     932 
    831933;;; with 
    832934 
     935(defclass js-with (statement) 
     936  ((obj :initarg :obj :accessor with-obj) 
     937   (body :initarg :body :accessor with-body))) 
     938 
     939(define-js-compiler-macro with (statement &rest body) 
     940  (make-instance 'js-with 
     941                 :obj (js-compile-to-expression (first statement)) 
     942                 :body (js-compile-to-body (cons 'progn body) :indent "  "))) 
     943 
     944(defmethod js-to-statement-strings ((with js-with) start-pos) 
     945  (nconc (dwim-join (list (js-to-strings (with-obj with) (+ start-pos 2))) 
     946                    (- 80 start-pos 2) 
     947                    :start "with (" :end ") {") 
     948         (js-to-statement-strings (with-body with) (+ start-pos 2)) 
     949         (list "}"))) 
     950 
    833951;;; case 
    834952 
     953(defclass js-case (statement) 
     954  ((value :initarg :value :accessor case-value) 
     955   (clauses :initarg :clauses :accessor case-clauses))) 
     956 
     957;;; XXX DEFAULT exporten 
     958(define-js-compiler-macro case (value &rest clauses) 
     959  (let ((clauses (mapcar #'(lambda (clause) 
     960                             (let ((val (first clause)) 
     961                                   (body (cdr clause))) 
     962                               (list (if (eql val 'default) 
     963                                         'default 
     964                                         (js-compile-to-expression val)) 
     965                                     (js-compile-to-body (cons 'progn body) :indent "  ")))) 
     966                         clauses)) 
     967        (check (js-compile-to-expression value))) 
     968    (make-instance 'js-case :value check 
     969                   :clauses clauses))) 
     970 
     971(defmethod js-to-statement-strings ((case js-case) start-pos) 
     972  (let ((body    (mapcan #'(lambda (clause) 
     973                     (let ((val (car clause)) 
     974                           (body (second clause))) 
     975                       (dwim-join (list (if (eql val 'default) 
     976                                            (list "") 
     977                                            (js-to-strings val (+ start-pos 2))) 
     978                                        (js-to-statement-strings body (+ start-pos 2))) 
     979                                  (- 80 start-pos 2) 
     980                                  :start (if (eql val 'default) "  default" "  case ") 
     981                                  :white-space "   " 
     982                                  :join-after ":"))) (case-clauses case)))) 
     983 
     984    (format t "body: ~S~%" body) 
     985    (nconc (dwim-join (list (js-to-strings (case-value case) (+ start-pos 2))) 
     986                    (- 80 start-pos 2) 
     987                    :start "switch (" :end ") {") 
     988           body 
     989           (list "}")))) 
     990 
    835991;;; throw catch 
     992 
     993(defclass js-try (statement) 
     994  ((body :initarg :body :accessor try-body) 
     995   (catch :initarg :catch :accessor try-catch) 
     996   (finally :initarg :finally :accessor try-finally))) 
     997 
     998(define-js-compiler-macro try (body clauses) 
     999  (let ((body (js-compile-to-body body :indent "  ")) 
     1000        (catch (cdr (assoc :catch clauses))) 
     1001        (finally (cdr (assoc :finally clauses)))) 
     1002    (make-instance 'js-try 
     1003                   :body body 
     1004                   :catch (when catch (list (js-compile-to-symbol (first catch)) 
     1005                                            (js-compile-to-body (cons 'progn (cdr catch)) 
     1006                                                                :indent "  "))) 
     1007                   :finally (when finally (js-compile-to-body finally :indent "   "))))) 
     1008 
     1009(defmethod js-to-statement-strings ((try js-try) start-pos) 
     1010  (let* ((catch (try-catch try)) 
     1011         (finally (try-finally try)) 
     1012         (catch-list (when catch 
     1013                       (dwim-join (list (list (symbol-to-js (first catch))) 
     1014                                        (js-to-strings (second catch) (+ start-pos 2))) 
     1015                                  (- 80 start-pos 2) 
     1016                                  :start "} catch (" 
     1017                                  :end ") {"))) 
     1018         (finally-list (when finally 
     1019                         (dwim-join (list (js-to-strings finally (+ start-pos 2))) 
     1020                                    (- 80 start-pos 2) 
     1021                                    :start "finally {")))) 
     1022    (nconc (dwim-join (list (js-to-statement-strings (try-body try) (+ start-pos 2))) 
     1023                      (- 80 start-pos 2) 
     1024                      :start "try {") 
     1025           catch-list 
     1026           finally-list 
     1027           (list "}")))) 
    8361028 
    8371029;;; regex 
     
    8701062           (if c-macro 
    8711063               (funcall c-macro) 
    872                (make-instance 'variable :value form)))) 
     1064               (make-instance 'js-variable :value form)))) 
    8731065        ((and (consp form) 
    8741066              (eql (first form) 'quote)) 
     
    9091101(defun js-compile-to-symbol (form) 
    9101102  (let ((res (js-compile form))) 
    911     (when (typep res 'variable ) 
     1103    (when (typep res 'js-variable ) 
    9121104      (setf res (value res))) 
    9131105    (assert (symbolp res)) 
  • trunk/bknr/src/js/utils.lisp

    r1405 r1458  
    2222        (t (princ-to-string val)))) 
    2323 
     24(defun prepend-to-first (form elt) 
     25  (cond ((stringp form) 
     26         (concatenate 'string elt form)) 
     27        ((consp form) 
     28         (cond ((stringp (first form)) 
     29                (rplaca form (concatenate 'string elt (car form)))) 
     30               ((consp (first form)) 
     31                (prepend-to-first (first form) elt)) 
     32               (t (error "unknown form"))) 
     33         form) 
     34        (t (error "unknown form")))) 
     35 
     36(defun append-to-last (form elt) 
     37  (cond ((stringp form) 
     38         (concatenate 'string form elt)) 
     39        ((consp form) 
     40         (let ((last (last form))) 
     41           (cond ((stringp (first last)) 
     42                  (rplaca last (concatenate 'string (first last) elt))) 
     43                 ((consp (first last)) 
     44                  (append-to-last last elt)) 
     45                 (t (error "unknown form"))) 
     46           form)) 
     47        (t (error "unknown form"))))