Skip to content

Commit

Permalink
🐞 Global refactoring of the error display system (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored Jan 31, 2024
1 parent 01110c9 commit 683f42b
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 117 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 = "1.4.0"
version = "1.4.1"
authors = ["Kirill Leonov <[email protected]>"]
edition = "2021"
repository = "https://github.com/leonovk/minicode"
Expand Down
2 changes: 1 addition & 1 deletion src/code_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ use crate::parser::parse;
pub fn run(file: String, args: Vec<String>) {
let lines = get_lines(&file);
let result = parse(&lines);
exegete(result, args);
exegete(result, args, &file);
}
13 changes: 10 additions & 3 deletions src/interpreter/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use super::opcode_result_type::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;

pub fn push<'a>(key: &'a String, value: &'a String, target: &mut HashMap<&'a String, ValueType>) {
pub fn push<'a>(
key: &'a String,
value: &'a String,
target: &mut HashMap<&'a String, ValueType>,
) -> Result<OpCodeResultType, String> {
let second_value = match target.get(value) {
Some(some) => some.clone(),
None => match value.parse::<f64>() {
Expand All @@ -14,10 +19,12 @@ pub fn push<'a>(key: &'a String, value: &'a String, target: &mut HashMap<&'a Str
let first_value: &mut Vec<ValueType> = match target.get_mut(key) {
Some(some) => match some {
Arr(a) => a,
_ => panic!("not is array"),
_ => return Err("not is array".to_string()),
},
None => panic!("not is array"),
None => return Err("not is array".to_string()),
};

first_value.push(second_value);

Ok(OpCodeResultType::Empty)
}
36 changes: 21 additions & 15 deletions src/interpreter/calculate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::opcode_result_type::*;
use crate::opcode::OperationType;
use crate::opcode::OperationType::*;
use crate::opcode::ValueType;
Expand All @@ -9,28 +10,33 @@ pub fn calculate<'a>(
o_type: &OperationType,
value: &ValueType,
target: &mut HashMap<&'a String, ValueType>,
) {
let old_value = match target.get(key).expect("Variable value not found") {
Int(int) => int,
_ => panic!("wrong type for calculate"),
) -> Result<OpCodeResultType, String> {
let old_value = match target.get(key) {
Some(s) => match s {
Int(int) => int,
_ => return Err("wrong type for calculate".to_string()),
},
None => return Err("Value not found".to_string()),
};

let operational_meaning = match value {
Int(int) => int,
Line(str) => match target.get(str) {
Some(some) => match some {
Int(link_int) => link_int,
_ => panic!("wrong type for calculate"),
_ => return Err("wrong type for calculate".to_string()),
},
None => panic!("wrong type for calculate"),
None => return Err("wrong type for calculate".to_string()),
},
Arr(_arr) => panic!("wrong type for calculate"),
Arr(_arr) => return Err("wrong type for calculate".to_string()),
};

target.insert(
key,
calculate_new_value(old_value, operational_meaning, o_type),
);

Ok(OpCodeResultType::Empty)
}

fn calculate_new_value(old_value: &f64, oper_value: &f64, o_type: &OperationType) -> ValueType {
Expand All @@ -55,7 +61,7 @@ mod tests {
let mut map: HashMap<&String, ValueType> = HashMap::new();
let binding = String::from("test_key");
map.insert(&binding, Int(10.0));
calculate(&binding, &OperationType::Increment, &Int(5.0), &mut map);
let _ = calculate(&binding, &OperationType::Increment, &Int(5.0), &mut map);
assert_eq!(map.get(&String::from("test_key")), Some(&Int(15.0)));
}

Expand All @@ -66,7 +72,7 @@ mod tests {
let binding_2 = String::from("test_key_2");
map.insert(&binding, Int(10.0));
map.insert(&binding_2, Int(5.0));
calculate(
let _ = calculate(
&binding,
&OperationType::Increment,
&Line("test_key_2".to_string()),
Expand All @@ -80,7 +86,7 @@ mod tests {
let mut map: HashMap<&String, ValueType> = HashMap::new();
let binding = String::from("test_key");
map.insert(&binding, Int(10.0));
calculate(&binding, &OperationType::Decrement, &Int(3.0), &mut map);
let _ = calculate(&binding, &OperationType::Decrement, &Int(3.0), &mut map);
assert_eq!(map.get(&String::from("test_key")), Some(&Int(7.0)));
}

Expand All @@ -91,7 +97,7 @@ mod tests {
let binding_2 = String::from("test_key_2");
map.insert(&binding, Int(10.0));
map.insert(&binding_2, Int(5.0));
calculate(
let _ = calculate(
&binding,
&OperationType::Decrement,
&Line("test_key_2".to_string()),
Expand All @@ -105,7 +111,7 @@ mod tests {
let mut map: HashMap<&String, ValueType> = HashMap::new();
let binding = String::from("test_key");
map.insert(&binding, Int(10.0));
calculate(
let _ = calculate(
&binding,
&OperationType::Multiplication,
&Int(3.0),
Expand All @@ -121,7 +127,7 @@ mod tests {
let binding_2 = String::from("test_key_2");
map.insert(&binding, Int(10.0));
map.insert(&binding_2, Int(5.0));
calculate(
let _ = calculate(
&binding,
&OperationType::Multiplication,
&Line("test_key_2".to_string()),
Expand All @@ -135,7 +141,7 @@ mod tests {
let mut map: HashMap<&String, ValueType> = HashMap::new();
let binding = String::from("test_key");
map.insert(&binding, Int(10.0));
calculate(&binding, &OperationType::Division, &Int(3.0), &mut map);
let _ = calculate(&binding, &OperationType::Division, &Int(3.0), &mut map);
assert_eq!(
map.get(&String::from("test_key")),
Some(&Int(3.3333333333333335))
Expand All @@ -149,7 +155,7 @@ mod tests {
let binding_2 = String::from("test_key_2");
map.insert(&binding, Int(10.0));
map.insert(&binding_2, Int(5.0));
calculate(
let _ = calculate(
&binding,
&OperationType::Division,
&Line("test_key_2".to_string()),
Expand Down
Loading

0 comments on commit 683f42b

Please sign in to comment.