|
| 1 | +use crate::{ |
| 2 | + icombs::{self, IcCompiled}, |
| 3 | + par::{language::CompileError, parse::SyntaxError, process::Expression, types::TypeError}, |
| 4 | +}; |
| 5 | +use crate::{ |
| 6 | + location::FileName, |
| 7 | + par::{ |
| 8 | + builtin::import_builtins, |
| 9 | + program::{CheckedModule, Definition, Module, ParseAndCompileError, TypeOnHover}, |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +use std::fmt::Write; |
| 14 | +use std::sync::Arc; |
| 15 | + |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub(crate) enum Error { |
| 18 | + Syntax(SyntaxError), |
| 19 | + Compile(CompileError), |
| 20 | + InetCompile(crate::icombs::compiler::Error), |
| 21 | + Type(TypeError), |
| 22 | +} |
| 23 | + |
| 24 | +impl Error { |
| 25 | + pub fn display(&self, code: Arc<str>) -> String { |
| 26 | + match self { |
| 27 | + Self::Syntax(error) => { |
| 28 | + // Show syntax error with miette's formatting |
| 29 | + format!( |
| 30 | + "{:?}", |
| 31 | + miette::Report::from(error.to_owned()).with_source_code(code) |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + Self::Compile(error) => format!("{:?}", error.to_report(code)), |
| 36 | + |
| 37 | + Self::Type(error) => format!("{:?}", error.to_report(code)), |
| 38 | + |
| 39 | + Self::InetCompile(err) => { |
| 40 | + format!("inet compilation error: {:?}", err) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Clone)] |
| 47 | +pub enum BuildResult { |
| 48 | + None, |
| 49 | + SyntaxError { |
| 50 | + error: SyntaxError, |
| 51 | + }, |
| 52 | + CompileError { |
| 53 | + error: CompileError, |
| 54 | + }, |
| 55 | + TypeError { |
| 56 | + #[allow(dead_code)] |
| 57 | + pretty: String, |
| 58 | + error: TypeError, |
| 59 | + }, |
| 60 | + InetError { |
| 61 | + #[allow(dead_code)] |
| 62 | + pretty: String, |
| 63 | + checked: Arc<CheckedModule>, |
| 64 | + type_on_hover: TypeOnHover, |
| 65 | + error: icombs::compiler::Error, |
| 66 | + }, |
| 67 | + Ok { |
| 68 | + #[allow(dead_code)] |
| 69 | + pretty: String, |
| 70 | + checked: Arc<CheckedModule>, |
| 71 | + type_on_hover: TypeOnHover, |
| 72 | + ic_compiled: IcCompiled, |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +impl BuildResult { |
| 77 | + pub fn error(&self) -> Option<Error> { |
| 78 | + match self { |
| 79 | + Self::None => None, |
| 80 | + Self::SyntaxError { error } => Some(Error::Syntax(error.clone())), |
| 81 | + Self::CompileError { error } => Some(Error::Compile(error.clone())), |
| 82 | + Self::TypeError { error, .. } => Some(Error::Type(error.clone())), |
| 83 | + Self::InetError { error, .. } => Some(Error::InetCompile(error.clone())), |
| 84 | + Self::Ok { .. } => None, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + #[cfg(feature = "playground")] |
| 89 | + pub fn pretty(&self) -> Option<&str> { |
| 90 | + match self { |
| 91 | + Self::None => None, |
| 92 | + Self::SyntaxError { .. } => None, |
| 93 | + Self::CompileError { .. } => None, |
| 94 | + Self::TypeError { pretty, .. } => Some(&pretty), |
| 95 | + Self::InetError { pretty, .. } => Some(&pretty), |
| 96 | + Self::Ok { pretty, .. } => Some(&pretty), |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn checked(&self) -> Option<Arc<CheckedModule>> { |
| 101 | + match self { |
| 102 | + Self::None => None, |
| 103 | + Self::SyntaxError { .. } => None, |
| 104 | + Self::CompileError { .. } => None, |
| 105 | + Self::TypeError { .. } => None, |
| 106 | + Self::InetError { checked, .. } => Some(Arc::clone(checked)), |
| 107 | + Self::Ok { checked, .. } => Some(Arc::clone(checked)), |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + pub fn type_on_hover(&self) -> Option<&TypeOnHover> { |
| 112 | + match self { |
| 113 | + Self::None => None, |
| 114 | + Self::SyntaxError { .. } => None, |
| 115 | + Self::CompileError { .. } => None, |
| 116 | + Self::TypeError { .. } => None, |
| 117 | + Self::InetError { type_on_hover, .. } => Some(&type_on_hover), |
| 118 | + Self::Ok { type_on_hover, .. } => Some(&type_on_hover), |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + pub fn ic_compiled(&self) -> Option<&IcCompiled> { |
| 123 | + match self { |
| 124 | + Self::None => None, |
| 125 | + Self::SyntaxError { .. } => None, |
| 126 | + Self::CompileError { .. } => None, |
| 127 | + Self::TypeError { .. } => None, |
| 128 | + Self::InetError { .. } => None, |
| 129 | + Self::Ok { ic_compiled, .. } => Some(&ic_compiled), |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + pub fn from_source(source: &str, file: FileName) -> Self { |
| 134 | + let mut module = match Module::parse_and_compile(source, file) { |
| 135 | + Ok(module) => module, |
| 136 | + Err(ParseAndCompileError::Parse(error)) => return Self::SyntaxError { error }, |
| 137 | + Err(ParseAndCompileError::Compile(error)) => return Self::CompileError { error }, |
| 138 | + }; |
| 139 | + import_builtins(&mut module); |
| 140 | + Self::from_compiled(module) |
| 141 | + } |
| 142 | + |
| 143 | + pub fn from_compiled(compiled: Module<Arc<Expression<()>>>) -> Self { |
| 144 | + let pretty = compiled |
| 145 | + .definitions |
| 146 | + .iter() |
| 147 | + .map( |
| 148 | + |Definition { |
| 149 | + span: _, |
| 150 | + name, |
| 151 | + expression, |
| 152 | + }| { |
| 153 | + let mut buf = String::new(); |
| 154 | + write!(&mut buf, "def {} = ", name).expect("write failed"); |
| 155 | + expression.pretty(&mut buf, 0).expect("write failed"); |
| 156 | + write!(&mut buf, "\n\n").expect("write failed"); |
| 157 | + buf |
| 158 | + }, |
| 159 | + ) |
| 160 | + .collect(); |
| 161 | + |
| 162 | + let checked = match compiled.type_check() { |
| 163 | + Ok(checked) => Arc::new(checked), |
| 164 | + Err(error) => return Self::TypeError { pretty, error }, |
| 165 | + }; |
| 166 | + Self::from_checked(pretty, checked) |
| 167 | + } |
| 168 | + |
| 169 | + pub fn from_checked(pretty: String, checked: Arc<CheckedModule>) -> Self { |
| 170 | + let type_on_hover = TypeOnHover::new(&checked); |
| 171 | + let ic_compiled = match icombs::compiler::compile_file(&checked) { |
| 172 | + Ok(ic_compiled) => ic_compiled, |
| 173 | + Err(error) => { |
| 174 | + return Self::InetError { |
| 175 | + pretty, |
| 176 | + checked, |
| 177 | + type_on_hover, |
| 178 | + error, |
| 179 | + } |
| 180 | + } |
| 181 | + }; |
| 182 | + Self::Ok { |
| 183 | + pretty, |
| 184 | + checked, |
| 185 | + type_on_hover, |
| 186 | + ic_compiled, |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments