Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const lowerWhileStatement =
- {
- // Apologies in advance if this isn't actually valid D code:
- // This is a design sketch and I currently don't have a way to compile it.
- //
- // The Pattern template, PatternMatch template, and PatternHandler class
- // have not yet been written. This is an example of how I might expect
- // them to be used.
- //
- auto consumes = "while_statement";
- auto produces = "if_statement","goto","label");
- auto recognizer = Pattern!
- "WhileStatement has
- {
- // Capture the conditional expression (call it \"expr\") and
- // capture the loop body (call it \"statement\").
- .expression $expr;
- .statement $statement has
- {
- // Capture any continue/break statements.
- any_amount_of {
- any_amount_of .; // Same as .* in regexes.
- one_of
- {
- ContinueStatement $continues;
- BreakStatement $breaks;
- }
- }
- any_amount_of .;
- }
- }";
- auto action = (PatternMatch!(recognizer) m)
- {
- m.captures.add("uniqLoopAgain", getUniqueLabel(syntaxNode.enclosingScope))
- m.captures.add("uniqExitLoop", getUniqueLabel(syntaxNode.enclosingScope))
- // The "recognizes" clause defines m.getCapture!"continues" with:
- // "ContinueStatement $continues;"
- // That line appears in a repitition context ("any_amount_of") and is
- // therefore typed as an array.
- foreach( ref node; m.getCapture!"continues" )
- node.replaceWith( m, "GotoStatement has $uniqLoopAgain" )
- // Ditto for m.getCapture!"breaks" and "BreakStatement $breaks;".
- foreach( ref node; m.getCapture!"breaks" )
- node.replaceWith( m, "GotoStatement has $uniqExitLoop" )
- };
- auto synthesizer = Pattern!
- "Label has $uniqLoopAgain
- IfStatement has
- {
- OpNegate has $expr
- GotoStatement has $uniqExitLoop
- }
- $statement
- GotoStatement has $uniqLoopAgain
- Label has $uniqExitLoop
- ";
- return new PatternHandler(produces, consumes, recognizer, action, synthesizer);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement