Varse

Documentation

Everything you need to write programs in Varse. Back to home

Installation

Varse is a single executable with no external dependencies. Build from source with NASM and GoLink:

git clone https://github.com/varse-lang/varse
cd varse
nasm -f win64 varse.asm -o varse.obj
golink /entry:main /console varse.obj kernel32.dll user32.dll gdi32.dll gdiplus.dll

Hello World

Create a file called hello.var:

fn main() {
    print(42);
}
42

Running Code

Every Varse program must have a fn main() function. The interpreter parses all definitions first, then finds and executes main.

varse hello.var

Variables

Variables store values that can be changed. Declared with let.

let name = expression;
fn main() {
    let x = 10;
    let y = x + 5;
    print(y);
    x = 20;
    print(x);
}
15 20

Constants

Constants are immutable values declared with def. They are resolved at the start of execution.

def name: value;
def pi: 314;
def max_retries: 3;

fn main() {
    print(pi);
}
314

Functions

Functions are declared with fn and contain a block of statements. Functions can be nested.

fn name(params) {
    statements
}
fn add(a + b) {
    return a + b;
}

fn main() {
    print(add(10 + 20));
}
30

Parameters

Parameters are separated by + (or ,) in both the definition and the call. When calling a function, + acts as the argument separator. To pass an arithmetic expression as a single argument, wrap it in parentheses.

SyntaxDescription
fn(a + b)Two parameters
fn(x)One parameter
fn()No parameters
add(10 + 20)Call with 2 arguments (10 and 20)
add((x + y))Call with 1 argument (the expression x + y)

Return Values

Use return to send a value back from a function.

fn fib(n) {
    if n <= 1 {
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}

fn main() {
    print(fib(10));
}
55

Operators

Arithmetic

OpDescriptionExample
+Addition / parameter separatorx + y
-Subtraction / unary negationx - y, -x
*Multiplicationx * y
/Integer divisionx / y

Comparison

OpDescription
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Precedence (high to low)

LevelOperators
1Unary -
2*, /
3+, -
4<, >, <=, >=, ==, !=

Comments

// This is a single-line comment

/*
   This is a
   block comment
*/

If / Else

Conditional execution with if, else if, and else.

fn classify(n) {
    if n < 10 {
        return 1;
    } else if n < 100 {
        return 2;
    } else {
        return 3;
    }
}

fn main() {
    print(classify(4));
    print(classify(42));
    print(classify(999));
}
1 2 3

Blocks & Scope

Variables declared inside a function are scoped to that function. Nested functions can access variables from enclosing scopes.

def base: 10;

fn main() {
    fn add(x + y) {
        return x + y;
    }
    let sum = add(base + 2);
    print(sum);
}
12

print()

Built-in function that outputs a number to the console.

ParameterTypeDescription
expressionnumberThe value to output. Any expression that evaluates to a number.

Multiple print() calls concatenate on the same line with no separator.

fn main() {
    print(197);
    print(12345);
}
19712345

GUI Framework

Varse includes a built-in GUI system for creating native Windows applications with styled widgets, rounded corners, and click handlers. The GUI uses Win32 API and GDI under the hood.

A GUI program defines widgets, registers click handlers, then calls show to enter the message loop.

window

Creates the application window. Must be the first GUI statement.

window "title" width height bg=#HEX;
ParameterTypeDescription
titlestringWindow title bar text
widthnumberWidth in pixels
heightnumberHeight in pixels
bghexBackground color #RRGGBB (optional)
window "My App" 800 600 bg=#121212;

button

Creates a clickable button with optional rounded corners and color.

button name x y w h "text" radius=N color=#HEX;
ParameterTypeDescription
nameidentifierUnique name for onclick
x, ynumberPosition from top-left
w, hnumberWidth and height in pixels
textstringLabel text, centered
radiusnumberCorner radius, default 8 (optional)
colorhexBackground color #RRGGBB (optional)

label

Creates a text label. Not clickable.

label name x y w h "text" color=#HEX;
ParameterTypeDescription
nameidentifierUnique name
x, ynumberPosition from top-left
w, hnumberText bounding box
textstringText to display
colorhexText color #RRGGBB (optional, default #000000)

show

Creates the window and enters the message loop. Blocks until the window is closed.

show;

onclick

Registers a callback that runs when a button is clicked.

onclick button_name {
    statements
};
window "Clicker" 400 200 bg=#222222;

button btn1 20 20 160 50 "Print 42" radius=25 color=#1DB954;

onclick btn1 {
    print(42);
};

show;

Example: Fibonacci

fn fib(n) {
    if n <= 1 {
        return 1;
    }
    return fib(n - 1) + fib(n - 2);
}

fn classify(n) {
    if n < 10 { return 1; }
    else if n < 100 { return 2; }
    else { return 3; }
}

fn main() {
    let z = fib(10);
    print(z);
    print(classify(4));
    print(classify(10));
    print(classify(99));
}
891234

Example: GUI App

window "Varse GUI" 600 400 bg=#222222;

label title 20 20 300 40 "Hello from Varse!" color=#FFFFFF;

button btn1 20 130 180 50 "Print Number" radius=10 color=#CC4422;
button btn2 220 130 180 50 "Say Hello" radius=10 color=#226644;
button btn3 20 200 180 50 "Big Radius" radius=25 color=#4444CC;

onclick btn1 { print(42); };
onclick btn2 { print(100); };
onclick btn3 { print(999); };

show;