home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / cref / conpkg.scm < prev    next >
Text File  |  2000-01-18  |  5KB  |  151 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: conpkg.scm,v 1.7 2000/01/18 20:43:28 cph Exp $
  4.  
  5. Copyright (c) 1988-2000 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; Generate construction program from package model
  23.  
  24. (declare (usual-integrations)
  25.      (integrate-external "object"))
  26.  
  27. ;;; Construct expressions to construct the package structure.
  28.  
  29. (define (construct-constructor pmodel)
  30.   (let ((packages (pmodel/packages pmodel)))
  31.     ;; SYSTEM-GLOBAL-ENVIRONMENT is here so that it is not integrated.
  32.     ;; This is necessary for cross-syntaxing when the representation of
  33.     ;; #F, () or the system-global-environment changes.
  34.     `((DECLARE (USUAL-INTEGRATIONS SYSTEM-GLOBAL-ENVIRONMENT))
  35.       ,@(append-map*
  36.      (let ((links
  37.         (append-map*
  38.          (append-map construct-links (pmodel/extra-packages pmodel))
  39.          construct-links packages)))
  40.        (if (pair? links)
  41.            `((LET ((ENVIRONMENT-LINK-NAME
  42.             (LET-SYNTAX
  43.                 ((UCODE-PRIMITIVE
  44.                   (MACRO (NAME) (MAKE-PRIMITIVE-PROCEDURE NAME))))
  45.               (UCODE-PRIMITIVE ENVIRONMENT-LINK-NAME))))
  46.            ,@links))
  47.            '()))
  48.      construct-definitions
  49.      (sort packages package-structure<?)))))
  50.  
  51. (define (construct-definitions package)
  52.   (cond ((package/root? package)
  53.      `((IN-PACKAGE SYSTEM-GLOBAL-ENVIRONMENT
  54.          ,@(map (lambda (binding) `(DEFINE ,(binding/name binding)))
  55.             (package/source-bindings package)))))
  56.     ((equal? (package/name package) '(PACKAGE))
  57.      ;; This environment is hand built by the cold-load.
  58.      '())
  59.     (else
  60.      (package-definition
  61.       (package/name package)
  62.       `(IN-PACKAGE ,(package-reference (package/parent package))
  63.          (LET (,@(map (lambda (binding) `(,(binding/name binding)))
  64.               (package/source-bindings package)))
  65.            (THE-ENVIRONMENT)))))))
  66.  
  67. (define (construct-links package)
  68.   (if (equal? (package/name package) '(PACKAGE))
  69.       '()
  70.       (append-map
  71.        (lambda (binding)
  72.      (map (lambda (link)
  73.         (let ((source (link/source link))
  74.               (destination (link/destination link)))
  75.           `(ENVIRONMENT-LINK-NAME
  76.             ,(package-reference (binding/package destination))
  77.             ,(package-reference (binding/package source))
  78.             ',(binding/name source))))
  79.           (binding/links binding)))
  80.        (package/sorted-bindings package))))
  81.  
  82. (define (package/source-bindings package)
  83.   (list-transform-positive (package/sorted-bindings package)
  84.     (lambda (binding)
  85.       (eq? (binding/source-binding binding) binding))))
  86.  
  87. (define (package-structure<? x y)
  88.   (cond ((package/topological<? x y) true)
  89.     ((package/topological<? y x) false)
  90.     (else (package<? x y))))
  91.  
  92. (define (package/topological<? x y)
  93.   (and (not (eq? x y))
  94.        (let loop ((y (package/parent y)))
  95.      (and y
  96.           (if (eq? x y)
  97.           true
  98.           (loop (package/parent y)))))))
  99.  
  100. ;;; Construct a procedure which will load the files into the package
  101. ;;; structure.
  102.  
  103. (define (construct-loader pmodel)
  104.   `((DECLARE (USUAL-INTEGRATIONS))
  105.     (LAMBDA (LOAD KEY-ALIST)
  106.       (LET ((LOOKUP-KEY
  107.          (LAMBDA (KEY)
  108.            (LET LOOP ((ALIST KEY-ALIST))
  109.          (IF (NULL? ALIST)
  110.              (ERROR "Missing key" KEY))
  111.          (IF (EQ? KEY (CAR (CAR ALIST)))
  112.              (CDR (CAR ALIST))
  113.              (LOOP (CDR ALIST)))))))
  114.     LOOKUP-KEY            ;ignore if not referenced
  115.     ,@(append-map (lambda (package)
  116.             (let ((reference (package-reference package)))
  117.               (if (> (package/n-files package) 1)
  118.                   `((LET ((ENVIRONMENT ,reference))
  119.                   ,@(load-package package 'ENVIRONMENT)))
  120.                   (load-package package reference))))
  121.               (pmodel/packages pmodel))))))
  122.  
  123. (define (load-package package environment)
  124.   (append-map (lambda (file-case)
  125.         (let ((type (file-case/type file-case)))
  126.           (if type
  127.               `((CASE (LOOKUP-KEY ',type)
  128.               ,@(map (lambda (clause)
  129.                    `(,(file-case-clause/keys clause)
  130.                      ,@(clause-loader clause environment)))
  131.                  (file-case/clauses file-case))))
  132.               (clause-loader (car (file-case/clauses file-case))
  133.                      environment))))
  134.           (package/file-cases package)))
  135.  
  136. (define (clause-loader clause environment)
  137.   (let ((files (file-case-clause/files clause)))
  138.     (if (null? files)
  139.     `(FALSE)
  140.     (map (lambda (file)
  141.            `(LOAD ,(->namestring file) ,environment))
  142.          files))))
  143.  
  144. (define (package-definition name value)
  145.   (let ((path (reverse name)))
  146.     `((PACKAGE/ADD-CHILD! (FIND-PACKAGE ',(reverse (cdr path)))
  147.               ',(car path)
  148.               ,value))))
  149.  
  150. (define (package-reference package)
  151.   `(PACKAGE/ENVIRONMENT (FIND-PACKAGE ',(package/name package))))