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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// NOTE: the runtime function take a &[ConcreteExpr] instead of BoundArguments
// because they notoriously might want not to bind arguments in the first
// place (e.g. "if"/"unless").
use std::sync::Arc;

use simd_csv::ByteRecord;

use super::error::{ConcretizationError, EvaluationError, SpecifiedEvaluationError};
use super::interpreter::{ConcreteExpr, EvaluationContext};
use super::parser::FunctionCall;
use super::types::{
    Arity, ColumIndexationBy, DynamicValue, EvaluationResult, FunctionArguments, LambdaArguments,
};

pub type ComptimeFunctionResult = Result<Option<ConcreteExpr>, ConcretizationError>;
pub type ComptimeFunction = fn(&FunctionCall, &ByteRecord) -> ComptimeFunctionResult;
pub type RuntimeFunction = fn(&EvaluationContext, &[ConcreteExpr]) -> EvaluationResult;

#[derive(Debug, Clone, Copy)]
enum AbstractColReturnValue {
    Cell,
    Index,
    Header,
}

pub fn get_special_function(
    name: &str,
) -> Option<(
    Option<ComptimeFunction>,
    Option<RuntimeFunction>,
    FunctionArguments,
)> {
    macro_rules! higher_order_fn {
        ($name:expr, $variant:ident) => {
            (
                None,
                Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                    runtime_higher_order(context, args, $name, HigherOrderOperation::$variant)
                }),
                FunctionArguments::binary(),
            )
        };
    }

    Some(match name {
        // NOTE: col, cols and headers need a comptime version because static evaluation
        // is not an option for them. What's more they rely on the headers index which
        // is not available to normal functions.
        "col" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_col(false, AbstractColReturnValue::Cell, call, headers)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_col(false, AbstractColReturnValue::Cell, context, args)
            }),
            FunctionArguments::with_range(1..=2),
        ),
        "header" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_col(false, AbstractColReturnValue::Header, call, headers)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_col(false, AbstractColReturnValue::Header, context, args)
            }),
            FunctionArguments::with_range(1..=2),
        ),
        "col_index" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_col(false, AbstractColReturnValue::Index, call, headers)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_col(false, AbstractColReturnValue::Index, context, args)
            }),
            FunctionArguments::with_range(1..=2),
        ),
        "col?" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_col(true, AbstractColReturnValue::Cell, call, headers)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_col(true, AbstractColReturnValue::Cell, context, args)
            }),
            FunctionArguments::with_range(1..=2),
        ),
        "header?" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_col(true, AbstractColReturnValue::Header, call, headers)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_col(true, AbstractColReturnValue::Header, context, args)
            }),
            FunctionArguments::with_range(1..=2),
        ),
        "col_index?" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_col(true, AbstractColReturnValue::Index, call, headers)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_col(true, AbstractColReturnValue::Index, context, args)
            }),
            FunctionArguments::with_range(1..=2),
        ),
        "cols" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_cols(call, headers, ConcreteExpr::Column)
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_cols(context, args, |i| DynamicValue::from(&context.record[i]))
            }),
            FunctionArguments::with_range(0..=2),
        ),
        "headers" => (
            Some(|call: &FunctionCall, headers: &ByteRecord| {
                abstract_comptime_cols(call, headers, |i| {
                    ConcreteExpr::Value(DynamicValue::from(&headers[i]))
                })
            }),
            Some(|context: &EvaluationContext, args: &[ConcreteExpr]| {
                abstract_runtime_cols(context, args, |i| {
                    DynamicValue::from(context.headers_index.get_at(i))
                })
            }),
            FunctionArguments::with_range(0..=2),
        ),
        // NOTE: index needs to be a special function because it relies on external
        // data that cannot be accessed by normal functions.
        "index" => (None, Some(runtime_index), FunctionArguments::nullary()),

        // NOTE: if and unless need to be special functions because they short-circuit
        // underlying evaluation and circumvent the typical DFS evaluation scheme.
        // NOTE: if and unless don't require a comptime version because static evaluation
        // will work just fine here.
        "if" => (None, Some(runtime_if), FunctionArguments::with_range(2..=3)),
        "unless" => (
            None,
            Some(runtime_unless),
            FunctionArguments::with_range(2..=3),
        ),
        "and" => (None, Some(runtime_and), FunctionArguments::variadic(2)),
        "or" => (None, Some(runtime_or), FunctionArguments::variadic(2)),

        // NOTE: try is special because you need to suppress the error if any
        "try" => (None, Some(runtime_try), FunctionArguments::unary()),

        // NOTE: warn must know row index
        "warn" => (None, Some(runtime_warn), FunctionArguments::unary()),

        // NOTE: lambda evaluation need to be a special function because, like
        // if and unless, they cannot work in DFS fashion unless you
        // bind some values ahead of time.
        // NOTE: higher-order functions work fine with static evaluation
        "map" => higher_order_fn!("map", Map),
        "filter" => higher_order_fn!("filter", Filter),
        "find" => higher_order_fn!("find", Find),
        "find_index" => higher_order_fn!("find_index", FindIndex),
        "all" => higher_order_fn!("all", All),
        "any" => higher_order_fn!("any", Any),

        _ => return None,
    })
}

fn abstract_comptime_col(
    unsure: bool,
    return_value: AbstractColReturnValue,
    call: &FunctionCall,
    headers: &ByteRecord,
) -> ComptimeFunctionResult {
    if let Some(column_indexation) = ColumIndexationBy::from_arguments(&call.raw_args_as_ref()) {
        match column_indexation.find_column_index(headers) {
            Some(index) => {
                return Ok(Some(match return_value {
                    AbstractColReturnValue::Cell => ConcreteExpr::Column(index),
                    AbstractColReturnValue::Index => ConcreteExpr::Value(DynamicValue::from(index)),
                    AbstractColReturnValue::Header => {
                        ConcreteExpr::Value(DynamicValue::from(&headers[index]))
                    }
                }))
            }
            None => {
                return if unsure {
                    Ok(Some(ConcreteExpr::Value(DynamicValue::None)))
                } else {
                    Err(ConcretizationError::ColumnNotFound(column_indexation))
                }
            }
        };
    }

    Ok(None)
}

fn abstract_comptime_cols<F>(
    call: &FunctionCall,
    headers: &ByteRecord,
    map: F,
) -> ComptimeFunctionResult
where
    F: Fn(usize) -> ConcreteExpr,
{
    if call.args.is_empty() {
        return Ok(Some(ConcreteExpr::List(
            (0..headers.len()).map(map).collect(),
        )));
    }

    match ColumIndexationBy::from_argument(&call.args[0].1) {
        None => Ok(None),
        Some(first_column_indexation) => match first_column_indexation.find_column_index(headers) {
            Some(first_index) => {
                if call.args.len() < 2 {
                    Ok(Some(ConcreteExpr::List(
                        (first_index..headers.len()).map(map).collect(),
                    )))
                } else {
                    match ColumIndexationBy::from_argument(&call.args[1].1) {
                        None => Ok(None),
                        Some(second_column_indexation) => {
                            match second_column_indexation.find_column_index(headers) {
                                Some(second_index) => {
                                    let range: Vec<_> = if first_index > second_index {
                                        (second_index..=first_index).map(map).rev().collect()
                                    } else {
                                        (first_index..=second_index).map(map).collect()
                                    };

                                    Ok(Some(ConcreteExpr::List(range)))
                                }
                                None => Err(ConcretizationError::ColumnNotFound(
                                    second_column_indexation,
                                )),
                            }
                        }
                    }
                }
            }
            None => Err(ConcretizationError::ColumnNotFound(first_column_indexation)),
        },
    }
}

fn runtime_if(context: &EvaluationContext, args: &[ConcreteExpr]) -> EvaluationResult {
    let arity = args.len();

    let condition = &args[0];
    let result = condition.evaluate(context)?;

    let mut branch: Option<&ConcreteExpr> = None;

    if result.is_truthy() {
        branch = Some(&args[1]);
    } else if arity == 3 {
        branch = Some(&args[2]);
    }

    match branch {
        None => Ok(DynamicValue::None),
        Some(arg) => arg.evaluate(context),
    }
}

fn runtime_unless(context: &EvaluationContext, args: &[ConcreteExpr]) -> EvaluationResult {
    let arity = args.len();

    let condition = &args[0];
    let result = condition.evaluate(context)?;

    let mut branch: Option<&ConcreteExpr> = None;

    if result.is_falsey() {
        branch = Some(&args[1]);
    } else if arity == 3 {
        branch = Some(&args[2]);
    }

    match branch {
        None => Ok(DynamicValue::None),
        Some(arg) => arg.evaluate(context),
    }
}

fn runtime_or(context: &EvaluationContext, args: &[ConcreteExpr]) -> EvaluationResult {
    debug_assert!(args.len() >= 2);

    let mut last: Option<DynamicValue> = None;

    for arg in args {
        let value = arg.evaluate(context)?;

        if value.is_truthy() {
            return Ok(value);
        }

        last.replace(value);
    }

    Ok(last.unwrap())
}

fn runtime_and(context: &EvaluationContext, args: &[ConcreteExpr]) -> EvaluationResult {
    debug_assert!(args.len() >= 2);

    let mut last: Option<DynamicValue> = None;

    for arg in args {
        let value = arg.evaluate(context)?;

        if value.is_falsey() {
            return Ok(value);
        }

        last.replace(value);
    }

    Ok(last.unwrap())
}

fn runtime_index(context: &EvaluationContext, _args: &[ConcreteExpr]) -> EvaluationResult {
    Ok(match context.index {
        None => DynamicValue::None,
        Some(index) => DynamicValue::from(index),
    })
}

fn abstract_runtime_col(
    unsure: bool,
    return_value: AbstractColReturnValue,
    context: &EvaluationContext,
    args: &[ConcreteExpr],
) -> EvaluationResult {
    let name_or_pos = args.first().unwrap().evaluate(context)?;

    let pos = match args.get(1) {
        Some(p) => Some(p.evaluate(context)?),
        None => None,
    };

    match ColumIndexationBy::from_bound_arguments(name_or_pos, pos) {
        None => Err(SpecifiedEvaluationError::new(
            "col",
            EvaluationError::Custom("invalid arguments".to_string()),
        )),
        Some(indexation) => match context.headers_index.get(&indexation) {
            None => {
                if unsure {
                    Ok(DynamicValue::None)
                } else {
                    Err(SpecifiedEvaluationError::new(
                        "col",
                        EvaluationError::ColumnNotFound(indexation),
                    ))
                }
            }
            Some(index) => Ok(match return_value {
                AbstractColReturnValue::Index => DynamicValue::from(index),
                AbstractColReturnValue::Cell => DynamicValue::from(&context.record[index]),
                AbstractColReturnValue::Header => {
                    DynamicValue::from(context.headers_index.get_at(index))
                }
            }),
        },
    }
}

fn abstract_runtime_cols<F>(
    context: &EvaluationContext,
    args: &[ConcreteExpr],
    map: F,
) -> EvaluationResult
where
    F: Fn(usize) -> DynamicValue,
{
    // NOTE: 0 is not reachable because it can be resolved at comptime by definition
    match args.len() {
        1 => {
            let start_index_arg = args.first().unwrap().evaluate(context)?;

            match ColumIndexationBy::from_bound_arguments(start_index_arg, None) {
                None => Err(SpecifiedEvaluationError::new(
                    "col",
                    EvaluationError::Custom("invalid arguments".to_string()),
                )),
                Some(indexation) => match context.headers_index.get(&indexation) {
                    None => Err(SpecifiedEvaluationError::new(
                        "col",
                        EvaluationError::ColumnNotFound(indexation),
                    )),
                    Some(index) => Ok(DynamicValue::from(
                        (index..context.headers_index.len())
                            .map(map)
                            .collect::<Vec<_>>(),
                    )),
                },
            }
        }
        2 => {
            let start_index_arg = args.first().unwrap().evaluate(context)?;

            match ColumIndexationBy::from_bound_arguments(start_index_arg, None) {
                None => Err(SpecifiedEvaluationError::new(
                    "col",
                    EvaluationError::Custom("invalid arguments".to_string()),
                )),
                Some(start_indexation) => match context.headers_index.get(&start_indexation) {
                    None => Err(SpecifiedEvaluationError::new(
                        "col",
                        EvaluationError::ColumnNotFound(start_indexation),
                    )),
                    Some(start_index) => {
                        let end_index_arg = args.last().unwrap().evaluate(context)?;

                        match ColumIndexationBy::from_bound_arguments(end_index_arg, None) {
                            None => Err(SpecifiedEvaluationError::new(
                                "col",
                                EvaluationError::Custom("invalid arguments".to_string()),
                            )),
                            Some(end_indexation) => {
                                match context.headers_index.get(&end_indexation) {
                                    None => Err(SpecifiedEvaluationError::new(
                                        "col",
                                        EvaluationError::ColumnNotFound(end_indexation),
                                    )),
                                    Some(end_index) => {
                                        let range: Vec<_> = if start_index > end_index {
                                            (end_index..=start_index).map(map).rev().collect()
                                        } else {
                                            (start_index..=end_index).map(map).collect()
                                        };

                                        Ok(DynamicValue::from(range))
                                    }
                                }
                            }
                        }
                    }
                },
            }
        }
        _ => unreachable!(),
    }
}

fn runtime_try(context: &EvaluationContext, args: &[ConcreteExpr]) -> EvaluationResult {
    let result = args.first().unwrap().evaluate(context);

    Ok(result.unwrap_or(DynamicValue::None))
}

fn runtime_warn(context: &EvaluationContext, args: &[ConcreteExpr]) -> EvaluationResult {
    let msg_arg = args.first().unwrap().evaluate(context)?;
    let msg = msg_arg.try_as_str().map_err(|err| err.specify("warn"))?;

    match context.index {
        Some(i) => eprintln!("Row index {}: {}", i, msg),
        None => eprintln!("{}", msg),
    };

    Ok(DynamicValue::None)
}

#[derive(Clone, Copy)]
enum HigherOrderOperation {
    Filter,
    Map,
    Find,
    FindIndex,
    All,
    Any,
}

fn runtime_higher_order(
    context: &EvaluationContext,
    args: &[ConcreteExpr],
    name: &str,
    op: HigherOrderOperation,
) -> EvaluationResult {
    let list = args
        .first()
        .unwrap()
        .evaluate(context)?
        .try_into_arc_list()
        .map_err(|err| err.specify(name))?;

    let (names, lambda) = args
        .get(1)
        .unwrap()
        .try_as_lambda()
        .map_err(|err| err.anonymous())?;

    // Validating arity
    Arity::Strict(1)
        .validate(names.len())
        .map_err(|invalid_arity| EvaluationError::InvalidArity(invalid_arity).anonymous())?;

    let arg_name = names.first().unwrap();

    let mut variables = match context.lambda_variables {
        None => LambdaArguments::new(),
        Some(v) => v.clone(),
    };

    let item_arg_index = variables.register(arg_name);

    match op {
        HigherOrderOperation::Map => {
            let mut new_list = Vec::with_capacity(list.len());

            match Arc::try_unwrap(list) {
                Ok(owned_list) => {
                    for item in owned_list {
                        variables.set(item_arg_index, item);

                        let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;
                        new_list.push(result);
                    }
                }
                Err(borrowed_list) => {
                    for item in borrowed_list.iter() {
                        variables.set(item_arg_index, item.clone());

                        let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;
                        new_list.push(result);
                    }
                }
            }

            Ok(DynamicValue::from(new_list))
        }
        HigherOrderOperation::Filter => {
            let mut new_list = Vec::new();

            for item in list.iter() {
                variables.set(item_arg_index, item.clone());

                let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;

                if result.is_truthy() {
                    new_list.push(item.clone());
                }
            }

            Ok(DynamicValue::from(new_list))
        }
        HigherOrderOperation::Find => {
            for item in list.iter() {
                variables.set(item_arg_index, item.clone());

                let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;

                if result.is_truthy() {
                    return Ok(item.clone());
                }
            }

            Ok(DynamicValue::None)
        }
        HigherOrderOperation::FindIndex => {
            for (i, item) in list.iter().enumerate() {
                variables.set(item_arg_index, item.clone());

                let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;

                if result.is_truthy() {
                    return Ok(DynamicValue::from(i));
                }
            }

            Ok(DynamicValue::None)
        }
        HigherOrderOperation::All => {
            for item in list.iter() {
                variables.set(item_arg_index, item.clone());

                let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;

                if result.is_falsey() {
                    return Ok(DynamicValue::Boolean(false));
                }
            }

            Ok(DynamicValue::Boolean(true))
        }
        HigherOrderOperation::Any => {
            for item in list.iter() {
                variables.set(item_arg_index, item.clone());

                let result = lambda.evaluate(&context.with_lambda_variables(&variables))?;

                if result.is_truthy() {
                    return Ok(DynamicValue::Boolean(true));
                }
            }

            Ok(DynamicValue::Boolean(false))
        }
    }
}