Tuesday, August 18, 2009

Evaluating string expressions in SCL

I have found a few cases where it would be better to allow a user to enter not just numbers, but some expression that can be evaluated to a number. An simple example of this is just evaluating calculations, like "10+2*(8/5)". Ordinarily, if someone entered this and you were expecting a number, you may have a real problem. But luckily QUEST provides a means for evaluating strings to Real values, or even to String values.

The method involves calling out to BCL to evaluate an SCL expression, returning either a String or Real value. The associated BCL commands are SCL_NUMERIC_EXPR and SCL_STRING. You just pass in the SCL expression and read the resulting BCL_MSG variable as the result.

Here are two SCL routines that make these BCL commands a bit easier to use:

BCL_VAR
bcl_msg : String
routine evaluate_scl_expr( the_string : String ) : Real
Var
bcl_err : Integer
Begin
bcl_err = bcl( "SCL_NUMERIC_EXPR('" + the_string + "')" )
return val( bcl_msg )
End

routine eval_scl_string( the_expr : String ) : String
Var
bcl_err : Integer
Begin
bcl_err = bcl( "SCL_STRING_EXPR('" + the_expr + "')" )
return bcl_msg
End

No comments: