Advertisement
Guest User

Rough Draft of xdc While Lowering

a guest
Jul 11th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.95 KB | None | 0 0
  1.  
  2. const lowerWhileStatement =
  3. {
  4.     // Apologies in advance if this isn't actually valid D code:
  5.     //   This is a design sketch and I currently don't have a way to compile it.
  6.     //
  7.     // The Pattern template, PatternMatch template, and PatternHandler class
  8.     //   have not yet been written.  This is an example of how I might expect
  9.     //   them to be used.
  10.     //
  11.  
  12.     auto consumes = "while_statement";
  13.     auto produces = "if_statement","goto","label");
  14.    
  15.     auto recognizer = Pattern!
  16.         "WhileStatement has
  17.         {
  18.             // Capture the conditional expression (call it "expr") and
  19.             //   capture the loop body (call it "statement").
  20.             .expression $expr;
  21.             .statement  $statement has
  22.             {
  23.                 // Capture any continue/break statements.
  24.                 any_amount_of {
  25.                     any_amount_of .; // Same as .* in regexes.
  26.                     one_of
  27.                     {
  28.                         ContinueStatement $continues;
  29.                         BreakStatement    $breaks;
  30.                     }
  31.                 }
  32.                 any_amount_of .;
  33.             }
  34.         }";
  35.    
  36.     auto action = (PatternMatch!(recognizer) m)
  37.     {
  38.         m.captures.add("uniqLoopAgain", getUniqueLabel(syntaxNode.enclosingScope))
  39.         m.captures.add("uniqExitLoop", getUniqueLabel(syntaxNode.enclosingScope))
  40.        
  41.         // The "recognizes" clause defines m.getCapture!"continues" with:
  42.         //   "ContinueStatement $continues;"
  43.         // That line appears in a repitition context ("any_amount_of") and is
  44.         //   therefore typed as an array.
  45.         foreach( ref node; m.getCapture!"continues" )
  46.             node.replaceWith( m, "GotoStatement has $uniqLoopAgain" )
  47.        
  48.         // Ditto for m.getCapture!"breaks" and "BreakStatement $breaks;".
  49.         foreach( ref node; m.getCapture!"breaks" )
  50.             node.replaceWith( m, "GotoStatement has $uniqExitLoop" )
  51.     };
  52.    
  53.     auto synthesizer = Pattern!
  54.         "Label has $uniqLoopAgain
  55.         IfStatement has
  56.         {
  57.             OpNegate has $expr
  58.             GotoStatement has $uniqExitLoop
  59.         }
  60.         $statement
  61.         GotoStatement has $uniqLoopAgain
  62.         Label has $uniqExitLoop
  63.         ";
  64.  
  65.     return new PatternHandler(produces, consumes, recognizer, action, synthesizer);
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement