1

I am trying to write a sweet macro but have some troubles.

macro to_str {
  case { _ ($tok) } => {
    return [makeValue(unwrapSyntax(#{$tok}) + '=', #{ here })];
  }
}
macro foo {
    rule {($vars (,) ...) } => {
         $(to_str($vars) + $vars) (,) ...
    }
}

foo(a, b) should expand to

'a=' + a , 'b=' + b

And it works as expected.

But if one of the arguments is another macro, there will be some faults.

E.g. there is a macro named 'bar', then foo(a, bar) will cause below error:

SyntaxError: [macro] Macro `bar` could not be matched with `...`
80: foo(a, bar)

How can I fix this problem? Thanks

astropeak
  • 147
  • 1
  • 8

1 Answers1

2

The problem can be found here:

$(to_str($vars) + $vars) (,) ...

The reason you get the error is because the second $vars gets expanded. So when $vars binds to bar, it is expanded as a macro since it is defined as such. What you can do is treat it as an identifier so it doesn't get expanded as a macro:

macro to_str {
  case { _ ($tok) } => {
    return [makeValue(unwrapSyntax(#{ $tok }) + '=', #{ here })];
  }
}

macro to_ident {
  case { _ ($tok) } => {
    return [makeIdent(unwrapSyntax(#{ $tok }), null)];
  }
}

macro foo {
    rule {($vars (,) ...) } => {
         $(to_str($vars) + to_ident($vars)) (,) ...
    }
}
Anthony Calandra
  • 1,429
  • 2
  • 11
  • 17