90 likes | 227 Views
The TXL Programming Language (3). Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica ceccato@itc.it. Rules and functions. function 2To42 replace [number] 2 by 42 end function. 2 ----> 42 3 2 6 2 78 4 2. Rules search the pattern!.
E N D
The TXL Programming Language (3) Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica ceccato@itc.it The TXL Programming Language (3)
Rules and functions function 2To42 replace [number] 2 by 42 end function 2 ----> 42 3 2 6 2 78 4 2 Rules search the pattern! rule 2To42 replace [number] 2 by 42 end rule 2 ----> 42 3 2 6 2 78 4 2 ----> 42 6 42 78 4 42 The TXL Programming Language (3)
Searching functions function 2To42 replace * [number] 2 by 42 end function 2 ----> 42 3 2 6 2 78 4 2 ----> 42 6 2 78 4 2 Note: change only * The TXL Programming Language (3)
Deconstruct and searching functions rule vectorizeScalarAssignments replace [repeat statement] C1 [statement] C2 [statement] rest [repeat statement] deconstruct C1 V1 [var] := E1 [expr]; deconstruct C2 V2 [var] := E2 [expr]; where not E2 [reference V1] where not E1 [reference V2] construct Passign [statement] <V1, V2> := <E1, E2>; by Passign rest end rule Example: x:=x+1; y:=t+4; <x, y>:= <x+1, t+4>; x:=x+1; y:=x+4; No! function reference V [variable] match * [variable] V end function The TXL Programming Language (3)
Extreme programming and TXL • Txl is ideally suited to extreme programming (XP). • Begin with an explicit set of testcases, and treat these as the specification of your transformation. • Program your transformation incrementally, as a sequence of successive approximations to the final result. • Actually run your partial transforms against the testcases to keep track of your progress and test as you go. • Always write the simplest possible transformation rules to achieve the result. • Begin each rule with an explicit example pattern and replacement, and generalize from there. The TXL Programming Language (3)
Example: XP and TXL (1) Step 1 – start with an explicit concrete example case. rule convertAddIJK replace [statement] add I to J giving K % COBOL by K = I + J; % PL/I end rule Step 2 – generalize by introducing pattern variables. rule convertAddGiving replace [statement] add I [Var] to J [Var] giving K [Var] by K = I + J; end rule N.B. Test at every stage The TXL Programming Language (3)
Example: XP and TXL (2) Step 3 – Add new cases. rule convertAddNoGiving replace [statement] add I [Var] to J [Var] by J = I + J; end rule Step 4 – Integrate different cases. rule convertAdds replace [statement] AddStatement [cobol_add_statement] by AddStatement [convertAddNoGiving] [convertAddGiving] end rule N.B. Test at every stage The TXL Programming Language (3)
Exercises - Add to the “commands language”: • If “cond” then “statements” endIf • Case cond1->statement1 … condN->statementN endCase - Transform a list of contiguos “if-statements” in a case-statement case x>1 -> x:=x+1; x=1 -> x=0; x<1 -> x:=x-1; end case If x>1 then x:=x+1 endIf If x=1 then x=0 endIf If x<1 then x:=x-1 endIf The TXL Programming Language (3)
Homework • Reading “The Guided Tour” (website www.txl.ca). • Transform a case-statement in a list of contiguos “if-statements”. case x>1 -> x:=x+1; x=1 -> x=0; x<1 -> x:=x-1; end case If x>1 then x:=x+1 endIf If x=1 then x=0 endIf If x<1 then x:=x-1 endIf The TXL Programming Language (3)