A toy language implementation in C demonstrating two different garbage collection strategies: Mark and Sweep and Reference Counting.
- Dual GC Modes: Switch between Mark & Sweep and Reference Counting at runtime.
- Bytecode Interpreter: A simple stack-based VM.
- Object System: Supports Integers, Floats, Strings, Vectors, and Arrays.
Windows (MinGW/GCC): Run the build script:
.\build.batRun the executable with a flag to select the GC mode:
-
Mark and Sweep:
.\snek_gc.exe -ms
-
Reference Counting:
.\snek_gc.exe -rc
The VM executes a hardcoded bytecode sequence defined in main.c. To run your own programs, modify the code array in main.c and rebuild.
Available Opcodes (defined in ops.h)://you can extend if want to
OP_CONST_INT(0): Followed by 4 bytes (little-endian integer). Pushes int to stack.OP_ADD(1): Pops two values, adds them, pushes result.OP_PRINT(2): Pops a value and prints it.OP_RETURN(3): Stops execution.
Example:
- in main.c file
main.c: Entry point and CLI.snek_vm.c: Virtual machine and Mark & Sweep implementation.snek_refcount.c: Reference counting logic.snek_object.c: Object creation and management.stack.c: Generic stack implementation.ops.h: Bytecode opcode definitions.