Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

Commit 37171b4

Browse files
authored
Merge pull request #10 from flew-software/development
Support for PRF importing
2 parents ae1dd20 + 760bf14 commit 37171b4

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/cli.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ args:
1111
short: i
1212
multiple: false
1313
about: Enables info logs
14+
- reg:
15+
long: reg
16+
multiple: false
17+
about: Imports registers from specified file
18+
takes_value: true

src/main.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ mod debug;
1515
mod label;
1616

1717
fn main() {
18-
let mut location = "";
1918
let yaml = load_yaml!("cli.yaml");
2019
let matches = App::from(yaml).get_matches();
21-
if let Some(x) = matches.value_of("FILE") {
22-
location = x;
23-
}
20+
let location = matches.value_of("FILE").unwrap();
21+
let mut register_file_location = "";
2422

23+
if let Some(x) = matches.value_of("reg") {
24+
register_file_location = x;
25+
}
26+
println!("{}",register_file_location);
2527
let info_log_filter = match matches.is_present("loginfo") {
2628
true => LevelFilter::Info,
2729
_ => LevelFilter::Off,
@@ -55,10 +57,17 @@ fn main() {
5557
.unwrap()
5658
.bytes()
5759
.map(|ch| ch.unwrap());
60+
5861
let mut vm = VM::new();
5962
for i in file {
6063
vm.program.append(&mut vec![i]);
6164
}
65+
if register_file_location != "" {
66+
let mut buffer = String::new();
67+
std::fs::File::open(register_file_location).unwrap().read_to_string(&mut buffer).unwrap();
68+
println!("{}",buffer);
69+
register::register_from_string(&buffer, &mut vm.registers)
70+
}
6271
vm.run();
6372
info!("process used {} register(s)", vm.get_register_usage());
6473
info!("process was allocated {}B", mem::size_of_val(&vm.registers))

src/register.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use log::error;
22

3-
#[derive(Debug, Clone, Copy)]
3+
#[derive(Debug, Clone, Copy, PartialEq)]
44
pub struct REGISTER {
55
pub content: i32,
66
pub locked: bool,
@@ -35,6 +35,27 @@ impl REGISTER {
3535
}
3636
}
3737

38+
pub fn register_from_string(s: &str, reg_array: &mut [REGISTER]) {
39+
let key_val_pairs: Vec<&str> = s.split("\n").collect();
40+
for key_val_pair in key_val_pairs {
41+
if key_val_pair.is_empty() {
42+
continue;
43+
}
44+
let sep: Vec<&str> = key_val_pair.split(":").collect();
45+
let key = sep[0].parse::<usize>().unwrap();
46+
let val = sep[1].parse::<i32>().unwrap();
47+
let locked = sep[2];
48+
reg_array[key].content = val;
49+
if locked == "1" {
50+
reg_array[key].locked = true;
51+
} else {
52+
reg_array[key].locked = false;
53+
}
54+
}
55+
}
56+
57+
58+
3859
#[cfg(test)]
3960
mod tests {
4061
use super::*;
@@ -86,4 +107,12 @@ mod tests {
86107
assert_eq!(sucessful, false);
87108

88109
}
110+
111+
#[test]
112+
fn test_register_from_string() {
113+
let s = "0:5:1\n1:10:0";
114+
let mut m = [REGISTER{ content: 0, locked: false }; 2];
115+
register_from_string(s, &mut m);
116+
assert_eq!(m[0], REGISTER{ content: 5, locked: true });
117+
}
89118
}

0 commit comments

Comments
 (0)