-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring, test coverage and bug fixing (#3)
- Loading branch information
Showing
17 changed files
with
223 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "minicode" | ||
version = "0.1.3" | ||
version = "1.0.1" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::opcode::OperationType; | ||
use crate::opcode::OperationType::*; | ||
use crate::opcode::ValueType; | ||
use crate::opcode::ValueType::*; | ||
use std::collections::HashMap; | ||
|
||
pub fn calculate<'a>( | ||
key: &'a String, | ||
o_type: &OperationType, | ||
value: &i64, | ||
target: &mut HashMap<&'a String, ValueType>, | ||
) { | ||
let old_value = match target.get(key) { | ||
Some(s) => match s { | ||
Int(i) => i, | ||
Line(_s) => panic!("wrong type for calculate"), | ||
}, | ||
None => panic!("not value for calculate"), | ||
}; | ||
|
||
let new_value = match o_type { | ||
Increment => old_value + value, | ||
Decrement => old_value - value, | ||
}; | ||
|
||
target.insert(key, Int(new_value)); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::opcode::ValueType; | ||
use crate::opcode::ValueType::*; | ||
use std::collections::HashMap; | ||
|
||
pub fn condition( | ||
key: &String, | ||
true_or_false: &bool, | ||
target_value: &i64, | ||
storage: &HashMap<&String, ValueType>, | ||
) -> bool { | ||
let value = storage.get(key); | ||
|
||
match value { | ||
Some(s) => match s { | ||
Int(i) => { | ||
if *true_or_false { | ||
return i == target_value; | ||
} else { | ||
return i != target_value; | ||
} | ||
} | ||
Line(_s) => panic!("condition - wrong type value"), | ||
}, | ||
None => panic!("condition - not value"), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::opcode::ValueType; | ||
use crate::opcode::ValueType::*; | ||
use std::collections::HashMap; | ||
|
||
pub fn create<'a>(key: &'a String, value: &ValueType, target: &mut HashMap<&'a String, ValueType>) { | ||
match value { | ||
Int(v) => target.insert(key, Int(*v)), | ||
Line(s) => target.insert(key, Line(s.to_string())), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::opcode::ValueType; | ||
use crate::opcode::ValueType::*; | ||
use std::collections::HashMap; | ||
|
||
pub fn print_value(key: &String, storage: &HashMap<&String, ValueType>) { | ||
let value = storage.get(key); | ||
|
||
match value { | ||
Some(s) => match s { | ||
Int(i) => println!("{}", i), | ||
Line(s) => println!("{}", s), | ||
}, | ||
None => panic!("not value for print"), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use crate::opcode::OpCode; | ||
use crate::opcode::OpCode::*; | ||
use crate::opcode::ValueType::*; | ||
|
||
pub fn appropriation(data: Vec<&str>) -> OpCode { | ||
let value_name = data[1].to_string(); | ||
let value: String = data.into_iter().skip(2).collect::<Vec<&str>>().join(" "); | ||
match value.parse::<i64>() { | ||
Ok(parsed) => Create(value_name, Int(parsed)), | ||
Err(_e) => Create(value_name, Line(value)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::opcode::OpCode; | ||
use crate::opcode::OpCode::*; | ||
use crate::opcode::OperationType::*; | ||
|
||
pub fn calculation(data: Vec<&str>) -> OpCode { | ||
if data.len() != 4 { | ||
return ErrorCode("the operation is not specified correctly".to_string()); | ||
} | ||
|
||
let value_name = data[1].to_string(); | ||
|
||
let op = match data[2] { | ||
"+" => Increment, | ||
"-" => Decrement, | ||
_ => return ErrorCode("wrong operation".to_string()), | ||
}; | ||
|
||
match data[3].to_string().parse::<i64>() { | ||
Ok(parsed) => Operation(value_name, op, parsed), | ||
Err(_e) => ErrorCode("wrong type for operation".to_string()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::opcode::OpCode; | ||
use crate::opcode::OpCode::*; | ||
|
||
pub fn condition(data: Vec<&str>) -> OpCode { | ||
if data.len() != 5 { | ||
return ErrorCode("the operation is not specified correctly".to_string()); | ||
} | ||
|
||
let value_name = data[1].to_string(); | ||
let true_or_false = match data[2] { | ||
"=" => true, | ||
"!" => false, | ||
_ => return ErrorCode("wrong condition".to_string()), | ||
}; | ||
|
||
let target_value = match data[3].parse::<i64>() { | ||
Ok(parsed) => parsed, | ||
Err(_) => return ErrorCode("wrong target value for operation".to_string()), | ||
}; | ||
|
||
let target_pointer = match data[4].parse::<usize>() { | ||
Ok(parsed) => parsed, | ||
Err(_) => return ErrorCode("wrong target pointer".to_string()), | ||
}; | ||
|
||
Condition(value_name, target_value, true_or_false, target_pointer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::files::get_content; | ||
pub use crate::opcode::OpCode; | ||
pub use crate::opcode::OpCode::*; | ||
pub use crate::opcode::ValueType::*; | ||
|
||
pub fn file(data: Vec<&str>) -> OpCode { | ||
let value_name = data[1].to_string(); | ||
let content = get_content(&data[2].to_string()); | ||
Create(value_name, Line(content)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.