flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Question on extending List example

Author
Thread Post new topic Reply to topic
Siberian



Joined: 09 Mar 2010
Posts: 14
Siberian 13 Mar 2010, 17:59
Hello, again.

This question concerns list example posted somewhere on this board. The main idea is redefining list constant each time new item is appended.
But is it possible to detect for any item if it is already contained in the list before appending it?
I was able to produce one implementation for items which are quoted strings (after studying includeonce example). But main interest is to do it with any item type - simple match doesn't help, or am I doing something wrong?
Post 13 Mar 2010, 17:59
View user's profile Send private message Reply with quote
Siberian



Joined: 09 Mar 2010
Posts: 14
Siberian 13 Mar 2010, 18:24
Small example to explain what I have in mind:
Code:
macro ListContains item, flagVar {
     flar equ 0                    
      match item,             list \{ flagVar equ 1 \}
        match item tail,        list \{ flagVar equ 1 \}
        match head item,        list \{ flagVar equ 1 \}
        match head item tail,   list \{ flagVar equ 1 \}
}

; case 1
list equ '1', '2', '4', '5'

ListContains '2', flag           ; flag equ 1 - correct
ListContains '3', flag              ; flag equ 0 - correct
ListContains '5', flag              ; flag equ 1 - correct
ListContains 'junk', flag   ; flag equ 0 - correct

; case 2
list  equ     1, 2, 4, 5

ListContains 2, flag          ; flag equ 1 - correct
ListContains 3, flag          ; flag equ 1 - incorrect
ListContains 5, flag                ; flag equ 1 - correct
ListContains junk, flag               ; flag equ 1 - incorrect
    
Basically, every macro match in second case evaluates. I'm aware of the reason, but is there a way to implement the solution for non quoted items in the list?
Post 13 Mar 2010, 18:24
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 13 Mar 2010, 19:47
Siberian,

match =0, 0 {…} works as expected. You may find irp useful to iterate through list items too.
Post 13 Mar 2010, 19:47
View user's profile Send private message Reply with quote
Siberian



Joined: 09 Mar 2010
Posts: 14
Siberian 13 Mar 2010, 20:10
It really works!

Thanks, baldr.

I don't know why, but I thought that expression at "=" sign within a match pattern will be always treated as direct value to compare with. But I guess macro argument is dereferenced in that case as well.
Post 13 Mar 2010, 20:10
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 13 Mar 2010, 20:27
Siberian,

You're right, that's the way macro parameters work. «=» is a match feature, not macro's.

Be careful: match 1,0 { db 1 } could surprise you. Wink
Post 13 Mar 2010, 20:27
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 14 Mar 2010, 13:36
restore the flagVar so it doesn't create unexpected bugs. Also you can use "define flagVar +" for the 'not found' signal and "define flagVar" (empty) for the 'found' signal, if you want.

But you have to restore it in each of the 4 matches (before defining), AND at the end of the macro.
Post 14 Mar 2010, 13:36
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Mar 2010, 14:01
Borsuc,

Something like this?
Code:
struc reequ [val] {
common
  restore .
  . equ val
}

macro ListContains item, contains {
  contains equ 0
  match l, list \{
    irp i, l \\{
      match =item,i \\\{ contains reequ 1 \\\}
    \\}
  \}
}

list    equ     1, 2, 4, 5

ListContains 2, flag            ; flag equ 1 - correct
display "0"+flag
ListContains 3, flag            ; flag equ 1 - incorrect
display "0"+flag
ListContains 5, flag            ; flag equ 1 - correct
display "0"+flag
ListContains junk, flag         ; flag equ 1 - incorrect
display "0"+flag    
Adding restore contains before contains equ 0 has its pros and cons. At the end of ListContains macro it will destroy returned value.
Post 14 Mar 2010, 14:01
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 14 Mar 2010, 14:23
Yes that's the idea, but...
baldr wrote:
Adding restore contains before contains equ 0 has its pros and cons. At the end of ListContains macro it will destroy returned value.
you add it at the end of the macro, not at the beginning. after the matches.

HOWEVER you do NOT add it in THIS particular case because you use the flag OUTSIDE the macro -- if you only used it INSIDE the macro, then you should restore it... but here it is not needed to do it at the end of the matches. Wink

I'm only saying this because I had some annoying bugs come out of it, "leaking" variables and stuff. took me a while to figure out. Sad

_________________
Previously known as The_Grey_Beast
Post 14 Mar 2010, 14:23
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.