From d6c28549555e4cd9084785dd9c8254ca9360ed9e Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 30 Dec 2019 01:43:41 -0400 Subject: Re-organized macro-expansion machinery. --- stdlib/source/lux/tool/compiler/analysis/macro.lux | 54 ++++++++++++++++++++++ .../lux/tool/compiler/default/evaluation.lux | 10 ++-- stdlib/source/lux/tool/compiler/default/init.lux | 4 +- .../source/lux/tool/compiler/default/platform.lux | 4 +- stdlib/source/lux/tool/compiler/phase/analysis.lux | 6 +-- .../source/lux/tool/compiler/phase/directive.lux | 6 +-- .../compiler/phase/extension/directive/lux.lux | 4 +- stdlib/source/lux/tool/compiler/phase/macro.lux | 53 --------------------- stdlib/source/program/compositor.lux | 4 +- 9 files changed, 73 insertions(+), 72 deletions(-) create mode 100644 stdlib/source/lux/tool/compiler/analysis/macro.lux delete mode 100644 stdlib/source/lux/tool/compiler/phase/macro.lux (limited to 'stdlib') diff --git a/stdlib/source/lux/tool/compiler/analysis/macro.lux b/stdlib/source/lux/tool/compiler/analysis/macro.lux new file mode 100644 index 000000000..9e191e514 --- /dev/null +++ b/stdlib/source/lux/tool/compiler/analysis/macro.lux @@ -0,0 +1,54 @@ +(.module: + [lux #* + [abstract + [monad (#+ do)]] + [control + ["." try (#+ Try)] + ["." exception (#+ exception:)]] + [data + ["." text + ["%" format (#+ format)]] + [collection + [array (#+ Array)] + ["." list ("#@." functor)]]] + ["." macro]] + [/// + ["." phase]]) + +(exception: #export (expansion-failed {macro Name} {inputs (List Code)} {error Text}) + (exception.report + ["Macro" (%.name macro)] + ["Inputs" (exception.enumerate %.code inputs)] + ["Error" error])) + +(exception: #export (must-have-single-expansion {macro Name} {inputs (List Code)} {outputs (List Code)}) + (exception.report + ["Macro" (%.name macro)] + ["Inputs" (exception.enumerate %.code inputs)] + ["Outputs" (exception.enumerate %.code outputs)])) + +(type: #export Expander + (-> Macro (List Code) Lux (Try (Try [Lux (List Code)])))) + +(def: #export (expand expander name macro inputs) + (-> Expander Name Macro (List Code) (Meta (List Code))) + (function (_ state) + (do try.monad + [output (expander macro inputs state)] + (case output + (#try.Success output) + (#try.Success output) + + (#try.Failure error) + ((phase.throw ..expansion-failed [name inputs error]) state))))) + +(def: #export (expand-one expander name macro inputs) + (-> Expander Name Macro (List Code) (Meta Code)) + (do macro.monad + [expansion (expand expander name macro inputs)] + (case expansion + (^ (list single)) + (wrap single) + + _ + (phase.throw ..must-have-single-expansion [name inputs expansion])))) diff --git a/stdlib/source/lux/tool/compiler/default/evaluation.lux b/stdlib/source/lux/tool/compiler/default/evaluation.lux index b6bc6b46b..2d7f32d85 100644 --- a/stdlib/source/lux/tool/compiler/default/evaluation.lux +++ b/stdlib/source/lux/tool/compiler/default/evaluation.lux @@ -9,24 +9,24 @@ ["%" format (#+ format)]]]] [/// ["." phase - [macro (#+ Expander)] [".P" analysis ["." type]] [".P" synthesis] ["." generation] [// - [analysis (#+ Operation)] + [analysis (#+ Operation) + [macro (#+ Expander)]] ["." synthesis]]]]) (type: #export Eval (-> Nat Type Code (Operation Any))) (def: #export (evaluator expander synthesis-state generation-state generate) - (All [anchor expression directive] + (All [anchor expression artifact] (-> Expander synthesis.State+ - (generation.State+ anchor expression directive) - (generation.Phase anchor expression directive) + (generation.State+ anchor expression artifact) + (generation.Phase anchor expression artifact) Eval)) (let [analyze (analysisP.phase expander)] (function (eval count type exprC) diff --git a/stdlib/source/lux/tool/compiler/default/init.lux b/stdlib/source/lux/tool/compiler/default/init.lux index c053445f2..4220659b9 100644 --- a/stdlib/source/lux/tool/compiler/default/init.lux +++ b/stdlib/source/lux/tool/compiler/default/init.lux @@ -19,11 +19,11 @@ ["#." syntax (#+ Aliases)] ["#." evaluation] ["/#" // (#+ Instancer) - ["#." analysis] + ["#." analysis + [macro (#+ Expander)]] ["#." synthesis] ["#." directive (#+ Requirements)] ["#." phase - [macro (#+ Expander)] [".P" analysis ["." module]] [".P" synthesis] diff --git a/stdlib/source/lux/tool/compiler/default/platform.lux b/stdlib/source/lux/tool/compiler/default/platform.lux index 753ab8f5c..23b1c5b6c 100644 --- a/stdlib/source/lux/tool/compiler/default/platform.lux +++ b/stdlib/source/lux/tool/compiler/default/platform.lux @@ -20,10 +20,10 @@ ["#." init] ["#." syntax] ["/#" // - ["#." analysis] + ["#." analysis + [macro (#+ Expander)]] ["#." directive] ["#." phase - [macro (#+ Expander)] ## TODO: Get rid of this import ASAP ["." extension (#+ Extender)] ["." generation (#+ Buffer)] diff --git a/stdlib/source/lux/tool/compiler/phase/analysis.lux b/stdlib/source/lux/tool/compiler/phase/analysis.lux index fbdb18f16..5a03a40c3 100644 --- a/stdlib/source/lux/tool/compiler/phase/analysis.lux +++ b/stdlib/source/lux/tool/compiler/phase/analysis.lux @@ -16,11 +16,11 @@ ["#." case] ["#." function] ["#/" // - ["#." macro (#+ Expander)] ["#." extension] ["#/" // #_ ["#." reference] - ["/" analysis (#+ Analysis Operation Phase)]]]]) + ["/" analysis (#+ Analysis Operation Phase) + ["#." macro (#+ Expander)]]]]]) (exception: #export (unrecognized-syntax {code Code}) (ex.report ["Code" (%.code code)])) @@ -108,7 +108,7 @@ (case ?macro (#.Some macro) (do @ - [expansion (//extension.lift (//macro.expand-one expander def-name macro argsC+))] + [expansion (//extension.lift (/macro.expand-one expander def-name macro argsC+))] (compile expansion)) _ diff --git a/stdlib/source/lux/tool/compiler/phase/directive.lux b/stdlib/source/lux/tool/compiler/phase/directive.lux index dc4115610..0144c877c 100644 --- a/stdlib/source/lux/tool/compiler/phase/directive.lux +++ b/stdlib/source/lux/tool/compiler/phase/directive.lux @@ -11,13 +11,13 @@ ["." list ("#;." fold monoid)]]] ["." macro]] ["." // - ["#." macro (#+ Expander)] ["#." extension] [".P" analysis ["." type]] ["#/" // #_ [reference (#+)] - ["#." analysis] + ["#." analysis + ["#/." macro (#+ Expander)]] ["/" directive (#+ Phase)]]]) (exception: #export (not-a-directive {code Code}) @@ -57,7 +57,7 @@ #.None (//.throw ..macro-was-not-found macro-name))] - (//extension.lift (//macro.expand expander macro-name macro inputs))) + (//extension.lift (///analysis/macro.expand expander macro-name macro inputs))) _ (//.throw ..invalid-macro-call code))))] diff --git a/stdlib/source/lux/tool/compiler/phase/extension/directive/lux.lux b/stdlib/source/lux/tool/compiler/phase/extension/directive/lux.lux index 856648097..cc00bfa5f 100644 --- a/stdlib/source/lux/tool/compiler/phase/extension/directive/lux.lux +++ b/stdlib/source/lux/tool/compiler/phase/extension/directive/lux.lux @@ -23,15 +23,15 @@ ["#." bundle] ["#." analysis] ["#/" // - ["#." macro (#+ Expander)] ["#." generation] [analysis ["." module] [".A" type]] ["#/" // #_ - ["#." analysis] ["#." synthesis (#+ Synthesis)] ["#." directive (#+ Import Requirements Phase Operation Handler Bundle)] + ["#." analysis + [macro (#+ Expander)]] [default ["#." evaluation]]]]]) diff --git a/stdlib/source/lux/tool/compiler/phase/macro.lux b/stdlib/source/lux/tool/compiler/phase/macro.lux deleted file mode 100644 index db384c727..000000000 --- a/stdlib/source/lux/tool/compiler/phase/macro.lux +++ /dev/null @@ -1,53 +0,0 @@ -(.module: - [lux #* - [abstract - [monad (#+ do)]] - [control - ["." try (#+ Try)] - ["." exception (#+ exception:)]] - [data - ["." text - ["%" format (#+ format)]] - [collection - [array (#+ Array)] - ["." list ("#@." functor)]]] - ["." macro]] - ["." //]) - -(exception: #export (expansion-failed {macro Name} {inputs (List Code)} {error Text}) - (exception.report - ["Macro" (%.name macro)] - ["Inputs" (exception.enumerate %.code inputs)] - ["Error" error])) - -(exception: #export (must-have-single-expansion {macro Name} {inputs (List Code)} {outputs (List Code)}) - (exception.report - ["Macro" (%.name macro)] - ["Inputs" (exception.enumerate %.code inputs)] - ["Outputs" (exception.enumerate %.code outputs)])) - -(type: #export Expander - (-> Macro (List Code) Lux (Try (Try [Lux (List Code)])))) - -(def: #export (expand expander name macro inputs) - (-> Expander Name Macro (List Code) (Meta (List Code))) - (function (_ state) - (do try.monad - [output (expander macro inputs state)] - (case output - (#try.Success output) - (#try.Success output) - - (#try.Failure error) - ((//.throw expansion-failed [name inputs error]) state))))) - -(def: #export (expand-one expander name macro inputs) - (-> Expander Name Macro (List Code) (Meta Code)) - (do macro.monad - [expansion (expand expander name macro inputs)] - (case expansion - (^ (list single)) - (wrap single) - - _ - (//.throw must-have-single-expansion [name inputs expansion])))) diff --git a/stdlib/source/program/compositor.lux b/stdlib/source/program/compositor.lux index 53598f8b5..117e90ac2 100644 --- a/stdlib/source/program/compositor.lux +++ b/stdlib/source/program/compositor.lux @@ -28,10 +28,10 @@ ["." console]] [tool [compiler - ["." analysis] + ["." analysis + [macro (#+ Expander)]] ["." directive] ["." phase - [macro (#+ Expander)] [extension (#+ Extender)] ["." generation]] [default -- cgit v1.2.3