<<<<<<<<<<<<<<<<<

expr(prog_tools) Programming Tools expr(prog_tools)

NAME

expr

SUPERCLASS

object

DESCRIPTION

The expr class adds an infix arithmetic capability to the LYMB scripting language. Using the normal LYMB syntax, complex arithmetic is often difficult. Expr is a class that is not normally instantiated; instead it functions as a sort of temporary variable for arithmetic operations.

INSTANCE VARIABLES

value is the value of the last expression evaluated.

MESSAGES

= expression sets the value to the result of evaluating the expression argument.

? gets the value of the last expression evaluated.

EXPRESSION SYNTAX

A subset of the usual C-style arithmetic expressions are supported. Arithmetic operators are "+", "-", "*", "/", and "", with the usual precedence. Parentheses can be used to force desired expression evaluation.

Tokens of the form "id", where id is a LYMB identifier are assumed to be the names of objects that return numbers in response to "?" messages (typically scalar or logic objects).

Tokens of the form "id[expression]" are assumed to reference vector elements and are evaluated as if "[id @ expression ?]" had been typed. End_of_message is sent to the "id" object to reset the from and to indices.

Tokens of the form "id:iv" are assumed to reference an object ("id") and an instance variable of that object that returns a scalar ("iv").

Finally, tokens of the form "id(expression[,expression...])" are assumed to reference functions from the C math library. The supported functions are sin, cos, tan, fabs, abs (same as fabs), log, ln, log10, pow, rand, drand48, exp, sqrt, asin, acos, atan, and atan2.

EXAMPLE

Expr can be used to avoid the use of temporary scalars. Common operations such as multiplying a scalar by 2 require use of temporaries or the use of the push and pop

scalar messages.

scalar new: tmp; scalar new: s = 57; scalar new: t mod= [tmp = s * 2 ?]; -- using a temporary scalar new: u mod= [s push! * 2 pop?]; -- using push/pop scalar new: v mod= [expr = `s * 2' ?]; -- using expr

Referencing scalar or vector instance variables is easier using expr, as is applying the value of complex expressions to scalar instance variables. To perform the following assignment without expr, a temporary scalar object is required because the tot_slider class does not have a slip/ message.

tot_slider new: t1 slip= [expr = `verbose_window_controller:x / 2' ?] ;

scalar new: tmp = [verbose_window controller x?] / 2; tot_slider new: t2 slip= tmp ;

LYMB's message/argument protocol makes its arithmetic precedence counterintuitive. For instance, if you want to assign "47 + (2 * 8)" to a scalar, you must first rearrange the expression to be "2 * 8 + 47":

Lymb> scalar new: s; Lymb> s = 47 + 2 * 8 print:value; s: value= (392.000000) Lymb> s = [expr = `47 + 2 * 8' ?] print:value; s: value= (63.000000)

Using complex expressions as the index of a vector are much easier using expr:

vector new: v = (15,12,47,-900,6,22); scalar new: s = [expr = `v[rand() v:dimension + 1]' ?];

The examples here are simple and have easy workarounds. More complex problems occur often enough to often make it easier to use the expr class to do arithmetic, however.

SEE ALSO

scalar, string, s_vector, vector, parser


Please send comments and suggestions to
consult@rpi.edu