A modern compiler for the Ferret programming language, featuring:
- Go-style AST architecture
- Rust-style error diagnostics
- Rich type system with inference
- Compile-time error detection
- Module system
run.bat test/simple.ferThat's it! The script uses go run to compile and run directly.
let x := 10; // Type inference
let y: i32 = 20; // Explicit type
const pi := 3.14; // Constant
// Basic types
i8, i16, i32, i64 // Signed integers
u8, u16, u32, u64 // Unsigned integers
f32, f64 // Floats
str, bool, byte // String, boolean, byte
// Arrays
[3]i32 // Fixed size array
[]i32 // Dynamic array
// Optional types
i32? // Nullable integer
// Error types
Data ! Error // Can return error
type Point struct {
.x: f64,
.y: f64
};
let p := Point{ .x = 1.0, .y = 2.0 };
type Color enum {
Red,
Green,
Blue
};
let color := Color::Red;
fn add(a: i32, b: i32) -> i32 {
return a + b;
}
let maybe: i32? = None;
let value := maybe ?: 42; // Defaults to 42 if None
fn divide(a: i32, b: i32) -> i32 ! Error {
if b == 0 {
return Error{ .msg = "Division by zero" }!;
}
return a / b;
}
const result := divide(10, 2) catch err {
println("Error: {}", err.msg);
} 0; // Fallback value
when color {
Color::Red => println("Red"),
Color::Green => println("Green"),
Color::Blue => println("Blue")
}
if x < y {
println("x is less");
} else {
println("x is greater or equal");
}
for i := 0; i < 10; i++ {
println(i);
}
while condition {
// loop body
}
Ferret provides beautiful, helpful error messages:
error[T0001]: type mismatch
--> example.fer:10:15
|
10 | let num: i32 = "hello";
| ~~~~~~~ expected i32, found str
|
= note: type inference determined this expression has type 'str'
= help: convert the string to an integer using the parse function
Compilation failed with 1 error(s)
Single script for everything:
run.bat <file.fer> # Compile a Ferret fileSet DEBUG=1 for debug output:
set DEBUG=1
run.bat test/simple.ferrun.bat test/simple.ferset DEBUG=1
run.bat test/simple.fergo test ./...- Go-style AST architecture
- Rust-style diagnostic system
- Semantic information system
- Type system infrastructure
- Module system design
- Lexer implementation
- Error code taxonomy
- Shared context architecture
- Multi-file compilation pipeline
- Parser implementation
- Collector (declaration gathering)
- Symbol resolution
- Type checker
- Code generation
MIT License
Contributions are welcome! Please read the documentation to understand the compiler architecture before submitting PRs.