Skip to content

Commit 3f01a66

Browse files
authored
Hurray, now there are arrays in minicode! (#26)
1 parent 9843396 commit 3f01a66

19 files changed

+140
-19
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 = "1.3.7"
3+
version = "1.4.0"
44
authors = ["Kirill Leonov <leonov7632@gmail.com>"]
55
edition = "2021"
66
repository = "https://github.com/leonovk/minicode"

src/interpreter/arrays.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::opcode::ValueType;
2+
use crate::opcode::ValueType::*;
3+
use std::collections::HashMap;
4+
5+
pub fn push<'a>(key: &'a String, value: &'a String, target: &mut HashMap<&'a String, ValueType>) {
6+
let second_value = match target.get(value) {
7+
Some(some) => some.clone(),
8+
None => match value.parse::<f64>() {
9+
Ok(i) => Int(i),
10+
Err(_) => Line(value.to_string()),
11+
},
12+
};
13+
14+
let first_value: &mut Vec<ValueType> = match target.get_mut(key) {
15+
Some(some) => match some {
16+
Arr(a) => a,
17+
_ => panic!("not is array"),
18+
},
19+
None => panic!("not is array"),
20+
};
21+
22+
first_value.push(second_value);
23+
}

src/interpreter/calculate.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ pub fn calculate<'a>(
1212
) {
1313
let old_value = match target.get(key).expect("Variable value not found") {
1414
Int(int) => int,
15-
Line(_s) => panic!("wrong type for calculate"),
15+
_ => panic!("wrong type for calculate"),
1616
};
1717

1818
let operational_meaning = match value {
1919
Int(int) => int,
2020
Line(str) => match target.get(str) {
2121
Some(some) => match some {
2222
Int(link_int) => link_int,
23-
Line(_line) => panic!("wrong type for calculate"),
23+
_ => panic!("wrong type for calculate"),
2424
},
2525
None => panic!("wrong type for calculate"),
2626
},
27+
Arr(_arr) => panic!("wrong type for calculate"),
2728
};
2829

2930
target.insert(

src/interpreter/condition.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ pub fn condition(
1919
Some(some) => condition_result(first_value, some, operator),
2020
None => condition_result(first_value, &Line(str.to_string()), operator),
2121
},
22+
Arr(_arr) => panic!("you can't compare arrays"),
2223
}
2324
}
2425

2526
fn condition_result(first: &ValueType, second: &ValueType, operator: &ComparisonOperators) -> bool {
2627
if type_are_different(first, second) {
27-
panic!("You cannot compare values of different types!");
28+
panic!("You cannot compare values of different types, or arrays!");
2829
}
2930

3031
match operator {
@@ -36,6 +37,12 @@ fn condition_result(first: &ValueType, second: &ValueType, operator: &Comparison
3637
}
3738

3839
fn type_are_different(v1: &ValueType, v2: &ValueType) -> bool {
40+
if let ValueType::Arr(_) = v1 {
41+
return true;
42+
} else if let ValueType::Arr(_) = v2 {
43+
return true;
44+
}
45+
3946
match (v1, v2) {
4047
(ValueType::Int(_), ValueType::Line(_)) => true,
4148
(ValueType::Line(_), ValueType::Int(_)) => true,

src/interpreter/create.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn create<'a>(key: &'a String, value: &ValueType, target: &mut HashMap<&'a S
1212
None => target.insert(key, value.clone()),
1313
},
1414
},
15+
Arr(_arr) => target.insert(key, Arr(Vec::new())),
1516
};
1617
}
1718

@@ -30,28 +31,42 @@ fn complex_assignments_value<'a>(
3031
}
3132
}
3233

34+
// > a b 1
3335
fn multiple_values<'a>(
3436
values: Vec<&str>,
3537
target: &HashMap<&'a String, ValueType>,
3638
) -> Option<ValueType> {
39+
let index = match values[1].to_string().parse::<f64>() {
40+
Ok(p) => p,
41+
Err(_) => match target.get(&values[1].to_string()) {
42+
Some(some) => match some {
43+
Int(i) => *i,
44+
_ => return None,
45+
},
46+
None => return None,
47+
},
48+
};
49+
3750
match target.get(&values[0].to_string()) {
3851
Some(some) => match some {
3952
Int(_i) => None,
40-
Line(str) => match values[1].to_string().parse::<f64>() {
41-
Ok(parsed) => Some(Line(parse_char(str, parsed))),
42-
Err(_) => match target.get(&values[1].to_string()) {
43-
Some(some_second) => match some_second {
44-
Int(sec_int) => Some(Line(parse_char(str, *sec_int))),
45-
Line(_l) => None,
46-
},
47-
None => None,
48-
},
49-
},
53+
Line(l) => Some(Line(parse_char(l, index))),
54+
Arr(arr) => Some(parse_elem(arr, index)),
5055
},
5156
None => None,
5257
}
5358
}
5459

60+
fn parse_elem(vec: &Vec<ValueType>, index: f64) -> ValueType {
61+
let elem = &vec[index as usize];
62+
63+
match elem {
64+
Line(l) => Line(l.to_string()),
65+
Int(i) => Int(*i),
66+
Arr(a) => Arr(a.to_vec()),
67+
}
68+
}
69+
5570
fn parse_char(str: &String, index: f64) -> String {
5671
if (index as usize) > (str.len() - 1) {
5772
return "".to_string();
@@ -60,11 +75,13 @@ fn parse_char(str: &String, index: f64) -> String {
6075
}
6176
}
6277

78+
// > a b
6379
fn one_value<'a>(values: Vec<&str>, target: &HashMap<&'a String, ValueType>) -> Option<ValueType> {
6480
match target.get(&values[0].to_string()) {
6581
Some(s) => match s {
6682
Int(int) => Some(Int(*int)),
6783
Line(str) => Some(Line(str.to_string())),
84+
Arr(arr) => Some(Arr(arr.to_vec())),
6885
},
6986
None => None,
7087
}

src/interpreter/include.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn include(
1818
Some(s) => match s {
1919
Line(l) => result_args_value.push(l.to_string()),
2020
Int(i) => result_args_value.push(i.to_string()),
21+
Arr(_a) => panic!("You can't pass arrays as arguments"),
2122
},
2223
};
2324
}

src/interpreter/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use crate::opcode::ValueType::*;
55
use std::collections::HashMap;
66
use std::thread;
77
use std::time::Duration;
8+
mod arrays;
89
mod calculate;
910
mod condition;
1011
mod create;
1112
mod execute;
1213
mod include;
1314
mod print_file;
1415
mod print_value;
16+
use arrays::push;
1517
use calculate::calculate;
1618
use condition::condition;
1719
use create::create;
@@ -41,6 +43,7 @@ pub fn exegete(operations: Vec<OpCode>, args: Vec<String>) {
4143

4244
match operation {
4345
Create(k, v) => create(k, v, &mut addresses),
46+
ArrayPush(k, v) => push(k, v, &mut addresses),
4447
Print(k) => print_value(k, &addresses),
4548
Operation(k, o, v) => calculate(k, o, v, &mut addresses),
4649
ErrorCode(e) => {

src/interpreter/print_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn print_file(key: &String, path: &String, storage: &HashMap<&String, ValueT
1010
let printed_content = match value {
1111
Line(line) => line.to_string().replace("\\n", "\n"),
1212
Int(int) => int.to_string(),
13+
Arr(_arr) => panic!("you can't print an array"),
1314
};
1415

1516
match write_content_to_file(&printed_content, path) {

src/interpreter/print_value.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn print_value(key: &String, storage: &HashMap<&String, ValueType>) {
99
Some(s) => match s {
1010
Int(i) => println!("{}", i),
1111
Line(s) => println!("{}", s),
12+
Arr(_a) => panic!("you can't print an array"),
1213
},
1314
None => println!("{}", key),
1415
};

src/opcode/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pub enum ValueType {
33
Int(f64),
44
Line(String),
5+
Arr(Vec<ValueType>),
56
}
67

78
#[derive(PartialEq, Debug)]
@@ -23,6 +24,7 @@ pub enum ComparisonOperators {
2324
#[derive(PartialEq, Debug)]
2425
pub enum OpCode {
2526
Create(String, ValueType),
27+
ArrayPush(String, String),
2628
Print(String),
2729
PrintFile(String, String),
2830
Operation(String, OperationType, ValueType),

src/parser/arrays.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::ValueType::*;
4+
5+
// [] a
6+
pub fn appropriation_array(data: Vec<&str>) -> OpCode {
7+
let value_name = data[1].to_string();
8+
9+
Create(value_name, Arr(Vec::new()))
10+
}
11+
12+
// []< a b
13+
pub fn push_array(data: Vec<&str>) -> OpCode {
14+
if data.len() != 3 {
15+
return ErrorCode("the operation is not specified correctly".to_string());
16+
}
17+
18+
let arr_name = data[1].to_string();
19+
let value_name = data[2].to_string();
20+
21+
ArrayPush(arr_name, value_name)
22+
}

src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod appropriation;
2+
mod arrays;
23
mod calculation;
34
mod condition;
45
mod exec;

src/parser/opcode_parser.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use crate::opcode::OpCode;
22
pub use crate::opcode::OpCode::*;
33

44
use super::appropriation::appropriation;
5+
use super::arrays::*;
56
use super::calculation::calculation;
67
use super::condition::condition;
78
use super::exec::exec;
@@ -18,6 +19,8 @@ pub fn get_opcode(line: &String) -> OpCode {
1819

1920
match command {
2021
">" => appropriation(data),
22+
"[]" => appropriation_array(data),
23+
"[]<" => push_array(data),
2124
"p" => print(data),
2225
"f" => file(data),
2326
"$>" => user_var(data),
@@ -27,7 +30,8 @@ pub fn get_opcode(line: &String) -> OpCode {
2730
"&" => exec(data),
2831
"->" => include(data, false),
2932
"-->" => include(data, true),
30-
"sleep" => sleep(data),
33+
"-_-" => sleep(data),
34+
"#" => EmptyLine,
3135
_ => ErrorCode("Could not recognize the command".to_string()),
3236
}
3337
}

tests/examples/arrays_1.mcode

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Сначало заполняем массив
2+
3+
[] a
4+
> empty_line
5+
> word hello
6+
> i 0
7+
> char word i
8+
[]< a char
9+
= i + 1
10+
? char ! empty_line 7
11+
12+
# Затем выбираем из него нужные элементы, кладем их в переменные
13+
# и печатаем на экран
14+
15+
> h a 0
16+
> e a 1
17+
> l a 2
18+
> ll a 3
19+
> o a 4
20+
21+
p h
22+
p e
23+
p l
24+
p ll
25+
p o

tests/examples/arrays_2.mcode

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[] a
2+
[]< a 23
3+
[]< a 10
4+
5+
> b a 0
6+
> c a 1
7+
> sum 0
8+
9+
= sum + b
10+
= sum + c
11+
12+
# output: 33
13+
p sum

tests/examples/sleep.mcode

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
> text lol kek cheburek
22
p start sleep now
3-
sleep 10
3+
-_- 10
44
p stop sleep now
55
p text

tests/examples/sleep_2.mcode

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
p sleep 2
2-
sleep 10
2+
-_- 10

tests/examples/sleep_3.mcode

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
p sleep 3
2-
sleep 10
2+
-_- 10

0 commit comments

Comments
 (0)