Syntax · 3 of 6

Operators

OxiScript has the operators you'd expect from a JavaScript-shaped language. They behave on JSON values.

Arithmetic

OpUseExample
+adda + b
-subtract / unary negatea - b, -x
*multiplyprice * qty
/dividetotal / count
%moduloi % 2
proc invoice(qty, unit_price, tax_rate) {
    let subtotal = qty * unit_price
    let tax      = subtotal * tax_rate
    let total    = subtotal + tax
    return {subtotal: subtotal, tax: tax, total: total}
}

Comparison

OpUse
==equal
!=not equal
< > <= >=numeric & string ordering
proc tier(spend) {
    if spend >= 10000 { return "platinum" }
    if spend >= 1000  { return "gold" }
    if spend >= 100   { return "silver" }
    return "bronze"
}

Logical

OpUse
&&and (short-circuits)
||or (short-circuits)
!not
proc can_purchase(user, item) {
    if user.is_adult && user.balance >= item.price {
        return {ok: true}
    }
    return {ok: false}
}
proc validate(input) {
    if input == null || input.email == null {
        abort "email required"
    }
    return {ok: true}
}

Field access

proc deep(doc) {
    let city = doc.user.address.city
    let first_tag = doc.tags[0]
    let by_key = doc["first name"]
    return {city: city, first_tag: first_tag, by_key: by_key}
}

Operator precedence (high → low)

  1. Field access (.), index ([]), call (f(...)), unary (-, !)
  2. *, /, %
  3. +, -
  4. <, >, <=, >=
  5. ==, !=
  6. &&
  7. ||

Use parens to make intent explicit:

let ok = (a + b) * c > threshold && user.active

Combining everything

proc score(user, opts) {
    let base = user.purchases * 10
    let bonus = 0
    if user.is_premium {
        bonus = 500
    }
    let final = base + bonus - opts.penalty
    if final < 0 {
        final = 0
    }
    return final
}
Note: reassignment (x = expr without let) parses, but it's the same statement form as let x — variables shadow the previous binding. Practically: just always write let.
Report Issue