home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0963.lha / SIOD / scm / siod.scm < prev    next >
Text File  |  1993-07-09  |  14KB  |  445 lines

  1. ; Scheme In One Define.
  2. ; The garbage collector, the name and other parts of this program are
  3. ;
  4. ; *                     COPYRIGHT (c) 1989 BY                              *
  5. ; *      PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  6. ;
  7. ; Conversion  to  full scheme standard, characters, vectors, ports, complex &
  8. ; rational numbers, debug utils, and other major enhancments by
  9. ;
  10. ; *      Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY        * 
  11. ;
  12. ; Permission  to use, copy, modify, distribute and sell this software and its
  13. ; documentation  for  any purpose and without fee is hereby granted, provided
  14. ; that  the  above  copyright  notice appear in all copies and that both that
  15. ; copyright   notice   and   this  permission  notice  appear  in  supporting
  16. ; documentation,  and that the name of Paradigm Associates Inc not be used in
  17. ; advertising or publicity pertaining to distribution of the software without
  18. ; specific, written prior permission.
  19. ;
  20. ; Optional runtime library for version 2.0
  21.  
  22.  
  23. (define (caar x) (cxr x "aa"))
  24. (define (cadr x) (cxr x "da"))
  25. (define (cdar x) (cxr x "ad"))
  26. (define (cddr x) (cxr x "dd"))
  27.  
  28. (define (caaar x) (cxr x "aaa"))
  29. (define (caadr x) (cxr x "daa"))
  30. (define (cadar x) (cxr x "ada"))
  31. (define (caddr x) (cxr x "dda"))
  32.  
  33. (define (cdaar x) (cxr x "aad"))
  34. (define (cdadr x) (cxr x "dad"))
  35. (define (cddar x) (cxr x "add"))
  36. (define (cdddr x) (cxr x "ddd"))
  37.  
  38. (define (caaaar x) (cxr x "aaaa"))
  39. (define (caaadr x) (cxr x "daaa"))
  40. (define (caadar x) (cxr x "adaa"))
  41. (define (caaddr x) (cxr x "ddaa"))
  42.  
  43. (define (cadaar x) (cxr x "aada"))
  44. (define (cadadr x) (cxr x "dada"))
  45. (define (caddar x) (cxr x "adda"))
  46. (define (cadddr x) (cxr x "ddda"))
  47.  
  48. (define (cdaaar x) (cxr x "aaad"))
  49. (define (cdaadr x) (cxr x "daad"))
  50. (define (cdadar x) (cxr x "adad"))
  51. (define (cdaddr x) (cxr x "ddad"))
  52.  
  53. (define (cddaar x) (cxr x "aadd"))
  54. (define (cddadr x) (cxr x "dadd"))
  55. (define (cdddar x) (cxr x "addd"))
  56. (define (cddddr x) (cxr x "dddd"))
  57.  
  58. (macro freeze (lambda (x)
  59.                       (cons 'lambda 
  60.                             (cons nil (cdr x)))))
  61.  
  62. (define (thaw x) (x))
  63.  
  64. (macro delay (lambda (x)
  65.                  (list 'cons ''delayed-object
  66.                        (list 'lambda 
  67.                                    '() (cadr x)))))
  68.  
  69. (define (force x) 
  70.         (if (eq? (car x) 'memoized-object)
  71.             (cdr x) 
  72.             (sequence (set-cdr! x ((cdr x)))
  73.                       (set-car! x 'memoized-object)
  74.                       (cdr x))))
  75.  
  76. (define (delayed-object? x)
  77.         (if (pair? x)
  78.             (or (eq? (car x) 'delayed-object)(eq? (car x) 'memoized-object))
  79.             #f))
  80.  
  81. (macro cons-stream 
  82.        (lambda (x) 
  83.                (list 'cons
  84.                      (cadr x)
  85.                      (list 'delay (caddr x)))))
  86.  
  87. (define (head x) (car x))
  88.  
  89. (define (tail x) (force (cdr x)))
  90.  
  91. (define the-empty-stream 
  92.         ((named-lambda (empty-stream) 
  93.                        (cons-stream 'empty-stream (empty-stream)))))
  94.  
  95. (define (empty-stream? x) (eq? (head x) 'empty-stream))
  96.  
  97. (define (stream? x)
  98.         (and (pair? x) (delayed-object? (cdr x))))
  99.  
  100. (define (stream->list z)
  101.         (define (str->ls x y)
  102.                 (if (empty-stream? x)
  103.                     y
  104.                     (str->ls (tail x) (cons (head x) y))))
  105.          (str->ls z nil))  
  106.  
  107. (define (list->stream z)
  108.         (if (null? z)
  109.             the-empty-stream
  110.             (cons-stream (car z) (list->stream (cdr z)))))
  111.  
  112. (define (integer->string x) (number->string x '(int)))
  113.  
  114. (macro make-environment (lambda (x)
  115.                         (append (list 'let '())
  116.                                 (cdr x)
  117.                                 (list (list 'the-environment)))))
  118.  
  119. (macro alias (lambda (x) 
  120.                      (list 'define
  121.                            (cadr x) 
  122.                            (caddr x))))
  123.  
  124. (macro rec (lambda (x) 
  125.            (list 'letrec 
  126.                  (list (list (cadr x) 
  127.                              (caddr x)))
  128.                  (cadr x))))
  129.  
  130. (define (file-length x)
  131.         (let ((a (open-input-file x))
  132.               (b nil))
  133.              (set-file-position! a 0 2)
  134.              (set! b (get-file-position a))
  135.              (close-port a)
  136.              b))
  137.  
  138. (define (open-binary-input-file x) (open-port x "rb" 1))
  139.  
  140. (define (open-binary-output-port x) (open-port x "wb" 1))
  141.  
  142. (define (open-input-file x) (open-port x "r" 1))
  143.  
  144. (define (open-output-file x) (open-port x "w" 1))
  145.  
  146. (define (open-extend-file x) (open-port x "a" 1))
  147.  
  148. (define (current-input-port) (fluid input-port))
  149.  
  150. (define close-output-port close-port)
  151.  
  152. (define close-input-port close-port)
  153.  
  154. (define (current-output-port) (fluid output-port))
  155.  
  156. (define (flush-input x) (begin (read-line x) '()))
  157.  
  158. (define (newline . x) (display #\newline (car x)))
  159.  
  160. (define (page . x) (display #\page (car x)))
  161.  
  162. (define (call-with-input-file x y)
  163.         (let* ((in (open-input-file x))
  164.                (res (y in)))
  165.         (close-port in)
  166.         res))
  167.  
  168. (define (call-with-output-file x y)
  169.         (let* ((out (open-output-file x))
  170.                (res (y out)))
  171.         (close-port out)
  172.         res))
  173.  
  174. (define (with-input-from-file x y)
  175.         (let ((old-input (fluid input-port))
  176.               (res nil))
  177.              (set! (fluid input-port) (open-input-file x))
  178.              (set! res (y))
  179.              (close-port (fluid input-port))
  180.              (set! (fluid input-port) old-input)
  181.              res))
  182.  
  183. (define (with-output-to-file x y)
  184.         (let ((old-output (fluid output-port))
  185.               (res nil))
  186.              (set! (fluid output-port) (open-output-file x))
  187.              (set! res (y))
  188.              (close-port (fluid output-port))
  189.              (set! (fluid output-port) old-output)
  190.              res))
  191.  
  192. (define #\backspace (integer->char 8))
  193.  
  194. (define #\escape (integer->char 27))
  195.  
  196. (define #\newline (integer->char 10))
  197.  
  198. (define #\page (integer->char 12))
  199.  
  200. (define #\return (integer->char 13))
  201.  
  202. (define #\rubout (integer->char 63))
  203.  
  204. (define #\space (integer->char 32))
  205.  
  206. (define #\tab (integer->char 9))
  207.  
  208. (define (string<? x y)
  209.         (< (string-cmp x y) 0))        
  210.  
  211. (define (string>? x y)
  212.         (> (string-cmp x y) 0))
  213.  
  214. (define (string=? x y)
  215.         (= (string-cmp x y) 0))
  216.  
  217. (define (string<=? x y)
  218.         (<= (string-cmp x y) 0))
  219.  
  220. (define (string>=? x y)
  221.         (>= (string-cmp x y) 0))
  222.  
  223. (define (string-CI<? x y)
  224.         (< (string-cmp-CI x y) 0))        
  225.  
  226. (define (string-CI=? x y)
  227.         (= (string-cmp-CI x y) 0))
  228.  
  229. (define (substring-CI<? x y z a b c)
  230.         (string-ci<? (substring x y z) (substring a b c)))
  231.  
  232. (define (substring-CI=? x y z a b c)
  233.         (string-ci=? (substring x y z) (substring a b c)))
  234.  
  235. (define (substring<? x y z a b c)
  236.         (string<? (substring x y z) (substring a b c)))
  237.  
  238. (define (substring=? x y z a b c)
  239.         (string=? (substring x y z) (substring a b c)))
  240.  
  241. (define (string-null? x)
  242.         (= (string-cmp x "") 0))
  243.  
  244. (define (substring-fill! x y z a)
  245.         (while (< y z)
  246.                (string-set! x y a)
  247.                (set! y (1+ y)))
  248.         x)
  249.  
  250. (define (substring-move-left! x y z a b)
  251.         (while (< y z)
  252.                (string-set! x b (string-ref a y))
  253.                (set! b (1+ b))
  254.                (set! y (1+ y)))
  255.         x)
  256.  
  257. (define (substring-move-right! x y z a b)
  258.         (while (<= y z)
  259.                (set! z (-1+ z))
  260.                (string-set! x b (string-ref a z))
  261.                (set! b (1+ b)))
  262.         x)
  263.  
  264. (define (symbol->ASCII x) 
  265.         (char->integer (string-ref (symbol->string x) 0)))
  266.  
  267. (define (ASCII->symbol x)
  268.         (string->symbol (make-string 1 (integer->char x))))
  269.  
  270. (define (implode x)
  271.         (define y "")
  272.         (while (not (atom? x)) 
  273.                (cond ((string? (car x)) 
  274.                       (set! y (string-append y 
  275.                                      (make-string 1 (string-ref (car x) 0)))))
  276.                      ((symbol? (car x))
  277.                       (set! y (string-append y 
  278.                                      (make-string 1 (integer->char (symbol->ASCII (car x)))))))
  279.                      ((integer? (car x))
  280.                       (set! y (string-append y 
  281.                                      (make-string 1 (integer->char (car x))))))
  282.                      (else (error "arg to implode must be a symbol or a string or an integer" (car x))))
  283.                 (set! x (cdr x)))
  284.         (string->symbol y))
  285.                   
  286. (define (explode x)
  287.         (cond ((symbol? x) (set! x (symbol->string x)))
  288.               ((integer? x) (set! x (integer->string x)))
  289.               ((string? x))
  290.               (else (error "arg to explode must be a symbol or a string or an integer" x)))
  291.         (do ((i 0 (1+ i))
  292.              (res nil))
  293.             ((= i (string-length x)) (reverse! res))
  294.             (set! res 
  295.                   (cons (string->symbol (make-string 1 (string-ref x i)))
  296.                         res))))
  297.  
  298. (define (char<? x y)
  299.         (< (char-cmp x y) 0))   
  300.      
  301. (define (char>? x y)
  302.         (> (char-cmp x y) 0))
  303.                    
  304. (define (char=? x y)
  305.         (= (char-cmp x y) 0))
  306.  
  307. (define (char<=? x y)
  308.         (<= (char-cmp x y) 0))
  309.  
  310. (define (char>=? x y)
  311.         (>= (char-cmp x y) 0))
  312.  
  313. (define (char-ci<? x y)
  314.         (< (char-cmp (char-downcase x) (char-downcase y)) 0))   
  315.      
  316. (define (char-ci>? x y)
  317.         (> (char-cmp (char-downcase x) (char-downcase y)) 0))   
  318.      
  319. (define (char-ci=? x y)
  320.         (= (char-cmp (char-downcase x) (char-downcase y)) 0))   
  321.      
  322. (define (char-ci<=? x y)
  323.         (<= (char-cmp (char-downcase x) (char-downcase y)) 0))   
  324.      
  325. (define (char-ci>=? x y)
  326.         (>= (char-cmp (char-downcase x) (char-downcase y)) 0))   
  327.      
  328. (define (char-upper-case? x)
  329.         (and (char>=? x #\A) (char<=? x #\Z)))
  330.  
  331. (define (char-lower-case? x)
  332.         (and (char>=? x #\a) (char<=? x #\z)))
  333.  
  334. (define (char-digit? x)
  335.         (and (char>=? x #\0) (char<=? x #\9)))
  336.  
  337. (define (boolean? x) (or (eq? x #t) (eq? x #f)))
  338.  
  339. (define (edit)
  340.         (begin (dos-call "c:ed siod.tmp")
  341.                (load "siod.tmp")))
  342.  
  343. (define (ced)
  344.         (dos-call "ced"))
  345.  
  346. (define (call-with-current-continuation fcn)
  347.   (let ((tag (cons nil nil)))
  348.     (*catch tag
  349.        (fcn (lambda (value)
  350.          (*throw tag value))))))
  351.  
  352. (define call/cc call-with-current-continuation)
  353.  
  354. (define (sort! x . y)
  355.         (define test <=)
  356.         (define (interchange x i j)
  357.                 (define tmp (vector-ref x i))
  358.                 (vector-set! x i (vector-ref x j))
  359.                 (vector-set! x j tmp))
  360.         (define (qsort x m n)
  361.                 (if (< m n)
  362.                     (do ((i m) (j (1+ n))
  363.                          (k (begin (interchange x m (quotient (+ m n) 2))  
  364.                                    (vector-ref x m))))
  365.                         ((>= i j) (interchange x m j)
  366.                                   (qsort x m (-1+ j))
  367.                                   (qsort x (1+ j) n) x)
  368.                         (set! i (1+ i))
  369.                         (do () ((or (test k (vector-ref x i)) (>= i n)))
  370.                                (set! i (1+ i)))
  371.                         (set! j (-1+ j))
  372.                         (do () ((or (test (vector-ref x j) k) (<= j m)))
  373.                                (set! j (-1+ j)))
  374.                         (if (< i j) (interchange x i j)))))
  375.         (define (m-s x y)
  376.                 (define res (list 'dummy))
  377.                 (do ((ptr res (cdr ptr))
  378.                      (done #f))
  379.                     (done (cdr res))
  380.                     (cond ((null? x) (set-cdr! ptr y) (set! done #t))
  381.                           ((null? y) (set-cdr! ptr x) (set! done #t))
  382.                           ((test (car x) (car y))
  383.                            (set-cdr! ptr x) (set! x (cdr x)))
  384.                           (else (set-cdr! ptr y) (set! y (cdr y))))))
  385.         (define (mer-so x)
  386.                 (if (or (null? x) (null? (cdr x))) 
  387.                     x
  388.                     (m-s x (mer-so (do ((ptr (cdr x) (cdr ptr))
  389.                                         (y (cddr x) (cdr y)))
  390.                                        ((or (null? y) (test (car y) (car ptr))) 
  391.                                         (set-cdr! ptr nil) y))))))
  392.         (if (pair? y)
  393.             (if (proc? (car y))
  394.                 (set! test (car y))
  395.                 (error "second arg to sort! must be a procedure" (car y))))
  396.         (cond ((vector? x) (qsort x 0 (-1+ (vector-length x))) x)
  397.               ((pair? x) (mer-so x))
  398.               (else (error "first arg to sort! must be a vector or a list" x))))
  399.  
  400. (define (break proc . nome)
  401.         (let ((code (procedure-code proc))
  402.               (text (if (string? (car nome))
  403.                         (string-append "break-point entered in " (car nome))
  404.                         "breakpoint entered")))
  405.              (set-cdr! code (list 'begin 
  406.                                   (list 'bkpt text)
  407.                                   (cdr code)))
  408.              (set-procedure-code! proc code)))
  409.  
  410. (define (unbreak proc)
  411.         (let ((code (procedure-code proc)))
  412.              (if (eq? (caaddr code) 'bkpt)
  413.                  (set-cdr! code (cadddr code))
  414.                  (error "procedure is not breaked"))
  415.              (set-procedure-code! proc code)))
  416.  
  417. (define (*tracer* nome env)
  418.         (display (string-append "entering procedure " 
  419.                                 nome
  420.                                 " with parameters:"))
  421.         (do ((ar (environment-bindings env) (cdr ar))) 
  422.             ((null? ar)) 
  423.             (print (cdar ar))
  424.             (newline)))
  425.  
  426. (define (trace proc nome)
  427.         (let ((code (procedure-code proc)))
  428.              (set-cdr! code 
  429.                        (list 'begin 
  430.                              (list '*tracer* (if (string? (car nome)) 
  431.                                                  (car nome) 
  432.                                                  "")   
  433.                                              (list 'the-environment))
  434.                              (cdr code)))
  435.              (set-procedure-code! proc code)))
  436.  
  437. (define (untrace proc)
  438.         (let ((code (procedure-code proc)))
  439.              (if (eq? (caaddr code) '*tracer*)
  440.                  (set-cdr! code (cadddr code))
  441.                  (error "procedure is not traced"))
  442.              (set-procedure-code! proc code)))
  443.  
  444.