From fc46719bbc9678be9db57103ad692c357a85ff70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20B=C3=B6ving?= Date: Sat, 28 Nov 2020 17:46:48 +0100 Subject: [PATCH] cargo fmt --- src/asm.rs | 17 ++++--- src/generate.rs | 1 + src/main.rs | 10 ++--- src/parse.rs | 116 ++++++++++++++++++++++++++++++++---------------- 4 files changed, 91 insertions(+), 53 deletions(-) diff --git a/src/asm.rs b/src/asm.rs index 3440059..4f6cf56 100644 --- a/src/asm.rs +++ b/src/asm.rs @@ -1,19 +1,19 @@ #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Argument { MemoryLocation(u8), - Constant(u8) + Constant(u8), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct Label<'a>{ +pub struct Label<'a> { pub name: &'a str, - pub location: u8 + pub location: u8, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum JumpArgument<'a> { Location(u8), - Label(&'a str) + Label(&'a str), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -22,25 +22,24 @@ pub enum Instruction<'a> { MemoryLocationInstruction(MemoryLocationInstruction, Option>), ConstantArgumentInstruction(ConstantArgumentInstruction, Option>), ArgumentInstruction(ArgumentInstruction, Option>), - Jump(JumpArgument<'a>, Option>) + Jump(JumpArgument<'a>, Option>), } - #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum NoArgumentInstruction { - NOP + NOP, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum MemoryLocationInstruction { - STA(u8) + STA(u8), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum ConstantArgumentInstruction { BRZ(u8), BRC(u8), - BRN(u8) + BRN(u8), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/src/generate.rs b/src/generate.rs index e69de29..8b13789 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -0,0 +1 @@ + diff --git a/src/main.rs b/src/main.rs index 504ccae..c38b89f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,14 @@ -use std::fs; use std::env; - +use std::fs; #[macro_use] extern crate pest_derive; use pest::Parser; - mod asm; -mod parse; mod generate; +mod parse; use parse::{parse_asm, AsmParser}; @@ -27,6 +25,8 @@ fn main() { } }; - let instructions = parse_asm(AsmParser::parse(parse::Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e))); + let instructions = parse_asm( + AsmParser::parse(parse::Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)), + ); println!("{:#?}", instructions); } diff --git a/src/parse.rs b/src/parse.rs index 80013d4..e9714fb 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -17,7 +17,11 @@ pub fn parse_asm<'a>(pairs: Pairs<'a, Rule>) -> Vec> { // Second can only be Some if we have a label, thus second must be the // instruction if that is the case. if second.is_some() { - instruction.push(parse_instruction(second.unwrap(), first, instruction_counter)); + instruction.push(parse_instruction( + second.unwrap(), + first, + instruction_counter, + )); } else { instruction.push(parse_instruction(first.unwrap(), None, instruction_counter)); } @@ -32,7 +36,11 @@ pub fn parse_asm<'a>(pairs: Pairs<'a, Rule>) -> Vec> { instruction } -fn parse_instruction<'a>(instruction: Pair<'a, Rule>, label: Option>, instruction_counter: u8) -> Instruction<'a>{ +fn parse_instruction<'a>( + instruction: Pair<'a, Rule>, + label: Option>, + instruction_counter: u8, +) -> Instruction<'a> { let label = label.map(|l| parse_label(l, instruction_counter)); let mut instruction = instruction.into_inner(); let mnemonic = instruction.next().unwrap(); @@ -41,61 +49,91 @@ fn parse_instruction<'a>(instruction: Pair<'a, Rule>, label: Option parse_no_arg_instruction(mnemonic, label), Rule::arg_instruction => { parse_arg_instruction(mnemonic, instruction.next().unwrap(), label) - }, + } Rule::jump_instruction => { parse_jump_instruction(mnemonic, instruction.next().unwrap(), label) - }, + } Rule::memory_location_instruction => { parse_memory_location_instruction(mnemonic, instruction.next().unwrap(), label) - }, + } Rule::constant_arg_instruction => { parse_constant_arg_instruction(mnemonic, instruction.next().unwrap(), label) - }, - _ => unreachable!() + } + _ => unreachable!(), } } -fn parse_no_arg_instruction<'a>(instruction: Pair<'a, Rule>, label: Option>) -> Instruction<'a> { +fn parse_no_arg_instruction<'a>( + instruction: Pair<'a, Rule>, + label: Option>, +) -> Instruction<'a> { match instruction.as_str() { "NOP" => Instruction::NoArgumentInstruction(NoArgumentInstruction::NOP, label), - _ => unreachable!() + _ => unreachable!(), } } -fn parse_arg_instruction<'a>(instruction: Pair<'a, Rule>, arg: Pair<'a, Rule>, label: Option>) -> Instruction<'a> { +fn parse_arg_instruction<'a>( + instruction: Pair<'a, Rule>, + arg: Pair<'a, Rule>, + label: Option>, +) -> Instruction<'a> { let arg = parse_argument(arg); match instruction.as_str() { "LDA" => Instruction::ArgumentInstruction(ArgumentInstruction::LDA(arg), label), "ADD" => Instruction::ArgumentInstruction(ArgumentInstruction::ADD(arg), label), "SUB" => Instruction::ArgumentInstruction(ArgumentInstruction::SUB(arg), label), - _ => unreachable!() + _ => unreachable!(), } } -fn parse_jump_instruction<'a>(instruction: Pair<'a, Rule>, arg: Pair<'a, Rule>, label: Option>) -> Instruction<'a> { +fn parse_jump_instruction<'a>( + instruction: Pair<'a, Rule>, + arg: Pair<'a, Rule>, + label: Option>, +) -> Instruction<'a> { let arg = parse_jump_argument(arg); match instruction.as_str() { "JMP" => Instruction::Jump(arg, label), - _ => unreachable!() - } -} - -fn parse_memory_location_instruction<'a>(instruction: Pair<'a, Rule>, arg: Pair<'a, Rule>,label: Option>) -> Instruction<'a> { - let arg_string = arg.as_str(); - let arg_value = u8::from_str_radix(&arg_string[1..arg_string.len()-1], 16).unwrap(); - match instruction.as_str() { - "STA" => Instruction::MemoryLocationInstruction(MemoryLocationInstruction::STA(arg_value), label), - _ => unreachable!() + _ => unreachable!(), } } -fn parse_constant_arg_instruction<'a>(instruction: Pair<'a, Rule>, arg: Pair<'a, Rule>, label: Option>) -> Instruction<'a> { +fn parse_memory_location_instruction<'a>( + instruction: Pair<'a, Rule>, + arg: Pair<'a, Rule>, + label: Option>, +) -> Instruction<'a> { + let arg_string = arg.as_str(); + let arg_value = u8::from_str_radix(&arg_string[1..arg_string.len() - 1], 16).unwrap(); + match instruction.as_str() { + "STA" => { + Instruction::MemoryLocationInstruction(MemoryLocationInstruction::STA(arg_value), label) + } + _ => unreachable!(), + } +} + +fn parse_constant_arg_instruction<'a>( + instruction: Pair<'a, Rule>, + arg: Pair<'a, Rule>, + label: Option>, +) -> Instruction<'a> { let arg_value = u8::from_str_radix(&arg.as_str()[1..], 16).unwrap(); match instruction.as_str() { - "BRZ" => Instruction::ConstantArgumentInstruction(ConstantArgumentInstruction::BRZ(arg_value), label), - "BRC" => Instruction::ConstantArgumentInstruction(ConstantArgumentInstruction::BRC(arg_value), label), - "BRN" => Instruction::ConstantArgumentInstruction(ConstantArgumentInstruction::BRN(arg_value), label), - _ => unreachable!() + "BRZ" => Instruction::ConstantArgumentInstruction( + ConstantArgumentInstruction::BRZ(arg_value), + label, + ), + "BRC" => Instruction::ConstantArgumentInstruction( + ConstantArgumentInstruction::BRC(arg_value), + label, + ), + "BRN" => Instruction::ConstantArgumentInstruction( + ConstantArgumentInstruction::BRN(arg_value), + label, + ), + _ => unreachable!(), } } @@ -104,26 +142,24 @@ fn parse_argument<'a>(argument: Pair<'a, Rule>) -> Argument { match argument.as_rule() { Rule::memory_location => { let arg_string = argument.as_str(); - let arg_value = u8::from_str_radix(&arg_string[1..arg_string.len()-1], 16).unwrap(); + let arg_value = u8::from_str_radix(&arg_string[1..arg_string.len() - 1], 16).unwrap(); Argument::MemoryLocation(arg_value) - }, + } Rule::digit_literal => { let arg_value = u8::from_str_radix(&argument.as_str()[1..], 16).unwrap(); Argument::Constant(arg_value) - }, - _ => unreachable!() + } + _ => unreachable!(), } } fn parse_label<'a>(label: Pair<'a, Rule>, instruction_counter: u8) -> Label<'a> { match label.as_rule() { - Rule::label => { - Label { - name: label.as_str(), - location: instruction_counter - } + Rule::label => Label { + name: label.as_str(), + location: instruction_counter, }, - _ => unreachable!() + _ => unreachable!(), } } @@ -131,7 +167,9 @@ fn parse_jump_argument<'a>(arg: Pair<'a, Rule>) -> JumpArgument<'a> { let arg = arg.into_inner().next().unwrap(); match arg.as_rule() { Rule::label => JumpArgument::Label(arg.as_str()), - Rule::jump_location => JumpArgument::Location(u8::from_str_radix(arg.as_str(), 16).unwrap()), - _ => unreachable!() + Rule::jump_location => { + JumpArgument::Location(u8::from_str_radix(arg.as_str(), 16).unwrap()) + } + _ => unreachable!(), } }