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 "Hello" SET ?greet
AFTER EMIT ?greet
Concatenation
The & operator joins strings. It works with literals, variables, or a mix of both.
STRING "Hello " & "World" SET ?full
AFTER EMIT ?full
STRING "Alice" SET ?name
AFTER STRING "Hello, " & ?name SET ?greeting
AFTER EMIT ?greeting
String Length
LENGTH returns the character count of a string.
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.
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.
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.
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.
ALL— replaces every occurrenceONCE— replaces the first occurrence onlyFROM [pos] TO [pos]— replaces by zero-based character position range
REPLACE STRING ALL "l" WITH "z" WHERE "hello" SET ?replaced
AFTER EMIT ?replaced
(* Output: hezzo *)
REPLACE STRING ONCE "l" WITH "z" WHERE "hello" SET ?once
AFTER EMIT ?once
(* Output: hezlo *)
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.
STRING "hello world" SET ?text
AFTER SEARCH STRING ?text FOR "world" SET ?pos
AFTER EMIT ?pos
(* Output: 6 *)
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.
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 "a,b,c" BY "," SET ?parts
AFTER EMIT ?parts(0)
AFTER EMIT ?parts(1)
AFTER EMIT ?parts(2)
(* Output: abc *)
SPLIT "abc" BY "" SET ?chars
AFTER EMIT ?chars(0)
(* Output: a *)
Creating Numbers
The NUMBER verb constructs a number value.
NUMBER 42 SET ?age
AFTER EMIT ?age
CALCULATE
CALCULATE evaluates a mathematical expression. Supports + - * /. Operator precedence follows standard math rules. Parentheses override precedence.
CALCULATE 10 + 5 SET ?sum
AFTER EMIT ?sum
NUMBER 10 SET ?a
AFTER NUMBER 5 SET ?b
AFTER CALCULATE ?a + ?b SET ?result
AFTER EMIT ?result
CALCULATE 10 + 5 * 2 SET ?complex
AFTER EMIT ?complex
(* Output: 20 — multiplication runs first *)
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.
CALCULATE ABS(-42) SET ?abs
AFTER EMIT ?abs
(* Output: 42 *)
CALCULATE ROUND(3.14159) SET ?rounded
AFTER EMIT ?rounded
(* Output: 3 *)
CALCULATE FLOOR(3.9) SET ?floor
AFTER EMIT ?floor
(* Output: 3 *)
CALCULATE CEIL(3.1) SET ?ceil
AFTER EMIT ?ceil
(* Output: 4 *)
CALCULATE POWER(2, 8) SET ?pow
AFTER EMIT ?pow
(* Output: 256 *)
CALCULATE SQRT(16) SET ?root
AFTER EMIT ?root
(* Output: 4 *)
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 1 TO 79 SET ?ans
AFTER EMIT ?ans
NUMBER 10 SET ?num
AFTER ADD ?num TO 5 SET ?ans
AFTER EMIT ?ans
SUBTRACT 9 FROM 79 SET ?ans
AFTER EMIT ?ans
MULTIPLY 10 BY 79 SET ?ans
AFTER EMIT ?ans
AFTER MULTIPLY 10 WITH 79 SET ?ans2
AFTER EMIT ?ans2
(* BY and WITH are interchangeable *)
NUMBER 20 SET ?num
AFTER DIVIDE ?num BY 5 SET ?ans
AFTER EMIT ?ans
SQUARE ROOT OF 16 SET ?ans
AFTER EMIT ?ans
(* Output: 4 *)
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 10 SET ?ans
AFTER EMIT ?ans
FACTORIAL OF 5 SET ?ans
AFTER EMIT ?ans
PRIME OF 100 SET ?ans
AFTER EMIT ?ans
ABSOLUTE VALUE OF -42 SET ?ans
AFTER EMIT ?ans
(* Output: 42 *)
ROUND OF 3.14159 SET ?ans
AFTER EMIT ?ans
(* Output: 3 *)
FLOOR OF 3.9 SET ?ans
AFTER EMIT ?ans
(* Output: 3 *)
CEILING OF 3.1 SET ?ans
AFTER EMIT ?ans
(* Output: 4 *)
RAISE 2 TO 8 SET ?ans
AFTER EMIT ?ans
(* Output: 256 *)
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.
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.
ADD 3 TO 5
AFTER MULTIPLY !THAT BY 6
AFTER EMIT !THAT
(* 3 + 5 = 8, then 8 * 6 = 48, then EMIT reads !THAT = 48 *)