Strings & Math

OcaltQL provides first-class string and number types. Strings are built, concatenated, sliced, replaced, searched, and inspected with dedicated verbs. Math runs through CALCULATE with standard operators and a full set of math functions.

Creating Strings

The STRING verb constructs a string value. It accepts a literal or a variable and assigns the result with SET.

String Variable
STRING "Hello" SET ?greet
AFTER EMIT ?greet

Concatenation

The & operator joins strings. It works with literals, variables, or a mix of both.

Joining Literals
STRING "Hello " & "World" SET ?full
AFTER EMIT ?full
Joining with Variables
STRING "Alice" SET ?name
AFTER STRING "Hello, " & ?name SET ?greeting
AFTER EMIT ?greeting

String Length

LENGTH returns the character count of a string.

LENGTH
STRING "hello" SET ?word
AFTER LENGTH ?word SET ?strlen
AFTER EMIT ?strlen
(* Output: 5 *)

String Case

STRING UPPERCASE and STRING LOWERCASE convert the case of a string.

Uppercase and Lowercase
STRING "hello" SET ?lower
AFTER STRING UPPERCASE ?lower SET ?upper
AFTER EMIT ?upper

AFTER STRING LOWERCASE ?upper SET ?back
AFTER EMIT ?back

Trim Whitespace

TRIM STRING removes leading and trailing whitespace from a string.

Trim
STRING "  hello  " SET ?spaced
AFTER TRIM STRING ?spaced SET ?trimmed
AFTER EMIT ?trimmed

Reverse a String

REVERSE STRING flips the character order of a string.

Reverse
STRING "hello" SET ?word
AFTER REVERSE STRING ?word SET ?backwards
AFTER EMIT ?backwards
(* Output: olleh *)

Replace String

REPLACE STRING has three modes. WHERE defines the subject — literal or variable. WITH is the replacement.

Replace All
REPLACE STRING ALL "l" WITH "z" WHERE "hello" SET ?replaced
AFTER EMIT ?replaced
(* Output: hezzo *)
Replace Once
REPLACE STRING ONCE "l" WITH "z" WHERE "hello" SET ?once
AFTER EMIT ?once
(* Output: hezlo *)
Replace by Position
REPLACE STRING FROM 0 TO 2 WITH "HE" WHERE "hello" SET ?pos
AFTER EMIT ?pos
(* Output: HEllo — positions 0 to 2 replaced *)

Search String

SEARCH STRING finds the position of a substring inside a string. Returns the zero-based position as a number, or null if not found.

Search — Found
STRING "hello world" SET ?text
AFTER SEARCH STRING ?text FOR "world" SET ?pos
AFTER EMIT ?pos
(* Output: 6 *)
Search — Not Found
STRING "hello world" SET ?text
AFTER SEARCH STRING ?text FOR "zulu" SET ?nopos
AFTER EMIT ?nopos
(* Output: null *)

Substring

SELECT STRING with FROM [pos] TO [pos] extracts a portion of a string by zero-based character position.

Substring
SELECT STRING "hello world" FROM 0 TO 5 SET ?sub
AFTER EMIT ?sub
(* Output: hello *)

Split String

SPLIT breaks a string by a delimiter and returns the result as an indexed array. Splitting by an empty string splits into individual characters.

Split by Delimiter
SPLIT "a,b,c" BY "," SET ?parts
AFTER EMIT ?parts(0)
AFTER EMIT ?parts(1)
AFTER EMIT ?parts(2)
(* Output: abc *)
Split into Characters
SPLIT "abc" BY "" SET ?chars
AFTER EMIT ?chars(0)
(* Output: a *)

Creating Numbers

The NUMBER verb constructs a number value.

Number Variable
NUMBER 42 SET ?age
AFTER EMIT ?age

CALCULATE

CALCULATE evaluates a mathematical expression. Supports + - * /. Operator precedence follows standard math rules. Parentheses override precedence.

Basic Arithmetic
CALCULATE 10 + 5 SET ?sum
AFTER EMIT ?sum
With Variables
NUMBER 10 SET ?a
AFTER NUMBER 5 SET ?b
AFTER CALCULATE ?a + ?b SET ?result
AFTER EMIT ?result
Precedence — Without Parentheses
CALCULATE 10 + 5 * 2 SET ?complex
AFTER EMIT ?complex
(* Output: 20 — multiplication runs first *)
Precedence — With Parentheses
CALCULATE (10 + 5) * 2 SET ?parens
AFTER EMIT ?parens
(* Output: 30 — addition runs first *)

Math Functions

Math functions are called inside CALCULATE. They map directly to native math functions in the runtime.

ABS — Absolute Value
CALCULATE ABS(-42) SET ?abs
AFTER EMIT ?abs
(* Output: 42 *)
ROUND
CALCULATE ROUND(3.14159) SET ?rounded
AFTER EMIT ?rounded
(* Output: 3 *)
FLOOR
CALCULATE FLOOR(3.9) SET ?floor
AFTER EMIT ?floor
(* Output: 3 *)
CEIL
CALCULATE CEIL(3.1) SET ?ceil
AFTER EMIT ?ceil
(* Output: 4 *)
POWER
CALCULATE POWER(2, 8) SET ?pow
AFTER EMIT ?pow
(* Output: 256 *)
SQRT
CALCULATE SQRT(16) SET ?root
AFTER EMIT ?root
(* Output: 4 *)
MOD
CALCULATE MOD(17, 5) SET ?mod
AFTER EMIT ?mod
(* Output: 2 *)

Natural Language Math

POWER and MOD are the only CALCULATE functions that take two arguments, separated by a comma. This is the one place a comma appears as a separator in OcaltQL — everywhere else, items are separated by AND.

Every arithmetic and math function is also expressible in natural-language phrasing, as an alternative to CALCULATE’s symbolic operators and COMPUTE’s function-call syntax. BY and WITH are interchangeable.

ADD
ADD 1 TO 79 SET ?ans
AFTER EMIT ?ans
ADD with a Variable
NUMBER 10 SET ?num
AFTER ADD ?num TO 5 SET ?ans
AFTER EMIT ?ans
SUBTRACT
SUBTRACT 9 FROM 79 SET ?ans
AFTER EMIT ?ans
MULTIPLY
MULTIPLY 10 BY 79 SET ?ans
AFTER EMIT ?ans
AFTER MULTIPLY 10 WITH 79 SET ?ans2
AFTER EMIT ?ans2
(* BY and WITH are interchangeable *)
DIVIDE
NUMBER 20 SET ?num
AFTER DIVIDE ?num BY 5 SET ?ans
AFTER EMIT ?ans
SQUARE ROOT OF
SQUARE ROOT OF 16 SET ?ans
AFTER EMIT ?ans
(* Output: 4 *)
LOGARITHM OF
LOGARITHM OF 100 SET ?ans
AFTER EMIT ?ans
(* Natural log, base e, by default *)

AFTER LOGARITHM OF 100 BASE 10 SET ?ans2
AFTER EMIT ?ans2
(* Output: 2 *)
FIBONACCI OF
FIBONACCI OF 10 SET ?ans
AFTER EMIT ?ans
FACTORIAL OF
FACTORIAL OF 5 SET ?ans
AFTER EMIT ?ans
PRIME OF
PRIME OF 100 SET ?ans
AFTER EMIT ?ans
ABSOLUTE VALUE OF
ABSOLUTE VALUE OF -42 SET ?ans
AFTER EMIT ?ans
(* Output: 42 *)
ROUND OF
ROUND OF 3.14159 SET ?ans
AFTER EMIT ?ans
(* Output: 3 *)
FLOOR OF
FLOOR OF 3.9 SET ?ans
AFTER EMIT ?ans
(* Output: 3 *)
CEILING OF
CEILING OF 3.1 SET ?ans
AFTER EMIT ?ans
(* Output: 4 *)
RAISE ... TO
RAISE 2 TO 8 SET ?ans
AFTER EMIT ?ans
(* Output: 256 *)
MODULO
MODULO 17 BY 5 SET ?ans
AFTER EMIT ?ans
(* Output: 2 *)

Inline References with Natural Language Math

Natural-language math forms can be nested inline using square brackets, exactly like CALCULATE.

Nested Inline Math
NUMBER 16 SET ?num
AFTER MULTIPLY 3 BY [SQUARE ROOT OF ?num] SET ?ans
AFTER EMIT ?ans
(* Output: 12 *)

!THAT — The Last Returned Value

!THAT is a read-only global that always holds the result of the most recently completed statement, whether or not that statement used SET. It updates after every single statement, chained or not.

Chaining Through !THAT
ADD 3 TO 5
AFTER MULTIPLY !THAT BY 6
AFTER EMIT !THAT
(* 3 + 5 = 8, then 8 * 6 = 48, then EMIT reads !THAT = 48 *)