Function table

Top  Previous  Next

Scripts > Class elements and c++ instructions > Interpreted C++ instructions > Container > Function table

 

A function table is a map in which functions are sorted according to keys of strings. All functions of a list have the same type. There are, however, different function table types, depending on the type the functions return. (See the table above)

 

Syntax

 

mstrfun <identifier> ;

bool_mstrfun <identifier> ;

int_mstrfun <identifier> ;

uint_mstrfun <identifier> ;

dbl_mstrfun <identifier> ;

str_mstrfun <identifier> ;

node_mstrfun <identifier> ;

dnode_mstrfun <bezeichner> ;

 

Description

 

By means of these special containers the processing of a parse-tree is easier. To each label of a node a function can be assigned, which evaluates the node.

A function table shall be exemplified by mstrfun. mstrfun is similar to a map mstrstr: mstrfun, as it has an str key and another str as value. But the value here denotes the name of a class method.

Class methods are added to a function table by their names. All methods must have the same type. The type of a function results from its return type and its parameters. So all functions contained in a table must have the same return type and the same number and kind of parameters. An additional condition is, that the first Parameter has to be the type:  const node&. By this parameter the node is passed, which shall be evaluated.

If you define an mstrfun-variable as an element of the TextTransformer, the parameter field is shown after you have specified the mstrfun-type in the field for the return type. Here you have to specify the parameters in the same manner as usual for the functions.

The special type of the function table determines the return type of the functions contained in the table:

 

type of function table

return type

mstrfun

void

bool_mstrfun

bool

int_mstrfun

int

uint_mstrfun

unsigned int

dbl_mstrfun

double

str_mstrfun

str

node_mstrfun

node

 

bool add( LABEL, FUNCTION );

 

In the text field you now can insert the functions by means of the add-command.

All functions, which mstrfun shall contain, must be inserted here. It is not possible to create function-tables dynamically, while a transformation is running.

 

LABEL and FUNCTION can be passed either as strings  - e.g. "number" - or as identifiers - e.g. number. To pass a parameter as an identifier has the advantage that the syntax highlighting then is active and you can change to the according function by a mouse click.

When C++ code is created for the export, the identifiers will be translated to strings or functions addresses automatically.

It is not possible, to use general expressions for these parameters.

 

Example:

 

m_Eval might be an mstrfun, of void functions with only one node parameter, like

 

void VisitVariable(const node& xNode)

 

The first and the second of the following calls are correct, the third not:

 

m_Eval.add( "Variable", "VisitVariable");

m_Eval.add( Variable, VisitVariable);

 

str sLabel =  "Variable";

m_Eval.add( sLabel, "VisitVariable"); // error

 

 

Default function

 

Each function table must contain a default function, which will treat the nodes with labels, which are different from all key values in the table. The default function is inserted by the empty string "" as key:

 

Previous example continued:

 

void Default(const node& xNode)

m_Eval.add( "", "Default");

 

The extension of function tables by new functions is made easier by the Function-Table-Wizard

 

 

. visit(const node& xNode ... )

 

A function of the table is called by a call of the visit method of the table. Depending on the value of the label of the passed node, the call of visit will be redirected automatically to the function, which is assigned to the value of the label.

 

 

Previous example continued:

 

node n( "Variable", ...);

m_Eval.visit( n );

 

is equivalent with

 

if ( n.label() == "Variable")

   VisitVariable( n );

else

  Default( n );

 

 

 

In the exported c++ code mstrfun is implemented as CTT_Mstrfun.

 



This page belongs to the TextTransformer Documentation

Home  Content  German