Skip to content

Commit

Permalink
Add % -kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
gilch committed May 27, 2023
1 parent efc53a4 commit d613fa6
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/hissp/macros.lissp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d613fa6

Please sign in to comment.