﻿ F-Correlatives

# F-Correlatives

On systems that process correlatives interpretively, A-correlatives are converted to the more efficient F-correlative format at the start of a query that uses them. F-correlatives use "reverse Polish" notation which, whilst actually very simple, is difficult for inexperienced developers to maintain.

QM compiles both A and F-correlatives to its own internal instruction set and, therefore, the potential advantage of writing these more complex expressions on other systems is irrelevant.

Reverse Polish notation defines data and operations to be performed on it as a series of steps that manipulate an internal stack. Each step is separated by a semicolon. As an example, consider a simple A-correlative expression such as

A;1*"11"/"10"

that adds 10% to the value in field 1. As an F-correlative expression this becomes

F;1;"11";*;"10";/

where the steps are

1        Push the value of field 1 onto the stack

"11"        Push the constant "11" onto the stack

*        Multiply the top two items on the stack, replacing them with the result

"10"        Push the constant "10" onto the stack

/        Divide the top two items on the stack, replacing them with the result

Data items

 n A field number. This field is extracted from the current record

 "string" A string constant. All constants of this style, including numeric values, must be enclosed in single quotes, double quotes or backslashes.

 Cvalue A constant value.

Any of the above three data item types may be followed by R to indicate that the REUSE() function is to be applied to the data. The field number form can be followed by a conversion code in brackets. If used with R, the sequence is nR(conv).

 D The internal date. This is the actual date at the point when the function is executed, not a reference to the @DATE variable (which does not change during a command).

 T The internal time. This is the actual time at the point when the function is executed, not a reference to the @TIME variable (which does not change during a command).

 @NB The breakpoint level.  The leading @ may be omitted.

 @ND The detail line counter.  The leading @ may be omitted.

 @NI The item counter.  The leading @ may be omitted.

 @NS The subvalue counter.  The leading @ may be omitted.

 @NV The value counter.  The leading @ may be omitted.

Functions and Operators

 + Adds the top two items on the stack, replacing then with the result

 - Subtracts the top item on the stack from the next item, replacing them with the result.

 * Multiplies the top two items on the stack, replacing then with the result

 / Divides the second item on the stack by the top item, replacing them with the result. Note that this is an integer division.

 : Concatenates the top stack item on to the end of the second stack item, replacing then with the result

 = Replaces the top two items on the stack by 1 if they are equal, 0 if they are unequal.

 # Replaces the top two items on the stack by 1 if they are unequal, 0 if they are equal.

 > Replaces the top two items on the stack by 1 if the top item is greater than the second item, otherwise 0.

 < Replaces the top two items on the stack by 1 if the top item is less than the second item, otherwise 0.

 ] Replaces the top two items on the stack by 1 if the top item is greater than or equal to the second item, otherwise 0.

 [ Replaces the top two items on the stack by 1 if the top item is less than or equal to the second item, otherwise 0.

 [ ] Replaces the top three stack items with a substring of third stack item, starting at the character position given by the second stack item, with a length determined by the top stack item.

 P Duplicates the top stack item.

 R Replaces the top two stack items with the remainder from dividing the second item by the top item.

 S Replaces the top stack item by the sum of all the values in multivalued item at the top of the stack.

 _ Exchanges the top two items on the stack.

 ^ Deletes the top item from the stack.

 (conv) Replaces the top item on the stack with the result of applying the given conversion code to the current top stack item. Note that conv is not quoted.

 & Replaces the top two items of the stack with the result of a logical AND between their values.

 ! Replaces the top two items of the stack with the result of a logical OR between their values.

Flow Control

 J label Unconditionally jumps to label. There must be a space before the label name.

 JF label Jumps to label if the top stack item is False. There must be a space before the label name.

 JT label Jumps to label if the top stack item is True. There must be a space before the label name.

 ~label Defines a label. The label name may be followed by a space or a semicolon. In QM, labels in correlatives must be constructed from a letter optionally followed by further letters, digits, periods, percent signs or dollar signs. Alternatively a label may be wholly numeric.

Examples

F;3;2;*

Multiplies the content of field 3 by the content of field 2.

F;3;7;+;(MD2)

Adds the field 3 and field 7. The result is then converted using an MD2 conversion code.

F;3;"1175";*;"1000";/

Adds 17.5% tax to the price in field 3. Note the need to perform the calculation in two steps because correlatives use integer arithmetic.

F;3;"1175"R;*;"1000"R;/

Adds 17.5% tax to each value in the multivalued price in field 3. Note the use of the R qualifier on both constants in this expression.

F;1;"1";"20";[]

Extracts the first 20 characters of field 1.

F;1;D;>;JT LATE;"";J END;~LATE;"Overdue";~END

Tests whether the due date in field 1 has passed. If so, the returned value is "Overdue", otherwise a null string is returned.