nesdev.com
http://forums.nesdev.com/

Check first few chars of argument to ca65 macro?
http://forums.nesdev.com/viewtopic.php?f=2&t=16314
Page 1 of 2

Author:  GradualGames [ Sun Aug 06, 2017 10:53 am ]
Post subject:  Check first few chars of argument to ca65 macro?

I've been wondering if there's any way to improve on this:

Code:
                .if (.strat(.string(arg), 0) = 'e' && \
                     .strat(.string(arg), 1) = 'n' && \
                     .strat(.string(arg), 2) = 't' && \
                     .strat(.string(arg), 3) = 'i' && \
                     .strat(.string(arg), 4) = 't' && \
                     .strat(.string(arg), 5) = 'y' && \
                     .strat(.string(arg), 6) = '_')


It works, but it's damn ugly. I would expect to be able to do something like this:

Code:
    .if (.match (.left (7, {arg}), entity_))


But this doesn't work. Interestingly, this doesn't do what I expect either:

Code:
    .out .string(.left(7, {arg}))


I actually get the entire contents of arg.

Author:  thefox [ Sun Aug 06, 2017 11:45 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

You could write a macro:
Code:
.macro string_starts_with string, starts_with, ret
  ret .set 1
  .if .strlen(string) < .strlen(starts_with)
    ret .set 0
    .exitmac
  .endif
  .repeat .strlen(starts_with), i
    .if .strat(string, i) <> .strat(starts_with, i)
      ret .set 0
      .exitmac
    .endif
  .endrepeat
.endmacro

Usage:
Code:
.macro my_macro arg
  .local is_entity
  string_starts_with .string(arg), "entity_", is_entity
  .if is_entity
    .out "arg started with 'entity_'"
  .else
    .out "arg didn't start with 'entity_'"
  .endif
.endmacro


EDIT: Fixed a bug (macro accepted anything starting with "e", "en", "ent", etc).

Author:  GradualGames [ Sun Aug 06, 2017 11:49 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

I was considering trying something like that. But why the heck doesn't .left work? I use it, but it only ever works with "1" as the parameter. Anything else fails.

Author:  thefox [ Sun Aug 06, 2017 11:52 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

GradualGames wrote:
I was considering trying something like that. But why the heck doesn't .left work? I use it, but it only ever works with "1" as the parameter. Anything else fails.

.left works on tokens, not strings. A string is always a single token.

Author:  GradualGames [ Sun Aug 06, 2017 11:57 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

thefox wrote:
GradualGames wrote:
I was considering trying something like that. But why the heck doesn't .left work? I use it, but it only ever works with "1" as the parameter. Anything else fails.

.left works on tokens, not strings. A string is always a single token.


Is there some place in the documentation that clarifies the difference between strings, tokens, token lists, etc that I haven't yet spotted?

Author:  thefox [ Sun Aug 06, 2017 12:09 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

GradualGames wrote:
Is there some place in the documentation that clarifies the difference between strings, tokens, token lists, etc that I haven't yet spotted?

Not to my knowledge. There's a list of tokens at https://github.com/cc65/cc65/blob/maste ... 65/token.h.

Author:  GradualGames [ Sun Aug 06, 2017 12:13 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

thefox wrote:
You could write a macro:
Code:
.macro string_starts_with string, starts_with, ret
  ret .set 0
  .if .strlen(string) < .strlen(starts_with)
    .exitmac
  .endif
  .repeat .strlen(starts_with), i
    .if .strat(string, i) = .strat(starts_with, i)
      ret .set 1
      .exitmac
    .endif
  .endrepeat
.endmacro


Usage:
Code:
.macro my_macro arg
  .local is_entity
  string_starts_with .string(arg), "entity_", is_entity
  .if is_entity
    .out "arg started with 'entity_'"
  .else
    .out "arg didn't start with 'entity_'"
  .endif
.endmacro


What is ret .set ? I don't see this in the ca65 doc either.

Author:  GradualGames [ Sun Aug 06, 2017 12:18 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

Wait, I understand now. ret is an argument to the macro, .set is in fact documented.

Author:  GradualGames [ Sun Aug 06, 2017 1:09 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

I was able to get your macro to work, but I had to change a couple of things. I think you had the logic reversed (start true, but exit if false)

Code:

.macro string_starts_with string, sub_string
    starts_with .set 1
    .if .strlen(string) < .strlen(sub_string)
        starts_with .set 0
        .exitmac
    .endif
    .repeat .strlen(sub_string), i
        .if !(.strat(string, i) = .strat(sub_string, i))
            starts_with .set 0
            .exitmac
        .endif
    .endrepeat
.endmacro



Much nicer using this. Thanks for the idea!

Author:  thefox [ Sun Aug 06, 2017 2:20 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

GradualGames wrote:
I was able to get your macro to work, but I had to change a couple of things. I think you had the logic reversed (start true, but exit if false)

Oops, you're right. I edited my post.

Author:  GradualGames [ Mon Aug 07, 2017 10:08 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

I found a neat way to accomplish the same "starts with" logic on a macro argument without a custom helper macro:

Code:
    .if .xmatch (.left (1, .ident(.sprintf("%.7s", .string(arg)))), entity_)

Author:  thefox [ Mon Aug 07, 2017 11:29 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

GradualGames wrote:
I found a neat way to accomplish the same "starts with" logic on a macro argument without a custom helper macro:

Code:
    .if .xmatch (.left (1, .ident(.sprintf("%.7s", .string(arg)))), entity_)

Ah, that's cool. It can be simplified a little further:
Code:
.if .xmatch(.sprintf("%.7s", .string(arg)), "entity_")
  ; ...
.endif

Here's a version with a generic .define style "string_starts_with" macro based on the same idea:
Code:
.define string_starts_with(string, starts_with) .xmatch(.sprintf(.sprintf("%%.%ds", .strlen(starts_with)), string), starts_with)
; ...
.if string_starts_with .string(arg), "entity_"
  ; ...
.endif

Author:  GradualGames [ Mon Aug 07, 2017 11:47 am ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

Just for the heck of it, I'm trying to think if there would be a way to do an "ends with" as well with as little code. I haven't thought of anything yet. One would probably have to use .strat and .strlen(arg) and walk back indices from the end of the string in that case. Aw well can't have everything. :lol: Unless there's more voodoo possible with .sprintf I haven't thought of. C lets you do pointer arithmetic but I don't think ca65 is exposing that type of functionality even though it might be calling sprintf in the C runtime under the hood after forwarding various parameters along...

Author:  thefox [ Mon Aug 07, 2017 12:13 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

GradualGames wrote:
Just for the heck of it, I'm trying to think if there would be a way to do an "ends with" as well with as little code. I haven't thought of anything yet. One would probably have to use .strat and .strlen(arg) and walk back indices from the end of the string in that case. Aw well can't have everything. :lol: Unless there's more voodoo possible with .sprintf I haven't thought of. C lets you do pointer arithmetic but I don't think ca65 is exposing that type of functionality even though it might be calling sprintf in the C runtime under the hood after forwarding various parameters along...

Challenge accepted. I think this one should work:
Code:
.define string_ends_with(string, ends_with) .xmatch(.sprintf(.sprintf("%%.%ds%%s", (.strlen(ends_with) < .strlen(string)) * (.strlen(string) - .strlen(ends_with))), string, ends_with), string)

It works by stripping off .strlen(string) - .strlen(ends_with) characters from the end of string, then appends ends_with to it, and compares to the original string. Some trickery was needed to avoid putting a negative number in the format string in case ends_with is longer than string. (ca65 doesn't short circuit expression evaluation so can't use .and to safeguard it.)

EDIT: "shorter than" => "longer than"

Author:  GradualGames [ Mon Aug 07, 2017 12:23 pm ]
Post subject:  Re: Check first few chars of argument to ca65 macro?

thefox wrote:
GradualGames wrote:
Just for the heck of it, I'm trying to think if there would be a way to do an "ends with" as well with as little code. I haven't thought of anything yet. One would probably have to use .strat and .strlen(arg) and walk back indices from the end of the string in that case. Aw well can't have everything. :lol: Unless there's more voodoo possible with .sprintf I haven't thought of. C lets you do pointer arithmetic but I don't think ca65 is exposing that type of functionality even though it might be calling sprintf in the C runtime under the hood after forwarding various parameters along...

Challenge accepted. I think this one should work:
Code:
.define string_ends_with(string, ends_with) .xmatch(.sprintf(.sprintf("%%.%ds%%s", (.strlen(ends_with) < .strlen(string)) * (.strlen(string) - .strlen(ends_with))), string, ends_with), string)

It works by stripping off .strlen(string) - .strlen(ends_with) characters from the end of string, then appends ends_with to it, and compares to the original string. Some trickery was needed to avoid putting a negative number in the format string in case ends_with is shorter than string. (ca65 doesn't short circuit expression evaluation so can't use .and to safeguard it.)

:shock: Brilliant! Would that be possible without using C style macros in conjunction with the other facilities? Guessing not. Not sure I understand all the syntax there, particularly the double %%'s.

Page 1 of 2 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/