The TXL Programming Language 3 Mariano Ceccato FBK
The TXL Programming Language (3) Mariano Ceccato FBK Fondazione Bruno Kessler ceccato@fbk. eu 1
Rules and functions function 2 To 42 replace [number] 2 by 42 end function rule 2 To 42 replace [number] 2 by 42 end rule 2 ----> 42 3 2 6 2 78 4 2 Rules search the pattern! 2 ----> 42 3 2 6 2 78 4 2 ----> 42 6 42 78 4 42 2
Searching functions function 2 To 42 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 * 3
Deconstruct and searching functions rule vectorize. Scalar. Assignments replace [repeat statement] C 1 [statement] C 2 [statement] rest [repeat statement] deconstruct C 1 Example: V 1 [var] : = E 1 [expr]; deconstruct C 2 x: =x+1; <x, y>: = <x+1, t+4>; V 2 [var] : = E 2 [expr]; y: =t+4; where not E 2 [reference V 1] x: =x+1; where not y: =x+4; No! E 1 [reference V 2] construct Passign [statement] function reference V [variable] <V 1, V 2> : = <E 1, E 2>; match * [variable] by Passign rest V end rule end function 4
Extreme programming and TXL w Txl is ideally suited to extreme programming (XP). w Begin with an explicit set of testcases, and treat these as the specification of your transformation. w Program your transformation incrementally, as a sequence of successive approximations to the final result. w Actually run your partial transforms against the testcases to keep track of your progress and test as you go. w Always write the simplest possible transformation rules to achieve the result. w Begin each rule with an explicit example pattern and replacement, and generalize from there. 5
Example: XP and TXL (1) Step 1 – start with an explicit concrete example case. rule convert. Add. IJK replace [statement] add I to J giving K % COBOL by K = I + J; % PL/I end rule N. B. Test at every stage Step 2 – generalize by introducing pattern variables. rule convert. Add. Giving replace [statement] add I [Var] to J [Var] giving K [Var] by K = I + J; end rule 6
Example: XP and TXL (2) Step 3 – Add new cases. rule convert. Add. No. Giving replace [statement] add I [Var] to J [Var] by J = I + J; end rule N. B. Test at every stage Step 4 – Integrate different cases. rule convert. Adds replace [statement] Add. Statement [cobol_add_statement] by Add. Statement [convert. Add. No. Giving] [convert. Add. Giving] end rule 7
Exercises - Add to the “commands language”: 1) If “cond” then “statements” end. If 2) Case cond 1 ->statement 1 … cond. N->statement. N end. Case - Transform a list of contiguos “if-statements” in a casestatement If x>1 then x: =x+1 end. If If x=1 then x=0 end. If If x<1 then x: =x-1 end. If case x>1 -> x: =x+1; x=1 -> x=0; x<1 -> x: =x-1; end case 8
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 end. If If x=1 then x=0 end. If If x<1 then x: =x-1 end. If 9
- Slides: 9