Download presentation
Presentation is loading. Please wait.
Published byMervyn Powell Modified over 9 years ago
1
06/03/2007 The TXL Programming Language (4) 1 Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica ceccato@itc.it
2
06/03/2007 The TXL Programming Language (4)2 Working with Global Variables Global variables are a rich and powerful feature that can be used for many distinct purposes, including: - global tables. - multiple results from a rule. - “deep” parameters. - “message-passing” communication between rules in a rule set (e.g, avoiding interference).
3
06/03/2007 The TXL Programming Language (4)3 Setting Global Table Global tables can be set up using an export clause before the replace clause in the main rule of a program. Example: function main export Table [repeat table_entry] “Veggie” -> “Cabbage” “Fruit” -> “Apple” “Fruit” -> “Orange” replace [program] P [program] by P [R1] end function define table_entry [stringlit] -> [stringlit] end define
4
06/03/2007 The TXL Programming Language (4)4 Adding Table Entry Global tables can be modified by exporting a new binding for the table based on the imported original binding. function addTableEntry import Table [repeat table_entry] … construct newEntry [table_entry] “Veggie” -> “XXX” export Table Table [. newEntry] … end function Example:
5
06/03/2007 The TXL Programming Language (4)5 Searching in a Table Global tables can be easily queried using searching deconstructors. Example: deconstruct * [table_entry] Table Kind [stringlit] -> “Orange” The binding for “Kind” will be the [stringlit] “Fruit”. If no match were to be found, then the deconstructor would fail.
6
06/03/2007 The TXL Programming Language (4)6 Avoiding interference between rules function shiftByOne export Flag [id] ‘not_found replace [number] N [number] by N [replaceOneByTwo] [replaceTwoByThree] end function function replaceOneByTwo replace [number] 1 export Flag ‘found by 2 end function function replaceTwoByThree import Flag [id] deconstruct Flag ‘not_found replace [number] 2 by 3 end function 1 ---> 2 ---> 3 2 ---> 3 We want:
7
06/03/2007 The TXL Programming Language (4)7 Remarks Transformation of a list of contiguos if-statements in a case-statement. 1) The rule “transformTwoCases” is necessary. 2) It is not possible create a sequence [repeat T] in this way: - L elem with L [repeat T] and elem [T] - L L’ with L [repeat T] and L’ [repeat T] The Built-in function L [. elem] (or L [. L’]) can be used. If x>1 then … If x<1 then … case … endCase
8
06/03/2007 The TXL Programming Language (4)8 Exercises Adding to the Expr-language the construct ‘let’. Extending the Calculator.txl at the ‘Let-Expr-language’. - without global-variables (similar to pag. 19 slides 2) - using global-variables let begin x = 1; y = 2; end x+y+1. The output of Calculator.txl on the example will be 4. [number]
9
06/03/2007 The TXL Programming Language (4)9 Homework Extending the right part of the ‘let-declarations’ ([expression] instead of [number]). Extending the Calculator.txl at the ‘New-Let-Expr-language’. - without global-variables - using global-variables let begin x = 2; y = 1+x; x = 3 end x+y. The output of Calculator.txl on the example will be 6.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.