skeleton for codegen
This commit is contained in:
@@ -470,20 +470,48 @@ impl<'a> TermFunctionsBuilder<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn finalize(self) -> (HashMap<TermId, TrieNode>, HashMap<TermId, TrieNode>) {
|
||||
let functions_by_input = self
|
||||
.builders_by_input
|
||||
.into_iter()
|
||||
.map(|(term, builder)| (term, builder.trie))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let functions_by_output = self
|
||||
.builders_by_output
|
||||
.into_iter()
|
||||
.map(|(term, builder)| (term, builder.trie))
|
||||
.collect::<HashMap<_, _>>();
|
||||
(functions_by_input, functions_by_output)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Automata {
|
||||
pub automata_by_input: HashMap<TermId, ()>,
|
||||
pub automata_by_output: HashMap<TermId, ()>,
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Codegen<'a> {
|
||||
typeenv: &'a TypeEnv,
|
||||
termenv: &'a TermEnv,
|
||||
functions_by_input: HashMap<TermId, TrieNode>,
|
||||
functions_by_output: HashMap<TermId, TrieNode>,
|
||||
}
|
||||
|
||||
impl Automata {
|
||||
pub fn compile(typeenv: &TypeEnv, termenv: &TermEnv) -> Result<Automata, Error> {
|
||||
impl<'a> Codegen<'a> {
|
||||
pub fn compile(typeenv: &'a TypeEnv, termenv: &'a TermEnv) -> Result<Codegen<'a>, 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<String, Error> {
|
||||
use std::fmt::Write;
|
||||
let mut code = String::new();
|
||||
writeln!(&mut code, "// GENERATED BY ISLE. DO NOT EDIT!")?;
|
||||
Ok(code)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
use crate::error::Error;
|
||||
use crate::{ast, codegen, sema};
|
||||
|
||||
pub fn compile(defs: &ast::Defs) -> Result<codegen::Automata, Error> {
|
||||
pub fn compile(defs: &ast::Defs) -> Result<String, Error> {
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -18,7 +18,7 @@ fn main() -> Result<(), error::Error> {
|
||||
stdin().read_to_string(&mut input)?;
|
||||
let mut parser = parser::Parser::new("<stdin>", &input[..]);
|
||||
let defs = parser.parse_defs()?;
|
||||
let automata = compile::compile(&defs)?;
|
||||
println!("automata: {:?}", automata);
|
||||
let code = compile::compile(&defs)?;
|
||||
println!("{}", code);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user