General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
|
Solutions of common problems
StrArith is a wrapper for the strmath library routines
(see Character String Arithmetic of Library Reference Manual).
It is instantiated by
$/Tech/StrArith.gnrc +instance=NAME +referto=BASE :inst
where NAME is a prefix for the operator names and BASE is the
radix of the numbers to be manipulated.
If the instance parameter is omitted then the operator names have an
empty prefix; if the referto parameter is omitted then the radix is
10.
All operations exported by this module can be used in specifications of
type `.lido', `.init', and `.finl'. They can also be used in C
modules if the interface file `NAMEStrArith.h' is imported there.
Strings representing numbers are all stored in the string table.
Their form is common in programming languages:
[+/-][d*][.][d*][e[+/-]d*]
Here [] indicate optional parts,
+/- indicates that a sign may be present,
d indicates digits in the chosen radix,
and * indicates repetition.
The optional dot separates the integer and fractional parts of a number,
and e stands for an exponent marker.
The actual characters used to represent digits, signs,
fractional separators and exponent symbols are determined by default or by
settings established by the strmath operation
(see Character String Arithmetic of Library Reference Manual).
An instantiation of StrArith with instance parameter NAME
provides the following operations:
int NAMEStrAdd(int left, int right)
int NAMEStrSub(int left, int right)
int NAMEStrMul(int left, int right)
int NAMEStrDiv(int left, int right)
int NAMEStrQuo(int left, int right)
int NAMEStrRem(int left, int right)
int NAMEStrExp(int left, int right)
- Dyadic arithmetic operations.
The two arguments are string table indices representing the left and right
operands.
The result is the string table index of a string representing the result.
NAMEStrDiv is a real division, possibly yielding a result with a
fractional part.
NAMEStrQuo yields the integer quotient from the division and
NAMEStrRem yields the integer remainder from the division.
NAMEStrExp raises the first operand to the power given by the second
operand.
int NAMEStrNeg(int opnd)
int NAMEStrSqrt(int opnd)
- Monadic arithmetic operations.
The argument is a string table index representing the operand.
The result is the string table index of a string representing the result.
int NAMEStrNorm(int opnd, int oldradix, int newradix, char *symbs)
- Normalizes a value, performing radix conversion if necessary.
The first argument is a string table index representing the operand.
The second and third arguments are the radix values for the conversion.
The result is the string table index of a string representing the result.
Its format depends on the content of the fourth argument:
symbs=0
- Whole number and fraction parts separated by the defined
fractional separator unless the result can be expressed as an
integral value, exponent marker and exponent if the length would
exceed
integer_size digits.
symbs=""
- Sequence of digits if the length does not exceed
integer_size digits,
otherwise the empty string (which is represented by string table index 0).
symbs=non-empty quoted string
- A fractional separator is guaranteed to appear in the result.
The first character of the string is taken as the exponent marker.
If there are additional characters in the string, then they will
be taken as the fractional separator, the minus sign, and the plus
sign respectively.
(The characters normally defined for these purposes will be used
if the corresponding character does not appear in the string.)
Here is a fragment of a specification for a calculator that uses
StrArith to implement the arithmetic:
ATTR val: int;
RULE: Program ::= Expr COMPUTE
printf("%s\n", StringTable(Expr.val));
END;
RULE: Expr ::= Expr '+' Expr COMPUTE
Expr[1].val=StrAdd(Expr[2].val,Expr[3].val);
END;
...
RULE: Expr ::= '-' Expr COMPUTE
Expr[1].val=StrNeg(Expr[2].val);
END;
RULE: Expr ::= 'sqrt' '(' Expr ')' COMPUTE
Expr[1].val=StrSqrt(Expr[2].val);
END;
RULE: Expr ::= Constant COMPUTE
Expr.val=Constant;
END;
StrArith was instantiated without parameters for this example:
$/Tech/StrArith.gnrc :inst
|