~starkingdoms/starkingdoms

ref: 3cad7d73f8ce1baf291a334628fb96a93c5fafe6 starkingdoms/kabel/src/debug.rs -rw-r--r-- 11.7 KiB
3cad7d73 — ghostly_zsh list initialization and subscripting 1 year, 3 months ago
                                                                                
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
use crate::{ast::AST, lexer::Token, push_codegen, push_output, vm::{Value, VM}};

pub fn debug_token_array(tokens: Vec<Token>) -> String {
    let mut output = "".to_string();
    for token in tokens {
        output += &token.token_type.to_string();
        output += "\n";
    }
    output[..output.len()-1].to_string()
}
pub fn debug_ast(ast: AST, level: usize) -> String {
    use crate::ast::ASTType::*;
    use crate::ast::BinOp::*;
    use crate::ast::UnOp::*;
    use crate::ast::Lit::*;
    let mut output = "".to_string();
    match ast.ast_type {
        Program(asts) => {
            output += &"| ".repeat(level);
            output += "Program";
            for ast in asts {
                output += "\n";
                output += &debug_ast(ast, level+1);
            }
        }
        Function(name, args, block) => {
            output += &"| ".repeat(level);
            output += "Function";
            output += &(" ".to_string() + &name.name);
            for arg in args {
                output += &(" ".to_string() + &arg.name);
            }
            output += &format!(" {:?}", ast.extensions);
            output += "\n";
            output += &debug_ast(*block, level+1);
        }
        Return(expr) => {
            output += &"| ".repeat(level);
            output += "Return";
            if let Some(expr) = *expr {
                output += "\n";
                output += &debug_ast(expr, level+1);
            }
        }
        Loop(block) => {
            output += &"| ".repeat(level);
            output += "Loop";
            output += "\n";
            output += &debug_ast(*block, level+1);
        }
        While(condition, block) => {
            output += &"| ".repeat(level);
            output += "While";
            output += "\n";
            output += &debug_ast(*condition, level+1);
            output += "\n";
            output += &debug_ast(*block, level+1);
        }
        For(expr1, expr2, expr3, block) => {
            output += &"| ".repeat(level);
            output += "For";
            if let Some(expr1) = *expr1 {
                output += "\n";
                output += &debug_ast(expr1, level+1);
            }
            if let Some(expr2) = *expr2 {
                output += "\n";
                output += &debug_ast(expr2, level+1);
            }
            if let Some(expr3) = *expr3 {
                output += "\n";
                output += &debug_ast(expr3, level+1);
            }
            output += "\n";
            output += &debug_ast(*block, level+1);
        }
        Break => {
            output += &"| ".repeat(level);
            output += "Break";
        }
        Continue => {
            output += &"| ".repeat(level);
            output += "Continue";
        }
        If(condition, block, else_expr) => {
            output += &"| ".repeat(level);
            output += "If\n";
            output += &debug_ast(*condition, level+1);
            output += "\n";
            output += &debug_ast(*block, level+1);
            if let Some(else_expr) = *else_expr {
                output += "\n";
                output += &"| ".repeat(level);
                output += "Else";
                output += "\n";
                output += &debug_ast(else_expr, level+1);
            }
        }
        Block(asts) => {
            output += &"| ".repeat(level);
            output += "Block";
            for ast in asts {
                output += "\n";
                output += &debug_ast(ast, level+1);
            }
        }
        Expr(expr) => {
            output += &"| ".repeat(level);
            output += "Expr\n";
            output += &debug_ast(*expr, level+1);
        }
        // REMOVE LATER
        Print(expr) => {
            output += &"| ".repeat(level);
            output += "Print\n";
            output += &debug_ast(*expr, level+1);
        }
        Decl(name, expr) => {
            output += &"| ".repeat(level);
            output += "Decl ";
            output += &name.name;
            output += &format!(" {:?}", ast.extensions);
            output += "\n";
            output += &debug_ast(*expr, level+1);
        }
        Assign(name, expr) => {
            output += &"| ".repeat(level);
            output += "Assign ";
            output += &name.name;
            output += &format!(" {:?}", ast.extensions);
            output += "\n";
            output += &debug_ast(*expr, level+1);
        }
        Ternary(condition, true_expr, false_expr) => {
            output += &"| ".repeat(level);
            output += "Ternary\n";
            output += &debug_ast(*condition, level+1);
            output += "\n";
            output += &debug_ast(*true_expr, level+1);
            output += "\n";
            output += &debug_ast(*false_expr, level+1);
        }
        Subscript(array, index) => {
            output += &"| ".repeat(level);
            output += "Subscript\n";
            output += &debug_ast(*array, level+1);
            output += "\n";
            output += &debug_ast(*index, level+1);
        }
        Binary(left, oper, right) => {
            output += &"| ".repeat(level);
            output += "Binary ";
            push_output!(oper, output,
                Add, Sub, Mul, Div, Mod, BitAnd, BitXor, BitOr,
                Eq, Ne, Gr, Ge, Ls, Le, Or, And);
            output += "\n";
            output += &debug_ast(*left, level+1);
            output += "\n";
            output += &debug_ast(*right, level+1);
        }
        Unary(oper, right) => {
            output += &"| ".repeat(level);
            output += "Unary ";
            push_output!(oper, output,
                Not, Neg);
            output += "\n";
            output += &debug_ast(*right, level+1);
        }
        Lit(lit) => {
            output += &"| ".repeat(level);
            output += "Lit ";
            match lit {
                Ident(name) => {
                    output += &name;
                    output += &format!(" {:?}", ast.extensions);
                }
                Str(value) => {
                    output += &value;
                }
                Num(value) => {
                    output += &value.to_string();
                }
                Bool(value) => {
                    output += &value.to_string();
                }
                Array(value) => {
                    for value in value {
                        output += "\n";
                        output += &debug_ast(value, level+1);
                    }
                }
            }
        }
        Call(name, args) => {
            output += &"| ".repeat(level);
            output += "Call\n";
            output += &debug_ast(*name, level+1);
            for arg in args {
                output += "\n";
                output += &debug_ast(arg, level+1);
            }
        }
        Member(left, right) => {
            output += &"| ".repeat(level);
            output += "Member\n";
            output += &debug_ast(*left, level+1);
            output += "\n";
            output += &debug_ast(*right, level+1);
        }
    }
    output
}

pub fn debug_bytecode(vm: &VM) -> String {
    use crate::opcodes::OpCode::*;
    //let mut ip = 0;
    let mut output = "".to_string();
    let mut vm = vm.clone();
    while vm.ip < vm.units[vm.unit_ptr].code.len() {
        // remove PRINT later
        push_codegen!(vm.units[vm.unit_ptr].code[vm.ip].into(), vm, output, ADD, SUB, MUL, DIV, MOD, BITAND,
            BITXOR, BITOR, EQ, NE, GR, GE, LS, LE, OR, AND, NOT, NEG,
            PRINT, ERR);
        match vm.units[vm.unit_ptr].code[vm.ip].into() {
            LOAD => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " CONSTANT ";
                vm.ip += 1;
                output += &vm.units[vm.unit_ptr].code[vm.ip].to_string();
                output += "\n";
            }
            VAR => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " VAR ";
                vm.ip += 1;
                output += &vm.units[vm.unit_ptr].code[vm.ip].to_string();
                output += "\n";
            }
            ASSIGN => { 
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " ASSIGN ";
                vm.ip += 1;
                output += &vm.units[vm.unit_ptr].code[vm.ip].to_string();
                output += "\n";
            }

            JMP => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " JMP ";
                vm.ip += 1;
                let byte_one = (vm.units[vm.unit_ptr].code[vm.ip] as u16) << 8;
                vm.ip += 1;
                let byte_two = vm.units[vm.unit_ptr].code[vm.ip] as u16;
                output += &(byte_one | byte_two).to_string();
                output += "\n";
            }
            JMP_UP => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " JMP_UP ";
                vm.ip += 1;
                let byte_one = (vm.units[vm.unit_ptr].code[vm.ip] as u16) << 8;
                vm.ip += 1;
                let byte_two = vm.units[vm.unit_ptr].code[vm.ip] as u16;
                output += &(byte_one | byte_two).to_string();
                output += "\n";
            }
            JNE => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " JNE ";
                vm.ip += 1;
                let byte_one = (vm.units[vm.unit_ptr].code[vm.ip] as u16) << 8;
                vm.ip += 1;
                let byte_two = vm.units[vm.unit_ptr].code[vm.ip] as u16;
                output += &(byte_one | byte_two).to_string();
                output += "\n";
            }
            CALL => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " CALL ";
                vm.ip += 1;
                output += &(vm.units[vm.unit_ptr].code[vm.ip]).to_string();
                output += "\n";
            }
            RET => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " RET";
                output += "\n";
            }
            LIST => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " LIST ";
                vm.ip += 1;
                output += &(vm.units[vm.unit_ptr].code[vm.ip]).to_string();
                output += "\n";
            }
            SCR => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " SCR";
                output += "\n";
            }
            POP => {
                output += &vm.ip.to_string();
                output += " ";
                output += &vm.find_line().to_string();
                output += " POP ";
                vm.ip += 1;
                output += &vm.units[vm.unit_ptr].code[vm.ip].to_string();
                output += "\n";
            }
            _ => {}
        }
        vm.ip += 1;
    }
    output
}
pub fn debug_stack(stack: &Vec<Value>) -> String {
    let mut output = "".to_string();
    for value in stack.iter().rev() {
        output += &format!("{:?}", value);
        output += "\n";
    }
    output
}