MESYETI's Website

<- Home
<- Log

Day 01 - Callisto

Note: if you know nothing about Callisto, I would recommend the blog series on soxfox's blog about Callisto and its compiler

Today is all about Callisto, and today it's not that interesting. I'm adding pointers to Callisto. You might think it's weird for a low level language that has existed for almost 9 months to not have pointers, but it kinda already has it. Like Forth, Callisto has the @ and ! functions. @ reads a cell-sized value from memory and ! writes a cell-sized value to memory. There are also equivalent functions for 8-bit, 16-bit, 32-bit values.

These functions are fine, I just don't like how they look in code. Well, they look fine if you're just using it for pointers to integers, but where it gets bad is when you have a pointer to a structure. Currently, I have only implemented proper structure syntax for regular structure variables, so no pointer support. This means you have to do it the old fashioned way, by manually adding the member's offset. It looks horrible:

let addr foo
# ...
# foo is a pointer to a structure (called FooType), with 2 members: bar and baz
# i'm going to set bar to 3 and baz to 4
3 &foo FooType.bar + !
4 &foo FooType.baz + !

This is extremely ugly. I want to change it to this:

let ptr FooType foo
# ...
# FooType has 2 members: bar and baz
# i'm going to set bar to 3 and baz to 4
3 -> foo.bar
4 -> foo.baz

Much better!! Soon I might even have a language that I would consider presentable.

But, if I will support pointers to structures, I think I should support pointers to integers too. A lot of my choices with Callisto's design is preventing mistakes, as it's very easy to make mistakes in a stack-based language. Pointers will help since with @/! it's very easy to pick the wrong function and break your program, and dereferencing will automatically use the right size.

So, here's my idea for pointers to integers:

let ptr cell myPtr
let cell myValue

# set pointer
&myValue -> myPtr

# set value
5 -> ^myPtr

# read value
^myPtr

Looks good to me, so that's what I'll be working on. Hopefully, I will finish it this month and move onto something more interesting.

Implementation

The first step to implementing this is a refactor. A not so long but still terrible and boring refactor. Callisto's AST currently stores the type/s used for statements like let, array, and function definitions as strings. This won't work anymore because the type syntax now also has ptr.

So, first I need to do that refactor. I made a new AST node type for types, called TypeNode, then I have to support this node type in all the statements I mentioned before. I also have to refactor every backend and some of the compiler to support this type node. There's nothing to really show for this, except that the ptr syntax does work:

Join me next time as I either start working on pointers, or work some more on ZK