Finished assembler paser

The assembler parser is finished and all tests do pass but the "load" command keeps pointing to "sub" for some reason.
This commit is contained in:
Anthony Foxclaw 2020-02-07 23:53:59 -05:00
parent fe69c0d0c8
commit afb68e46b3
5 changed files with 33 additions and 32 deletions

View file

@ -18,7 +18,7 @@ impl AssemblerInstruction {
pub fn to_bytes(&self) -> Vec<u8> {
let mut results = vec![];
match self.opcode.clone() {
match self.opcode.to_owned() {
Token::Opcode { code } => match code {
_ => {
results.push(code as u8);
@ -26,7 +26,7 @@ impl AssemblerInstruction {
},
_ => {
println!("Incorrect opcode!");
std::process::exit(0);
std::process::exit(1);
}
};
@ -49,6 +49,7 @@ impl AssemblerInstruction {
let conv = *value as u16;
let byte1 = conv;
let byte2 = conv >> 8;
results.push(byte2 as u8);
results.push(byte1 as u8);
},
@ -56,7 +57,7 @@ impl AssemblerInstruction {
println!("Opcode found in operand field");
std::process::exit(1);
}
}
};
}
}
@ -83,7 +84,7 @@ mod instruction_parser_test {
use crate::instruction::Opcode;
#[test]
fn test_parse_instruction() {
fn test_parse_instruction_form_one() {
let result = instruction_one(CompleteStr("load $0 #100\n"));
assert_eq!(
result,

View file

@ -17,6 +17,7 @@ mod opcode_parser_test {
fn test_parser_op_load() {
// Test that opcode is dected and parsed correctly
let result = opcode_load(CompleteStr("load"));
assert_eq!(result.is_ok(), true);
let (rest, token) = result.unwrap();
assert_eq!(token, Token::Opcode { code: Opcode::LOAD });
assert_eq!(rest, CompleteStr(""));

View file

@ -44,6 +44,7 @@ mod instruction_parser_test {
#[test]
fn test_program_to_bytes() {
let result = program(CompleteStr("load $0 #100\n"));
assert_eq!(result.is_ok(), true);
let (_, prog) = result.unwrap();
let bytecode = prog.to_bytes();
assert_eq!(bytecode.len(), 4);

View file

@ -17,3 +17,17 @@ named!(pub register <CompleteStr, Token>,
)
)
);
mod register_parser_tests {
use super::*;
#[test]
fn test_parse_register() {
let result = register(CompleteStr("$0"));
assert_eq!(result.is_ok(), true);
let result = register(CompleteStr("0"));
assert_eq!(result.is_ok(), false);
let result = register(CompleteStr("$a"));
assert_eq!(result.is_ok(), false);
}
}