diff options
author | Eduardo Julian | 2021-08-19 04:59:06 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-08-19 04:59:06 -0400 |
commit | d772fe99d5d4990c6774481fb64d12280cdb6aae (patch) | |
tree | 209a2ce3b8e896be15db40bc58db830a5304b4f9 /stdlib/source/library/lux/tool/compiler | |
parent | e00ba096c8837abe85d366e0c1293c09dbe84d81 (diff) |
Enabled compile-time code evaluation (i.e. "eval" function).
Diffstat (limited to '')
13 files changed, 129 insertions, 95 deletions
diff --git a/stdlib/source/library/lux/tool/compiler/default/init.lux b/stdlib/source/library/lux/tool/compiler/default/init.lux index 04971dadd..c01a1f6c1 100644 --- a/stdlib/source/library/lux/tool/compiler/default/init.lux +++ b/stdlib/source/library/lux/tool/compiler/default/init.lux @@ -169,9 +169,9 @@ (in [buffer registry]))) ... TODO: Inline ASAP -(def: (process_directive archive expander pre_payoad code) +(def: (process_directive wrapper archive expander pre_payoad code) (All [directive] - (-> Archive Expander (Payload directive) Code + (-> ///phase.Wrapper Archive Expander (Payload directive) Code (All [anchor expression] (///directive.Operation anchor expression directive [Requirements (Payload directive)])))) @@ -181,26 +181,26 @@ (///generation.set_buffer pre_buffer)) _ (///directive.lifted_generation (///generation.set_registry pre_registry)) - requirements (let [execute! (directiveP.phase expander)] + requirements (let [execute! (directiveP.phase wrapper expander)] (execute! archive code)) post_payload (..get_current_payload pre_payoad)] (in [requirements post_payload]))) -(def: (iteration' archive expander reader source pre_payload) +(def: (iteration' wrapper archive expander reader source pre_payload) (All [directive] - (-> Archive Expander Reader Source (Payload directive) + (-> ///phase.Wrapper Archive Expander Reader Source (Payload directive) (All [anchor expression] (///directive.Operation anchor expression directive [Source Requirements (Payload directive)])))) (do ///phase.monad [[source code] (///directive.lifted_analysis (..read source reader)) - [requirements post_payload] (process_directive archive expander pre_payload code)] + [requirements post_payload] (process_directive wrapper archive expander pre_payload code)] (in [source requirements post_payload]))) -(def: (iteration archive expander module source pre_payload aliases) +(def: (iteration wrapper archive expander module source pre_payload aliases) (All [directive] - (-> Archive Expander Module Source (Payload directive) Aliases + (-> ///phase.Wrapper Archive Expander Module Source (Payload directive) Aliases (All [anchor expression] (///directive.Operation anchor expression directive (Maybe [Source Requirements (Payload directive)]))))) @@ -208,7 +208,7 @@ [reader (///directive.lifted_analysis (..reader module aliases source))] (function (_ state) - (case (///phase.result' state (..iteration' archive expander reader source pre_payload)) + (case (///phase.result' state (..iteration' wrapper archive expander reader source pre_payload)) (#try.Success [state source&requirements&buffer]) (#try.Success [state (#.Some source&requirements&buffer)]) @@ -228,11 +228,11 @@ (-> .Module Aliases) (|>> (value@ #.module_aliases) (dictionary.of_list text.hash))) -(def: .public (compiler expander prelude write_directive) +(def: .public (compiler wrapper expander prelude write_directive) (All [anchor expression directive] - (-> Expander Module (-> directive Binary) + (-> ///phase.Wrapper Expander Module (-> directive Binary) (Instancer (///directive.State+ anchor expression directive) .Module))) - (let [execute! (directiveP.phase expander)] + (let [execute! (directiveP.phase wrapper expander)] (function (_ key parameters input) (let [dependencies (default_dependencies prelude input)] {#///.dependencies dependencies @@ -243,7 +243,7 @@ (..begin dependencies hash input)) .let [module (value@ #///.module input)]] (loop [iteration (<| (///phase.result' state) - (..iteration archive expander module source buffer ///syntax.no_aliases))] + (..iteration wrapper archive expander module source buffer ///syntax.no_aliases))] (do ! [[state ?source&requirements&temporary_payload] iteration] (case ?source&requirements&temporary_payload @@ -284,5 +284,5 @@ (value@ #///directive.referrals) (monad.map ! (execute! archive))) temporary_payload (..get_current_payload temporary_payload)] - (..iteration archive expander module source temporary_payload (..module_aliases analysis_module))))))})])) + (..iteration wrapper archive expander module source temporary_payload (..module_aliases analysis_module))))))})])) )))))})))) diff --git a/stdlib/source/library/lux/tool/compiler/default/platform.lux b/stdlib/source/library/lux/tool/compiler/default/platform.lux index 1ac28821f..b5eed68f8 100644 --- a/stdlib/source/library/lux/tool/compiler/default/platform.lux +++ b/stdlib/source/library/lux/tool/compiler/default/platform.lux @@ -64,15 +64,12 @@ (with_expansions [<type_vars> (as_is anchor expression directive) <Operation> (as_is ///generation.Operation <type_vars>)] - (type: .public Phase_Wrapper - (All [s i o] (-> (Phase s i o) Any))) - (type: .public (Platform <type_vars>) {#&file_system (file.System Async) #host (///generation.Host expression directive) #phase (///generation.Phase <type_vars>) #runtime (<Operation> [Registry Output]) - #phase_wrapper (-> Archive (<Operation> Phase_Wrapper)) + #phase_wrapper (-> Archive (<Operation> ///phase.Wrapper)) #write (-> directive Binary)}) ... TODO: Get rid of this @@ -197,7 +194,7 @@ (def: (phase_wrapper archive platform state) (All [<type_vars>] - (-> Archive <Platform> <State+> (Try [<State+> Phase_Wrapper]))) + (-> Archive <Platform> <State+> (Try [<State+> ///phase.Wrapper]))) (let [phase_wrapper (value@ #phase_wrapper platform)] (|> archive phase_wrapper @@ -206,8 +203,8 @@ (def: (complete_extensions host_directive_bundle phase_wrapper [analysers synthesizers generators directives]) (All [<type_vars>] - (-> (-> Phase_Wrapper (///directive.Bundle <type_vars>)) - Phase_Wrapper + (-> (-> ///phase.Wrapper (///directive.Bundle <type_vars>)) + ///phase.Wrapper [(Dictionary Text ///analysis.Handler) (Dictionary Text ///synthesis.Handler) (Dictionary Text (///generation.Handler <type_vars>)) @@ -230,11 +227,11 @@ ///analysis.Bundle <Platform> <Bundle> - (-> Phase_Wrapper (///directive.Bundle <type_vars>)) + (-> ///phase.Wrapper (///directive.Bundle <type_vars>)) (Program expression directive) - [Type Type Type] (-> Phase_Wrapper Extender) + [Type Type Type] (-> ///phase.Wrapper Extender) Import (List Context) - (Async (Try [<State+> Archive])))) + (Async (Try [<State+> Archive ///phase.Wrapper])))) (do {! (try.with async.monad)} [.let [state (//init.state (value@ #static.host static) module @@ -247,7 +244,8 @@ [archive analysis_state bundles] (ioW.thaw (value@ #host platform) (value@ #&file_system platform) static import compilation_sources) .let [with_missing_extensions (: (All [<type_vars>] - (-> <Platform> (Program expression directive) <State+> (Async (Try <State+>)))) + (-> <Platform> (Program expression directive) <State+> + (Async (Try [///phase.Wrapper <State+>])))) (function (_ platform program state) (async\in (do try.monad @@ -256,19 +254,20 @@ (initialize_state (extender phase_wrapper) (:expected (..complete_extensions host_directive_bundle phase_wrapper (:expected bundles))) analysis_state) - (try\map (//init.with_default_directives expander host_analysis program anchorT,expressionT,directiveT (extender phase_wrapper))))))))]] + (try\map (|>> (//init.with_default_directives expander host_analysis program anchorT,expressionT,directiveT (extender phase_wrapper)) + [phase_wrapper])))))))]] (if (archive.archived? archive archive.runtime_module) (do ! - [state (with_missing_extensions platform program state)] - (in [state archive])) + [[phase_wrapper state] (with_missing_extensions platform program state)] + (in [state archive phase_wrapper])) (do ! [[state [archive payload]] (|> (..process_runtime archive platform) (///phase.result' state) async\in) _ (..cache_module static platform 0 payload) - state (with_missing_extensions platform program state)] - (in [state archive]))))) + [phase_wrapper state] (with_missing_extensions platform program state)] + (in [state archive phase_wrapper]))))) (def: compilation_log_separator (format text.new_line text.tab)) @@ -523,9 +522,9 @@ try.trusted product.left)) - (def: .public (compile import static expander platform compilation context) + (def: .public (compile phase_wrapper import static expander platform compilation context) (All [<type_vars>] - (-> Import Static Expander <Platform> Compilation <Context> <Return>)) + (-> ///phase.Wrapper Import Static Expander <Platform> Compilation <Context> <Return>)) (let [[compilation_sources compilation_host_dependencies compilation_libraries compilation_target compilation_module] compilation base_compiler (:sharing [<type_vars>] <Context> @@ -533,7 +532,7 @@ (///.Compiler <State+> .Module Any) (:expected - ((//init.compiler expander syntax.prelude (value@ #write platform)) $.key (list)))) + ((//init.compiler phase_wrapper expander syntax.prelude (value@ #write platform)) $.key (list)))) compiler (..parallel context (function (_ importer import! module_id [archive state] module) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux index aefd908c4..45216a70f 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis.lux @@ -553,4 +553,5 @@ #.seed 0 #.scope_type_vars (list) #.extensions [] + #.eval (:as (-> Type Code (Meta Any)) []) #.host []}) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux index 1859802d6..8bba841e2 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/evaluation.lux @@ -27,7 +27,7 @@ [descriptor (#+ Module)]]]]]]]]) (type: .public Eval - (-> Archive Nat Type Code (Operation Any))) + (-> Archive Type Code (Operation Any))) (def: (context [module_id artifact_id]) (-> Context Context) @@ -42,12 +42,14 @@ (generation.Phase anchor expression artifact) Eval)) (let [analyze (analysisP.phase expander)] - (function (eval archive count type exprC) + (function (eval archive type exprC) (do phase.monad [exprA (type.with_type type (analyze archive exprC)) module (extensionP.lifted - meta.current_module_name)] + meta.current_module_name) + count (extensionP.lifted + meta.seed)] (phase.lifted (do try.monad [exprS (|> exprA (synthesisP.phase archive) (phase.result synthesis_state))] (phase.result generation_state diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux index ee12a8bf0..c8cfe9c0e 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux @@ -124,10 +124,10 @@ (compile archive expansion)) _ - (/function.on compile argsC+ functionT functionA archive functionC))) + (/function.apply compile argsC+ functionT functionA archive functionC))) _ - (/function.on compile argsC+ functionT functionA archive functionC))) + (/function.apply compile argsC+ functionT functionA archive functionC))) _ (//.except ..unrecognized_syntax [location.dummy code']))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux index b3642f5f3..fc7575260 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/case.lux @@ -84,7 +84,7 @@ (#.Var id) (do ///.monad [?caseT' (//type.with_env - (check.read id))] + (check.read' id))] (.case ?caseT' (#.Some caseT') (recur envs caseT') @@ -110,7 +110,7 @@ (do ///.monad [funcT' (//type.with_env (do check.monad - [?funct' (check.read funcT_id)] + [?funct' (check.read' funcT_id)] (.case ?funct' (#.Some funct') (in funct') diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux index 69e75f374..8aa2f284f 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux @@ -71,7 +71,7 @@ (#.Var id) (do ! [?expectedT' (//type.with_env - (check.read id))] + (check.read' id))] (case ?expectedT' (#.Some expectedT') (recur expectedT') @@ -85,8 +85,7 @@ functionA (recur functionT) _ (//type.with_env (check.check expectedT functionT))] - (in functionA)) - )) + (in functionA)))) (#.Function inputT outputT) (<| (\ ! map (.function (_ [scope bodyA]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux index 0420b7811..452bf6bc1 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/inference.lux @@ -166,7 +166,8 @@ (#.Var infer_id) (do ///.monad - [?inferT' (//type.with_env (check.read infer_id))] + [?inferT' (//type.with_env + (check.read' infer_id))] (case ?inferT' (#.Some inferT') (general archive analyse inferT' args) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux index fe296c83e..886ffe065 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/structure.lux @@ -119,7 +119,7 @@ (#.Var id) (do ! [?expectedT' (//type.with_env - (check.read id))] + (check.read' id))] (case ?expectedT' (#.Some expectedT') (//type.with_type expectedT' @@ -144,7 +144,8 @@ (case funT (#.Var funT_id) (do ! - [?funT' (//type.with_env (check.read funT_id))] + [?funT' (//type.with_env + (check.read' funT_id))] (case ?funT' (#.Some funT') (//type.with_type (#.Apply inputT funT') @@ -208,7 +209,7 @@ (#.Var id) (do ! [?expectedT' (//type.with_env - (check.read id))] + (check.read' id))] (case ?expectedT' (#.Some expectedT') (//type.with_type expectedT' @@ -237,7 +238,8 @@ (case funT (#.Var funT_id) (do ! - [?funT' (//type.with_env (check.read funT_id))] + [?funT' (//type.with_env + (check.read' funT_id))] (case ?funT' (#.Some funT') (//type.with_type (#.Apply inputT funT') diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux index cc34e04cf..3c6425da3 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux @@ -1,16 +1,17 @@ (.module: [library [lux #* + ["." meta] [abstract ["." monad (#+ do)]] [control + ["." try] ["." exception (#+ exception:)]] [data [text ["%" format (#+ format)]] [collection - ["." list ("#\." fold monoid)]]] - ["." meta]]] + ["." list ("#\." fold monoid)]]]]] ["." // #_ ["#." extension] ["#." analysis @@ -18,11 +19,14 @@ ["/#" // #_ ["/" directive (#+ Phase)] ["#." analysis + ["." evaluation] ["#/." macro (#+ Expander)]] [/// ["//" phase] [reference (#+) - [variable (#+)]]]]]) + [variable (#+)]] + [meta + [archive (#+ Archive)]]]]]) (exception: .public (not_a_directive {code Code}) (exception.report @@ -36,44 +40,68 @@ (exception.report ["Name" (%.name name)])) +(type: Eval + (-> Type Code (Meta Any))) + +(def: (meta_eval archive bundle compiler_eval) + (-> Archive ///analysis.Bundle evaluation.Eval + Eval) + (function (_ type code lux) + (case (compiler_eval archive type code [bundle lux]) + (#try.Success [[_bundle lux'] value]) + (#try.Success [lux' value]) + + (#try.Failure error) + (#try.Failure error)))) + (with_expansions [<lux_def_module> (as_is [|form_location| (#.Form (list& [|text_location| (#.Text "lux def module")] annotations))])] - (def: .public (phase expander) - (-> Expander Phase) - (let [analyze (//analysis.phase expander)] + (def: .public (phase wrapper expander) + (-> //.Wrapper Expander Phase) + (let [analysis (//analysis.phase expander)] (function (recur archive code) - (case code - (^ [_ (#.Form (list& [_ (#.Text name)] inputs))]) - (//extension.apply archive recur [name inputs]) + (do {! //.monad} + [state //.get_state + .let [compiler_eval (meta_eval archive + (value@ [#//extension.state #/.analysis #/.state #//extension.bundle] state) + (evaluation.evaluator expander + (value@ [#//extension.state #/.synthesis #/.state] state) + (value@ [#//extension.state #/.generation #/.state] state) + (value@ [#//extension.state #/.generation #/.phase] state))) + extension_eval (:as Eval (wrapper (:expected compiler_eval)))] + _ (//.set_state (with@ [#//extension.state #/.analysis #/.state #//extension.state #.eval] extension_eval state))] + (case code + (^ [_ (#.Form (list& [_ (#.Text name)] inputs))]) + (//extension.apply archive recur [name inputs]) - (^ [_ (#.Form (list& macro inputs))]) - (do {! //.monad} - [expansion (/.lifted_analysis - (do ! - [macroA (//analysis/type.with_type Macro - (analyze archive macro))] - (case macroA - (^ (///analysis.constant macro_name)) - (do ! - [?macro (//extension.lifted (meta.macro macro_name)) - macro (case ?macro - (#.Some macro) - (in macro) - - #.None - (//.except ..macro_was_not_found macro_name))] - (//extension.lifted (///analysis/macro.expand expander macro_name macro inputs))) - - _ - (//.except ..invalid_macro_call code))))] - (case expansion - (^ (list& <lux_def_module> referrals)) - (|> (recur archive <lux_def_module>) - (\ ! map (revised@ #/.referrals (list\compose referrals)))) + (^ [_ (#.Form (list& macro inputs))]) + (do ! + [expansion (/.lifted_analysis + (do ! + [macroA (//analysis/type.with_type Macro + (analysis archive macro))] + (case macroA + (^ (///analysis.constant macro_name)) + (do ! + [?macro (//extension.lifted (meta.macro macro_name)) + macro (case ?macro + (#.Some macro) + (in macro) + + #.None + (//.except ..macro_was_not_found macro_name))] + (//extension.lifted (///analysis/macro.expand expander macro_name macro inputs))) + + _ + (//.except ..invalid_macro_call code))))] + (case expansion + (^ (list& <lux_def_module> referrals)) + (|> (recur archive <lux_def_module>) + (\ ! map (revised@ #/.referrals (list\compose referrals)))) - _ - (|> expansion - (monad.map ! (recur archive)) - (\ ! map (list\fold /.merge_requirements /.no_requirements))))) + _ + (|> expansion + (monad.map ! (recur archive)) + (\ ! map (list\fold /.merge_requirements /.no_requirements))))) - _ - (//.except ..not_a_directive code)))))) + _ + (//.except ..not_a_directive code))))))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/lux.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/lux.lux index d26820e9a..e56a48572 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/lux.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/lux.lux @@ -164,9 +164,8 @@ (case args (^ (list typeC valueC)) (do {! ////.monad} - [seed (///.lifted meta.seed) - actualT (\ ! map (|>> (:as Type)) - (eval archive seed Type typeC)) + [actualT (\ ! map (|>> (:as Type)) + (eval archive Type typeC)) _ (typeA.infer actualT)] (typeA.with_type actualT (analyse archive valueC))) @@ -180,9 +179,8 @@ (case args (^ (list typeC valueC)) (do {! ////.monad} - [seed (///.lifted meta.seed) - actualT (\ ! map (|>> (:as Type)) - (eval archive seed Type typeC)) + [actualT (\ ! map (|>> (:as Type)) + (eval archive Type typeC)) _ (typeA.infer actualT) [valueT valueA] (typeA.with_inference (analyse archive valueC))] @@ -240,7 +238,8 @@ (///bundle.install "error" (unary Text Nothing)) (///bundle.install "exit" (unary Int Nothing))))) -(def: I64* (type (I64 Any))) +(def: I64* + (type (I64 Any))) (def: bundle::i64 Bundle diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux index 1e7ca8cc3..f26b13ade 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux @@ -34,7 +34,7 @@ (def: arity_arguments (-> Arity (List Synthesis)) - (|>> dec + (|>> -- (enum.range n.enum 1) (list\map (|>> /.variable/local)))) diff --git a/stdlib/source/library/lux/tool/compiler/phase.lux b/stdlib/source/library/lux/tool/compiler/phase.lux index 92680654d..7e137387e 100644 --- a/stdlib/source/library/lux/tool/compiler/phase.lux +++ b/stdlib/source/library/lux/tool/compiler/phase.lux @@ -34,6 +34,9 @@ (type: .public (Phase s i o) (-> Archive i (Operation s o))) +(type: .public Wrapper + (All [s i o] (-> (Phase s i o) Any))) + (def: .public (result' state operation) (All [s o] (-> s (Operation s o) (Try [s o]))) |