# OcaltQL > OcaltQL (Ocalt Query Language) is a verb-first scripting language that runs on Ocalt's servers. One script, sent in one HTTP request, can do databases, files, HTTP, email, dates, maths, HTML, remote machines and more, and returns whatever it EMITs. This file teaches an AI assistant to write correct OcaltQL. The full specification is the tutorial at https://ql.ocalt.com/tutorial/ ; where this file and the tutorial differ, the tutorial wins. ## Grammar and checking The complete grammar is at https://ql.ocalt.com/grammar/ocaltql.ebnf (every verb with its operand, modifiers and which are required) and https://ql.ocalt.com/grammar/ocaltql.gbnf (to constrain a model's output). Check a script without running it with the API's dry_run=1 field, the check_ocaltql tool of the OcaltQL MCP server, or the parser described at https://ql.ocalt.com/tutorial/grammar-parser . ## Running a script POST https://ql.ocalt.com/api with three form fields: - identity: the Ocalt account email - password: the account's OcaltQL API key (never put a real key in code you share) - q: the script curl -X POST https://ql.ocalt.com/api \ --data-urlencode "identity=you@example.com" \ --data-urlencode "password=$OCALTQL_KEY" \ --data-urlencode 'q=EMIT "Hello"' The response body is everything the script EMITted. A failing script returns HTTP 4xx or 5xx with a message such as `Line 2: invalid JSON ...`. ## Statement shape [CHAIN] VERB [ARGS] [PREPOSITION TARGET] [SET ?var] - Variables start with `?`: `?name`, `?rows`. Global request values start with `!`: `!GET('id')`, `!NOW('year')`. - Strings use double quotes. Join values with `&`: `"Hi " & ?name`. A newline inside a string is `\n`. - Comments are `(* like this *)` and do not count as statements. - Read an array element or object key with brackets: `?rows(0)("name")`. ## Chaining rules (the most important part) Statements are joined by a chain keyword, not by line breaks. - The FIRST statement of a script has no chain keyword. So does the first statement inside every `OPEN ... CLOSE` block. - `AFTER` - run this statement after the previous one has finished. Use it for normal step-by-step code. - `AND` - run this statement in parallel with the previous one. - `OR` - run this statement only if the previous one failed or returned false or null. After an `IF ... CLOSE`, `OR OPEN ... CLOSE` is the else branch. - A whole `IF/LOOP/FOREACH/WHILE ... OPEN ... CLOSE` block counts as ONE statement in the outer chain, so the statement after `CLOSE` needs `AFTER`. - There is no ELSE keyword. Use `OR`, and `OR IF` for else-if. Wrong (missing chain keyword): STRING "a" SET ?x EMIT ?x Right: STRING "a" SET ?x AFTER EMIT ?x ## The nesting rule OcaltQL is nestable everywhere; only operations are not nestable within each other. Blocks nest to any depth and inline `[ ... ]` references can sit inside each other, but a `NEW OPERATION` must never be defined inside another `NEW OPERATION`. ## Top 30 verbs EMIT value output text or a value STRING "text" SET ?s make a string NUMBER 42 SET ?n make a number SET ?var AS value assign any value UNSET ?var delete a variable CALCULATE ?a * (?b + 2) SET ?r maths: + - * / ( ), ROUND() SQRT() MOD() ... LENGTH ?s SET ?n string length REPLACE STRING ALL "a" WITH "b" WHERE ?s SET ?out replace text SPLIT ?s BY "," SET ?parts string to array NEW ARRAY SET ?a / APPEND value TO ?a make and grow an array COUNT ?a SET ?n number of items JOIN ?a WITH ", " SET ?s array to string NEW OBJECT ["k" AS 1 AND "j" AS 2] SET ?o object literal; SET ?o("k") AS 5 to change a key KEYS ?o SET ?k / VALUES ?o SET ?v object keys or values IF ?n IS GREATER THAN 3 OPEN ... CLOSE condition (IS EQUAL TO, IS LESS THAN, CONTAINS, IS EMPTY, IS SET, IS NULL ...) FOREACH ?a SET ?item OPEN ... CLOSE loop over values (FOREACH ?o SET ?key AS ?value for pairs) LOOP 1 TO 10 SET ?i OPEN ... CLOSE counted loop (STEP n optional) WHILE ?n IS LESS THAN 5 OPEN ... CLOSE loop while true; BREAK and CONTINUE work inside loops NEW OPERATION name WITH ?p OPEN ... RETURN v CLOSE define a reusable operation RUN name WITH "x" SET ?r call an operation FETCH "https://..." SET ?body HTTP GET (SET PROMISE ?p plus WAIT FOR ?p for async) PARSE ?s AS JSON SET ?data parse JSON, CSV, XML, YAML, TOML, HTML, MARKDOWN ... CAST ?o AS JSON SET ?s convert type: STRING, NUMBER, BOOL, DATE, JSON FILE WRITE "text" TO "/root/a.txt" write a file in your namespace FILE READ "/root/a.txt" SET ?s read a file (FILE DELETE "path" removes it) NEW DB "shop" / NEW TABLE "t" IN DB "shop" COLUMNS ADD ... create a database and table INSERT INTO DB "shop" TABLE "t" ROW "col" AS v AND "c2" AS w SET ?id insert a row SELECT ROWS FROM DB "shop" TABLE "t" WHERE "age" IS GREATER THAN 25 SET ?rows query rows (UPDATE ROWS, DELETE ROWS, COUNT ROWS work alike) NEW DATE AND TIME SET ?now current date and time; CALCULATE ?d + 3 DAY for date maths MAIL TEXT BODY "Hi" TO "a@b.com" SUBJECT "Hello" send an email REMEMBER "key" AS v / RECALL "key" SET ?v permanent key-value storage (FORGET "key" deletes) CACHE STORE "k" AS v TTL 10 MINUTES / CACHE GET "k" SET ?v storage that expires CATCH ERROR SET ?err catch the previous statement's error and keep going CURRENCY RATE FROM "USD" TO "ZAR" SET ?r exchange rate WEATHER "Durban" SET ?w current weather as an object The complete list of verbs is at https://ql.ocalt.com/tutorial/verb-reference ## Worked scripts (each one tested against the live API) 1. Variables, maths and output STRING "Ada" SET ?name AFTER NUMBER 3 SET ?n AFTER CALCULATE ?n * 14 SET ?total AFTER EMIT "Hello " & ?name & ", total " & ?total Output: `Hello Ada, total 42` 2. Arrays, a loop and a condition inside it NEW ARRAY SET ?prices AFTER APPEND 12 TO ?prices AFTER APPEND 40 TO ?prices AFTER APPEND 7 TO ?prices AFTER NUMBER 0 SET ?sum AFTER FOREACH ?prices SET ?p OPEN CALCULATE ?sum + ?p SET ?sum AFTER IF ?p IS GREATER THAN 10 OPEN EMIT "big: " & ?p & "\n" CLOSE CLOSE AFTER EMIT "sum: " & ?sum Output: `big: 12`, `big: 40`, `sum: 59` 3. If and else NUMBER 5 SET ?n AFTER IF ?n IS GREATER THAN 3 OPEN EMIT "big" CLOSE OR OPEN EMIT "small" CLOSE Output: `big` 4. Calling a web API and reading its JSON FETCH "https://open.er-api.com/v6/latest/USD" SET ?raw AFTER PARSE ?raw AS JSON SET ?fx AFTER EMIT "1 USD = " & ?fx("rates")("EUR") & " EUR" 5. A reusable operation NEW OPERATION greet WITH ?who OPEN RETURN "Hi " & ?who CLOSE AFTER RUN greet WITH "Sam" SET ?msg AFTER EMIT ?msg Output: `Hi Sam` 6. A database from start to finish NEW DB "demo" AFTER NEW TABLE "people" IN DB "demo" COLUMNS ADD "id" TYPE NUMBER AUTOINCREMENT PRIMARY AND ADD "name" LENGTH 100 TYPE STRING REQUIRED AND ADD "age" TYPE NUMBER DEFAULT 0 AFTER INSERT INTO DB "demo" TABLE "people" ROW "name" AS "Thandi" AND "age" AS 31 SET ?id AFTER INSERT INTO DB "demo" TABLE "people" ROW "name" AS "Sipho" AND "age" AS 24 AFTER SELECT ROWS FROM DB "demo" TABLE "people" WHERE "age" IS GREATER THAN 25 SET ?rows AFTER FOREACH ?rows SET ?r OPEN EMIT ?r("name") & " is " & ?r("age") & "\n" CLOSE Output: `Thandi is 31` 7. A file and a CSV FILE WRITE "name,qty\napple,3\npear,5" TO "/root/stock.csv" AFTER FILE READ "/root/stock.csv" SET ?csv AFTER PARSE ?csv AS CSV SET ?rows AFTER COUNT ?rows SET ?n AFTER EMIT ?n & " rows, first: " & ?rows(0)("name") Output: `2 rows, first: apple` 8. Two things at once with AND, then an object as JSON STRING "a" SET ?x AND STRING "b" SET ?y AFTER NEW OBJECT ["first" AS ?x AND "second" AS ?y] SET ?o AFTER CAST ?o AS JSON SET ?json AFTER EMIT ?json Output: `{"first": "a", "second": "b"}` ## Common mistakes to avoid - Forgetting `AFTER` before the second statement. Every statement except the first in its chain needs a chain keyword. - Writing ELSE. Use `OR OPEN ... CLOSE` after the IF block. - Defining a `NEW OPERATION` inside another one. - Using `=` or `==`. Assign with `SET ?x AS value`; compare with `IS EQUAL TO`, `IS GREATER THAN` and the other IS forms. - Inventing verbs. If a verb is not in the verb reference, it does not exist. ## More - Tutorial and specification: https://ql.ocalt.com/tutorial/ - Verb reference: https://ql.ocalt.com/tutorial/verb-reference - API reference: https://ql.ocalt.com/tutorial/api-reference - Chaining: https://ql.ocalt.com/tutorial/chaining