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

Commit

Permalink
Merge pull request #10 from flew-software/development
Browse files Browse the repository at this point in the history
Support for PRF importing
  • Loading branch information
tarithj authored Apr 23, 2021
2 parents ae1dd20 + 760bf14 commit 37171b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ args:
short: i
multiple: false
about: Enables info logs
- reg:
long: reg
multiple: false
about: Imports registers from specified file
takes_value: true
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ mod debug;
mod label;

fn main() {
let mut location = "";
let yaml = load_yaml!("cli.yaml");
let matches = App::from(yaml).get_matches();
if let Some(x) = matches.value_of("FILE") {
location = x;
}
let location = matches.value_of("FILE").unwrap();
let mut register_file_location = "";

if let Some(x) = matches.value_of("reg") {
register_file_location = x;
}
println!("{}",register_file_location);
let info_log_filter = match matches.is_present("loginfo") {
true => LevelFilter::Info,
_ => LevelFilter::Off,
Expand Down Expand Up @@ -55,10 +57,17 @@ fn main() {
.unwrap()
.bytes()
.map(|ch| ch.unwrap());

let mut vm = VM::new();
for i in file {
vm.program.append(&mut vec![i]);
}
if register_file_location != "" {
let mut buffer = String::new();
std::fs::File::open(register_file_location).unwrap().read_to_string(&mut buffer).unwrap();
println!("{}",buffer);
register::register_from_string(&buffer, &mut vm.registers)
}
vm.run();
info!("process used {} register(s)", vm.get_register_usage());
info!("process was allocated {}B", mem::size_of_val(&vm.registers))
Expand Down
31 changes: 30 additions & 1 deletion src/register.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::error;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct REGISTER {
pub content: i32,
pub locked: bool,
Expand Down Expand Up @@ -35,6 +35,27 @@ impl REGISTER {
}
}

pub fn register_from_string(s: &str, reg_array: &mut [REGISTER]) {
let key_val_pairs: Vec<&str> = s.split("\n").collect();
for key_val_pair in key_val_pairs {
if key_val_pair.is_empty() {
continue;
}
let sep: Vec<&str> = key_val_pair.split(":").collect();
let key = sep[0].parse::<usize>().unwrap();
let val = sep[1].parse::<i32>().unwrap();
let locked = sep[2];
reg_array[key].content = val;
if locked == "1" {
reg_array[key].locked = true;
} else {
reg_array[key].locked = false;
}
}
}



#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -86,4 +107,12 @@ mod tests {
assert_eq!(sucessful, false);

}

#[test]
fn test_register_from_string() {
let s = "0:5:1\n1:10:0";
let mut m = [REGISTER{ content: 0, locked: false }; 2];
register_from_string(s, &mut m);
assert_eq!(m[0], REGISTER{ content: 5, locked: true });
}
}

0 comments on commit 37171b4

Please sign in to comment.