ParserCalculator 0.1.3

In the parser, we will parse a mathematical expression into its main components: numbers, signs, brackets. The result of the parsing will be used to calculate the mathematical expression, taking into account priorities: first the brackets, then multiplication/dividing and at the end addition and subtraction. The calculator also takes into account the nested brackets. The calculator also raises the number to the power (pow(n, p)), calculates the square root (sqrt(n)) and calculates sin(n) and cos(n).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#![doc = include_str!("../README.md")]

pub mod error;
use std::char;

use error::{CalcError, Res};
#[derive(pest_derive::Parser)]
#[grammar = "grammar.pest"]
pub struct ParserCalculator;

/// Це парсер математичних виразів який бере наш вираз(input) та парсить його на
/// основні складові: числа, знаки, дужки.
/// Вираз складається з:
/// - Числа
/// - Нуль або більше операторів та чисел
/// - Нуль або більше дужок з виразами всередині
///
/// Порядок обчислення стандартний:
/// 1. Дужки
/// 2. Множення і ділення
/// 3. Додавання і віднімання

pub fn pars(input: &str) -> Res<i32> {
    if input.trim().is_empty() {
        return Err(CalcError::EmptyExp);
    }
    let input = input.trim();
    check_bracket(&input.to_string())
}
/// Функція що видаляє коментарі та зайві пробіли
pub fn remove_spaces(input: &String) -> String {
    let mut res = String::new();
    let mut chars = input.chars().peekable();
    while let Some(&c) = chars.peek() {
        match c {
            ' ' | '\t' | '\n' => {
                chars.next();
            }
            '#' => {
                while let Some(&c) = chars.peek() {
                    chars.next();
                    if c == '\n' {
                        break;
                    }
                }
            }
            _ => {
                res.push(c);
                chars.next();
            }
        }
    }
    res
}
/// Функція шукає дужки у нашому виразі, якщо знаходить то
/// обчислюємо значення в дужках та замінюємо цей вираз з дужками на саме значення.
pub fn check_bracket(input: &String) -> Res<i32> {
    let input = remove_spaces(input);
    if input.contains("pow") {
        return to_power(&input);
    }
    if input.contains("sqrt") {
        return to_sqrt(&input);
    }
    if input.contains("sin") {
        return sin_func(&input);
    }
    if input.contains("cos") {
        return cos_func(&input);
    }
    if input.contains("!") && input.contains('(') && input.contains(')') {
        return fac_func(&input);
    }
    if input.starts_with("-") {
        return unary_func(&input);
    }
    if input.starts_with("|") && input.ends_with("|") {
        return abs_func(&input);
    }
    let mut new_input = input.clone();
    while new_input.contains('(') && new_input.contains(')') {
        let input_chars: Vec<char> = new_input.chars().collect();
        let mut open_bracket: Option<usize> = None;
        let mut close_bracket: Option<usize> = None;

        for (a, &c) in input_chars.iter().enumerate() {
            if c == '(' {
                open_bracket = Some(a);
            } else if c == ')' && open_bracket.is_some() {
                close_bracket = Some(a);
                break;
            }
        }
        if open_bracket.is_none() || close_bracket.is_none() {
            return Err(CalcError::NoBracket);
        }

        let index_start = open_bracket.unwrap();
        let index_end = close_bracket.unwrap();
        let in_bracket = input_chars[index_start + 1..index_end].iter().collect();

        let num_in_bracket = parse_number(&in_bracket);
        let sign_in_bracket = parse_sign(&in_bracket);
        let value_bracket = calc(num_in_bracket, sign_in_bracket)?;

        new_input.replace_range(index_start..=index_end, &value_bracket.to_string());
    }
    let number_input: Vec<i32> = parse_number(&new_input);
    let sign_input: Vec<char> = parse_sign(&new_input);

    calc(number_input, sign_input)
}

/// Парсимо input на числа.
/// Для цього ми замінюємо всі знаки на пробіли і потім
/// парсимо по пробілам наші числв.
/// Число - це одна або більше цифр(0-9).
pub fn parse_number(input: &String) -> Vec<i32> {
    let number: Vec<i32> = input
        .replace("+", " ")
        .replace("-", " ")
        .replace("*", " ")
        .replace("/", " ")
        .split_whitespace()
        .filter_map(|n| n.parse::<i32>().ok())
        .collect();
    println!("{:?}", number);
    number
}

/// Парсимо input на знаки.
/// Для цього ми просто використовуємо метод filtre щоб відфільтрувати
/// всі знаки від інших символів та чисел.
/// Підтримуються знаки: +, -. *, /

pub fn parse_sign(input: &String) -> Vec<char> {
    let ch: Vec<char> = input
        .chars()
        .filter(|c| *c == '+' || *c == '-' || *c == '*' || *c == '/')
        .collect();

    println!("{:?}", ch);
    ch
}

/// Функція яка обчислює значення виразу приймаючи як аргумент ветор
/// з числами та знаками. Спочатку ми шукаємо "*" та "/" щоб виконати ці дії,
/// обчислюємо число яке стоїть на місці(a) індекса знака "*" або "/" з a+1
/// та отримуємо значення. Потім додавання та віднімання.

pub fn calc(mut number: Vec<i32>, mut ch: Vec<char>) -> Res<i32> {
    if number.is_empty() {
        return Err(CalcError::EmptyExp);
    }
    let mut b = 0;
    while b < ch.len() {
        if b + 1 >= number.len() {
            break;
        }
        if ch[b] == '*' {
            number[b] *= number[b + 1];
            number.remove(b + 1);
            ch.remove(b);
        } else if ch[b] == '/' {
            if number[b + 1] == 0 {
                return Err(CalcError::DivByZero);
            }
            number[b] /= number[b + 1];
            number.remove(b + 1);
            ch.remove(b);
        } else {
            b += 1;
        }
    }

    let mut res = number[0];
    for a in 0..ch.len() {
        if a + 1 >= number.len() {
            break;
        }
        if ch[a] == '+' {
            res += number[a + 1];
        } else if ch[a] == '-' {
            res -= number[a + 1];
        }
    }

    Ok(res)
}

pub fn to_power(input: &String) -> Res<i32> {
    let mut res: i32 = 1;
    let replace_input: String = input.replace("pow(", "").replace(")", "");
    if replace_input.is_empty() {
        return Err(CalcError::EmptyExp);
    }
    let numbers: Vec<i32> = replace_input
        .replace(",", " ")
        .split(" ")
        .map(|s| s.parse().unwrap())
        .collect();
    let number = numbers[0];
    let power = numbers[1];
    for _ in 0..power as usize {
        res *= number;
    }
    Ok(res)
}

pub fn to_sqrt(input: &String) -> Res<i32> {
    let replcae_input: String = input.replace("sqrt(", "").replace(")", "");

    if replcae_input.is_empty() {
        return Err(CalcError::EmptyExp);
    }
    let number = replcae_input.parse().unwrap();
    let mut res: i32 = 0;
    for i in 1..number {
        if i * i == number {
            res = i;
            break;
        }
    }
    Ok(res)
}

pub fn sin_func(input: &String) -> Res<i32> {
    let num = input.replace("sin(", "").replace(")", "");
    let num: f64 = num.parse().unwrap();
    Ok(num.to_radians().sin().round() as i32)
}

pub fn cos_func(input: &String) -> Res<i32> {
    let num = input.replace("cos(", "").replace(")", "");
    let num: f64 = num.parse().unwrap();
    Ok(num.to_radians().cos().round() as i32)
}

pub fn fac_func(input: &String) -> Res<i32> {
    if input.is_empty() {
        return Err(CalcError::EmptyExp);
    }
    let mut res = 1;
    let mut count = 0;
    for i in input.chars() {
        if i == '!' {
            count += 1;
        }
    }
    if count == 1 {
        let num = input.replace("!", "");
        let num: i32 = if num.contains('(') || num.contains('+') || num.contains('-') {
            check_bracket(&num)?
        } else {
            num.parse().map_err(|_| CalcError::InvalidInput)?
        };
        for i in 1..=num {
            res *= i;
        }
    } else if count == 2 {
        let num = input.replace("(", "").replace("!", "").replace(")", "");
        let num: i32 = if num.contains('(') || num.contains('+') || num.contains('-') {
            check_bracket(&num)?
        } else {
            num.parse().map_err(|_| CalcError::InvalidInput)?
        };

        for i in 1..=num {
            res *= i;
        }
        let temp = res;
        res = 1;
        for i in 1..=temp {
            res *= i;
        }
    } else {
        return Err(CalcError::InvalidInput);
    }

    Ok(res)
}

pub fn unary_func(input: &String) -> Res<i32> {
    let num = input.trim_matches('-').to_string();
    let num: i32 = if num.contains('(')
        || num.contains('+')
        || num.contains('-')
        || num.contains('*')
        || num.contains('/')
    {
        check_bracket(&num)?
    } else {
        num.parse().map_err(|_| CalcError::InvalidInput)?
    };
    Ok(-num)
}

pub fn abs_func(input: &String) -> Res<i32> {
    let num = input.trim_matches('|').to_string();
    let num: i32 = if num.contains('(')
        || num.contains('+')
        || num.contains('-')
        || num.contains('*')
        || num.contains('/')
    {
        check_bracket(&num)?
    } else {
        num.parse().map_err(|_| CalcError::InvalidInput)?
    };
    Ok(num.abs())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_number_one() {
        assert_eq!(check_bracket(&"12+(3*1)*2".to_string()).unwrap(), 18);
    }

    #[test]
    fn test_number_two() {
        assert_eq!(check_bracket(&"(2+3)*5-25".to_string()).unwrap(), 0);
    }

    #[test]

    fn test_number_three() {
        assert_eq!(check_bracket(&"2*(2*(2*2))".to_string()).unwrap(), 16);
    }

    #[test]

    fn test_number_four() {
        assert_eq!(
            check_bracket(&"200+200+100*(100+100)-(100-90)".to_string()).unwrap(),
            20390
        );
    }

    #[test]

    fn test_number_five() {
        assert_eq!(
            check_bracket(
                &"123+387-321*(322-2)/313+1000-23*23-(3211)/3+3000-1*423+(3*(3*3))-1532/2+4*(231)"
                    .to_string()
            )
            .unwrap(),
            2345
        );
    }
    #[test]

    fn test_power_one() {
        assert_eq!(check_bracket(&"pow(2,2)".to_string()).unwrap(), 4);
    }

    #[test]

    fn test_power_two() {
        assert_eq!(check_bracket(&"pow(9,5)".to_string()).unwrap(), 59049);
    }

    #[test]

    fn test_sqrt_one() {
        assert_eq!(check_bracket(&"sqrt(16)".to_string()).unwrap(), 4);
    }

    #[test]

    fn test_sqrt_two() {
        assert_eq!(check_bracket(&"sqrt(100)".to_string()).unwrap(), 10);
    }
    #[test]

    fn test_sin_one() {
        assert_eq!(check_bracket(&"sin(0)".to_string()).unwrap(), 0);
    }

    #[test]

    fn test_sin_two() {
        assert_eq!(check_bracket(&"sin(90)".to_string()).unwrap(), 1);
    }

    #[test]

    fn test_cos_one() {
        assert_eq!(check_bracket(&"cos(0)".to_string()).unwrap(), 1);
    }

    #[test]

    fn test_cos_two() {
        assert_eq!(check_bracket(&"cos(90)".to_string()).unwrap(), 0);
    }

    #[test]

    fn test_fac_one() {
        assert_eq!(check_bracket(&"(5)!".to_string()).unwrap(), 120);
    }

    #[test]

    fn test_fac_two() {
        assert_eq!(check_bracket(&"(2+1)!!".to_string()).unwrap(), 720);
    }

    #[test]

    fn test_fac_three() {
        assert_eq!(check_bracket(&"(2+3)!".to_string()).unwrap(), 120);
    }

    #[test]

    fn test_fac_four() {
        assert_eq!(check_bracket(&"(3)!!".to_string()).unwrap(), 720);
    }
    #[test]

    fn test_unary_one() {
        assert_eq!(check_bracket(&"-5".to_string()).unwrap(), -5);
    }

    #[test]

    fn test_unary_two() {
        assert_eq!(check_bracket(&"-(3+2)".to_string()).unwrap(), -5);
    }

    #[test]

    fn test_unary_three() {
        assert_eq!(check_bracket(&"-(2*(3+2))".to_string()).unwrap(), -10);
    }

    #[test]

    fn test_abs_one() {
        assert_eq!(check_bracket(&"|-5|".to_string()).unwrap(), 5);
    }

    #[test]

    fn test_abs_two() {
        assert_eq!(check_bracket(&"|2-5|".to_string()).unwrap(), 3);
    }

    #[test]

    fn test_remove_spaces_one() {
        assert_eq!(check_bracket(&"12+3*4 #comment".to_string()).unwrap(), 24);
    }

    #[test]

    fn test_remove_spaces_two() {
        assert_eq!(
            check_bracket(&"12 + 3 * 4 #вираз\n+1".to_string()).unwrap(),
            25
        );
    }
}