Enter Home Page

JUEXPR(3f)

Version 3.0.0:Use at your own risk.

SYNOPSIS

The JUEXPR(3f) library evaluates CHARACTER strings containing FORTRAN-like expressions and returns numeric and string values. It supports named variables and most ANSI FORTRAN 77 functions.

   f77 juexpr.f mycalc.f -o mycalc

Using the JUEXPR(3f) interface it is easy to make free-format and order-independent input interfaces where values can be expressions and variable names instead of simple numbers. It has been used in lieu of NAMELIST in many programs; and as a basic building block in creating language-like input formats.

There are much more powerful and modern expression parsers available now-adays, but this routine has proven to be very portable and simple to maintain; and is still useful.

The library is basically written in FORTRAN 77; it uses no recursion and no dynamically allocated variables and is very portable. It does not support user-defined arrays, handling of complex numbers, or interpreted user-defined functions.

The parser is not intended to be used to generate a large number (hundreds of thousands) of calculations. It was written to be portable, not optimal. The library has been used on many operating systems, including NOS, NOS/VE, VMS, COS, Aegis, UNICOS, SunOS, HP-UX, IRIX64, AIX, Solaris, Ultrix, Tru64, OpenBSD, Linux, CygWin, ...

Perhaps the simplest example is a loop that reads input lines and calls the calculator using the convenience function RNUM0(3f):

      PROGRAM simple
      CHARACTER*255 line
      REAL*8 rnum0
      WRITE(*,*)'Enter numeric expressions'
1     CONTINUE
         READ(*,'(A)',ERR=1,IOSTAT=ios,END=999) line
         WRITE(*,*)rnum0(line)
      GOTO 1
999   CONTINUE
      END

A run of the example program might look like this:

300+4/3-1
 300.33334
sin(d2r(90))
 1.
f2c(210)
 98.888885
a=10
 10.
b=20
 20.
a/b
 0.5
?+a+b
 30.5

DESCRIPTION

A summary of the syntax rules for the expressions follows:

VARIABLE NAMES

Names must be 1 to 20 characters long, and are case-sensitive. Up to 2345 names are permitted. Numeric variable names should be composed of the letters a-z and underscores and numbers. String variables are similar but start with a dollar sign($). Names must not end in a "digit-E" combination. For example:

   A=sin(3.1416/2)
   big=200.333E200
   $name="Thomas Jefferson"

Variables may be defined by equating them to an expression. To define or redefine a variable called FRED, simply enter:

     > FRED=300*4/500

The last value assigned to a variable will be used to evaluate the expression on the left of the equals sign when this expression redefines the variable. For example:

     > A=2
       2
     > A
       2
     > A=A+A
       4
     > A=A+A
       8

To allow FORTRAN-type E-format numeric entry and yet not cause the calculator routine to do an excessive amount of checking, a variable name ending in the letter E must not have a digit (012345789) in front of that ending E. Attempting to define such a variable name will produce an error. This limitation prevents the calculator from becoming confused by whether 12E+3 is a variable called 12E plus 3 or the exponential number 12E3=12000.

CURRENT VALUE

The variable name '?' is automatically set by the program to contain the last calculated value. This current-value register may be used like any variable or number. It is 0 at program initialization. Example:

       > (300+500)
         800
       > (1/4)*?
         200
       > ?+?
         400

THE X AND Y ARRAYS

Two arrays called X and Y are available that can contain up to 3333 values each. The arrays are originally initialized to all zeros. To set values in the arrays, use the xstore (or ystore) command. The format of the commands is

xstore(start,ex1,ex2,ex3)
ystore(start,ex1,ex2,ex3)
where start=array address to start storing at and ex(i) are expressions.

The current value is assigned the last value stored.

In addition there are similar string arrays and functions that can hold up to 50 255-character strings:

For example, to store into the locations 10,11,and 12 the values 1/10,2/10, and 3/10, the following could be entered:

     xstore( 10 , 1/10 , 2/20 , 3/10 )
             ^
             !
             *-------Start storing evaluated expressions sequentially,
                     beginning at x(10).

REFERENCING AN ARRAY VALUE

The values stored into the arrays may be referenced by subscript. For example:

   > xstore(1,10,20,30)
     30
   > fred=x(1)+x(2)+x(3)
     60
NOTES:
  1. x and y array values cannot be used on the left of equal signs.
             x(10)=5   #  IS ILLEGAL 
       
  2. The current value is set to the value of the last expression by the xstore and ystore commands

AVAILABLE FUNCTIONS

conversion functions

logical functions

For example:
  a=if(ge(b,c),a,d)

means return a if b is greater than or equal to c else return d.

lexical logical functions

miscellaneous functions

String-related

ANSI FORTRAN INTRINSIC MATH FUNCTIONS

The common FORTRAN 77 intrinsic functions are supported.

FORTRAN Functions
abs acos aint anint asin
atan atan2 cos cosh dim
exp frac idnint int log
log10 max min mod nint
real sign sin sinh sqrt
tan tanh

MISCELLANEOUS COMMANDS

Displaying variable values: dump
The current value and all defined variable names are displayed via the dump command.
Listing Available Functions: funcs
A display of all available functions can be obtained when executing JUEXPR by entering the command 'funcs'. No descriptions are provided.

Known bugs and limitations