cao_lang/compiler/
compilation_error.rs

1use std::fmt::Display;
2
3use crate::prelude::Trace;
4
5use thiserror::Error;
6
7#[derive(Debug, Clone, Error)]
8pub struct CompilationError {
9    pub payload: CompilationErrorPayload,
10    pub loc: Option<Trace>,
11}
12
13impl CompilationError {
14    pub fn with_loc(payload: CompilationErrorPayload, index: Trace) -> Self {
15        Self {
16            payload,
17            loc: Some(index),
18        }
19    }
20}
21
22impl Display for CompilationError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        if let Some(loc) = self.loc.as_ref() {
25            write!(f, "CompilationError: [{}], Error: {}", loc, self.payload)
26        } else {
27            write!(f, "{}", self.payload)
28        }
29    }
30}
31
32#[derive(Debug, Clone, Error)]
33pub enum CompilationErrorPayload {
34    #[error("The requested functionality ({0}) is not yet implemented")]
35    Unimplemented(&'static str),
36
37    #[error("Entrypoint not found")]
38    NoMain,
39
40    #[error("Program was empty")]
41    EmptyProgram,
42
43    #[error("Functions {0} has too many cards. Number of cards in a function may not be larger than 2^16 - 1 = 65535")]
44    TooManyCards(usize),
45
46    #[error("Function names must be unique. Found duplicated name: {0}")]
47    DuplicateName(String),
48
49    #[error("Module names must be unique. Found duplicated name: {0}")]
50    DuplicateModule(String),
51
52    #[error("SubProgram: [{0}] was not found")]
53    MissingSubProgram(String),
54
55    #[error("Jumping to {dst} can not be performed\n{msg:?}")]
56    InvalidJump { dst: String, msg: Option<String> },
57
58    #[error("Internal failure during compilation")]
59    InternalError,
60
61    #[error("Too many locals in scope")]
62    TooManyLocals,
63
64    #[error("Too many upvalues in scope. Try capturing less variables")]
65    TooManyUpvalues,
66
67    #[error("Variable name {0} can not be used")]
68    BadVariableName(String),
69
70    #[error("Variable name can't be empty")]
71    EmptyVariable,
72
73    #[error("{0:?} is not a valid name for a Function")]
74    BadFunctionName(String),
75
76    #[error("Recursion limit ({0}) reached")]
77    RecursionLimitReached(u32),
78
79    #[error("Import '{0}' is not valid")]
80    BadImport(String),
81
82    #[error("Import '{0}' is ambigous")]
83    AmbigousImport(String),
84
85    #[error("Too many `super.` calls.")]
86    SuperLimitReached,
87}