1pub mod abstractions;
31pub mod components;
32pub mod processes;
33pub mod utils;
34
35pub use abstractions::*;
37pub use components::context::config::Environment;
38pub use components::context::Context;
39pub use components::error_message::typr_error::TypRError;
40pub use components::language::Lang;
41pub use components::r#type::Type;
42
43pub use processes::parsing;
45pub use processes::transpiling;
46pub use processes::type_checking::{typing, typing_with_errors, TypingResult};
47
48pub struct Compiler<S: SourceProvider> {
50 source_provider: S,
51 context: Context,
52}
53
54impl<S: SourceProvider> Compiler<S> {
55 pub fn new(source_provider: S) -> Self {
57 Self {
58 source_provider,
59 context: Context::default(),
60 }
61 }
62
63 pub fn new_wasm(source_provider: S) -> Self {
70 use components::context::config::Config;
71
72 let config = Config::default().set_environment(Environment::Wasm);
73 Self {
74 source_provider,
75 context: config.to_context(),
76 }
77 }
78
79 pub fn get_context(&self) -> Context {
81 self.context.clone()
82 }
83
84 pub fn parse(&self, filename: &str) -> Result<Lang, CompileError> {
86 let source = self
87 .source_provider
88 .get_source(filename)
89 .ok_or_else(|| CompileError::FileNotFound(filename.to_string()))?;
90
91 Ok(parsing::parse_from_string(&source, filename))
92 }
93
94 pub fn type_check(&self, ast: &Lang) -> TypingResult {
96 typing_with_errors(&self.context, ast)
97 }
98
99 pub fn transpile(&self, ast: &Lang) -> TranspileResult {
101 use processes::type_checking::type_checker::TypeChecker;
102
103 let type_checker = TypeChecker::new(self.context.clone()).typing(ast);
104 let r_code = type_checker.clone().transpile();
105 let context = type_checker.get_context();
106
107 TranspileResult {
108 r_code,
109 type_annotations: context.get_type_anotations(),
110 generic_functions: context
111 .get_all_generic_functions()
112 .iter()
113 .map(|(var, _)| var.get_name())
114 .filter(|x| !x.contains("<-"))
115 .collect(),
116 }
117 }
118}
119
120#[derive(Debug, Clone)]
122pub struct TranspileResult {
123 pub r_code: String,
124 pub type_annotations: String,
125 pub generic_functions: Vec<String>,
126}
127
128#[derive(Debug, Clone)]
130pub enum CompileError {
131 FileNotFound(String),
132 TypeErrors(Vec<TypRError>),
133}
134
135impl std::fmt::Display for CompileError {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 match self {
138 CompileError::FileNotFound(name) => write!(f, "File not found: {}", name),
139 CompileError::TypeErrors(errors) => {
140 writeln!(f, "Type errors:")?;
141 for err in errors {
142 writeln!(f, " - {:?}", err)?;
143 }
144 Ok(())
145 }
146 }
147 }
148}
149
150impl std::error::Error for CompileError {}