jMax IRCAM - Centre Georges Pompidou

Expression



The Expression Interpreter

The expression interpreter is a classical expression interpreter based on a recursive descending parser; the code wait a volunteer to rewrite it with tools like yacc; in this case, all the evalution code should still be valid, only the parser would need to be rewritten.

The Interpreter use a classical structure based on an operand stack and a value stack, using operator priority to trigger operator evaluation

Refer to the user documentation for the exact syntax accepted.

Note: there is a small memory leak bug in the handling of array constants {1 2 3}: memory for the constant is allocated at each evaluation, and never freed.

Code Structure

The interpreter structure is kept in the fts_expression_state_t structure; one is allocated for each evalution, so that recursive calls are possible; together with the structure there are a number of functions handling the evalution machine stacks and status.

Then follows two version of the recursive parser, one for simple expressions (as define in the syntax) fts_expression_eval_simple and the other for full expressions fts_expression_eval_one.

These two functions call the fts_op_eval that apply the top of the stack operator to the top of the stack values, popping the values and popping the result; it implement standard C like type promotion, plus promotion to symbols; currently implements the scope operator, also if not supported by the variable algorithm.

Finally, the fts_expression_eval take an object description, interpret it as a list of expressions and evaluate them all; the result is stored in the fts_expression_state_t, together with some additional information, like the variables referenced during the evaluation, and the assignement constructs parsed at the end of the expression list.

The fts_expression_init initialize a number of essential tables for operator type (unary/binary) and priority; for the sake of efficency, symbols are reconized as operators by a kernel supported additional flag in the symbol structure itself, that work also as an index in the operator type and priority tables; these indexes are initialized in this function.

Related Files

  • expressions.c
  • expressions.h