Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Martin Ward Newsgroups: comp.compilers Subject: Re: Best language for implementing compilers? Date: Tue, 19 Feb 2019 11:23:12 -0500 (EST) Organization: Compilers Central Lines: 49 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-02-013@comp.compilers> References: <02fa4eb9-584c-3472-8a31-6d2cec140863@gkc.org.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="23367"; mail-complaints-to="abuse@iecc.com" Keywords: tools Posted-Date: 19 Feb 2019 11:23:12 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com In-Reply-To: <02fa4eb9-584c-3472-8a31-6d2cec140863@gkc.org.uk> Xref: csiph.com comp.compilers:2165 On 12/02/19 17:27, Costello, Roger L. wrote: > Are there programming languages that have the pattern matching capability > shown in Kaz's last example? MetaWSL is a domain-specific language designed for implementing program transformations. Pattern matching is built into the language: to test if the currently selected item is an expression that is the binary product of two binary sums we can use an IFMATCH: IFMATCH Expression (~?a + ~?b) * (~?c + ~?d) THEN ... here a, b, c, and d are local variables ELSE ... the match failed ENDMATCH The pattern matching includes variable interpolation and back references, for example to match an IF statement whose ELSE body is the same as the THEN body: IFMATCH Statement IF ~?B THEN ~*S ELSE ~*=S FI THEN ... ELSE ... ENDMATCH The FILL construct builds a new item from the given components. A program transformation to reverse the branches of an IF statement is as simple as: IFMATCH Statement IF ~?B THEN ~*S1 ELSE ~*S2 FI THEN @Paste_Over(FILL Statement IF ~=(@Not(B)) THEN ~*S2 ELSE ~*S1 FI ENDFILL) ENDMATCH The function @Not takes a condition and returns a simplified version of the negation of the condition. So we negate the condition and interpolate the result into the IF statement that we are constructing. This is then "pasted over" the currently selected statement. The FermaT program transformation system is largely implemented in MetaWSL. The system and a graphical front end (called fme) can be downloaded from here: http://www.gkc.org.uk/fermat.html (Disclaimer: I implemented most of FermaT and currently maintain it). -- Martin Dr Martin Ward | Email: martin@gkc.org.uk | http://www.gkc.org.uk G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4