Skip to content

Commit

Permalink
Refactoring, test coverage and bug fixing (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored Jan 19, 2024
1 parent 27f1c3c commit 0a204c8
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 160 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
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]
Expand Down
27 changes: 27 additions & 0 deletions src/interpreter/calculate.rs
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));
}
70 changes: 0 additions & 70 deletions src/interpreter/code_operations.rs

This file was deleted.

26 changes: 26 additions & 0 deletions src/interpreter/condition.rs
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"),
};
}
10 changes: 10 additions & 0 deletions src/interpreter/create.rs
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())),
};
}
17 changes: 12 additions & 5 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use crate::opcode::OpCode;
use crate::opcode::OpCode::*;
use crate::opcode::ValueType;
use std::collections::HashMap;
mod code_operations;
mod calculate;
mod condition;
mod create;
mod print_value;
use calculate::calculate;
use condition::condition;
use create::create;
use print_value::print_value;

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

match operation {
Create(k, v) => code_operations::create(k, v, &mut addresses),
Print(k) => code_operations::print_value(k, &addresses),
Operation(k, o, v) => code_operations::calculate(k, o, v, &mut addresses),
Create(k, v) => create(k, v, &mut addresses),
Print(k) => print_value(k, &addresses),
Operation(k, o, v) => calculate(k, o, v, &mut addresses),
ErrorCode(e) => {
println!("{} - line - {}", e, pointer + 1);
break;
}
Condition(k, v, b, p) => {
let result = code_operations::condition(k, b, v, &addresses);
let result = condition(k, b, v, &addresses);
if result {
new_pointer(&mut pointer, p);
}
Expand Down
15 changes: 15 additions & 0 deletions src/interpreter/print_value.rs
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"),
};
}
12 changes: 12 additions & 0 deletions src/parser/appropriation.rs
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)),
}
}
22 changes: 22 additions & 0 deletions src/parser/calculation.rs
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()),
}
}
27 changes: 27 additions & 0 deletions src/parser/condition.rs
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)
}
10 changes: 10 additions & 0 deletions src/parser/file.rs
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))
}
16 changes: 15 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
mod opcode_operations;
mod appropriation;
mod calculation;
mod condition;
mod file;
mod opcode_parser;
mod print;
mod user_var;
use crate::opcode::*;

pub fn parse(lines: &Vec<String>) -> Vec<OpCode> {
Expand All @@ -20,6 +25,7 @@ pub fn parse(lines: &Vec<String>) -> Vec<OpCode> {
#[cfg(test)]
mod tests {
use crate::opcode::OpCode::*;
use crate::opcode::OperationType::*;
use crate::opcode::ValueType::*;
use crate::parser::*;

Expand All @@ -31,6 +37,10 @@ mod tests {
"p a".to_string(),
"p b".to_string(),
"dsad".to_string(),
"= a + 1".to_string(),
"= a - 1".to_string(),
"? a ! 5 3".to_string(),
"? a = 5 3".to_string(),
];

let result = parse(&input);
Expand All @@ -41,6 +51,10 @@ mod tests {
Print("a".to_string()),
Print("b".to_string()),
ErrorCode("Could not recognize the command".to_string()),
Operation("a".to_string(), Increment, 1),
Operation("a".to_string(), Decrement, 1),
Condition("a".to_string(), 5, false, 3),
Condition("a".to_string(), 5, true, 3),
];

let mut i = 0;
Expand Down
69 changes: 0 additions & 69 deletions src/parser/opcode_operations.rs

This file was deleted.

Loading

0 comments on commit 0a204c8

Please sign in to comment.