Skip to content

rievallib

This library lets a program evaluate expressions that the user types, such as 2+6*3, without parsing them itself: a calculator, or a debugger that reads registers and memory, needs only a few commands. It works with integers (longs) only, not with floating point values.

Evaluation takes two steps. ConvToPostFix converts the expression into postfix form, where each operator follows its two operands (2+6 becomes 2 6 +), and writes it to a buffer; PFEvaluate evaluates such a postfix string. After PFEvaluate, PFErrorType and PFErrorText tell whether an error occurred.

An operand is a decimal number, a hexadecimal number after $, a binary number after %, one of the registers d0 to d7, a0 to a7 and pc of a block set with PFRegisters, or a value read from memory: {address} reads the long at the address, {address}.w a word and {address}.b a byte. A - in front of a number or register makes it negative; before a bracket it is taken as subtraction (see ConvToPostFix). The operators, from the lowest precedence to the highest:

  • = (equal), & (bitwise and) and | (bitwise or),
  • < and >,
  • + and -,
  • * and /,
  • ^ (power).

The comparisons =, < (less than) and > (greater than) give -1 for true and 0 for false; < and > compare with sign. Brackets group operands and change the precedence. ConvToPostFix makes upper case letters lower case, the form PFEvaluate reads. The commands work in Amiga and Blitz mode.

The library has the number of a library of Blitz Basic 2’s own set, so it can only be used with the library variant rwe-1999-eval, which moves that library to another number (see The RWE userlibs).

e$="2+6*3"
b$=String$(" ",80)
ConvToPostFix e$,&b$
v.l=PFEvaluate(Peek$(&b$))
If PFErrorType=0
  NPrint v
Else
  NPrint PFErrorText
EndIf

The fixed bugs of the library are listed in Fixes in rievallib.

Based on the RI Eval Lib guide by Reflective Images.

Library number 121.

Commands

ConvToPostFix

Converts the expression in string into postfix form, with operands and operators separated by spaces, and writes it to the memory at buffer, ending with a zero byte. Read it with Peek$(buffer).

Statement. Help text: string,buffer
Arguments: $, .l

The buffer should be at least twice as long as the expression, plus one byte. A - counts as a sign at the start of the expression, after an operator and after (, also with spaces in between, so 4* -2 is a product with -2; after a value it is a subtraction, so 3 -4 is 3-4. The command makes the letters of string lower case, in place. A character that cannot belong to an operand, such as the , in 1,2, stays in the operand, and PFEvaluate reports it as error 4.

A sign works only before a number or a register, not before a bracket. Inside the expression a - directly before ( is always a subtraction, so 2*-(3) lacks a value for it and PFEvaluate fails with error 1 “Need 2 arguments for expression!”. At the start of the expression, as in -(3), the - becomes an operator of its own with nothing to subtract from, and PFEvaluate fails with the same error. Write a negated bracket as a subtraction from 0 or a product with -1 instead: 2*(0-3) gives -6, and -1*(3) gives -3.

PFErrorText

The text of the error of the last PFEvaluate, such as “Divide by 0 error!”.

Function, result $. Help text: - returns error text corresponding to error
No arguments.

After a PFEvaluate without an error it returns “”.

PFErrorType

The number of the error of the last PFEvaluate, 1 to 6, or 0 if it succeeded.

Function, result .w. Help text: - returns error number or 0 for none
No arguments.

PFEvaluate

Evaluates a postfix expression as ConvToPostFix makes it and returns its value. On an error it returns 0; check PFErrorType afterwards.

Function, result .l. Help text: (string)
Arguments: $

The errors are these:

PFErrorTypeError
1“Need 2 arguments for expression!”
2“Register structure not found!” (a register without PFRegisters)
3“Divide by 0 error!”
4“No result value!”
5“No close bracket on indirect!”
6“Invalid size for indirection!”

An empty string returns 0 without an error.

A number may hold only the digits of its base: 0 to 9, after $ also a to f, after % only 0 and 1. The registers are d0 to d7, a0 to a7 and pc. Letters must be lower case, as ConvToPostFix writes them. Any other operand, and an expression that leaves more than one value, such as 1 2, is error 4; so is a string of spaces only.

* and ^ give the low 32 bits of a result that does not fit into a long. ^ is the integer power: x^0 is 1, and a negative exponent gives the whole part of the result, 0 for most bases; 0 with a negative exponent is error 3. There is no limit to the number of values pending. {address}.b and {address}.w read only the byte or the word, so they also work at an odd address on a 68000.

PFIndirectAddr

The address read by the last {address} in an expression that PFEvaluate evaluated, or 0 if the program has evaluated none yet.

Function, result .l. Help text: - returns last indirect lookup
No arguments.

PFRegisters

Sets the register block whose values PFEvaluate uses for the register names. The block is 17 longs, in this order - d0 to d7, a0 to a7 and pc.

Statement. Help text: *registerblock - pointer to registers
Arguments: .l

The program fills the block with the values it wants the names to stand for; they need not be real register values.