MY CALCULATOR 16 December 1987 written by: Thomas Wolfe Jet Propulsion Laboratory Mail Stop 510/202 4800 Oak Grove Drive Pasadena, CA 91109 (818) 397-9280 1 My Calculator CONTENTS 1 INTRODUCTION . . . . . . . . . . . . . . . . . . . . 4 2 THE DESIGN . . . . . . . . . . . . . . . . . . . . . 4 3 COMMAND SUMMARY . . . . . . . . . . . . . . . . . . 5 4 MODES OF OPERATIONS . . . . . . . . . . . . . . . . 6 4.1 SINGLE COMMAND MODE . . . . . . . . . . . . . . . 6 4.2 INTERACTIVE MODE . . . . . . . . . . . . . . . . . 6 5 EXPRESSION COMMAND . . . . . . . . . . . . . . . . . 7 6 ASSIGNMENT COMMAND . . . . . . . . . . . . . . . . . 8 7 SHOW COMMAND . . . . . . . . . . . . . . . . . . . . 9 8 DISPLAY COMMAND . . . . . . . . . . . . . . . . . 10 9 INPUT COMMAND . . . . . . . . . . . . . . . . . . 11 10 EXIT COMMAND . . . . . . . . . . . . . . . . . . . 12 11 COMMENT COMMAND . . . . . . . . . . . . . . . . . 12 12 DEBUG COMMAND . . . . . . . . . . . . . . . . . . 12 13 ALGEBRAIC EXPRESSIONS . . . . . . . . . . . . . . 13 13.1 DATA TYPES . . . . . . . . . . . . . . . . . . . 13 13.2 DATA TYPE CONVERSION . . . . . . . . . . . . . . 13 13.3 CONSTANTS . . . . . . . . . . . . . . . . . . . 13 13.4 SYMBOLS . . . . . . . . . . . . . . . . . . . . 14 13.5 ARITHMETIC OPERATORS . . . . . . . . . . . . . . 14 13.6 OPERATOR PRECEDENCE . . . . . . . . . . . . . . 15 13.7 MATH FUNCTIONS . . . . . . . . . . . . . . . . . 15 13.7.1 ABS . . . . . . . . . . . . . . . . . . . . . . 15 13.7.2 ACOS . . . . . . . . . . . . . . . . . . . . . . 15 13.7.3 ASIN . . . . . . . . . . . . . . . . . . . . . . 16 13.7.4 ATAN . . . . . . . . . . . . . . . . . . . . . . 16 13.7.5 CEILING . . . . . . . . . . . . . . . . . . . . 16 13.7.6 COS . . . . . . . . . . . . . . . . . . . . . . 16 13.7.7 COSH . . . . . . . . . . . . . . . . . . . . . . 16 13.7.8 EXP . . . . . . . . . . . . . . . . . . . . . . 16 13.7.9 FLT . . . . . . . . . . . . . . . . . . . . . . 17 13.7.10 FLOOR . . . . . . . . . . . . . . . . . . . . . 17 13.7.11 INT . . . . . . . . . . . . . . . . . . . . . . 17 13.7.12 LN . . . . . . . . . . . . . . . . . . . . . . . 17 13.7.13 LOG . . . . . . . . . . . . . . . . . . . . . . 17 13.7.14 POW . . . . . . . . . . . . . . . . . . . . . . 17 13.7.15 RAND . . . . . . . . . . . . . . . . . . . . . . 18 13.7.16 MAX . . . . . . . . . . . . . . . . . . . . . . 18 13.7.17 MIN . . . . . . . . . . . . . . . . . . . . . . 18 13.7.18 MOD . . . . . . . . . . . . . . . . . . . . . . 18 13.7.19 SEED . . . . . . . . . . . . . . . . . . . . . . 18 13.7.20 SIN . . . . . . . . . . . . . . . . . . . . . . 19 13.7.21 SINH . . . . . . . . . . . . . . . . . . . . . . 19 13.7.22 SQRT . . . . . . . . . . . . . . . . . . . . . . 19 13.7.23 TAN . . . . . . . . . . . . . . . . . . . . . . 19 13.7.24 TANH . . . . . . . . . . . . . . . . . . . . . . 19 14 POSSIBLE FUTURE IMPROVEMENTS . . . . . . . . . . . 19 15 LOGICAL NAMES AND SYMBOLS . . . . . . . . . . . . 21 16 BUILD THE CALCULATOR FROM SOURCE CODE . . . . . . 21 2 My Calculator APPENDIX A CALCULATOR SYNTAX DIAGRAMS APPENDIX B EXPRESSION EVALUATION ALGORITHM 3 My Calculator 1 INTRODUCTION My calculator program started as an exercise to learn the run-time library routine LIB$TPARSE and the C language. The functionality provided by my calculator is based on whim and a vague idea of what I wanted to accomplish. My calculator provides the capability of evaluating algebraic expression. The results may be displayed on the terminal or stored as a symbol and used in further calculations. 100 symbols may be defined with each symbol being up to 31 characters long. Symbols may be used in calculations, modified or there value displayed on the terminal. Over twenty math functions are available including pseudo random number generation. Calculator commands can be stored in files and executed. Command files allow the user to develop there own unique functions. Command files can also prompt the user for input as well as execute other command files. Integer data may be entered in octal, decimal and hexadecimal; floating-point data may be entered in decimal only. 2 THE DESIGN My calculator program uses LIB$TPARSE to parse command. Action routines are called when part of a command has parsed correctly. The calculator executes parts of commands before the command is completely parsed. This means two separate types of errors are maintained. Returned values are used to determine if the command parsed correctly and the internal flag ERROR_FLAG is used to pass execution error information. For example, when a command is entered it may parse correctly but the command may not be able to execute. The returned value shows that the command parsed correctly but the error flag indicated that the command could not be executed. If I were to do it all over again I would probably parse the command completely and then execute it. However, the choice really depends on the application. Evaluation of algebraic expressions is done in two steps. The expression is first converted to a reverse Polish expression and then the expression is evaluated. Appendix B describes the algorithm used by my calculator. 4 My Calculator 3 COMMAND SUMMARY This is a brief description of the calculator commands with examples. Assignment: An algebraic expression is evaluated and the results is stored in a symbol. PI = 3.14159 RATE = 4.00 Expression: An algebraic expression is evaluated and the results displayed on the terminal. 123 * (SIN(76.0/PI) + COS(76.0/PI)) (RATE * 40) - 100.00 Show: Display on the current value of a symbol. SHOW PI, RATE SHOW (shows all defined symbols) Display: Change the calculator display characteristics. DISPLAY FILES,PRECISION=4 DISPLAY NOFILES Input: Prompt the user for a numeric value. INPUT/FLOAT/DEFAULT=98.6 TEMP "ENTER TEMPERATURE" INPUT/INTEGER/DEFAULT=4.00 RATE "PAY RATE" Command File: Execute command files (with a default file type of .CALC). @ABC, DEF, HIG, XYZ.CMDS Exit: Exit the currently executing command file or interactive session. EXIT Comment: Comment commands are not executed. ! THIS IS A COMMENT 5 My Calculator 4 MODES OF OPERATIONS The calculator has two modes of operation, single command and interactive mode. When an error occurs, the action taken by the calculator depends the mode. 4.1 SINGLE COMMAND MODE The single command mode assumes that the calculator program has been defined as a VMS foreign command (see LOGICAL NAMES AND SYMBOLS for further information). In this mode, only the command entered on the DCL command line is executed. This allows the calculator to be part of DCL command files. The single command that is entered on the DCL command line may execute one or more command files containing many calculator commands. If an error occurs execution of the calculator is immediately terminated and control return to DCL. For example: $ CALC 123 + 456 $ CALC @ABC 4.2 INTERACTIVE MODE In interactive mode the calculator prompts the user for commands. If an error occurs, the interactive command is terminated and the user is prompted for the next command. If an error occurs in a command file all executing command files are terminated and the user is prompted for for the next command. Interactive mode is entered automatically if the calculator program is not defined as a VMS foreign command or if no command is entered on the DCL command line. For example: $ CALC Calc: A = 98.6 Calc: SHOW A A = 98.599998 Calc: EXIT 6 My Calculator 5 EXPRESSION COMMAND ---------------------------------------------------------------- Format: ---------------------------------------------------------------- Parameter: Specifies the algebraic expression to be evaluated. See ALGEBRAIC EXPRESSIONS for more information. ---------------------------------------------------------------- Description: When an algebraic expression is entered, it is evaluated and the results displayed on the terminal. ---------------------------------------------------------------- Examples: 1 + 2 - 3 log(ln(1.0)) MAX(LN(10.0),LOG(10.0)) * 42/SIN(0.5) 7 My Calculator 6 ASSIGNMENT COMMAND ---------------------------------------------------------------- Format: = ---------------------------------------------------------------- Parameters: Specifies the name of the symbol to receive the results of the evaluation of the expression. Specifies the algebraic expression to be evaluated. See ALGEBRAIC EXPRESSIONS for more information. ---------------------------------------------------------------- Description: When an assignment command is entered the expression is evaluated and the results stored in the specified symbol. ---------------------------------------------------------------- Examples: PAY_RATE = 5.50 HOURS_WORKED = 40.0 GROSS_EARNINGS = PAY_RATE * HOURS_WORKED TAKE_HOME = GROSS_EARNINGS * 0.50 8 My Calculator 7 SHOW COMMAND ---------------------------------------------------------------- Format: SHOW [ [[,]]] ---------------------------------------------------------------- Parameters: Specifies the name of the symbol to be displayed. ---------------------------------------------------------------- Description: The SHOW command displays the value of one or more symbols on the terminal. If no symbols are specified all currently defined symbols are displayed. If the show command specifies a symbol that is currently not defined a warning message is displayed. This warning in not an error and will not cause the termination of a command file or interactive command. Parameters can be separate by commas or blanks. ---------------------------------------------------------------- Examples: SHOW PAY_RATE, HOURS_WORKED, TAKE_HOME SHOW A B C SHOW 9 My Calculator 8 DISPLAY COMMAND ---------------------------------------------------------------- Format: DISPLAY PARAMETER [[,]PARAMETER] ---------------------------------------------------------------- Parameters: FILE Specifies that commands read from command files will be displayed on the terminal before execution. An alternate form of the parameter is FILES. NOFILE (default) Specifies that commands read from command files will NOT be displayed on the terminal before execution. An alternate form of the parameter is NOFILES. PRECISION=[integer] Specifies the maximum number of fractional digits to be display for floating-point values. If no integer value is entered the default value of 6 is used. The precision must be an integer between 1 and 16. If a precision outside this range is entered a warning message is displayed and the precision set to the default value. This warning in not an error and will not cause the termination of a command file or interactive command. ---------------------------------------------------------------- Description: ThE DISPLAY command changes the display characteristics of the calculator. Parameters can be separate by commas or blanks. ---------------------------------------------------------------- Examples: DISPLAY NOFILE,PRECISION=4 DISPLAY FILE DISPLAY PRECISION=28 (will use the default) 10 My Calculator 9 INPUT COMMAND ---------------------------------------------------------------- Format: INPUT [qualifiers] ---------------------------------------------------------------- Parameters: The name of the symbol that recieves the data entered by the user. The prompt string is displayed on the terminal and the calculator waits for a response from the user. The prompt string may be up to 80 characters long and must be surrounded by quote marks ("). ---------------------------------------------------------------- Qualifier: /INTEGER (default) Specifies that the users input will be converted into a integer value. This is the default data type for user input if none is specified on the input command. /FLOAT Specifies that the users input will be converted into a floating-point value. /DEFAULT=numeric_value Specifies that if the user enters nothing (a carriage return) the symbol will receive the default value (including the default data type). ---------------------------------------------------------------- Examples: INPUT/FLOAT/DEFAULT=99 AGE "ENTER YOUR AGE:" In the example, the command prompts the user for input and stores it in the symbol AGE. If the user enters a carriage return the default value 99 is stored in the symbol AGE. The data type of AGE depends on what the user enters. The symbol AGE will be forced to a floating-point value by the /FLOAT qualifier unless the default is chosen. The default value forces AGE to become the integer value 99. 11 My Calculator 10 EXIT COMMAND ---------------------------------------------------------------- Format: EXIT ---------------------------------------------------------------- Description: The EXIT command causes a command file or an interactive session to terminate. 11 COMMENT COMMAND ---------------------------------------------------------------- Format: ! ---------------------------------------------------------------- Description: Comments are not true commands and are never executed. The are provided to help document command files. The first non-blank character of a comment must be an "!". 12 DEBUG COMMAND ---------------------------------------------------------------- Format: DEBUG ---------------------------------------------------------------- Description: Toggles on and off the display of calculator debugging information. This command will eventually go away. 12 My Calculator 13 ALGEBRAIC EXPRESSIONS An algebraic expression is a sequence of arithmetic operators, numeric values, symbols and functions. 13.1 DATA TYPES The calculator maintains internal numeric data in integer and floating-point form. Integer values are stored in 32 bit and can represent integers in the range -2,147,483,648 to 2,147,483,647. Floating-point values are store 64 bits and can represent values in the range 0.29E-38 to 1.7E38 with approximately 16 decimal digits of precision. 13.2 DATA TYPE CONVERSION Functions automatically convert arguments to the correct data type(s) (which is usually floating-point) for there needs. Operators do not change the data type of operands unless integer and floating_point are mixed together. When mixed together integers are converted floating-point before an operation is performed. Conversion to a specific data type is accomplished with the functions FLT and INT. 13.3 CONSTANTS Integer constants may be entered in octal, decimal and hexadecimal form. Octal constants must be preceeded by "%o" or "%O". Hexadecimal constants must be proceeded by "%x" or "%X". Decimal constants may be preceeded by "%d" or "%D" but it is not required. For example: %o7777 octal constant %xFFFF hexadecimal constant %d12345 decimal constant 12345 decimal constant 13 My Calculator Floating-point constants can only be entered in decimal form. It must have a decimal point and be preceeded by at least one decimal digit (which may be zero). It may also be followed by a signed exponent preceeded by the letter "e" or "E". For example: 1234.5 decimal constant 0.0001 decimal constant 1232.5E10 decimal constant 0.0001e-10 decimal constant 13.4 SYMBOLS Symbols are named data structures containing numeric data. The symbol name is a string of one or more alphanumeric characters, in addition to the dollar sign ($) and the underscore (_). Symbols must start with an uppercase or lowercase A thru Z to distinguish them from numeric constants. Symbols may be of any length but only the first 31 characters will be used. Symbols may be used in algebraic expressions any were a numeric value is required. When the expression is evaluated the numeric value of the symbol is substituted in the calculation. The data type and value of a symbol may be modified with the ASSIGNMENT and INPUT commands. 13.5 ARITHMETIC OPERATORS Symbols and numeric values may be used with operators to create more complex expressions. The operators are: Operator Example Results - [unary] -a negative of a + [unary] +a no change in a - [binary] a-b a plus b + [binary] a+b a minus b * [binary] a*b a times b / [binary] a/b a divided by b 14 My Calculator 13.6 OPERATOR PRECEDENCE The following is the precedence used by my calculator to evaluates expressions. Operators with the highest precedence appear a the top of the list; those with the lowest appear at the bottom. Category Operator Associativity Primary ( ) left to right Unary + - right to left Unary function right to left Binary * / left to right Binary + - left to right 13.7 MATH FUNCTIONS A variety of math function are are available in the calculator. Math functions may be used anywhere in an algebraic expression that a symbol or numeric value is used. The form of a functions is: F(A1,A2,...An) Where F is the function name and each A is a function argument. The number of arguments is function dependent. Arguments are algebraic expressions and may be as complex as needed. 13.7.1 ABS The ABS function returns the absolute value of an integer or floating-point value. Format: ABS(VALUE) 13.7.2 ACOS The ACOS function returns a value in the range zero to pi, which is the arc cosine of its radian argument. Format: ACOS(VALUE) 15 My Calculator 13.7.3 ASIN The ASIN function returns a value in the range -pi/2 to pi/2, which is the arc sine of its radian argument. Format: ASIN(VALUE) 13.7.4 ATAN The ATAN function returns a value in the range -pi/2 to pi/2, which is the arc tangent of its radian argument. Format: ATAN(VALUE) 13.7.5 CEILING The CEILING function returns a floating-point value which is the smallest integer which is greater than or equal to its argument. Format: CEILING(VALUE) 13.7.6 COS The COS function returns the cosine of its radian argument. Format: COS(VALUE) 13.7.7 COSH The COSH function returns the hyperbolic cosine of its radian argument. Format: COSH(VALUE) 13.7.8 EXP The EXP function returns the base e raised to the power of the argument. Format: EXP(VALUE) 16 My Calculator 13.7.9 FLT The FLT function converts its argument to a floating-point value. Format: FLT(VALUE) 13.7.10 FLOOR The FLOOR function returns a floating-point value which is the largest integer which is less than or equal to its argument. Format: FLOOR(VALUE) 13.7.11 INT The INT function converts its argument to an integer value. Format: INT(VALUE) 13.7.12 LN The LN function returns the natural logarithm of its argument which must be a positive and non-zero value. Format: LN(VALUE) 13.7.13 LOG The LOG function returns the base 10 logarithm of its argument which must be a positive and non-zero value. Format: LOG(VALUE) 13.7.14 POW The POW function return the first argument raised to the power of the second argument. Format: POW(BASE,EXP) 17 My Calculator 13.7.15 RAND The RAND function uses a multiplicative congruential random number generator with a repeat factor (period) of 2 to the 32 power. The function must have a legal argument even though it is not used. See the SEED function for further information. Format: RAND(VALUE) 13.7.16 MAX The MAX function return the maximum of two arguments. Format: MAX(VALUE,VALUE) 13.7.17 MIN The MIN function return the minimum of two arguments. Format: MIN(VALUE,VALUE) 13.7.18 MOD The MOD function return an integer which is the remainder of the first integer argument divided by the second integer argument. Format: MOD(VALUE,VALUE) 13.7.19 SEED The SEED function returns a random number exactly like the RAND function. The random number generator is reinitialized by calling SEED with the argument 1, or it can be set to a specific point by calling SEED with any number. Format: SEED(VALUE) 18 My Calculator 13.7.20 SIN The SIN function returns the sine of the radian argument. Format: SIN(VALUE) 13.7.21 SINH The SINH function returns the hyperbolic sine of the radian argument. Format: SINH(VALUE) 13.7.22 SQRT The SQRT function returns the square root of its argument which may not be a negative value. Format: SQRT(VALUE) 13.7.23 TAN The TAN function returns the tangent of the radian argument. Format: TAN(VALUE) 13.7.24 TANH The TANH function returns the hyperbolic tangent of the radian argument. Format: TANH(VALUE) 14 POSSIBLE FUTURE IMPROVEMENTS Modify the actual calculator to be a callable interface so that it can be incorporated into other programs. For example, my editor. 19 My Calculator Rewrite the calculator in MACRO. Add more math functions. Expand the data types from integer and floating-point to include a greater number of types such as Q floating-point, complex, etc. Add special functions, operators, etc. for special data types such coordinate pairs, etc. Add the capability to store calculator results in a VMS symbol and/or logical name. This would increase the usefulness of the calculator in DCL command files. Not all errors are detected (underflow and overflow). Make sure that all errors are detected and reported. Improve the code. It is somewhat klugy (to say the least). 20 My Calculator 15 LOGICAL NAMES AND SYMBOLS The following logical name defines the calculator help library. $ DEFINE CALCULATOR_HELP DISK:[DIR]CALCULATOR_HELP.HLB The following symbol definition is needed if the calculator is to be a VMS foreign command. $ CALC :== $DISK:[DIR]CALC.EXE 16 BUILD THE CALCULATOR FROM SOURCE CODE $ LIBRARY/CREATE/HELP CALC_HELP CALC_HELP $! $ MACRO CALC_TABLE.MAR $ CC CALC.C $ CC CALC_ASSIGNMENT.C $ CC CALC_COMMAND_FILE.C $ CC CALC_DISPLAY.C $ CC CALC_EVALUATION.C $ CC CALC_EXIT.C $ CC CALC_EXPRESSION.C $ CC CALC_FUNCTIONS.C $ CC CALC_HELP.C $ CC CALC_INPUT.C $ CC CALC_SHOW.C $ CC CALC_SYMBOLS.C $! $ LINK CALC, - CALC_ASSIGNMENT, - CALC_COMMAND_FILE, - CALC_DISPLAY, - CALC_EVALUATION, - CALC_EXIT, - CALC_EXPRESSION, - CALC_FUNCTIONS, - CALC_HELP, - CALC_INPUT, - CALC_SHOW, - CALC_SYMBOLS, - CALC_TABLE 21 APPENDIX A CALCULATOR SYNTAX DIAGRAMS commands +-------------------+ +---------------------+ ---+--->| COMMENT |----+------->| end of input string | | +-------------------+ ^ +---------------------+ | | | +-------------------+ | +--->| COMMAND FILES |--->+ | +-------------------+ | | | | +-------------------+ | +--->| DISPLAY |--->+ | +-------------------+ | | | | +-------------------+ | +--->| HELP |--->+ | +-------------------+ | | | | +-------------------+ | +--->| INPUT |--->+ | +-------------------+ | | | | +-------------------+ | +--->| SHOW |--->+ | +-------------------+ | | | | +-------------------+ | +--->| ASSIGNMENT |--->+ | +-------------------+ | | | | +-------------------+ | +--->| EXPRESSION |--->+ +-------------------+ A-1 CALCULATOR SYNTAX DIAGRAMS comment +-----+ +---------------------------+ ------->| "!" +----------->| any string including null |--------> +-----+ +---------------------------+ command files +-----+ +-----------+ ------->| "@" +------------+----->| FILE NAME |------+----------> +-----+ ^ +-----------+ | | | +<------------------------+ | | | +-----+ | +<--------| "," |---------+ +-----+ display +------------+ +-------------------+ ------->| "DISPLAY" |---+---->| DISPLAY PARAMETER |----+-------> +------------+ ^ +-------------------+ | | | +<-----------------------------+ | | | +-----+ | +<----------| "," |------------+ +-----+ A-2 CALCULATOR SYNTAX DIAGRAMS display parameter +------------+ ---+--->| "FILE" |-------------------------------------+----> | +------------+ | | | | +------------+ | +--->| "FILES" |------------------------------------>+ | +------------+ | | | | +------------+ | +--->| "NOFILE" |------------------------------------>+ | +------------+ | | | | +------------+ | +--->| "NOFILES" |------------------------------------>+ | +------------+ | | | | +--------------+ +-----+ | +--->| "PRECISION" |--->| "=" |---+------------------->+ +--------------+ +-----+ | | | +-----------+ | +-->| PRECISION |--->+ +-----------+ precision +---------+ ------->| INTEGER |---------------------------------------------> +---------+ help +------------+ +---------------------------+ ------->| "HELP" |---->| ANY STRING INCLUDING NULL |--------> +------------+ +---------------------------+ input +------------+ +-------------+ +----------+ ------->| "INPUT" |--+->| I QUALIFIER |--+->| I PROMPT |-----> +------------+ ^ +-------------+ | +----------+ | | +-------------------+ A-3 CALCULATOR SYNTAX DIAGRAMS i qualifier ---+--------------------------------------------------------+---> | ^ | +-----+ +-----------+ | +-->| "/" |-->+-->| "FLOAT" |------------------------->+ +-----+ | +-----------+ | | | | +-----------+ | +-->| "INTEGER" |------------------------->+ | +-----------+ | | | | +-----------+ +-----+ +--------+ | +-->| "DEFAULT" |-->| "=" |-->| NUMBER |-->+ +-----------+ +-----+ +--------+ i prompt +--------+ +---------------+ ------->| SYMBOL |-->| QUOTED STRING |--------------------------> +--------+ +---------------+ show +------------+ ------->| "SHOW" |---+------------------------------+-------> +------------+ | ^ | +------------+ | +---+--->| SYMBOL |---+--->+ ^ +------------+ | | | +<--------------------+ | | | +-----+ | +<------| "," |-------+ +-----+ assignment +------------+ +-----+ +------------+ ------->| SYMBOL |------->| "=" |------->| EXPRESSION |-----> +------------+ +-----+ +------------+ A-4 CALCULATOR SYNTAX DIAGRAMS expression +-----+ +-->| "+" |---+ | +-----+ | | V +------------+ ---+-------------+----+--->| TERM |------+----------+-----> | ^ ^ +------------+ | | | +-----+ | | V V +-->| "-" |---+ | +-----+ +-----+ +-----+ | | "+" | | "-" | | +--+--+ +--+--+ | | | | | | +------------------------+----------+ term +------------+ ---+--->| FACTOR |-----------+----------+-------------------> ^ +------------+ | | | V V | +-----+ +-----+ | | "*" | | "/" | | +--+--+ +--+--+ | | | | | | +-----------------------------+----------+ factor +------------+ ---+--->| NUMBER |----------------------------------+-------> | +------------+ ^ | | | +------------+ | +--->| SYMBOL |--------------------------------->+ | +------------+ | | | | +------------+ +--------------------+ | +--->| FUNCTION |----->| FUNCTION ARGUMENTS |----->+ | +------------+ +--------------------+ | | | | +-----+ +------------+ +-----+ | +--->| "(" |------>| EXPRESSION |------>| ")" |----->+ +-----+ +------------+ +-----+ A-5 CALCULATOR SYNTAX DIAGRAMS function arguments (the number is function dependent) +-----+ +------------+ +-----+ ------->| "(" |--+--->| EXPRESSION |---+-->| ")" |--------------> +-----+ ^ +------------+ | +-----+ | | | +-----+ | +<------| "," |-------+ +-----+ number +----------------+ ---+--->| DECIMAL NUMBER |-------------------------------+------> | +----------------+ ^ | | | +------+ +--------------------+ | +--->| "%O" |------>| octal number |----------->+ | +------+ +--------------------+ | | | | +------+ +--------------------+ | +--->| "%D" |------>| DECIMAL NUMBER |----------->+ | +------+ +--------------------+ | | | | +------+ +--------------------+ | +--->| "%X" |------>| hexadecimal number |----------->+ +------+ +--------------------+ decimal number +---------+ ---+--->| INTEGER |------------------------------------->+------> | +---------+ | | | | +-------------+ | +--->| REAL NUMBER |--------------------------------->+ | +-------------+ | | | | +-------------+ +-----+ +----------+ | +--->| REAL NUMBER |--->| "E" |--->| EXPONENT |------>+ +-------------+ +-----+ +----------+ real number +---------+ +-----+ +---------+ ------->| INTEGER |--->| "." |--->| INTEGER |-------------------> +---------+ +-----+ +---------+ A-6 CALCULATOR SYNTAX DIAGRAMS exponent +-----+ +-->| "+" |---+ | +-----+ | | V +---------+ ---+-------------+-------->| INTEGER |--------------------------> | ^ +---------+ | +-----+ | +-->| "-" |---+ +-----+ integer +---------ANY-DECIMAL-DIGIT------------+ ---+--->| 0 thru 9 |----+-----------> ^ +--------------------------------------+ | | | +------------------------------------------------+ quoted string +-----+ +--------------------------+ +-----+ --->| '"' |---+--->| ANY CHARACTER EXCEPT '"' |---+---| '"' |---> +-----+ ^ +--------------------------+ | +-----+ | | +-----------------------------------+ string +-------------ANY-CHARACTER------------+ ---+--->| a thru z, A thru Z or 0 thru 9 |----+-----------> ^ +--------------------------------------+ | | | +------------------------------------------------+ symbol +-------------ANY-CHARACTER------------+ ---+--->| a thru z, A thru Z, 0 thru 9, $ or _ |----+-----------> ^ +--------------------------------------+ | | | +------------------------------------------------+ A-7 APPENDIX B EXPRESSION EVALUATION ALGORITHM The following algorithm evaluates an algebraic expression in two steps. The expression is first converted to a reverse Polish notation expression and then the reverse Polish expression is evaluated. Step one converts an algebraic expression (infix notation) to a reverse Polish expression (postfix notation). The algebraic expression is first broken up into tokens which are passed to the conversion algorithm which reassembles them into a reverse Polish expression. The tokens are of two types, operands (values) and operators (+,-,*,/,etc). The reverse polish expression is built in a FIFO queue (called the "Intermediate Queue") which is used by step two. The conversion algorithm also uses a stack called the "Operator Stack". Step two evaluates the reverse Polish expression and generate an results. This process requires a stack to hold operands called the "Operand Stack". Each token is read, one at a time, from the "Intermediate Queue". If the token is an operand it is pushed onto the the "Operand Stack". If the token is an operator the operation is performed on the operands on the top of the stack and the results pushed onto the stack. When there are no more tokens in the queue the results will be on the top of the stack. Converting And Algebraic Expression To Reverse Polish Notation 1. Scan the algebraic expression form left to right breaking up into tokens. Pass the tokens to the following steps. 2. If the token is a operand (value), add it to the "Intermediate Queue". 3. if the token is an operator (+,-,*,/,etc.) do one of the following: B-1 EXPRESSION EVALUATION ALGORITHM (a) Look at the operator on the top of the "Operator Stack" (TOS). If the TOS has an equal or greater precedence that the token, pop the TOS and add it to the "Intermediate Queue". Got to step (a). (b) If the TOS has a precedence less than the token, push the token onto the "Operator Stack". (c) When the token is a left parenthesis push it onto the "Operator Stack". (d) If the token is a right parenthesis discard it. then pop operators one at a time from the "Operator Stack" and add them to the "Intermediate Queue". Continue this until a left parenthesis is found, which is discarded. 4. When the scan is complete, add any operators left on the "Operator Stack" to the "Intermediate Queue". Evaluating An Expression In Reverse Polish Notation 1. Scan the expression from left to right breaking it into tokens which are passed to the following steps. 2. Push any tokens that are operands onto the "Operand Stack". 3. If a token is a operator the appropriate number of operands are removed from the top of the stack and the operation preformed. The results is pushed onto the operand stack. 4. When the scan is complete the results of the expression is on the top of the stack. B-2