Formulas
tilttek/character-sheetA Formula is a whole-number expression of the rules of a system. Formula::parse() reads one, and SheetRules reads
each derived value's.
What a formula holds
| Element | Writing | Value |
|---|---|---|
| A number | 10, 007 | a whole number from 0 to 1 000 000 |
| A name | str, dex_mod, level, gear.armour | the value of that name: a lowercase letter or _, then lowercase letters, digits or _, with one optional . part |
| Addition, subtraction | a + b, a - b | from left to right |
| Multiplication, division | a * b, a / b | from left to right, before addition and subtraction; the division rounds down |
| A sign | -a | before everything else |
| Parentheses | (a + b) * c | |
abs(a) | the value without its sign | |
min(a, b, …), max(a, b, …) | 2 to 8 arguments | the lowest or the highest |
clamp(a, low, high) | a kept between low and high, refused when low is above high |
Spaces and tabs separate the elements anywhere; any other character is refused, with its position.
The division rounds down, as the modifiers of most systems count: (9 - 10) / 2 is -1, (15 - 10) / 2 is 2. A
division by zero is refused when it is computed.
A formula holds no loop, no condition and no call beyond those four functions: it always ends, in a number of steps bounded by its length.
Bounds
| Bound | Value |
|---|---|
| Length | 256 bytes |
| Nesting of parentheses, calls and signs | 16 levels |
| Numbers and names | 64 |
| A number written | 1 000 000 |
| A value read or reached at any step | -1 000 000 000 000 to 1 000 000 000 000 |
A value beyond the last bound — read from a name, or reached by an addition, a subtraction or a multiplication — is refused, never wrapped nor turned into a float.
Computing
Formula::evaluate() takes the value of each name it reads and gives the result. Formula::$names lists the names it
reads, each once, in the order they first appear; SheetRules checks them against the level, the attributes of the
system, the derived values and the gear.
Canonical text
Formula::canonical() writes the formula back without spaces or leading zeros, with only the parentheses that keep its
steps: ( str - 10 ) / 2 is (str-10)/2, (a - b) - c is a-b-c, a - (b - c) keeps its parentheses. It is never
longer than the text it was read from, and it reads back to the same steps. Formula::$text and the string of a formula
keep the text as it was written.