flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > fasm 1: list push/pop and shift/unshift

Author
Thread Post new topic Reply to topic
Liebranca



Joined: 14 Dec 2021
Posts: 3
Location: Buenos Aires, Argentina
Liebranca 04 Sep 2022, 12:04
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,
Code:
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,
Code:
 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:
Code:
0 : 1 :& 2
    


One may simply:
Code:
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:
Code:
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:
Code:
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:
Code:
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:
Code:
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 ;>
Post 04 Sep 2022, 12:04
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.