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
use std::alloc::Layout;

use crate::{ast::Identifier, token};

#[derive(Debug)]
pub enum CompileError {
    /// compile had not ended in main scope
    NotFinishedInMain,

    /// cannot find identifier on symbol table
    IdentifierNotFound(Identifier),

    /// creation of [`super::compiler::Compiler`] have failed because of inner instruction failed
    CreationFailed(InstructionsError),

    /// failed to write (emit) instruction
    InstructionWriteError(InstructionsError),

    /// failed to create new scope bacause of
    /// [`InstructionsError`]
    ScopeCreateFaild(InstructionsError),

    WrongPrefixOperator(token::Kind),
    WrongInfixOperator(token::Kind),
}

#[derive(Debug)]
pub enum InstructionsError {
    /// memory allocation have failed
    AllocationFailed(Layout),
    /// alloc size > [isize::MAX]
    TooLargeToAllocate,

    /// cannot read instruction at offset
    CannotRead { offset: usize },
}

#[derive(Debug)]
pub enum FrameError {
    /// wraper of InstructionsError
    InnerError(InstructionsError),
}

impl From<InstructionsError> for FrameError {
    fn from(err: InstructionsError) -> Self {
        FrameError::InnerError(err)
    }
}

#[derive(Debug)]
pub enum BytecodeError {
    /// wraper of InstructionsError
    InnerError(InstructionsError),
}

impl From<InstructionsError> for BytecodeError {
    fn from(err: InstructionsError) -> Self {
        Self::InnerError(err)
    }
}