jc Compiler Development Log #2: March/April/May 2020

Published by Jon on

The last few months have been rather hectic (both in terms of compiler development and life in general) so I reckon I should probably do another development log to cover the large gap since the last one.

Implicit Casts (Numeric Types)

Technically speaking this was more of a convenience feature for certain funky things I’ve done inside the compiler but nevertheless it is a feature in the language now.

More Foreign Function Interface Features

Variadic functions are now support in the language purely for the FFI. An example of the syntax is shown below:

i32 printf(i8 *str, ...);

Structs and Unions

Structs and unions are now supported in the language. They function practically the same way they do in C/C++. This example also shows the default initialisation syntax for structs.

struct MyCoolStruct
{
    i32 x = 0;
    f32 f = 0.0f;
}

main()
{
    MyCoolStruct s;
    s.x = 5;
    s.f = 32.0f;
    i32 x = s.x;
    return s.x;
}

Default Initialization and Initialization Lists

Structs can now have default member values at initialization and arrays can be initialized with a list of data.

i32 x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Modules and Build Directives

The language now sports modules and baked-in build directives. The build directives are somewhat simple; “include” adds a search path to the compiler when it is building that module (these search paths are used to find modules for import and libraries to link) and “link” adds a library to be linked against when the compiler invokes the linker.

import io;
import memory;
include "stdlib";
link "libcmt.lib";

main()
{
	i8 *byte = alloc(1);
	printf("Allocated memory pointer: %p\n", byte);
	byte = dealloc(byte);
	printf("Deallocated memory pointer: %p\n", byte);
	return 0;
}

As for modules this introduces two cool new keywords as well: “import” and “export”. The “export” keyword exports any function, struct/union definition or module variable that follows it. For example the following code exports a function named “alloc” and doesn’t export another function named “greet”.

export i8 *alloc(i32 bytes)
{
	return malloc(bytes);
}

void greet()
{
        printf("Hello!");
}

Only names that are exported from a module can be used from another module that imports it. If another module imports a module that contains the code above, it would only be able to use “alloc”.

Nested Functions and Function Pointers

Nested functions and function pointers are now both supported. Declaring a function pointer type is required before you can use that type. Some example code for this is shown below.

func_ptr i32 op(i32, i32);
i32 main()
{
	op operator;
	operator = &add;
	print_greeting("Jon");
	greet();
	printf("IO successfully imported.\n");
	operator(1, 2);
	return 0;
}

Some example code for nested functions is show below. It shows a nested function named “Greet” that can only be accessed inside the scope of its enclosing function, “main”.

i32 main()
{
	void Greet()
	{
		printf("Greetings!\n");
	}
	Greet();
	return 0;
}

Type Inference

The last major change is the addition of some fairly simplistic type inference. The syntax for type inference on variables is taken directly from Go and Jai. In the original design it would have used the “auto” keyword a la C++, however this works so much better and looks much cleaner. Type inference also doesn’t currently work on function arguments.

add(i32 x, i32 y)
{
	z := x + y;
	return z;
}

main()
{
	p := add(1, 1);
	return p;
}

Future of the Project

Now that we’re all caught up I’m going to wrap up this long overdue development log by talking about the future of the project. After working on this project for almost half a year I’ve decided to take a short hiatus to re-evaluate some of the compiler’s internal design and the syntax of the language. I hope to continue working on this project until at least the end of the year, however there is also a lot happening right now so I truthfully do not know when the next development log will be. Nevertheless, thanks for reading this whirlwind update on the state of the project. Here’s hoping the next one won’t be too far away!