cfd16_assembler/
lib.rs

1//! A library which implements an assembler backend for the CFD-16
2//! instruction set architecture.
3
4#![warn(missing_docs)]
5
6use std::{fs, path::PathBuf};
7
8mod elf;
9
10mod error;
11use error::{AssemErrorKind, AssemblerError};
12
13mod parse;
14use parse::{BinFormat, Program};
15
16mod text;
17
18#[allow(missing_docs)]
19pub mod prelude;
20
21#[cfg(test)]
22mod tests;
23
24/// Assembles the contents of the file `srcfile` into properly encoded
25/// CFD-16 machine code. The `format` variable indicates the file type
26/// in which the resulting program is encoded.
27pub fn assemble(srcfile: PathBuf, format: BinFormat) -> Result<Vec<u8>, AssemblerError> {
28    let source = match fs::read_to_string(&srcfile) {
29        Ok(s) => s,
30        Err(_) => return Err(AssemblerError::new(None, AssemErrorKind::FileRead(srcfile))),
31    };
32
33    let source = Program::preprocess(source)?;
34    let mut prog = Program::new(format);
35
36    prog.parse(&source)?;
37    prog.resolve_symbols()?;
38    prog.encode()
39}