the tour

All of W, by example. Everything here runs.

blocks and comments

Blocks are indented (spaces). A one-line block follows a comma.

-- a comment
---
a longer one
---
note a doc comment
fun w()
    say true
        log("a block")
    say true, log("one line")

types and variables

fun w()
    dec n: num = 42          -- a whole number (C's int; it wraps around)
    dec r: real = 2.5        -- C's float
    dec c: chr = 'w'
    dec s: str = "text"      -- memory handled for you
    dec b: bool = true
    dec guess := n + 1       -- := takes the value's type
    n = n * 2
    log(n, r, c, s, b, guess)

zil is "no value": a function without a return type returns zil.

operators

fun w()
    dec a := 7
    log(a + 1, a * 2, a / 2, a % 3, a mod 3, -a)
    log(a is 7, a is not 7, a < 10 and a > 1, not (a > 9))
    log("he" + "llo", "abc" < "abd")

is / ==, is not / !=, lt gt le ge as well as < > <= >=. Dividing by zero stops the program with the line it happened on.

choosing

fun w()
    dec x := 12
    say x > 10
        log("big")
    elf x > 5
        log("medium")
    else
        log("small")
    lest x is 0, log("not zero")
    when x % 2 is 0, log("even")

say and when are if; lest is unless; elf is else if; nor is else unless.

loops

fun w()
    dec i := 0
    loop while i < 3, i = i + 1
    loop until i is 0, i = i - 1
    loop forever
        i = i + 1
        say i is 2, skip
        say i > 4, stop
    loop x in [10, 20], log(x)
    loop i2, word in ["a", "b"], log(i2, word)

functions

fun add(a: num, b: num): num
    ret a + b

fun grow(n: ref num)            -- ref: changes the caller's variable
    n = n + 1

fun w(): num
    dec total := add(2, 3)
    grow(ref total)
    log(total)                  -- 6
    ret 0

Functions can come in any order. A call with ref says so, so you can see what may change.

pods

pod Vec
    x: real
    y: real

pod Ball
    pos: Vec
    name: str

fun w()
    dec b := Ball(pos: Vec(x: 1, y: 2), name: "ball")
    b.pos.x = b.pos.x + 10
    dec copy := b            -- pods are values: this is a copy
    copy.name = "other"
    log(b)                   -- Ball(pos: Vec(x: 11.000000, y: 2.000000), name: ball)

vecs

fun w()
    dec v := [1, 2, 3]
    v.add(4)
    v.add_front(0)
    v[1] = 10
    dec last := v.pop()
    dec first := v.pop_front()
    log(v, v.len, last, first)    -- [10, 2, 3] 3 4 0
    dec names: vec[str] = []
    names.add("ann")

Copying a vec is as cheap as a pointer: it's only copied when one of the copies changes. An index outside the vec stops the program with a trace.

modules and C

-- lib/shapes.w
pod Circle
    r: real

fun area(c: Circle): real
    ret 3.14 * c.r * c.r
-- main.w
use lib.shapes
ext lib "m"
ext fun sqrtf(x: real): real

fun w()
    dec c := shapes.Circle(r: 2)
    log(shapes.area(c), sqrtf(16))

What a module defines is used by its name; ext fun declares a C function. The standard library (use raylib) is found when there's no file of that name next to yours.