1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// Copyright (c) 2017 Fabian Schuiki //! Facilities to emit a module as human-readable assembly, or to parse such //! assembly back into a module. mod reader; mod writer; pub use self::{reader::parse_str, writer::Writer}; use crate::{Module, Visitor}; /// Emit assembly for a module. /// /// # Example /// ``` /// # use llhd::{Module, Entity, entity_ty, assembly::write}; /// // Create a module. /// let mut module = Module::new(); /// module.add_entity(Entity::new("foo", entity_ty(vec![], vec![]))); /// /// // Write to a vector of bytes and convert into a string. /// let mut asm = vec![]; /// write(&mut asm, &module); /// let asm = String::from_utf8(asm).unwrap(); /// /// assert_eq!(asm, "entity @foo () () {\n}\n"); /// ``` pub fn write(sink: &mut impl std::io::Write, module: &Module) { Writer::new(sink).visit_module(module); } /// Emit assembly for a module as string. /// /// # Example /// ``` /// # use llhd::{Module, Entity, entity_ty, assembly::write_string}; /// // Create a module. /// let mut module = Module::new(); /// module.add_entity(Entity::new("foo", entity_ty(vec![], vec![]))); /// /// assert_eq!(write_string(&module), "entity @foo () () {\n}\n"); /// ``` pub fn write_string(module: &Module) -> String { let mut asm = vec![]; write(&mut asm, &module); String::from_utf8(asm).expect("writer should emit proper utf8") }