Built-ins

Built-in Namespaces

Serez Code ships with built-in namespaces available everywhere without imports. OS/hardware namespaces (Terminal, OS, Env, Time, System) require explicit permission declarations.

Math

All math functions are called as Math.functionName(args):

out Math.PI    // → 3.141592653589793
out Math.E     // → 2.718281828459045

// Rounding
out Math.floor(3.9)    // → 3
out Math.ceil(3.1)     // → 4
out Math.round(3.5)    // → 4
out Math.trunc(-3.9)   // → -3  (toward zero)

// Basic
out Math.abs(-7)       // → 7
out Math.sqrt(16.0)    // → 4.0
out Math.pow(2.0, 10.0)  // → 1024.0

// Min / max / clamp
out Math.min(3, 1, 4, 1, 5)    // → 1
out Math.max(3, 1, 4, 1, 5)    // → 5
out Math.clamp(15, 0, 10)      // → 10

// Logarithms
out Math.log(Math.E)   // → 1.0
out Math.log2(8.0)     // → 3.0
out Math.log10(1000.0) // → 3.0

// Random — decimal in [0, 1)
out Math.random()

Trigonometry

All trig functions use radians:

out Math.sin(Math.PI / 2.0)   // → 1.0
out Math.cos(0.0)              // → 1.0
out Math.tan(Math.PI / 4.0)   // → ~1.0

out Math.atan2(1.0, 1.0)      // → 0.785...  (π/4)

// Degrees → radians helper
let deg = 90.0
let rad = deg * Math.PI / 180.0
out Math.sin(rad)   // → 1.0

File I/O

Read and write files with the File namespace:

// Write a file (creates it if it doesn't exist, overwrites if it does)
File.write("data.txt", "Hello, world!")

// Check if it exists
out File.exists("data.txt")   // → true

// Read the whole file as a string
let content = File.read("data.txt")
out content   // → Hello, world!

// Create an empty file (no-op if it already exists)
File.create("log.txt")

Binary files

// Read raw bytes — returns [int] where each value is 0-255
let bytes = File.read_asBinary("image.png")
out bytes.length   // number of bytes

// Write raw bytes
File.write_asBinary("copy.png", bytes)

Extended file system methods

// List directory contents
let entries = File.listDir("./src")
out entries   // → [main.sz, utils.sz, ...]

// Create a directory (including parent dirs)
File.mkdir("output/logs")

// File metadata
let stat = File.stat("data.txt")
out stat.size      // → bytes
out stat.isDir     // → false
out stat.modified  // → Unix ms timestamp

// Rename / move (requires unsafe)
unsafe {
    File.rename("old.txt", "new.txt")
}

// Delete file or directory (requires unsafe)
unsafe {
    File.delete("temp_dir")
}

Practical example — save and load JSON data

let data <string, any> = (
    {"name", "Sergio"},
    {"score", 42},
    {"active", true}
)

// Save
File.write("save.json", JSON.stringify(data))

// Load
let loaded = JSON.parse(File.read("save.json"))
out loaded["name"]   // → Sergio

JSON

Serialize any value to JSON and parse it back:

// Stringify — works with any value
out JSON.stringify(42)          // → "42"
out JSON.stringify(true)        // → "true"
out JSON.stringify([1, 2, 3])   // → "[1,2,3]"

let user <string, any> = ({"name", "Sergio"}, {"age", 28})
out JSON.stringify(user)
// → {"name":"Sergio","age":28}

// Parse — returns the equivalent Serez value
let json = '{"x": 10, "y": 20}'
let obj = JSON.parse(json)
out obj["x"]   // → 10

// Round-trip
let original = [1, "hello", true, null]
let json_str = JSON.stringify(original)
let parsed   = JSON.parse(json_str)
out parsed[1]   // → hello

Permissions

OS/hardware namespaces are sandboxed by default. Grant access with a three-level permission model:

// Level 1 — serez.json (project-wide)
// { "permissions": ["Terminal", "OS", "Env"] }

// Level 2 — file-level
use permissions { OS, Time }

// Level 3 — operation-level (unsafe required for destructive ops)
unsafe {
    OS.exec("git", ["status"])
    File.delete("temp.txt")
}

Terminal

Interact with the terminal emulator — keyboard, mouse, cursor, raw mode. Requires use permissions { Terminal }.

use permissions { Terminal }

// Terminal size
let size = Terminal.getSize()
out "Cols: {size[0]}, Rows: {size[1]}"

// Clear screen and move cursor
Terminal.clear()
Terminal.setCursor(0, 0)

// Write raw byte to stdout (e.g. ANSI ESC = 27)
Terminal.writeByte(27)

// Raw mode + keyboard + mouse (all require unsafe)
unsafe {
    Terminal.setRawMode(true)
    Terminal.enableMouse(true)

    let evt = Terminal.readEvent()
    if (evt.type == "key") {
        out "Key: {evt.code}"         // "a", "Enter", "Esc", "F1", ...
        out "Mods: {evt.modifiers}"   // ["ctrl"], ["shift"], ...
    } else if (evt.type == "mouse") {
        out "Mouse {evt.kind} at {evt.col},{evt.row}"
        out "Button: {evt.button}"    // "left", "right", "middle"
    } else if (evt.type == "resize") {
        out "New size: {evt.cols}x{evt.rows}"
    }

    Terminal.enableMouse(false)
    Terminal.setRawMode(false)
}

OS

Process and operating system information. Requires use permissions { OS }.

use permissions { OS }

out OS.platform()   // → "windows" | "linux" | "macos"
out OS.pid()        // → current process ID

// Execute external command (requires unsafe)
let result = null
unsafe {
    result = OS.exec("git", ["log", "--oneline", "-5"])
}
out result.stdout   // command output
out result.code     // exit code (0 = success)

// Kill a process by PID (requires unsafe)
unsafe {
    OS.kill(1234)
}

Env

Environment variables and command-line arguments. Requires use permissions { Env }.

use permissions { Env }

out Env.get("HOME")     // → "/Users/sergio" or null if not set
out Env.get("PATH")     // → full PATH string

let args = Env.args()   // command-line args including program name
out args.length

// Set env var (requires unsafe — not thread-safe)
unsafe {
    Env.set("MY_VAR", "hello")
}
out Env.get("MY_VAR")   // → hello

Time

Timestamps and sleep. Requires use permissions { Time }.

use permissions { Time }

let t1 = Time.now()   // Unix timestamp in milliseconds
Time.sleep(500)       // pause 500ms
let t2 = Time.now()
out t2 - t1           // → ~500

System

Read-only system information. Requires use permissions { System }.

use permissions { System }

out System.cpuCount()      // → 15  (logical cores)
out System.totalMemory()   // → 34279034880  (bytes)
out System.freeMemory()    // → 13000000000  (bytes)
out System.hostname()      // → "DESKTOP-XYZ"
out System.uptime()        // → 168517  (seconds since boot)