inkling 0.12.5

Limited implementation of the Ink markup language.
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
//! Expressions of numerical work or string concatenation of variables.

use crate::{
    error::{
        parse::validate::{ExpressionKind, InvalidVariableExpression, ValidationError},
        utils::MetaData,
        InklingError,
    },
    follow::FollowData,
    knot::Address,
    line::Variable,
    story::validate::{ValidateContent, ValidationData},
};

#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))]
/// Single mathematical expression.
///
/// Consists of a head operand after which pairs of operators and operands appear.
/// In an expression `a + b + c`, `a` will be the head operand, with `+ b` and `+ c`
/// forming the tail.
pub struct Expression {
    /// Head term of expression.
    pub head: Operand,
    /// Tail terms of expression along with the operators operating on them.
    pub tail: Vec<(Operator, Operand)>,
}

impl Expression {
    pub fn add(&mut self, variable: Variable) {
        self.tail.push((Operator::Add, Operand::Variable(variable)));
    }

    pub fn sub(&mut self, variable: Variable) {
        self.tail
            .push((Operator::Subtract, Operand::Variable(variable)));
    }
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))]
/// Operand of an operation.
pub enum Operand {
    /// Nested inner expression from a parenthesis.
    Nested(Box<Expression>),
    /// Variable with a value.
    Variable(Variable),
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Deserialize, Serialize))]
/// Mathematical operator applied to a term.
///
/// In strings these operators are assigned to values on the right of them.
pub enum Operator {
    Add,
    Subtract,
    Multiply,
    Divide,
    Remainder,
}

/// Evaluate an expression from start to finish, producing a single `Variable` value.
pub fn evaluate_expression(
    expression: &Expression,
    data: &FollowData,
) -> Result<Variable, InklingError> {
    expression
        .tail
        .iter()
        .map(|(operation, operand)| (operation, get_value(operand, data)))
        .fold(
            get_value(&expression.head, data),
            |acc, (operation, operand)| {
                let lhs_variable = acc?;
                let rhs_variable = operand?;

                match operation {
                    Operator::Add => lhs_variable.add(&rhs_variable),
                    Operator::Subtract => lhs_variable.subtract(&rhs_variable),
                    Operator::Multiply => lhs_variable.multiply(&rhs_variable),
                    Operator::Divide => lhs_variable.divide(&rhs_variable),
                    Operator::Remainder => lhs_variable.remainder(&rhs_variable),
                }
                .map_err(|err| err.into())
            },
        )
}

/// Nest inner operations based on order of precedence in operations.
///
/// Subitems which multiply, divide or take the remainder with their next item are grouped
/// in `Operand::Nested` containers as single units, to be evaluated as a whole before
/// addition and subtraction.
///
/// This operation corresponds to inserting parenthesis around these groups.
///
/// # Notes
/// *   This function does *not* recurse into nested expressions to apply order of operations
///     for them. This has to be done separately, as those items are created.
pub fn apply_order_of_operations(expression: &Expression) -> Expression {
    split_expression_into_groups_of_same_precedence(expression)
        .into_iter()
        .map(|group| get_maybe_nested_operand_from_group(group))
        .collect::<Vec<_>>()
        .split_first()
        .map(|((_, head), tail)| Expression {
            head: head.clone(),
            tail: tail.to_vec(),
        })
        .unwrap()
}

/// Evaluate a variable or inner expression to produce a single variable.
fn get_value(operand: &Operand, data: &FollowData) -> Result<Variable, InklingError> {
    match operand {
        Operand::Nested(expression) => evaluate_expression(expression, data),
        Operand::Variable(variable) => variable.as_value(data),
    }
}

/// Split the expression items into groups, divided by addition and subtraction.
///
/// This groups multiplied, divided with and remainder or items, while added and subtracted
/// items remain alone.
fn split_expression_into_groups_of_same_precedence(
    expression: &Expression,
) -> Vec<Vec<(Operator, Operand)>> {
    let mut items = vec![(Operator::Add, expression.head.clone())];
    items.extend_from_slice(&expression.tail);

    let mut groups = Vec::new();
    let mut group = Vec::new();

    for (operation, operand) in items {
        match operation {
            Operator::Add | Operator::Subtract if !group.is_empty() => {
                groups.push(group);
                group = Vec::new();
            }
            _ => (),
        }

        group.push((operation, operand));
    }

    if !group.is_empty() {
        groups.push(group);
    }

    groups
}

/// Create `Variable` or `Nested` variants from a group.
///
/// If the group contains a single item it will be returned as an `Operand::Variable` object.
/// If not, a `Operand::Nested` object is constructed from all items.
fn get_maybe_nested_operand_from_group(group: Vec<(Operator, Operand)>) -> (Operator, Operand) {
    if group.len() == 1 {
        group[0].clone()
    } else {
        group
            .split_first()
            .map(|((operation, head), tail)| {
                let expression = Expression {
                    head: head.clone(),
                    tail: tail.to_vec(),
                };

                (*operation, Operand::Nested(Box::new(expression)))
            })
            .unwrap()
    }
}

impl ValidateContent for Expression {
    fn validate(
        &mut self,
        error: &mut ValidationError,
        current_location: &Address,
        meta_data: &MetaData,
        data: &ValidationData,
    ) {
        let num_errors = error.num_errors();

        self.head.validate(error, current_location, meta_data, data);

        self.tail
            .iter_mut()
            .for_each(|(_, operand)| operand.validate(error, current_location, meta_data, data));

        if num_errors == error.num_errors() {
            if let Err(err) = evaluate_expression(self, &data.follow_data) {
                error.variable_errors.push(InvalidVariableExpression {
                    expression_kind: ExpressionKind::Expression,
                    kind: err.into(),
                    meta_data: meta_data.clone(),
                });
            }
        }
    }
}

impl ValidateContent for Operand {
    fn validate(
        &mut self,
        error: &mut ValidationError,
        current_location: &Address,
        meta_data: &MetaData,
        data: &ValidationData,
    ) {
        match self {
            Operand::Nested(ref mut expression) => {
                expression.validate(error, current_location, meta_data, data)
            }
            Operand::Variable(ref mut variable) => {
                variable.validate(error, current_location, meta_data, data)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::{knot::Address, story::types::VariableInfo};

    use std::collections::HashMap;

    impl From<Variable> for Expression {
        fn from(variable: Variable) -> Self {
            Expression {
                head: Operand::Variable(variable),
                tail: Vec::new(),
            }
        }
    }

    fn get_simple_expression(head: Variable, tail: &[(Operator, Variable)]) -> Expression {
        let tail = tail
            .iter()
            .cloned()
            .map(|(operation, operand)| (operation, Operand::Variable(operand)))
            .collect();

        Expression {
            head: Operand::Variable(head),
            tail,
        }
    }

    fn mock_follow_data(knots: &[(&str, &str, u32)], variables: &[(&str, Variable)]) -> FollowData {
        let mut knot_visit_counts = HashMap::new();

        for (knot, stitch, num_visited) in knots {
            let mut stitch_count = HashMap::new();
            stitch_count.insert(stitch.to_string(), *num_visited);

            knot_visit_counts.insert(knot.to_string(), stitch_count);
        }

        let variables = variables
            .into_iter()
            .cloned()
            .enumerate()
            .map(|(i, (name, var))| (name, VariableInfo::new(var, i)))
            .map(|(name, var)| (name.to_string(), var))
            .collect();

        FollowData {
            knot_visit_counts,
            variables,
        }
    }

    #[test]
    fn expression_can_add_and_subtract_values_with_methods() {
        let mut expression = get_simple_expression(Variable::Int(5), &[]);

        expression.add(Variable::Int(2));
        expression.sub(Variable::Int(1));

        assert_eq!(
            expression.tail[0],
            (Operator::Add, Operand::Variable(Variable::Int(2)))
        );
        assert_eq!(
            expression.tail[1],
            (Operator::Subtract, Operand::Variable(Variable::Int(1)))
        );
    }

    #[test]
    fn expression_with_just_head_evaluates_to_head() {
        let data = mock_follow_data(&[], &[]);
        let expression = get_simple_expression(Variable::Int(5), &[]);

        assert_eq!(
            evaluate_expression(&expression, &data).unwrap(),
            Variable::Int(5)
        );
    }

    #[test]
    fn adding_two_variables_creates_summed_variable() {
        let data = mock_follow_data(&[], &[]);

        let expression =
            get_simple_expression(Variable::Int(1), &[(Operator::Add, Variable::Int(2))]);

        assert_eq!(
            evaluate_expression(&expression, &data).unwrap(),
            Variable::Int(3)
        );
    }

    #[test]
    fn all_operations_work_in_order() {
        let data = mock_follow_data(&[], &[]);

        // 1 + 2 - (-2) * (-3) / 5 = -3
        let expression = get_simple_expression(
            Variable::Float(1.0),
            &[
                (Operator::Add, Variable::Float(2.0)),
                (Operator::Subtract, Variable::Float(-2.0)),
                (Operator::Multiply, Variable::Float(-3.0)),
                (Operator::Divide, Variable::Float(5.0)),
            ],
        );

        assert_eq!(
            evaluate_expression(&expression, &data).unwrap(),
            Variable::Float(-3.0)
        );
    }

    #[test]
    fn get_value_evaluates_variables_by_following_addresses_if_necessary() {
        let data = mock_follow_data(&[], &[("counter", 1.into())]);

        let variable = Variable::Address(Address::variable_unchecked("counter"));

        assert_eq!(
            get_value(&Operand::Variable(variable), &data).unwrap(),
            Variable::Int(1)
        );
    }

    #[test]
    fn nested_expression_evaluates_into_variable() {
        let data = mock_follow_data(&[], &[]);

        let nested_expression = get_simple_expression(
            Variable::Int(1),
            &[(Operator::Multiply, Variable::Float(3.9))],
        );

        let nested = Operand::Nested(Box::new(nested_expression.clone()));

        assert_eq!(
            evaluate_expression(&nested_expression, &data).unwrap(),
            get_value(&nested, &data).unwrap()
        );
    }

    #[test]
    fn order_of_operations_on_expression_with_just_head_is_head() {
        let expression = get_simple_expression(Variable::Int(5), &[]);
        assert_eq!(apply_order_of_operations(&expression), expression);
    }

    #[test]
    fn order_of_operations_with_just_add_and_subtract_changes_nothing() {
        // 1 + 1 - 2 - 3 + 4 = 1 + 1 - 2 - 3 + 4
        let expression = get_simple_expression(
            1.into(),
            &[
                (Operator::Add, 1.into()),
                (Operator::Subtract, 2.into()),
                (Operator::Subtract, 3.into()),
                (Operator::Add, 4.into()),
            ],
        );

        let ooo_expression = apply_order_of_operations(&expression);

        assert_eq!(ooo_expression, expression);
    }

    #[test]
    fn order_of_operations_gathers_multiplied_items_into_nested_groups() {
        // 1 + 1 * 2 * 3 = 1 + (1 * 2 * 3)
        let expression = get_simple_expression(
            1.into(),
            &[
                (Operator::Add, 1.into()),
                (Operator::Multiply, 2.into()),
                (Operator::Multiply, 3.into()),
            ],
        );

        let nested_expression = get_simple_expression(
            1.into(),
            &[
                (Operator::Multiply, 2.into()),
                (Operator::Multiply, 3.into()),
            ],
        );

        let nested = Operand::Nested(Box::new(nested_expression));

        let ooo_expression = apply_order_of_operations(&expression);

        assert_eq!(ooo_expression.tail[0], (Operator::Add, nested));
    }

    #[test]
    fn multiple_nested_groups_are_separated_by_addition_or_subtraction() {
        // 1 + 1 * 2 + 3 * 4 = 1 + (1 * 2) + (3 * 4)
        let expression = get_simple_expression(
            1.into(),
            &[
                (Operator::Add, 1.into()),
                (Operator::Multiply, 2.into()),
                (Operator::Add, 3.into()),
                (Operator::Multiply, 4.into()),
            ],
        );

        let nested_expression_one =
            get_simple_expression(1.into(), &[(Operator::Multiply, 2.into())]);

        let nested_expression_two =
            get_simple_expression(3.into(), &[(Operator::Multiply, 4.into())]);

        let nested_one = Operand::Nested(Box::new(nested_expression_one));
        let nested_two = Operand::Nested(Box::new(nested_expression_two));

        let ooo_expression = apply_order_of_operations(&expression);

        assert_eq!(ooo_expression.tail[0], (Operator::Add, nested_one));
        assert_eq!(ooo_expression.tail[1], (Operator::Add, nested_two));
    }

    #[test]
    fn if_all_items_are_multiply_they_gather_into_one_item_in_head() {
        // 1 * 2 * 3 = (1 * 2 * 3)
        let expression = get_simple_expression(
            1.into(),
            &[
                (Operator::Multiply, 1.into()),
                (Operator::Multiply, 2.into()),
            ],
        );

        let nested = Operand::Nested(Box::new(expression.clone()));

        let ooo_expression = apply_order_of_operations(&expression);

        assert_eq!(ooo_expression.head, nested);
    }
}