diff options
author | Eduardo Julian | 2022-07-03 00:35:32 -0400 |
---|---|---|
committer | Eduardo Julian | 2022-07-03 00:35:32 -0400 |
commit | 9e7ddacf853efd7a18c1911d2f287d483b083229 (patch) | |
tree | 140eee091b7453879f072a48044635d03aa5096b /stdlib/source/library/lux/meta/macro/pattern.lux | |
parent | 7e4c9ba2e02f06fa621ffe24bc0ca046536429ef (diff) |
Added a new custom type for pattern-matching macros.
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/library/lux/meta/macro/pattern.lux | 226 |
1 files changed, 116 insertions, 110 deletions
diff --git a/stdlib/source/library/lux/meta/macro/pattern.lux b/stdlib/source/library/lux/meta/macro/pattern.lux index affa8273a..7e3b30d9d 100644 --- a/stdlib/source/library/lux/meta/macro/pattern.lux +++ b/stdlib/source/library/lux/meta/macro/pattern.lux @@ -2,14 +2,14 @@ [library [lux (.except or let with_template |> `)]]) -(def partial_list - (`` ("lux in-module" (,, (static .prelude)) .partial_list))) +(def list#partial + (`` ("lux in-module" (,, (static .prelude)) .list#partial))) (def locally (macro (_ tokens lux) (.let [[prelude _] (symbol ._)] (case tokens - (pattern (list [@ {.#Symbol ["" name]}])) + (list [@ {.#Symbol ["" name]}]) {.#Right [lux (list (.` ("lux in-module" (, [@ {.#Text prelude}]) (, [@ {.#Symbol [prelude name]}]))))]} @@ -73,107 +73,112 @@ ) (def .public or - (macro (_ tokens) - (case tokens - (pattern (partial_list [_ {.#Form patterns}] body branches)) - (case patterns - {.#End} - (failure (..wrong_syntax_error (symbol ..or))) - - _ - (.let [pairs (.|> patterns - (list#each (function (_ pattern) (list pattern body))) - list#conjoint)] - (meta#in (list#composite pairs branches)))) - _ - (failure (..wrong_syntax_error (symbol ..or)))))) + (pattern + (macro (_ tokens) + (case tokens + (list#partial [_ {.#Form patterns}] body branches) + (case patterns + {.#End} + (failure (..wrong_syntax_error (symbol ..or))) + + _ + (.let [pairs (.|> patterns + (list#each (function (_ pattern) (list pattern body))) + list#conjoint)] + (meta#in (list#composite pairs branches)))) + _ + (failure (..wrong_syntax_error (symbol ..or))))))) (def .public with_template - (macro (_ tokens) - (case tokens - (pattern (partial_list [_ {.#Form (list [_ {.#Tuple bindings}] - [_ {.#Tuple templates}])}] - [_ {.#Form data}] - branches)) - (case (is (Maybe (List Code)) - (do maybe#monad - [bindings' (monad#each maybe#monad symbol_short bindings) - data' (monad#each maybe#monad tuple_list data)] - (.let [num_bindings (list#size bindings')] - (if (every? (|>> ("lux i64 =" num_bindings)) - (list#each list#size data')) - (.let [apply (is (-> Replacement_Environment (List Code)) - (function (_ env) (list#each (realized_template env) templates)))] - (.|> data' - (list#each (function#composite apply (replacement_environment bindings'))) - list#conjoint - in)) - {.#None})))) - {.#Some output} - (meta#in (list#composite output branches)) - - {.#None} - (failure (..wrong_syntax_error (symbol ..with_template)))) - - _ - (failure (..wrong_syntax_error (symbol ..with_template)))))) + (pattern + (macro (_ tokens) + (case tokens + (list#partial [_ {.#Form (list [_ {.#Tuple bindings}] + [_ {.#Tuple templates}])}] + [_ {.#Form data}] + branches) + (case (is (Maybe (List Code)) + (do maybe#monad + [bindings' (monad#each maybe#monad symbol_short bindings) + data' (monad#each maybe#monad tuple_list data)] + (.let [num_bindings (list#size bindings')] + (if (every? (|>> ("lux i64 =" num_bindings)) + (list#each list#size data')) + (.let [apply (is (-> Replacement_Environment (List Code)) + (function (_ env) (list#each (realized_template env) templates)))] + (.|> data' + (list#each (function#composite apply (replacement_environment bindings'))) + list#conjoint + in)) + {.#None})))) + {.#Some output} + (meta#in (list#composite output branches)) + + {.#None} + (failure (..wrong_syntax_error (symbol ..with_template)))) + + _ + (failure (..wrong_syntax_error (symbol ..with_template))))))) (def .public multi - (macro (_ tokens) - (case tokens - (pattern (partial_list [_meta {.#Form levels}] body next_branches)) - (do meta#monad - [mlc (multi_level_case^ levels) - .let [initial_bind? (case mlc - [[_ {.#Symbol _}] _] - #1 - - _ - #0)] - expected ..expected_type - g!temp (..generated_symbol "temp")] - (in (list g!temp - (.` ({{.#Some (, g!temp)} - (, g!temp) - - {.#None} - (.case (, g!temp) - (,* next_branches))} - ("lux type check" {.#Apply (, (type_code expected)) Maybe} - (.case (, g!temp) - (,* (multi_level_case$ g!temp [mlc body])) - - (,* (if initial_bind? - (list) - (list g!temp (.` {.#None}))))))))))) - - _ - (failure (..wrong_syntax_error (symbol ..multi)))))) + (pattern + (macro (_ tokens) + (case tokens + (list#partial [_meta {.#Form levels}] body next_branches) + (do meta#monad + [mlc (multi_level_case^ levels) + .let [initial_bind? (case mlc + [[_ {.#Symbol _}] _] + #1 + + _ + #0)] + expected ..expected_type + g!temp (..generated_symbol "temp")] + (in (list g!temp + (.` ({{.#Some (, g!temp)} + (, g!temp) + + {.#None} + (.case (, g!temp) + (,* next_branches))} + ("lux type check" {.#Apply (, (type_code expected)) Maybe} + (.case (, g!temp) + (,* (multi_level_case$ g!temp [mlc body])) + + (,* (if initial_bind? + (list) + (list g!temp (.` {.#None}))))))))))) + + _ + (failure (..wrong_syntax_error (symbol ..multi))))))) (def .public let - (macro (_ tokens) - (case tokens - (pattern (partial_list [_meta {.#Form (list [_ {.#Symbol ["" name]}] pattern)}] body branches)) - (.let [g!whole (local$ name)] - (meta#in (partial_list g!whole - (.` (case (, g!whole) (, pattern) (, body))) - branches))) - - _ - (failure (..wrong_syntax_error (symbol ..let)))))) + (pattern + (macro (_ tokens) + (case tokens + (list#partial [_meta {.#Form (list [_ {.#Symbol ["" name]}] pattern)}] body branches) + (.let [g!whole (local$ name)] + (meta#in (list#partial g!whole + (.` (case (, g!whole) (, pattern) (, body))) + branches))) + + _ + (failure (..wrong_syntax_error (symbol ..let))))))) (def .public |> - (macro (_ tokens) - (case tokens - (pattern (partial_list [_meta {.#Form (list [_ {.#Symbol ["" name]}] [_ {.#Tuple steps}])}] body branches)) - (.let [g!name (local$ name)] - (meta#in (partial_list g!name - (.` (.let [(, g!name) (.|> (, g!name) (,* steps))] - (, body))) - branches))) - - _ - (failure (..wrong_syntax_error (symbol ..|>)))))) + (pattern + (macro (_ tokens) + (case tokens + (list#partial [_meta {.#Form (list [_ {.#Symbol ["" name]}] [_ {.#Tuple steps}])}] body branches) + (.let [g!name (local$ name)] + (meta#in (list#partial g!name + (.` (.let [(, g!name) (.|> (, g!name) (,* steps))] + (, body))) + branches))) + + _ + (failure (..wrong_syntax_error (symbol ..|>))))))) (def (name$ [module name]) (-> Symbol Code) @@ -240,17 +245,18 @@ ))) (def .public ` - (macro (_ tokens) - (case tokens - (pattern (partial_list [_meta {.#Form (list template)}] body branches)) - (do meta#monad - [pattern (untemplated_pattern template)] - (in (partial_list pattern body branches))) - - (pattern (list template)) - (do meta#monad - [pattern (untemplated_pattern template)] - (in (list pattern))) - - _ - (failure (..wrong_syntax_error (symbol ..`)))))) + (pattern + (macro (_ tokens) + (case tokens + (list#partial [_meta {.#Form (list template)}] body branches) + (do meta#monad + [pattern (untemplated_pattern template)] + (in (list#partial pattern body branches))) + + (list template) + (do meta#monad + [pattern (untemplated_pattern template)] + (in (list pattern))) + + _ + (failure (..wrong_syntax_error (symbol ..`))))))) |