Advanced Topics - BBpezsgo/Interpreter GitHub Wiki

Extension functions

You can define an extension function as follows:

i32 Add(this i32 a, i32 b) {
  return a + b;
}

If the first parameter defined with the this modifier, the function can be called like this:

i32 sum = 57.Add(2);

You can also call an extension function in a normal way:

i32 sum = Add(57, 2);

Structs

You can define a struct as follows:

struct Point
{
  i32 x;
  i32 y;
}

And construct it like this:

Point point = new Point;
point.x = 37; // Set the "x" field to 37
point.y = 81;

Structs are live on the stack, so you don't have to explicitly make a new struct. In the following example, the variable point will be initialized to 0 for both x and y.

Point point;

External Functions

External functions are functions that does not have a body, because the implementation is outside of the program (ie.: in the interpreter as a predefined function, or in a .dll file).

To import an external function, use the External attribute with the external function's name as a parameter:

[External("stdout")]
void Print(u16 data);

If the return type or parameter types don't match, a compiler exception is thrown.

Predefined External Functions

[!NOTE] These external functions are only available in default mode.

  • "stdin"

    Reads a key from the console. This blocks the code execution until a key is pressed.

    • Parameters: none
    • Return type: u16

    Corresponding .NET function:

    return (char)System.Console.In.Read();
    
  • "stdout"

    Writes a character to the standard output stream.

    • Parameters: u16 character
    • Return value: void

    Corresponding .NET function:

    System.Console.Out.Write(character);
    
  • "sleep"

    Pauses the code execution for t milliseconds.

    • Parameters: i32 t
    • Return value: void
  • "utc-time"

    Returns the elapsed milliseconds since midnight in UTC time.

    • Parameters: none
    • Return value: i32

    Corresponding .NET function:

    return (int)DateTime.UtcNow.TimeOfDay.TotalMilliseconds;
    
  • "local-time"

    Returns the elapsed milliseconds since midnight in local time

    • Parameters: none
    • Return value: i32

    Corresponding .NET function:

    return (int)DateTime.Now.TimeOfDay.TotalMilliseconds;
    
  • "utc-date-day"

    Returns the current day of year in UTC time

    • Parameters: none
    • Return value: i32

    Corresponding .NET function:

    return (int)DateTime.UtcNow.DayOfYear;
    
  • "local-date-day"

    Returns the current day of year in local time

    • Parameters: none
    • Return value: i32

    Corresponding .NET function:

    return (int)DateTime.Now.DayOfYear;
    
  • "utc-date-year"

    Returns the current year in UTC time

    • Parameters: none
    • Return value: i32

    Corresponding .NET function:

    return (int)DateTime.UtcNow.Year;
    
  • "local-date-year"

    Returns the current year in local time

    • Parameters: none
    • Return value: i32

    Corresponding .NET function:

    return (int)DateTime.Now.Year;
    

Pointers

Yeah there are also pointers.

[!NOTE] Pointers technically aint supported in brainfuck, but you can still pass a variable as a reference to a function anyway.

You can declare a pointer type like this:

i32* yeah;

This will declare a variable "yeah" that points to a 32 bit signed integer.

You can also declare a pointer that points to a struct like this:

struct Vector2
{
    f32 X;
    f32 Y;
}

Vector2* point;

... and you can access its fields like normally you do:

f32 x = point.X;

[!NOTE] If you try to access a field of a zero pointer, depending on the --no-nullcheck argument a runtime exception will be thrown.

You can set the value where the pointer points to like this:

*yeah = 44;

[!NOTE] I want to make a better syntax for field dereferencing, because what if the field is a pointer type and you want to set the pointer's value or the value where the pointer points to? Idk man, I will implement it when I feel like to.