Hello,
Originally I wrote this as a question as I couldn't find anything through search, but since I figured out the answer as I was writing the examples I'm just going to share the solution ;>
While making lists by concatenating tokens, I separated the elements with colons as it carries no meaning in the particular syntax I'm working with and I need whitespaces as well as commas within the elements themselves.
And so I cat like so,
match any , list { list equ list : item}
match , list {list equ item}
Or the other way around (list equ item : list). I can then shift the first element as such,
match item =: next , list {
list equ next
status equ 1
}
match =0 item , status list {
list equ
}
And so I wondered: can I pop too?
The answer is yes, but it takes a little bit more logic as you need a unique pattern to mark the position you want to match up to. Given the following constant:
One may simply:
match first =: middle =:& last , list {...}
However, now the pattern needs to be rellocated whenever adding or removing elements. My 4am half-awake attempt at doing so:
macro List@$append list,elem& {
match any,list \{
local status
status equ 0
match items =:& last , list \\{
list equ items : last :& elem
status equ 1
\\}
match =0 =:& items , status list \\{
list equ items :& elem
\\}
\}
match ,list \{
list equ :& elem
\}
}
And then the pop:
macro List@$pop list,elem {
local status
status equ 0
match items =: pen =:& last , list \{
elem equ last
list equ items :& pen
status equ 1
\}
match =0 items =:& last , status list \{
elem equ last
list equ :& items
status equ 1
\}
match =0 =:& last , status list \{
elem equ last
list equ
\}
}
Reversing the operations, we get:
macro List@$unshift list,elem& {
match any,list \{
local status
status equ 0
match items =:& last , list \\{
list equ elem : items :& last
status equ 1
\\}
match =0 =:& items , status list \\{
list equ elem :& items
\\}
\}
match ,list \{
list equ :& elem
\}
}
^to append at the beginning, and then to remove first element:
macro List@$shift list,elem {
local status
status equ 0
match first =: pen =:& last , list \{
elem equ first
list equ pen :& last
status equ 1
\}
match =0 first =:& last , status list \{
elem equ first
list equ :& last
status equ 1
\}
match =0 =:& last , status list \{
elem equ last
list equ
\}
}
And so I get some more fine control of the list. I theorize that I *could* use patterns like this for indexing, but although useful that'd be much more complicated; I'm having trouble just beggining to think about it.
Also note that, as I have mentioned, this is just my brain at 4m. Though in quick tests they seem to work alright, the macros might still require some revisioning. But hopefully they provide a starting point for anyone looking to do something similar.
Cheers ;>