Expected Features - BBpezsgo/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
Variables
You can define a variable like this:
i32 number = 46;
In this example, the variable type is
i32, the name isnumber, and the initial value is46.
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, and 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
breakstatement 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 isadd, and it has two parameters of typei32namedaandb.
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
returnstatement 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
addfunction is called with the parameters33and52.
If you don't assign the return value to anything or use it in another expression, it will be discarded.
Generics
I will explain it when I feel like to.