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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Automata {
|
pub struct Codegen<'a> {
|
||||||
pub automata_by_input: HashMap<TermId, ()>,
|
typeenv: &'a TypeEnv,
|
||||||
pub automata_by_output: HashMap<TermId, ()>,
|
termenv: &'a TermEnv,
|
||||||
|
functions_by_input: HashMap<TermId, TrieNode>,
|
||||||
|
functions_by_output: HashMap<TermId, TrieNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Automata {
|
impl<'a> Codegen<'a> {
|
||||||
pub fn compile(typeenv: &TypeEnv, termenv: &TermEnv) -> Result<Automata, Error> {
|
pub fn compile(typeenv: &'a TypeEnv, termenv: &'a TermEnv) -> Result<Codegen<'a>, Error> {
|
||||||
let mut builder = TermFunctionsBuilder::new(typeenv, termenv);
|
let mut builder = TermFunctionsBuilder::new(typeenv, termenv);
|
||||||
builder.build();
|
builder.build();
|
||||||
log::trace!("builder: {:?}", builder);
|
log::trace!("builder: {:?}", builder);
|
||||||
// TODO
|
let (functions_by_input, functions_by_output) = builder.finalize();
|
||||||
Ok(Automata::default())
|
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::error::Error;
|
||||||
use crate::{ast, codegen, sema};
|
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 mut typeenv = sema::TypeEnv::from_ast(defs)?;
|
||||||
let termenv = sema::TermEnv::from_ast(&mut typeenv, defs)?;
|
let termenv = sema::TermEnv::from_ast(&mut typeenv, defs)?;
|
||||||
let automata = codegen::Automata::compile(&typeenv, &termenv)?;
|
let codegen = codegen::Codegen::compile(&typeenv, &termenv)?;
|
||||||
Ok(automata)
|
codegen.generate_rust()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ pub enum Error {
|
|||||||
SemaError(#[from] SemaError),
|
SemaError(#[from] SemaError),
|
||||||
#[error("IO error")]
|
#[error("IO error")]
|
||||||
IoError(#[from] std::io::Error),
|
IoError(#[from] std::io::Error),
|
||||||
|
#[error("Formatting error")]
|
||||||
|
FmtError(#[from] std::fmt::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Error)]
|
#[derive(Clone, Debug, Error)]
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ fn main() -> Result<(), error::Error> {
|
|||||||
stdin().read_to_string(&mut input)?;
|
stdin().read_to_string(&mut input)?;
|
||||||
let mut parser = parser::Parser::new("<stdin>", &input[..]);
|
let mut parser = parser::Parser::new("<stdin>", &input[..]);
|
||||||
let defs = parser.parse_defs()?;
|
let defs = parser.parse_defs()?;
|
||||||
let automata = compile::compile(&defs)?;
|
let code = compile::compile(&defs)?;
|
||||||
println!("automata: {:?}", automata);
|
println!("{}", code);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user