Skip to content

Commit

Permalink
Add parser tests for future implementation of binary literal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Granddave committed Dec 23, 2023
1 parent 9c9a71a commit 6ef8480
Showing 1 changed file with 124 additions and 8 deletions.
132 changes: 124 additions & 8 deletions src/assembler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@ mod tests {
ASTOperand::Immediate(0xC8),
),
),
(
// Immediate - hex
"LDA #%01010101",
ASTInstructionNode::new(
ASTMnemonic::LDA,
ASTAddressingMode::Immediate,
ASTOperand::Immediate(0b01010101),
),
),
(
// Immediate - decimal
"LDA #128",
Expand Down Expand Up @@ -676,6 +685,15 @@ mod tests {
ASTOperand::Absolute(0x0ABC),
),
),
(
// Absolute - decimal
"ADC %1111111111111111",
ASTInstructionNode::new(
ASTMnemonic::ADC,
ASTAddressingMode::Absolute,
ASTOperand::Absolute(0b1111111111111111),
),
),
(
// Absolute - decimal
"ADC 65535",
Expand Down Expand Up @@ -703,6 +721,15 @@ mod tests {
ASTOperand::ZeroPage(0x0F),
),
),
(
// ZeroPage - binary
"ADC %10000000",
ASTInstructionNode::new(
ASTMnemonic::ADC,
ASTAddressingMode::ZeroPage,
ASTOperand::ZeroPage(0b10000000),
),
),
(
// ZeroPage - decimal
"ADC 128",
Expand All @@ -721,6 +748,15 @@ mod tests {
ASTOperand::ZeroPage(0xC8),
),
),
(
// ZeroPageX - binary
"INC %01010101,X",
ASTInstructionNode::new(
ASTMnemonic::INC,
ASTAddressingMode::ZeroPageX,
ASTOperand::ZeroPage(0b01010101),
),
),
(
// ZeroPageX - decimal
"INC 128,X",
Expand All @@ -739,6 +775,15 @@ mod tests {
ASTOperand::ZeroPage(0xC8),
),
),
(
// ZeroPageY - binary
"LDX %010101010,Y",
ASTInstructionNode::new(
ASTMnemonic::LDX,
ASTAddressingMode::ZeroPageY,
ASTOperand::ZeroPage(0b01010101),
),
),
(
// ZeroPageY - decimal
"LDX 128,Y",
Expand All @@ -757,6 +802,15 @@ mod tests {
ASTOperand::Absolute(0x00EF),
),
),
(
// AbsoluteX - binary
"CMP %01010101,X",
ASTInstructionNode::new(
ASTMnemonic::CMP,
ASTAddressingMode::AbsoluteX,
ASTOperand::Absolute(0b01010101),
),
),
(
// AbsoluteX - decimal
"CMP 65535,X",
Expand Down Expand Up @@ -784,6 +838,15 @@ mod tests {
ASTOperand::Relative(0x03),
),
),
(
// Relative - binary
"BEQ %00000011",
ASTInstructionNode::new(
ASTMnemonic::BEQ,
ASTAddressingMode::Relative,
ASTOperand::Relative(3),
),
),
(
// Relative - decimal
"BEQ 3",
Expand All @@ -802,6 +865,15 @@ mod tests {
ASTOperand::Absolute(0xBEEF),
),
),
(
// Indirect - binary
"JMP (%1111111111111111)",
ASTInstructionNode::new(
ASTMnemonic::JMP,
ASTAddressingMode::Indirect,
ASTOperand::Absolute(65535),
),
),
(
// Indirect - decimal
"JMP (65535)",
Expand All @@ -820,6 +892,15 @@ mod tests {
ASTOperand::ZeroPage(0xC8),
),
),
(
// Indirect indexed X - binary
"EOR (%01010101,X)",
ASTInstructionNode::new(
ASTMnemonic::EOR,
ASTAddressingMode::IndirectIndexedX,
ASTOperand::ZeroPage(0b0101010),
),
),
(
// Indirect indexed X - decimal
"EOR (128,X)",
Expand All @@ -838,6 +919,15 @@ mod tests {
ASTOperand::ZeroPage(0xC8),
),
),
(
// Indirect indexed Y - binary
"LDA (%01010101),Y",
ASTInstructionNode::new(
ASTMnemonic::LDA,
ASTAddressingMode::IndirectIndexedY,
ASTOperand::ZeroPage(0b01010101),
),
),
(
// Indirect indexed Y - decimal
"LDA (128),Y",
Expand Down Expand Up @@ -878,33 +968,54 @@ mod tests {
fn test_parse_constant() -> Result<(), ParseError> {
let tests = vec![
(
"define zero $00",
// byte hex
"define val $FE",
vec![ASTNode::Constant(ASTConstantNode::new_byte(
"val".to_string(),
0xFE,
))],
),
(
// byte binary
"define val %01010101",
vec![ASTNode::Constant(ASTConstantNode::new_byte(
"zero".to_string(),
0x00,
"val".to_string(),
0b01010101,
))],
),
(
"define zero 0",
// byte decimal
"define val 32",
vec![ASTNode::Constant(ASTConstantNode::new_byte(
"zero".to_string(),
0,
"val".to_string(),
32,
))],
),
(
// word hex
"define sysRandom $d010",
vec![ASTNode::Constant(ASTConstantNode::new_word(
"sysRandom".to_string(),
0xd010,
))],
),
(
// word decimal
"define sysRandom %0101010101010101",
vec![ASTNode::Constant(ASTConstantNode::new_word(
"sysRandom".to_string(),
0b0101010101010101,
))],
),
(
// word decimal
"define sysRandom 53264",
vec![ASTNode::Constant(ASTConstantNode::new_word(
"sysRandom".to_string(),
53264,
))],
),
// Different usages
(
"define sysRandom $d010\nLDY sysRandom",
vec![
Expand Down Expand Up @@ -1020,8 +1131,8 @@ mod tests {
for (input, expected) in tests {
let mut lexer = Lexer::new(input);
let mut parser = Parser::new(&mut lexer);
eprintln!("-----");
eprintln!("input: \n\n{}\n", input);
// eprintln!("-----");
// eprintln!("input: \n\n{}\n", input);
assert_eq!(parser.parse_program()?, expected);
}
Ok(())
Expand All @@ -1030,6 +1141,7 @@ mod tests {
#[test]
fn test_parse_program() -> Result<(), ParseError> {
let input = " define zero 0
define some_constant %01010101
LDX #zero
LDY #0
firstloop:
Expand All @@ -1048,6 +1160,10 @@ secondloop:
BNE secondloop";
let expected = vec![
ASTNode::Constant(ASTConstantNode::new_byte("zero".to_string(), 0x00)),
ASTNode::Constant(ASTConstantNode::new_byte(
"some_constant".to_string(),
0b01010101,
)),
ASTNode::Instruction(ASTInstructionNode::new(
ASTMnemonic::LDX,
ASTAddressingMode::Immediate,
Expand Down

0 comments on commit 6ef8480

Please sign in to comment.