Expected Features - banszkyy/BBLang GitHub Wiki

Expected Features

Some features that are exists in almost every languages.

Comments

Anything between // and the end of the line is considered a comment and won't be compiled.

You can also use /* and */ to for multiline comment.

Type System

Read more here

Variables

You can define a variable like this:

i32 number = 46;

In this example, the variable type is i32, the name is number, and the initial value is 46.

If no initial value is defined, the default value will be assigned (which is zero):

i32 number;

You can use the var keyword to specify the type of a variable based on its initial value:

var number = 46;

Local Variables

Any variable defined in a function will be available in its scope.

Global Variables

Any variable defined at the top level will be available throughout the whole program. You can also export these to allow other files to access them.

export var number = 42; // Exported global variable that can be accessed from any file.

Constants

You can define constants with the const keyword:

const var number = 42;

You can also define local and global constants, or export them, just like with variables.

Flow Control

You can use some basic flow controls to create loops and branches.

Conditional Branches

if (condition) {
  // ...
}
if (condition) {
  // ...
} else if (condition) {
  // ...
} else {
  // ...
}

Loops

You can create two types of loops: for loops and while loops. You can stop the loop with the break keyword. There is no continue, I will implement it when I feel like it.

While loop

while (condition) {
  // ...
}

For loop

for (i32 i; i < length; i++) {
  // ...
}

Three statements must be specified after the for keyword: variable declaration (executed once before the loop), any statement with a return value (executed every time before the loop, if it evaluates to false, the loop will stops) and any statement (executed every time after the loop).

[!IMPORTANT] If you compile to brainfuck, use the break statement carefully and expect buggy behavior.

Functions

You can define a function like this:

// This will return the sum of "a" and "b" parameters.
i32 add(i32 a, i32 b) {
  return a + b;
}

In this example, the function type is i32, the name is add, and it has two parameters of type i32 named a and b.

If the function returns nothing, use the void keyword as the type.

If you specify a type other than void and you don't return anything, thats will be some undefined behavior for you.

i32 something() {
  // You don't have to return anything, but idk what it will result in.
}

[!IMPORTANT] If you compile to brainfuck, use the return statement carefully and expect buggy behavior, especially if returning from loops or conditions.

Calling functions

You can call a function as follows:

i32 sum = add(33, 52);

In this example, the add function is called with the parameters 33 and 52.

If you don't assign the return value to anything or use it in another expression, it will be discarded.

Closures & lambda functions

You can define a lambda function with the => operator like so:

@int(int, int) func = (int a, int b) => {
  return a + b;
};

This will define an anonymous function that has two integer parameters, and returns an integer.

You can also capture locals form outside of the lambda's body:

int x = 0;
@int(int) func = (int v) => {
  return v + x;
};

Why put the @?

There are two kind of function pointers: ones with a captured closure, and ones without it.

You can mark function pointers with closures with the @ type modifiers.

Note that any function pointer without a closure can be implicitly converted into that has one, but not in the other way around.

Also note that if you take the address of a function, you will get a "raw" function pointer, without a closure. Also, if you define a lambda function, and you use the var keyword, the compiler will automatically decide if it's a "raw" function type or one with a closure.

Generics

I will explain it when I feel like to.