Changeset 22502
- Timestamp:
- 10/04/08 03:12:27 (7 weeks ago)
- Location:
- misc/elfish/on_sbcl
- Files:
-
- 2 modified
-
EmitSBCL.pm (modified) (13 diffs)
-
PrimitivesSBCL.pm (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
misc/elfish/on_sbcl/EmitSBCL.pm
r22466 r22502 21 21 ''; 22 22 } 23 # (setq |GLOBAL::@ARGS| (new-Array (mapcar #'UP '("-v" "-e" "say 3"))))24 23 method prelude ($n) { 25 24 '#| … … 30 29 |# 31 30 32 (require \'asdf)33 (require \'sb-posix)34 31 (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) 37 34 ) 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))) 41 41 42 42 ;;------------------------------------------------------------------------------ 43 ;; Multi-methods - avoid generic-functioncongruence restrictions.43 ;; Generic-functions without congruence restrictions. 44 44 ;; http://www.lispworks.com/documentation/HyperSpec/Body/07_fd.htm 45 45 … … 47 47 (defvar *maximum-number-of-dispatch-affecting-variables* 10) 48 48 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))) 52 50 ) 53 51 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) 61 53 (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 62 54 (len (length args)) 63 (syms ( loop for n from 1 to len collect (gensym)))55 (syms (n-gensyms len)) 64 56 (dispatch-syms (subseq syms 0 (min n len))) 65 57 (dispatch-padding (make-list (max 0 (- n len)))) … … 71 63 (funcall ,f (list ,@syms) ,@dispatch-syms ,@dispatch-padding)))))) 72 64 73 (defgeneric ap(func args))74 (defmethod ap(func args)65 (defgeneric ncgf-apply (func args)) 66 (defmethod ncgf-apply (func args) 75 67 (apply func args)) 76 (defmethod ap((func standard-generic-function) args)68 (defmethod ncgf-apply ((func standard-generic-function) args) 77 69 (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 78 70 (len (length args)) … … 82 74 (apply func (cons args dispatch-args)))) 83 75 84 (defmacro dg(name sig)76 (defmacro ncgf-defgeneric (name sig) 85 77 (declare (ignore sig)) 86 78 (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 87 (vars (n- variable-names n)))79 (vars (n-gensyms n))) 88 80 `(defgeneric ,name (args ,@vars)))) 89 81 90 82 (eval-when (:compile-toplevel :load-toplevel :execute) 91 83 (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. 97 90 ) 98 91 99 (defmacro dm(name sig &rest body)92 (defmacro ncgf-defmethod (name sig &rest body) 100 93 (let* ((n (1+ *maximum-number-of-dispatch-affecting-variables*)) 101 94 (n-1 (1- n)) … … 106 99 (gensym) 107 100 `(,(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)))) 109 102 (dispatch-vars (concatenate \'list real-vars bounds-var pad-vars)) 110 103 (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 116 110 117 111 ;;------------------------------------------------------------------------------ 118 112 ;; Classes 119 120 (defgeneric UP (x))121 113 122 114 (defmacro pkg-init-flag-name (pkg) `(concatenate \'string ,pkg "/initialized")) … … 198 190 199 191 ;;------------------------------------------------------------------------------ 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 ;;------------------------------------------------------------------------------ 200 228 ;; Prelude & stuff 201 229 202 230 ;; abbreviations 231 (defgeneric UP (x)) 203 232 (defmacro S (x) `(to-s ,x)) 204 233 (defmacro N (x) `(to-n ,x)) … … 206 235 (defgeneric to-n (x)) 207 236 237 ;; predecls 238 (defclass |Any/cls| () ()) 239 (defclass |Bool/cls| () ()) 240 (defclass |Int/cls| () ()) 241 (defclass |Num/cls| () ()) 242 (defclass |Str/cls| () ()) 243 244 ;; 208 245 (defun set-slots (o argl) 209 246 (let* ((clsname (symbol-name (class-name (class-of o)))) … … 217 254 o) 218 255 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 ;; 229 257 (dm |M::new| ((co |Any/cls|) &rest argl) 230 258 (declare (ignorable argl)) 231 259 (set-slots (make-instance (class-of co)) argl)) 232 260 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. 234 266 (defmacro undef () nil) 235 267 (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 237 270 238 271 ;;Array.new is defined here to a avoid cyclic dependency on *@args. … … 532 565 my $enc_name = $.qsym('M::'~$.e($n.name)); 533 566 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~')))'); 535 570 $whiteboard::compunit_header.push($decl); 536 571 ""; … … 563 598 if $n.plurality && $n.plurality eq 'multi' { 564 599 my $dm_name = $.qsym('MS::'~$pkg~'::&'~$name); 565 $decl = ('(dm '~$dm_name~' '~$most~')'~"\n"~600 $decl = ('(dm-without-can '~$dm_name~' '~$most~')'~"\n"~ 566 601 '(defparameter '~$enc_name~' #\''~$dm_name~')'); 567 602 } else { … … 618 653 $invocant = ""~$.classobject_from_package_name($invocant); 619 654 } 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~')'; 621 666 $call; 622 667 } … … 696 741 if $op eq '< >' { 697 742 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(" ")); 704 746 } 705 747 } -
misc/elfish/on_sbcl/PrimitivesSBCL.pm
r22466 r22502 82 82 multi slurp ($filename) is cl {' 83 83 (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))) 86 88 (UP str))) 87 89 '} … … 119 121 (labels 120 122 ((undump (node) 121 (cond ((listp node) 123 (cond ((null node) (undef)) 124 ((listp node) 122 125 (let ((args (mapcar #\'undump (cdr node)))) 123 126 (ecase (car node) … … 142 145 } 143 146 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 '} 144 153 } 145 154 # regexp elf bootstrap primitives 146 155 package 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)) '} 148 157 method re_groups ($re) is cl {' 149 158 (multiple-value-bind (match_str a) (ppcre::scan-to-strings (S |$re|) (S self)) … … 160 169 } 161 170 171 162 172 package Main { 163 173 } … … 171 181 class Undef { 172 182 } 183 173 184 174 185 class Pair { … … 204 215 (setf (slot-value inst \'|Str::._native_|) (S |$s|)) 205 216 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|)))))) 206 227 '} 207 228 } … … 269 290 (let* ((a (slot-value self \'|Array::._native_|))) 270 291 (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))) 271 303 '} 272 304 } … … 290 322 inst) 291 323 '} 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 '} 292 336 293 337 method kv () is cl {' … … 368 412 class Str { method Num () { self.primitive_Num() } } 369 413 class Array { method Num () { self.elems } } 370 class Hash { method Num () { self.keys.elems } } 414 class Hash { method Num () { self.keys.elems } } ;#X hash-table-count 371 415 class Pair { method Num () { 2 } } 372 416
