Changeset 22502

Show
Ignore:
Timestamp:
10/04/08 03:12:27 (7 weeks ago)
Author:
putter
Message:

[elfish/on_sbcl] Additional prelude. macro renaming. can(), .<> . CL elf did -e 'say 3'.

Location:
misc/elfish/on_sbcl
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • misc/elfish/on_sbcl/EmitSBCL.pm

    r22466 r22502  
    2121   ''; 
    2222  } 
    23 # (setq |GLOBAL::@ARGS| (new-Array (mapcar #'UP '("-v" "-e" "say 3")))) 
    2423  method prelude ($n) { 
    2524  '#| 
     
    3029|# 
    3130 
    32 (require \'asdf) 
    33 (require \'sb-posix) 
    3431(eval-when (:compile-toplevel :load-toplevel :execute) 
    35 (pushnew #p"lib-cl/systems/" asdf:*central-registry*) 
    36 (asdf:operate \'asdf:load-op :cl-ppcre) 
     32  (require \'asdf) 
     33  (require \'sb-posix) 
    3734) 
    38  
    39 ;(declaim (optimize (debug 2))) 
    40  
     35(eval-when (:compile-toplevel :load-toplevel :execute) 
     36  (pushnew #p"lib-cl/systems/" asdf:*central-registry*) 
     37  (asdf:operate \'asdf:load-op :cl-ppcre) 
     38) 
     39 
     40(declaim (optimize (debug 2))) 
    4141 
    4242;;------------------------------------------------------------------------------ 
    43 ;; Multi-methods - avoid generic-function congruence restrictions. 
     43;; Generic-functions without congruence restrictions. 
    4444;; http://www.lispworks.com/documentation/HyperSpec/Body/07_fd.htm 
    4545 
     
    4747(defvar *maximum-number-of-dispatch-affecting-variables* 10) 
    4848 
    49 (defun n-variable-names (n &optional l) 
    50   (cond ((= 0 n) l) 
    51         (t (n-variable-names (1- n) (cons (gensym) l))))) 
     49(defun n-gensyms (n) (loop for i from 1 to n collect (gensym))) 
    5250) 
    5351 
    54 (defmacro fc-old (func &rest args) 
    55   `(ap ,func (list ,@args))) 
    56  
    57 (defmacro fc (func &rest args) 
    58   `(rw (fc-preserving-rw ,func ,@args))) 
    59  
    60 (defmacro fc-preserving-rw (func &rest args) 
     52(defmacro ncgf-funcall (func &rest args) 
    6153  (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 
    6254         (len (length args)) 
    63          (syms (loop for n from 1 to len collect (gensym))) 
     55         (syms (n-gensyms len)) 
    6456         (dispatch-syms (subseq syms 0 (min n len))) 
    6557         (dispatch-padding (make-list (max 0 (- n len)))) 
     
    7163          (funcall ,f (list ,@syms) ,@dispatch-syms ,@dispatch-padding)))))) 
    7264 
    73 (defgeneric ap (func args)) 
    74 (defmethod ap (func args) 
     65(defgeneric ncgf-apply (func args)) 
     66(defmethod ncgf-apply (func args) 
    7567  (apply func args)) 
    76 (defmethod ap ((func standard-generic-function) args) 
     68(defmethod ncgf-apply ((func standard-generic-function) args) 
    7769  (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 
    7870         (len (length args)) 
     
    8274    (apply func (cons args dispatch-args)))) 
    8375          
    84 (defmacro dg (name sig) 
     76(defmacro ncgf-defgeneric (name sig) 
    8577  (declare (ignore sig)) 
    8678  (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 
    87          (vars (n-variable-names n))) 
     79         (vars (n-gensyms n))) 
    8880    `(defgeneric ,name (args ,@vars)))) 
    8981 
    9082(eval-when (:compile-toplevel :load-toplevel :execute) 
    9183(defun parameters-in-lambda-list (sig) 
    92   (let ((pred (lambda(e) (case e 
    93                                (&optional t) 
    94                                (&rest t) 
    95                                )))) 
    96     (remove-if pred sig))) ;X should stop at &aux, etc. 
     84  (let ((not-param 
     85         (lambda(e) (case e 
     86                          (&optional t) 
     87                          (&rest t) 
     88                          )))) 
     89    (remove-if not-param sig))) ;X should stop at &aux, etc. 
    9790) 
    9891 
    99 (defmacro dm (name sig &rest body) 
     92(defmacro ncgf-defmethod (name sig &rest body) 
    10093  (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 
    10194         (n-1 (1- n)) 
     
    10699                               (gensym) 
    107100                             `(,(gensym) ,(class-of nil))))) 
    108          (pad-vars (n-variable-names (max 0 (- n-1 len)))) 
     101         (pad-vars (n-gensyms (max 0 (- n-1 len)))) 
    109102         (dispatch-vars (concatenate \'list real-vars bounds-var pad-vars)) 
    110103         (typeless-sig (map \'list (lambda (p) (if (listp p) (car p) p)) sig)) 
    111          ) 
    112     `(defmethod ,name (args ,@dispatch-vars) 
    113        (declare (ignore ,@pad-vars)) 
    114        (destructuring-bind ,typeless-sig args 
    115          ,@body)))) 
     104         (def `(defmethod ,name (args ,@dispatch-vars) 
     105                 (declare (ignore ,@pad-vars)) 
     106                 (destructuring-bind ,typeless-sig args 
     107                                     ,@body)))) 
     108    def)) 
     109 
    116110 
    117111;;------------------------------------------------------------------------------ 
    118112;; Classes 
    119  
    120 (defgeneric UP (x)) 
    121113 
    122114(defmacro pkg-init-flag-name (pkg) `(concatenate \'string ,pkg "/initialized")) 
     
    198190 
    199191;;------------------------------------------------------------------------------ 
     192;; can 
     193 
     194(defmacro def-can (name sig) 
     195  (let* ((arg0-type (cond ((not (listp (car sig))) t) 
     196                          (t (cadar sig)))) 
     197         (sym (intern (concatenate \'string "CAN::" (symbol-name name))))) 
     198    `(defmethod ,sym ((x ,arg0-type)) nil))) 
     199 
     200(defun can-p (name obj) 
     201  (let* ((sym (intern (concatenate \'string "CAN::" name))) 
     202         (gf (symbol-function sym)) 
     203         (argl (list obj))) 
     204    (if (not gf) nil 
     205      (if (compute-applicable-methods gf argl) 
     206          t nil)))) 
     207 
     208;;------------------------------------------------------------------------------ 
     209 
     210(defmacro fc (func &rest args) 
     211  `(rw (ncgf-funcall ,func ,@args))) 
     212 
     213(defmacro fc-preserving-rw (func &rest args) 
     214  `(ncgf-funcall ,func ,@args)) 
     215 
     216(defmacro ap (func args) 
     217  `(ncgf-apply ,func ,args)) 
     218 
     219(defmacro dm-without-can (name sig &rest body) 
     220  `(ncgf-defmethod ,name ,sig ,@body)) 
     221 
     222(defmacro dm (name sig &rest body) 
     223  `(progn 
     224     (def-can ,name ,sig) 
     225     (ncgf-defmethod ,name ,sig ,@body))) 
     226 
     227;;------------------------------------------------------------------------------ 
    200228;; Prelude & stuff 
    201229 
    202230;; abbreviations 
     231(defgeneric UP (x)) 
    203232(defmacro S (x) `(to-s ,x)) 
    204233(defmacro N (x) `(to-n ,x)) 
     
    206235(defgeneric to-n (x)) 
    207236 
     237;; predecls 
     238(defclass |Any/cls| () ()) 
     239(defclass |Bool/cls| () ()) 
     240(defclass |Int/cls| () ()) 
     241(defclass |Num/cls| () ()) 
     242(defclass |Str/cls| () ()) 
     243 
     244;; 
    208245(defun set-slots (o argl) 
    209246  (let* ((clsname (symbol-name (class-name (class-of o)))) 
     
    217254  o) 
    218255 
    219 (make-package "M") 
    220  
    221 (defclass |Any/cls| () ()) 
    222 (defclass |Bool/cls| () ()) 
    223 (defclass |Int/cls| () ()) 
    224 (defclass |Num/cls| () ()) 
    225 (defclass |Str/cls| () ()) 
    226  
    227 (dg |M::new| (cls &rest argl)) 
    228  
     256;; 
    229257(dm |M::new| ((co |Any/cls|) &rest argl) 
    230258  (declare (ignorable argl)) 
    231259  (set-slots (make-instance (class-of co)) argl)) 
    232260 
    233 ;; Undef is still being kludged as nil. 
     261(dm |M::can| (self |$method_name|) 
     262    (let* ((name (concatenate \'string "M::" (S |$method_name|)))) 
     263      (UP (can-p name self)))) 
     264 
     265;; Undef is still being kludged as nil.  And Bools. 
    234266(defmacro undef () nil) 
    235267(dm |M::make_ir_from_Match_tree| ((self null) ) (block __f__ (let () self))) 
    236 (dm |M::isa| ((x null) s) (equal (S s) "Undef")) 
     268(dm |M::isa| ((x null) str) (declare (ignorable x)) (equal (S str) "Undef")) 
     269(dm |M::Str| ((x symbol)) (UP (symbol-name x))) ;X for t 
    237270 
    238271 ;;Array.new is defined here to a avoid cyclic dependency on *@args. 
     
    532565    my $enc_name = $.qsym('M::'~$.e($n.name)); 
    533566    my $sig = $.e($n.multisig); 
    534     my $decl = '(eval \'(dm '~$enc_name~' ((self '~$cls~') '~$sig~' (block __f__ '~$body~')))'; 
     567    my $decl = ('(eval \'(dm '~$enc_name~' ((self '~$cls~') '~$sig~ 
     568                ' (declare (ignorable self))'~ 
     569                ' (block __f__ '~$body~')))'); 
    535570    $whiteboard::compunit_header.push($decl); 
    536571    ""; 
     
    563598      if $n.plurality && $n.plurality eq 'multi' { 
    564599        my $dm_name = $.qsym('MS::'~$pkg~'::&'~$name); 
    565         $decl = ('(dm '~$dm_name~' '~$most~')'~"\n"~ 
     600        $decl = ('(dm-without-can '~$dm_name~' '~$most~')'~"\n"~ 
    566601                 '(defparameter '~$enc_name~' #\''~$dm_name~')'); 
    567602      } else { 
     
    618653      $invocant = ""~$.classobject_from_package_name($invocant); 
    619654    } 
    620     my $call = '(fc '~$meth~' '~$invocant~' '~$.e($n.capture)~')'; 
     655    my $e_capture = $.e($n.capture); 
     656    if $g = $method.re_groups('^postcircumfix:(.+)') { 
     657      my $op = $g[0]; 
     658      if $op eq '< >' { 
     659        my $s = $n.capture.arguments[0]; 
     660        my $words = $s.split('\s+'); 
     661        my $self = self; 
     662        $e_capture = $words.map(sub($x){$self.qstr($x)}).join(" "); 
     663      } 
     664    } 
     665    my $call = '(fc '~$meth~' '~$invocant~' '~$e_capture~')'; 
    621666    $call; 
    622667  } 
     
    696741      if $op eq '< >' { 
    697742        my $s = $n.capture.arguments[0]; 
    698         my $words = $s.split(/\s+/); 
    699         if $words.elems == 0 { 
    700           return $.emit_array(''); 
    701         } else { 
    702           return $.emit_array('"'~$words.join('" "')~'"'); 
    703         } 
     743        my $words = $s.split('\s+'); 
     744        my $self = self; 
     745        return $.emit_array($words.map(sub($x){$self.qstr($x)}).join(" ")); 
    704746      } 
    705747    } 
  • misc/elfish/on_sbcl/PrimitivesSBCL.pm

    r22466 r22502  
    8282  multi slurp ($filename) is cl {' 
    8383    (with-open-file (stream (S |$filename|)) 
    84       (let ((str (make-string (file-length stream)))) 
    85         (read-sequence str stream) 
     84      (let* ((byte-length (file-length stream)) 
     85             (buf (make-string byte-length)) ; likely too long 
     86             (char-length (read-sequence buf stream)) 
     87             (str (subseq buf 0 char-length))) 
    8688        (UP str))) 
    8789  '} 
     
    119121      (labels 
    120122       ((undump (node) 
    121            (cond ((listp node) 
     123           (cond ((null node) (undef)) 
     124                 ((listp node) 
    122125                  (let ((args (mapcar #\'undump (cdr node)))) 
    123126                    (ecase (car node) 
     
    142145  } 
    143146  sub private_tidy ($s) { $s } 
     147  sub eval_runtime_code($code,$env) is cl {' 
     148    (eval (read-from-string (S |$code|))) 
     149  '} 
     150  sub file_exists ($filename) is cl {' 
     151    (UP (if (probe-file (S |$filename|)) t nil)) 
     152  '} 
    144153} 
    145154# regexp elf bootstrap primitives 
    146155package Str { 
    147   method re_matchp ($re) is cl {' (UP (ppcre::scan (S |$re|) (S self))) '} 
     156  method re_matchp ($re) is cl {' (UP (if (ppcre::scan (S |$re|) (S self)) t nil)) '} 
    148157  method re_groups ($re) is cl {' 
    149158    (multiple-value-bind (match_str a) (ppcre::scan-to-strings (S |$re|) (S self)) 
     
    160169} 
    161170 
     171 
    162172package Main { 
    163173} 
     
    171181class Undef { 
    172182} 
     183 
    173184 
    174185class Pair { 
     
    204215      (setf (slot-value inst \'|Str::._native_|) (S |$s|)) 
    205216      inst) 
     217  '} 
     218  method split ($pat) is cl {' 
     219    (let ((s (slot-value self \'|Str::._native_|))) 
     220      (new-Array (ppcre::split (S |$pat|) s))) 
     221  '} 
     222  method substr ($offset,$length) is cl {' 
     223    (let* ((s (slot-value self \'|Str::._native_|)) 
     224           (len (length s)) 
     225           (off (wrapped-index len (N |$offset|)))) 
     226      (UP (subseq s off (min len (+ off (N |$length|)))))) 
    206227  '} 
    207228} 
     
    269290    (let* ((a (slot-value self \'|Array::._native_|))) 
    270291      (new-Array (loop for v across a collect (fc |$code| v)))) 
     292  '} 
     293  method splice ($from,$to) is cl {' 
     294    (let* ((a (slot-value self \'|Array::._native_|)) 
     295           (len (length a)) 
     296           (from (wrapped-index len (N |$from|))) 
     297           (to (wrapped-index len (N |$to|)))) 
     298      (new-Array (subseq a from to))) 
     299  '} 
     300  method reverse () is cl {' 
     301    (let* ((a (slot-value self \'|Array::._native_|))) 
     302      (new-Array (reverse a))) 
    271303  '} 
    272304} 
     
    290322      inst) 
    291323  '} 
     324  method dup () is cl {' 
     325    (let ((hk (slot-value self \'|Hash::._keys_|)) 
     326          (hv (slot-value self \'|Hash::._values_|)) 
     327          (hk2 (make-hash-table :test #\'equal)) 
     328          (hv2 (make-hash-table :test #\'equal)) 
     329          (inst (make-instance \'|Hash/cls|))) 
     330      (setf (slot-value inst \'|Hash::._keys_|) hk) 
     331      (setf (slot-value inst \'|Hash::._values_|) hv) 
     332      (maphash #\'(lambda (k v) (setf (gethash k hk2) v)) hk) 
     333      (maphash #\'(lambda (k v) (setf (gethash k hv2) v)) hv) 
     334      inst) 
     335  '} 
    292336 
    293337  method kv () is cl {' 
     
    368412class Str   { method Num () { self.primitive_Num() } } 
    369413class Array { method Num () { self.elems } } 
    370 class Hash  { method Num () { self.keys.elems } } 
     414class Hash  { method Num () { self.keys.elems } } ;#X hash-table-count 
    371415class Pair  { method Num () { 2 } } 
    372416