From a072c6e81c1091b995ab0d7dda77e8cc5c7c5f5e Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas <100799838+Robert-M-Lucas@users.noreply.github.com> Date: Sat, 3 Aug 2024 00:22:31 +0100 Subject: [PATCH 1/4] Refactoring --- .idea/workspace.xml | 133 ++++++------------ src/root.rs | 19 +-- src/root/name_resolver/resolve_names.rs | 4 +- .../parser/parse_function/parse_evaluable.rs | 2 +- 4 files changed, 49 insertions(+), 109 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a09d2e1..68af405 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,64 +7,11 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { + "keyToString": { + "Cargo.Build `Run whython-8`.executor": "Run", + "Cargo.Run whython-8.executor": "Run", + "Cargo.Test whython-8.executor": "Run", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.rust.reset.selective.auto.import": "true", + "Shell Script.Test Asm.executor": "Run", + "git-widget-placeholder": "dev", + "ignore.virus.scanning.warn.message": "true", + "last_opened_file_path": "/home/robertlucas/RustroverProjects/whython-8", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "org.rust.cargo.project.model.PROJECT_DISCOVERY": "true", + "org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon": "", + "org.rust.first.attach.projects": "true", + "settings.editor.selected.configurable": "language.rust.cargo.check", + "vue.rearranger.settings.migration": "true" }, - "keyToStringList": { - "com.intellij.ide.scratch.ScratchImplUtil$2/New Scratch File": [ - "Rust" + "keyToStringList": { + "com.intellij.ide.scratch.ScratchImplUtil$2/New Scratch File": [ + "Rust" ] } -}]]> +} @@ -325,15 +272,9 @@ - - - - - @@ -795,13 +744,13 @@ - - diff --git a/src/root.rs b/src/root.rs index 58c1691..cef8d05 100644 --- a/src/root.rs +++ b/src/root.rs @@ -1,6 +1,4 @@ use crate::root::parser::parse::parse; -// use crate::root::assembler::assemble::generate_assembly; -// use crate::root::name_resolver::processor::process; use crate::root::compiler::compile::compile; use crate::root::errors::WErr; use crate::root::name_resolver::resolve::resolve; @@ -19,17 +17,6 @@ use std::time::Instant; #[cfg(debug_assertions)] pub const DEBUG_ON_ERROR: bool = false; -// #[cfg(target_os = "windows")] -// use crate::root::runner::run; -// #[cfg(target_os = "windows")] -// use runner::link; -// -// #[cfg(target_os = "linux")] -// use crate::root::runner::run_wine_experimental; -// #[cfg(target_os = "linux")] -// use runner::link_gcc_experimental; -// use crate::root::parser::parse::parse; - pub mod assembler; pub mod builtin; pub mod compiler; @@ -108,13 +95,13 @@ pub fn main_args(args: Args) -> Result<(), WErr> { let t = Instant::now(); link_gcc(&args.output).unwrap(); let end = t.elapsed(); - // TODO: Don't unwrap let size = File::open(format!("{}.out", args.output)) .unwrap() .metadata() .unwrap() .len() .to_formatted_string(&Locale::en); + cprintln!("Completed [{:?}] - {} bytes", end, size); if args.build { @@ -141,6 +128,10 @@ pub fn main_args(args: Args) -> Result<(), WErr> { run(&args.output); } } + #[cfg(not(target_os = "linux"))] + { + println!("Not linking / executing as OS is not Linux"); + } cprintln!("Done!"); Ok(()) diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index 8a3bb74..f3d7e92 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; - +use b_box::b; use crate::root::errors::evaluable_errors::EvalErrs; use crate::root::errors::name_resolver_errors::NRErrs; use crate::root::errors::WErr; @@ -187,7 +187,7 @@ pub fn resolve_names( } for (id, user_type) in final_types { - global_table.add_user_type(id, Box::new(user_type)); + global_table.add_user_type(id, b!(user_type)); } // (final_types, type_names, unprocessed_functions) diff --git a/src/root/parser/parse_function/parse_evaluable.rs b/src/root/parser/parse_function/parse_evaluable.rs index d1f1e2e..77b80d0 100644 --- a/src/root/parser/parse_function/parse_evaluable.rs +++ b/src/root/parser/parse_function/parse_evaluable.rs @@ -517,7 +517,7 @@ pub fn parse_evaluable<'a, 'b>( Ok(( remaining, - TempOperation::Prefix(operator.clone(), Box::new(operand)), + TempOperation::Prefix(operator.clone(), b!(operand)), )) } From 6f261bb2e4cc6f0eca3cd8b548cc7ce5ceaa300d Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas <100799838+Robert-M-Lucas@users.noreply.github.com> Date: Tue, 6 Aug 2024 15:42:01 +0100 Subject: [PATCH 2/4] location.rs refactor --- .../shelved.patch | 141 +++++++++ ...heckout_at_06_08_2024__15_37__Changes_.xml | 4 + .../shelved.patch | 96 ++++++ .../shelved.patch | 77 ----- ..._Update_at_27_07_2024__23_06__Changes_.xml | 4 - linked_list.why | 14 - main.why | 2 + src/root/builtin/core/referencing.rs | 2 +- src/root/compiler/compile_function_call.rs | 2 +- src/root/compiler/evaluation/coerce_self.rs | 2 +- src/root/compiler/evaluation/into.rs | 2 +- src/root/compiler/evaluation/new.rs | 2 +- src/root/errors/mod.rs | 2 +- src/root/name_resolver/name_resolvers.rs | 2 +- src/root/name_resolver/resolve_names.rs | 2 +- src/root/name_resolver/resolve_type_sizes.rs | 2 +- src/root/parser/handle_errors.rs | 3 +- src/root/parser/location.rs | 274 +++++++++++++++++ src/root/parser/mod.rs | 2 + src/root/parser/parse.rs | 275 +----------------- src/root/parser/parse_function.rs | 13 +- src/root/parser/parse_function/parse_break.rs | 4 +- .../parser/parse_function/parse_evaluable.rs | 9 +- src/root/parser/parse_function/parse_if.rs | 10 +- .../parse_function/parse_initialisation.rs | 5 +- .../parser/parse_function/parse_literal.rs | 3 +- .../parser/parse_function/parse_operator.rs | 3 +- .../parser/parse_function/parse_return.rs | 6 +- .../parse_function/parse_struct_init.rs | 8 +- src/root/parser/parse_function/parse_while.rs | 10 +- src/root/parser/parse_impl.rs | 9 +- src/root/parser/parse_name.rs | 3 +- src/root/parser/parse_struct.rs | 9 +- src/root/parser/use_parser.rs | 0 src/root/shared/types.rs | 2 +- todo.md | 4 - 36 files changed, 582 insertions(+), 426 deletions(-) create mode 100644 .idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024,_15_37_[Changes]/shelved.patch create mode 100644 .idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024__15_37__Changes_.xml create mode 100644 .idea/shelf/Uncommitted_changes_before_Update_at_06_08_2024,_15_37_[Changes]/shelved.patch delete mode 100644 .idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch delete mode 100644 .idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml create mode 100644 src/root/parser/location.rs create mode 100644 src/root/parser/use_parser.rs diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024,_15_37_[Changes]/shelved.patch b/.idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024,_15_37_[Changes]/shelved.patch new file mode 100644 index 0000000..9f1e4c7 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024,_15_37_[Changes]/shelved.patch @@ -0,0 +1,141 @@ +Index: .idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch +=================================================================== +diff --git a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch b/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch +deleted file mode 100644 +--- a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch (revision a072c6e81c1091b995ab0d7dda77e8cc5c7c5f5e) ++++ /dev/null (revision a072c6e81c1091b995ab0d7dda77e8cc5c7c5f5e) +@@ -1,77 +0,0 @@ +-Index: .idea/workspace.xml +-IDEA additional info: +-Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP +-<+>\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n {\n "lastFilter": {\n "state": "OPEN",\n "assignee": "Robert-M-Lucas"\n }\n}\n \n \n {\n "selectedUrlAndAccountId": {\n "url": "https://github.com/Robert-M-Lucas/whython-8.git",\n "accountId": "dcb8df19-0b39-47e1-9073-4a54f7034be8"\n }\n}\n \n \n \n \n {\n "customColor": "",\n "associatedIndex": 4\n}\n \n \n \n {\n "keyToString": {\n "Cargo.Build `Run whython-8`.executor": "Run",\n "Cargo.Run whython-8.executor": "Run",\n "Cargo.Test whython-8.executor": "Run",\n "RunOnceActivity.OpenProjectViewOnStart": "true",\n "RunOnceActivity.ShowReadmeOnStart": "true",\n "RunOnceActivity.rust.reset.selective.auto.import": "true",\n "Shell Script.Test Asm.executor": "Run",\n "git-widget-placeholder": "master",\n "ignore.virus.scanning.warn.message": "true",\n "last_opened_file_path": "/home/robertlucas/RustroverProjects/trace",\n "node.js.detected.package.eslint": "true",\n "node.js.detected.package.tslint": "true",\n "node.js.selected.package.eslint": "(autodetect)",\n "node.js.selected.package.tslint": "(autodetect)",\n "nodejs_package_manager_path": "npm",\n "org.rust.cargo.project.model.PROJECT_DISCOVERY": "true",\n "org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon": "",\n "org.rust.first.attach.projects": "true",\n "settings.editor.selected.configurable": "preferences.pluginManager",\n "vue.rearranger.settings.migration": "true"\n },\n "keyToStringList": {\n "com.intellij.ide.scratch.ScratchImplUtil$2/New Scratch File": [\n "Rust"\n ]\n }\n}\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n 1714088144106\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/.idea/workspace.xml b/.idea/workspace.xml +--- a/.idea/workspace.xml (revision a072c6e81c1091b995ab0d7dda77e8cc5c7c5f5e) ++++ b/.idea/workspace.xml (date 1722642752537) +@@ -7,12 +7,7 @@ + + + +- +- +- +- +- +- ++ + + diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024__15_37__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024__15_37__Changes_.xml new file mode 100644 index 0000000..89ae3b6 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Checkout_at_06_08_2024__15_37__Changes_.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/.idea/shelf/Uncommitted_changes_before_Update_at_06_08_2024,_15_37_[Changes]/shelved.patch b/.idea/shelf/Uncommitted_changes_before_Update_at_06_08_2024,_15_37_[Changes]/shelved.patch new file mode 100644 index 0000000..8dd88a6 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Update_at_06_08_2024,_15_37_[Changes]/shelved.patch @@ -0,0 +1,96 @@ +Index: .idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml +=================================================================== +diff --git a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml +deleted file mode 100644 +--- a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml (revision d7abd142d3530f02bec11beab0cb81b23b907ad7) ++++ /dev/null (revision d7abd142d3530f02bec11beab0cb81b23b907ad7) +@@ -1,4 +0,0 @@ +- +- +\ No newline at end of file +Index: .idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch +=================================================================== +diff --git a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch b/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch +deleted file mode 100644 +--- a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024,_23_06_[Changes]/shelved.patch (revision d7abd142d3530f02bec11beab0cb81b23b907ad7) ++++ /dev/null (revision d7abd142d3530f02bec11beab0cb81b23b907ad7) +@@ -1,77 +0,0 @@ +-Index: .idea/workspace.xml +-IDEA additional info: +-Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP +-<+>\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n {\n "lastFilter": {\n "state": "OPEN",\n "assignee": "Robert-M-Lucas"\n }\n}\n \n \n {\n "selectedUrlAndAccountId": {\n "url": "https://github.com/Robert-M-Lucas/whython-8.git",\n "accountId": "dcb8df19-0b39-47e1-9073-4a54f7034be8"\n }\n}\n \n \n \n \n {\n "customColor": "",\n "associatedIndex": 4\n}\n \n \n \n {\n "keyToString": {\n "Cargo.Build `Run whython-8`.executor": "Run",\n "Cargo.Run whython-8.executor": "Run",\n "Cargo.Test whython-8.executor": "Run",\n "RunOnceActivity.OpenProjectViewOnStart": "true",\n "RunOnceActivity.ShowReadmeOnStart": "true",\n "RunOnceActivity.rust.reset.selective.auto.import": "true",\n "Shell Script.Test Asm.executor": "Run",\n "git-widget-placeholder": "master",\n "ignore.virus.scanning.warn.message": "true",\n "last_opened_file_path": "/home/robertlucas/RustroverProjects/trace",\n "node.js.detected.package.eslint": "true",\n "node.js.detected.package.tslint": "true",\n "node.js.selected.package.eslint": "(autodetect)",\n "node.js.selected.package.tslint": "(autodetect)",\n "nodejs_package_manager_path": "npm",\n "org.rust.cargo.project.model.PROJECT_DISCOVERY": "true",\n "org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon": "",\n "org.rust.first.attach.projects": "true",\n "settings.editor.selected.configurable": "preferences.pluginManager",\n "vue.rearranger.settings.migration": "true"\n },\n "keyToStringList": {\n "com.intellij.ide.scratch.ScratchImplUtil$2/New Scratch File": [\n "Rust"\n ]\n }\n}\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n 1714088144106\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/.idea/workspace.xml b/.idea/workspace.xml ---- a/.idea/workspace.xml (revision f39ebc39f3c1966f09766fcb00b4c79886954a55) -+++ b/.idea/workspace.xml (date 1722117982574) -@@ -7,11 +7,8 @@ - - - -- -- -- -- -- -+ -+ - - - -@@ -640,7 +638,6 @@ - - - -- - - - -@@ -665,7 +662,8 @@ - - - -- - - diff --git a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml deleted file mode 100644 index 1d07340..0000000 --- a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml +++ /dev/null @@ -1,4 +0,0 @@ - - \ No newline at end of file diff --git a/linked_list.why b/linked_list.why index d32861d..93fc87a 100644 --- a/linked_list.why +++ b/linked_list.why @@ -51,18 +51,4 @@ impl Node { self.next.print(); }; } -} - -fn main() -> int { - let r: LL = LL::new(); - r.add(2); - r.add(3); - r.add(2); - r.add(4); - r.add(7); - r.add(8); - r.add(4); - r.print(); - - return 0; } \ No newline at end of file diff --git a/main.why b/main.why index 47a811a..c328c4b 100644 --- a/main.why +++ b/main.why @@ -1,3 +1,5 @@ +use linked_list; + struct Test { a: int } diff --git a/src/root/builtin/core/referencing.rs b/src/root/builtin/core/referencing.rs index 49d4132..1f346ce 100644 --- a/src/root/builtin/core/referencing.rs +++ b/src/root/builtin/core/referencing.rs @@ -2,7 +2,7 @@ use crate::root::compiler::assembly::utils::copy_from_indirect; use crate::root::errors::evaluable_errors::EvalErrs; use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::shared::common::{AddressedTypeRef, Indirection}; /// Sets `into` to the address of `to_ref` diff --git a/src/root/compiler/compile_function_call.rs b/src/root/compiler/compile_function_call.rs index 01feeb0..2330501 100644 --- a/src/root/compiler/compile_function_call.rs +++ b/src/root/compiler/compile_function_call.rs @@ -9,7 +9,7 @@ use crate::root::compiler::local_variable_table::LocalVariableTable; use crate::root::errors::evaluable_errors::EvalErrs; use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_function::parse_evaluable::EvaluableToken; use crate::root::parser::parse_parameters::SelfType; use crate::root::shared::common::{AddressedTypeRef, ByteSize, FunctionID}; diff --git a/src/root/compiler/evaluation/coerce_self.rs b/src/root/compiler/evaluation/coerce_self.rs index 8a9b70f..50f5970 100644 --- a/src/root/compiler/evaluation/coerce_self.rs +++ b/src/root/compiler/evaluation/coerce_self.rs @@ -3,7 +3,7 @@ use crate::root::compiler::assembly::utils::copy_from_indirect; use crate::root::compiler::local_variable_table::LocalVariableTable; use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_parameters::SelfType; use crate::root::shared::common::{AddressedTypeRef, Indirection}; diff --git a/src/root/compiler/evaluation/into.rs b/src/root/compiler/evaluation/into.rs index 043cc2c..a2fedaf 100644 --- a/src/root/compiler/evaluation/into.rs +++ b/src/root/compiler/evaluation/into.rs @@ -16,7 +16,7 @@ use crate::root::errors::evaluable_errors::EvalErrs::WrongAttributeNameInInit; use crate::root::errors::name_resolver_errors::NRErrs; use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResult}; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens}; use crate::root::parser::parse_function::parse_operator::{OperatorTokens, PrefixOrInfixEx}; use crate::root::parser::parse_parameters::SelfType; diff --git a/src/root/compiler/evaluation/new.rs b/src/root/compiler/evaluation/new.rs index 6b8773e..668bcf1 100644 --- a/src/root/compiler/evaluation/new.rs +++ b/src/root/compiler/evaluation/new.rs @@ -16,7 +16,7 @@ use crate::root::errors::evaluable_errors::EvalErrs::{ExpectedNotNone, WrongAttr use crate::root::errors::name_resolver_errors::NRErrs; use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, NameResult}; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens}; use crate::root::parser::parse_function::parse_operator::{OperatorTokens, PrefixOrInfixEx}; use crate::root::parser::parse_parameters::SelfType; diff --git a/src/root/errors/mod.rs b/src/root/errors/mod.rs index 2392bb2..1000c17 100644 --- a/src/root/errors/mod.rs +++ b/src/root/errors/mod.rs @@ -1,4 +1,4 @@ -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; #[cfg(debug_assertions)] use crate::root::DEBUG_ON_ERROR; use color_print::cformat; diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs index dbca7cf..7809a82 100644 --- a/src/root/name_resolver/name_resolvers.rs +++ b/src/root/name_resolver/name_resolvers.rs @@ -5,7 +5,7 @@ use crate::root::compiler::local_variable_table::LocalVariableTable; use crate::root::errors::name_resolver_errors::NRErrs; use crate::root::errors::WErr; use crate::root::name_resolver::resolve_function_signatures::FunctionSignature; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_function::parse_evaluable::{ FullNameToken, FullNameTokens, FullNameWithIndirectionToken, }; diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index f3d7e92..1f5455d 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -6,7 +6,7 @@ use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; use crate::root::name_resolver::resolve_function_signatures::resolve_function_signature; use crate::root::name_resolver::resolve_type_sizes::{resolve_type_sizes, UnsizedUserType}; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_function::parse_evaluable::{FullNameToken, FullNameTokens}; use crate::root::parser::parse_function::parse_literal::LiteralToken; use crate::root::parser::parse_function::FunctionToken; diff --git a/src/root/name_resolver/resolve_type_sizes.rs b/src/root/name_resolver/resolve_type_sizes.rs index 4be56b6..3f3f8d3 100644 --- a/src/root/name_resolver/resolve_type_sizes.rs +++ b/src/root/name_resolver/resolve_type_sizes.rs @@ -5,7 +5,7 @@ use derive_getters::Dissolve; use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; use crate::root::name_resolver::resolve_names::UserType; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::shared::common::TypeRef; use crate::root::shared::common::{ByteSize, TypeID}; diff --git a/src/root/parser/handle_errors.rs b/src/root/parser/handle_errors.rs index 503482f..8c54119 100644 --- a/src/root/parser/handle_errors.rs +++ b/src/root/parser/handle_errors.rs @@ -2,7 +2,8 @@ use nom_supreme::error::{BaseErrorKind, StackContext}; use crate::root::errors::parser_errors::ParseError; use crate::root::errors::WErr; -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_toplevel::TopLevelTokens; pub fn handle_error<'a>( diff --git a/src/root/parser/location.rs b/src/root/parser/location.rs new file mode 100644 index 0000000..dedadd9 --- /dev/null +++ b/src/root/parser/location.rs @@ -0,0 +1,274 @@ +use std::marker::PhantomData; +use std::rc::Rc; +use std::path::PathBuf; +use std::fmt::{Display, Formatter}; +use std::fs; +use std::cmp::min; +use color_print::cformat; +use nom::InputTake; +use crate::root::parser::parse::Span; + +#[derive(Debug, Clone, Hash)] +struct InnerLocation { + /// Path of file + path: Rc, + /// Offset in the line, counted from 0 + offset: usize, + /// Line number, counted from 1 + line: u32, +} + +#[derive(Debug, Clone)] +pub struct ErrorL; + +#[derive(Debug, Clone)] +pub struct WarningL; + +pub type Location = LocationTyped; + +#[derive(Debug, Clone, Hash)] +enum ErrorLocation { + Location(InnerLocation), + Builtin, + None, +} + +#[derive(Debug, Clone, Hash)] +pub struct LocationTyped { + error_type: PhantomData, + inner_location: ErrorLocation, +} + +impl LocationTyped { + pub fn into_warning(self) -> LocationTyped { + LocationTyped { + error_type: Default::default(), + inner_location: self.inner_location, + } + } +} + +impl LocationTyped { + pub fn from_span(span: &Span) -> LocationTyped { + LocationTyped { + error_type: Default::default(), + inner_location: ErrorLocation::Location(InnerLocation { + path: span.extra.clone(), + offset: span.location_offset(), + line: span.location_line(), + }), + } + } + + pub fn from_span_end(span: &Span) -> LocationTyped { + let (span, _) = &span.take_split(span.len()); + + LocationTyped { + error_type: Default::default(), + inner_location: ErrorLocation::Location(InnerLocation { + path: span.extra.clone(), + offset: span.location_offset(), + line: span.location_line(), + }), + } + } + + pub fn path(&self) -> Option<&Rc> { + match &self.inner_location { + ErrorLocation::Location(l) => Some(&l.path), + ErrorLocation::Builtin => None, + ErrorLocation::None => None, + } + } + + pub fn builtin() -> LocationTyped { + LocationTyped { + error_type: Default::default(), + inner_location: ErrorLocation::Builtin, + } + } + + pub fn none() -> LocationTyped { + LocationTyped { + error_type: Default::default(), + inner_location: ErrorLocation::None, + } + } + + pub fn has_location(&self) -> bool { + !matches!(self.inner_location, ErrorLocation::None) + } + + fn fmt_choice(&self, f: &mut Formatter<'_>, is_warning: bool) -> std::fmt::Result { + // TODO: Inefficient! + // (Maybe fine because it is a 'bad' path?) + + let location = match &self.inner_location { + ErrorLocation::Builtin => { + writeln!(f, "{}", cformat!("Builtin Definition"))?; + return Ok(()); + } + ErrorLocation::None => { + writeln!(f, "{}", cformat!("No Location"))?; + return Ok(()); + } + ErrorLocation::Location(l) => l, + }; + + writeln!(f, "{}", cformat!("In File:"))?; + writeln!(f, " {}", location.path.as_path().to_string_lossy())?; + writeln!(f, "{}", cformat!("At:"))?; + + fn fail(f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "Failed to fetch file reference (has the file changed?)") + } + + let Ok(file) = fs::read_to_string(location.path.as_path()) else { + return fail(f); + }; + + if location.line == (file.lines().count() + 1) as u32 { + return writeln!(f, "{}", cformat!(" End Of File")); + } + + let mut offset = 0usize; + let mut chars = file.chars(); + for _ in 0..location.offset { + let Some(c) = chars.next() else { + return fail(f); + }; + if c == '\n' { + offset = 0; + } else { + offset += 1; + } + } + + let mut line_iter = file.lines(); + + let largest_num_len = format!("{}", location.line + 2).len(); + + if location.line > 1 { + if location.line > 2 { + writeln!( + f, + "{:0width$} | ...", + location.line - 2, + width = largest_num_len + )?; + } + + let Some(line) = line_iter.nth(location.line as usize - 2) else { + return fail(f); + }; + let line = if line.chars().count() > CHAR_LIMIT { + format!( + "{} ...", + line.chars().take(CHAR_LIMIT - 4).collect::() + ) + } else { + line.to_string() + }; + writeln!( + f, + "{:0width$} | {}", + location.line - 1, + line, + width = largest_num_len + )?; + } + + let Some(line) = line_iter.next() else { + return fail(f); + }; + let (mut start, mut end) = (0usize, line.chars().count() - 1); + + if end > CHAR_LIMIT { + let start_dist = offset - start; + let end_dist = end - offset; + + if start_dist > end_dist { + let take_from_start = min(start_dist, CHAR_LIMIT / 2); + start += take_from_start; + end -= CHAR_LIMIT - 1 - take_from_start; + } else { + let take_from_end = min(end_dist, CHAR_LIMIT / 2); + end -= take_from_end; + start = CHAR_LIMIT - 1 - take_from_end; + } + } + + end += 1; + + writeln!( + f, + "{:0width$} | {}", + location.line, + line.chars() + .skip(start) + .take(end - start) + .collect::(), + width = largest_num_len + )?; + + if is_warning { + let warn_line = format!( + "{:0width$} | {}^Here", + "W", + (0..(offset - start)).map(|_| ' ').collect::(), + width = largest_num_len + ); + writeln!(f, "{}", cformat!("{}", warn_line))?; + } else { + let err_line = format!( + "{:0width$} | {}^Here", + "E", + (0..(offset - start)).map(|_| ' ').collect::(), + width = largest_num_len + ); + writeln!(f, "{}", cformat!("{}", err_line))?; + } + + if let Some(line) = line_iter.next() { + let line = if line.chars().count() > CHAR_LIMIT { + format!( + "{} ...", + line.chars().take(CHAR_LIMIT - 4).collect::() + ) + } else { + line.to_string() + }; + writeln!( + f, + "{:0width$} | {}", + location.line + 1, + line, + width = largest_num_len + )?; + if line_iter.next().is_some() { + writeln!( + f, + "{:0width$} | ...", + location.line + 2, + width = largest_num_len + )?; + } + } + + Ok(()) + } +} + +const CHAR_LIMIT: usize = 61; + +impl Display for LocationTyped { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.fmt_choice(f, false) + } +} + +impl Display for LocationTyped { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.fmt_choice(f, true) + } +} \ No newline at end of file diff --git a/src/root/parser/mod.rs b/src/root/parser/mod.rs index c79ee7d..96c051f 100644 --- a/src/root/parser/mod.rs +++ b/src/root/parser/mod.rs @@ -12,3 +12,5 @@ pub mod parse_struct; pub mod parse_toplevel; pub mod parse_util; pub mod soft_alt; +mod use_parser; +pub mod location; diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs index b06943b..438ea89 100644 --- a/src/root/parser/parse.rs +++ b/src/root/parser/parse.rs @@ -1,14 +1,8 @@ -use std::cmp::min; -use std::ffi::OsStr; -use std::fmt::{Display, Formatter}; use std::fs; -use std::marker::PhantomData; use std::path::PathBuf; use std::rc::Rc; -use color_print::cformat; -use lazy_static::lazy_static; -use nom::{IResult, InputTake}; +use nom::IResult; use nom_locate::LocatedSpan; use nom_supreme::error::GenericErrorTree; @@ -22,273 +16,6 @@ pub type Span<'a> = LocatedSpan<&'a str, &'a Rc>; pub type ParseResult<'a, I = Span<'a>, O = Span<'a>, E = ErrorTree<'a>> = IResult; pub type ErrorTree<'a> = GenericErrorTree, &'static str, &'static str, String>; -lazy_static! { - static ref BUILTIN_PATH: &'static OsStr = OsStr::new("builtin"); -} - -#[derive(Debug, Clone, Hash)] -struct InnerLocation { - path: Rc, - /// Offset in the line, counted from 0 - offset: usize, - /// Line number, counted from 1 - line: u32, -} - -#[derive(Debug, Clone)] -pub struct ErrorL; -#[derive(Debug, Clone)] -pub struct WarningL; - -pub type Location = LocationTyped; - -#[derive(Debug, Clone, Hash)] -enum ErrorLocation { - Location(InnerLocation), - Builtin, - None, -} - -#[derive(Debug, Clone, Hash)] -pub struct LocationTyped { - error_type: PhantomData, - inner_location: ErrorLocation, -} - -impl LocationTyped { - pub fn into_warning(self) -> LocationTyped { - LocationTyped { - error_type: Default::default(), - inner_location: self.inner_location, - } - } -} - -impl LocationTyped { - pub fn from_span(span: &Span) -> LocationTyped { - LocationTyped { - error_type: Default::default(), - inner_location: ErrorLocation::Location(InnerLocation { - path: span.extra.clone(), - offset: span.location_offset(), - line: span.location_line(), - }), - } - } - - pub fn from_span_end(span: &Span) -> LocationTyped { - let (span, _) = &span.take_split(span.len()); - - LocationTyped { - error_type: Default::default(), - inner_location: ErrorLocation::Location(InnerLocation { - path: span.extra.clone(), - offset: span.location_offset(), - line: span.location_line(), - }), - } - } - - pub fn path(&self) -> Option<&Rc> { - match &self.inner_location { - ErrorLocation::Location(l) => Some(&l.path), - ErrorLocation::Builtin => None, - ErrorLocation::None => None, - } - } - - pub fn builtin() -> LocationTyped { - LocationTyped { - error_type: Default::default(), - inner_location: ErrorLocation::Builtin, - } - } - - pub fn none() -> LocationTyped { - LocationTyped { - error_type: Default::default(), - inner_location: ErrorLocation::None, - } - } - - pub fn has_location(&self) -> bool { - !matches!(self.inner_location, ErrorLocation::None) - } - - fn fmt_choice(&self, f: &mut Formatter<'_>, is_warning: bool) -> std::fmt::Result { - // TODO: Inefficient! - // (Maybe fine because it is a 'bad' path?) - - let location = match &self.inner_location { - ErrorLocation::Builtin => { - writeln!(f, "{}", cformat!("Builtin Definition"))?; - return Ok(()); - } - ErrorLocation::None => { - writeln!(f, "{}", cformat!("No Location"))?; - return Ok(()); - } - ErrorLocation::Location(l) => l, - }; - - writeln!(f, "{}", cformat!("In File:"))?; - writeln!(f, " {}", location.path.as_path().to_string_lossy())?; - writeln!(f, "{}", cformat!("At:"))?; - - fn fail(f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "Failed to fetch file reference (has the file changed?)") - } - - let Ok(file) = fs::read_to_string(location.path.as_path()) else { - return fail(f); - }; - - if location.line == (file.lines().count() + 1) as u32 { - return writeln!(f, "{}", cformat!(" End Of File")); - } - - let mut offset = 0usize; - let mut chars = file.chars(); - for _ in 0..location.offset { - let Some(c) = chars.next() else { - return fail(f); - }; - if c == '\n' { - offset = 0; - } else { - offset += 1; - } - } - - let mut line_iter = file.lines(); - - let largest_num_len = format!("{}", location.line + 2).len(); - - if location.line > 1 { - if location.line > 2 { - writeln!( - f, - "{:0width$} | ...", - location.line - 2, - width = largest_num_len - )?; - } - - let Some(line) = line_iter.nth(location.line as usize - 2) else { - return fail(f); - }; - let line = if line.chars().count() > CHAR_LIMIT { - format!( - "{} ...", - line.chars().take(CHAR_LIMIT - 4).collect::() - ) - } else { - line.to_string() - }; - writeln!( - f, - "{:0width$} | {}", - location.line - 1, - line, - width = largest_num_len - )?; - } - - let Some(line) = line_iter.next() else { - return fail(f); - }; - let (mut start, mut end) = (0usize, line.chars().count() - 1); - - if end > CHAR_LIMIT { - let start_dist = offset - start; - let end_dist = end - offset; - - if start_dist > end_dist { - let take_from_start = min(start_dist, CHAR_LIMIT / 2); - start += take_from_start; - end -= CHAR_LIMIT - 1 - take_from_start; - } else { - let take_from_end = min(end_dist, CHAR_LIMIT / 2); - end -= take_from_end; - start = CHAR_LIMIT - 1 - take_from_end; - } - } - - end += 1; - - writeln!( - f, - "{:0width$} | {}", - location.line, - line.chars() - .skip(start) - .take(end - start) - .collect::(), - width = largest_num_len - )?; - - if is_warning { - let warn_line = format!( - "{:0width$} | {}^Here", - "W", - (0..(offset - start)).map(|_| ' ').collect::(), - width = largest_num_len - ); - writeln!(f, "{}", cformat!("{}", warn_line))?; - } else { - let err_line = format!( - "{:0width$} | {}^Here", - "E", - (0..(offset - start)).map(|_| ' ').collect::(), - width = largest_num_len - ); - writeln!(f, "{}", cformat!("{}", err_line))?; - } - - if let Some(line) = line_iter.next() { - let line = if line.chars().count() > CHAR_LIMIT { - format!( - "{} ...", - line.chars().take(CHAR_LIMIT - 4).collect::() - ) - } else { - line.to_string() - }; - writeln!( - f, - "{:0width$} | {}", - location.line + 1, - line, - width = largest_num_len - )?; - if line_iter.next().is_some() { - writeln!( - f, - "{:0width$} | ...", - location.line + 2, - width = largest_num_len - )?; - } - } - - Ok(()) - } -} - -const CHAR_LIMIT: usize = 61; - -impl Display for LocationTyped { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - self.fmt_choice(f, false) - } -} - -impl Display for LocationTyped { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - self.fmt_choice(f, true) - } -} - pub fn parse(path: PathBuf) -> Result, WErr> { let text = fs::read_to_string(&path).unwrap(); let path = Rc::new(path); diff --git a/src/root/parser/parse_function.rs b/src/root/parser/parse_function.rs index 5c44e0e..63e2612 100644 --- a/src/root/parser/parse_function.rs +++ b/src/root/parser/parse_function.rs @@ -1,19 +1,20 @@ -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_blocks::{ - parse_terminator_default_set, BRACE_TERMINATOR, BRACKET_TERMINATOR, + BRACE_TERMINATOR, BRACKET_TERMINATOR, parse_terminator_default_set, }; use crate::root::parser::parse_function::parse_evaluable::{ - parse_full_name, FullNameWithIndirectionToken, + FullNameWithIndirectionToken, parse_full_name, }; -use crate::root::parser::parse_function::parse_line::{parse_lines, LineTokens}; +use crate::root::parser::parse_function::parse_line::{LineTokens, parse_lines}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; -use crate::root::parser::parse_parameters::{parse_parameters, Parameters, SelfType}; -use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; +use crate::root::parser::parse_parameters::{Parameters, parse_parameters, SelfType}; +use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use derive_getters::{Dissolve, Getters}; use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; +use crate::root::parser::location::Location; pub mod parse_assigner; pub mod parse_assignment; diff --git a/src/root/parser/parse_function/parse_break.rs b/src/root/parser/parse_function/parse_break.rs index ecdde0f..5953fcc 100644 --- a/src/root/parser/parse_function/parse_break.rs +++ b/src/root/parser/parse_function/parse_break.rs @@ -1,8 +1,8 @@ use derive_getters::Getters; use nom::character::complete::char; use nom_supreme::tag::complete::tag; - -use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; use crate::root::parser::parse_util::discard_ignored; diff --git a/src/root/parser/parse_function/parse_evaluable.rs b/src/root/parser/parse_function/parse_evaluable.rs index 77b80d0..cf114da 100644 --- a/src/root/parser/parse_function/parse_evaluable.rs +++ b/src/root/parser/parse_function/parse_evaluable.rs @@ -5,11 +5,12 @@ use nom::bytes::complete::tag; use nom::character::complete::char; use crate::root::errors::parser_errors::create_custom_error; -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_arguments::parse_arguments; -use crate::root::parser::parse_blocks::{parse_terminator_default_set, BRACKET_TERMINATOR}; -use crate::root::parser::parse_function::parse_literal::{parse_literal, LiteralToken}; -use crate::root::parser::parse_function::parse_operator::{parse_operator, OperatorToken}; +use crate::root::parser::parse_blocks::{BRACKET_TERMINATOR, parse_terminator_default_set}; +use crate::root::parser::parse_function::parse_literal::{LiteralToken, parse_literal}; +use crate::root::parser::parse_function::parse_operator::{OperatorToken, parse_operator}; use crate::root::parser::parse_function::parse_struct_init::{parse_struct_init, StructInitToken}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_util::discard_ignored; diff --git a/src/root/parser/parse_function/parse_if.rs b/src/root/parser/parse_function/parse_if.rs index 6c22064..08c805d 100644 --- a/src/root/parser/parse_function/parse_if.rs +++ b/src/root/parser/parse_function/parse_if.rs @@ -1,13 +1,13 @@ use derive_getters::Getters; use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; - -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_blocks::{ - parse_terminator_default_set, BRACE_TERMINATOR, BRACKET_TERMINATOR, + BRACE_TERMINATOR, BRACKET_TERMINATOR, parse_terminator_default_set, }; -use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; -use crate::root::parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; +use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; +use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens, parse_lines}; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; diff --git a/src/root/parser/parse_function/parse_initialisation.rs b/src/root/parser/parse_function/parse_initialisation.rs index eaa7b3c..2983de1 100644 --- a/src/root/parser/parse_function/parse_initialisation.rs +++ b/src/root/parser/parse_function/parse_initialisation.rs @@ -1,6 +1,6 @@ -use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_function::parse_evaluable::{ - parse_evaluable, parse_full_name, EvaluableToken, FullNameWithIndirectionToken, + EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name, }; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; @@ -9,6 +9,7 @@ use derive_getters::Getters; use nom::character::complete::char; use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; +use crate::root::parser::location::Location; #[derive(Debug, Getters)] pub struct InitialisationToken { diff --git a/src/root/parser/parse_function/parse_literal.rs b/src/root/parser/parse_function/parse_literal.rs index c1143b6..69b930e 100644 --- a/src/root/parser/parse_function/parse_literal.rs +++ b/src/root/parser/parse_function/parse_literal.rs @@ -1,11 +1,12 @@ use crate::root::builtin::types::bool::BoolType; use crate::root::builtin::types::int::IntType; -use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_util::discard_ignored; use crate::root::shared::common::TypeID; use derive_getters::{Dissolve, Getters}; use nom::branch::alt; use nom::bytes::complete::tag; +use crate::root::parser::location::Location; #[derive(Debug, Dissolve, Getters)] pub struct LiteralToken { diff --git a/src/root/parser/parse_function/parse_operator.rs b/src/root/parser/parse_function/parse_operator.rs index 295cfc1..7754c62 100644 --- a/src/root/parser/parse_function/parse_operator.rs +++ b/src/root/parser/parse_function/parse_operator.rs @@ -1,9 +1,10 @@ -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use derive_getters::Getters; use nom::Err::Error; use nom_supreme::error::GenericErrorTree; use nom_supreme::tag::complete::tag; use nom_supreme::tag::TagError; +use crate::root::parser::location::Location; #[derive(PartialEq, Debug)] pub enum PrefixOrInfix { diff --git a/src/root/parser/parse_function/parse_return.rs b/src/root/parser/parse_function/parse_return.rs index 66ebd45..3ffd02b 100644 --- a/src/root/parser/parse_function/parse_return.rs +++ b/src/root/parser/parse_function/parse_return.rs @@ -2,9 +2,9 @@ use derive_getters::Getters; use nom::character::complete::char; use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; - -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; -use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; +use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::require_ignored; diff --git a/src/root/parser/parse_function/parse_struct_init.rs b/src/root/parser/parse_function/parse_struct_init.rs index b905978..73f74ce 100644 --- a/src/root/parser/parse_function/parse_struct_init.rs +++ b/src/root/parser/parse_function/parse_struct_init.rs @@ -1,13 +1,13 @@ use derive_getters::{Dissolve, Getters}; use nom::bytes::complete::tag; use nom::character::streaming::char; - -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_blocks::{ - parse_terminator_default_set, take_until_or_end_discard_smart, BRACE_TERMINATOR, + BRACE_TERMINATOR, parse_terminator_default_set, take_until_or_end_discard_smart, }; use crate::root::parser::parse_function::parse_evaluable::{ - parse_evaluable, parse_full_name, EvaluableToken, FullNameWithIndirectionToken, + EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name, }; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_util::discard_ignored; diff --git a/src/root/parser/parse_function/parse_while.rs b/src/root/parser/parse_function/parse_while.rs index 0870ca0..7244bcb 100644 --- a/src/root/parser/parse_function/parse_while.rs +++ b/src/root/parser/parse_function/parse_while.rs @@ -1,13 +1,13 @@ use derive_getters::Getters; use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; - -use crate::root::parser::parse::{Location, ParseResult, Span}; +use crate::root::parser::location::Location; +use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_blocks::{ - parse_terminator_default_set, BRACE_TERMINATOR, BRACKET_TERMINATOR, + BRACE_TERMINATOR, BRACKET_TERMINATOR, parse_terminator_default_set, }; -use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; -use crate::root::parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; +use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; +use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens, parse_lines}; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; diff --git a/src/root/parser/parse_impl.rs b/src/root/parser/parse_impl.rs index 2631d76..93b19d6 100644 --- a/src/root/parser/parse_impl.rs +++ b/src/root/parser/parse_impl.rs @@ -1,13 +1,14 @@ -use crate::root::parser::parse::{Location, ParseResult, Span}; -use crate::root::parser::parse_blocks::{parse_terminator_default_set, BRACE_TERMINATOR}; -use crate::root::parser::parse_function::{parse_function, FunctionToken}; +use crate::root::parser::parse::{ParseResult, Span}; +use crate::root::parser::parse_blocks::{BRACE_TERMINATOR, parse_terminator_default_set}; +use crate::root::parser::parse_function::{FunctionToken, parse_function}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; -use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; +use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use derive_getters::{Dissolve, Getters}; use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; +use crate::root::parser::location::Location; #[derive(Debug, Getters, Dissolve)] pub struct ImplToken { diff --git a/src/root/parser/parse_name.rs b/src/root/parser/parse_name.rs index 340bd4d..dddfd14 100644 --- a/src/root/parser/parse_name.rs +++ b/src/root/parser/parse_name.rs @@ -1,8 +1,9 @@ -use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use derive_getters::Getters; use nom::bytes::complete::take_till; use nom::Err::Error; use nom_supreme::error::{BaseErrorKind, Expectation}; +use crate::root::parser::location::Location; #[derive(Debug, Getters, Clone)] pub struct SimpleNameToken { diff --git a/src/root/parser/parse_struct.rs b/src/root/parser/parse_struct.rs index 2377f3f..f135b3a 100644 --- a/src/root/parser/parse_struct.rs +++ b/src/root/parser/parse_struct.rs @@ -1,14 +1,15 @@ -use crate::root::parser::parse::{Location, ParseResult, Span}; -use crate::root::parser::parse_blocks::{parse_terminator_default_set, BRACE_TERMINATOR}; +use crate::root::parser::parse::{ParseResult, Span}; +use crate::root::parser::parse_blocks::{BRACE_TERMINATOR, parse_terminator_default_set}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; -use crate::root::parser::parse_parameters::{parse_parameters, Parameters}; -use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; +use crate::root::parser::parse_parameters::{Parameters, parse_parameters}; +use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use crate::root::shared::common::TypeID; use derive_getters::{Dissolve, Getters}; use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; +use crate::root::parser::location::Location; #[derive(Debug, Getters, Dissolve)] pub struct StructToken { diff --git a/src/root/parser/use_parser.rs b/src/root/parser/use_parser.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/root/shared/types.rs b/src/root/shared/types.rs index a5fc3ae..b61bb91 100644 --- a/src/root/shared/types.rs +++ b/src/root/shared/types.rs @@ -1,6 +1,6 @@ use crate::root::errors::evaluable_errors::EvalErrs; use crate::root::errors::WErr; -use crate::root::parser::parse::Location; +use crate::root::parser::location::Location; use crate::root::parser::parse_function::parse_literal::LiteralToken; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::shared::common::{ByteSize, LocalAddress, TypeID, TypeRef}; diff --git a/todo.md b/todo.md index 863cad5..959d0d6 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,3 @@ -- Add Struct::null() to get &Struct -- Fix everything required to get linked list working -- Add free - Add array support -- Add rich parser errors - Add support for multiple files From dcb10f9601b1618b2187179bcaf0d23b400a2140 Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas <100799838+Robert-M-Lucas@users.noreply.github.com> Date: Tue, 6 Aug 2024 16:21:11 +0100 Subject: [PATCH 3/4] Added imports --- build/out.asm | 275 ++++++++++++++++++++++++++++++- build/out.o | Bin 592 -> 2608 bytes build/out.out | Bin 16480 -> 17664 bytes main.why | 13 +- src/root/errors/parser_errors.rs | 2 + src/root/parser/handle_errors.rs | 6 +- src/root/parser/location.rs | 22 +++ src/root/parser/parse.rs | 36 +++- src/root/parser/use_parser.rs | 41 +++++ todo.md | 2 +- 10 files changed, 372 insertions(+), 25 deletions(-) diff --git a/build/out.asm b/build/out.asm index ace0deb..6ce2918 100644 --- a/build/out.asm +++ b/build/out.asm @@ -5,10 +5,281 @@ section .text main: push rbp mov rbp, rsp - mov qword [rbp-8], 3 - mov qword [rbp-16], 8 + sub rsp, 24 + call _10 + add rsp, 24 + mov rax, qword [rbp-24] + mov qword [rbp-8], rax + mov rax, rbp + add rax, -8 + mov qword [rbp-32], rax + mov qword [rbp-40], 12 + mov rax, qword [rbp-40] + mov qword [rbp-64], rax + mov rax, qword [rbp-32] + mov qword [rbp-72], rax + sub rsp, 72 + call _11 + add rsp, 72 + mov rax, rbp + add rax, -8 + mov qword [rbp-64], rax + mov rax, qword [rbp-64] + mov qword [rbp-72], rax + sub rsp, 72 + call _12 + add rsp, 72 + mov qword [rbp-72], 8 + mov rax, qword [rbp-72] + leave + ret + + +_10: + push rbp + mov rbp, rsp + mov qword [rbp+16], 0 + leave + ret + + +_11: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-17], rax + mov rdx, qword [rbp-17] + mov rax, qword [rdx+0] + mov qword [rbp-9], rax + mov rax, qword [rbp-9] + cmp rax, 0 + jz __36_12 + mov byte [rbp-1], 0 + jmp __36_13 + __36_12: + mov byte [rbp-1], 1 + __36_13: + cmp byte [rbp-1], 0 + jz _11_15 + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-25], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-57], rax + sub rsp, 57 + call _13 + add rsp, 57 + mov rax, qword [rbp-49] + mov qword [rbp-33], rax + mov rdx, qword [rbp-25] + mov rax, qword [rbp-33] + mov qword [rdx+0], rax + jmp _11_14 + _11_15: + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-25], rax + mov rdx, qword [rbp-25] + mov rax, qword [rdx+0] + mov qword [rbp-33], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-65], rax + mov rax, qword [rbp-33] + mov qword [rbp-73], rax + sub rsp, 73 + call _14 + add rsp, 73 + _11_16: + _11_14: + +leave +ret + +_12: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-18], rax + mov rdx, qword [rbp-18] + mov rax, qword [rdx+0] + mov qword [rbp-10], rax + mov rax, qword [rbp-10] + cmp rax, 0 + jz __36_0 + mov byte [rbp-2], 0 + jmp __36_1 + __36_0: + mov byte [rbp-2], 1 + __36_1: + mov al, byte [rbp-2] + cmp al, 0 + jz __23_2 + mov byte [rbp-1], 0 + jmp __23_3 + __23_2: + mov byte [rbp-1], 1 + __23_3: + cmp byte [rbp-1], 0 + jz _12_4 + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-26], rax + mov rdx, qword [rbp-26] + mov rax, qword [rdx+0] + mov qword [rbp-34], rax + mov rax, qword [rbp-34] + mov qword [rbp-42], rax + sub rsp, 42 + call _15 + add rsp, 42 + _12_5: + _12_4: + +leave +ret + +_13: + push rbp + mov rbp, rsp + mov rax, qword [rbp+16] + mov qword [rbp-16], rax + mov qword [rbp-8], 0 + mov rdi, 16 + sub rsp, 16 + extern malloc + call malloc + add rsp, 16 + mov qword [rbp-24], rax + mov rdx, qword [rbp-24] mov rax, qword [rbp-16] + mov qword [rdx+0], rax + mov rax, qword [rbp-8] + mov qword [rdx+8], rax + mov rax, qword [rbp-24] + mov qword [rbp+24], rax leave ret +_14: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-17], rax + mov rdx, qword [rbp-17] + mov rax, qword [rdx+0] + mov qword [rbp-9], rax + mov rax, qword [rbp-9] + cmp rax, 0 + jz __36_17 + mov byte [rbp-1], 0 + jmp __36_18 + __36_17: + mov byte [rbp-1], 1 + __36_18: + cmp byte [rbp-1], 0 + jz _14_20 + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-25], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-57], rax + sub rsp, 57 + call _13 + add rsp, 57 + mov rax, qword [rbp-49] + mov qword [rbp-33], rax + mov rdx, qword [rbp-25] + mov rax, qword [rbp-33] + mov qword [rdx+0], rax + jmp _14_19 + _14_20: + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-25], rax + mov rdx, qword [rbp-25] + mov rax, qword [rdx+0] + mov qword [rbp-33], rax + mov rax, qword [rbp+24] + mov qword [rbp-41], rax + mov rax, qword [rbp-41] + mov qword [rbp-65], rax + mov rax, qword [rbp-33] + mov qword [rbp-73], rax + sub rsp, 73 + call _14 + add rsp, 73 + _14_21: + _14_19: + +leave +ret + +_15: + push rbp + mov rbp, rsp + mov rax, [rbp+16] + add rax, 0 + mov qword [rbp-16], rax + mov rdx, qword [rbp-16] + mov rax, qword [rdx+0] + mov qword [rbp-8], rax + mov rdi, __8_fstr + mov rsi, [rbp-8] + mov al, 0 + sub rsp, 16 + extern printf + call printf + add rsp, 16 + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-34], rax + mov rdx, qword [rbp-34] + mov rax, qword [rdx+0] + mov qword [rbp-26], rax + mov rax, qword [rbp-26] + cmp rax, 0 + jz __36_6 + mov byte [rbp-18], 0 + jmp __36_7 + __36_6: + mov byte [rbp-18], 1 + __36_7: + mov al, byte [rbp-18] + cmp al, 0 + jz __23_8 + mov byte [rbp-17], 0 + jmp __23_9 + __23_8: + mov byte [rbp-17], 1 + __23_9: + cmp byte [rbp-17], 0 + jz _15_10 + mov rax, [rbp+16] + add rax, 8 + mov qword [rbp-42], rax + mov rdx, qword [rbp-42] + mov rax, qword [rdx+0] + mov qword [rbp-50], rax + mov rax, qword [rbp-50] + mov qword [rbp-58], rax + sub rsp, 58 + call _15 + add rsp, 58 + _15_11: + _15_10: + +leave +ret + +section .data_readonly + __8_fstr db `Integer: %ld\n`,0 \ No newline at end of file diff --git a/build/out.o b/build/out.o index 3add5ba63690c0c311b1cefa34a37ca0eb187a4c..279baffcf26b7f606138251d6312e62e8b0bb85a 100644 GIT binary patch literal 2608 zcmbW2O>9h26vyw3qC}X8$Sf@CD-n@0Gg?|AC3$iy782>k!t+M-*=k{nM zqAE{BZBbLR3RS9YYRKa@HCxRH(f=D#FS1!OJ!-)Y(IO_|W@AqI96ssYHPa5~PZB$j z@3t6Nv@n7DIDgY0@*Ciny(#4?=kMd!K6rf>Y9kK<5V`&Zr)8-p(U;(RqvcAaoSk zk31c;**rZJPyWG4|Er3B$myr%aP~iYWZDin<}`U$bYf_zkT|Z39k3{d-x2;@=S#|; z^~5QwPj&c>oI6ui{uX#D2_G?zBl->W4Ri=AYYCp)&Mkm10Y^}k0!MTq`e`!;p=@$c zJH-4_Xu!rh_ffDtcxVf@Ph0R9O2eqaH)MRPEO^2!cp`GqnbVFNVYbwP1)UL?JlBZS_z()p)2(ey|?o zpZ1y(s_(U1E|<~%w|mXmf4A2iw&3)={(`rI(N2~00KGOny4Y8`BX+``Fm=V2cEBhH zn+)R*YRsI^7|v(Rp3k@e^|g&mP%@w0HZN zLz!G_!^&)WE}=$0bJAChY0vlET*gcHbZ1Yg#@@rde$MyyV|1#^D$jG7tY>pVzm{@27~mAovoFza#hx=Jy0&!`ytXAzzC5dU2t( z*5F&2H;Z_OxhMEe<{80vGtUXWhxu{AJ?1wBKU9NvF~23^Pcr{1_-W=##L=8*-XQoT z=4rt%GrubMl^XmS^Xnr1HuH%1r*xP3Ou-*8UnuxP=8FY?%3KTnvIZYETr3i70W4PK zJ3nT)N&4mIMh&j^_jP8|YkT_qMz6O^b$OlL%1x|;C6w!?n%zW_HAT!#G+Al3vL3<# z$T9JdWXdH7p`@fsIZ2mdQ|OS*_8hfMd*LEJD(J>~C>c5oT&2kt9+26#)!9X^WJ%t@Aq;xRPOPnEX! Avj6}9 literal 592 zcmb<-^>JfjWMqH=Mg}_u1P><4z~F#jLfH-stPD&@qU13_c7ZS(n)U{$Bc#y81K(7s$UUJ4k`>66$G-d`QidpVFFYo3y=oc!vW<>Kxs*+IFt*g44{UJz(pVo zbo)a+I-hzRcl`k}8%TcuneWl<`r+hZs7Ae#)QS=Yz2c1GlA@BtBp|CY7eYh$P-TQD ski9GnAOB-h$BZS$`GE=ru!u7xm1gFo=;xP~=p`2CGUO&^=Ajq~0N2PPwg3PC diff --git a/build/out.out b/build/out.out index 329ee4f4a7bdf308587d47faee9bad17f78c3590..9b29bb0d2119a5615421fe04eca098a8792fd450 100755 GIT binary patch literal 17664 zcmeHPYiu0V6~4O;#Npv=9uSkzW@r->(t7Q+9mm=57~5HUi~f=u$$UY{gu@E{%9nkq(s&Aidq|Ewua1Jsi|vqld*1f zsj}5o%2F2snT5^P8j_|a@@Uj3L^iTr10?a9siLJlLtd#{N{N&RNfPgLvAJsGiVI9r zq>7m~@wAHDijPm^L+n(fpQlxp@uMu;R9HU7ut-0Tax>jx45qKMbw%WroEM9w@Tf_= z#<(_Djn~_{BJxE}gh=d-)P@I+eBQL*$M{#gp|Hs;;NJdx|zP9)h=+5Ebf zq54PdZzJ<8i@YEn&ZEd>238`azgxhgyqs^5)4YlMJKK3^mpIZZrMwhJa;smE2yzjuHbD~1XH@Y)WG_|-afZI-X337t9NvQIWk%-Fw>>3qd5AA z&Hx>A6yuA@G3mw^=jKp$KEv4K-}(+Nhp1XjN|d8F*jmyxLNQA@=EM0CH!9oKayIt< zd^h@VIzM3vGZ1DV%s`ldFau!*!VH8N2s7~il!1TF{p1JE?zbDAy%!v5Rm!=4%8OQC zbap@8cu*f^b=7l#tIMCqe(q8mHEQo5-J8d%D9e9EZF=N>^F=htU*hkBlSl4!CSP-Q z|K;SyEvdv*;#ueLVS66vmkq=Go$c|tOTVW5ll{Td&3*fu@c`~zM|*F!6?V)CT()bSgfr=}a6$RX#*DQ{lQ!)*2tL}V%kjEhRgXdIi!*fpClYXkEKO*|Wsr@vmgL40~2X(iHQ+k%mt-Yxj zI#eK@aQ1G+7De@an7{Aro>tz4hf-Db)-3EJyS`La-u2K3;SJQAVf`}X%aDf=*_z64 zOwVfo{ubb?@JfcSI+M}2u0_SiCIxi|n;DJ{82i){a5z|b$~YW6BjDq3jg36K2gZ96 z@F^YesmPwxscX+Xg3tP5_$xeMBM+|teg*O+^trNf%JjL?*XLyFB+ke*6zTva5O%Rj z=fA5}oW2-{75KsitH~$?PGK)@4}DgfeQtJ7HNyBK<(dffbM4}yL-*&*HM7qz*ISLl zGjn|sM~96vHSHXTSvKVXSJn9RV9eOYne=JiGu8FF+nb-jkI(O`OSRG2GC-1+ek-=%Zr`cVPy?41^g7 zGZ1DV%s`ldFau!*!VG*&8KB?U{AIAti)u2&YR zuMPUv7Y9lRBkasUCgqXqj^{zP$In{&_$05G>gx6$)-(NoCgl6^ zV253Ne*0OHexR=}X|2E)$=6tk&Uhl8u-e<&JKI*bC9D;jvKh-sd#q?#eNDi+U97tU z)=;!0u2DVPlAU9T_EoFZrftd6c%hIhj94SX!@-_Z@3v&RP$&-N3U$>3wyvHgP!IIL z#m2#8r`nuKwyE4^>}G_JE95-%r+eYLcFFY_w)j}u%MG`bviN}7#Mov9FzqLRX@3Ds zf5#uAlZFdzpl4a6BaJxuT;9WyuHB%`{j3vN&qmfpTz(EeG9G-J$Ax-+_A$SU$C`RJ zdzdfszqOHAmz*PJiS;4wlhSv#Lig`1^^a;$Pw_>dxsS&uiw)*O*0cWv@Th81k_$Fh zOBtWycm;1~T*)}i3I{TR(RHD1YedEudJh*-7zKH})k^HXh>iNKjrXkhI%EW+pp4%P zs~@6LFaGtqqbBu4Af6QCzYgG!YllthWB~sq&Y)@t+3pv;%xL@iK1wrnx2$$Iu<+Wzv|dMq)X(EZM@tz#1W z0d3!`N1+1hj#6BYrl*Y8T;F^pIAeV)Ixl6|yE?TQ&(p8sA) zzw>eb%HOc7nKZzDf_d1i@g_wd7y8R>z~@D;IHTW;?iWwD+;5FDT=Ej}B332g>B05V zBMQJ!b|hB-n8}ty5g@`C=hkhGnMcj^v9&>Aaiqilwrf9^a{ki=$)tte4Hi z+v;1m+wn-?rc0&tgqtmRr3tmYlpf8xneoxl2~d13mmVHwm=2BS@|o7+xED{CM`7I2 z82dlXq|PNlsm9~LvPP|cSC>w=F}Ein8y29ZoHl2<&> zxnnud-Jx=syV0xRTzAV&eR9lnK9!1>Cq}*W5TsW!a);EgaxPmMQ*r(DI#L*qkClpJ z*^)QmgY-4flFKmE(>vHgmusBNccjZZR6H|LKo>^h3~0?g*-|-IEX-)Q(BUA`MBv(3 z-cxZMX57o}M4hfy=!!bMah=3cHk~OJ@`x$BgD=+|8Mu;`abT`n<3Q@@Cq0@QMi51~ z#xAb=Gro~H702x}in~I^`PpA*0yrI6SuApch2?_q8#@jt@xKf-)D{tKBOj6V&W=HkMiV*V8KcQTJ+j9?SK%$sw- z$W-_;|L^8NZ-LHV$cMjt9u1mn3t#5fdzdfp4J3ZC6ZvKEY3?p<-Us7<4JgIPw_9yKC+ppV_RDN1JUK57ICwgK;mbPiqpjNfjO#`CH5uT`^LB;#D~y}kzQ$vO z=1Knk<@q?)W(-`$^El=&9(6Fs3E2z{fk_V3iTc2k1?=8 z4B3w~Mbf-X&JCF~-(_pdse~44ofw+t8P_kV<=$ZvEgCzx#RC()=b%#}`IqsZ-lIzH en4v}E8q>Nv;5-lx<|6g^rL~5CV}KB7s{a6dY3L{b delta 2085 zcmZ`)eN0?M5Z`@w2X}Ck_qc=mg1+~(;Yw-WeL&!V!o4epp79Hfn3$Myr<6cq=?7=5 zTGQ6}K{X~^Co%AcX&Muwe<{+$`iB-$Yuo4_m}sK@VIa|zXk#%ok(lT?``#|4R_F3| zW_~j}yR$nxcl`Pz!6YeCgO;tNsgy2xW}LQZ1{CTmU2ClB zqBhV)p^h5IihQQvM79CuRe@j1u=P}*X$gXV$-^vhF6K$-#pdPsyd0k=_%!$D9M02^ zby>4Q6InAYLGVT!CXFyFJ$T3e?c_3#c$q?#W3T`Di>3R2tUMi=y0iIR?S&f-Wn1tS zIO=#DG-uGY9V2QmC;218!%vMr@#OGiv2eIJyuUC$L6~k#9xys!!l^Z}pcH>Twtbcw%%U7S0H5im5du7EW<$8z_^82V!tG;Ay{zmQAKQG)=r_Z@f zljM5YSoED^F1eQ%ik|ZfC@+srUl_gVT&tSoQ_KIvuWq;^>(mXu$Ps@FEjh@h(TCs^<5fRcd&?;2G-n+zdo=wKjlACJs)@F9j7!oL?ezKcBm45@>;pf z%Em-~A7r#>Xz6;n+>8jxR>~~pIDXd$F_oHdDE5p4%LlJ51pauS zp(7dBwm-aYM9US5TB6-Z7>QjSE#QmCJefzk+Ycs^9dURz-snI_n6df%NAV`=5%1-Ie4~N?7 z@AZ;t9*!qSk|;>TyK#~N&!Ip`fH<)p7>N^p#FFdq%)V-?Z}k$wSF@aOc-68i8)q)? zcR+6o(6emxd}8^?m)B30)y#}-$wd3S&OJmA1cnN#^o9PY26{+F2 zg=g6UQZ@XMx(J2tNx0gbf@IIP!SRXlA~A~lj^H?o3Na2J7%3DB(6f8-!`?i_w@~Pd zDQ>5|jGHIe31<*BIcjpW0#*>saWuzK4fJ%(Gse*|cr4utpQU%xw}6^zrV2N2CCey5Ej G81fHZqc!pX diff --git a/main.why b/main.why index c328c4b..44fb610 100644 --- a/main.why +++ b/main.why @@ -1,17 +1,10 @@ use linked_list; -struct Test { - a: int -} - -impl Test { - fn static() { - - } -} fn main() -> int { - let x: Test = Test { a: 3 }; + let x: LL = LL::new(); + x.add(12); + x.print(); return 8; } diff --git a/src/root/errors/parser_errors.rs b/src/root/errors/parser_errors.rs index 3e76768..b631090 100644 --- a/src/root/errors/parser_errors.rs +++ b/src/root/errors/parser_errors.rs @@ -13,6 +13,8 @@ pub enum ParseError { Expected(String), #[error("Failed parsing {0}")] NomErrorKind(String), + #[error("Failed to open file [{0}]")] + FailedToOpenFile(String) } pub fn create_custom_error(e: String, l: Span) -> nom::Err { diff --git a/src/root/parser/handle_errors.rs b/src/root/parser/handle_errors.rs index 8c54119..6dbde53 100644 --- a/src/root/parser/handle_errors.rs +++ b/src/root/parser/handle_errors.rs @@ -6,9 +6,9 @@ use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_toplevel::TopLevelTokens; -pub fn handle_error<'a>( - res: ParseResult<'a, Span<'a>, Vec>, -) -> Result<(Span<'a>, Vec), WErr> { +pub fn handle_error( + res: ParseResult, +) -> Result<(A, B), WErr> { match res { Ok(v) => Ok(v), Err(e) => match &e { diff --git a/src/root/parser/location.rs b/src/root/parser/location.rs index dedadd9..7add73e 100644 --- a/src/root/parser/location.rs +++ b/src/root/parser/location.rs @@ -8,6 +8,28 @@ use color_print::cformat; use nom::InputTake; use crate::root::parser::parse::Span; +pub enum ToLocation<'a> { + Location(Location), + Span(Span<'a>) +} + +impl<'a> ToLocation<'a> { + pub fn from_location(location: Location) -> ToLocation<'a> { + ToLocation::Location(location) + } + + pub fn from_span(span: Span<'a>) -> ToLocation<'a> { + ToLocation::Span(span) + } + + pub fn to_location(self) -> Location { + match self { + ToLocation::Location(location) => location, + ToLocation::Span(s) => Location::from_span(&s) + } + } +} + #[derive(Debug, Clone, Hash)] struct InnerLocation { /// Path of file diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs index 438ea89..48b7203 100644 --- a/src/root/parser/parse.rs +++ b/src/root/parser/parse.rs @@ -5,11 +5,13 @@ use std::rc::Rc; use nom::IResult; use nom_locate::LocatedSpan; use nom_supreme::error::GenericErrorTree; - +use crate::root::errors::parser_errors::ParseError; use crate::root::errors::WErr; use crate::root::parser::handle_errors::handle_error; +use crate::root::parser::location::{Location, ToLocation}; use crate::root::parser::parse_toplevel; use crate::root::parser::parse_toplevel::TopLevelTokens; +use crate::root::parser::use_parser::parse_uses; pub type Span<'a> = LocatedSpan<&'a str, &'a Rc>; @@ -17,14 +19,30 @@ pub type ParseResult<'a, I = Span<'a>, O = Span<'a>, E = ErrorTree<'a>> = IResul pub type ErrorTree<'a> = GenericErrorTree, &'static str, &'static str, String>; pub fn parse(path: PathBuf) -> Result, WErr> { - let text = fs::read_to_string(&path).unwrap(); - let path = Rc::new(path); - let base = Span::new_extra(&text, &path); - - let res = parse_toplevel::parse_toplevel(base); - let (remaining, output) = handle_error(res)?; - - debug_assert!(remaining.is_empty()); + let mut path_queue = vec![(path, Location::builtin())]; + let mut output = Vec::new(); + + while let Some((path, location)) = path_queue.pop() { + print!("\n - {}", path.display()); + let Ok(text) = fs::read_to_string(path.as_path()) else { + return WErr::ne( + ParseError::FailedToOpenFile(format!("{}", path.display())), + location + ); + }; + + let path = Rc::new(path); + let base = Span::new_extra(&text, &path); + + let (after_use, found_paths) = handle_error(parse_uses(base))?; + path_queue.extend(found_paths); + + let res = parse_toplevel::parse_toplevel(after_use); + let (remaining, new_output) = handle_error(res)?; + debug_assert!(remaining.is_empty()); + output.extend(new_output); + } + println!(); Ok(output) } diff --git a/src/root/parser/use_parser.rs b/src/root/parser/use_parser.rs index e69de29..e28a964 100644 --- a/src/root/parser/use_parser.rs +++ b/src/root/parser/use_parser.rs @@ -0,0 +1,41 @@ +use std::fmt::format; +use std::path::PathBuf; +use nom::bytes::complete::{tag, take_till}; +use nom::character::complete::anychar; +use nom::complete::take; +use crate::root::errors::parser_errors::create_custom_error; +use crate::root::parser::location::{Location, ToLocation}; +use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; +use crate::root::parser::parse_util::discard_ignored; + +pub fn parse_uses(s: Span) -> ParseResult> { + let mut s = s; + let mut found_paths = Vec::new(); + loop { + let (ns, _) = discard_ignored(s)?; + let Ok((ns, _)) = tag::<_, _, ErrorTree>("use")(ns) else { + return Ok((ns, found_paths)); + }; + + let (ns, _) = discard_ignored(ns)?; + let Ok((pre_s, path)) = take_till::<_, _, ErrorTree>(|c| c == ';' || c =='\n' || c=='\r')(ns) else { + return Err(create_custom_error( + "Did not find ending ';' when parsing path".to_string(), + ns, + )); + }; + + let (ns, next) = anychar::<_, ErrorTree>(pre_s).unwrap(); + if next != ';' { + return Err(create_custom_error( + "Use path cannot be broken by newline".to_string(), + pre_s, + )); + } + + let path_buf = PathBuf::from(format!("{}.why", path)); + found_paths.push((path_buf, Location::from_span(&path))); + + s = ns; + } +} \ No newline at end of file diff --git a/todo.md b/todo.md index 959d0d6..8c7865f 100644 --- a/todo.md +++ b/todo.md @@ -1,3 +1,3 @@ - Add array support -- Add support for multiple files +- Namespacing From 843edb17ba8cd31c2be8422d7b5f20ac370e25e0 Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas <100799838+Robert-M-Lucas@users.noreply.github.com> Date: Tue, 6 Aug 2024 16:21:27 +0100 Subject: [PATCH 4/4] fmt --- src/root.rs | 4 +-- src/root/errors/parser_errors.rs | 2 +- src/root/name_resolver/resolve_names.rs | 4 +-- src/root/parser/handle_errors.rs | 4 +-- src/root/parser/location.rs | 24 ++++++++--------- src/root/parser/mod.rs | 2 +- src/root/parser/parse.rs | 14 +++++----- src/root/parser/parse_function.rs | 12 ++++----- src/root/parser/parse_function/parse_break.rs | 6 ++--- .../parser/parse_function/parse_evaluable.rs | 6 ++--- src/root/parser/parse_function/parse_if.rs | 12 ++++----- .../parse_function/parse_initialisation.rs | 4 +-- .../parser/parse_function/parse_literal.rs | 2 +- .../parser/parse_function/parse_operator.rs | 2 +- .../parser/parse_function/parse_return.rs | 10 +++---- .../parse_function/parse_struct_init.rs | 10 +++---- src/root/parser/parse_function/parse_while.rs | 12 ++++----- src/root/parser/parse_impl.rs | 8 +++--- src/root/parser/parse_name.rs | 2 +- src/root/parser/parse_struct.rs | 8 +++--- src/root/parser/use_parser.rs | 26 ++++++++++--------- 21 files changed, 87 insertions(+), 87 deletions(-) diff --git a/src/root.rs b/src/root.rs index cef8d05..41b3475 100644 --- a/src/root.rs +++ b/src/root.rs @@ -1,7 +1,7 @@ -use crate::root::parser::parse::parse; use crate::root::compiler::compile::compile; use crate::root::errors::WErr; use crate::root::name_resolver::resolve::resolve; +use crate::root::parser::parse::parse; use crate::root::runner::{assemble, link_gcc, run}; use crate::time; use clap::Parser; @@ -101,7 +101,7 @@ pub fn main_args(args: Args) -> Result<(), WErr> { .unwrap() .len() .to_formatted_string(&Locale::en); - + cprintln!("Completed [{:?}] - {} bytes", end, size); if args.build { diff --git a/src/root/errors/parser_errors.rs b/src/root/errors/parser_errors.rs index b631090..10e8c4e 100644 --- a/src/root/errors/parser_errors.rs +++ b/src/root/errors/parser_errors.rs @@ -14,7 +14,7 @@ pub enum ParseError { #[error("Failed parsing {0}")] NomErrorKind(String), #[error("Failed to open file [{0}]")] - FailedToOpenFile(String) + FailedToOpenFile(String), } pub fn create_custom_error(e: String, l: Span) -> nom::Err { diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index 1f5455d..4f9a2b9 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; -use b_box::b; use crate::root::errors::evaluable_errors::EvalErrs; use crate::root::errors::name_resolver_errors::NRErrs; use crate::root::errors::WErr; @@ -16,8 +14,10 @@ use crate::root::shared::common::{ByteSize, FunctionID, TypeID}; use crate::root::shared::common::{LocalAddress, TypeRef}; use crate::root::shared::types::Type; use crate::root::unrandom::new_hashmap; +use b_box::b; use derive_getters::Getters; use itertools::Itertools; +use std::collections::HashMap; /// A Whython-code defined type #[derive(Getters)] diff --git a/src/root/parser/handle_errors.rs b/src/root/parser/handle_errors.rs index 6dbde53..2c093bf 100644 --- a/src/root/parser/handle_errors.rs +++ b/src/root/parser/handle_errors.rs @@ -6,9 +6,7 @@ use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_toplevel::TopLevelTokens; -pub fn handle_error( - res: ParseResult, -) -> Result<(A, B), WErr> { +pub fn handle_error(res: ParseResult) -> Result<(A, B), WErr> { match res { Ok(v) => Ok(v), Err(e) => match &e { diff --git a/src/root/parser/location.rs b/src/root/parser/location.rs index 7add73e..324c38c 100644 --- a/src/root/parser/location.rs +++ b/src/root/parser/location.rs @@ -1,31 +1,31 @@ -use std::marker::PhantomData; -use std::rc::Rc; -use std::path::PathBuf; -use std::fmt::{Display, Formatter}; -use std::fs; -use std::cmp::min; +use crate::root::parser::parse::Span; use color_print::cformat; use nom::InputTake; -use crate::root::parser::parse::Span; +use std::cmp::min; +use std::fmt::{Display, Formatter}; +use std::fs; +use std::marker::PhantomData; +use std::path::PathBuf; +use std::rc::Rc; pub enum ToLocation<'a> { Location(Location), - Span(Span<'a>) + Span(Span<'a>), } impl<'a> ToLocation<'a> { pub fn from_location(location: Location) -> ToLocation<'a> { ToLocation::Location(location) } - + pub fn from_span(span: Span<'a>) -> ToLocation<'a> { ToLocation::Span(span) } - + pub fn to_location(self) -> Location { match self { ToLocation::Location(location) => location, - ToLocation::Span(s) => Location::from_span(&s) + ToLocation::Span(s) => Location::from_span(&s), } } } @@ -293,4 +293,4 @@ impl Display for LocationTyped { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.fmt_choice(f, true) } -} \ No newline at end of file +} diff --git a/src/root/parser/mod.rs b/src/root/parser/mod.rs index 96c051f..d8e9c0e 100644 --- a/src/root/parser/mod.rs +++ b/src/root/parser/mod.rs @@ -1,4 +1,5 @@ mod handle_errors; +pub mod location; pub mod parse; pub mod parse_arguments; pub mod parse_blocks; @@ -13,4 +14,3 @@ pub mod parse_toplevel; pub mod parse_util; pub mod soft_alt; mod use_parser; -pub mod location; diff --git a/src/root/parser/parse.rs b/src/root/parser/parse.rs index 48b7203..f1832dc 100644 --- a/src/root/parser/parse.rs +++ b/src/root/parser/parse.rs @@ -2,9 +2,6 @@ use std::fs; use std::path::PathBuf; use std::rc::Rc; -use nom::IResult; -use nom_locate::LocatedSpan; -use nom_supreme::error::GenericErrorTree; use crate::root::errors::parser_errors::ParseError; use crate::root::errors::WErr; use crate::root::parser::handle_errors::handle_error; @@ -12,6 +9,9 @@ use crate::root::parser::location::{Location, ToLocation}; use crate::root::parser::parse_toplevel; use crate::root::parser::parse_toplevel::TopLevelTokens; use crate::root::parser::use_parser::parse_uses; +use nom::IResult; +use nom_locate::LocatedSpan; +use nom_supreme::error::GenericErrorTree; pub type Span<'a> = LocatedSpan<&'a str, &'a Rc>; @@ -21,22 +21,22 @@ pub type ErrorTree<'a> = GenericErrorTree, &'static str, &'static str, pub fn parse(path: PathBuf) -> Result, WErr> { let mut path_queue = vec![(path, Location::builtin())]; let mut output = Vec::new(); - + while let Some((path, location)) = path_queue.pop() { print!("\n - {}", path.display()); let Ok(text) = fs::read_to_string(path.as_path()) else { return WErr::ne( ParseError::FailedToOpenFile(format!("{}", path.display())), - location + location, ); }; let path = Rc::new(path); let base = Span::new_extra(&text, &path); - + let (after_use, found_paths) = handle_error(parse_uses(base))?; path_queue.extend(found_paths); - + let res = parse_toplevel::parse_toplevel(after_use); let (remaining, new_output) = handle_error(res)?; debug_assert!(remaining.is_empty()); diff --git a/src/root/parser/parse_function.rs b/src/root/parser/parse_function.rs index 63e2612..fbfdebb 100644 --- a/src/root/parser/parse_function.rs +++ b/src/root/parser/parse_function.rs @@ -1,20 +1,20 @@ +use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_blocks::{ - BRACE_TERMINATOR, BRACKET_TERMINATOR, parse_terminator_default_set, + parse_terminator_default_set, BRACE_TERMINATOR, BRACKET_TERMINATOR, }; use crate::root::parser::parse_function::parse_evaluable::{ - FullNameWithIndirectionToken, parse_full_name, + parse_full_name, FullNameWithIndirectionToken, }; -use crate::root::parser::parse_function::parse_line::{LineTokens, parse_lines}; +use crate::root::parser::parse_function::parse_line::{parse_lines, LineTokens}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; -use crate::root::parser::parse_parameters::{Parameters, parse_parameters, SelfType}; -use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; +use crate::root::parser::parse_parameters::{parse_parameters, Parameters, SelfType}; +use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use derive_getters::{Dissolve, Getters}; use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; -use crate::root::parser::location::Location; pub mod parse_assigner; pub mod parse_assignment; diff --git a/src/root/parser/parse_function/parse_break.rs b/src/root/parser/parse_function/parse_break.rs index 5953fcc..d2f2f0f 100644 --- a/src/root/parser/parse_function/parse_break.rs +++ b/src/root/parser/parse_function/parse_break.rs @@ -1,10 +1,10 @@ -use derive_getters::Getters; -use nom::character::complete::char; -use nom_supreme::tag::complete::tag; use crate::root::parser::location::Location; use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; use crate::root::parser::parse_util::discard_ignored; +use derive_getters::Getters; +use nom::character::complete::char; +use nom_supreme::tag::complete::tag; #[derive(Debug, Getters)] pub struct BreakToken { diff --git a/src/root/parser/parse_function/parse_evaluable.rs b/src/root/parser/parse_function/parse_evaluable.rs index cf114da..75e1474 100644 --- a/src/root/parser/parse_function/parse_evaluable.rs +++ b/src/root/parser/parse_function/parse_evaluable.rs @@ -8,9 +8,9 @@ use crate::root::errors::parser_errors::create_custom_error; use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_arguments::parse_arguments; -use crate::root::parser::parse_blocks::{BRACKET_TERMINATOR, parse_terminator_default_set}; -use crate::root::parser::parse_function::parse_literal::{LiteralToken, parse_literal}; -use crate::root::parser::parse_function::parse_operator::{OperatorToken, parse_operator}; +use crate::root::parser::parse_blocks::{parse_terminator_default_set, BRACKET_TERMINATOR}; +use crate::root::parser::parse_function::parse_literal::{parse_literal, LiteralToken}; +use crate::root::parser::parse_function::parse_operator::{parse_operator, OperatorToken}; use crate::root::parser::parse_function::parse_struct_init::{parse_struct_init, StructInitToken}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_util::discard_ignored; diff --git a/src/root/parser/parse_function/parse_if.rs b/src/root/parser/parse_function/parse_if.rs index 08c805d..93d2e45 100644 --- a/src/root/parser/parse_function/parse_if.rs +++ b/src/root/parser/parse_function/parse_if.rs @@ -1,15 +1,15 @@ -use derive_getters::Getters; -use nom::sequence::Tuple; -use nom_supreme::tag::complete::tag; use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_blocks::{ - BRACE_TERMINATOR, BRACKET_TERMINATOR, parse_terminator_default_set, + parse_terminator_default_set, BRACE_TERMINATOR, BRACKET_TERMINATOR, }; -use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; -use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens, parse_lines}; +use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; +use crate::root::parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; +use derive_getters::Getters; +use nom::sequence::Tuple; +use nom_supreme::tag::complete::tag; #[derive(Debug, Getters)] pub struct IfToken { diff --git a/src/root/parser/parse_function/parse_initialisation.rs b/src/root/parser/parse_function/parse_initialisation.rs index 2983de1..ef06803 100644 --- a/src/root/parser/parse_function/parse_initialisation.rs +++ b/src/root/parser/parse_function/parse_initialisation.rs @@ -1,6 +1,7 @@ +use crate::root::parser::location::Location; use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_function::parse_evaluable::{ - EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name, + parse_evaluable, parse_full_name, EvaluableToken, FullNameWithIndirectionToken, }; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; @@ -9,7 +10,6 @@ use derive_getters::Getters; use nom::character::complete::char; use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; -use crate::root::parser::location::Location; #[derive(Debug, Getters)] pub struct InitialisationToken { diff --git a/src/root/parser/parse_function/parse_literal.rs b/src/root/parser/parse_function/parse_literal.rs index 69b930e..2a1c92d 100644 --- a/src/root/parser/parse_function/parse_literal.rs +++ b/src/root/parser/parse_function/parse_literal.rs @@ -1,12 +1,12 @@ use crate::root::builtin::types::bool::BoolType; use crate::root::builtin::types::int::IntType; +use crate::root::parser::location::Location; use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_util::discard_ignored; use crate::root::shared::common::TypeID; use derive_getters::{Dissolve, Getters}; use nom::branch::alt; use nom::bytes::complete::tag; -use crate::root::parser::location::Location; #[derive(Debug, Dissolve, Getters)] pub struct LiteralToken { diff --git a/src/root/parser/parse_function/parse_operator.rs b/src/root/parser/parse_function/parse_operator.rs index 7754c62..0da38d7 100644 --- a/src/root/parser/parse_function/parse_operator.rs +++ b/src/root/parser/parse_function/parse_operator.rs @@ -1,10 +1,10 @@ +use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use derive_getters::Getters; use nom::Err::Error; use nom_supreme::error::GenericErrorTree; use nom_supreme::tag::complete::tag; use nom_supreme::tag::TagError; -use crate::root::parser::location::Location; #[derive(PartialEq, Debug)] pub enum PrefixOrInfix { diff --git a/src/root/parser/parse_function/parse_return.rs b/src/root/parser/parse_function/parse_return.rs index 3ffd02b..75e87d9 100644 --- a/src/root/parser/parse_function/parse_return.rs +++ b/src/root/parser/parse_function/parse_return.rs @@ -1,13 +1,13 @@ -use derive_getters::Getters; -use nom::character::complete::char; -use nom::sequence::Tuple; -use nom_supreme::tag::complete::tag; use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; -use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; +use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens}; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::require_ignored; +use derive_getters::Getters; +use nom::character::complete::char; +use nom::sequence::Tuple; +use nom_supreme::tag::complete::tag; #[derive(Debug, Getters)] pub struct ReturnToken { diff --git a/src/root/parser/parse_function/parse_struct_init.rs b/src/root/parser/parse_function/parse_struct_init.rs index 73f74ce..f82bbe2 100644 --- a/src/root/parser/parse_function/parse_struct_init.rs +++ b/src/root/parser/parse_function/parse_struct_init.rs @@ -1,17 +1,17 @@ -use derive_getters::{Dissolve, Getters}; -use nom::bytes::complete::tag; -use nom::character::streaming::char; use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_blocks::{ - BRACE_TERMINATOR, parse_terminator_default_set, take_until_or_end_discard_smart, + parse_terminator_default_set, take_until_or_end_discard_smart, BRACE_TERMINATOR, }; use crate::root::parser::parse_function::parse_evaluable::{ - EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name, + parse_evaluable, parse_full_name, EvaluableToken, FullNameWithIndirectionToken, }; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; use crate::root::parser::parse_util::discard_ignored; use crate::root::shared::common::Indirection; +use derive_getters::{Dissolve, Getters}; +use nom::bytes::complete::tag; +use nom::character::streaming::char; #[derive(Debug, Dissolve, Getters)] pub struct StructInitToken { diff --git a/src/root/parser/parse_function/parse_while.rs b/src/root/parser/parse_function/parse_while.rs index 7244bcb..8f342f6 100644 --- a/src/root/parser/parse_function/parse_while.rs +++ b/src/root/parser/parse_function/parse_while.rs @@ -1,15 +1,15 @@ -use derive_getters::Getters; -use nom::sequence::Tuple; -use nom_supreme::tag::complete::tag; use crate::root::parser::location::Location; use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_blocks::{ - BRACE_TERMINATOR, BRACKET_TERMINATOR, parse_terminator_default_set, + parse_terminator_default_set, BRACE_TERMINATOR, BRACKET_TERMINATOR, }; -use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; -use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens, parse_lines}; +use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; +use crate::root::parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; use crate::root::parser::parse_name::SimpleNameToken; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; +use derive_getters::Getters; +use nom::sequence::Tuple; +use nom_supreme::tag::complete::tag; #[derive(Debug, Getters)] pub struct WhileToken { diff --git a/src/root/parser/parse_impl.rs b/src/root/parser/parse_impl.rs index 93b19d6..d594e4a 100644 --- a/src/root/parser/parse_impl.rs +++ b/src/root/parser/parse_impl.rs @@ -1,14 +1,14 @@ +use crate::root::parser::location::Location; use crate::root::parser::parse::{ParseResult, Span}; -use crate::root::parser::parse_blocks::{BRACE_TERMINATOR, parse_terminator_default_set}; -use crate::root::parser::parse_function::{FunctionToken, parse_function}; +use crate::root::parser::parse_blocks::{parse_terminator_default_set, BRACE_TERMINATOR}; +use crate::root::parser::parse_function::{parse_function, FunctionToken}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; -use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; +use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use derive_getters::{Dissolve, Getters}; use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; -use crate::root::parser::location::Location; #[derive(Debug, Getters, Dissolve)] pub struct ImplToken { diff --git a/src/root/parser/parse_name.rs b/src/root/parser/parse_name.rs index dddfd14..a553604 100644 --- a/src/root/parser/parse_name.rs +++ b/src/root/parser/parse_name.rs @@ -1,9 +1,9 @@ +use crate::root::parser::location::Location; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use derive_getters::Getters; use nom::bytes::complete::take_till; use nom::Err::Error; use nom_supreme::error::{BaseErrorKind, Expectation}; -use crate::root::parser::location::Location; #[derive(Debug, Getters, Clone)] pub struct SimpleNameToken { diff --git a/src/root/parser/parse_struct.rs b/src/root/parser/parse_struct.rs index f135b3a..a5eb217 100644 --- a/src/root/parser/parse_struct.rs +++ b/src/root/parser/parse_struct.rs @@ -1,15 +1,15 @@ +use crate::root::parser::location::Location; use crate::root::parser::parse::{ParseResult, Span}; -use crate::root::parser::parse_blocks::{BRACE_TERMINATOR, parse_terminator_default_set}; +use crate::root::parser::parse_blocks::{parse_terminator_default_set, BRACE_TERMINATOR}; use crate::root::parser::parse_name::{parse_simple_name, SimpleNameToken}; -use crate::root::parser::parse_parameters::{Parameters, parse_parameters}; -use crate::root::parser::parse_toplevel::{ToplevelTestFn, TopLevelTokens}; +use crate::root::parser::parse_parameters::{parse_parameters, Parameters}; +use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; use crate::root::parser::parse_util::{discard_ignored, require_ignored}; use crate::root::shared::common::TypeID; use derive_getters::{Dissolve, Getters}; use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; -use crate::root::parser::location::Location; #[derive(Debug, Getters, Dissolve)] pub struct StructToken { diff --git a/src/root/parser/use_parser.rs b/src/root/parser/use_parser.rs index e28a964..70daa2f 100644 --- a/src/root/parser/use_parser.rs +++ b/src/root/parser/use_parser.rs @@ -1,30 +1,32 @@ -use std::fmt::format; -use std::path::PathBuf; -use nom::bytes::complete::{tag, take_till}; -use nom::character::complete::anychar; -use nom::complete::take; use crate::root::errors::parser_errors::create_custom_error; use crate::root::parser::location::{Location, ToLocation}; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_util::discard_ignored; +use nom::bytes::complete::{tag, take_till}; +use nom::character::complete::anychar; +use nom::complete::take; +use std::fmt::format; +use std::path::PathBuf; pub fn parse_uses(s: Span) -> ParseResult> { let mut s = s; let mut found_paths = Vec::new(); loop { let (ns, _) = discard_ignored(s)?; - let Ok((ns, _)) = tag::<_, _, ErrorTree>("use")(ns) else { + let Ok((ns, _)) = tag::<_, _, ErrorTree>("use")(ns) else { return Ok((ns, found_paths)); }; - + let (ns, _) = discard_ignored(ns)?; - let Ok((pre_s, path)) = take_till::<_, _, ErrorTree>(|c| c == ';' || c =='\n' || c=='\r')(ns) else { + let Ok((pre_s, path)) = + take_till::<_, _, ErrorTree>(|c| c == ';' || c == '\n' || c == '\r')(ns) + else { return Err(create_custom_error( "Did not find ending ';' when parsing path".to_string(), ns, )); }; - + let (ns, next) = anychar::<_, ErrorTree>(pre_s).unwrap(); if next != ';' { return Err(create_custom_error( @@ -32,10 +34,10 @@ pub fn parse_uses(s: Span) -> ParseResult> { pre_s, )); } - + let path_buf = PathBuf::from(format!("{}.why", path)); found_paths.push((path_buf, Location::from_span(&path))); - + s = ns; } -} \ No newline at end of file +}