flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to properly implement data type casting?

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
defining params, I would like to be able to cast data types.
For example (byte) 1 or simply byte 1 to a dword-sized variable.
This is the simplest example, but stretches to complex custom data types
For instance, I have string.ucs2 data type which converts string value to UCS-2. The part of data type casting macro is as follows:

Code:
. . .
          virtual at 0
            match (type) v, value
              type v
              align size
            else
              dbx size: value
            end match
            length = $
            load value:length from 0
          end virtual
. . .
    

Unfortunately, this breaks passing evaluated values like ( CONST shl 8 ) + 2
Adding match (=type?) v, value for each data type is obviously not an option, but checks like
Code:
if value relativeto 0
    

or
Code:
if value eqtype ''
    

throw error "extra characters on line"

How can I solve this?

Tomasz, maybe "extra characters on line" error should not be thrown at all when in IF param, simply returning false to allow further processing?
Post 18 Feb 2017, 13:41
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7752
Location: Kraków, Poland
Tomasz Grysztar
Allowing a freeform text in the arguments to IF is a can of worms that I wanted to avoid in case of fasmg, so IF only allows proper expressions and processing of freeform parameters is left to MATCH.

And your problem is really devious, too - considering that symbols like "byte" are actually numeric constants. So for example "(byte) +1" is a valid numeric expression with value 2, because it is the same as "(1)+1".

You could try making some symbolic definitions that you could then detect with MATCH:
Code:
cast.byte equ ++1--

match (x) v, arg
    match ++sz--,  cast.x
        size = sz
        value = v
    else
        value = arg
    end match
else
    value = arg
end match    
Post 18 Feb 2017, 14:01
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.