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:

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 (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 (i32 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.
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 Booleanish(i32 condition) {
  if (condition) {
    return 1;
  }
  // If the "condition" is false, it will return some random stuff.
}

[!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:

i32 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.