-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pattern matching API / DSL for rewrite rules #20
Comments
I love the go variant (e.g. https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/_gen/generic.rules) What about making a source generator that generates one pass for each rules definition file? |
We could also use Scheme, because it's much easier to parse and compile it also to a custom pass for each file |
Yeah the go ruleset syntax looks pretty nice, if we have a bunch of rule files then source generators might just be the best way to go about it. If the rulesets allow calling C# code directly, having a Match() style API would probably be redundant. |
But we should also allow matching and replacing without a source generator maybe something like this: I am going to implement this, if you agree |
I had something like that in mind as well, I was thinking of maybe using string interpolation handlers + source interceptors to capture the values, something like: (I actually had an working prototype using handlers this way but I don't think I have it anymore, and iirc the argument holes don't actually allow out qualifiers, the variables were taken by I would be happy to take any implementation for this feature though! |
I'm not sure how your original proposal would return the values, but I quickly sketched something with the interpolators and it seems like they could work reasonably well. I imagine the source generator could just capture the entire string at once to generate/intercept with an specialized matcher, so an actual runtime implementation would not be needed. void Foo(Instruction inst)
{
Value? x = null, y = null;
if (Match(inst, $"add (ConstInt {x}, {y})")) {
}
}
public static bool Match(Value value, ValueMatchInterpolator pattern) { return false; }
[InterpolatedStringHandler, EditorBrowsable(EditorBrowsableState.Never)]
public ref struct ValueMatchInterpolator
{
public ValueMatchInterpolator(int literalLength, int formattedCount) { }
public void AppendLiteral(string value) { }
public void AppendFormatted<T>(in T value) {
Unsafe.AsRef(in value);
}
} I guess even something like |
May it would be easier to discuss things if we connect on discord or similar |
I'm also dubiousconst282 on discord |
If we decide to apply simple pattern based rewrites, we'll need a proper matching API since C#'s is not very scalable.
An API like LLVM's would be nice and it's possible but it may come at a cost, depending on how it is implemented:
It may be worth considering an extension-based approach instead, see https://www.reddit.com/r/csharp/comments/83ep10/comment/dvhdjss
Other:
The Go compiler has a nice DSL for rewrite rules, which we may also want to copy from :')
The text was updated successfully, but these errors were encountered: