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
use std::fmt::{self, Display, Formatter};

use rush_analyzer::{AssignOp, InfixOp, PrefixOp, Type as AnalyzerType};

use crate::Value;

pub struct Program(pub(crate) Vec<Vec<Instruction>>);

impl Display for Program {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let out = self
            .0
            .iter()
            .enumerate()
            .map(|(idx, func)| {
                let label = match idx {
                    0 => "prelude".to_string(),
                    1 => "main".to_string(),
                    idx => idx.to_string(),
                };
                format!(
                    "{label}: {}\n",
                    func.iter()
                        .enumerate()
                        .map(|(idx, i)| format!("\n [{idx:02}]    {i}"))
                        .collect::<String>()
                )
            })
            .collect::<String>();
        write!(f, "{out}")
    }
}

#[derive(Debug, Clone, Copy)]
pub enum Type {
    Int,
    Bool,
    Char,
    Float,
}

impl From<AnalyzerType> for Type {
    fn from(src: AnalyzerType) -> Self {
        match src {
            AnalyzerType::Int => Self::Int,
            AnalyzerType::Float => Self::Float,
            AnalyzerType::Bool => Self::Bool,
            AnalyzerType::Char => Self::Char,
            _ => unreachable!("cannot convert from these types"),
        }
    }
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Type::Int => "int",
                Type::Bool => "bool",
                Type::Char => "char",
                Type::Float => "float",
            }
        )
    }
}

#[derive(Debug)]
pub enum Instruction {
    /// Adds a new constant to stack.
    Push(Value),
    /// Pops the top-most value off the stack and discards the value.
    Drop,
    /// Calls a function (specified by index).
    Call(usize),
    /// Returns from the current function call.
    Ret,
    /// Special instruction for exit calls.
    Exit,
    /// Jumps to the specified index.
    Jmp(usize),
    /// Jumps to the specified index if the value on the stack is `false`.
    JmpFalse(usize),
    /// Pops the top element off the stack and binds it to the specified number.
    SetVar(usize),
    /// Retrieves the variable with the specified index and places it on top of the stack.
    GetVar(usize),
    // Pops the top element off the stack and sets it as a global using the specified index.
    SetGlobal(usize),
    /// Retrieves the global with the specified index and places it on top of the stack.
    GetGlobal(usize),
    /// Cast the current item on the stack to the specified type.
    Cast(Type),

    // Prefix operators
    Neg,
    Not,

    // Infix operators
    Add,
    Sub,
    Mul,
    Pow,
    Div,
    Rem,
    Eq,
    Ne,
    Lt,
    Gt,
    Le,
    Ge,
    Shl,
    Shr,
    BitOr,
    BitAnd,
    BitXor,
    And,
    Or,
}

impl From<InfixOp> for Instruction {
    fn from(src: InfixOp) -> Self {
        match src {
            InfixOp::Plus => Self::Add,
            InfixOp::Minus => Self::Sub,
            InfixOp::Mul => Self::Mul,
            InfixOp::Div => Self::Div,
            InfixOp::Rem => Self::Rem,
            InfixOp::Pow => Self::Pow,
            InfixOp::Eq => Self::Eq,
            InfixOp::Neq => Self::Ne,
            InfixOp::Lt => Self::Lt,
            InfixOp::Gt => Self::Gt,
            InfixOp::Lte => Self::Le,
            InfixOp::Gte => Self::Ge,
            InfixOp::Shl => Self::Shl,
            InfixOp::Shr => Self::Shr,
            InfixOp::BitOr => Self::BitOr,
            InfixOp::BitAnd => Self::BitAnd,
            InfixOp::BitXor => Self::BitXor,
            InfixOp::And => Self::And,
            InfixOp::Or => Self::Or,
        }
    }
}

impl TryFrom<AssignOp> for Instruction {
    type Error = ();

    fn try_from(src: AssignOp) -> Result<Self, Self::Error> {
        match src {
            AssignOp::Basic => Err(()),
            AssignOp::Plus => Ok(Self::Add),
            AssignOp::Minus => Ok(Self::Sub),
            AssignOp::Mul => Ok(Self::Mul),
            AssignOp::Div => Ok(Self::Div),
            AssignOp::Rem => Ok(Self::Rem),
            AssignOp::Pow => Ok(Self::Pow),
            AssignOp::Shl => Ok(Self::Shl),
            AssignOp::Shr => Ok(Self::Shr),
            AssignOp::BitOr => Ok(Self::BitOr),
            AssignOp::BitAnd => Ok(Self::BitAnd),
            AssignOp::BitXor => Ok(Self::BitXor),
        }
    }
}

impl From<PrefixOp> for Instruction {
    fn from(src: PrefixOp) -> Self {
        match src {
            PrefixOp::Not => Self::Not,
            PrefixOp::Neg => Self::Neg,
        }
    }
}

impl Display for Instruction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Instruction::Push(val) => write!(f, "push {val}"),
            Instruction::Drop => write!(f, "pop"),
            Instruction::Jmp(idx) => write!(f, "jmp {idx}"),
            Instruction::JmpFalse(idx) => write!(f, "jmpfalse {idx}"),
            Instruction::SetVar(idx) => write!(f, "setvar {idx}"),
            Instruction::GetVar(idx) => write!(f, "getvar {idx}"),
            Instruction::SetGlobal(idx) => write!(f, "setglob {idx}"),
            Instruction::GetGlobal(idx) => write!(f, "getglob {idx}"),
            Instruction::Call(idx) => write!(f, "call {idx}"),
            Instruction::Cast(to) => write!(f, "cast {to}"),
            Instruction::Ret => write!(f, "ret"),
            Instruction::Exit => write!(f, "exit"),
            Instruction::Not => write!(f, "not"),
            Instruction::Neg => write!(f, "neg"),
            Instruction::Add => write!(f, "add"),
            Instruction::Sub => write!(f, "sub"),
            Instruction::Mul => write!(f, "mul"),
            Instruction::Pow => write!(f, "pow"),
            Instruction::Div => write!(f, "div"),
            Instruction::Rem => write!(f, "rem"),
            Instruction::Eq => write!(f, "eq"),
            Instruction::Ne => write!(f, "ne"),
            Instruction::Lt => write!(f, "lt"),
            Instruction::Gt => write!(f, "gt"),
            Instruction::Le => write!(f, "le"),
            Instruction::Ge => write!(f, "ge"),
            Instruction::Shl => write!(f, "shl"),
            Instruction::Shr => write!(f, "shr"),
            Instruction::BitOr => write!(f, "bitor"),
            Instruction::BitAnd => write!(f, "bitand"),
            Instruction::BitXor => write!(f, "bitxor"),
            Instruction::And => write!(f, "and"),
            Instruction::Or => write!(f, "or"),
        }
    }
}