Update api bootstrap

This commit is contained in:
2026-06-22 02:30:18 +02:00
parent d58d4c74cd
commit 77e7430776
3 changed files with 45 additions and 44 deletions
+14 -14
View File
@@ -20,12 +20,12 @@ pub struct MistDiagnosticMessage {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum MistDiagnostic { pub enum MistDiagnostic {
Error[MistDiagnosticMessage], Error(MistDiagnosticMessage),
Warning[MistDiagnosticMessage], Warning(MistDiagnosticMessage),
Rust[CompilerMessage], Rust(CompilerMessage),
} }
pub fn build(mut args Vec<String>, root PathBuf) bool { pub fn bool build(mut args Vec<String>, root PathBuf) {
args.remove(0); args.remove(0);
args.insert(1, "--message-format=json".to_string()); args.insert(1, "--message-format=json".to_string());
@@ -45,7 +45,7 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
for message in cargo_metadata::Message::parse_stream(&mut reader) { for message in cargo_metadata::Message::parse_stream(&mut reader) {
match message { match message {
Ok[Message::CompilerMessage[msg]] => { Ok(Message::CompilerMessage(msg)) => {
for span in &msg.message.spans { for span in &msg.message.spans {
if span.is_primary { if span.is_primary {
let rust_path = root.join(&span.file_name); let rust_path = root.join(&span.file_name);
@@ -65,7 +65,7 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
let mist_file = map.mist_path.strip_prefix(&root); let mist_file = map.mist_path.strip_prefix(&root);
let [_, rev_mapper::MistMap[line, column]] = let (_, rev_mapper::MistMap(line, column)) =
map.find(&rev_mapper::RustMap(span.line_end, span.column_start)) map.find(&rev_mapper::RustMap(span.line_end, span.column_start))
.expect("Unable to find mapping"); .expect("Unable to find mapping");
@@ -100,7 +100,7 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
} }
} }
Ok[Message::BuildFinished[finish]] => { Ok(Message::BuildFinished(finish)) => {
print_diagnostics(&diagnostics); print_diagnostics(&diagnostics);
let mut raw_reader = reader.into_inner(); let mut raw_reader = reader.into_inner();
@@ -115,7 +115,7 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
return finish.success; return finish.success;
} }
Ok[Message::TextLine[text]] => println!("{text}"); Ok(Message::TextLine(text)) => println!("{text}");
_ => {} _ => {}
} }
} }
@@ -123,12 +123,12 @@ pub fn build(mut args Vec<String>, root PathBuf) bool {
false false
} }
pub fn print_diagnostics(diagnostics *Vec<MistDiagnostic>) { pub fn void print_diagnostics(diagnostics *Vec<MistDiagnostic>) {
let mut files = HashMap::new(); let mut files = HashMap::new();
for diag in diagnostics { for diag in diagnostics {
match diag { match diag {
MistDiagnostic::Error[msg] => { MistDiagnostic::Error(msg) => {
let line = get_line(&mut files, &msg); let line = get_line(&mut files, &msg);
println!( println!(
@@ -141,7 +141,7 @@ pub fn print_diagnostics(diagnostics *Vec<MistDiagnostic>) {
) )
} }
MistDiagnostic::Warning[msg] => { MistDiagnostic::Warning(msg) => {
let line = get_line(&mut files, &msg); let line = get_line(&mut files, &msg);
println!( println!(
@@ -154,15 +154,15 @@ pub fn print_diagnostics(diagnostics *Vec<MistDiagnostic>) {
) )
} }
MistDiagnostic::Rust[rs] => println!("{rs}"); MistDiagnostic::Rust(rs) => println!("{rs}");
} }
} }
} }
pub fn get_line( pub fn Option<String> get_line(
files *mut HashMap<PathBuf, Vec<String>>, files *mut HashMap<PathBuf, Vec<String>>,
msg *MistDiagnosticMessage, msg *MistDiagnosticMessage,
) Option<String> { ) {
let src_path = msg.file_path.clone(); let src_path = msg.file_path.clone();
let lines = files.entry(src_path.clone()).or_insert_with(fn() { let lines = files.entry(src_path.clone()).or_insert_with(fn() {
+9 -9
View File
@@ -37,10 +37,10 @@ pub class Module {
} }
} }
fn scan_directory(*mut self, search_dir *Path, config *MistConfig) { fn void scan_directory(*mut self, search_dir *Path, config *MistConfig) {
let entries = match fs::read_dir(search_dir) { let entries = match fs::read_dir(search_dir) {
Ok[entries] => entries; Ok(entries) => entries;
Err[e] => { Err(e) => {
eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e); eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
process::exit(1); process::exit(1);
} }
@@ -48,8 +48,8 @@ pub class Module {
for entry in entries { for entry in entries {
let entry = match entry { let entry = match entry {
Ok[entry] => entry; Ok(entry) => entry;
Err[e] => { Err(e) => {
eprintln!("error: failed to read directory entry\n {}", e); eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1); process::exit(1);
} }
@@ -65,11 +65,11 @@ pub class Module {
} }
} }
pub fn is_package(*self) bool { pub fn bool is_package(*self) {
self.path.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() == Some("package.mist") self.path.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() == Some("package.mist")
} }
pub fn output_dir(*self, parent_dir *PathBuf) PathBuf { pub fn PathBuf output_dir(*self, parent_dir *PathBuf) {
if self.path.is_dir() || self.is_package() { if self.path.is_dir() || self.is_package() {
parent_dir.join(&self.name) parent_dir.join(&self.name)
} else { } else {
@@ -77,8 +77,8 @@ pub class Module {
} }
} }
pub fn output_path(*self, parent_dir *PathBuf, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) PathBuf { pub fn PathBuf output_path(*self, parent_dir *PathBuf, curr_mod_decl *Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>) {
let name = if let Some[[_, name]] = curr_mod_decl { let name = if let Some((_, name)) = curr_mod_decl {
&name.0 &name.0
} else { } else {
&self.name &self.name
+22 -21
View File
@@ -20,8 +20,9 @@ pub struct MistConfig {
pub packages Vec<PathBuf>, pub packages Vec<PathBuf>,
pub main_mods String pub main_mods String
} }
impl MistConfig { impl MistConfig {
pub fn should_ignore(*self, path *Path) bool { pub fn bool should_ignore(*self, path *Path) {
if path == self.package { if path == self.package {
return true; return true;
} }
@@ -36,7 +37,7 @@ impl MistConfig {
} }
} }
impl Default for MistConfig { impl Default for MistConfig {
fn default() Self { fn Self default() {
Self { Self {
package: PathBuf::from("main.mist"), package: PathBuf::from("main.mist"),
packages: Vec::new(), packages: Vec::new(),
@@ -45,7 +46,7 @@ impl Default for MistConfig {
} }
} }
pub fn transpile() PathBuf { pub fn PathBuf transpile() {
let start = time::Instant::now(); let start = time::Instant::now();
let root = env::current_dir().expect("Unable to find project root"); let root = env::current_dir().expect("Unable to find project root");
@@ -84,7 +85,7 @@ pub fn transpile() PathBuf {
} }
impl modules::Module { impl modules::Module {
fn transpile(*self, parent_dir *PathBuf, config *mut MistConfig) [bool, String] { fn (bool, String) transpile(*self, parent_dir *PathBuf, config *mut MistConfig) {
if !self.path.is_dir() && self.path.extension().and_then(fn(v) -> v.to_str()) != Some("mist") { if !self.path.is_dir() && self.path.extension().and_then(fn(v) -> v.to_str()) != Some("mist") {
let file_name = self.path.file_name() let file_name = self.path.file_name()
.and_then(fn(s) -> s.to_str()) .and_then(fn(s) -> s.to_str())
@@ -123,7 +124,7 @@ impl modules::Module {
self.write_transpile(output, parent_dir, child_changed) self.write_transpile(output, parent_dir, child_changed)
} }
fn write_transpile(*self, output String, parent_dir *PathBuf, child_changed bool) [bool, String] { fn (bool, String) write_transpile(*self, output String, parent_dir *PathBuf, child_changed bool) {
if self.path.is_dir() { if self.path.is_dir() {
let output_file = self.output_path(parent_dir, &None); let output_file = self.output_path(parent_dir, &None);
@@ -146,8 +147,8 @@ impl modules::Module {
(false, format!("pub mod {};\n", self.name)) (false, format!("pub mod {};\n", self.name))
} else { } else {
let source = match fs::read_to_string(&self.path) { let source = match fs::read_to_string(&self.path) {
Ok[s] => s; Ok(s) => s;
Err[e] => { Err(e) => {
eprintln!("error: failed to read file {}\n {}", self.path.display(), e); eprintln!("error: failed to read file {}\n {}", self.path.display(), e);
process::exit(1); process::exit(1);
@@ -173,7 +174,7 @@ impl modules::Module {
} }
} }
fn is_source_newer(source *Path, output *Path) std::io::Result<bool> { fn std::io::Result<bool> is_source_newer(source *Path, output *Path) {
if !output.exists() { if !output.exists() {
return Ok(true); return Ok(true);
} }
@@ -185,12 +186,12 @@ fn is_source_newer(source *Path, output *Path) std::io::Result<bool> {
} }
pub enum TranspileError<'a> { pub enum TranspileError<'a> {
Parse[error::ParseError<'a, Vec<mist_parser::ast::TopLevel>>], Parse(error::ParseError<'a, Vec<mist_parser::ast::TopLevel>>),
Semantic[Vec<mist_parser::semantics::SemanticError>] Semantic(Vec<mist_parser::semantics::SemanticError>)
} }
pub fn transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) pub fn Result<(PathBuf, mist_codegen::RustCodegen, String), TranspileError<'a>>
Result<[PathBuf, mist_codegen::RustCodegen, String], TranspileError<'a>> { transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>) {
if let Some[[_, name]] = &curr_mod_decl { if let Some[[_, name]] = &curr_mod_decl {
if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") { if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") {
@@ -222,10 +223,10 @@ pub fn transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf
Ok((output_file, gc, output)) Ok((output_file, gc, output))
} }
pub fn transpile_file(path *PathBuf, source *str, output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) { pub fn void transpile_file(path *PathBuf, source *str, output_file PathBuf, mod_decl *str, curr_mod_decl *Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>) {
let [output_file, gc, output] = match transpile_code(path, source, output_file, mod_decl, curr_mod_decl) { let (output_file, gc, output) = match transpile_code(path, source, output_file, mod_decl, curr_mod_decl) {
Ok[output] => output; Ok(output) => output;
Err[e] => { Err(e) => {
decode_err(&e, path); decode_err(&e, path);
process::exit(1); process::exit(1);
} }
@@ -258,11 +259,11 @@ pub fn transpile_file(path *PathBuf, source *str, output_file PathBuf, mod_decl
} }
} }
pub fn decode_err<'a>(err *TranspileError<'a>, path *PathBuf) { pub fn void decode_err<'a>(err *TranspileError<'a>, path *PathBuf) {
let curr_dir = std::env::current_dir().unwrap(); let curr_dir = std::env::current_dir().unwrap();
match err { match err {
TranspileError::Semantic[e_list] => { TranspileError::Semantic(e_list) => {
for e in e_list { for e in e_list {
eprintln!( eprintln!(
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}", "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}",
@@ -274,9 +275,9 @@ pub fn decode_err<'a>(err *TranspileError<'a>, path *PathBuf) {
} }
} }
TranspileError::Parse[e] => { TranspileError::Parse(e) => {
match e { match e {
error::ParseError::Ast[e] => { error::ParseError::Ast(e) => {
let start_pos = e.span.start_pos().line_col(); let start_pos = e.span.start_pos().line_col();
let span = e.span.as_str(); let span = e.span.as_str();
@@ -293,7 +294,7 @@ pub fn decode_err<'a>(err *TranspileError<'a>, path *PathBuf) {
); );
} }
error::ParseError::PreAst[e] => { error::ParseError::PreAst(e) => {
eprintln!("error: parse failed in {}\n{}", path.strip_prefix(curr_dir).unwrap_or_else(|_| path).display(), e); eprintln!("error: parse failed in {}\n{}", path.strip_prefix(curr_dir).unwrap_or_else(|_| path).display(), e);
} }
} }