Skip to content

Commit

Permalink
Macros are built only if they are missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
CuppoJava committed Dec 11, 2022
2 parents 7912cd7 + 15df15d commit 4f5c7df
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions compiler/compiler-main.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ public defn compile (proj-manager:ProjManager,
backend:Backend,
optimize?:True|False,
verbose?:True|False,
macro-plugins:Tuple<String>) -> CompilationResult :
macro-plugins:Tuple<String>,
force-build-macros?:True|False) -> CompilationResult :
defn driver () :
val denv = DEnv()
val macroexpander = StanzaMacroexpander(proj-manager, macro-plugins)
val macroexpander = StanzaMacroexpander(force-build-macros?, proj-manager, macro-plugins)
val result = compile-to-el $ new FrontEndInputs :
defmethod inputs (this) : to-tuple(inputs)
defmethod find-package (this, name:Symbol) : find-package(proj-manager, name)
Expand Down
5 changes: 3 additions & 2 deletions compiler/compiler.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public defn dependencies (settings:BuildSettings, ignore-cache?:True|False) -> D
val params = ProjParams(compiler-flags(), optimize?(settings))
val auxfile = AuxFile() when not ignore-cache?
val proj-manager = ProjManager(proj, params, auxfile)
val macroexpander = StanzaMacroexpander(proj-manager, macro-plugins(settings))
val macroexpander = StanzaMacroexpander(false, proj-manager, macro-plugins(settings))
dependencies $ new FrontEndInputs :
defmethod inputs (this) : build-inputs!(settings)
defmethod find-package (this, name:Symbol) : find-package(proj-manager, name)
Expand Down Expand Up @@ -261,7 +261,8 @@ public defn compile (settings:BuildSettings, system:System, verbose?:True|False)
setup-system-flags(settings*)
val proj-manager = ProjManager(proj, ProjParams(compiler-flags(), optimize?(settings*)), auxfile)
val comp-result = compile(proj-manager, build-inputs!(settings*), vm-packages(settings*), asm?(settings*), pkg-dir(settings*),
backend(platform(settings*) as Symbol), optimize?(settings*), verbose?, macro-plugins(settings*))
backend(platform(settings*) as Symbol), optimize?(settings*), verbose?, macro-plugins(settings*),
inputs(settings) is BuildTarget)
save(auxfile)
within delete-temporary-file-on-finish(settings*) :
link-output-file(projenv, settings*, comp-result, proj, auxfile)
Expand Down
22 changes: 15 additions & 7 deletions compiler/macroexpander.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,35 @@ public defmulti compile-build-target (s:MacroSystem, target:Symbol) -> False

;Create and immediately set a proj manager and load macros.
public defn StanzaMacroexpander (sys:MacroSystem,
force-build-macros?:True|False,
proj:ProjManager|False,
macros:Tuple<String>) -> StanzaMacroexpander :
val expander = StanzaMacroexpander(sys)
val expander = StanzaMacroexpander(sys, force-build-macros?)
match(proj:ProjManager) :
set-proj-manager(expander, proj)
for file in macros do :
load-macro-plugin(expander, file)
expander

;Create a StanzaMacroexpander using the default MacroBuilder.
public defn StanzaMacroexpander (proj:ProjManager|False,
public defn StanzaMacroexpander (force-build-macros?:True|False,
proj:ProjManager|False,
macros:Tuple<String>) -> StanzaMacroexpander :
StanzaMacroexpander(stz/macrobuilder/MacroBuilder()
StanzaMacroexpander(stz/macrobuilder/MacroBuilder(),
force-build-macros?
proj,
macros)

;============================================================
;======================= Implementation =====================
;============================================================

public defn StanzaMacroexpander (sys:MacroSystem) -> StanzaMacroexpander :
;- force-build-macros?: By default, we attempt to build macro-plugins
; only if they are missing. If force-build-macros? is true, then
; we *always* call `stanza build` on the macros even if it already
; exists.
public defn StanzaMacroexpander (sys:MacroSystem,
force-build-macros?:True|False) -> StanzaMacroexpander :
;Holds the macros that have been loaded.
;Maintain their order such that more relevant ones appear
;first.
Expand All @@ -77,7 +85,7 @@ public defn StanzaMacroexpander (sys:MacroSystem) -> StanzaMacroexpander :
defn load-macro-from-file (source:MacroSource,
filename:String,
expected-packages:Maybe<Tuple<Symbol>>) -> LoadedMacros :
build-macro(filename, source == LoadedFromProj)
build-macro(filename)
val plugin = load-macro-plugin(filename, expected-packages)
val loaded = LoadedMacros(source, plugin)
add(loaded-macros, loaded)
Expand All @@ -87,8 +95,8 @@ public defn StanzaMacroexpander (sys:MacroSystem) -> StanzaMacroexpander :
;If the file doesn't exist, then we always try to build it.
;Otherwise if the file exists, then we try to build it only if
;build-if-exists? is true.
defn build-macro (filename:String, build-if-exists?:True|False) :
val build? = if file-exists?(filename) : build-if-exists?
defn build-macro (filename:String) :
val build? = if file-exists?(filename) : force-build-macros?
else : true
if build? :
match(proj-manager:ProjManager) :
Expand Down
2 changes: 1 addition & 1 deletion compiler/repl.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public defn REPL (macro-plugins:Tuple<String>) :
val file-env = FileEnv()
val proj-files = Vector<String>()
val syntaxes = to-hashset<Symbol>(REPL-INITIAL-SYNTAX-PACKAGES)
val macroexpander = StanzaMacroexpander(false, macro-plugins)
val macroexpander = StanzaMacroexpander(false, false, macro-plugins)

;============================================================
;=============== Ensure Syntax Packages Exist ===============
Expand Down

0 comments on commit 4f5c7df

Please sign in to comment.