csvpp 0.4.1

Compile csvpp source code to a target spreadsheet format
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
//! # Template
//!
//! A `template` holds the final compiled state for a single csv++ source file, as well as managing
//! evaluation and scope resolution.
//!
// TODO:
// * we need more unit tests around the various eval phases
//      - expands
//      - row vs cell variable definitions
// * eval cells in parallel (rayon)
// * make sure there is only one infinite expand in the docs (ones can follow it, but they have to
//      be finite and subtract from it
use crate::ast::{
    Ast, AstReferences, BuiltinFunction, BuiltinVariable, Functions, Node, VariableValue, Variables,
};
use crate::error::{EvalError, EvalResult};
use crate::parser::code_section_parser::{CodeSection, CodeSectionParser};
use crate::{Cell, Result, Row, Runtime, Spreadsheet};
use a1_notation::{Address, A1};
use std::cell;
use std::collections;

mod display;
mod template_at_rest;

#[derive(Debug)]
pub struct Template<'a> {
    pub functions: Functions,
    pub spreadsheet: cell::RefCell<Spreadsheet>,
    pub variables: Variables,
    csv_line_number: usize,
    runtime: &'a Runtime,
}

impl<'a> Template<'a> {
    pub fn compile(runtime: &'a Runtime) -> Result<Self> {
        let spreadsheet = Spreadsheet::parse(runtime)?;

        let code_section = if let Some(code_section_source) = &runtime.source_code.code_section {
            Some(CodeSectionParser::parse(code_section_source, runtime)?)
        } else {
            None
        };

        let compiled_template = Self::new(spreadsheet, code_section, runtime)
            .eval()
            .map_err(|e| runtime.source_code.eval_error(&e.message, e.position))?;

        runtime.info(&compiled_template);

        Ok(compiled_template)
    }

    /// Given a parsed code section and spreadsheet section, this function will assemble all of the
    /// available functions and variables.  There are some nuances here because there are a lot of
    /// sources of functions and variables and they're allowed to override each other.
    ///
    /// ## Function Precedence
    ///
    /// Functions are just comprised of what is builtin and what the user puts in the code section.
    /// The code section functions can override builtins so the precedence is (with the lowest
    /// number being the one that is used):
    ///
    /// 1. Functions in the code section
    /// 2. Builtin functions
    ///
    /// ## Variable Precedence
    ///
    /// There are a lot more sources of variables - here is their order of precedence:
    ///
    /// 1. Variables from the -k/--key-values CLI flag
    /// 2. Variables defined in cells
    /// 3. Variables defined in the code section
    /// 4. Builtin variables
    ///
    pub fn new(
        spreadsheet: Spreadsheet,
        code_section: Option<CodeSection>,
        runtime: &'a Runtime,
    ) -> Self {
        let cli_vars = &runtime.options.key_values;
        let spreadsheet_vars = spreadsheet.variables();
        let (code_section_vars, code_section_fns) = if let Some(cs) = code_section {
            (cs.variables, cs.functions)
        } else {
            (collections::HashMap::new(), collections::HashMap::new())
        };

        Self {
            runtime,
            csv_line_number: runtime.source_code.length_of_code_section + 1,
            spreadsheet: cell::RefCell::new(spreadsheet),
            functions: code_section_fns,
            variables: code_section_vars
                .into_iter()
                .chain(spreadsheet_vars)
                .chain(cli_vars.clone())
                .collect(),
        }
    }

    fn eval(self) -> EvalResult<Self> {
        self.runtime.progress("Evaluating all cells");
        self.eval_expands().eval_cells()
    }

    /// For each row of the spreadsheet, if it has an [[expand=]] modifier then we need to actually
    /// expand it to that many rows.  
    ///
    /// This has to happen before eval()ing the cells because that process depends on them being in
    /// their final location.
    // TODO: make sure there is only one infinite expand
    fn eval_expands(self) -> Self {
        let mut new_spreadsheet = Spreadsheet::default();
        let s = self.spreadsheet.borrow_mut();
        let mut row_num = 0;

        for row in s.rows.iter() {
            if let Some(e) = row.modifier.expand {
                for _ in 0..e.expand_amount(row_num) {
                    new_spreadsheet.rows.push(row.clone_to_row(row_num.into()));
                    row_num += 1;
                }
            } else {
                new_spreadsheet.rows.push(row.clone_to_row(row_num.into()));
                row_num += 1;
            }
        }

        Self {
            spreadsheet: cell::RefCell::new(new_spreadsheet),
            ..self
        }
    }

    // TODO:
    // * do this in parallel (thread for each cell)
    fn eval_cells(&self) -> EvalResult<Self> {
        let spreadsheet = self.spreadsheet.borrow();

        let mut evaled_rows = vec![];
        for row in spreadsheet.rows.iter() {
            evaled_rows.push(self.eval_row(row)?);
        }

        Ok(Self {
            csv_line_number: self.csv_line_number,
            functions: self.functions.clone(),
            runtime: self.runtime,
            spreadsheet: cell::RefCell::new(Spreadsheet { rows: evaled_rows }),
            variables: self.variables.clone(),
        })
    }

    /// The idea here is just to keep looping as long as we are making progress eval()ing.
    /// Progress being defined as `.extract_references()` returning the same result twice in a row
    fn eval_ast(&self, ast: &Ast, position: Address) -> EvalResult<Ast> {
        let mut evaled_ast = *ast.clone();
        let mut last_round_refs = AstReferences::default();

        loop {
            let refs = evaled_ast.extract_references(self);
            if refs.is_empty() || refs == last_round_refs {
                break;
            }
            last_round_refs = refs.clone();

            evaled_ast = evaled_ast
                .eval_variables(self.resolve_variables(&refs.variables, position)?)?
                .eval_functions(&refs.functions, |fn_id, args| {
                    if let Some(function) = self.functions.get(fn_id) {
                        Ok(function.clone())
                    } else if let Some(BuiltinFunction { eval, .. }) =
                        self.runtime.builtin_functions.get(fn_id)
                    {
                        Ok(Box::new(eval(position, args)?))
                    } else {
                        Err(EvalError::new(position, "Undefined function: {fn_id}"))
                    }
                })?;
        }

        Ok(Box::new(evaled_ast))
    }

    fn eval_row(&self, row: &Row) -> EvalResult<Row> {
        let mut cells = vec![];

        for cell in row.cells.iter() {
            let evaled_ast = if let Some(ast) = &cell.ast {
                Some(self.eval_ast(ast, cell.position)?)
            } else {
                None
            };

            cells.push(Cell {
                ast: evaled_ast,
                position: cell.position,
                modifier: cell.modifier.clone(),
                value: cell.value.clone(),
            });
        }

        Ok(Row {
            cells,
            row: row.row,
            modifier: row.modifier.clone(),
        })
    }

    pub fn is_function_defined(&self, fn_name: &str) -> bool {
        self.functions.contains_key(fn_name) || self.runtime.builtin_functions.contains_key(fn_name)
    }

    pub fn is_variable_defined(&self, var_name: &str) -> bool {
        self.variables.contains_key(var_name)
            || self.runtime.builtin_variables.contains_key(var_name)
    }

    /// Variables can all be resolved in one go - we just loop them by name and resolve the ones
    /// that we can and leave the rest alone.
    fn resolve_variables(
        &self,
        var_names: &[String],
        position: Address,
    ) -> EvalResult<collections::HashMap<String, Ast>> {
        let mut resolved_vars = collections::HashMap::new();
        for var_name in var_names {
            if let Some(val) = self.resolve_variable(var_name, position)? {
                resolved_vars.insert(var_name.to_string(), val);
            }
        }

        Ok(resolved_vars)
    }

    fn resolve_variable(&self, var_name: &str, position: Address) -> EvalResult<Option<Ast>> {
        Ok(if let Some(value) = self.variables.get(var_name) {
            let value_from_var = match &**value {
                Node::Variable { value, .. } => {
                    match value {
                        // absolute value, just turn it into a Ast
                        VariableValue::Absolute(address) => (*address).into(),

                        // already an AST, just clone it
                        VariableValue::Ast(ast) => *ast.clone(),

                        // it's relative to an expand - so if it's referenced inside the
                        // expand, it's the value at that location.  If it's outside the expand
                        // it's the range that it represents
                        VariableValue::ColumnRelative { scope, column } => {
                            let scope_a1: A1 = (*scope).into();
                            if scope_a1.contains(&position.into()) {
                                position.with_x(column.x).into()
                            } else {
                                let row_range: A1 = (*scope).into();
                                row_range.with_x(column.x).into()
                            }
                        }

                        VariableValue::Row(row) => {
                            let a1: a1_notation::A1 = (*row).into();
                            a1.into()
                        }

                        VariableValue::RowRelative { scope, .. } => {
                            let scope_a1: A1 = (*scope).into();
                            if scope_a1.contains(&position.into()) {
                                // we're within the scope (expand) so it's the row we're on
                                let row_a1: A1 = position.row.into();
                                row_a1.into()
                            } else {
                                // we're outside the scope (expand), so it represents the entire
                                // range contained by it (the scope)
                                let row_range: A1 = (*scope).into();
                                row_range.into()
                            }
                        }
                    }
                }
                n => n.clone(),
            };

            Some(Box::new(value_from_var))
        } else if let Some(BuiltinVariable { eval, .. }) =
            self.runtime.builtin_variables.get(var_name)
        {
            Some(Box::new(eval(position)?))
        } else {
            None
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::TestFile;
    use std::cell;

    fn build_template(runtime: &Runtime) -> Template {
        Template {
            csv_line_number: 5,
            functions: collections::HashMap::new(),
            variables: collections::HashMap::new(),
            runtime,
            spreadsheet: cell::RefCell::new(Spreadsheet::default()),
        }
    }

    #[test]
    fn compile_empty() {
        let test_file = TestFile::new("csv", "");
        let runtime = test_file.into();
        let template = Template::compile(&runtime);

        assert!(template.is_ok());
    }

    #[test]
    fn compile_simple() {
        let test_file = TestFile::new("csv", "---\nfoo,bar,baz\n1,2,3");
        let runtime = test_file.into();
        let template = Template::compile(&runtime).unwrap();

        assert_eq!(template.spreadsheet.borrow().rows.len(), 2);
    }

    #[test]
    fn compile_with_expand_finite() {
        let test_file = TestFile::new("xlsx", "![[expand=10]]foo,bar,baz");
        let runtime = test_file.into();
        let template = Template::compile(&runtime).unwrap();

        assert_eq!(template.spreadsheet.borrow().rows.len(), 10);
    }

    #[test]
    fn compile_with_expand_infinite() {
        let test_file = TestFile::new("xlsx", "![[expand]]foo,bar,baz");
        let runtime = test_file.into();
        let template = Template::compile(&runtime).unwrap();

        assert_eq!(template.spreadsheet.borrow().rows.len(), 1000);
    }

    #[test]
    fn compile_with_expand_multiple() {
        let test_file = TestFile::new("xlsx", "![[e=10]]foo,bar,baz\n![[e]]1,2,3");
        let runtime = test_file.into();
        let template = Template::compile(&runtime).unwrap();

        assert_eq!(template.spreadsheet.borrow().rows.len(), 1000);
    }

    #[test]
    fn compile_with_expand_and_rows() {
        let test_file = TestFile::new("xlsx", "foo,bar,baz\n![[e=2]]foo,bar,baz\none,last,row\n");
        let runtime = test_file.into();
        let template = Template::compile(&runtime).unwrap();

        let spreadsheet = template.spreadsheet.borrow();
        assert_eq!(spreadsheet.rows[0].row, 0.into());
        assert_eq!(spreadsheet.rows[0].cells[0].position.row, 0.into());
        assert_eq!(spreadsheet.rows[1].row, 1.into());
        assert_eq!(spreadsheet.rows[1].cells[0].position.row, 1.into());
        assert_eq!(spreadsheet.rows[2].row, 2.into());
        assert_eq!(spreadsheet.rows[2].cells[0].position.row, 2.into());
        assert_eq!(spreadsheet.rows[3].row, 3.into());
        assert_eq!(spreadsheet.rows[3].cells[0].position.row, 3.into());
    }

    #[test]
    fn is_function_defined_true() {
        let test_file = TestFile::new("csv", "");
        let runtime = test_file.into();
        let mut template = build_template(&runtime);
        template
            .functions
            .insert("foo".to_string(), Box::new(42.into()));

        assert!(template.is_function_defined("foo"));
    }

    #[test]
    fn is_function_defined_builtin_true() {
        let test_file = TestFile::new("csv", "");
        let mut runtime: Runtime = test_file.into();
        runtime.builtin_functions.insert(
            "foo".to_string(),
            BuiltinFunction {
                name: "foo".to_owned(),
                eval: Box::new(|_a1, _args| Ok(42.into())),
            },
        );
        let template = build_template(&runtime);

        assert!(template.is_function_defined("foo"));
    }

    #[test]
    fn is_variable_defined_true() {
        let test_file = TestFile::new("csv", "");
        let runtime = test_file.into();
        let mut template = build_template(&runtime);
        template
            .variables
            .insert("foo".to_string(), Box::new(42.into()));

        assert!(template.is_variable_defined("foo"));
    }

    #[test]
    fn is_variable_defined_builtin_true() {
        let test_file = TestFile::new("csv", "");
        let mut runtime: Runtime = test_file.into();
        runtime.builtin_variables.insert(
            "foo".to_string(),
            BuiltinVariable {
                name: "foo".to_owned(),
                eval: Box::new(|_a1| Ok(42.into())),
            },
        );
        let template = build_template(&runtime);

        assert!(template.is_variable_defined("foo"));
    }

    #[test]
    fn new_with_code_section() {
        let test_file = TestFile::new("csv", "");
        let runtime = test_file.into();
        let mut functions = collections::HashMap::new();
        functions.insert("foo".to_string(), Box::new(1.into()));
        let mut variables = collections::HashMap::new();
        variables.insert("bar".to_string(), Box::new(2.into()));
        let code_section = CodeSection {
            functions,
            variables,
        };
        let template = Template::new(Spreadsheet::default(), Some(code_section), &runtime);

        assert!(template.functions.contains_key("foo"));
        assert!(template.variables.contains_key("bar"));
    }

    #[test]
    fn new_without_code_section() {
        let test_file = TestFile::new("csv", "");
        let runtime = test_file.into();
        let template = Template::new(Spreadsheet::default(), None, &runtime);

        assert!(template.functions.is_empty());
        assert!(template.variables.is_empty());
    }
}