Basics of Yabal

This page will teach you the basics of Yabal.

Comments

Comments are used to add notes to your code. They are ignored by the compiler.

There are two types of comments in Yabal: single-line comments and multi-line comments.

Single-line comments start with // and end at the end of the line:

// This is a single-line comment

Multi-line comments start with /* and end with */:

/*
This is a multi-line comment
*/

Types

Yabal is a statically typed language. An statically typed language means that the type of a variable is known at compile time. This is in contrast to a dynamically typed language (like JavaScript), where the type of a variable is only known at runtime.

Yabal has the following types:

TypeDescription
intA 16-bit unsigned integer
boolA boolean value (true or false) stored as a 16-bit integer
type[]An array of values of the specified type
func<parameters, return type>A function that takes the specified parameters and returns the specified type
UserDefinedAn user-defined struct

Structs

A struct is a user-defined type. It contains a number of fields, which can be of any type.

Structs are declared using the struct keyword, followed by the name of the struct, and then a block of code surrounded by curly braces ({ and }). Within the block of code, you can declare fields by specifying the type and name of the field.

For example, we declare a struct called Position with two fields, x and y:

struct Position {
    int x
    int y
}

Arrays

An array is a collection of values of the same type. Arrays are declared using the [] operator:

int[] numbers // An array of integers

Statements

The actual code in a Yabal program is made up of statements. Common statements are variable declarations, declaring functions, calling functions and control flow statements.

A statement can be a single line of code that ends with a new-line or semi-colon. A statement can also be multiple lines of code surrounded by curly braces ({ and }).

The following code shows an example of a block of code:

// Variable declaration statement
int variable

// Assignment statement
variable = 0

// If statement that contains multiple statements
if (variable == 0) {
    // Assignment statement
    variable = 1

    // Expression statement (postfix increment)
    variable++
}

Expressions

An expression is a statement that evaluates to a value. For example, 1 + 2 is an expression that evaluates to 3.

Expressions can be used in many places, such as in assignment statements, function calls, and control flow statements.

Variables

Variables are used to store values in memory.

You can declare variable specifying the type and name of the variable:

// Create an integer variable called "variable"
int variable

You can also assign a value to a variable when declaring it by using the = operator:

// Create an integer variable called "variable" and assign it the value 1
int variable = 1

It is also possible to let the compiler infer the type of the variable. This is done by using the var keyword. The compiler will look at the value that is assigned to the variable, and infer the type from that:

// Create an integer variable called "variable" and assign it the value 1
var variable = 1

Semi-colons

Semi-colons can be used to separate statements. They are optional, but are required if you want to put multiple statements on the same line.

For example, this is valid:

int a = 1

But this is not, since there are multiple statements on the same line:

int a = 1 int b = 2

If you want to put multiple statements on the same line, you can use semi-colons to separate them:

int a = 1; int b = 2

Functions

Functions are used to reuse code. When you declare a function with code, you can call it from other places in your code.

Functions are specified the return type and name of the function, followed by the parameters of the function in parentheses (( and )), and then a block of code surrounded by curly braces ({ and }).

To return a value from a function, use the return keyword followed by the value to return.

For example, the following code declares a function called add that takes two integers as parameters and returns an integer:

// Declare the add function
// The function takes two integers (a and b) as parameters and returns an integer
int add(int a, int b) {
    return a + b
}

Functions can also be declared without a return type by specifying void as the return type. A function with a return type of void cannot return a value.

For example, the following code declares a function called print that takes a int array as a parameter and returns nothing:

void print(int[] data) {
    // Print the value to the screen
}

Calling functions

To call a function, use the name of the function followed by the arguments of the function in parentheses (( and )).

For example, the following code calls the add function that we declared earlier:

// Call the add function and store the result in the "result" variable
int result = add(1, 2)

int add(int a, int b) {
    return a + b
}