Skip to content

Basics & Syntax

This reference covers the core syntax of Zigma, including variable declarations, mutability, type casting, and data structures.


Comments

Zigma uses double slashes // for both standalone and trailing comments. There are no multi-line block comments.

// This is a comment
g = 9.81 m/s2 // This is a trailing comment

Variables (Scalars)

By default, all scalars are constant (immutable) and are defined using the = operator.

x = 1
y = 2.5 m

Mutability

To define a mutable variable, use the := operator.

A mutable variable cannot change its physical dimension, shape, or underlying data type after initialization. It can only change its value and its scale (e.g., from meters to millimeters).

To update the variable later, you must use the := operator again and explicitly provide a compatible unit.

g := 9.81 m    // Declared as mutable Length
g := 15 m      // Valid update
g := 15000 mm  // Valid update (Same dimension, different scale)

// g := 15 s   // COMPILE ERROR: Cannot change dimension from Length to Time

Data Types

By default, all variables in Zigma are f64 (64-bit floating-point numbers). You can override this by explicitly casting to any numeric type available in Zig using the as keyword.

x = 1 as i32
y = 10.5 m/s as f32

Scoped Types

If you have multiple variables that need to share the same type, you can use a type block.

as i128:
  x = 1
  y = 2

GPU Type Limitations

When working with Tensors destined for GPU pipelines, you are limited to hardware-native types: f32, f64, i32, and i64.


Tensors (Data Structures)

In Zigma, everything is technically a Tensor. A scalar is simply a Tensor with a shape of {1}.

Instantiation

You can create Vectors (1D) and Matrices (2D) using bracket syntax. Dimensions and types apply to the entire tensor.

a = [1, 2, 3] m
m = [[1, 2], [3, 4]]

Shape Casting

You can initialize empty or uniform tensors of specific shapes using the as (shape) syntax.

a = 1 as (3)       // 1D Vector: [1, 1, 1]
b = 1 as (3, 3)    // 2D Matrix (3x3 filled with 1s)
c = 0 as (2, 3, 4) // 3D Tensor
d = 1 as (3) i32   // 1D Vector of type i32

Indexing and Slicing

Elements are accessed using 0-based indexing.

a = [10, 20, 30] m

// Element access
x = a[0]      // 10 m
y = a[1]      // 20 m

// Slicing (Uses range syntax)
slice_1 = a[0..2]  //[10, 20] m
slice_2 = a[1..]   // [20, 30] m (Implicit end)

For multi-dimensional tensors, indices are separated by commas. You can use : to select an entire row or column.

m = [[1, 2], 
     [3, 4]]

x = m[0, 1]     // Element at row 0, col 1 (Returns 2)
row = m[0, :]   // Entire first row [1, 2]
col = m[:, 1]   // Entire second column [2, 4]