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
use super::ast;
use crate::operands::PluralOperands;
pub fn test_condition(condition: &ast::Condition, operands: &PluralOperands) -> bool {
condition.0.is_empty() || condition.0.iter().any(|c| test_and_condition(c, operands))
}
fn test_and_condition(condition: &ast::AndCondition, operands: &PluralOperands) -> bool {
condition.0.iter().all(|r| test_relation(r, operands))
}
fn test_relation(relation: &ast::Relation, operands: &PluralOperands) -> bool {
calculate_expression(&relation.expression, operands).map_or(false, |exp| {
test_range(&relation.range_list, exp, relation.operator)
})
}
fn calculate_expression(expression: &ast::Expression, operands: &PluralOperands) -> Option<u64> {
let value = match expression.operand {
ast::Operand::N => {
if operands.w == 0 {
operands.i
} else {
return None;
}
}
ast::Operand::I => operands.i,
ast::Operand::F => operands.f,
ast::Operand::V => operands.v as u64,
ast::Operand::W => operands.w as u64,
ast::Operand::T => operands.t,
ast::Operand::C | ast::Operand::E => operands.c as u64,
};
if let Some(modulus) = &expression.modulus {
value.checked_rem_euclid(modulus.0)
} else {
Some(value)
}
}
fn test_range(range: &ast::RangeList, value: u64, operator: ast::Operator) -> bool {
let connect = match operator {
ast::Operator::Eq => Iterator::any,
ast::Operator::NotEq => Iterator::all,
};
connect(&mut range.0.iter(), |item| {
test_range_item(item, value, operator)
})
}
fn test_range_item(item: &ast::RangeListItem, value: u64, operator: ast::Operator) -> bool {
let value = match item {
ast::RangeListItem::Value(n) => n.0 == value,
ast::RangeListItem::Range(range) => range.contains(&ast::Value(value)),
};
match operator {
ast::Operator::Eq => value,
ast::Operator::NotEq => !value,
}
}