From fc2737b5226eda69c12bc593e83e22ed54e4d3af Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 8 Jul 2022 19:15:02 -0400 Subject: Extracted macro-expansion machinery into its own module. --- stdlib/source/library/lux/meta/macro.lux | 133 +------------------- stdlib/source/library/lux/meta/macro/expansion.lux | 140 +++++++++++++++++++++ .../library/lux/meta/macro/syntax/definition.lux | 5 +- stdlib/source/library/lux/meta/type.lux | 5 +- 4 files changed, 152 insertions(+), 131 deletions(-) create mode 100644 stdlib/source/library/lux/meta/macro/expansion.lux (limited to 'stdlib/source/library') diff --git a/stdlib/source/library/lux/meta/macro.lux b/stdlib/source/library/lux/meta/macro.lux index 464329a25..c8c3a26fb 100644 --- a/stdlib/source/library/lux/meta/macro.lux +++ b/stdlib/source/library/lux/meta/macro.lux @@ -6,89 +6,16 @@ [data ["[0]" text (.use "[1]#[0]" monoid)] [collection - ["[0]" list (.use "[1]#[0]" monoid monad)]]] + ["[0]" list (.use "[1]#[0]" monad)]]] [math [number - ["[0]" nat] - ["[0]" int]]]]] + ["[0]" nat]]]]] + ["[0]" / + ["[1][0]" expansion]] ["[0]" // (.only) ["[0]" code] - ["[0]" location] ["[0]" symbol (.use "[1]#[0]" codec)]]) -(def .public (single_expansion syntax) - (-> Code (Meta (List Code))) - (case syntax - [_ {.#Form {.#Item [[_ {.#Symbol name}] args]}}] - (do //.monad - [?macro (//.macro name)] - (case ?macro - {.#Some macro} - ((as Macro' macro) args) - - {.#None} - (at //.monad in (list syntax)))) - - _ - (at //.monad in (list syntax)))) - -(def .public (expansion syntax) - (-> Code (Meta (List Code))) - (case syntax - [_ {.#Form {.#Item [[_ {.#Symbol name}] args]}}] - (do //.monad - [?macro (//.macro name)] - (case ?macro - {.#Some macro} - (do [! //.monad] - [top_level_expansion ((as Macro' macro) args)] - (|> top_level_expansion - (monad.each //.monad expansion) - (at ! each list#conjoint))) - - {.#None} - (at //.monad in (list syntax)))) - - _ - (at //.monad in (list syntax)))) - -(def .public (full_expansion syntax) - (-> Code (Meta (List Code))) - (case syntax - [_ {.#Form {.#Item [[_ {.#Symbol name}] args]}}] - (do //.monad - [?macro (//.macro name)] - (case ?macro - {.#Some macro} - (do //.monad - [expansion ((as Macro' macro) args) - expansion' (monad.each //.monad full_expansion expansion)] - (in (list#conjoint expansion'))) - - {.#None} - (do //.monad - [parts' (monad.each //.monad full_expansion (list.partial (code.symbol name) args))] - (in (list (code.form (list#conjoint parts'))))))) - - [_ {.#Form {.#Item [harg targs]}}] - (do //.monad - [harg+ (full_expansion harg) - targs+ (monad.each //.monad full_expansion targs)] - (in (list (code.form (list#composite harg+ (list#conjoint (is (List (List Code)) targs+))))))) - - [_ {.#Variant members}] - (do //.monad - [members' (monad.each //.monad full_expansion members)] - (in (list (code.variant (list#conjoint members'))))) - - [_ {.#Tuple members}] - (do //.monad - [members' (monad.each //.monad full_expansion members)] - (in (list (code.tuple (list#conjoint members'))))) - - _ - (at //.monad in (list syntax)))) - (def .public (symbol prefix) (-> Text (Meta Code)) (do //.monad @@ -129,54 +56,6 @@ _ (//.failure (..wrong_syntax_error (.symbol ..with_symbols)))))) -(def .public (one_expansion token) - (-> Code (Meta Code)) - (do //.monad - [token+ (..expansion token)] - (case token+ - (list token') - (in token') - - _ - (//.failure "Macro expanded to more than 1 element.")))) - -(with_template [ ] - [(def .public - (.macro (_ tokens) - (let [[module _] (.symbol .._) - [_ short] (.symbol ) - macro_name [module short]] - (case (is (Maybe [Bit Code]) - (case tokens - (list [_ {.#Text "omit"}] - token) - {.#Some [#1 token]} - - (list token) - {.#Some [#0 token]} - - _ - {.#None})) - {.#Some [omit? token]} - (do //.monad - [location //.location - output ( token) - .let [_ ("lux io log" (all text#composite (symbol#encoded macro_name) " " (location.format location))) - _ (list#each (|>> code.format "lux io log") - output) - _ ("lux io log" "")]] - (in (if omit? - (list) - output))) - - {.#None} - (//.failure (..wrong_syntax_error macro_name))))))] - - [log_single_expansion! ..single_expansion] - [log_expansion! ..expansion] - [log_full_expansion! ..full_expansion] - ) - (def .public times (.macro (_ tokens) (case tokens @@ -190,7 +69,7 @@ _ (do [! //.monad] [after (|> before - (monad.each ! ..single_expansion) + (monad.each ! /expansion.single) (at ! each list#conjoint))] (again (-- times) after)))) @@ -201,7 +80,7 @@ (.macro (_ it) (let [! //.monad] (|> it - (monad.each ! ..expansion) + (monad.each ! /expansion.complete) (at ! each list#conjoint))))) (def .public function diff --git a/stdlib/source/library/lux/meta/macro/expansion.lux b/stdlib/source/library/lux/meta/macro/expansion.lux new file mode 100644 index 000000000..781eb141b --- /dev/null +++ b/stdlib/source/library/lux/meta/macro/expansion.lux @@ -0,0 +1,140 @@ +(.require + [library + [lux (.except local symbol function macro) + [abstract + ["[0]" monad (.only do)]] + [data + ["[0]" text (.use "[1]#[0]" monoid)] + [collection + ["[0]" list (.use "[1]#[0]" monoid monad)]]]]] + ["[0]" /// (.only) + ["[0]" code] + ["[0]" location] + ["[0]" symbol (.use "[1]#[0]" codec)]]) + +(def wrong_syntax_error + (-> Symbol Text) + (|>> symbol#encoded + (text.prefix (text#composite "Wrong syntax for " text.\'')) + (text.suffix (text#composite text.\'' ".")))) + +(def .public (single syntax) + (-> Code (Meta (List Code))) + (case syntax + [_ {.#Form {.#Item [[_ {.#Symbol name}] args]}}] + (do ///.monad + [?macro (///.macro name)] + (case ?macro + {.#Some macro} + ((as Macro' macro) args) + + {.#None} + (at ///.monad in (list syntax)))) + + _ + (at ///.monad in (list syntax)))) + +(def .public (complete syntax) + (-> Code (Meta (List Code))) + (case syntax + [_ {.#Form {.#Item [[_ {.#Symbol name}] args]}}] + (do ///.monad + [?macro (///.macro name)] + (case ?macro + {.#Some macro} + (do [! ///.monad] + [top_level_complete ((as Macro' macro) args)] + (|> top_level_complete + (monad.each ///.monad complete) + (at ! each list#conjoint))) + + {.#None} + (at ///.monad in (list syntax)))) + + _ + (at ///.monad in (list syntax)))) + +(def .public (total syntax) + (-> Code (Meta (List Code))) + (case syntax + [_ {.#Form {.#Item [[_ {.#Symbol name}] args]}}] + (do ///.monad + [?macro (///.macro name)] + (case ?macro + {.#Some macro} + (do ///.monad + [complete ((as Macro' macro) args) + complete' (monad.each ///.monad total complete)] + (in (list#conjoint complete'))) + + {.#None} + (do ///.monad + [parts' (monad.each ///.monad total (list.partial (code.symbol name) args))] + (in (list (code.form (list#conjoint parts'))))))) + + [_ {.#Form {.#Item [harg targs]}}] + (do ///.monad + [harg+ (total harg) + targs+ (monad.each ///.monad total targs)] + (in (list (code.form (list#composite harg+ (list#conjoint (is (List (List Code)) targs+))))))) + + [_ {.#Variant members}] + (do ///.monad + [members' (monad.each ///.monad total members)] + (in (list (code.variant (list#conjoint members'))))) + + [_ {.#Tuple members}] + (do ///.monad + [members' (monad.each ///.monad total members)] + (in (list (code.tuple (list#conjoint members'))))) + + _ + (at ///.monad in (list syntax)))) + +(def .public (one token) + (-> Code (Meta Code)) + (do ///.monad + [token+ (..complete token)] + (case token+ + (list token') + (in token') + + _ + (///.failure "Macro expanded to more than 1 element.")))) + +(with_template [ ] + [(def .public + (.macro (_ tokens) + (let [[module _] (.symbol .._) + [_ short] (.symbol ) + macro_name [module short]] + (case (is (Maybe [Bit Code]) + (case tokens + (list [_ {.#Text "omit"}] + token) + {.#Some [#1 token]} + + (list token) + {.#Some [#0 token]} + + _ + {.#None})) + {.#Some [omit? token]} + (do ///.monad + [location ///.location + output ( token) + .let [_ ("lux io log" (all text#composite (symbol#encoded macro_name) " " (location.format location))) + _ (list#each (|>> code.format "lux io log") + output) + _ ("lux io log" "")]] + (in (if omit? + (list) + output))) + + {.#None} + (///.failure (..wrong_syntax_error macro_name))))))] + + [log_single! ..single] + [log_complete! ..complete] + [log_total! ..total] + ) diff --git a/stdlib/source/library/lux/meta/macro/syntax/definition.lux b/stdlib/source/library/lux/meta/macro/syntax/definition.lux index 44030c108..445068226 100644 --- a/stdlib/source/library/lux/meta/macro/syntax/definition.lux +++ b/stdlib/source/library/lux/meta/macro/syntax/definition.lux @@ -17,7 +17,8 @@ ["[0]" list]]] ["[0]" meta (.only) ["[0]" location] - ["[0]" macro] + [macro + ["[0]" expansion]] ["[0]" code (.only) ["<[1]>" \\parser (.only Parser)]]]]] ["[0]" // (.only) @@ -67,7 +68,7 @@ (do [! <>.monad] [raw .any me_raw (|> raw - macro.full_expansion + expansion.total (meta.result compiler) <>.lifted)] (<| (.locally me_raw) diff --git a/stdlib/source/library/lux/meta/type.lux b/stdlib/source/library/lux/meta/type.lux index 9af406098..42f9f2961 100644 --- a/stdlib/source/library/lux/meta/type.lux +++ b/stdlib/source/library/lux/meta/type.lux @@ -24,7 +24,8 @@ ["[0]" code (.only) ["<[1]>" \\parser (.only Parser)]] ["[0]" macro (.only) - [syntax (.only syntax)]]]]]) + [syntax (.only syntax)] + ["[0]" expansion]]]]]) (with_template [ ] [(def .public ( type) @@ -474,7 +475,7 @@ (-> Lux (Parser Typed)) (do <>.monad [it .any - type_check (<>.lifted (meta.result lux (macro.expansion it)))] + type_check (<>.lifted (meta.result lux (expansion.complete it)))] (<| (.locally type_check) .form (<>.after (.this (` "lux type check"))) -- cgit v1.2.3