Basic Topics - BBpezsgo/Interpreter GitHub Wiki

Comments

Anything between // and the end of the line is considered a comment and will not be compiled.

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

Built-in Types

Keyword Size Signed? Kind Note
u8 8 bit No Integer
i8 8 bit Yes Integer Not tested
u16 16 bit No Integer Used by character literals
i16 16 bit Yes Integer Not tested
u32 32 bit No Integer Not tested
i32 32 bit Yes Integer
f32 32 bit Yes Floating

Booleans

There is no boolean type defined explicitly. Any zero value is interpreted as false, everything else is true.

Variables

You can define a variable like this:

int number = 46;

In this example, the variable type is int, 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):

int 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 (including other files).

[!WARNING] Global variables can do some unexpected stuff

Flow Control

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

Conditional Branches

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

Loops

You can create two types of loops: for loops and while loops. You can stop the loop with the break keyword. continue isn't supported.

While loop

while (condition) {
  // ...
}

For loop

for (int iterator; iterator < length; iterator++) {
  // ...
}

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.
int Add(int a, int b) {
  return a + b;
}

In this example, the function type is int, the name is Add, and it has two parameters of type int 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, the return type's default value is returned.

int Booleanish(int condition) {
  if (condition) {
    return 1;
  }
  // If the "condition" is false, it will return 0
}

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

Calling functions

You can call a function as follows:

int sum = Add(33, 52);

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

If you do not assign the return value to anything or use it in another expression, it is discarded.