Skip to content

Latest commit

 

History

History
161 lines (112 loc) · 2.17 KB

File metadata and controls

161 lines (112 loc) · 2.17 KB

Nimble Syntax Reference

Nimble is indentation‑sensitive with a minimal, Python‑like syntax.

Comments

# This is a single‑line comment. No block comments.

Variables

Variables are mutable by default. Type annotation is optional.

x = 10                  # Inferred int
name = "Nimble"        # Inferred str
pi float = 3.14         # Explicit float
active bool = true      # Explicit bool

Types

Primitive types:

  • int (64‑bit)
  • float (64‑bit)
  • str
  • bool
  • null

Compound types:

  • Lists: [T]
  • Maps: {K: V}
  • Unions: A | B

Example:

items [int] = [1, 2, 3]
meta {str: str} = {"id": "123"}
result int | error = safe_divide(10, 2)?

Strings & Interpolation

Use {expr} inside strings. To render literal braces, use {{ and }}.

name = "Nimble"
out("Hello, {name}!")

Control Flow

If / Elif / Else

if x > 10:
    out("Greater")
elif x == 10:
    out("Equal")
else:
    out("Smaller")

Ternary Expression

label = "adult" if age >= 18 else "minor"

Loops

Range Loop

for i in 0..10:
    out(i)

Range Loop with Step

for i in 0..10 step 2:
    out(i)

Collection Loop

for item in [1, 2, 3]:
    out(item)

for key, val in {"a": 1}:
    out("{key}: {val}")

While Loop

while x > 0:
    x -= 1

Functions

Defined with the fn keyword.

fn add(a int, b int) -> int:
    return a + b

# Short syntax
fn square(x int) -> int = x * x

Lambdas

spawn fn():
    out("Async task")

Classes (Structs)

cls defines a data structure. Fields require types.

cls User:
    name str
    age  int

user = User(name="Alice", age=30)
out(user.name)

Error Handling

Errors are values. Use | error for fallible return types and ? for propagation.

fn check(n int) -> int | error:
    if n < 0: return error("Negative")
    return n

val = check(-1)?

Modules

Use load to import modules and export to make symbols public.

load math
load utils from "./local_utils"

export fn add(a int, b int) -> int:
    return a + b