cmacro
cmacro copied to clipboard
Is the pattern matching broken?
Hi,
I am looking for c++ preprocessor / macro system. I have found this project, but it seems that it is either broken or has an odd semantics. For example, I would expect this macro not to match.
macro foo {
case {
match {
( $(a) )
}
template {
$(a)
}
}
}
foo()
However, it matches, yet $(a)
has undefined value, so we get an error
Unknown variable: 'a'.
It is possible to prevent this situation by matching against a pair of empty parentheses prior to the original pattern.
macro foo {
case {
match { () }
template { /**/ }
}
case {
match { ( $(a) ) }
template { $(a) }
}
}
foo ()
This time macro succeeds and it results in /**/
.
Unfortunately, it seems not to work with @splice
.
macro bar {
case {
match { ( $(block list) ) }
template { foo ( $(@splice block) ) }
}
}
bar (())
Once again we get Unknown variable: 'a'
error.
When we change foo
for some placeholder
then the macro succeeds and expands into placeholder ()
, so in case of foo
it should expand into foo ()
, which expands into /**/
, but it does not happen.
Is this a bug or intentional behaviour?
Sorry, I've been looking at this project today and fixing some of the outstanding issues. The way it behaves here is a little strange but ($(a))
should be equivalent to writing $(a)
without the brackets - i.e cmacro is treating these are expressions.
From what I've understood from your question, it looks like you are hoping that it is matching on the ()
itself... I don't think this is the intended to work in cmacro. However, the error Unknown variable: 'a'
is still a bug in itself.