Implemented more of the assembler
This commit is contained in:
parent
4bfde58cc5
commit
fe69c0d0c8
4 changed files with 68 additions and 4 deletions
|
@ -5,7 +5,6 @@ use crate::assembler::operand_parser::integer_operand;
|
|||
use crate::assembler::register_parser::register;
|
||||
|
||||
use nom::types::CompleteStr;
|
||||
use nom::*;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct AssemblerInstruction {
|
||||
|
@ -15,6 +14,53 @@ pub struct AssemblerInstruction {
|
|||
op3: Option<Token>,
|
||||
}
|
||||
|
||||
impl AssemblerInstruction {
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut results = vec![];
|
||||
|
||||
match self.opcode.clone() {
|
||||
Token::Opcode { code } => match code {
|
||||
_ => {
|
||||
results.push(code as u8);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
println!("Incorrect opcode!");
|
||||
std::process::exit(0);
|
||||
}
|
||||
};
|
||||
|
||||
for operand in vec![&self.op1, &self.op2, &self.op3] {
|
||||
match operand {
|
||||
Some(t) => AssemblerInstruction::extract_operand(t, &mut results),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
fn extract_operand(t: &Token, results: &mut Vec<u8>) {
|
||||
match t {
|
||||
Token::Register { reg_num } => {
|
||||
results.push(*reg_num);
|
||||
},
|
||||
Token::Number { value } => {
|
||||
let conv = *value as u16;
|
||||
let byte1 = conv;
|
||||
let byte2 = conv >> 8;
|
||||
results.push(byte2 as u8);
|
||||
results.push(byte1 as u8);
|
||||
},
|
||||
_ => {
|
||||
println!("Opcode found in operand field");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
named!(pub instruction_one<CompleteStr, AssemblerInstruction>,
|
||||
do_parse!(
|
||||
o: opcode_load >>
|
||||
|
|
Reference in a new issue