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