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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use crate::{
    ast::{
        ArrayLiteral, BlockStatement, BooleanLiteral, CallExpression,
        Expression, ExpressionStatement, FunctionLiteral, Identifier,
        IfExpression, IndexExpression, InfixExpression, IntegerLiteral,
        LetStatement, PrefixExpression, Program, ReturnStatement, Statement,
        StringLiteral,
    },
    object::{Bool, CompiledFunction, Int, Object, StringObject},
    token::Kind,
};

use super::{
    errors::CompileError,
    instruction::Instruction,
    instructions::Instructions,
    symbol::{self, Symbol, SymbolTable},
    Bytecode,
};

const SUCCESS: Result<bool, CompileError> = Ok(true);

/// Hold current function's instructions
#[derive(Debug, Clone)]
struct Scope {
    /// current function's instructions
    instructions: Instructions,
}

impl Scope {
    pub fn new(instructions: Instructions) -> Self {
        Self { instructions }
    }
}

#[derive(Debug)]
struct InstructionInfo {
    instruction: Instruction,
    pos: usize,
}

#[derive(Debug)]
pub struct Compiler {
    /// constants of Program
    constants: Vec<Object>,

    /// stack memory of function's instruction aka [`Scope`]
    scopes: Vec<Scope>,

    /// index of current_function's scope
    scope_idx: usize,

    /// symbol_table which holds symbol.
    /// never be None
    symbol_table: Option<SymbolTable>,

    /// last written instruction's information
    last_instruction: Option<InstructionInfo>,
}

impl Compiler {
    /// Creates a new [`Compiler`].
    ///
    /// # Panics
    ///
    /// May Panics if OOM
    ///
    /// # Errors [`CompileError::CreationFailed`]
    ///
    /// This function will return an error if inner instructions creation
    /// have failed
    pub fn create() -> Result<Self, CompileError> {
        let mut scopes = Vec::new();
        match Instructions::create() {
            Err(e) => {
                return Err(CompileError::CreationFailed(e));
            }
            Ok(ins) => {
                scopes.push(
                    // this Scope is will be main function
                    Scope { instructions: ins },
                );
            }
        }

        Ok(Self {
            constants: Vec::new(),

            scopes,
            scope_idx: 0,

            symbol_table: Some(SymbolTable::new()),

            last_instruction: None,
        })
    }

    /// Generate bytecode as compile result
    ///
    /// # Errors [`CompileError::NotFinishedInMain`]
    ///
    /// This function will return an error if
    /// scope_idx is not 0 ( it means that compiling had not ended in main )
    pub fn bytecode(self) -> Result<Bytecode, CompileError> {
        if self.scope_idx != 0 {
            return Err(CompileError::NotFinishedInMain);
        }
        Ok(Bytecode {
            constants: self.constants.clone(),
            instructions: self.current_scope().instructions.clone(),
        })
    }

    /// compile given src: Program
    ///
    /// # Errors [`CompileError`]
    ///
    /// This function will return an error if inner function failed.
    ///
    /// if have successfully compiled, returns Ok(True)
    pub fn compile(&mut self, src: Program) -> Result<bool, CompileError> {
        for stm in src.statements {
            self.compile_stm(&stm)?;
        }

        SUCCESS
    }

    /// compile statement
    fn compile_stm(&mut self, stm: &Statement) -> Result<bool, CompileError> {
        match stm {
            Statement::ExpressionStatement(stm) => {
                self.compile_expression_stm(stm)
            }
            Statement::LetStatement(stm) => self.compile_let_stm(stm),
            Statement::ReturnStatement(stm) => self.compile_return_stm(stm),
            Statement::BlockStatement(stm) => self.compile_block_stm(stm),
        }
    }

    /// compile expression
    fn compile_exp(&mut self, exp: &Expression) -> Result<bool, CompileError> {
        match exp {
            Expression::Identifier(exp) => self.compile_identifier_exp(exp),
            Expression::IntegerLiteral(lit) => {
                self.compile_integer_literal(lit)
            }
            Expression::BooleanLiteral(lit) => self.compile_bool_literal(lit),
            Expression::StringLiteral(lit) => self.compile_string_literal(lit),
            Expression::FunctionLiteral(lit) => {
                self.compile_function_literal(lit)
            }
            Expression::ArrayLiteral(lit) => self.compile_array_literal(lit),
            Expression::InfixExpression(exp) => self.compile_infix_exp(exp),
            Expression::PrefixExpression(exp) => self.compile_prefix_exp(exp),
            Expression::IfExpression(exp) => self.compile_if_exp(exp),
            Expression::CallExpression(exp) => self.compile_call_exp(exp),
            Expression::IndexExpression(exp) => self.compile_index_exp(exp),
        }
    }

    /// compile expression statement
    fn compile_expression_stm(
        &mut self,
        stm: &ExpressionStatement,
    ) -> Result<bool, CompileError> {
        self.compile_exp(stm.expression.as_ref().unwrap())?;
        self.emit(Instruction::POP)?;
        SUCCESS
    }

    /// compile let statement
    fn compile_let_stm(
        &mut self,
        stm: &LetStatement,
    ) -> Result<bool, CompileError> {
        let idx = self
            .symbol_table
            .as_mut()
            .unwrap()
            .define(&stm.identifier.value);

        self.compile_exp(&stm.value.clone().unwrap())?;

        if self.symbol_table.as_ref().unwrap().is_global() {
            self.emit(Instruction::DEFGLB { idx })?;
        } else {
            self.emit(Instruction::DEFLCL { idx })?;
        }

        SUCCESS
    }

    /// compile return statement
    fn compile_return_stm(
        &mut self,
        stm: &ReturnStatement,
    ) -> Result<bool, CompileError> {
        match &stm.value {
            Some(exp) => {
                self.compile_exp(exp)?;
                self.emit(Instruction::RETV)?;
            }
            None => {
                self.emit(Instruction::RETN)?;
            }
        }
        SUCCESS
    }

    /// compile block statement
    fn compile_block_stm(
        &mut self,
        stm: &BlockStatement,
    ) -> Result<bool, CompileError> {
        for statement in &stm.statements {
            self.compile_stm(statement)?;
        }
        SUCCESS
    }

    /// Compile given identifier expression.
    /// try to find a symbol with given identifier,
    /// and if have found, emit load symbol instruction.
    ///
    /// # Errors [CompileError::IdentifierNotFound]
    /// This function will return an error if identifier not found on [`self.symbol_table`]
    fn compile_identifier_exp(
        &mut self,
        exp: &Identifier,
    ) -> Result<bool, CompileError> {
        let rst = self.symbol_table.as_mut().unwrap().resolve(&exp.value);
        if rst.is_some() {
            let sym = rst.unwrap();
            self.load_symbol(&sym)?;

            SUCCESS
        } else {
            Err(CompileError::IdentifierNotFound(exp.clone()))
        }
    }

    /// Compile given IntegerLiteral
    ///
    /// # Errors
    ///
    /// This function will return an error if .
    fn compile_integer_literal(
        &mut self,
        lit: &IntegerLiteral,
    ) -> Result<bool, CompileError> {
        let int = Object::Int(Int { value: lit.value });
        self.constants.push(int); // enlist Int to constant pool
        self.emit(Instruction::CONST {
            idx: self.constants.len() - 1,
        })?;

        SUCCESS
    }

    /// Compile given bool literal
    ///
    /// # Errors
    ///
    /// This function will return an error if .
    fn compile_bool_literal(
        &mut self,
        lit: &BooleanLiteral,
    ) -> Result<bool, CompileError> {
        let boolean = Object::Bool(Bool { value: lit.value });
        self.constants.push(boolean); // enlist boolean to constant pool
        self.emit(Instruction::CONST {
            idx: self.constants.len() - 1,
        })?;

        SUCCESS
    }
    /// Compile given string literal
    ///
    /// # Errors
    ///
    /// This function will return an error if .
    fn compile_string_literal(
        &mut self,
        lit: &StringLiteral,
    ) -> Result<bool, CompileError> {
        let str = Object::String(StringObject {
            value: lit.value.clone(),
        });
        self.constants.push(str);
        self.emit(Instruction::CONST {
            idx: self.constants.len() - 1,
        })?;

        SUCCESS
    }

    /// Compile given function literal
    ///
    /// # Errors
    ///
    /// This function will return an error if failed to compile
    fn compile_function_literal(
        &mut self,
        lit: &FunctionLiteral,
    ) -> Result<bool, CompileError> {
        self.enter_scope()?;

        if lit.ident.is_some() {
            unsafe {
                self.symbol_table
                    .as_mut()
                    // this is safe because it's never be None
                    .unwrap_unchecked()
                    .define_function_name(&lit.ident.as_ref().unwrap().value);
            }
        }

        // Instruction example

        // parameters
        // 001 DEFLCL 0 (param1)
        // 002 DEFLCL 1 (param2)

        for param in &lit.parameters {
            self.symbol_table.as_mut().unwrap().define(&param.value);
        }

        // 003 DEFLCL 2 (local1)
        // 004 DEFLCL 3 (local2)

        self.compile_block_stm(&lit.body)?;
        // 005 GETLCL 3 (local2)
        // 006 GETLCL 2 (local1)

        // if the last instuction is POP then
        //  replace it with RETV
        //  which means it's have ended with expression statement.
        //  it's implicitly returned
        //
        if !self.update_last_instruction_if(Instruction::POP, Instruction::RETV)
        {
            self.emit(Instruction::RETN)?;
        }

        let free_syms = self.symbol_table.as_ref().unwrap().get_free();

        let local_len = self.symbol_table.as_ref().unwrap().len;
        let body_scope = self.leave_scope();
        // free variable (local variable of outer function)
        // 007 GETFREE 0 (free1)
        // 008 GETFREE 1 (free2)

        for free in free_syms.iter() {
            self.load_symbol(free)?;
        }

        let compiled_function = CompiledFunction {
            arg_len: lit.parameters.len(),
            local_len,
            instructions: body_scope.instructions,
        };

        self.constants
            .push(Object::CompiledFunction(compiled_function));

        self.emit(Instruction::CLOSURE {
            idx: self.constants.len() - 1,
            free: free_syms.len(),
        })?;

        // if function has identifier and have't let bind
        if lit.ident.is_some() && !lit.is_let_bind {
            let idx = self
                .symbol_table
                .as_mut()
                .unwrap()
                .define(&lit.ident.as_ref().unwrap().value);
            if self.symbol_table.as_ref().unwrap().is_global() {
                self.emit(Instruction::DEFGLB { idx })?;
            } else {
                self.emit(Instruction::DEFLCL { idx })?;
            }
        }

        SUCCESS
    }

    fn compile_array_literal(
        &mut self,
        lit: &ArrayLiteral,
    ) -> Result<bool, CompileError> {
        for el in lit.elements.iter() {
            self.compile_exp(el)?;
        }
        self.emit(Instruction::ARRAY {
            count: lit.elements.len(),
        })?;

        SUCCESS
    }

    fn compile_prefix_exp(
        &mut self,
        exp: &PrefixExpression,
    ) -> Result<bool, CompileError> {
        self.compile_exp(&exp.right)?;

        match exp.token.kind {
            Kind::Bang => self.emit(Instruction::BANG)?,
            Kind::Minus => self.emit(Instruction::NEG)?,
            wrong_token => Err(CompileError::WrongPrefixOperator(wrong_token))?,
        };

        SUCCESS
    }

    fn compile_infix_exp(
        &mut self,
        exp: &InfixExpression,
    ) -> Result<bool, CompileError> {
        self.compile_exp(&exp.right)?;
        self.compile_exp(&exp.left)?;

        match exp.operator.kind {
            Kind::Assign => todo!(),
            Kind::Plus => self.emit(Instruction::ADD)?,
            Kind::Minus => self.emit(Instruction::SUB)?,
            Kind::Product => self.emit(Instruction::PRODUCT)?,
            Kind::Divide => self.emit(Instruction::DIVIDE)?,
            Kind::Mod => self.emit(Instruction::MOD)?,
            Kind::LT => self.emit(Instruction::CLT)?,
            Kind::LT_OR_EQ => self.emit(Instruction::CLTE)?,
            Kind::GT => self.emit(Instruction::CGT)?,
            Kind::GT_OR_EQ => self.emit(Instruction::CGTE)?,
            Kind::EQ => self.emit(Instruction::CEQ)?,
            Kind::NOT_EQ => self.emit(Instruction::CNEQ)?,
            Kind::And => self.emit(Instruction::AND)?,
            Kind::Or => self.emit(Instruction::OR)?,
            Kind::Bit_And => self.emit(Instruction::BAND)?,
            Kind::Bit_Or => self.emit(Instruction::BOR)?,
            wrong_token => Err(CompileError::WrongInfixOperator(wrong_token))?,
        };

        SUCCESS
    }
    /// .
    ///
    /// # Panics
    ///
    /// Panics if .
    ///
    /// # Errors
    ///
    /// This function will return an error if .
    fn compile_if_exp(
        &mut self,
        exp: &IfExpression,
    ) -> Result<bool, CompileError> {
        self.compile_exp(&exp.condition)?;
        let jump_consequence = self.emit(Instruction::JNS { idx: 0 })?;
        self.compile_stm(&Statement::BlockStatement(exp.consequence.clone()))?;

        self.remove_last_instruction_if(Instruction::POP);

        if exp.alternative.is_some() {
            let jump_alternative = self.emit(Instruction::JMP { idx: 0 })?;

            unsafe {
                self.update(
                    Instruction::JNS {
                        idx: self.next_offset(),
                    },
                    jump_consequence,
                );
            }

            self.compile_stm(&Statement::BlockStatement(
                exp.alternative.clone().unwrap(),
            ))?;
            self.remove_last_instruction_if(Instruction::POP);

            unsafe {
                self.update(
                    Instruction::JMP {
                        idx: self.next_offset(),
                    },
                    jump_alternative,
                );
            }
        } else {
            unsafe {
                self.update(
                    Instruction::JNS {
                        idx: self.next_offset(),
                    },
                    jump_consequence,
                );
            }
        }

        SUCCESS
    }

    fn compile_call_exp(
        &mut self,
        exp: &CallExpression,
    ) -> Result<bool, CompileError> {
        self.compile_exp(&exp.function)?;

        for arg in &exp.arguments {
            self.compile_exp(arg)?;
        }

        self.emit(Instruction::CALL {
            arg_len: exp.arguments.len(),
        })?;

        SUCCESS
    }

    fn compile_index_exp(
        &mut self,
        exp: &IndexExpression,
    ) -> Result<bool, CompileError> {
        self.compile_exp(&exp.left)?;
        self.compile_exp(&exp.index)?;

        self.emit(Instruction::INDEX)?;

        SUCCESS
    }

    fn emit(&mut self, ins: Instruction) -> Result<usize, CompileError> {
        let res = self
            .current_scope_mut()
            .instructions
            .add_instruction(ins.clone());

        if let Err(e) = res {
            return Err(CompileError::InstructionWriteError(e));
        }

        let pos = res.unwrap();

        self.last_instruction = Some(InstructionInfo {
            instruction: ins,
            pos,
        });

        Ok(pos)
    }
    /// Update instruction at `offset` with given `ins`
    ///
    /// # Safety
    ///
    /// this function is unsafe because it call [`Instructions::update_instruction`]
    ///
    unsafe fn update(&mut self, ins: Instruction, offset: usize) {
        self.current_scope_mut()
            .instructions
            .update_instruction(ins, offset)
    }

    /// Returns the remove last instruction of this [`Compiler`].
    fn remove_last_instruction(&mut self) {
        let new_length = self.last_instruction.as_ref().unwrap().pos;
        self.current_scope_mut()
            .instructions
            .remove_instruction(new_length);
        self.last_instruction = None;
    }

    /// Update last_instruction if (ins_info.instruction == ins) || (ins_info.instruction == with)
    ///
    /// returns true if have changed instruction
    fn update_last_instruction_if(
        &mut self,
        ins: Instruction,
        with: Instruction,
    ) -> bool {
        if self.last_instruction.as_ref().is_some_and(|ins_info| {
            (ins_info.instruction == ins) || (ins_info.instruction == with)
        }) {
            let pos = self.last_instruction.as_ref().unwrap().pos;

            unsafe {
                self.update(with.clone(), pos);
            }

            self.last_instruction = Some(InstructionInfo {
                instruction: with,
                pos,
            });

            return true;
        }
        false
    }

    /// Remove last instruction if `self.last_instruction == ins`
    fn remove_last_instruction_if(&mut self, ins: Instruction) {
        if self
            .last_instruction
            .as_ref()
            .is_some_and(|ins_info| ins_info.instruction == ins)
        {
            self.remove_last_instruction();
        }
    }

    /// Returns the next offset of this [`Compiler`].
    fn next_offset(&self) -> usize {
        self.current_scope().instructions.length()
    }

    /// create new scope and enclose current `self.symbol_table`
    fn enter_scope(&mut self) -> Result<bool, CompileError> {
        let inst_rst = Instructions::create();
        if inst_rst.is_err() {
            // this is safe
            unsafe {
                return Err(CompileError::ScopeCreateFaild(
                    // this will prohibit redundant checking
                    inst_rst.unwrap_err_unchecked(),
                ));
            }
        }
        let new_scope = Scope::new(unsafe { inst_rst.unwrap_unchecked() });

        self.scopes.push(new_scope);
        self.scope_idx += 1;

        self.symbol_table =
            Some(SymbolTable::enclose(self.symbol_table.take().unwrap()));

        SUCCESS
    }

    /// Leave [`Scope`] of this [`Compiler`]
    /// and return previous [`Scope`]
    ///
    /// # Panics
    ///
    /// Panics if scopes length below 1 ( len < 1 )
    fn leave_scope(&mut self) -> Scope {
        self.symbol_table =
            Some(self.symbol_table.as_mut().unwrap().get_outer());
        self.scope_idx -= 1;
        self.scopes.pop().unwrap()
    }
    /// Returns a reference to the current scope of this [`Compiler`].
    fn current_scope(&self) -> &Scope {
        &self.scopes[self.scope_idx]
    }

    /// Returns a mutable reference to the current scope of this [`Compiler`].
    fn current_scope_mut(&mut self) -> &mut Scope {
        &mut self.scopes[self.scope_idx]
    }

    fn load_symbol(&mut self, sym: &Symbol) -> Result<bool, CompileError> {
        let inst = match sym.scope() {
            symbol::Scope::Global => Instruction::GETGLB { idx: sym.index },
            symbol::Scope::Local => Instruction::GETLCL { idx: sym.index },
            symbol::Scope::Free => Instruction::GETFREE { idx: sym.index },
            symbol::Scope::Function => Instruction::GETCUR,
        };

        self.emit(inst)?;

        SUCCESS
    }
}