diff --git a/cranelift/isle/src/codegen.rs b/cranelift/isle/src/codegen.rs index 4ffceb38c5..e080f30a25 100644 --- a/cranelift/isle/src/codegen.rs +++ b/cranelift/isle/src/codegen.rs @@ -470,20 +470,48 @@ impl<'a> TermFunctionsBuilder<'a> { } } } + + fn finalize(self) -> (HashMap, HashMap) { + let functions_by_input = self + .builders_by_input + .into_iter() + .map(|(term, builder)| (term, builder.trie)) + .collect::>(); + let functions_by_output = self + .builders_by_output + .into_iter() + .map(|(term, builder)| (term, builder.trie)) + .collect::>(); + (functions_by_input, functions_by_output) + } } -#[derive(Clone, Debug, Default)] -pub struct Automata { - pub automata_by_input: HashMap, - pub automata_by_output: HashMap, +#[derive(Clone, Debug)] +pub struct Codegen<'a> { + typeenv: &'a TypeEnv, + termenv: &'a TermEnv, + functions_by_input: HashMap, + functions_by_output: HashMap, } -impl Automata { - pub fn compile(typeenv: &TypeEnv, termenv: &TermEnv) -> Result { +impl<'a> Codegen<'a> { + pub fn compile(typeenv: &'a TypeEnv, termenv: &'a TermEnv) -> Result, Error> { let mut builder = TermFunctionsBuilder::new(typeenv, termenv); builder.build(); log::trace!("builder: {:?}", builder); - // TODO - Ok(Automata::default()) + let (functions_by_input, functions_by_output) = builder.finalize(); + Ok(Codegen { + typeenv, + termenv, + functions_by_input, + functions_by_output, + }) + } + + pub fn generate_rust(&self) -> Result { + use std::fmt::Write; + let mut code = String::new(); + writeln!(&mut code, "// GENERATED BY ISLE. DO NOT EDIT!")?; + Ok(code) } } diff --git a/cranelift/isle/src/compile.rs b/cranelift/isle/src/compile.rs index 3aa01e599c..618998980c 100644 --- a/cranelift/isle/src/compile.rs +++ b/cranelift/isle/src/compile.rs @@ -3,9 +3,9 @@ use crate::error::Error; use crate::{ast, codegen, sema}; -pub fn compile(defs: &ast::Defs) -> Result { +pub fn compile(defs: &ast::Defs) -> Result { let mut typeenv = sema::TypeEnv::from_ast(defs)?; let termenv = sema::TermEnv::from_ast(&mut typeenv, defs)?; - let automata = codegen::Automata::compile(&typeenv, &termenv)?; - Ok(automata) + let codegen = codegen::Codegen::compile(&typeenv, &termenv)?; + codegen.generate_rust() } diff --git a/cranelift/isle/src/error.rs b/cranelift/isle/src/error.rs index 2399fb1231..7fce2df669 100644 --- a/cranelift/isle/src/error.rs +++ b/cranelift/isle/src/error.rs @@ -11,6 +11,8 @@ pub enum Error { SemaError(#[from] SemaError), #[error("IO error")] IoError(#[from] std::io::Error), + #[error("Formatting error")] + FmtError(#[from] std::fmt::Error), } #[derive(Clone, Debug, Error)] diff --git a/cranelift/isle/src/main.rs b/cranelift/isle/src/main.rs index 922afbb57e..038a8a7a85 100644 --- a/cranelift/isle/src/main.rs +++ b/cranelift/isle/src/main.rs @@ -18,7 +18,7 @@ fn main() -> Result<(), error::Error> { stdin().read_to_string(&mut input)?; let mut parser = parser::Parser::new("", &input[..]); let defs = parser.parse_defs()?; - let automata = compile::compile(&defs)?; - println!("automata: {:?}", automata); + let code = compile::compile(&defs)?; + println!("{}", code); Ok(()) }