preserves/implementations/rust/preserves-schema/src/compiler/mod.rs

76 lines
2.2 KiB
Rust
Raw Normal View History

2021-06-28 14:35:45 +00:00
pub mod types;
pub mod names;
pub mod context;
pub mod parsers;
use std::path::PathBuf;
use preserves::value::{Map, IOValue, NestedValue, Value};
use crate::syntax::block::constructors::*;
pub type ModulePath = Vec<String>;
pub type Schema = IOValue;
#[derive(Debug)]
pub struct CompilerConfig {
pub bundle: Map<ModulePath, Schema>,
pub output_dir: PathBuf,
}
impl CompilerConfig {
pub fn new(output_dir: PathBuf) -> Self {
CompilerConfig {
bundle: Map::new(),
output_dir: output_dir,
}
}
}
pub fn compile(config: &CompilerConfig) -> Result<(), std::io::Error> {
for (k, v) in config.bundle.iter() {
let mut output_path = config.output_dir.clone();
output_path.extend(k);
output_path.set_extension("rs");
let mut m = context::ModuleContext::new();
// println!("\n{:?}", &output_path);
// TODO: embedded type
let definitions = v.value().to_dictionary()?.get(&Value::symbol("definitions").wrap())
.unwrap().value().to_dictionary()?;
for (n, d) in definitions.iter() {
m.define_type(item(types::render_definition_type(
n.value().to_symbol()?,
&types::definition_type(d)?)));
parsers::gen_definition_parser(&mut m, n.value().to_symbol()?, &d);
}
//---------------------------------------------------------------------------
println!("#![allow(unused_parens)]");
println!();
println!("use std::convert::TryFrom;");
println!("use preserves::value::{{NestedValue, Reader}};");
println!("use lazy_static::lazy_static;");
println!();
println!("lazy_static! {{");
for (value, name) in m.literals {
let bs = preserves::value::PackedWriter::encode(&value).unwrap();
println!(" pub static ref {}: preserves::value::IOValue = /* {:?} */ preserves::value::PackedReader::decode_bytes(&vec!{:?}).demand_next(false).unwrap();",
name,
value,
bs);
}
println!("}}\n");
for i in m.typedefs { println!("{:?}\n", i); }
for i in m.functiondefs { println!("{:?}\n", i); }
}
Ok(())
}