diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 289bcb7..969e32f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,14 +7,14 @@ - + - - + + @@ -24,6 +24,8 @@ + + @@ -291,15 +293,7 @@ - - - - - @@ -742,7 +744,6 @@ - @@ -767,7 +768,8 @@ - diff --git a/src/root.rs b/src/root.rs index e6e7cb9..f2cb134 100644 --- a/src/root.rs +++ b/src/root.rs @@ -2,6 +2,7 @@ use crate::root::compiler::compile::compile; use crate::root::errors::{WErr, WErrContext}; use crate::root::name_resolver::resolve::resolve; use crate::root::parser::parse::parse; +use crate::root::parser::path_storage::PathStorage; use crate::root::runner::{assemble, link_gcc, run}; use crate::time; use clap::Parser; @@ -13,7 +14,6 @@ use std::fs::File; use std::io::ErrorKind; use std::path::PathBuf; use std::time::Instant; -use crate::root::parser::path_storage::PathStorage; #[cfg(debug_assertions)] pub const DEBUG_ON_ERROR: bool = false; @@ -64,10 +64,9 @@ pub fn main_args(args: Args) -> Result<(), String> { } } - print!("Parsing files... "); time!( - let mut path_storage = PathStorage::new(&args.input).unwrap(); // TODO: + let mut path_storage = PathStorage::new(&args.input).unwrap(); // TODO: let toplevel_tokens = parse(&mut path_storage) .map_err(|e| e.with_context(&path_storage).to_string())?; ); diff --git a/src/root/builtin/core/referencing.rs b/src/root/builtin/core/referencing.rs index 8f99e2a..2b313cf 100644 --- a/src/root/builtin/core/referencing.rs +++ b/src/root/builtin/core/referencing.rs @@ -12,8 +12,7 @@ pub fn set_reference( into: AddressedTypeRef, global_table: &GlobalDefinitionTable, ) -> Result { - let new_type = to_ref - .type_ref().plus_one_indirect(); + let new_type = to_ref.type_ref().plus_one_indirect(); if new_type != *into.type_ref() { return WErr::ne( EvalErrs::OpWrongReturnType( diff --git a/src/root/compiler/compile.rs b/src/root/compiler/compile.rs index e4705d0..0712322 100644 --- a/src/root/compiler/compile.rs +++ b/src/root/compiler/compile.rs @@ -16,7 +16,7 @@ use crate::root::unrandom::{new_hashmap, new_hashset}; pub fn compile( mut global_table: GlobalDefinitionTable, unprocessed_functions: HashMap, - path_storage: &PathStorage + path_storage: &PathStorage, ) -> Result { let mut unprocessed_functions = unprocessed_functions; let mut compiled_functions = new_hashmap(); diff --git a/src/root/compiler/compile_function.rs b/src/root/compiler/compile_function.rs index 84fc6ab..73db8aa 100644 --- a/src/root/compiler/compile_function.rs +++ b/src/root/compiler/compile_function.rs @@ -132,8 +132,10 @@ fn recursively_compile_lines( )?); } LineTokens::If(if_token) => { - let condition_addr = global_table - .add_local_variable_unnamed_base(BoolType::id().immediate_single(), local_variables); + let condition_addr = global_table.add_local_variable_unnamed_base( + BoolType::id().immediate_single(), + local_variables, + ); contents.other(&compile_evaluable_into( fid, if_token.if_condition(), @@ -225,8 +227,10 @@ fn recursively_compile_lines( contents.line(&format!("{start_tag}:")); - let condition_addr = global_table - .add_local_variable_unnamed_base(BoolType::id().immediate_single(), local_variables); + let condition_addr = global_table.add_local_variable_unnamed_base( + BoolType::id().immediate_single(), + local_variables, + ); contents.other(&compile_evaluable_into( fid, while_token.condition(), diff --git a/src/root/compiler/compile_function_call.rs b/src/root/compiler/compile_function_call.rs index 625774c..1d5012f 100644 --- a/src/root/compiler/compile_function_call.rs +++ b/src/root/compiler/compile_function_call.rs @@ -116,8 +116,9 @@ pub fn call_function( if into.type_ref().type_id() != signature_args[i].type_id() { return WErr::ne( EvalErrs::ExpectedDifferentType( - global_table - .get_type_name(&into.type_ref().type_id().immediate_single()), + global_table.get_type_name( + &into.type_ref().type_id().immediate_single(), + ), global_table.get_type_name( &signature_args[i].type_id().immediate_single(), ), @@ -228,8 +229,9 @@ pub fn call_function( if into.type_ref().type_id() != signature_args[i].type_id() { return WErr::ne( EvalErrs::ExpectedDifferentType( - global_table - .get_type_name(&into.type_ref().type_id().immediate_single()), + global_table.get_type_name( + &into.type_ref().type_id().immediate_single(), + ), global_table.get_type_name( &signature_args[i].type_id().immediate_single(), ), diff --git a/src/root/compiler/global_tracker.rs b/src/root/compiler/global_tracker.rs index b350e7c..ca1cbfe 100644 --- a/src/root/compiler/global_tracker.rs +++ b/src/root/compiler/global_tracker.rs @@ -1,7 +1,7 @@ +use crate::root::parser::path_storage::PathStorage; use crate::root::shared::common::FunctionID; use derive_getters::{Dissolve, Getters}; use std::collections::HashSet; -use crate::root::parser::path_storage::PathStorage; /// Tracks data between function compilations #[derive(Dissolve, Getters)] @@ -23,7 +23,7 @@ impl<'a> GlobalTracker<'a> { unique_tag_counter: 0, } } - + pub fn functions_mut(&mut self) -> &mut HashSet { &mut self.function_calls } diff --git a/src/root/errors/mod.rs b/src/root/errors/mod.rs index 46091f8..0c6543f 100644 --- a/src/root/errors/mod.rs +++ b/src/root/errors/mod.rs @@ -1,17 +1,16 @@ use crate::root::parser::location::{ErrorL, Location, LocationTyped}; +use crate::root::parser::path_storage::PathStorage; #[cfg(debug_assertions)] use crate::root::DEBUG_ON_ERROR; use color_print::cformat; #[cfg(debug_assertions)] use std::backtrace::Backtrace; use std::fmt::{Display, Formatter}; -use crate::root::parser::path_storage::PathStorage; pub mod evaluable_errors; pub mod name_resolver_errors; pub mod parser_errors; - /// Universal error for Whython-8 #[derive(Debug)] pub struct WErr { @@ -77,7 +76,11 @@ impl WErr { fn fmt(&self, f: &mut Formatter<'_>, path_storage: &PathStorage) -> std::fmt::Result { let text = if let Some(location) = &self.location { - cformat!("Error:\n {}\n{}\n", self.error, location.with_context(path_storage)) + cformat!( + "Error:\n {}\n{}\n", + self.error, + location.with_context(path_storage) + ) } else { cformat!("Error:\n {}", self.error) }; @@ -87,11 +90,11 @@ impl WErr { pub struct WErrContext<'a> { err: &'a WErr, - path_storage: &'a PathStorage + path_storage: &'a PathStorage, } impl<'a> Display for WErrContext<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.err.fmt(f, self.path_storage) } -} \ No newline at end of file +} diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs index 05cdeef..01b5da0 100644 --- a/src/root/name_resolver/name_resolvers.rs +++ b/src/root/name_resolver/name_resolvers.rs @@ -13,13 +13,13 @@ use crate::root::parser::parse_function::parse_operator::{OperatorToken, PrefixO use crate::root::parser::parse_function::FunctionToken; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_struct::StructToken; +use crate::root::parser::path_storage::FileID; use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID, TypeID, TypeRef}; use crate::root::shared::types::Type; use crate::root::POINTER_SIZE; use std::collections::HashMap; use std::path::PathBuf; use std::rc::Rc; -use crate::root::parser::path_storage::FileID; #[derive(Debug)] enum NameTreeEntry { @@ -235,10 +235,7 @@ impl GlobalDefinitionTable { } /// Takes a name and resolves it to a type (or error) - pub fn resolve_to_type_ref( - &mut self, - name: &UnresolvedTypeRefToken, - ) -> Result { + pub fn resolve_to_type_ref(&mut self, name: &UnresolvedTypeRefToken) -> Result { let (indirection, full_name) = (name.indirection(), name.inner()); fn find_error_point(name: &FullNameToken, prev_location: &Location) -> Location { diff --git a/src/root/parser/handle_errors.rs b/src/root/parser/handle_errors.rs index d378c9d..a766008 100644 --- a/src/root/parser/handle_errors.rs +++ b/src/root/parser/handle_errors.rs @@ -6,7 +6,10 @@ use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult}; use crate::root::parser::path_storage::PathStorage; -pub fn handle_error(res: ParseResult, path_storage: &PathStorage) -> Result<(A, B), WErr> { +pub fn handle_error( + res: ParseResult, + path_storage: &PathStorage, +) -> Result<(A, B), WErr> { match res { Ok(v) => Ok(v), Err(e) => match &e { @@ -34,7 +37,11 @@ fn handle_error_tree(e: &ErrorTree, path_storage: &PathStorage) -> WErr { }, ErrorTree::Stack { base, contexts } => { let mut sb = "Base Error:\n".to_string(); - for l in handle_error_tree(base, path_storage).with_context(path_storage).to_string().lines() { + for l in handle_error_tree(base, path_storage) + .with_context(path_storage) + .to_string() + .lines() + { sb += " "; sb += l; sb += "\n"; @@ -48,7 +55,11 @@ fn handle_error_tree(e: &ErrorTree, path_storage: &PathStorage) -> WErr { StackContext::Context(c) => c.to_string(), }; - for l in WErr::n(e, Location::from_span(s)).with_context(path_storage).to_string().lines() { + for l in WErr::n(e, Location::from_span(s)) + .with_context(path_storage) + .to_string() + .lines() + { sb += " "; sb += l; sb += "\n"; @@ -63,7 +74,9 @@ fn handle_error_tree(e: &ErrorTree, path_storage: &PathStorage) -> WErr { for (i, e) in z.iter().enumerate() { sb += &format!("{}:\n", i + 1); - let werr = handle_error_tree(e, path_storage).with_context(path_storage).to_string(); + let werr = handle_error_tree(e, path_storage) + .with_context(path_storage) + .to_string(); for line in werr.lines() { sb += " "; sb += line; diff --git a/src/root/parser/location.rs b/src/root/parser/location.rs index df0f3fc..7a1ed90 100644 --- a/src/root/parser/location.rs +++ b/src/root/parser/location.rs @@ -1,4 +1,5 @@ use crate::root::parser::parse::Span; +use crate::root::parser::path_storage::{FileID, PathStorage}; use color_print::cformat; use nom::InputTake; use std::cmp::min; @@ -7,7 +8,6 @@ use std::fs; use std::marker::PhantomData; use std::path::PathBuf; use std::rc::Rc; -use crate::root::parser::path_storage::{FileID, PathStorage}; pub enum ToLocation<'a> { Location(Location), @@ -104,10 +104,13 @@ impl LocationTyped { } } - pub fn with_context<'a>(&'a self, path_storage: &'a PathStorage) -> LocationContext<'a, ErrorType> { + pub fn with_context<'a>( + &'a self, + path_storage: &'a PathStorage, + ) -> LocationContext<'a, ErrorType> { LocationContext { location: self, - path_storage + path_storage, } } @@ -129,7 +132,12 @@ impl LocationTyped { !matches!(self.inner_location, ErrorLocation::None) } - fn fmt_choice(&self, f: &mut Formatter<'_>, is_warning: bool, path_storage: &PathStorage) -> std::fmt::Result { + fn fmt_choice( + &self, + f: &mut Formatter<'_>, + is_warning: bool, + path_storage: &PathStorage, + ) -> std::fmt::Result { let location = match &self.inner_location { ErrorLocation::Builtin => { writeln!(f, "{}", cformat!("Builtin Definition"))?; @@ -306,7 +314,7 @@ impl LocationFilledFmt for LocationTyped { pub struct LocationContext<'a, ErrorType> { location: &'a LocationTyped, - path_storage: &'a PathStorage + path_storage: &'a PathStorage, } impl<'a> Display for LocationContext<'a, ErrorL> { @@ -319,4 +327,4 @@ impl<'a> Display for LocationContext<'a, WarningL> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.location.fmt(f, self.path_storage) } -} \ No newline at end of file +} diff --git a/src/root/parser/mod.rs b/src/root/parser/mod.rs index 8f20d65..79b242d 100644 --- a/src/root/parser/mod.rs +++ b/src/root/parser/mod.rs @@ -12,6 +12,6 @@ pub mod parse_parameters; pub mod parse_struct; pub mod parse_toplevel; pub mod parse_util; +pub mod path_storage; pub mod soft_alt; mod use_parser; -pub mod path_storage; diff --git a/src/root/parser/path_storage.rs b/src/root/parser/path_storage.rs index 27f1ff2..0f46e00 100644 --- a/src/root/parser/path_storage.rs +++ b/src/root/parser/path_storage.rs @@ -1,17 +1,19 @@ -use std::collections::{HashMap, HashSet}; -use std::fmt::{Display, Formatter}; -use nom::character::complete::anychar; use crate::root::errors::parser_errors::{create_custom_error, create_custom_error_tree}; use crate::root::errors::WErr; use crate::root::parser::location::{Location, LocationFilledFmt}; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::utils::identify_first_last::{IdentifyFirstLast, IdentifyLast}; +use nom::character::complete::anychar; +use std::collections::{HashMap, HashSet}; +use std::fmt::{Display, Formatter}; #[derive(Hash, Copy, Clone, Debug, Eq, PartialEq)] pub struct FileID(usize); impl FileID { - pub fn main_file() -> FileID { FileID(0) } + pub fn main_file() -> FileID { + FileID(0) + } } #[derive(Hash, Copy, Clone, Debug, Eq, PartialEq)] @@ -21,7 +23,7 @@ struct CodeFolder { parent: FolderID, child_folders: HashMap, child_files: HashMap, - current: String + current: String, } impl CodeFolder { @@ -35,8 +37,6 @@ impl CodeFolder { } } - - struct CodeFile { parent: FolderID, current: String, @@ -46,7 +46,7 @@ struct CodeFile { pub struct PathStorage { folders: Vec, - files: Vec + files: Vec, } impl PathStorage { @@ -54,18 +54,17 @@ impl PathStorage { // TODO: Only allow certain characters in base let mut folders = vec![CodeFolder::root()]; let mut files = Vec::new(); - + let mut current = FolderID(0); for (is_last, section) in base.split('/').identify_last() { if is_last { - files.push(CodeFile { - parent: current, + files.push(CodeFile { + parent: current, current: section.to_string(), use_files: vec![], use_folders: vec![], }); - } - else { + } else { folders.push(CodeFolder { parent: current, child_folders: Default::default(), @@ -75,11 +74,8 @@ impl PathStorage { current = FolderID(folders.len() - 1); } } - - Ok(PathStorage { - folders, - files, - }) + + Ok(PathStorage { folders, files }) } fn get_file(&self, file_id: FileID) -> &CodeFile { @@ -117,8 +113,11 @@ impl PathStorage { } sb } - - pub fn get_file_path_id_checked<'a>(&mut self, path: Span<'a>) -> ParseResult<(), (FileID, bool), ErrorTree<'a>> { + + pub fn get_file_path_id_checked<'a>( + &mut self, + path: Span<'a>, + ) -> ParseResult<(), (FileID, bool), ErrorTree<'a>> { let mut path_rem = path; let mut last_dot = false; while let Ok((rem, c)) = anychar::<_, ErrorTree>(path_rem) { @@ -138,14 +137,13 @@ impl PathStorage { last_dot = true; continue; } - + let mut utf8 = [0u8; 4]; c.encode_utf8(&mut utf8); let mut utf8_str = "[".to_string(); utf8_str += &utf8.map(|b| format!("{b:02X}")).join(", "); utf8_str.push(']'); - return Err(create_custom_error( format!("Invalid character in path '{}' - UTF-8 bytes: {}. Allowed characters are alphanumerics, '_' and '/'", c, utf8_str), path_rem, @@ -159,13 +157,19 @@ impl PathStorage { return if let Some(id) = self.get_folder(current).child_files.get(section) { Ok(((), (*id, false))) } else { - self.files.push(CodeFile { parent: current, current: section.to_string(), use_files: vec![], use_folders: vec![] }); + self.files.push(CodeFile { + parent: current, + current: section.to_string(), + use_files: vec![], + use_folders: vec![], + }); let id = FileID(self.files.len() - 1); - self.get_folder_mut(current).child_files.insert(section.to_string(), id); + self.get_folder_mut(current) + .child_files + .insert(section.to_string(), id); Ok(((), (id, true))) }; - } - else { + } else { current = if let Some(id) = self.get_folder(current).child_folders.get(section) { *id } else { @@ -176,7 +180,9 @@ impl PathStorage { current: section.to_string(), }); let id = FolderID(self.folders.len() - 1); - self.get_folder_mut(current).child_folders.insert(section.to_string(), id); + self.get_folder_mut(current) + .child_folders + .insert(section.to_string(), id); id }; } @@ -184,4 +190,4 @@ impl PathStorage { panic!() } -} \ No newline at end of file +} diff --git a/src/root/parser/use_parser.rs b/src/root/parser/use_parser.rs index 5e8b025..37294ae 100644 --- a/src/root/parser/use_parser.rs +++ b/src/root/parser/use_parser.rs @@ -9,7 +9,10 @@ use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_util::discard_ignored; use crate::root::parser::path_storage::{FileID, PathStorage}; -pub fn parse_uses<'a>(s: Span<'a>, path_storage: &mut PathStorage) -> ParseResult<'a, Span<'a>, Vec<(FileID, Location)>> { +pub fn parse_uses<'a>( + s: Span<'a>, + path_storage: &mut PathStorage, +) -> ParseResult<'a, Span<'a>, Vec<(FileID, Location)>> { let mut s = s; let mut found_paths = Vec::new(); loop { @@ -37,11 +40,11 @@ pub fn parse_uses<'a>(s: Span<'a>, path_storage: &mut PathStorage) -> ParseResul } let (_, (id, new)) = path_storage.get_file_path_id_checked(path)?; - + if new { found_paths.push((id, Location::from_span(&path))); } - + s = ns; } } diff --git a/src/root/shared/common.rs b/src/root/shared/common.rs index 039cd7c..caa200c 100644 --- a/src/root/shared/common.rs +++ b/src/root/shared/common.rs @@ -60,7 +60,7 @@ impl Indirection { pub fn has_indirection(&self) -> bool { self.0 != 0 } - + pub fn plus(&self, amount: usize) -> Indirection { Indirection(self.0 + amount) } @@ -107,17 +107,19 @@ impl TypeRef { indirection, } } - - pub fn is_array(&self) -> bool { self.elements == 1 } + + pub fn is_array(&self) -> bool { + self.elements == 1 + } pub fn with_indirection(&self, indirection: Indirection) -> TypeRef { TypeRef { type_id: self.type_id, elements: self.elements, - indirection + indirection, } } - + pub fn plus_one_indirect(&self) -> TypeRef { TypeRef { type_id: self.type_id, @@ -133,7 +135,7 @@ impl TypeRef { indirection: Indirection(self.indirection.0 - 1), } } - + pub fn immediate(&self) -> TypeRef { TypeRef { type_id: self.type_id, diff --git a/src/root/utils/identify_first_last.rs b/src/root/utils/identify_first_last.rs index bc3d0c1..4b3cdec 100644 --- a/src/root/utils/identify_first_last.rs +++ b/src/root/utils/identify_first_last.rs @@ -2,7 +2,7 @@ // ! Specifically from users @Nemo157 and @reu //! This module contains general helper traits. -use std::{ iter, mem }; +use std::{iter, mem}; pub trait IdentifyFirstLast: Iterator + Sized { fn identify_first_last(self) -> IterFirstLast; @@ -16,32 +16,49 @@ pub trait IdentifyLast: Iterator + Sized { fn identify_last(self) -> IterLast; } - -impl IdentifyFirstLast for I where I: Iterator { +impl IdentifyFirstLast for I +where + I: Iterator, +{ fn identify_first_last(self) -> IterFirstLast { IterFirstLast(true, self.peekable()) } } -impl IdentifyFirst for I where I: Iterator { +impl IdentifyFirst for I +where + I: Iterator, +{ fn identify_first(self) -> IterFirst { IterFirst(true, self) } } -impl IdentifyLast for I where I: Iterator { +impl IdentifyLast for I +where + I: Iterator, +{ fn identify_last(self) -> IterLast { IterLast(true, self.peekable()) } } -pub struct IterFirstLast(bool, iter::Peekable) where I: Iterator; +pub struct IterFirstLast(bool, iter::Peekable) +where + I: Iterator; -pub struct IterFirst(bool, I) where I: Iterator; +pub struct IterFirst(bool, I) +where + I: Iterator; -pub struct IterLast(bool, iter::Peekable) where I: Iterator; +pub struct IterLast(bool, iter::Peekable) +where + I: Iterator; -impl Iterator for IterFirstLast where I: Iterator { +impl Iterator for IterFirstLast +where + I: Iterator, +{ type Item = (bool, bool, I::Item); fn next(&mut self) -> Option { @@ -50,7 +67,10 @@ impl Iterator for IterFirstLast where I: Iterator { } } -impl Iterator for IterFirst where I: Iterator { +impl Iterator for IterFirst +where + I: Iterator, +{ type Item = (bool, I::Item); fn next(&mut self) -> Option { @@ -59,10 +79,13 @@ impl Iterator for IterFirst where I: Iterator { } } -impl Iterator for IterLast where I: Iterator { +impl Iterator for IterLast +where + I: Iterator, +{ type Item = (bool, I::Item); fn next(&mut self) -> Option { self.1.next().map(|e| (self.1.peek().is_none(), e)) } -} \ No newline at end of file +}