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
use arret_syntax::span::Span;

use arret_runtime::boxed;

use crate::mir::builder::Builder;
use crate::mir::eval_hir::EvalHirCtx;
use crate::mir::value::to_const::value_to_const;
use crate::mir::value::types::possible_type_tags_for_value;
use crate::mir::value::Value;

pub enum PartialPrint {
    Constant(String),
    SimplifiedArgs(Box<[Value]>),
}

impl PartialPrint {
    /// Converts the partial print to an arg list suitable for passing to a stdlib print function
    pub fn into_arg_list_value(self, ehx: &mut EvalHirCtx) -> Value {
        match self {
            PartialPrint::Constant(string) => {
                Value::List(Box::new([boxed::Str::new(ehx, &string).into()]), None)
            }
            PartialPrint::SimplifiedArgs(args) => Value::List(args, None),
        }
    }
}

/// Partially pretty prints an arg list value
///
/// If partial printing isn't possible or isn't an improvement over the original arg list, `None`
/// will be returned.
pub fn partial_pretty_print(
    ehx: &mut EvalHirCtx,
    b: &mut Builder,
    span: Span,
    arg_list_value: &Value,
) -> Option<PartialPrint> {
    let mut list_iter = arg_list_value.try_sized_list_iter()?;
    let original_arg_count = list_iter.len();

    if original_arg_count < 2 {
        // Nothing we can do to simplify this further
        return None;
    }

    // Accumulates our string literal
    let mut literal_acc = String::new();
    let mut simplified_args: Vec<Value> = vec![];

    while let Some(value) = list_iter.next(b, span) {
        // Check early for function values
        // This is to avoid doing a full JIT just to print `#fn` and to allow
        // printing non-constant function values.
        if possible_type_tags_for_value(&value) == boxed::TypeTag::FunThunk.into() {
            literal_acc.push_str("#fn");
            continue;
        }

        match value_to_const(ehx, &value) {
            Some(boxed) => {
                let mut output: Vec<u8> = vec![];
                arret_runtime_syntax::writer::pretty_print_boxed(&mut output, ehx, boxed);

                literal_acc.push_str(
                    std::str::from_utf8(&output)
                        .expect("pretty printed invalid UTF-8 during partial print"),
                );
            }
            None => {
                if !literal_acc.is_empty() {
                    simplified_args.push(boxed::Str::new(ehx, &literal_acc).into());
                    literal_acc.clear();
                }

                simplified_args.push(value);
            }
        };
    }

    if simplified_args.is_empty() {
        Some(PartialPrint::Constant(literal_acc))
    } else {
        // Push on the end of the accumulator
        if !literal_acc.is_empty() {
            simplified_args.push(boxed::Str::new(ehx, &literal_acc).into());
        }

        if simplified_args.len() < original_arg_count {
            Some(PartialPrint::SimplifiedArgs(
                simplified_args.into_boxed_slice(),
            ))
        } else {
            // Didn't improve anything
            None
        }
    }
}