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
use colored::Colorize;

use crate::{context::NessaContext, parser::NessaExpr, operations::Operator};

impl NessaContext {
    pub fn to_string(&self, expr: &NessaExpr) -> String  {
        match expr {
            NessaExpr::NameReference(_, n) |
            NessaExpr::Variable(_, _, n, _) => n.clone().cyan().to_string(),
            NessaExpr::CompiledVariableDefinition(_, _, n, t, e) => format!("let {}: {} = {}", n.cyan(), t.get_name(self), self.to_string(e)),
            NessaExpr::CompiledVariableAssignment(_, _, n, _, e) => format!("{} = {}", n.cyan(), self.to_string(e)),

            NessaExpr::Literal(_, obj) => obj.to_debug_string().magenta().to_string(),
            NessaExpr::Tuple(_, args) => format!("({})", args.iter().map(|i| self.to_string(i)).collect::<Vec<_>>().join(", ")),

            NessaExpr::FunctionCall(_, id, t, args) => {
                let temp = if t.is_empty() {
                    String::new()

                } else {
                    format!("<{}>", t.iter().map(|t| t.get_name(self)).collect::<Vec<_>>().join(", "))
                };

                format!(
                    "{}{}({})", 
                    self.functions[*id].name.green(), 
                    temp,
                    args.iter().map(|i| self.to_string(i)).collect::<Vec<_>>().join(", ")
                )
            },

            NessaExpr::UnaryOperation(_, id, t, expr) => {
                let temp = if t.is_empty() {
                    String::new()

                } else {
                    format!("<{}>", t.iter().map(|t| t.get_name(self)).collect::<Vec<_>>().join(", "))
                };

                if let Operator::Unary { representation, prefix, .. } = &self.unary_ops[*id] {
                    if *prefix {
                        return format!("{}{}{}", representation, temp, self.to_string(expr))

                    } else {
                        return format!("{}{}{}", self.to_string(expr), temp, representation)
                    }
                }

                unreachable!()
            },

            NessaExpr::BinaryOperation(_, id, t, a, b) => {
                let temp = if t.is_empty() {
                    String::new()

                } else {
                    format!("<{}>", t.iter().map(|t| t.get_name(self)).collect::<Vec<_>>().join(", "))
                };

                format!("{} {}{} {}", self.to_string(a), temp, self.binary_ops[*id].get_repr(), self.to_string(b))
            },

            NessaExpr::NaryOperation(_, id, t, a, b) => {
                let temp = if t.is_empty() {
                    String::new()

                } else {
                    format!("<{}>", t.iter().map(|t| t.get_name(self)).collect::<Vec<_>>().join(", "))
                };

                if let Operator::Nary { open_rep, close_rep, .. } = &self.nary_ops[*id] {
                    return format!("{}{}{}{}{}", self.to_string(a), temp, open_rep, b.iter().map(|i| self.to_string(i)).collect::<Vec<_>>().join(", "), close_rep)
                }

                unreachable!()
            },

            NessaExpr::Return(_, expr) => format!("return {}", self.to_string(expr)),

            _ => todo!("Unable to convert {:?} to pretty string", expr)
        }
    }
}