Skip to content

Commit 25d0e4d

Browse files
Slightly clean up label code and fix wrong default
1 parent 75a64d9 commit 25d0e4d

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/load_file.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum_string!(pub Opcode {
2727
});
2828

2929
impl Default for Opcode {
30-
fn default() -> Opcode {
30+
fn default() -> Self {
3131
Opcode::Dat
3232
}
3333
}
@@ -49,13 +49,13 @@ enum_string!(pub Modifier {
4949
});
5050

5151
impl Default for Modifier {
52-
fn default() -> Modifier {
52+
fn default() -> Self {
5353
Modifier::F
5454
}
5555
}
5656

5757
impl Modifier {
58-
pub fn default_88_to_94(opcode: Opcode, a_mode: AddressMode, b_mode: AddressMode) -> Modifier {
58+
pub fn default_88_to_94(opcode: Opcode, a_mode: AddressMode, b_mode: AddressMode) -> Self {
5959
/// Implemented based on the ICWS '94 document,
6060
/// section A.2.1.2: ICWS'88 to ICWS'94 Conversion
6161
use Opcode::*;
@@ -151,18 +151,30 @@ impl ToString for Field {
151151
}
152152
}
153153

154-
#[derive(Clone, Debug, Default, PartialEq)]
154+
#[derive(Clone, Debug, PartialEq)]
155155
pub struct Instruction {
156156
pub opcode: Opcode,
157157
pub modifier: Modifier,
158158
pub field_a: Field,
159159
pub field_b: Field,
160160
}
161161

162+
impl Default for Instruction {
163+
fn default() -> Self {
164+
Instruction {
165+
opcode: Opcode::default(),
166+
modifier: Modifier::default(),
167+
field_a: Field::direct(0),
168+
field_b: Field::direct(0),
169+
}
170+
}
171+
}
172+
162173
impl Instruction {
163-
pub fn new(opcode: Opcode, field_a: Field, field_b: Field) -> Instruction {
174+
pub fn new(opcode: Opcode, field_a: Field, field_b: Field) -> Self {
164175
let modifier =
165176
Modifier::default_88_to_94(opcode, field_a.address_mode, field_b.address_mode);
177+
166178
Instruction {
167179
opcode,
168180
modifier,
@@ -191,7 +203,7 @@ pub struct Core {
191203
}
192204

193205
impl Core {
194-
pub fn new(core_size: usize) -> Core {
206+
pub fn new(core_size: usize) -> Self {
195207
Core {
196208
instructions: vec![Instruction::default(); core_size],
197209
labels: HashMap::new(),
@@ -219,17 +231,18 @@ impl Core {
219231
));
220232
}
221233

222-
// TODO: make this nice with some kind of try_insert
223-
labels
224-
.into_iter()
225-
.map(|label| match self.labels.entry(label.into()) {
226-
Entry::Occupied(entry) => Err(format!("Duplicate label '{}'", entry.key())),
234+
for label in labels {
235+
match self.labels.entry(label.into()) {
236+
Entry::Occupied(entry) => {
237+
return Err(format!("Label '{}' already exists", entry.key()));
238+
}
227239
Entry::Vacant(entry) => {
228240
entry.insert(index);
229-
Ok(())
230241
}
231-
})
232-
.collect()
242+
}
243+
}
244+
245+
Ok(())
233246
}
234247

235248
pub fn label_address(&self, label: &str) -> Option<usize> {
@@ -255,7 +268,7 @@ impl Core {
255268
}
256269

257270
impl Default for Core {
258-
fn default() -> Core {
271+
fn default() -> Self {
259272
Core::new(DEFAULT_CORE_SIZE)
260273
}
261274
}
@@ -431,6 +444,7 @@ mod tests {
431444
assert_eq!(core.label_address("bar").unwrap(), 0);
432445
assert_eq!(core.label_address("baz").unwrap(), 123);
433446
assert_eq!(core.label_address("boo").unwrap(), 123);
447+
434448
assert!(core.label_address("goblin").is_none());
435449
assert!(core.label_address("never_mentioned").is_none());
436450
}

src/parser.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ pub fn parse(file_contents: &str) -> Result<Core, Error> {
9494
}
9595

9696
fn parse_instruction(mut instruction_pairs: Pairs<Rule>) -> Instruction {
97+
dbg!(&instruction_pairs.peek());
9798
let mut operation_pairs = instruction_pairs
9899
.next()
99100
.expect("Operation must be first pair after Label in Instruction")
100101
.into_inner();
101102

103+
dbg!(&operation_pairs.peek());
102104
let opcode = parse_opcode(
103105
&operation_pairs
104106
.next()
@@ -334,8 +336,10 @@ mod tests {
334336
Instruction::new(Opcode::Jmp, Field::direct(123), Field::direct(45)),
335337
);
336338

337-
expected_core.add_labels(0, &["preload", "begin"]).unwrap();
338-
expected_core.add_labels(2, &["loop", "main"]).unwrap();
339+
expected_core
340+
.add_labels(0, vec!["preload", "begin"])
341+
.unwrap();
342+
expected_core.add_labels(2, vec!["loop", "main"]).unwrap();
339343

340344
assert_eq!(parse(simple_input).unwrap(), expected_core);
341345
}

0 commit comments

Comments
 (0)