Built-ins

Built-in Namespaces

Serez Code ships with three built-in namespaces — Math, File, and JSON — available everywhere without imports.

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)

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