Skip to content

Advanced Topics

BB edited this page May 20, 2024 · 9 revisions

Extension functions

You can define an extension function as follows:

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

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

int sum = 57.Add(2);

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

int sum = Add(57, 2);

Structs

You can define a struct as follows:

struct Point
{
  int x;
  int 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(char 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: char
  • "stdout"

    Writes a character to the standard output stream.

    • Parameters: char character
    • Return value: void
  • "stderr"

    Writes a character to the standard error stream.

    • Parameters: none
    • Return value: void
  • "console-set"

    Sets a character on the console.

    • Parameters: char character, int x, int y
    • Return value: void
  • "console-clear"

    Clears the console.

    • Parameters: none
    • Return value: void
  • "sleep"

    Pauses the code execution for t millisecs.

    • Parameters: int t
    • Return value: void
  • "sin"

    Returns the sine of v angle.

    • Parameters: float v
    • Return value: float
  • "cos"

    Returns the cosine of v angle.

    • Parameters: float v
    • Return value: float

Importing .dll files

The compiler searches for .dll files in the source file directory. You can override this location with the --basepath command line argument. All public static functions are imported, with names defined in the assembly.

Pointers

Yes, there are also pointers. There are two types of pointers depending on where they point: HEAP and stack pointers. In the future I want to combine them so you don't have to worry about what is happening.

HEAP

You can declare a pointer type that points to the HEAP like this:

int* yeah;

This will declare a variable "yeah" that points to an integer located in the HEAP.

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

struct Vector2
{
  float X;
  float Y;
}

Vector2* point;

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

float x = point.X;

If you try to access a field of a zero pointer, depending on the --no-null-checks argument a runtime exception will be thrown.

Stack