From 4965597043aca57a05760113f1851e96dad1eaf8 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 14 Mar 2022 06:14:57 -0400 Subject: Leaner syntax for the "for" macro. --- stdlib/source/library/lux.lux | 99 ++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 49 deletions(-) (limited to 'stdlib/source/library/lux.lux') diff --git a/stdlib/source/library/lux.lux b/stdlib/source/library/lux.lux index 09f0a9e4c..6120a52be 100644 --- a/stdlib/source/library/lux.lux +++ b/stdlib/source/library/lux.lux @@ -2584,6 +2584,17 @@ {#None} {#Some [tokens (list)]})) +(def:' .private (maybeP itP tokens) + (All (_ a) + (-> (Parser a) + (Parser (Maybe a)))) + (case (itP tokens) + {#Some [tokens it]} + {#Some [tokens {#Some it}]} + + {#None} + {#Some [tokens {#None}]})) + (def:' .private (tupleP itP tokens) (All (_ a) (-> (Parser a) (Parser a))) @@ -4882,52 +4893,41 @@ (target_pick target options' default))))) (macro: .public (for tokens) - (do meta_monad - [target ..target] - (case tokens - (^ (list [_ {#Tuple options}])) - (case (pairs options) - {#Some options} - (target_pick target options {#None}) - - {#None} - (failure (..wrong_syntax_error (symbol ..for)))) - - (^ (list [_ {#Tuple options}] default)) - (case (pairs options) - {#Some options} - (target_pick target options {#Some default}) - - {#None} - (failure (..wrong_syntax_error (symbol ..for)))) + (case (..parsed (..andP (..someP (..andP ..anyP ..anyP)) + (..maybeP ..anyP)) + tokens) + {.#Some [options default]} + (do meta_monad + [target ..target] + (target_pick target options default)) - _ - (failure (..wrong_syntax_error (symbol ..for)))))) + {.#None} + (failure (..wrong_syntax_error (symbol ..for))))) ... TODO: Delete "scope_type_vars" (including the #scope_type_vars Lux state) and ":parameter" ASAP. -(for ["{old}" (as_is (def: (scope_type_vars state) - (Meta (List Nat)) - (case state - [..#info info ..#source source ..#current_module _ ..#modules modules - ..#scopes scopes ..#type_context types ..#host host - ..#seed seed ..#expected expected ..#location location ..#extensions extensions - ..#scope_type_vars scope_type_vars ..#eval _eval] - {#Right [state scope_type_vars]})) - - (macro: .public (:parameter tokens) - (case tokens - (^ (list [_ {#Nat idx}])) - (do meta_monad - [stvs ..scope_type_vars] - (case (..item idx (list#reversed stvs)) - {#Some var_id} - (in (list (` {.#Ex (~ (nat$ var_id))}))) - - {#None} - (failure (text#composite "Indexed-type does not exist: " (nat#encoded idx))))) +(for "{old}" (as_is (def: (scope_type_vars state) + (Meta (List Nat)) + (case state + [..#info info ..#source source ..#current_module _ ..#modules modules + ..#scopes scopes ..#type_context types ..#host host + ..#seed seed ..#expected expected ..#location location ..#extensions extensions + ..#scope_type_vars scope_type_vars ..#eval _eval] + {#Right [state scope_type_vars]})) + + (macro: .public (:parameter tokens) + (case tokens + (^ (list [_ {#Nat idx}])) + (do meta_monad + [stvs ..scope_type_vars] + (case (..item idx (list#reversed stvs)) + {#Some var_id} + (in (list (` {.#Ex (~ (nat$ var_id))}))) - _ - (failure (..wrong_syntax_error (symbol ..$))))))] + {#None} + (failure (text#composite "Indexed-type does not exist: " (nat#encoded idx))))) + + _ + (failure (..wrong_syntax_error (symbol ..$)))))) (as_is)) (macro: .public (using _imports) @@ -4946,13 +4946,14 @@ =module (` ("lux def module" (~ =imports)))] g!_ (..generated_symbol "")] (in {#Item =module - (for [... TODO: Remove this hack once Jython is no longer being used as the Python interpreter. - ... Without it, I get this strange error - ... {library/lux/tool/compiler/language/lux/generation.no_buffer_for_saving_code} - ... Artifact ID: 0 - ... Which only ever happens for the Python compiler. - "Python" (list& (` ("lux def" (~ g!_) [] #0)) - =refers)] + (for "Python" + ... TODO: Remove this hack once Jython is no longer being used as the Python interpreter. + ... Without it, I get this strange error + ... {library/lux/tool/compiler/language/lux/generation.no_buffer_for_saving_code} + ... Artifact ID: 0 + ... Which only ever happens for the Python compiler. + (list& (` ("lux def" (~ g!_) [] #0)) + =refers) =refers)}))) (def: (embedded_expansions code) -- cgit v1.2.3