use eggplant_transpiler::ast::parse::Parser;
use eggplant_transpiler::eggplant::{
CodeGenOptions, EggplantCodeGenerator, convert_to_eggplant_with_source_and_program,
};
pub struct Transpiler {
options: CodeGenOptions,
}
impl Transpiler {
#[allow(unused)]
pub fn new() -> Self {
Self {
options: CodeGenOptions::default(),
}
}
pub fn with_options(options: CodeGenOptions) -> Self {
Self { options }
}
pub fn transpile(&self, dsl_code: &str) -> String {
let mut parser = Parser::default();
let mut codegen = EggplantCodeGenerator::with_options(self.options.clone());
let commands = parser
.get_program_from_string(None, dsl_code)
.unwrap_or_else(|_| {
return vec![];
});
let eggplant_commands = convert_to_eggplant_with_source_and_program(
&commands,
Some("transpiled.egg".to_string()),
);
codegen.generate_rust(&eggplant_commands)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_transpile_datatype() {
let transpiler = Transpiler::new();
let dsl = "(datatype Math (MNum i64) (MAdd Math Math))";
let rust = transpiler.transpile(dsl);
assert!(!rust.is_empty());
}
#[test]
fn test_transpile_rewrite() {
let transpiler = Transpiler::new();
let dsl = "(rewrite (MAdd (MNum ?a) (MNum ?b)) (MNum (+ ?a ?b)))";
let rust = transpiler.transpile(dsl);
assert!(!rust.is_empty());
}
}