Skip to main content

wave_compiler/frontend/
mod.rs

1// Copyright 2026 Ojima Abraham
2// SPDX-License-Identifier: Apache-2.0
3
4//! Language frontends for parsing source code into HIR.
5//!
6//! Each frontend parses a specific language (Python, Rust, C++, TypeScript)
7//! and produces a language-independent HIR kernel definition.
8
9pub mod cpp;
10pub mod python;
11pub mod rust;
12pub mod typescript;
13
14use crate::diagnostics::CompileError;
15use crate::driver::config::Language;
16use crate::hir::kernel::Kernel;
17
18/// Parse source code in the given language and produce an HIR kernel.
19///
20/// # Errors
21///
22/// Returns `CompileError` if parsing fails.
23pub fn parse(source: &str, language: Language) -> Result<Kernel, CompileError> {
24    match language {
25        Language::Python => python::parse_python(source),
26        Language::Rust => rust::parse_rust(source),
27        Language::Cpp => cpp::parse_cpp(source),
28        Language::TypeScript => typescript::parse_typescript(source),
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_python_frontend() {
38        let source = r#"
39@kernel
40def test(n: u32):
41    x = 42
42"#;
43        let kernel = parse(source, Language::Python).unwrap();
44        assert_eq!(kernel.name, "test");
45    }
46
47    #[test]
48    fn test_rust_frontend() {
49        let source = r#"
50#[kernel]
51fn test(n: u32) {
52    let x = 42;
53}
54"#;
55        let kernel = parse(source, Language::Rust).unwrap();
56        assert_eq!(kernel.name, "test");
57    }
58}