flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [fasmg] String detect with match directive

Author
Thread Post new topic Reply to topic
Jessé



Joined: 03 May 2025
Posts: 26
Jessé 06 May 2025, 00:09
Hello,

Can anyone tell me if is it possible to match strings (or not strings) using match directive?
I try a lot of different things, (like ="o", ="o=", &what), but none of them work.

Sample test:
Code:
macro printstr text
        display `text, 10
end macro

macro isString what
        display "Probing ", `what, ': -> '
        match 'o', what
                display "String is single quoted", 10
                db 'o', 0
        else match "o", what
                display "String is double quoted", 10
                db "o", 0
        else
                display "This isn't a string", 10
        end match
end macro

                        isString "This is"
                        isString 'This also is'
                        isString This is not
                        
                        display 10, 10
                        
                        printstr "Test 2 quotes."
                        printstr 'Test 1 quote.'
                        printstr No string
                        db -1,-1
    


It results in:

Quote:
─ ❯ fasmg matcher.fasm matcher
flat assembler version g.kp60
Probing 'This is': -> This isn't a string
Probing 'This also is': -> This isn't a string
Probing This is not: -> This isn't a string


'Test 2 quotes.'
'Test 1 quote.'
No string

1 pass, 2 bytes.


Thanks in advance, and sorry if there are any beginner mistakes.

_________________
jesse6
Post 06 May 2025, 00:09
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 06 May 2025, 06:04
MATCH is not the right tool. For fasmg, just like for fasm, a quoted string is a complete, indivisible token (see the fundamental syntax rules for a short description of fasmg's tokenization). Quoted string tokens, just like special characters, are matched literally (in this case it means that the content of the string must be the same), so "=" modifier has no additional effect on them.
Post 06 May 2025, 06:04
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 06 May 2025, 06:17
EQTYPE is the operator:
Code:
if val eqtype ""
    display "val is a string", 10
end if    
... assuming val is a definite.

If val can be anything then some corner cases need to be excluded.
This thread should be useful.
Post 06 May 2025, 06:17
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 06 May 2025, 08:45
bitRAKE wrote:
EQTYPE is the operator:
Code:
if val eqtype ""
    display "val is a string", 10
end if    
... assuming val is a definite.

If val can be anything then some corner cases need to be excluded.
This thread should be useful.
Note that EQTYPE compares the results of computed expressions, and there are other ways to produce a string type:
Code:
assert 'x' eqtype string 32
assert 'x' eqtype 13 bappend 10
assert 'x' eqtype 1 bswap 4    
Post 06 May 2025, 08:45
View user's profile Send private message Visit poster's website Reply with quote
Jessé



Joined: 03 May 2025
Posts: 26
Jessé 06 May 2025, 12:26
I've got some success while trying with ' if something eqtype "" '. Could not diferentiate single or double quoted strings yet, but this is not a problem, because I have noticed that strings are just strings to fasmg anyway. And that's fine for the application I will use it.
The only issue I had is when an invalid token is to be tested, match just doesn't match and ' if testme eqtype "" ' fails with an error.
Also not a problem at all, because invalid tokens has no meaning in any context, I suppose.
Post 06 May 2025, 12:26
View user's profile Send private message Reply with quote
Jessé



Joined: 03 May 2025
Posts: 26
Jessé 07 May 2025, 12:44
Another related topic: can I differentiate a label from a number in a macro?
Every test I do, I find that the assembler (at macro level) defines both types equally.
Post 07 May 2025, 12:44
View user's profile Send private message Reply with quote
dosmancer



Joined: 20 Feb 2025
Posts: 19
Location: Kingdom of Sweden
dosmancer 07 May 2025, 15:45
Jessé wrote:
Another related topic: can I differentiate a label from a number in a macro?
Every test I do, I find that the assembler (at macro level) defines both types equally.


Maybe like this:

Code:
define labels

struc (label) ? line&
        define labels.label
        . line
end struc

label1:

calminstruction test var
        transform var, labels
        jno not_label
        display "Is a label" bappend 13 bappend 10
        exit
    not_label:
        display "Not a label" bappend 13 bappend 10
end calminstruction

test label1
test 55    


I'm not sure if it's a good solution but it adds labels to a labels namespace then uses transform and if the transform occurred then it means var was found in the labels namespace.
Post 07 May 2025, 15:45
View user's profile Send private message Visit poster's website Reply with quote
Jessé



Joined: 03 May 2025
Posts: 26
Jessé 07 May 2025, 22:01
Interesting, I'll test it.
Actually, this will benefit my fastcall project, because I can give a 'memory operand' treatment to a label, and a 'value parameter' to a number.

Thanks. Smile

P.S.: it worked! Nice! I'll test it even further and adapt the concept to my project. Very Happy
Post 07 May 2025, 22:01
View user's profile Send private message Reply with quote
dosmancer



Joined: 20 Feb 2025
Posts: 19
Location: Kingdom of Sweden
dosmancer 07 May 2025, 23:47
Jessé wrote:
Interesting, I'll test it.
Actually, this will benefit my fastcall project, because I can give a 'memory operand' treatment to a label, and a 'value parameter' to a number.

Thanks. Smile

P.S.: it worked! Nice! I'll test it even further and adapt the concept to my project. Very Happy

Awesome. Note that there is another way labels can be created, with the label keyword so that also needs to be handled:
Code:
define labels

struc (label) ? line&
        define labels.label
        . line
end struc

; without this label2 and label3 will show "Not a label"
macro label? line&
        match a:b, line
            define labels.a
        else
            define labels.line
        end match
        label line
end macro

calminstruction test var
        transform var, labels
        jno not_label
        display "Is a label" bappend 13 bappend 10
        exit
    not_label:
        display "Not a label" bappend 13 bappend 10
end calminstruction

label1:
label label2
label label3:byte

test label1
test label2
test label3
test 55    
Post 07 May 2025, 23:47
View user's profile Send private message Visit poster's website Reply with quote
Jessé



Joined: 03 May 2025
Posts: 26
Jessé 08 May 2025, 04:03
Is there a way to export the result to a global variable or something similar?

Ex.:

_is_label = 0

test label1 ; _is_label = 1 if is label, _is_label = 0 if not.

I have tried here, without success for now.

Edit: I've got it right. Just a typing error (endif instead of end if). It's late here in Brazil, and I waiting for a feedback on my work while doing it.

Edit²: by the way, this is my working version (don't know if it could be done better):

Code:
define labels

struc (label) ? line&
        define labels.label
        . line
end struc

label1:

__is_label_ = 0

calminstruction test_para var
        transform var, labels
        jno not_label
        ; display "Is a label" bappend 13 bappend 10
        compute __is_label_, 1
        exit
    not_label:
        compute __is_label_, 0
        ; display "Not a label" bappend 13 bappend 10
end calminstruction
    


Also, I'll include your statement about the label directive.
Post 08 May 2025, 04:03
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 08 May 2025, 11:41
Jessé wrote:
Edit²: by the way, this is my working version (don't know if it could be done better)
You could also do it like this:
Code:
define labels

struc (label) ? line&
        define labels.label
        . line
end struc

label1:

TRUE := 1
FALSE := 0

calminstruction (out) test_para var
        transform var, labels
        jno not_label
        publish out, TRUE
        exit
    not_label:
        publish out, FALSE
end calminstruction

is_label test_para label1

if is_label
        display "Is a label",13,10
else
        display "Not a label",13,10
end if    
But having a dedicated global variable is faster, if you don't mind reserving a word. So your version has its advantages, too.

PS. For a check done with TRANSFORM it would be more appropriate to call it "contains_label" instead of "is_label".
Post 08 May 2025, 11:41
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8394
Location: Kraków, Poland
Tomasz Grysztar 08 May 2025, 14:37
Also please note that the feature of fasmg that was actually intended to help with distinguishing labels from raw numbers is the ORG directive with base containing terms defined with ELEMENT. See the second half of "How are the labels processed?" section of the auxiliary documentation. Look for the "if the labels need to be differentiated from absolute values" phrase and all the following examples.
Post 08 May 2025, 14:37
View user's profile Send private message Visit poster's website Reply with quote
Jessé



Joined: 03 May 2025
Posts: 26
Jessé 08 May 2025, 23:36
Nice, I had missed this part of the famsg manual.
Already saved it so I can study it.
Post 08 May 2025, 23:36
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.