Skip to content

Commit 05811d1

Browse files
Try to reuse the original label name in Program::dump
1 parent df1ea69 commit 05811d1

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/parser.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use num_bigint::Sign;
22

33
use std::i64;
4+
use std::rc::Rc;
45

56
use label::Label;
67
use program::{Program, Command, Integer, BigInteger, SizedInteger, SourceLoc};
@@ -272,6 +273,7 @@ impl Program {
272273

273274
use program::Command::*;
274275
for (index, command) in self.commands.iter().enumerate() {
276+
let label = self.locs.as_ref().and_then(|l| l[index].label.as_ref());
275277
let (code, arg): (&[u8], _) = match *command {
276278
Push {value} => (b" ", Some(number_to_ws(value))),
277279
PushBig {ref value} => (b" ", Some(large_number_to_ws(value))),
@@ -287,11 +289,11 @@ impl Program {
287289
Modulo => (b"\t \t\t", None),
288290
Set => (b"\t\t ", None),
289291
Get => (b"\t\t\t", None),
290-
Label => (b"\n ", Some(label_to_ws(index))),
291-
Call {index} => (b"\n \t", Some(label_to_ws(index - 1))),
292-
Jump {index} => (b"\n \n", Some(label_to_ws(index - 1))),
293-
JumpIfZero {index} => (b"\n\t ", Some(label_to_ws(index - 1))),
294-
JumpIfNegative {index} => (b"\n\t\t", Some(label_to_ws(index - 1))),
292+
Label => (b"\n ", Some(label_to_ws(label, index))),
293+
Call {index} => (b"\n \t", Some(label_to_ws(label, index - 1))),
294+
Jump {index} => (b"\n \n", Some(label_to_ws(label, index - 1))),
295+
JumpIfZero {index} => (b"\n\t ", Some(label_to_ws(label, index - 1))),
296+
JumpIfNegative {index} => (b"\n\t\t", Some(label_to_ws(label, index - 1))),
295297
EndSubroutine => (b"\n\t\n", None),
296298
EndProgram => (b"\n\n\n", None),
297299
PrintChar => (b"\t\n ", None),
@@ -364,8 +366,14 @@ fn large_number_to_ws(n: &BigInteger) -> Vec<u8> {
364366
return res;
365367
}
366368

367-
fn label_to_ws(i: usize) -> Vec<u8> {
368-
let label: Label = i.to_string().as_bytes().into();
369+
fn label_to_ws(label: Option<&Rc<Label>>, i: usize) -> Vec<u8> {
370+
let label_storage: Label;
371+
let label = if let Some(l) = label {
372+
l
373+
} else {
374+
label_storage = i.to_string().as_bytes().into();
375+
&label_storage
376+
};
369377
let mut res: Vec<u8> = (&label).into_iter()
370378
.map(|i| if i {b'\t'} else {b' '})
371379
.collect();

0 commit comments

Comments
 (0)