about summary refs log tree commit diff
path: root/README.md
blob: a4879548c5b59ac5fcebf4f3ee44ace11e32c625 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<h1>
  Pry Programming Language
  <img src="images/logo.svg" width="40" alt="Logo" />
</h1>

Pry is a simple, statically-typed programming language that compiles to native code via LLVM. It features a minimal C-like syntax with basic functional programming elements. Pry is fully self-hosted, meaning the compiler is written in Pry itself and can compile its own source code.

## Features

- **Static typing** with explicit type annotations
- **Basic functions** with parameters and return types
- **Structs** with field access
- **Manual memory management** via C library functions
- **C interoperability** through extern declarations
- **Simple import system** for code organization
- **LLVM backend** for native code generation

## Quick Start

### Prerequisites

- LLVM development libraries (`llvm-config` must be available)
- A C compiler (gcc/clang) for linking

### Using with Nix

If you have Nix with flakes enabled:

```bash
# Enter development shell with all dependencies
nix develop
```

The flake provides LLVM and debugging tools.

### Bootstrapping the Compiler

Pry is now fully self-hosted! The compiler is written in Pry and compiles itself through a multi-stage bootstrap process:

1. **Bootstrap the compiler:**
   ```bash
   ./bootstrap.sh
   ```

   This creates a 3-stage bootstrap:
   - **Stage 0**: Compiles the initial LLVM IR (`bootstrap/output.ll`) to create the first compiler
   - **Stage 1**: Uses stage0 to compile the Pry source code (`src/main.pry`)
   - **Stage 2**: Uses stage1 to recompile itself (verification step)
   - **Stage 3**: Uses stage2 to recompile itself again (final verification)

2. **Compile a Pry program:**
   ```bash
   ./stage3 examples/1.pry
   ```

3. **Link and create executable:**
   ```bash
   cc $(llvm-config --libs) bootstrap_output.o -o program
   ```

4. **Run the program:**
   ```bash
   ./program
   ```

## Language Overview

### Basic Syntax

```pry
// Hello World
import "!stdlib.pry";

let main = () => i64 {
    printf("Hello, World!\n");
    return 0;
};
```

### Variables and Types

```pry
let x = 42;           // i64 integer
let name = "Alice";   // String literal (*i8)
let flag = true;      // Boolean
```

**Built-in types:**
- `i64`, `i32`, `i8` - Signed integers  
- `bool` - Boolean values
- `*T` - Pointers to type T
- `void` - Unit type

### Functions

```pry
// Function with explicit parameter types
let add = (a: i64, b: i64) => i64 {
    return a + b;
};

// Recursive function
let fib = (n: i64) => i64 {
    if n == 0 {
        return 0;
    };
    if n == 1 {
        return 1;
    };
    return fib(n-2) + fib(n-1);
};
```

### Control Flow

```pry
// If statements (no else yet)
if condition {
    // statements
};

// While loops
let i = 0;
while i < 10 {
    printf("%d\n", i);
    i = i + 1;
};
```

### Structs

```pry
// Define a struct type
let Point = struct {
    x: i64,
    y: i64
};

let main = () => i64 {
    let p = Point{};  // Empty initialization
    p.x = 10;         // Field assignment
    p.y = 20;
    return 0;
};
```

### Memory Management

```pry
// Use C library functions for memory management
extern malloc = (i64) => *void;
extern free = (*void) => void;

let main = () => i64 {
    let ptr = cast(*i64, malloc(8));
    *ptr = 42;
    free(cast(*void, ptr));
    return 0;
};
```

### C Interoperability

```pry
// Declare external C functions
extern printf = (*i8, varargs) => void;
extern putchar = (i64) => i64;

let main = () => i64 {
    putchar(72);  // 'H'
    putchar(105); // 'i'
    putchar(10);  // '\n'
    return 0;
};
```

## Current Limitations

Pry is a work-in-progress language with several limitations:

- No `else` clauses for if statements
- No `for` loops (only `while`)
- No arrays (use pointers and manual indexing)
- No string manipulation beyond C functions
- No generics or templates
- No pattern matching
- Limited error handling
- Basic type system

## Examples

The `examples/` directory contains test programs showing:

- Basic I/O (`examples/1.pry`)
- Recursive functions (`examples/8.pry`) 
- Memory management (`examples/20.pry`)
- Struct usage (`examples/22.pry`, `examples/24.pry`)
- C library integration (`examples/15.pry`)

Try running:
```bash
./stage3 examples/8.pry && cc $(llvm-config --libs) bootstrap_output.o -o fib && ./fib    # Fibonacci sequence
./stage3 examples/1.pry && cc $(llvm-config --libs) bootstrap_output.o -o hello && ./hello    # Hello world
./stage3 examples/20.pry && cc $(llvm-config --libs) bootstrap_output.o -o array && ./array   # Array manipulation
```

## Standard Library

The minimal standard library (`std/stdlib.pry`) provides:

- `printf` - C printf wrapper
- `strcmp` - String comparison
- `isdigit`, `isalpha` - Character classification helpers
- `assert` - Simple assertion function

## Implementation

Pry is now fully self-hosted! The compiler is written in Pry itself:

- `src/main.pry` - Compiler entry point
- `src/tokenizer.pry` - Lexical analysis
- `src/parser.pry` - Syntax analysis and AST generation  
- `src/codegen.pry` - LLVM IR generation
- `bootstrap/output.ll` - Initial LLVM IR for bootstrapping
- `bootstrap.sh` - Multi-stage bootstrap script

The grammar is formally defined in `grammar.ebnf`.

## Development Status

Pry is an experimental language that has achieved **full self-hosting**! The compiler successfully compiles itself through a multi-stage bootstrap process. While functional for basic programs, it's still evolving with new features being added.

---

*Pry is a minimal, self-hosted systems programming language focused on simplicity and clean design.*