Skip to content

mro95/pebble-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pebble Programming Language

A modern systems programming language compiler built in Rust, featuring LLVM-based code generation, static typing, and memory-aware constructs.

πŸš€ Features

  • Fast Lexical Analysis: Token-based lexer using the Logos crate
  • LALR(1) Parser: Grammar-driven parsing with LALRPOP
  • Static Type System: Type inference with explicit type annotations
  • LLVM Code Generation: Native binary compilation via Inkwell
  • Semantic Analysis: Symbol table management and type checking
  • Memory Management: Built-in memory allocation primitives (alloc, shared alloc, transfer)
  • Cross-Platform: Automatic target detection for Linux, macOS, and Windows

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Source Code   │───▢│      Lexer      │───▢│     Parser      β”‚
β”‚   (*.pebble)    β”‚     β”‚   (tokens.rs)   β”‚     β”‚   (parse.rs)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                        β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Native Binary  │◀───│  LLVM Codegen   │◀───│       AST       β”‚
β”‚  (executable)   β”‚     β”‚  (codegen/)     β”‚     β”‚     (ast.rs)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β–²                       β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚  Target Machine β”‚    β”‚ Semantic Check  β”‚
                         β”‚   & Linker      β”‚    β”‚  (semantic/)    β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Language Syntax

Variables and Types

let x = 42;           // Type inference
let name: String = "Pebble";  // Explicit typing

Functions

fn add(a: int, b: int) -> int {
    return a + b;
}

Built-in Operations

print(argc);          // Print command-line argument count
print(argv);          // Print command-line arguments array

Memory Management

let ptr = alloc(42);        // Allocate memory
let shared = shared alloc(data);  // Shared allocation
let moved = transfer(ptr);        // Transfer ownership

πŸ› οΈ Getting Started

Prerequisites

  • Rust 1.70+ with Cargo
  • LLVM 18.1 development libraries
  • GCC or Clang (for linking)

Building the Compiler

git clone https://github.com/mro95/pebble-rs.git
cd pebble-rs
cargo build --release

Compiling Pebble Programs

# Create a Pebble source file
echo 'print("Hello, Pebble!");' > hello.pebble

# Compile and run
cargo run hello.pebble
./output_bin

🎯 Example

Input (example.pebble):

print(argv);
print(argc);

Compilation:

$ cargo run example.pebble
Detected target: x86_64-unknown-linux-gnu
LLVM IR written to output.ll
Object file written to output.o
Successfully linked executable with gcc
Binary written to output_bin

Execution:

$ ./output_bin hello world
["./output_bin", "hello", "world"]
3

πŸ”§ Project Structure

src/
β”œβ”€β”€ main.rs              # Compiler driver and CLI
β”œβ”€β”€ lib.rs               # Library root and module declarations
β”œβ”€β”€ lexer.rs             # Token stream processing
β”œβ”€β”€ tokens.rs            # Token definitions and lexer rules
β”œβ”€β”€ parse.rs             # Parser interface
β”œβ”€β”€ ast.rs               # Abstract Syntax Tree definitions
β”œβ”€β”€ codegen/             # LLVM code generation
β”‚   β”œβ”€β”€ mod.rs           # Code generator core
β”‚   β”œβ”€β”€ expressions.rs   # Expression compilation
β”‚   β”œβ”€β”€ statements.rs    # Statement compilation
β”‚   β”œβ”€β”€ functions.rs     # Function compilation
β”‚   β”œβ”€β”€ printing.rs      # Print statement implementation
β”‚   └── types.rs         # Type system integration
β”œβ”€β”€ semantic/            # Static analysis
β”‚   β”œβ”€β”€ mod.rs           # Semantic analyzer
β”‚   β”œβ”€β”€ scope.rs         # Symbol table management
β”‚   β”œβ”€β”€ expr.rs          # Expression type checking
β”‚   β”œβ”€β”€ stmt.rs          # Statement analysis
β”‚   └── type.rs          # Type system and error handling
└── grammar/
    β”œβ”€β”€ grammar.ebnf     # Language grammar specification
    └── parser.lalrpop   # LALRPOP parser definition

πŸ§ͺ Testing

Run the test suite:

cargo test

The project includes comprehensive tests for:

  • Parser functionality (tests/parser_test.rs)
  • Type system validation
  • Code generation accuracy

🎨 Language Features

Type System

  • Primitive Types: int, String, void
  • Type Inference: Automatic type deduction where possible
  • Array Types: Array<T> with bounds checking
  • Function Types: First-class function support

Built-in Variables

  • argc: Command-line argument count (int)
  • argv: Command-line arguments array (Array)

Control Flow

  • Function declarations with parameters and return types
  • Block scoping with proper variable shadowing
  • Return statements with type validation

πŸ” LLVM Integration

The compiler generates optimized LLVM IR and produces native executables:

  • Target Detection: Automatic host architecture detection
  • Cross-compilation: Support for multiple target architectures
  • Optimization: LLVM optimization passes for performance
  • Debug Info: Structured debug information generation (planned)

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

πŸ”— Dependencies

  • logos 0.15.1 - Fast lexical analysis
  • lalrpop 0.22.2 - Parser generator
  • inkwell 0.6.0 - LLVM bindings for Rust
  • thiserror 2.0 - Error handling macros

About

A programming language build in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages