intuicio_frontend_simpleton/library/
debug.rs

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
use crate::{
    library::bytes::Bytes, Array, Boolean, Function, Integer, Map, Real, Reference, Text, Type,
};
use intuicio_core::registry::Registry;
use intuicio_derive::intuicio_function;
use std::fmt::Write;

#[intuicio_function(module_name = "debug")]
pub fn assert(status: Reference, message: Reference) -> Reference {
    if !*status.read::<Boolean>().unwrap() {
        panic!("{}", message.read::<Text>().unwrap().as_str());
    }
    Reference::null()
}

fn debug_indent(result: &mut dyn Write, indent: usize) {
    for _ in 0..(indent * 2) {
        result.write_char(' ').unwrap()
    }
}

fn debug_impl(value: &Reference, result: &mut dyn Write, indent: &mut Option<usize>) {
    if value.is_null() {
        write!(result, "null").unwrap();
    } else if let Some(value) = value.read::<Boolean>() {
        write!(result, "{}", *value).unwrap();
    } else if let Some(value) = value.read::<Integer>() {
        write!(result, "{}", *value).unwrap();
    } else if let Some(value) = value.read::<Real>() {
        write!(result, "{}", *value).unwrap();
    } else if let Some(value) = value.read::<Text>() {
        write!(result, r#""{}""#, value.as_str()).unwrap();
    } else if let Some(value) = value.read::<Bytes>() {
        write!(result, "#{:?}", value.get_ref()).unwrap();
    } else if let Some(value) = value.read::<Array>() {
        write!(result, "[").unwrap();
        if !value.is_empty() {
            if let Some(indent) = indent {
                *indent += 1;
                writeln!(result).unwrap();
                debug_indent(result, *indent);
            }
            for (index, value) in value.iter().enumerate() {
                if index > 0 {
                    write!(result, ", ").unwrap();
                    if let Some(indent) = indent {
                        writeln!(result).unwrap();
                        debug_indent(result, *indent);
                    }
                }
                debug_impl(value, result, indent);
            }
            if let Some(indent) = indent {
                *indent -= 1;
                writeln!(result).unwrap();
                debug_indent(result, *indent);
            }
        }
        write!(result, "]").unwrap();
    } else if let Some(value) = value.read::<Map>() {
        write!(result, "{{").unwrap();
        if !value.is_empty() {
            if let Some(indent) = indent {
                *indent += 1;
                writeln!(result).unwrap();
                debug_indent(result, *indent);
            }
            for (index, (key, value)) in value.iter().enumerate() {
                if index > 0 {
                    write!(result, ", ").unwrap();
                    if let Some(indent) = indent {
                        writeln!(result).unwrap();
                        debug_indent(result, *indent);
                    }
                }
                write!(result, "{}: ", key).unwrap();
                debug_impl(value, result, indent);
            }
            if let Some(indent) = indent {
                *indent -= 1;
                writeln!(result).unwrap();
                debug_indent(result, *indent);
            }
        }
        write!(result, "}}").unwrap();
    } else if let Some(value) = value.read::<Type>() {
        write!(
            result,
            "<{}::{}>",
            value.handle().unwrap().module_name().unwrap_or(""),
            value.handle().unwrap().name()
        )
        .unwrap();
    } else if let Some(value) = value.read::<Function>() {
        let signature = value.handle().unwrap().signature();
        write!(
            result,
            "<{}::{}(",
            signature.module_name.as_deref().unwrap_or(""),
            signature.name,
        )
        .unwrap();
        for (index, parameter) in signature.inputs.iter().enumerate() {
            if index > 0 {
                write!(result, ", ").unwrap();
            }
            write!(result, "{}", parameter.name).unwrap();
        }
        write!(result, ")>").unwrap();
    } else if let Some(value) = value.read_object() {
        match &**value.type_handle() {
            intuicio_core::types::Type::Struct(type_) => {
                write!(
                    result,
                    "<{}::{}> {{",
                    type_.module_name.as_deref().unwrap_or(""),
                    type_.name
                )
                .unwrap();
                if !type_.fields().is_empty() {
                    if let Some(indent) = indent {
                        *indent += 1;
                        writeln!(result).unwrap();
                        debug_indent(result, *indent);
                    }
                    for (index, field) in type_.fields().iter().enumerate() {
                        if let Some(value) = value.read_field::<Reference>(&field.name) {
                            if index > 0 {
                                write!(result, ", ").unwrap();
                                if let Some(indent) = indent {
                                    writeln!(result).unwrap();
                                    debug_indent(result, *indent);
                                }
                            }
                            write!(result, "{}: ", field.name).unwrap();
                            debug_impl(value, result, indent);
                        } else {
                            if index > 0 {
                                write!(result, ", ").unwrap();
                            }
                            write!(result, "<?>").unwrap();
                        }
                    }
                    if let Some(indent) = indent {
                        *indent -= 1;
                        writeln!(result).unwrap();
                        debug_indent(result, *indent);
                    }
                }
                write!(result, "}}").unwrap();
            }
            intuicio_core::types::Type::Enum(_) => {}
        }
    } else {
        write!(result, "<?>").unwrap();
    }
}

#[intuicio_function(module_name = "debug", use_registry)]
pub fn debug(registry: &Registry, value: Reference, pretty: Reference) -> Reference {
    let mut result = String::new();
    let indent = Some(0);
    debug_impl(
        &value,
        &mut result,
        &mut if *pretty.read::<Boolean>().unwrap() {
            indent
        } else {
            None
        },
    );
    Reference::new_text(result, registry)
}

pub fn install(registry: &mut Registry) {
    registry.add_function(assert::define_function(registry));
    registry.add_function(debug::define_function(registry));
}