Advertisement
logicmoo

Untitled

Sep 2nd, 2014
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 37.36 KB | None | 0 0
  1. ;;Saved into a file called common_lisp.lisp <?
  2.  
  3.  
  4. ;; ussually CYC
  5. (defvar *cl-importing-package* *package*)
  6.  
  7. ;;(in-package "SUBLISP")
  8. (defmacro prog1 (body1 &body body) (ret `(clet ((prog1res ,body1)) ,@body prog1res)))
  9.  
  10.  
  11. (sl:defmacro defun (symbolp args sl:&body body)
  12.              (ret `(progn  
  13.                      ;; (sl::export '(,symbolp))
  14.                      (format t ";; ~A cl-defun \"~A\" ~S " ,(package-name  *package* )',symbolp ',args) (terpri)(force-output)
  15.  
  16.                      (sl::define ,symbolp ,args (ret (progn ,@body))))))
  17.  
  18. (sl:defmacro cl-defun (symbolp args sl:&body body)
  19.              (ret `(progn  
  20.                      ;; (sl::export '(,symbolp))
  21.                      (format t ";; ~A cl-defun \"~A\" ~S " ,(package-name  *package* )',symbolp ',args) (terpri)(force-output)
  22.                      (sl::define ,symbolp ,args (ret (progn ,@body))))))
  23.  
  24. ;;(sl::in-package "CL")
  25. ;;(sl::import '(defun defmacro) *cl-package*)
  26. (defmacro cl-defmacro (symbolp args sl:&body body)
  27.   (ret `(progn  
  28.           ;; (sl::export '(,symbolp))
  29.           (format t ";; ~A defmacro-like-cl \"~A\" ~S " ,(package-name *package* )',symbolp ',args) (terpri)(force-output)
  30.           ( sl::defmacro ,symbolp ,args (ret (progn ,@body))))))
  31.  
  32. ;;(sl::export '(cl::defmacro-like-cl) *cl-package*)
  33.  
  34.  
  35. (cl-defmacro memq (item my-list)
  36.              `(member ,item ,my-list :test #'eq))
  37.  
  38. (defun cons-when (cond f)
  39.     (if (and cond f) (cons cond f ) nil))
  40.  
  41.  
  42. (defun ele (num obj)
  43.   (cond
  44.     ((vectorp obj)(aref obj num))
  45.     ((listp obj)(nth num obj))
  46.     ((iterator-p obj)(ele num (ITERATOR-VALUE-LIST  (COPY-ITERATOR obj))))
  47.     ((SET-P obj)(ele num (SET-ELEMENT-LIST obj)))
  48.     ((SET-CONTENTS-P obj)(ele num (SET-CONTENTS-ELEMENT-LIST obj)))
  49.     ))
  50.  
  51. #|
  52. ;; (cl-rewrite-function 'set-dispatch-macro-character)
  53.  
  54. (cl-defmacro psetq (&rest pairs)
  55.              ;; not use reverse for build order consistency
  56.              (do* ((pairs pairs (cddr pairs))
  57.                    (tmp (gensym) (gensym))
  58.                    (inits (list nil))
  59.                    (inits-splice inits)
  60.                    (setqs (list nil))
  61.                    (setqs-splice setqs))
  62.                   ((null pairs) (when (cdr inits)
  63.                                   `(let ,(cdr inits)
  64.                                      (setq ,@(cdr setqs))
  65.                                      nil)))
  66.                (setq inits-splice
  67.                      (cdr (rplacd inits-splice (list (list tmp (cadr pairs)))))
  68.                    setqs-splice
  69.                      (cddr (rplacd setqs-splice (list (car pairs) tmp))))))
  70.  
  71.  
  72. (cl-defmacro return (&optional result)
  73.              `(return-from nil ,result))
  74.  
  75. (defun equal (x y)
  76.   (cond
  77.    ((eql x y) t)
  78.    ((consp x) (and (consp y) (equal (car x) (car y)) (equal (cdr x) (cdr y))))
  79.    ((stringp x) (and (stringp y) (string= x y)))
  80.    ((bit-vector-p x) (and (bit-vector-p y) (= (length x) (length y))
  81.                           (dotimes (i (length x) t)
  82.                             (unless (eql (aref x i) (aref y i))
  83.                               (return nil)))))
  84.    ((pathnamep x) (and (pathnamep y)
  85.                        (equal (pathname-host x) (pathname-host y))
  86.                        (equal (pathname-device x) (pathname-device y))
  87.                        (equal (pathname-directory x) (pathname-directory y))
  88.                        (equal (pathname-name x) (pathname-name y))
  89.                        (equal (pathname-type x) (pathname-type y))
  90.                        (equal (pathname-version x) (pathname-version y))))
  91.    (t nil)))
  92. |#
  93. #|
  94. (defun identity (object)
  95.   object)
  96.  
  97. (defun complement (function)
  98.   #'(lambda (&rest arguments) (not (apply function arguments))))
  99.  
  100. (defun constantly (object)
  101.   #'(lambda (&rest arguments)
  102.       (declare (ignore arguments))
  103.       object))
  104.  
  105. (cl-defmacro and (&rest forms)
  106.              (cond
  107.               ((null forms) t)
  108.               ((null (cdr forms)) (car forms))
  109.               (t `(when ,(car forms)
  110.                     (and ,@(cdr forms))))))
  111.  
  112. (cl-defmacro or (&rest forms)
  113.              (cond
  114.               ((null forms) nil)
  115.               ((null (cdr forms)) (car forms))
  116.               (t (let ((tmp (gensym)))
  117.                    `(let ((,tmp ,(car forms)))
  118.                       (if ,tmp
  119.                           ,tmp
  120.                         (or ,@(cdr forms))))))))
  121.  
  122. (cl-defmacro cond (&rest clauses)
  123.              (when clauses
  124.                (let ((test1 (caar clauses))
  125.                      (forms1 (cdar clauses)))
  126.                  (if forms1
  127.                      `(if ,test1
  128.                           (progn ,@forms1)
  129.                         (cond ,@(cdr clauses)))
  130.                    (let ((tmp (gensym)))
  131.                      `(let ((,tmp ,test1))
  132.                         (if ,tmp
  133.                             ,tmp
  134.                           (cond ,@(cdr clauses)))))))))
  135.  
  136. (cl-defmacro when (test-form &rest forms)
  137.              `(if ,test-form
  138.                   (progn ,@forms)
  139.                 nil))
  140.  
  141. (cl-defmacro unless (test-form &rest forms)
  142.              `(if ,test-form
  143.                   nil
  144.                 (progn ,@forms)))
  145.  
  146. ;;(defmacro block-to-tagname (bname) (ret `(gensym ',bname)))
  147. (defmacro block-to-tagname (bname) (print (ret `',bname)))
  148.  
  149. (cl-defmacro case (keyform &rest clauses)(expand-case keyform clauses))
  150.  
  151. (cl-defmacro ccase (keyplace &rest clauses)
  152.              (let* ((clauses (mapcar #'(lambda (clause)
  153.                                          (let ((key (first clause))
  154.                                                (forms (rest clause)))
  155.                                            `(,(%list key) ,@forms)))
  156.                                clauses))
  157.                     (expected-type `(member ,@(apply #'append (mapcar #'car clauses))))
  158.                     (block-name (gensym))
  159.                     (tag (gensym)))
  160.                `(block ,block-name
  161.                   (tagbody
  162.                     ,tag
  163.                     (return-from ,block-name
  164.                       (case ,keyplace
  165.                         ,@clauses
  166.                         (t (restart-case (error 'type-error :datum ,keyplace
  167.                                            :expected-type ',expected-type)
  168.                              (store-value (value)
  169.                                           :report (lambda (stream)
  170.                                                     (store-value-report stream ',keyplace))
  171.                                           :interactive store-value-interactive
  172.                                           (setf ,keyplace value)
  173.                                           (go ,tag))))))))))
  174.  
  175.  
  176. (cl-defmacro ecase (keyform &rest clauses)
  177.              (let* ((clauses (mapcar #'(lambda (clause)
  178.                                          (let ((key (first clause))
  179.                                                (forms (rest clause)))
  180.                                            `(,(%list key) ,@forms)))
  181.                                clauses))
  182.                     (expected-type `(member ,@(apply #'append (mapcar #'car clauses)))))
  183.                `(case ,keyform
  184.                   ,@clauses
  185.                   (t (error 'type-error :datum ,keyform :expected-type ',expected-type)))))
  186.  
  187. (cl-defmacro typecase (keyform &rest clauses)
  188.              (let* ((last (car (last clauses)))
  189.                     (clauses (mapcar #'(lambda (clause)
  190.                                          (let ((type (first clause))
  191.                                                (forms (rest clause)))
  192.                                            (if (and (eq clause last)
  193.                                                     (member type '(otherwise t)))
  194.                                                clause
  195.                                              `((,type) ,@forms))))
  196.                                clauses)))
  197.                (expand-case keyform clauses :test #'typep)))
  198.  
  199. (cl-defmacro ctypecase (keyplace &rest clauses)
  200.              (let ((expected-type `(or ,@(mapcar #'car clauses)))
  201.                    (block-name (gensym))
  202.                    (tag (gensym)))
  203.                `(block ,block-name
  204.                   (tagbody
  205.                     ,tag
  206.                     (return-from ,block-name
  207.                       (typecase ,keyplace
  208.                         ,@clauses
  209.                         (t (restart-case (error 'type-error
  210.                                            :datum ,keyplace
  211.                                            :expected-type ',expected-type)
  212.                              (store-value (value)
  213.                                           :report (lambda (stream)
  214.                                                     (store-value-report stream ',keyplace))
  215.                                           :interactive store-value-interactive
  216.                                           (setf ,keyplace value)
  217.                                           (go ,tag))))))))))
  218.  
  219.  
  220.  
  221. (cl-defmacro etypecase (keyform &rest clauses)
  222.              `(typecase ,keyform
  223.                 ,@clauses
  224.                 (t (error 'type-error
  225.                      :datum ',keyform :expected-type '(or ,@(mapcar #'car clauses))))))
  226. |#
  227. #|
  228. (cl-defmacro multiple-value-bind (vars values-form &body body)
  229.              (cond
  230.               ((null vars)
  231.                `(progn ,@body))
  232.               ((null (cdr vars))
  233.                `(let ((,(car vars) ,values-form))
  234.                   ,@body))
  235.               (t
  236.                (let ((rest (gensym)))
  237.                  `(multiple-value-call #'(lambda (&optional ,@vars &rest ,rest)
  238.                                            (declare (ignore ,rest))
  239.                                            ,@body)
  240.                     ,values-form)))))
  241.  
  242.  
  243.  
  244. (cl-defmacro multiple-value-list (form)
  245.              `(multiple-value-call #'list ,form))
  246.  
  247.  
  248. (cl-defmacro multiple-value-setq (vars form)
  249.              `(values (setf (values ,@vars) ,form)))
  250. ;;  (let ((temps (mapcar #'(lambda (x) (declare (ignore x)) (gensym)) vars)))
  251. ;;    `(multiple-value-bind ,temps ,form
  252. ;;       (setq ,@(mapcan #'(lambda (var temp) (list var temp)) vars temps))
  253. ;;       ,(car temps))))
  254.  
  255. (defun values-list (list)
  256.   (check-type list proper-list)
  257.   (apply #'values list))
  258.  
  259. (cl-defmacro nth-value (n form)
  260.              `(nth ,n (multiple-value-list ,form)))
  261.  
  262. (define-setf-expander values (&rest places &environment env)
  263.   (let (all-temps all-vars 1st-newvals rest-newvals all-setters all-getters)
  264.     (dolist (place places)
  265.       (multiple-value-bind (temps vars newvals setter getter)
  266.           (get-setf-expansion place env)
  267.         (setq all-temps    (cons temps all-temps)
  268.             all-vars     (cons vars all-vars)
  269.             1st-newvals  (cons (car newvals) 1st-newvals)
  270.             rest-newvals (cons (cdr newvals) rest-newvals)
  271.             all-setters  (cons setter all-setters)
  272.             all-getters  (cons getter all-getters))))
  273.     (values (apply #'append (reverse (append rest-newvals all-temps)))
  274.             (append (apply #'append (reverse all-vars))
  275.                     (make-list (reduce #'+ rest-newvals :key #'length)))
  276.             (reverse 1st-newvals)
  277.             `(values ,@(reverse all-setters))
  278.             `(values ,@(reverse all-getters)))))
  279. ;;(define-setf-expander apply (function &rest args)
  280. ;;  (assert (and (listp function)
  281. ;;               (= (list-length function) 2)
  282. ;;               (eq (first function) 'function)
  283. ;;               (symbolp (second function))))
  284. ;;  (let ((function (cadr function))
  285. ;;        (newvals (list (gensym)))
  286. ;;        (temps (mapcar #'(lambda (arg) (gensym)) args)))
  287. ;;    (values temps
  288. ;;            args
  289. ;;            newvals
  290. ;;            `(apply #'(setf ,function) ,(car newvals) ,@vars)
  291. ;;            `(apply #',function ,@temps))))
  292.  
  293. (cl-defmacro prog (vars &body body)
  294.              (flet ((declare-p (expr)
  295.                                (and (consp expr) (eq (car expr) 'declare))))
  296.                (do ((decls nil)
  297.                     (forms body (cdr forms)))
  298.                    ((not (declare-p (car forms))) `(block nil
  299.                                                      (let ,vars
  300.                                                        ,@(reverse decls)
  301.                                                        (tagbody ,@forms))))
  302.                  (push (car forms) decls))))
  303.  
  304. (cl-defmacro prog* (vars &body body)
  305.              (multiple-value-bind (decls forms) (split-into-declarations-and-forms body)
  306.                `(block nil
  307.                   (let* ,vars
  308.                     ,@(reverse decls)
  309.                     (tagbody ,@forms)))))
  310.  
  311. (cl-defmacro prog1 (first-form &rest more-forms)
  312.              (let ((result (gensym)))
  313.                `(let ((,result ,first-form))
  314.                   ,@more-forms
  315.                   ,result)))
  316.  
  317. (cl-defmacro prog2 (first-form second-form &rest more-forms)
  318.              `(prog1 (progn ,first-form ,second-form) ,@more-forms))
  319.  
  320.  
  321. (cl-defmacro setf (&rest pairs &environment env)
  322.              (let ((nargs (length pairs)))
  323.                (assert (evenp nargs))
  324.                (cond
  325.                 ((zerop nargs) nil)
  326.                 ((= nargs 2)
  327.                  (let ((place (car pairs))
  328.                        (value-form (cadr pairs)))
  329.                    (cond
  330.                     ((symbolp place)
  331.                      `(setq ,place ,value-form))
  332.                     ((consp place)
  333.                      (if (eq (car place) 'the)
  334.                          `(setf ,(caddr place) (the ,(cadr place) ,value-form))
  335.                        (multiple-value-bind (temps vars newvals setter getter)
  336.                            (get-setf-expansion place env)
  337.                          (declare (ignore getter))
  338.                          `(let (,@(mapcar #'list temps vars))
  339.                             (multiple-value-bind ,newvals ,value-form
  340.                               ,setter))))))))
  341.                 (t
  342.                  (do* ((pairs pairs (cddr pairs))
  343.                        (setfs (list 'progn))
  344.                        (splice setfs))
  345.                       ((endp pairs) setfs)
  346.                    (setq splice (cdr (rplacd splice
  347.                                              `((setf ,(car pairs) ,(cadr pairs)))))))))))
  348.  
  349. (cl-defmacro psetf (&rest pairs &environment env)
  350.              (let ((nargs (length pairs)))
  351.                (assert (evenp nargs))
  352.                (if (< nargs 4)
  353.                    `(progn (setf ,@pairs) nil)
  354.                  (let ((setters nil))
  355.                    (labels ((expand (pairs)
  356.                                     (if pairs
  357.                                         (multiple-value-bind (temps vars newvals setter getter)
  358.                                             (get-setf-expansion (car pairs) env)
  359.                                           (declare (ignore getter))
  360.                                           (setq setters (cons setter setters))
  361.                                           `(let (,@(mapcar #'list temps vars))
  362.                                              (multiple-value-bind ,newvals ,(cadr pairs)
  363.                                                ,(expand (cddr pairs)))))
  364.                                       `(progn ,@setters nil))))
  365.                      (expand pairs))))))
  366.  
  367. (cl-defmacro shiftf (&rest places-and-newvalue &environment env)
  368.              (let ((nargs (length places-and-newvalue)))
  369.                (assert (>= nargs 2))
  370.                (let ((place (car places-and-newvalue)))
  371.                  (multiple-value-bind (temps vars newvals setter getter)
  372.                      (get-setf-expansion place env)
  373.                    `(let (,@(mapcar #'list temps vars))
  374.                       (multiple-value-prog1 ,getter
  375.                         (multiple-value-bind ,newvals
  376.                             ,(if (= nargs 2)
  377.                                  (cadr places-and-newvalue)
  378.                                `(shiftf ,@(cdr places-and-newvalue)))
  379.                           ,setter)))))))
  380.  
  381. (cl-defmacro rotatef (&rest places &environment env)
  382.              (if (< (length places) 2)
  383.                  nil
  384.                (multiple-value-bind (temps vars newvals setter getter)
  385.                    (get-setf-expansion (car places) env)
  386.                  `(let (,@(mapcar #'list temps vars))
  387.                     (multiple-value-bind ,newvals (shiftf ,@(cdr places) ,getter)
  388.                       ,setter)
  389.                     nil))))
  390. |#
  391.  
  392. (defvar *eval-mode* (list :load-toplevel :execute) )
  393. (defmacro eval-when (when &body body) (ret `(if (intersection ',when *eval-mode*) (progn ,@body))))
  394.  
  395.  
  396. ;; transliterations
  397. (defmacro let (&body body) (ret `( clet ,@body)))
  398. (defmacro let* (&body body) (ret `( clet ,@body)))
  399. (defmacro dotimes (&body body) (ret `(cdotimes ,@body)))
  400. (defmacro case (&body body) (ret `( pcase ,@body)))
  401. (defmacro if (&body body) (ret `(fif ,@body)))
  402. (defmacro do (&body body) (ret `( cdo ,@body)))
  403. (defmacro not (&body body) (ret `(cnot ,@body)))
  404. (defmacro or (&body body) (ret `(cor ,@body)))
  405. (defmacro cond (&body body) (ret `( pcond ,@body)))
  406. (defmacro and (&body body) (ret `(cand ,@body)))
  407. (defmacro unless (&body body) (ret `(funless ,@body)))
  408. (defmacro when (&body body) (ret `(pwhen ,@body)))
  409. (defmacro setq (&body body) (ret `( csetq ,@body)))
  410. (defmacro setf (&body body) (ret `(csetf ,@body)))
  411. (defmacro pushnew (item place) (ret `(progn (cpushnew ,item ,place) ,place)))
  412. (defmacro push (&body body) (ret `(cpush ,@body)))
  413. (defmacro pop (place)
  414.   (ret `(let ((f1rst (elt ,place 0))) (CPOP) f1rst)))
  415. (defmacro concatenate (cltype &body args) (ret `(coerce (cconcatenate ,@args) ,cltype)))
  416.  
  417. ;;(defmacro until (test &body body)"Repeatedly evaluate BODY until TEST is true."(ret `(do ()(,test) ,@body)))
  418. (defmacro make-array (size &key initial-element ) (ret `(make-vector ,size  ,initial-element)))
  419.  
  420. (defmacro svref (array idx) (ret `(aref ,array ,idx)))
  421. ;;(defmacro incf (arg1 &body body) (ret `(fif (null body) (cincf arg1) (progn (cincf ,@body) ,@body)))
  422. (defmacro incf (&body body) (ret `(cinc ,@body)))
  423. (defmacro decf (&body body) (ret `(cdec ,@body)))
  424.  
  425. (defmacro unwind-protect (protected-form &body body) (ret `(cunwind-protect ,protected-form ,@body)))
  426. (defmacro destructuring-bind (args datum &body body) (ret `(cdestructuring-bind ,args ,datum  ,@body)))
  427. (defmacro multiple-value-bind (args datum &body body) (ret `(cmultiple-value-bind  ,args ,datum  ,@body)))
  428. (defmacro cmultiple-value-list (value &rest ignore) (ret `(multiple-value-list ,value)))
  429.  
  430. (defmacro debug-print (&body stuff)
  431.   (print stuff)(terpri)(force-output)
  432.   (pcond
  433.    ;; ((cdr stuff) (ret `(print (cons 'progn ,stuff))))
  434.    ;;  ((consp stuff) (ret `(print (cons 'prog1 ,stuff))))
  435.    (t (ret `(print (eval ',@stuff))))))
  436.  
  437. ;;(defmacro concat (&rest body) (ret `(progn (mapcar #'(lambda (x) (if (not (stringp x)) (debug-print (cons 'concat ',body)))) ,body)(apply #'cconcatenate (cons "" ,body)))))
  438. (define concat (&rest list) (ret (apply #'cconcatenate (cons "" (mapcar #'(lambda (x) (ret (if (stringp x) x (coerce x 'string) ))) list)))))
  439.  
  440.  
  441. (defmacro catch (tag &body body)
  442.   (ret
  443.    `(apply #'values  
  444.            (let ((*thrown* :UNTHROWN) (*result* :UNEVALED))
  445.              ;;(print (list 'eval (cons 'catch (cons ',tag  ',body))))(terpri)
  446.              (ccatch ,tag *thrown* (setq *result* (multiple-value-list (progn ,@body))))
  447.              (cond
  448.               ((equal *result* :UNEVALED) (list *thrown*))
  449.               (t *result*))))))
  450.  
  451. (define map-sequences (function sequences)
  452.   (ret (fif (member () sequences) () (cons (apply function (mapcar #'car sequences)) (map-sequences function (mapcar #'cdr sequences))))))
  453.  
  454. (define map (result-type function &body sequences)
  455.   (ret (fif result-type (coerce (map-sequences function sequences) result-type) (progn (map-sequences function sequences) nil))))
  456.  
  457. (define cl-make-string (&rest rest)
  458.   (ret (make-string (find 'numberp rest #'funcall)(find #'characterp rest 'funcall))))
  459.  
  460. ;;(define coerce (value result-type) (ret value))
  461. ;;are hashtables supposed ot be coercable back and forth from alists?
  462. (define coerce (value result-type)
  463.   (clet ((len value)(vtype (type-of value))(cltype result-type))
  464.         (pwhen (equal result-type vtype) (ret value))
  465.         (unless (cand (consp cltype) (setq len (second cltype)) (setq cltype (car cltype)))
  466.           (if (consp value) (setq len (length value))))
  467.         ;;     (print (list 'coerce value result-type cltype len))
  468.         (case cltype
  469.           ('t (ret value))
  470.           ('sequence
  471.            (if (sequencep value) (ret (copy-seq value)) (setq value (write-to-string value)))
  472.            (setq cltype (make-vector len))
  473.            (do ((idx 0 (+ 1 idx))) ((= idx len) (ret  cltype )) (set-aref cltype idx (elt value idx))))
  474.           ('character
  475.            (cond
  476.             ((characterp value) (ret value))
  477.             ((numberp value) (ret (code-char value)))
  478.             ((stringp value) (ret (char value 0)))
  479.             (t (ret (char (coerce value 'string ) 0)))))
  480.           ('number
  481.            (cond
  482.             ((numberp value) (ret value))
  483.             ((characterp value) (ret (char-code value)))
  484.             ((stringp value) (ret (string-to-number value)))
  485.             ;; not like CL
  486.             (t (ret (string-to-number (write-to-string value))))))
  487.           ('integer
  488.            (ret (round (coerce value 'number))))
  489.           ('fixnum
  490.            (ret (round (coerce value 'number))))
  491.           ('float
  492.            (ret (float (coerce value 'number))))
  493.           ('real
  494.            (ret (float (coerce value 'number))))
  495.           ('flonum
  496.            (ret (float (coerce value 'number))))
  497.           ('string
  498.            (cond
  499.             ((stringp value) (ret value))
  500.             ((characterp value) (ret (make-string 1 value)))
  501.             ((sequencep value) (setq cltype (make-string len))
  502.              (do ((idx 0 (+ 1 idx))) ((= idx len) (ret cltype )) (set-aref cltype idx (coerce (elt value idx) 'character))))
  503.             (t (ret (write-to-string value)))))
  504.           ('list
  505.            (cond
  506.             ((listp value) (ret list))
  507.             ((sequencep value)
  508.              (setq cltype nil)
  509.              (do ((idx len (- idx 1))) ((= idx 0) (ret  cltype )) (setq cltype (cons (elt value idx) cltype))))
  510.             (t
  511.              (setq cltype nil)
  512.              (setq value (write-to-string value))
  513.              (do ((idx len (- idx 1))) ((= idx 0) (ret  cltype )) (setq cltype (cons (elt value idx) cltype))))))
  514.           ('cons
  515.            (cond
  516.             ((listp value) (ret list))
  517.             ((sequencep value)
  518.              (setq cltype nil)
  519.              (do ((idx len (- idx 1))) ((= idx 0) (ret  cltype )) (setq cltype (cons (elt value idx) cltype))))
  520.             (t
  521.              (setq cltype nil)
  522.              (setq value (write-to-string value))
  523.              (do ((idx len (- idx 1))) ((= idx 0) (ret  cltype )) (setq cltype (cons (elt value idx) cltype))))))
  524.           ;; not finished
  525.           ('keypair
  526.            (cond
  527.             ((atom value) (ret list value))
  528.             (t (ret (coerce value 'cons)))))
  529.           ;; not finished
  530.           ('alist
  531.            ;;(if (hash-table-p value) (ret value))
  532.            (setq cltype (setq cltype nil))
  533.            (if (sequencep value) t (setq value (coerce value 'sequence)))
  534.            (do ((idx 0 (+ 1 idx))) ((= idx len) (ret cltype))
  535.              (setq result-type (coerce (elt value idx) 'cons))
  536.              (setq cltype (acons (car result-type) (cdr result-type) cltype)))
  537.            (ret cltype))
  538.           ;; not finished
  539.           ('hash-table
  540.            (if (hash-table-p value) (ret value))
  541.            (setq cltype (make-hash-table len))
  542.            (if (sequencep value) t (setq value (coerce value 'sequence)))
  543.            (do ((idx 0 (+ 1 idx))) ((= idx len) (ret cltype))
  544.              (print (list 'coerce value result-type cltype len (elt value idx)))
  545.              (setq result-type (coerce (elt value idx) 'keypair))
  546.              (sethash (car result-type) cltype (cdr result-type))))
  547.           ;; not like CL
  548.           (otherwise (ret value)))
  549.         (throw :coerce (list value result-type)))
  550.   (ret value))
  551.  
  552.  
  553.  
  554.  
  555.  
  556. ;;;;(load "sublisp-cl.lisp")
  557. #|
  558.  
  559. (define FIND-ALL-SYMBOLS (stringp &optional (packagelist (list-all-packages)) (status '(:inherited :external :internal)))
  560.   (ret (if packagelist
  561.            (clet ((package (car packagelist))(res (multiple-values-list (find-symbol stringp package))))
  562.                  (if  
  563.                      (member (cdr res) status)
  564.                      (cons (car res) (FIND-ALL-SYMBOLS stringp (cdr packagelist) status ))
  565.                    (FIND-ALL-SYMBOLS stringp (cdr packagelist) status ))))))
  566.  
  567. (defun eval-remote (server &rest remote)  (print remote))
  568.  
  569. ;;
  570. ;;  (load "common_lisp.lisp")(macroexpand '(defstub :COMMON-LISP DEFPACKAGE))
  571. (define defstub (pack symb &rest body)
  572.   ;;  (clet ((symb `,symbn))
  573.   (let ((sname (if (symbolp symb) (symbol-name symb) (if (stringp symb) symb "")))
  574.         (fpack (if (packagep pack) pack (find-package pack)))
  575.         (fsym  (if fpack (find-symbol sname fpack) (find-symbol sname))))
  576.     (when (and(symbolp symb)(fboundp symb)) (ret `(symbol-function ',symb)))
  577.     (when (and(symbolp fsym)(fboundp fsym)) (ret `(symbol-function ',fsym)))
  578.     (when (and(symbolp fsym)(fboundp fsym)(member fpack *packages-local*)) (ret `(symbol-function ',fsym)))
  579.     (unless (symbolp fsym)(setq fsym symb))
  580.     (unless (symbolp fsym)(setq fsym (intern sname)))
  581.     (unless fpack (setq fpack (symbol-package fsym)))
  582.     (setq sname (concat (package-name fpack) "::" sname))
  583.     (ret
  584.      (print `(eval
  585.               ',(print (if body
  586.                            ;;(list 'defmacro fsym (list 'quote (car body))(list 'ret (list 'BQ-LIST* (cons '(quote eval-remote) (cons (list 'quote sname) (cdr body))))))
  587.                            `(defmacro ,fsym ,(car body) (ret `(eval-remote ,,sname ,,@(cdr body))))
  588.                          
  589.                          (list 'defmacro fsym '(&rest args)(list 'ret (list 'BQ-LIST* '(quote eval-remote) (list 'quote sname) 'args))))))))))
  590.  
  591.  
  592. ;;(define do-server4005 (in-stream out-stream)(print (read in-stream) out-stream))
  593.  
  594. (defstub :common-lisp 'defpackage)
  595.  
  596.  
  597. ;; We will show that only one of the three non-local exit mechanisms block/return-from, tagbody/go, catch/throw is required to be primitive, by showing how to emulate any two in terms of the third.[4] We first emulate block/return-from in terms of catch/throw. We map the block name into the name of a lexical variable which will hold the unique tag which distinguishes this dynamical block from any other. If trivial return-from's are optimized away, then this emulation can be quite efficient.
  598. (cl-defmacro return-from-no (bname exp)
  599.              "BLOCK/RETURN-FROM EMULATED BY CATCH/THROW"
  600.              (let ((tagname (block-to-tagname bname)))
  601.                `(throw ,tagname ,exp)))
  602.  
  603. (cl-defmacro block-no (bname &body forms)
  604.              "BLOCK/RETURN-FROM EMULATED BY CATCH/THROW"
  605.              (let ((tagname (block-to-tagname bname)))
  606.                `(let ((,tagname (list nil))) ; Unique cons cell used as catch tag.
  607.                   (catch ,tagname (progn ,@forms)))))
  608.  
  609. ;; dont know if this is correct
  610.  
  611. (defmacro return (body) (ret `(ret ,body)))
  612.  
  613.  
  614.  
  615.  
  616. (defconstant *unbound-value* (list nil))
  617.  
  618. (defun msymbol-value (var)
  619.   (if (boundp var) (symbol-value var) *unbound-value*))
  620.  
  621. (defun mset (var val)
  622.   (if (eq val *unbound-value*) (makunbound var) (set var val)))
  623.  
  624. (defmacro progv (syms vals &body forms)
  625.   (let* ((vsyms (gensym)) (vvals (gensym)) (vovals (gensym)))
  626.     `(let* ((,vsyms ,syms)
  627.             (,vvals ,vals)
  628.             (,vovals ,(mapcar #'msymbol-value ,vsyms)))
  629.        (unwind-protect
  630.            (progn (mapc #'mset ,vsyms ,vvals)
  631.              (mapc #'makunbound (subseq ,vsyms (min (length ,vsyms) (length ,vvals))))
  632.              ,@forms )
  633.          (mapc #'mset ,vsyms ,vovals)))))
  634.  
  635. ;;EMULATE "THE" USING "LET" AND "DECLARE"
  636. ;;The emulation of the the special form emphasizes the fact that there is a run-time type test which must be passed in order for the program to proceed. Of course, a clever compiler can eliminate the run-time test if it can prove that it will always succeed--e.g., the gcd function always returns an integer if it returns at all.
  637.  
  638. (defmacro the (typ exp)
  639.   (if (and (consp typ) (eq (car typ) 'values))
  640.       (let ((vals (gensym)))
  641.         `(let ((,vals (multiple-value-list ,exp)))
  642.            (assert (= (length ,vals) ,(length (cdr typ))))
  643.            ,@(mapcar #'(lambda (typ i) `(assert (typep (elt ,vals ,i) ',typ)))
  644.                (cdr typ) (iota-list (length (cdr typ))))
  645.            (values-list ,vals)))
  646.     (let ((val (gensym)))
  647.       `(let ((,val ,exp))
  648.          (assert (typep ,val ',typ))
  649.          (let ((,val ,val)) (declare (type ,typ ,val))
  650.            ,val)))))
  651.  
  652.  
  653.  
  654. (cl-defmacro go (label)
  655.              "TAGBODY/GO EMULATED BY CATCH/THROW"
  656.              (let ((name (label-to-functionname label)))
  657.                `(throw ,name #',name)))
  658.  
  659. (cl-defmacro tagbody-no (&body body)
  660.              "TAGBODY/GO EMULATED BY CATCH/THROW"
  661.              (let* ((init-tag (gensym)) (go-tag (gensym)) (return-tag (gensym))
  662.                    
  663.                     (functions
  664.                      (mapcon
  665.                          #'(lambda (seq &aux (label (car seq) (s (cdr seq)))
  666.                                         (when (atom label)
  667.                                           (let ((p (position-if #'atom s)))
  668.                                             `((,(label-to-functionname label) ()
  669.                                                  ,@(subseq s 0 (or p (length s)))
  670.                                                  ,(if p `(,(label-to-functionname (elt s p)))
  671.                                                     `(throw ,return-tag 'nil)))))))
  672.                              `(,init-tag ,@body))))
  673.                     `(let* ((,go-tag (list nil)) (,return-tag (list nil))
  674.                                                  ,@(mapcar #'(lambda (f) `(,(car f) ,go-tag)) functions))
  675.                        (catch ,return-tag
  676.                               (labels ,functions
  677.                                 (let ((nxt-label #',(caar functions)))
  678.                                   (loop (setq nxt-label (catch ,go-tag (funcall nxt-label)))))))))))
  679.  
  680. (print "The emulation of tagbody/go by catch/throw is considerably less obvious than the emulation of block/return-from.
  681. This is because tagbody defines a number of different labels rather than a single block name, and because the parsing of the
  682. tagbody body is considerably more complicated. The various segments of the tagbody are emulated by a labels nest of mutually
  683. recursive functions, which are forced to all execute at the correct dynamic depth by means of a
  684. 'trampoline. If the implementation implements the 'tail recursion' optimization for functions
  685. which have no arguments and return no values, and if the simpler cases of go's are optimized away, then this emulation can be quite efficient."
  686.        )
  687.  
  688.  
  689. (cl-defmacro labels (fns &body forms)
  690.              "CIRCULAR ENVIRONMENTS OF 'LABELS EMULATED BY 'FLET AND 'SETQ: It is generally believed that the circular environments of labels cannot be
  691.    obtained by means of flet. This is incorrect, as the following emulation (reminiscent of Scheme) shows.
  692.    With a more sophisticated macro-expansion, this emulation can be optimized into production-quality code."
  693.              (let* ((fnames (mapcar #'car fns))
  694.                     (nfnames (mapcar #'(lambda (ignore) (gensym)) fnames))
  695.                     (nfbodies (mapcar #'(lambda (f) `#'(lambda ,@(cdr f))) fns)))
  696.                `(let ,(mapcar #'(lambda (nf) `(,nf #'(lambda () ()))) nfnames)
  697.                   (flet ,(mapcar #'(lambda (f nf) `(,f (&rest a) (apply ,nf a)))
  698.                            fnames nfnames)
  699.                     (flet ,fns
  700.                       (progn ,@(mapcar #'(lambda (f nf) `(setq ,nf #',f))
  701.                                  fnames nfnames))
  702.                       ,@forms)))))
  703.  
  704. ;;(* + - / /= < <= = > > >= ABS ACONS ACOS ADJOIN ALPHA-CHAR-P ALPHANUMERICP APPEND AREF ASH ASIN ASSOC ASSOC-IF ATAN ATOM
  705. ;; BOOLE BOOLEAN BOTH-CASE-P BQ-CONS BQ-VECTOR BUTLAST BYTE CAAR CADR CAR CCONCATENATE CDAR CDDR CDR CEILING CERROR CHAR CHAR-CODE CHAR-DOWNCASE CHAR-EQUAL CHAR-GREATERP CHAR-LESSP CHAR-NOT-EQUAL CHAR-NOT-GREATERP CHAR-NOT-LESSP CHAR-UPCASE CHAR/= CHAR< CHAR<= CHAR= CHAR> CHAR>= CHARACTERP CLRHASH
  706. ;; CMERGE CODE-CHAR CONS CONSP CONSTANTP CONSTRUCT-FILENAME COPY-ALIST COPY-LIST COPY-SEQ COPY-TREE COS COUNT COUNT-IF CREDUCE CURRENT-PROCESS DATE-RELATIVE-GUID-P DECODE-FLOAT DECODE-UNIVERSAL-TIME DELETE DELETE-DUPLICATES DELETE-IF DIGIT-CHAR DIGIT-CHAR-P DISASSEMBLE-INTEGER-TO-FIXNUMS DPB EIGHTH ELT ENCODE-UNIVERSAL-TIME ENDP EQ EQL EQUAL EQUALP EVENP EXIT EXP EXPT FALSE FIFTH FILL FIND FIND-IF FIND-PACKAGE FIND-SYMBOL FIRST FIXNUMP FLOAT FLOAT-DIGITS FLOAT-RADIX FLOAT-SIGN FLOATP FLOOR FORCE-OUTPUT FORMAT FOURTH FRESH-LINE FUNCTION-SPEC-P FUNCTIONP GC GC-DYNAMIC GC-EPHEMERAL GC-FULL GENSYM GENTEMP GET GET-DECODED-TIME GET-INTERNAL-REAL-TIME GET-INTERNAL-REAL-TIME GET-INTERNAL-RUN-TIME GET-UNIVERSAL-TIME GET-UNIVERSAL-TIME GETF GETHASH GETHASH-WITHOUT-VALUES GUID-P GUID-STRING-P GUID-TO-STRING GUID/= GUID< GUID<= GUID= GUID> GUID>= HASH-TABLE-COUNT HASH-TABLE-P HASH-TABLE-SIZE HASH-TABLE-TEST IDENTITY IGNORE INFINITY-P INT/ INTEGER-DECODE-FLOAT INTEGER-LENGTH INTEGERP INTERN INTERRUPT-PROCESS INTERSECTION ISQRT KEYWORDP KILL-PROCESS LAST LDB LDIFF LENGTH LISP-IMPLEMENTATION-TYPE LISP-IMPLEMENTATION-VERSION LIST LIST* LIST-ALL-PACKAGES LIST-LENGTH LISTP LISTP LOCK-IDLE-P LOCK-P LOG LOGAND LOGANDC1 LOGANDC2 LOGBITP LOGCOUNT LOGEQV LOGIOR LOGNAND LOGNOR LOGNOT LOGORC1 LOGORC2 LOGTEST LOGXOR LOWER-CASE-P MAKE-HASH-TABLE MAKE-LOCK MAKE-LOCK MAKE-STRING MAKUNBOUND MAX MEMBER MEMBER-IF MIN MINUSP MISMATCH MOD NBUTLAST NCONC NEW-GUID NINTERSECTION NINTH NOT-A-NUMBER-P NOTE-PERCENT-PROGRESS NOTIFY NRECONC NREVERSE NSET-DIFFERENCE NSET-EXCLUSIVE-OR NSTRING-CAPITALIZE NSTRING-DOWNCASE NSTRING-UPCASE NSUBLIS NSUBST NSUBST-IF NSUBSTITUTE NSUBSTITUTE-IF NTH NTHCDR NULL NUMBERP NUMBERP NUNION ODDP PAIRLIS PEEK-CHAR PLUSP POSITION POSITION-IF PRIN1 PRIN1-TO-STRING PRINC PRINC-TO-STRING PRINT PROCESS-ACTIVE-P PROCESS-BLOCK PROCESS-NAME PROCESS-STATE PROCESS-UNBLOCK PROCESS-WAIT PROCESS-WAIT-WITH-TIMEOUT PROCESS-WHOSTATE PROCESSP RANDOM RASSOC RASSOC-IF READ-FROM-STRING READ-FROM-STRING-IGNORING-ERRORS REM REMF REMHASH REMOVE REMOVE-DUPLICATES REMOVE-IF REPLACE REST REVAPPEND REVERSE REVERSE ROOM ROUND RPLACA RPLACD SCALE-FLOAT SEARCH SECOND SEED-RANDOM SEQUENCEP SET-AREF SET-CONSING-STATE SET-DIFFERENCE SET-NTH SEVENTH SHOW-PROCESSES SIN SIXTH QUIT SLEEP SORT SQRT STABLE-SORT STRING STRING-CAPITALIZE STRING-DOWNCASE STRING-EQUAL STRING-GREATERP STRING-LEFT-TRIM STRING-LESSP STRING-NOT-EQUAL STRING-NOT-GREATERP STRING-NOT-LESSP STRING-RIGHT-TRIM STRING-TO-GUID STRING-TRIM STRING-UPCASE STRING/= STRING< STRING<= STRING= STRING> STRING>= STRINGP SUBLIS SUBLISP::PROPERTY-LIST-MEMBER SUBSEQ SUBSETP SUBST SUBST-IF SUBSTITUTE SUBSTITUTE-IF SXHASH SYMBOL-FUNCTION SYMBOL-NAME SYMBOLP SYMBOLP TAILP TAN TENTH TERPRI THIRD TREE-EQUAL TRUE TRUNCATE TYPE-OF UNINTERN UNION UPPER-CASE-P VALID-PROCESS-P VALUES VECTOR VECTORP WARN WRITE-IMAGE Y-OR-N-P YES-OR-NO-P ZEROP)
  707.  
  708.  
  709.  
  710. (DEFMACRO HANDLER-CASE-CAD (FORM &REST CASES)
  711.   (ret (LET ((NO-ERROR-CLAUSE (ASSOC ':NO-ERROR CASES)))
  712.          (IF NO-ERROR-CLAUSE
  713.              (LET ((NORMAL-RETURN (MAKE-SYMBOL "NORMAL-RETURN"))
  714.                    (ERROR-RETURN  (MAKE-SYMBOL "ERROR-RETURN")))
  715.                `(BLOCK ,ERROR-RETURN
  716.                   (MULTIPLE-VALUE-CALL #'(LAMBDA ,@(CDR NO-ERROR-CLAUSE))
  717.                     (BLOCK ,NORMAL-RETURN
  718.                       (RETURN-FROM ,ERROR-RETURN
  719.                         (HANDLER-CASE (RETURN-FROM ,NORMAL-RETURN ,FORM)
  720.                           ,@(REMOVE NO-ERROR-CLAUSE CASES)))))))
  721.            (LET ((TAG (GENSYM))
  722.                  (VAR (GENSYM))
  723.                  (ANNOTATED-CASES (MAPCAR #'(LAMBDA (CASE) (CONS (GENSYM) CASE))
  724.                                     CASES)))
  725.              `(BLOCK ,TAG
  726.                 (LET ((,VAR NIL))
  727.                   ,VAR              ;ignorable
  728.                   (TAGBODY
  729.                     (HANDLER-BIND ,(MAPCAR #'(LAMBDA (ANNOTATED-CASE)
  730.                                                (LIST (CADR ANNOTATED-CASE)
  731.                                                      `#'(LAMBDA (TEMP)
  732.                                                           ,@(IF (CADDR ANNOTATED-CASE)
  733.                                                                 `((SETQ ,VAR TEMP)))
  734.                                                           (GO ,(CAR ANNOTATED-CASE)))))
  735.                                      ANNOTATED-CASES)
  736.                       (RETURN-FROM ,TAG ,FORM))
  737.                     ,@(MAPCAN #'(LAMBDA (ANNOTATED-CASE)
  738.                                   (LIST (CAR ANNOTATED-CASE)
  739.                                         (LET ((BODY (CDDDR ANNOTATED-CASE)))
  740.                                           `(RETURN-FROM ,TAG
  741.                                              ,(COND ((CADDR ANNOTATED-CASE)
  742.                                                      `(LET ((,(CAADDR ANNOTATED-CASE)
  743.                                                                ,VAR))
  744.                                                         ,@BODY))
  745.                                                     ((NOT (CDR BODY))
  746.                                                      (CAR BODY))
  747.                                                     (T
  748.                                                      `(PROGN ,@BODY)))))))
  749.                         ANNOTATED-CASES)))))))))
  750. |#
  751.  
  752.  
  753. (load "cycdcg.lisp")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement