From d613fa6f65960000d586abfbec39de8d20baca59 Mon Sep 17 00:00:00 2001 From: gilch Date: Sat, 27 May 2023 16:22:47 -0600 Subject: [PATCH] Add % -kwargs --- src/hissp/macros.lissp | 46 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/hissp/macros.lissp b/src/hissp/macros.lissp index 0f5cd608..0190e025 100644 --- a/src/hissp/macros.lissp +++ b/src/hissp/macros.lissp @@ -2023,8 +2023,10 @@ except ModuleNotFoundError:pass" The language is applied right-to-left, like function calls. Magic characters are - ``^`` DEPTH - Suffix to increase arity. Assume depth 1 otherwise. + ``%`` -kwargs + Suffix interprets top element as **kwargs. Write before any ``^``. + ``^`` -depth + Suffix increases arity. Assume depth 1 otherwise. Can be repeated. ``/`` DROP Pops (at depth) and discards. ``&`` PICK @@ -2069,7 +2071,9 @@ except ModuleNotFoundError:pass" control-word? X#(&& (op#is_ (type X) str) (.startswith X ":")) module-handle? X#(&& (op#is_ (type X) str) (.endswith X ".")) quotation? X#(&& (op#is_ (type X) tuple) (op#eq 'quote (get#0 X))) - method? X#(&& (op#is_ (type X) str) (.startswith X "."))) + method? X#(&& (op#is_ (type X) str) (.startswith X ".")) + kwargs? X#(.startswith X "%") + depth X#(.count X "^")) `(lambda (: :* $#args) (let ($#stack (list $#args)) (.reverse $#stack) @@ -2081,33 +2085,37 @@ except ModuleNotFoundError:pass" (control-word? obj) (module-handle? obj) (quotation? obj)) - (if-else Y + (if-else (depth Y) `(op#getitem (.pop ,'$#stack) ,obj) obj) - (if-else (method? obj) - (if-else Y - `(,obj ,@(XY#.#"X*Y" `((.pop ,'$#stack)) - (len Y))) - `((op#attrgetter ',.#"obj[1:]") - (.pop ,'$#stack))) - `(,obj ,@(XY#.#"X*(Y+1)" `((.pop ,'$#stack)) - (len Y))))))) - .#"/" `(.pop ,'$#stack ,(op#sub -1 (len Y))) - .#"&" `(.append ,'$#stack (,'XY#.#"X[-1-Y]" ,'$#stack ,(len Y))) - .#"@" `(.append ,'$#stack (.pop ,'$#stack ,(op#sub -2 (len Y)))) + (if-else (|| (depth Y) (not (method? obj))) + `(,obj ,@(XYZW#.#"(X+1-Y-Z)*W" + (depth Y) + (method? obj) + (kwargs? Y) + `((.pop ,'$#stack + ,(op#sub -1 (kwargs? Y))))) + : ,@(when (kwargs? Y) + `(:** (dict (.pop ,'$#stack))))) + `((op#attrgetter ',.#"obj[1:]") + (.pop ,'$#stack)))))) + .#"/" `(.pop ,'$#stack ,(op#sub -1 (depth Y))) + .#"&" `(.append ,'$#stack (,'XY#.#"X[-1-Y]" ,'$#stack ,(depth Y))) + .#"@" `(.append ,'$#stack (.pop ,'$#stack ,(op#sub -2 (depth Y)))) .#"[" `(.append ,'$#stack (-<>> (tuple (iter ,'$#stack.pop ','$#\])) ,@(when Y `(op#itemgetter (:<> (,'$#stack.pop)))))) .#"]" `(.insert ,'$#stack - (op#sub (len ,'$#stack) ,(len Y)) + (op#sub (len ,'$#stack) ,(depth Y)) ','$#\]) .#"*" `(.extend ,'$#stack (reversed (tuple (.pop ,'$#stack - ,(op#sub -1 (len Y)))))) + ,(op#sub -1 (depth Y)))))) : ()) - (reversed (re..findall "([/&@[\]*:]|(?:[^^`/&@[\]*:]|`[/&@[\]*:])+)(\^*)" - (hissp..demunge s)))) + (reversed (re..findall + "([/&@[\]*:]|(?:[^%^`/&@[\]*:]|`[%^/&@[\]*:])+)(%?\^*)" + (hissp..demunge s)))) (.pop $#stack))))) (defmacro _spy (expr file)