jetcrab\bytecode/
error.rs

1use crate::ast::Position;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub enum BytecodeError {
6    GenerationError {
7        message: String,
8        position: Option<Position>,
9    },
10
11    OptimizationError {
12        message: String,
13        position: Option<Position>,
14    },
15
16    InvalidInstruction {
17        instruction: String,
18        message: String,
19        position: Option<Position>,
20    },
21
22    ConstantPoolFull {
23        message: String,
24        position: Option<Position>,
25    },
26
27    UnsupportedNode {
28        node_type: String,
29        message: String,
30        position: Option<Position>,
31    },
32
33    StackOverflow {
34        message: String,
35        position: Option<Position>,
36    },
37}
38
39impl std::fmt::Display for BytecodeError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            BytecodeError::GenerationError { message, position } => {
43                write!(f, "Bytecode generation error: {message}")?;
44                if let Some(pos) = position {
45                    write!(f, " at line {}, column {}", pos.line, pos.column)?;
46                }
47                Ok(())
48            }
49            BytecodeError::OptimizationError { message, position } => {
50                write!(f, "Bytecode optimization error: {message}")?;
51                if let Some(pos) = position {
52                    write!(f, " at line {}, column {}", pos.line, pos.column)?;
53                }
54                Ok(())
55            }
56            BytecodeError::InvalidInstruction {
57                instruction,
58                message,
59                position,
60            } => {
61                write!(f, "Invalid instruction '{instruction}': {message}")?;
62                if let Some(pos) = position {
63                    write!(f, " at line {}, column {}", pos.line, pos.column)?;
64                }
65                Ok(())
66            }
67            BytecodeError::ConstantPoolFull { message, position } => {
68                write!(f, "Constant pool full: {message}")?;
69                if let Some(pos) = position {
70                    write!(f, " at line {}, column {}", pos.line, pos.column)?;
71                }
72                Ok(())
73            }
74            BytecodeError::UnsupportedNode {
75                node_type,
76                message,
77                position,
78            } => {
79                write!(f, "Unsupported node type '{node_type}': {message}")?;
80                if let Some(pos) = position {
81                    write!(f, " at line {}, column {}", pos.line, pos.column)?;
82                }
83                Ok(())
84            }
85            BytecodeError::StackOverflow { message, position } => {
86                write!(f, "Stack overflow: {message}")?;
87                if let Some(pos) = position {
88                    write!(f, " at line {}, column {}", pos.line, pos.column)?;
89                }
90                Ok(())
91            }
92        }
93    }
94}
95
96impl std::error::Error for BytecodeError {}