Cella
A minimalistic systems programming language


Cella is a minimalistic systems programming language one level above C. It has raw pointers, no garbage collector, and no runtime. It adds ownership and destructors, borrowed parameters, enums with payloads, generics, traits and modules. The compiler generates code with LLVM for 64-bit Windows and Linux.

The language is still in development and does not yet have a release. The standard library has only just started, so programs call C functions through ext declarations.


Hello World:
Copy Code
1
2
3
4
5
6
7
8
9
10
11
12
mod helloWorld

ext
{
	puts: fun(text: cstr) -> i32
}

main: fun -> i32
{
	puts("Hello, World!")
	ret 0
}


Ownership:

Areas owns memory from malloc and frees it in its drop operator when main ends. Calls name how each argument is passed: mut and imm borrow it, no mode takes it by value.

Copy Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
mod shapes

ext
{
	printf: fun(format: cstr, ...) -> i32
	malloc: fun(size: usize) -> ptr[f64]
	free: fun(memory: ptr[f64])
}

Shape: enum
{
	circle(radius: f64)
	rect(width: f64, height: f64)
}

area: fun(shape: Shape) -> f64 => match shape
{
	circle(val r) => 3.14159 * r * r
	rect(val w, val h) => w * h
}

Areas: own rec
{
	data: ptr[f64]
	count: usize

	new: op(count: usize)
	{
		self.data = malloc(count * sizeOf(f64))
		self.count = count
	}

	drop: op
	{
		printf("freeing %zu areas\n", self.count)
		free(self.data)
	}
}

fill: fun(mut areas: Areas, shapes: array[Shape, 3])
{
	for i in 0..areas.count
	{
		areas.data[i] = area(shapes[i])
	}
}

total: fun(imm areas: Areas) -> f64
{
	var sum = 0.0
	for i in 0..areas.count
	{
		sum += areas.data[i]
	}

	ret sum
}

main: fun -> i32
{
	val shapes = [Shape.circle(1.0), Shape.rect(2.0, 3.0), Shape.rect(0.5, 4.0)]
	var areas = Areas(3)
	fill(mut areas, shapes)
	printf("total %.2f\n", total(imm areas))
	ret 0
}

Output:

total 11.14
freeing 3 areas