Z-Script Syntax & Expressions
Z-Script is an S-expression based Lisp language with native JSON data types.
1. Literals and Data Types
| Type | Examples |
|---|
| Numbers | 42, -10, 3.1415 |
| Booleans | true, false, nil |
| Strings | "hello world", "invoice #942" |
| Keywords | :status, :name, :id |
| Symbols | x, clean, input |
| Lists | '(1 2 3), (list 10 20 30) |
| Maps | {:name "Alice" :role "admin"} |
2. Defining Functions (define)
Functions are defined using (define (name args...) body...):
(define (clean s)
(trim (lower s)))
(clean " MUHAMMAD AHMED ")
;; => "muhammad ahmed"
Note: Recursion is strictly prohibited. Defining (define (f x) (f x)) fails ahead-of-time during AST compilation.
3. Conditionals (if and cond)
;; Standard if: (if condition then-expr else-expr)
(if (nil? (get input :ticket_id))
(fail "ticket_id is required")
{:accepted true :id (get input :ticket_id)})
;; Multi-branch cond: (cond test1 expr1 test2 expr2 :else default)
(cond
(> (get input :score) 90) "A"
(> (get input :score) 75) "B"
:else "C")
4. Pattern Matching (match)
Unpack nested maps, lists, and values with pattern variables (?var):
(match input
{:action "route" :tool ?t} (str "dispatching tool: " ?t)
{:action "decide"} "settled by System-1"
_ "unhandled action")
5. Functional Higher-Order Operations
(define (even? n) (= 0 (mod n 2)))
(map (fn [x] (* x 10))
(filter even? (get input :scores)))
When input is {:scores [1 2 3 4 5 6 7 8]}: Output is [20, 40, 60, 80].