Skip to content

Commit 0a204c8

Browse files
authored
Refactoring, test coverage and bug fixing (#3)
1 parent 27f1c3c commit 0a204c8

17 files changed

+223
-160
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "minicode"
3-
version = "0.1.3"
3+
version = "1.0.1"
44
edition = "2021"
55

66
[dependencies]

src/interpreter/calculate.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::opcode::OperationType;
2+
use crate::opcode::OperationType::*;
3+
use crate::opcode::ValueType;
4+
use crate::opcode::ValueType::*;
5+
use std::collections::HashMap;
6+
7+
pub fn calculate<'a>(
8+
key: &'a String,
9+
o_type: &OperationType,
10+
value: &i64,
11+
target: &mut HashMap<&'a String, ValueType>,
12+
) {
13+
let old_value = match target.get(key) {
14+
Some(s) => match s {
15+
Int(i) => i,
16+
Line(_s) => panic!("wrong type for calculate"),
17+
},
18+
None => panic!("not value for calculate"),
19+
};
20+
21+
let new_value = match o_type {
22+
Increment => old_value + value,
23+
Decrement => old_value - value,
24+
};
25+
26+
target.insert(key, Int(new_value));
27+
}

src/interpreter/code_operations.rs

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/interpreter/condition.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::opcode::ValueType;
2+
use crate::opcode::ValueType::*;
3+
use std::collections::HashMap;
4+
5+
pub fn condition(
6+
key: &String,
7+
true_or_false: &bool,
8+
target_value: &i64,
9+
storage: &HashMap<&String, ValueType>,
10+
) -> bool {
11+
let value = storage.get(key);
12+
13+
match value {
14+
Some(s) => match s {
15+
Int(i) => {
16+
if *true_or_false {
17+
return i == target_value;
18+
} else {
19+
return i != target_value;
20+
}
21+
}
22+
Line(_s) => panic!("condition - wrong type value"),
23+
},
24+
None => panic!("condition - not value"),
25+
};
26+
}

src/interpreter/create.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::opcode::ValueType;
2+
use crate::opcode::ValueType::*;
3+
use std::collections::HashMap;
4+
5+
pub fn create<'a>(key: &'a String, value: &ValueType, target: &mut HashMap<&'a String, ValueType>) {
6+
match value {
7+
Int(v) => target.insert(key, Int(*v)),
8+
Line(s) => target.insert(key, Line(s.to_string())),
9+
};
10+
}

src/interpreter/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ use crate::opcode::OpCode;
22
use crate::opcode::OpCode::*;
33
use crate::opcode::ValueType;
44
use std::collections::HashMap;
5-
mod code_operations;
5+
mod calculate;
6+
mod condition;
7+
mod create;
8+
mod print_value;
9+
use calculate::calculate;
10+
use condition::condition;
11+
use create::create;
12+
use print_value::print_value;
613

714
pub fn exegete(operations: Vec<OpCode>) {
815
let code_max_point = operations.len() - 1;
@@ -13,15 +20,15 @@ pub fn exegete(operations: Vec<OpCode>) {
1320
let operation = &operations[pointer];
1421

1522
match operation {
16-
Create(k, v) => code_operations::create(k, v, &mut addresses),
17-
Print(k) => code_operations::print_value(k, &addresses),
18-
Operation(k, o, v) => code_operations::calculate(k, o, v, &mut addresses),
23+
Create(k, v) => create(k, v, &mut addresses),
24+
Print(k) => print_value(k, &addresses),
25+
Operation(k, o, v) => calculate(k, o, v, &mut addresses),
1926
ErrorCode(e) => {
2027
println!("{} - line - {}", e, pointer + 1);
2128
break;
2229
}
2330
Condition(k, v, b, p) => {
24-
let result = code_operations::condition(k, b, v, &addresses);
31+
let result = condition(k, b, v, &addresses);
2532
if result {
2633
new_pointer(&mut pointer, p);
2734
}

src/interpreter/print_value.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::opcode::ValueType;
2+
use crate::opcode::ValueType::*;
3+
use std::collections::HashMap;
4+
5+
pub fn print_value(key: &String, storage: &HashMap<&String, ValueType>) {
6+
let value = storage.get(key);
7+
8+
match value {
9+
Some(s) => match s {
10+
Int(i) => println!("{}", i),
11+
Line(s) => println!("{}", s),
12+
},
13+
None => panic!("not value for print"),
14+
};
15+
}

src/parser/appropriation.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::opcode::OpCode;
2+
use crate::opcode::OpCode::*;
3+
use crate::opcode::ValueType::*;
4+
5+
pub fn appropriation(data: Vec<&str>) -> OpCode {
6+
let value_name = data[1].to_string();
7+
let value: String = data.into_iter().skip(2).collect::<Vec<&str>>().join(" ");
8+
match value.parse::<i64>() {
9+
Ok(parsed) => Create(value_name, Int(parsed)),
10+
Err(_e) => Create(value_name, Line(value)),
11+
}
12+
}

src/parser/calculation.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::opcode::OpCode;
2+
use crate::opcode::OpCode::*;
3+
use crate::opcode::OperationType::*;
4+
5+
pub fn calculation(data: Vec<&str>) -> OpCode {
6+
if data.len() != 4 {
7+
return ErrorCode("the operation is not specified correctly".to_string());
8+
}
9+
10+
let value_name = data[1].to_string();
11+
12+
let op = match data[2] {
13+
"+" => Increment,
14+
"-" => Decrement,
15+
_ => return ErrorCode("wrong operation".to_string()),
16+
};
17+
18+
match data[3].to_string().parse::<i64>() {
19+
Ok(parsed) => Operation(value_name, op, parsed),
20+
Err(_e) => ErrorCode("wrong type for operation".to_string()),
21+
}
22+
}

src/parser/condition.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::opcode::OpCode;
2+
use crate::opcode::OpCode::*;
3+
4+
pub fn condition(data: Vec<&str>) -> OpCode {
5+
if data.len() != 5 {
6+
return ErrorCode("the operation is not specified correctly".to_string());
7+
}
8+
9+
let value_name = data[1].to_string();
10+
let true_or_false = match data[2] {
11+
"=" => true,
12+
"!" => false,
13+
_ => return ErrorCode("wrong condition".to_string()),
14+
};
15+
16+
let target_value = match data[3].parse::<i64>() {
17+
Ok(parsed) => parsed,
18+
Err(_) => return ErrorCode("wrong target value for operation".to_string()),
19+
};
20+
21+
let target_pointer = match data[4].parse::<usize>() {
22+
Ok(parsed) => parsed,
23+
Err(_) => return ErrorCode("wrong target pointer".to_string()),
24+
};
25+
26+
Condition(value_name, target_value, true_or_false, target_pointer)
27+
}

src/parser/file.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::files::get_content;
2+
pub use crate::opcode::OpCode;
3+
pub use crate::opcode::OpCode::*;
4+
pub use crate::opcode::ValueType::*;
5+
6+
pub fn file(data: Vec<&str>) -> OpCode {
7+
let value_name = data[1].to_string();
8+
let content = get_content(&data[2].to_string());
9+
Create(value_name, Line(content))
10+
}

src/parser/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
mod opcode_operations;
1+
mod appropriation;
2+
mod calculation;
3+
mod condition;
4+
mod file;
25
mod opcode_parser;
6+
mod print;
7+
mod user_var;
38
use crate::opcode::*;
49

510
pub fn parse(lines: &Vec<String>) -> Vec<OpCode> {
@@ -20,6 +25,7 @@ pub fn parse(lines: &Vec<String>) -> Vec<OpCode> {
2025
#[cfg(test)]
2126
mod tests {
2227
use crate::opcode::OpCode::*;
28+
use crate::opcode::OperationType::*;
2329
use crate::opcode::ValueType::*;
2430
use crate::parser::*;
2531

@@ -31,6 +37,10 @@ mod tests {
3137
"p a".to_string(),
3238
"p b".to_string(),
3339
"dsad".to_string(),
40+
"= a + 1".to_string(),
41+
"= a - 1".to_string(),
42+
"? a ! 5 3".to_string(),
43+
"? a = 5 3".to_string(),
3444
];
3545

3646
let result = parse(&input);
@@ -41,6 +51,10 @@ mod tests {
4151
Print("a".to_string()),
4252
Print("b".to_string()),
4353
ErrorCode("Could not recognize the command".to_string()),
54+
Operation("a".to_string(), Increment, 1),
55+
Operation("a".to_string(), Decrement, 1),
56+
Condition("a".to_string(), 5, false, 3),
57+
Condition("a".to_string(), 5, true, 3),
4458
];
4559

4660
let mut i = 0;

src/parser/opcode_operations.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)