Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ randy Public

the self-hosting randy programming language compiler.

traplol/randy

Repository files navigation

Randy Programming Language

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 history here. 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.

Features

  • 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

Building

Prerequisites

  • Linux x86_64 system
  • GNU assembler (as)
  • GNU linker (ld)
  • musl libc (configured in bootstrap.sh)

Build Steps

Bootstrap the compiler:

./bootstrap.sh

The bootstrap process compiles the compiler three times:

  1. Using the existing binary to compile version 0
  2. Using version 0 to compile version 1
  3. Using version 1 to compile version 2

If versions 1 and 2 match (verified by comparing assembly output), the bootstrap is successful.

Link with libc

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

Project Structure

  • src/randy/ - Compiler source code

    • main.randy - Main compiler entry point
    • lexer.randy - Lexical analysis
    • parser.randy - Syntax analysis
    • ast.randy - Abstract Syntax Tree definitions
    • type_*.randy - Type system implementation
    • ir_*.randy - Intermediate representation
    • x86_64_backend.randy - Code generation
  • include/std/ - Standard library

    • Core data structures
    • System interfaces
    • Utility functions

Language Features

Basic Syntax

def main argc: int, argv: ptr -> int in
    printf("Hello, World!\n");
    return 0;
end

Types

// Structs
struct Point in
    x: int;
    y: int;
end

// Enums
enum Direction in
    North;
    South;
    East;
    West;
end

// Generic Types
struct Vector[T] in
    buffer: T&;
    length: int;
    capacity: int;
end

Memory Management

Randy provides low-level memory management through:

  • Manual allocation with malloc/free
  • Reference types with & suffix
  • Pointer operations for direct memory access

Contributing

This project is dead, don't bother.

About

the self-hosting randy programming language compiler.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published