Interpretation of line expressions as functions
Sometimes there is a need{requirement} to interpret in the program line expression as function. For example, at a spelling of the graph plotter it is required to process the received line so that in result function, and working with a speed necessary for repeated recalculation of coordinates of points has turned out. Actually the circle of similar problems{tasks} is much wider, he includes the programs using any variants of conditions of selection (for example, interpretation of conditions SQL of searches). This clause{article} is devoted to the decision of such problems{tasks}, the truth I shall not result ready source codes which can be downloaded and compiled, my problem{task} - to show one of opportunities of realization. For whom it is necessary and interesting, will write everything, that is necessary and will develop idea.
Examples in clause{article} will be written on PERL only because this language is floppy enough and it is not necessary to distract attention from a problem{task} on feature of realization under concrete language.
So all over again about production of a problem{task}: the program receives the line expression containing definition of mathematical or logic function of any complexity. For example, expression
1/(5*6 + x^0.5 + y*0.8) or
(A == B AND C! = 5) OR (D! = 'abc')
These expressions are complex{difficult}, but they can be reduced to a simple kind
[PER1] [Funkcija1] [PER2]
Operators of comparison also are functions, for example the operator! = receives values of 2 variables and returns "0" or "1". Let [Funkcija1] returns value [Znachenie1] then process of simplification of a line can conduct as follows:
$A == 34 AND $C! = 5
[PER1] = $A, [PER2] = 34, [Funkcija1] = "=="
After allocation of function it is replaced her{it} in an initial line with returned value:
[Znachenie1] AND $C! = 5,
[Znachenie1] AMD [Znachenie2],
[Znachenie3]
Last value also will be value of expression which is required to be found (Further job only with simple expressions) will be considered{examined}. However, if to use processing an initial line with replacement of function by its{her} value at concrete values of variables there will be serious losses of productivity. Therefore logical is to use not values, and indexes on them. For this purpose it is required to store{keep} values of variables, constants and values of functions.
* A file of arguments - constants
my @F_args_const;
my $Ch_args_const;
* An associative file of arguments - variables
my %zn_p;
* A file of arguments - results of functions
my $F_rez;
* A file of indexes on arguments of function
my @F_args_p;
* The index on function
my $F_Name_p;
* Two indexes on arguments of function
my $F_arg_p1;
my $F_arg_p2;
For those who is not familiar with syntax PERL I shall explain, that if before a name of a variable there is a sign "$" the variable is a scalar and can contain any individual value: a line, number, the index on object. If it is a file of scalars to address to each element of a file it is possible using a name of a file with the instruction{indication} before it{him} of a sign "$" (that is an element of a file - a scalar) and an index in square brackets after a name. If "%" it is an associative file, address to it{him} the same as and to usual, only instead of an index in square brackets line expression in figured is underlined.
It is necessary to note, that the line understands only once at filling files, therefore the subsequent calculations are carried out faster.
* Definition of function "=="
sub ravno_ch
{
my $str1 = $ _ [0];
my $str2 = $ _ [1];
if ($str1 == $str2) {return 1;};
return 0;
};
* The values received from an initial line
my $ARG1 = '$A';
my $ARG2 = '34';
my $FUNC = '==';
* Filling the data on function
if ($FUNC eq '==')
{
$F_Name_p = \ *ravno_ch;
};
* We remember VALUE $ARG2 in messive constants
$F_args_const [0] = $ARG2;
* Zapominiem the INDEX on the first argument of function
$F_arg_p1 = \ $zn_p {$ARG1};
* Zapominiem the INDEX on the second argument of function
$F_arg_p2 = \ $F_args_const [0];
Now some words about a call of function and about the task of its{her} arguments. With constant argument all is simple, his{its} value is saved in a file of constants and at each calculation of value of function it will be used. Value of a variable $A will be stored{kept} in an element of an associative file $zn_p {'$A'}. It will be possible to set it{him} easily before each calculation.
* We remember the current value of a variable
$zn_p {'$A'} = 12;
* We cause function
$F_rez = and $F_Name_p ($$ F_arg_p1, $$ F_arg_p2);
The sign "$$" before a name of variables F_arg_p1 and F_arg_p2 means, that at calculation it is necessary to take not values of these variables (in them indexes lay), and the data which they specify. The sign " and $ " before name F_Name_p means, that it is necessary to call function, the index on which is written down in a variable $F_Name_p.
Everything, that has been described in clause{article}, it is possible to use and for interpretation of complex{difficult} expressions, in that case they will be simply represented not by one function, and a set of functions (indexes on them are logical for writing down in a file and to cause them consistently). For certain there will be readers who will say, that realization of such problem{task} on PERL is not necessary, or she{it} can be realized with use of standard means of language. They will be certainly right, however the approach applied for the decision of a problem{task} allows to make analogue for example on SI (language supports all methods which were used in the given example)

|