Skip to content

Commit 75a64d9

Browse files
Allow labels as values and parse them correctly
1 parent 99e1539 commit 75a64d9

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

src/load_file.rs

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,65 @@ enum_string!(pub AddressMode {
9393
});
9494

9595
impl Default for AddressMode {
96-
fn default() -> AddressMode {
97-
AddressMode::Direct
96+
fn default() -> Self {
97+
Self::Immediate
9898
}
9999
}
100100

101-
#[derive(Copy, Clone, Default, Debug, PartialEq)]
101+
#[derive(Clone, Debug, PartialEq)]
102+
pub enum Value {
103+
Label(String),
104+
Literal(i32),
105+
}
106+
107+
impl Default for Value {
108+
fn default() -> Self {
109+
Self::Literal(0)
110+
}
111+
}
112+
113+
impl ToString for Value {
114+
fn to_string(&self) -> String {
115+
match self {
116+
Self::Label(value) => value.clone(),
117+
Self::Literal(value) => value.to_string(),
118+
}
119+
}
120+
}
121+
122+
#[derive(Clone, Default, Debug, PartialEq)]
102123
pub struct Field {
103124
pub address_mode: AddressMode,
104-
pub value: i32,
125+
pub value: Value,
105126
}
106127

107128
impl Field {
108-
pub fn direct(value: i32) -> Field {
109-
Field {
129+
pub fn direct(value: i32) -> Self {
130+
Self {
110131
address_mode: AddressMode::Direct,
111-
value,
132+
value: Value::Literal(value),
112133
}
113134
}
114135

115136
pub fn immediate(value: i32) -> Field {
116-
Field {
137+
Self {
117138
address_mode: AddressMode::Immediate,
118-
value,
139+
value: Value::Literal(value),
119140
}
120141
}
121142
}
122143

123144
impl ToString for Field {
124145
fn to_string(&self) -> String {
125-
format!("{}{}", self.address_mode.to_string(), self.value)
146+
format!(
147+
"{}{}",
148+
self.address_mode.to_string(),
149+
self.value.to_string()
150+
)
126151
}
127152
}
128153

129-
#[derive(Copy, Clone, Default, Debug, PartialEq)]
154+
#[derive(Clone, Debug, Default, PartialEq)]
130155
pub struct Instruction {
131156
pub opcode: Opcode,
132157
pub modifier: Modifier,
@@ -194,6 +219,7 @@ impl Core {
194219
));
195220
}
196221

222+
// TODO: make this nice with some kind of try_insert
197223
labels
198224
.into_iter()
199225
.map(|label| match self.labels.entry(label.into()) {
@@ -238,7 +264,7 @@ impl fmt::Debug for Core {
238264
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
239265
write!(
240266
formatter,
241-
"Labels:\n{:?}\n Core:\n{}",
267+
"Labels:{:?}\nCore:\n{}",
242268
self.labels,
243269
self.dump()
244270
)
@@ -259,11 +285,11 @@ mod tests {
259285
modifier: Modifier::F,
260286
field_a: Field {
261287
address_mode: AddressMode::Direct,
262-
value: 0,
288+
value: Value::Literal(0),
263289
},
264290
field_b: Field {
265291
address_mode: AddressMode::Direct,
266-
value: 0,
292+
value: Value::Literal(0),
267293
},
268294
};
269295

@@ -281,6 +307,16 @@ mod tests {
281307
}
282308
}
283309

310+
#[test]
311+
fn value_to_string() {
312+
assert_eq!(
313+
String::from("some_label"),
314+
Value::Label(String::from("some_label")).to_string()
315+
);
316+
317+
assert_eq!(String::from("123"), Value::Literal(123).to_string());
318+
}
319+
284320
#[test]
285321
fn modifier_b_default() {
286322
let opcodes = [Mov, Cmp, Seq, Sne];
@@ -388,6 +424,9 @@ mod tests {
388424
core.add_labels(256, vec!["goblin"])
389425
.expect_err("Should have failed to add labels for 256, but didn't");
390426

427+
core.add_labels(5, vec!["baz"])
428+
.expect_err("Should have failed to add duplicate label");
429+
391430
assert_eq!(core.label_address("foo").unwrap(), 0);
392431
assert_eq!(core.label_address("bar").unwrap(), 0);
393432
assert_eq!(core.label_address("baz").unwrap(), 123);

0 commit comments

Comments
 (0)