- Notifications
You must be signed in to change notification settings - Fork1
traplol/randy
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This project is a toy and probably won't work correctly on your system but I wanted to make the source available so here it is.
Randy is a self-hosting programming language compiler written in its own language. It compiles to x86_64 assembly and generates native Linux executables.
Randy started as a Python script converting a made-up toy language into assembly and compiling the assembly with gcc. Early development continued like this until the randy language Python compiler had enough features to start implementing a randy compiler using the randy language. Shortly afterwards the Python-based compiler was dropped and all development on the randy compiler was done using its own self-hosting process. The only way to see the original Python-based compiler is to view the commit historyhere. Since this project is entirely self-hosted a pre-existing binary must be provided as this language doesn't exist anywhere else. This was a toy project only for me to explore the ideas and process of self-hosting a compiled programming language.
- Strong static typing with type inference
- Structs, unions, and enums
- Generic types and functions
- Manual memory management
- Standard library including:
- Dynamic arrays (Vector)
- Strings
- Maps and Sets
- Queue data structure
- Memory management utilities
- File I/O
- Process management
- System calls
- Linux x86_64 system
- GNU assembler (as)
- GNU linker (ld)
- musl libc (configured in bootstrap.sh)
./bootstrap.sh
The bootstrap process compiles the compiler three times:
- Using the existing binary to compile version 0
- Using version 0 to compile version 1
- Using version 1 to compile version 2
If versions 1 and 2 match (verified by comparing assembly output), the bootstrap is successful.
There is a util script provided in./compile-libc
, you'll need to correctly point it at musl libc.
./bin/randy"$@" -I include -ld -dynamic-linker /home/max/workspace/musl-1.2.4/lib/libc.so -lc
src/randy/
- Compiler source codemain.randy
- Main compiler entry pointlexer.randy
- Lexical analysisparser.randy
- Syntax analysisast.randy
- Abstract Syntax Tree definitionstype_*.randy
- Type system implementationir_*.randy
- Intermediate representationx86_64_backend.randy
- Code generation
include/std/
- Standard library- Core data structures
- System interfaces
- Utility functions
def main argc: int, argv: ptr -> int in printf("Hello, World!\n"); return 0;end
// Structsstruct Point in x: int; y: int;end// Enumsenum Direction in North; South; East; West;end// Generic Typesstruct Vector[T] in buffer: T&; length: int; capacity: int;end
Randy provides low-level memory management through:
- Manual allocation with
malloc
/free
- Reference types with
&
suffix - Pointer operations for direct memory access
This project is dead, don't bother.