asar icon indicating copy to clipboard operation
asar copied to clipboard

Add $= and %= Define Operators

Open Yoshifanatic1 opened this issue 5 years ago • 11 comments
trafficstars

Asar currently has the define operator "#=" that evaluates what is after it as a math expression and sets the result of it as the value for a define. Using the example from the asar manual:

!define = 10
!anotherdefine #= !define+1
; !anotherdefine now contains "11"

"#=" always sets the result as a decimal number so what I'm suggesting is that two new define operators ought to be added that do the same thing, except the output is a hexadecimal/binary number.

So, using the above example:

!define = 10
!anotherdefine $= !define+1
; !anotherdefine now contains "B"
!define = 10
!anotherdefine %= !define+1
; !anotherdefine now contains "1011"

There are times where having the instant evaluation property of "#=" would be useful, but I can't use it because I need the result to be in hexadecimal and the decimal conversion causes problems (ex. If you're using the result of a calculation for a nested define). The proposed syntax is meant to be consistent with how you denote a hexadecimal/binary number in asar.

Yoshifanatic1 avatar Nov 04 '20 17:11 Yoshifanatic1

Do you have a concrete usage example for this? Currently, I'm not quite sure what it could be needed for that isn't possible with current methods. More specifically, what is the exact reason for needing hex/binary in your situation? I can't think of a lot of cases where hex or binary text literals would be useful, except for actual text meant to be read by humans, but for that, we already have the print macros. So can you provide a simple example?

RPGHacker avatar Nov 04 '20 17:11 RPGHacker

Doesn't incbin take hex offsets? And things like levelasm could be

!idx = 0
while $!idx < 512
dl Level!idx
!idx $= $!idx+1
endif

instead of dl Level0, Level1, Level2, Level3, ...

Though I have no clue what binary would be useful for, other than consistency.

Alcaro avatar Nov 04 '20 19:11 Alcaro

The main reason for my suggestion is simply to make certain things easier to work with. There are times where it'd be useful to be able to do a calculation with some defines and use said calculation to reference different defines/labels/macros based on the result. Using decimal numbers is fine for many cases, but who uses decimal as part of a label/define/macro name for stuff like RAM addresses or SNES offsets? That would be confusing to work with.

Also, the reason I suggested a %= operator was simply for consistency.

Yoshifanatic1 avatar Nov 13 '20 02:11 Yoshifanatic1

I wonder if there is a better method to handle this. as defines are more substitutions than anything, so perhaps it would be better to have something like !$my_define to specify an attempt to convert to hex during resolution? Since this should only impact concatenation not calculation I feel its better applied at the site of use to better reflect intent. Syntax should still be up for debate of course.

p4plus2 avatar Nov 17 '20 05:11 p4plus2

Yeah, I was thinking something like that as well. Honestly, I was thinking about something like hex(!my_define), but that syntax would clash with functions and it would probably be too complex to disambiguate that in Asar (really wish functions were more flexible and could return strings, because that would make this a non-issue).

Anyways, I like your proposal of using !$my_define for hex literals, maybe along with !%my_define for binary literals. Putting the conversion into the define processor rather than into the math processor feels more appropriate to me, and putting the $ or % behind the ! would also mean that these symbols would probably only be seen by the define processor itself and wouldn't mess with anything else.

RPGHacker avatar Nov 17 '20 08:11 RPGHacker

To add to that Levelhex(!idx) would be absolute insanity compared to Level!$idx. And math functions don't return strings, they return numbers -- so that adds a really nasty complication in attempting to abuse functions anyways. So I think Level!$idx (or something similar) is indeed the path forwards.

Additionally, we need to figure out a few design factors such as what cases are errors versus warnings. To that note, we probably need to figure out if this is appropriate for all types of labels or only #= labels since those force mathematical expressions to be evaluated.

p4plus2 avatar Nov 17 '20 11:11 p4plus2

I went to implement this but it turns out !$ breaks arch spc700 syntax.

p4plus2 avatar Oct 29 '21 06:10 p4plus2

If that doesn't work, I can really only think of nasty workarounds.

!idx = hex(!idx)
Level!idx:

Either that, or adding another define operator (but even then it would still be a two-step process).

RPGHacker avatar Oct 29 '21 08:10 RPGHacker

the hex method doesn't really work with how asar is designed. Also, how should padding work? Should there be any? Operator approaches become more limited, for example labels can't be used when creating defines. Not sure thats strictly a problem mind you

p4plus2 avatar Oct 29 '21 08:10 p4plus2

Also of note: The SPC syntax actually creates several bugs, such as the inability to use functions with certain opcodes. So thats an issue to consider too.

p4plus2 avatar Oct 29 '21 08:10 p4plus2

Ah, right, I completely forgot that's not how functions work in Asar. Whoops.

RPGHacker avatar Oct 29 '21 08:10 RPGHacker