Title: The TXL Programming Language 4
1The TXL Programming Language (4)
- Filippo Ricca
- ITC-Irst
- Istituto per la ricerca Scientifica e Tecnologica
- ricca_at_itc.it
2Working 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).
3Setting 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 -gt Cabbage
Fruit -gt Apple
Fruit -gt Orange replace program
P program by P
R1 end function
define table_entry stringlit -gt
stringlit end define
4Adding 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 -gt XXX
export Table Table . NewEntry
end function
Example
5Searching in a Table
- Global tables can be easily queried using
searching deconstructors.
deconstruct table_entry Table Kind
stringlit -gt Orange
Example
- The binding for Kind will be the stringlit
Fruit. If no match were to be found, then the
deconstructor would fail.
6Avoiding 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
We want
1 ---gt 2 ---gt 3 2 ---gt 3
function replaceTwoByThree import Flag id
deconstruct Flag not_found
replace number 2 by
3 end function
7Remarks
- 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 xgt1 then If xlt1 then case endCase
case endCase case endCase
case endCase
8Exercises
- 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 xy1.
number
The output of Calculator.txl on the example will
be 4.
9Homework
- 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 1x x 3 end
xy.
The output of Calculator.txl on the example will
be 4.