#| -*-Scheme-*-

Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
    2017, 2018, 2019, 2020, 2021, 2022 Massachusetts Institute of
    Technology

This file is part of MIT/GNU Scheme.

MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.

|#

;;;; Script to incrementally syntax the compiler

(load-option 'cref)
(load-option 'sf)

;; Guarantee that the compiler's package structure exists.
(if (not (name->package '(compiler)))
    (let ((package-set
	   (merge-pathnames
	    (enough-pathname
	     (merge-pathnames (package-set-pathname "compiler"))
	     cref/source-root)
	    cref/object-root)))
      (if (not (file-exists? package-set))
	  (cref/generate-trivial-constructor "compiler"))
      (construct-packages-from-file (fasload package-set))))

;; Guarantee that the necessary syntactic transforms and optimizers
;; are loaded.
(if (lexical-unreferenceable? (->environment '(compiler)) 'syntax-files!)
    (let ((sf-and-load
	   (lambda (files package)
	     (fluid-let ((sf/default-syntax-table (->environment package)))
	       (sf-conditionally files))
	     (for-each (lambda (file)
			 (receive (scm bin spec)
				  (sf/pathname-defaulting file #f #f)
			   scm spec
			   (load bin package)))
		       files))))
      (load-option 'hash-table)
      (fresh-line)
      (newline)
      (write-string "---- Loading compile-time files ----")
      (newline)
      (sf-and-load '("base/switch") '(compiler))
      (sf-and-load '("base/macros") '(compiler macros))
      (sf-and-load '("machines/C/decls") '(compiler declarations))
      (let ((environment (->environment '(compiler declarations))))
	(set! (access source-file-expression environment) "*.scm")
	((access initialize-package! environment)))
      (sf-and-load '("base/pmlook") '(compiler pattern-matcher/lookup))
      (sf-and-load '("base/pmpars") '(compiler pattern-matcher/parser))
      (sf-and-load '("machines/C/machin") '(compiler))
      (sf-and-load '("back/syntax") '(compiler lap-syntaxer))
      (sf-and-load '("base/scode") '(compiler))
      (sf-and-load '("base/pmerly") '(compiler pattern-matcher/early))
      (sf-and-load '("back/syerly") '(compiler lap-syntaxer))))

;; Resyntax any files that need it.
((access syntax-files! (->environment '(compiler))))

;; Rebuild the package constructors and cref.
(cref/generate-constructors "compiler")