Parse Time Calculation

But substituting tokens recursively is not the only trick. Sometimes some additional control is needed. That's why Qompose also supports a powerful, if simple RPN processor. It is invoked by putting a %= at the start of a token's content. Processing runs to the first newline and elements are separated by spaces. The stack starts initialized with the rest of the token's content as it's only entry.

Qompose supports the following operations. All are based on operators in Icon, so some will seem a bit unfamiliar, and type-conversion is automatic.

+ - / * % standard arithmetic operations (% is modulo)
|| string concatenation
repl string replication (replicates the second-top-most string the top-most number of times)
< > = ~= <= >= numeric comparison (~= is not equals to; C-style != will not work)
== ~== string comparison
size makes the top-most value a string and returns its length
-ve makes the top-most value a number and changes its sign
drop dup over swap standard stack operations
"text" inserts the specified text (quotes are optional but still highly recommended if text does not contain spaces or is not an existing operation)
$token. inserts the current value of the token
$*token. inserts the number of values this token does (this works hand in hand with $-token+.
store stores a value (puts the second-top-most value into the token with the name given by the top-most item; currently cannot create new items)
$%var. inserts the contents of the indicated environment variable

Operators that must be ordered, like subtraction, take the top-most item as their second parameter. So 4 3 - results in 1. Be careful; you may have to swap arguments to get the arithmetic to work right. (This is a standard RPN gotcha.)

When there are no more operators to execute, the value on the top of the stack is returned as the token's content. This, coupled with the stack's initial state, permits modification of the token's content.

The comparison operators have the added feature that if they fail, the entire token fails and returns a null string (i.e. no substitution is done where it was referenced). If they succeed, though, then RPN processing continues as if the comparison operator was not there. (It is often useful to include a drop operator at that point.)

< Prev: Building a Qompose File> Next: Setting up Qompose