Skip to main content

kore/
lib.rs

1//! # KORE - The Ultimate Programming Language
2//! 
3//! ## Philosophy
4//! 1. **Rust's Safety** - Ownership, borrowing, no null, no data races
5//! 2. **Python's Syntax** - Significant whitespace, minimal ceremony  
6//! 3. **Lisp's Power** - Code as data, hygienic macros, DSL-friendly
7//! 4. **Zig's Comptime** - Compile-time execution, no separate macro language
8//! 5. **Effect Tracking** - Side effects in the type system
9//! 6. **Actor Concurrency** - Erlang-style message passing built-in
10//! 7. **Universal Targets** - WASM, LLVM native, SPIR-V shaders
11//!
12//! ## Example
13//! ```KORE
14//! fn factorial(n: Int) -> Int with Pure:
15//!     match n:
16//!         0 => 1
17//!         _ => n * factorial(n - 1)
18//! ```
19
20pub mod lexer;
21pub mod ast;
22pub mod parser;
23pub mod types;
24pub mod effects;
25pub mod codegen;
26pub mod runtime;
27pub mod stdlib;
28pub mod error;
29pub mod span;
30pub mod comptime;
31pub mod diagnostics;
32pub mod packager;
33pub mod lsp;
34pub mod monomorphize;
35
36
37pub use lexer::Lexer;
38pub use parser::Parser;
39pub use ast::*;
40pub use types::*;
41pub use effects::*;
42pub use error::KoreError;
43pub use span::Span;
44
45/// Compile KORE source to the specified target
46pub fn compile(source: &str, target: CompileTarget) -> Result<Vec<u8>, KoreError> {
47    // 1. Lex
48    let tokens = Lexer::new(source).tokenize()?;
49    
50    // 2. Parse
51    let mut ast = Parser::new(&tokens).parse()?;
52    
53    // 2.5 Comptime Execution
54    // Evaluate comptime blocks and expressions before type checking
55    comptime::eval_program(&mut ast)?;
56
57    // 3. Type check with effect inference
58    let mut typed_ast = types::check(&ast)?;
59    
60    // 3.5 Monomorphization (for native targets and interpreter if we want to test lowering)
61    if matches!(target, CompileTarget::Llvm | CompileTarget::Wasm | CompileTarget::SpirV | CompileTarget::Interpret) {
62        let mono_prog = monomorphize::monomorphize(&typed_ast)?;
63        // Replace items with monomorphized items
64        // Since codegen expects TypedProgram, we can just update it.
65        // But TypedProgram might have other fields later. 
66        // For now, MonomorphizedProgram just has items.
67        typed_ast.items = mono_prog.items; 
68    }
69    
70    // 4. Generate code
71    match target {
72        CompileTarget::Wasm => codegen::wasm::generate(&typed_ast),
73        #[cfg(feature = "llvm")]
74        CompileTarget::Llvm => codegen::llvm::generate(&typed_ast),
75        #[cfg(not(feature = "llvm"))]
76        CompileTarget::Llvm => Err(KoreError::codegen("LLVM backend not compiled. Rebuild with --features llvm", Span::new(0, 0))),
77        CompileTarget::SpirV => codegen::spirv::generate(&typed_ast),
78        CompileTarget::Interpret => {
79            runtime::interpret(&typed_ast)?;
80            Ok(vec![])
81        }
82        CompileTarget::Test => {
83            runtime::run_tests(&typed_ast)?;
84            Ok(vec![])
85        }
86    }
87}
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum CompileTarget {
91    Wasm,
92    Llvm,
93    SpirV,
94    Interpret,
95    Test,
96}
97
98/// Version of the KORE language
99pub const VERSION: &str = "0.1.0";
100pub const LANGUAGE_NAME: &str = "KORE";
101