flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > Question on extending List example |
Author |
|
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 |
|||
13 Mar 2010, 18:24 |
|
baldr 13 Mar 2010, 19:47
Siberian,
match =0, 0 {…} works as expected. You may find irp useful to iterate through list items too. |
|||
13 Mar 2010, 19:47 |
|
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. |
|||
13 Mar 2010, 20:10 |
|
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. |
|||
13 Mar 2010, 20:27 |
|
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. |
|||
14 Mar 2010, 13:36 |
|
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 |
|||
14 Mar 2010, 14:01 |
|
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. 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. 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. _________________ Previously known as The_Grey_Beast |
|||
14 Mar 2010, 14:23 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.