mod ddirective;
mod label;
mod mnemonic;
mod operand;
mod register;
mod section;
pub use ddirective::DDirective;
pub use ddirective::DDirective as DD;
pub use label::Label;
pub use mnemonic::Mnemonic;
pub use operand::Operand;
pub use operand::Operand as Op;
pub use register::Register;
pub use register::Register as Reg;
pub use section::Section;
use crate::convert::{ Separator, ToAssembly };
#[macro_export]
macro_rules! _instruction {
($mnemonic:expr) => {
InstructionBuilder::new($mnemonic).create()
};
($mnemonic:expr $(, $operands:expr)*) => {
InstructionBuilder::new($mnemonic)
.with_operands(&[ $($operands),* ])
.create()
}
}
pub struct InstructionBuilder {
instruction: Instruction,
}
impl InstructionBuilder {
pub fn new(mnemonic: Mnemonic) -> Self {
Self {
instruction: Instruction {
mnemonic,
operands: vec![],
comment: None,
}
}
}
pub fn with_operands(&mut self, operands: &[Operand]) -> &mut Self {
self.instruction.operands = operands.to_vec();
self
}
pub fn with_comment(&mut self, comment: &str) -> &mut Self {
self.instruction.comment = Some(comment.to_string());
self
}
pub fn create(&mut self) -> Instruction {
self.instruction.clone()
}
}
#[derive(Clone)]
pub struct Instruction {
pub mnemonic: Mnemonic,
pub operands: Vec<Operand>,
pub comment: Option<String>,
}
impl ToAssembly for Instruction {
fn to_assembly(&self, separator: Separator) -> String {
let mut assembly = String::new();
assembly += &format!(
"{}{}{}",
self.mnemonic.to_assembly(separator),
separator.value(),
self.operands.to_assembly(separator),
);
match &self.comment {
Some(comment) => assembly += &format!(" ; {}", comment),
None => {},
}
assembly
}
}