

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Handling Cycles
incomplete
2: Pros and Cons
incomplete
3: Stack Frames
incomplete
4: Tracking Objects
incomplete
5: Free
incomplete
6: Frame References
incomplete
7: Mark and Sweep
incomplete
8: Mark
incomplete
9: Trace
incomplete
10: Sweep
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
To solve our cyclic reference issue (and to force you to implement another GC algorithm, HA!) we're going to implement a Mark and Sweep garbage collector.
We'll be using a vm_t struct which stands for Virtual Machine Type. This vm_t simulates what would normally be tracked if Sneklang were a fully functional interpreted language (it's not). This virtual machine is much simpler than a real one because all we care about is demonstrating the garbage collection aspects.
You can spam Lane if you want a full "write your own programming language" course on boot.dev
Please don't. I have a wife and kids.
Open vm.h and take a look at the vm_t struct. The frames field holds a stack of frames, which are pushed and popped as we enter and exit new scopes. For example:
msg1 = "This is in scope 1"
def outer_func():
msg2 = "This is in scope 2"
def inner_func():
msg3 = "This is in scope 3"
return
return
At each of the scope entrances (in this case function calls), a new stack frame is pushed onto the frames stack. When we exit a scope (a function returns), we pop the stack frame off the frames stack. Because we use void * to work with generics in C, you can't actually tell what the data type held by the stack_t is for each field. We'll write some wrapper functions later to help us make sure that we don't accidentally push the wrong kinds of data into our stacks (yay, C!).
The objects field is also a stack, but it holds snek_object_t pointers.