View
29
Download
1
Category
Preview:
DESCRIPTION
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!. - PowerPoint PPT Presentation
Citation preview
02/03/2007 The TXL Programming Language (3)
1
The TXL Programming Language (3)
Mariano Ceccato
ITC-Irst
Istituto per la ricerca Scientifica e Tecnologica
ceccato@itc.it
02/03/2007 The TXL Programming Language (3) 2
Rules and functions
function 2To42 replace [number] 2 by 42end function
rule 2To42 replace [number] 2 by 42end rule
2 ----> 4232 6 2 78 4 2
2 ----> 4232 6 2 78 4 2 ----> 42 6 42 78 4 42
Rules search the pattern!Rules search the pattern!
02/03/2007 The TXL Programming Language (3) 3
Searching functions
function 2To42 replace * [number] 2 by 42end function
Note: change only *
2 ----> 4232 6 2 78 4 2 ----> 42 6 2 78 4 2
02/03/2007 The TXL Programming Language (3) 4
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 restend rule
function reference V [variable] match * [variable] Vend function
Example:
x:=x+1;y:=t+4; <x, y>:= <x+1, t+4>;
x:=x+1;y:=x+4; No!
02/03/2007 The TXL Programming Language (3) 5
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.
02/03/2007 The TXL Programming Language (3) 6
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/Iend 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
02/03/2007 The TXL Programming Language (3) 7
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
02/03/2007 The TXL Programming Language (3) 8
Exercises
- Add to the “commands language”:1) If “cond” then “statements” endIf2) Case cond1->statement1 … condN->statementN endCase- Transform a list of contiguos “if-statements” in a case-
statement
If x>1 then x:=x+1 endIfIf x=1 then x=0 endIfIf x<1 then x:=x-1 endIf
case x>1 -> x:=x+1; x=1 -> x=0; x<1 -> x:=x-1;end case
02/03/2007 The TXL Programming Language (3) 9
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 endIfIf x=1 then x=0 endIfIf x<1 then x:=x-1 endIf
Recommended