From a0517211a4e107f013995cd10e9693acad6885a9 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 19 Aug 2022 01:14:18 -0400 Subject: Added functions with named parameters. --- stdlib/source/test/lux/control/function.lux | 4 +- stdlib/source/test/lux/control/function/named.lux | 73 +++++++++++++++ stdlib/source/test/lux/meta/macro.lux | 4 +- stdlib/source/test/lux/meta/macro/context.lux | 103 ++++++++++++++++++++++ 4 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 stdlib/source/test/lux/control/function/named.lux create mode 100644 stdlib/source/test/lux/meta/macro/context.lux (limited to 'stdlib/source/test') diff --git a/stdlib/source/test/lux/control/function.lux b/stdlib/source/test/lux/control/function.lux index b3e79f47d..a2ac4a355 100644 --- a/stdlib/source/test/lux/control/function.lux +++ b/stdlib/source/test/lux/control/function.lux @@ -21,7 +21,8 @@ ["[1][0]" mutual] ["[1][0]" inline] ["[1][0]" predicate] - ["[1][0]" variadic]]) + ["[1][0]" variadic] + ["[1][0]" named]]) (def .public test Test @@ -69,4 +70,5 @@ /inline.test /predicate.test /variadic.test + /named.test )))) diff --git a/stdlib/source/test/lux/control/function/named.lux b/stdlib/source/test/lux/control/function/named.lux new file mode 100644 index 000000000..064e2e8ab --- /dev/null +++ b/stdlib/source/test/lux/control/function/named.lux @@ -0,0 +1,73 @@ +(.require + [library + [lux (.except) + [abstract + [monad (.only do)]] + [control + ["[0]" try] + ["[0]" exception]] + [data + ["[0]" text]] + [math + ["[0]" random] + [number + ["n" nat]]] + [meta + ["[0]" code (.only) + ["<[1]>" \\parser]] + [macro + [syntax (.only syntax)] + ["[0]" expansion]]] + [test + ["_" property (.only Test)]]]] + [\\library + ["[0]" /]]) + +(def macro_error + (syntax (_ [macro .any]) + (function (_ compiler) + (when ((expansion.complete macro) compiler) + {try.#Failure error} + {try.#Success [compiler (list (code.text error))]} + + {try.#Success _} + {try.#Failure "OOPS!"})))) + +(/.def (+ left right) + (-> Nat Nat Nat) + (n.+ left right)) + +(def .public test + Test + (do [! random.monad] + [p0 random.nat + p1 random.nat] + (<| (_.covering /._) + (all _.and + (_.coverage [/.def] + (n.= (n.+ p0 p1) + (+ left p0 + right p1))) + (_.coverage [/.duplicate_parameters] + (text.contains? (the exception.#label /.duplicate_parameters) + (macro_error + (/.def .public (- _ _) + (-> Nat (List Nat) Nat) + (undefined))))) + (_.coverage [/.invalid_parameters] + (and (text.contains? (the exception.#label /.invalid_parameters) + (macro_error + (+ left p0))) + (text.contains? (the exception.#label /.invalid_parameters) + (macro_error + (+ right p1))) + (text.contains? (the exception.#label /.invalid_parameters) + (macro_error + (+ left p0 + right p1 + yolo p0))) + (text.contains? (the exception.#label /.invalid_parameters) + (macro_error + (+ left p0 + yolo p0))))) + )))) diff --git a/stdlib/source/test/lux/meta/macro.lux b/stdlib/source/test/lux/meta/macro.lux index b2b7e0eda..270040d6d 100644 --- a/stdlib/source/test/lux/meta/macro.lux +++ b/stdlib/source/test/lux/meta/macro.lux @@ -30,6 +30,7 @@ ["[0]" template] ["[0]" expansion]]] ["[0]" / + ["[1][0]" context] ["[1][0]" local] ["[1][0]" syntax] ["[1][0]" template] @@ -247,7 +248,8 @@ )) ..test|expansion - + + /context.test /local.test /syntax.test /template.test diff --git a/stdlib/source/test/lux/meta/macro/context.lux b/stdlib/source/test/lux/meta/macro/context.lux new file mode 100644 index 000000000..cbcdf60e0 --- /dev/null +++ b/stdlib/source/test/lux/meta/macro/context.lux @@ -0,0 +1,103 @@ +(.require + [library + [lux (.except) + [abstract + [monad (.only do)]] + [control + ["[0]" try] + ["[0]" exception]] + [data + ["[0]" text (.only) + ["%" \\format]]] + [math + [number + ["n" nat]]] + ["[0]" meta (.only) + ["[0]" static] + ["[0]" code (.only) + ["<[1]>" \\parser]] + [macro + [syntax (.only syntax)] + ["[0]" expansion]]] + [test + ["_" property (.only Test)]]]] + [\\library + ["[0]" /]]) + +(def macro_error + (syntax (_ [macro .any]) + (function (_ compiler) + (when ((expansion.complete macro) compiler) + {try.#Failure error} + {try.#Success [compiler (list (code.text error))]} + + {try.#Success _} + {try.#Failure "OOPS!"})))) + +(/.def [stack expression declaration] Nat) + +(with_expansions [ (static.random_nat)] + (<| static.expansion + (declaration ) + (` (def (,' declaration!) true)))) + +(def .public test + Test + (<| (_.covering /._) + (all _.and + (_.coverage [/.def] + (and declaration! + (with_expansions [ (static.random_nat)] + (<| static.expansion + (do meta.monad + [it (expression (` true))] + (in (list it))))))) + (_.coverage [/.peek /.peek' + /.push /.push'] + (with_expansions [ (static.random_nat)] + (n.= + (<| static.expansion + (do meta.monad + [_ (/.push ..stack) + actual (/.peek ..stack) + _ (/.pop ..stack)] + (in (list (code.nat actual)))))))) + (_.coverage [/.no_active_context] + (<| (text.contains? (the exception.#label /.no_active_context)) + macro_error + static.expansion + (do meta.monad + [top (/.peek ..stack)] + (in (list (code.nat top)))))) + (_.coverage [/.pop /.pop' /.pop''] + (with_expansions [ (static.random_nat) + (static.nat (++ ))] + (n.= + (<| static.expansion + (do meta.monad + [_ (/.push ..stack) + _ (/.pop ..stack) + _ (/.push ..stack) + actual (/.peek ..stack) + _ (/.pop ..stack)] + (in (list (code.nat actual)))))))) + (_.coverage [/.search /.search'] + (with_expansions [ (static.random_nat)] + (n.= + (<| static.expansion + (do meta.monad + [_ (/.push ..stack) + actual (/.search (n.= ) ..stack) + _ (/.pop ..stack)] + (in (list (code.nat actual)))))))) + (_.coverage [/.no_example] + (with_expansions [ (static.random_nat)] + (<| (text.contains? (the exception.#label /.no_example)) + macro_error + static.expansion + (do meta.monad + [_ (/.push ..stack) + actual (/.search (|>> (n.= ) not) ..stack) + _ (/.pop ..stack)] + (in (list (code.nat actual))))))) + ))) -- cgit v1.2.3