From 93c98f24389fa0c00e1578172fdc9e0c7337dc28 Mon Sep 17 00:00:00 2001 From: Robert Lucas <100799838+Robert-M-Lucas@users.noreply.github.com> Date: Fri, 26 Apr 2024 01:03:09 +0100 Subject: [PATCH] Changes --- .idea/.gitignore | 8 - .idea/git_toolbox_prj.xml | 15 -- .idea/misc.xml | 6 - .idea/vcs.xml | 2 +- .idea/whython-8.iml | 6 - src/root.rs | 5 +- src/root/name_resolver/mod.rs | 5 +- src/root/name_resolver/resolve.rs | 165 +----------------- .../resolve_function_contents.rs | 14 ++ src/root/name_resolver/resolve_type_sizes.rs | 53 ++++++ src/root/parser/parse_toplevel.rs | 1 - src/root/parser/parse_util.rs | 7 +- src/root/utils.rs | 20 --- 13 files changed, 86 insertions(+), 221 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/git_toolbox_prj.xml delete mode 100644 .idea/misc.xml create mode 100644 src/root/name_resolver/resolve_function_contents.rs create mode 100644 src/root/name_resolver/resolve_type_sizes.rs diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml deleted file mode 100644 index 02b915b..0000000 --- a/.idea/git_toolbox_prj.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index cafab9f..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/whython-8.iml b/.idea/whython-8.iml index ae4a2df..bbe0a70 100644 --- a/.idea/whython-8.iml +++ b/.idea/whython-8.iml @@ -1,10 +1,5 @@ - - - - - @@ -13,6 +8,5 @@ - \ No newline at end of file diff --git a/src/root.rs b/src/root.rs index 607089a..ced7335 100644 --- a/src/root.rs +++ b/src/root.rs @@ -1,4 +1,4 @@ -use crate::root::name_resolver::resolve::resolve_names; +use name_resolver::resolve_names::resolve_names; use crate::root::parser::parse::parse; // use crate::root::assembler::assemble::generate_assembly; // use crate::root::name_resolver::processor::process; @@ -8,6 +8,7 @@ use color_print::cprintln; use std::fs; use std::io::ErrorKind; use std::path::PathBuf; +use crate::root::name_resolver::resolve::resolve; // #[cfg(target_os = "windows")] // use crate::root::runner::run; @@ -70,7 +71,7 @@ pub fn main_args(args: Args) { let parsed = parse(PathBuf::from(&args.input)).unwrap(); ); - resolve_names(parsed); + resolve(parsed); // print!("Compiling... "); diff --git a/src/root/name_resolver/mod.rs b/src/root/name_resolver/mod.rs index 3c6b865..8b649e5 100644 --- a/src/root/name_resolver/mod.rs +++ b/src/root/name_resolver/mod.rs @@ -1 +1,4 @@ -pub mod resolve; \ No newline at end of file +pub mod resolve; +pub mod resolve_names; +pub mod resolve_type_sizes; +mod resolve_function_contents; \ No newline at end of file diff --git a/src/root/name_resolver/resolve.rs b/src/root/name_resolver/resolve.rs index ae37b65..d7a2147 100644 --- a/src/root/name_resolver/resolve.rs +++ b/src/root/name_resolver/resolve.rs @@ -1,16 +1,14 @@ use std::collections::HashMap; - use derive_getters::{Dissolve, Getters}; -use itertools::Itertools; - -use crate::root::POINTER_SIZE; +use crate::root::name_resolver::resolve_function_contents::resolve_function_contents; +use crate::root::name_resolver::resolve_names::{resolve_names, UserType}; use crate::root::parser::parse_name::NameToken; -use crate::root::parser::parse_function::FunctionToken; use crate::root::parser::parse::Location; +use crate::root::parser::parse_function::FunctionToken; use crate::root::parser::parse_toplevel::TopLevelTokens; #[derive(Getters)] -struct TypeRef { +pub struct TypeRef { type_id: isize, indirection: usize } @@ -21,163 +19,14 @@ impl TypeRef { } } -#[derive(Hash)] -struct TypeName { - name: String -} - -impl TypeName { - pub fn from_string(name: String) -> TypeName { - TypeName { name } - } - - pub fn from_name_token(name: NameToken) -> TypeName { - TypeName { name: name.dissolve().1 } - } -} - -impl PartialEq for TypeName { - fn eq(&self, other: &Self) -> bool { - self.name == other.name - } -} - -impl Eq for TypeName {} - - -#[derive(Dissolve)] -struct UnsizedUserType { - id: isize, - attributes: Vec<(String, TypeRef)>, - location: Location -} - -impl UnsizedUserType { - pub fn new(id: isize, attributes: Vec<(String, TypeRef)>, location: Location) -> UnsizedUserType { - UnsizedUserType { id, attributes, location } - } -} - -#[derive(Getters)] -struct UserType { - id: isize, - size: usize, - attributes: Vec<(usize, String, TypeRef)>, - location: Location -} - -impl UserType { - pub fn new(id: isize, size: usize, attributes: Vec<(usize, String, TypeRef)>, location: Location) -> UserType { - UserType { id, size, attributes, location } - } -} struct Function { id: isize, args: Vec } -// ! Intentionally unoptimised -pub fn resolve_names(ast: Vec) { - // ! User types > 1; Bultin Types < -1 - let mut type_names: HashMap = HashMap::new(); - let mut type_id: isize = 1; - // ! (Name, (type, id)) - Type 0 means global - let mut function_names: HashMap = HashMap::new(); - let mut function_id: isize = 1; - - // * Type names - - for symbol in &ast { - match symbol { - TopLevelTokens::Struct(s) => { - let name = TypeName::from_string(s.name().clone()); - // TODO: Name collision error - if type_names.get(&name).is_none() { - todo!(); - } - type_names.insert(name, type_id); - type_id += 1; - }, - TopLevelTokens::Impl(_) => {}, - TopLevelTokens::Function(_) => {} - }; - } - - let mut unsized_final_types: HashMap = HashMap::new(); - let mut unprocessed_functions: Vec<(isize, FunctionToken)> = Vec::new(); - - for symbol in ast { - match symbol { - TopLevelTokens::Struct(s) => { - let (location, name, attributes) = s.dissolve(); - let id = *type_names.get(&TypeName::from_string(name.clone())).unwrap(); - // TODO: Process indirection - let attributes = attributes.into_iter() - .map(|(name, type_name)| { - // TODO: Name error - (name, TypeRef::new(*type_names.get(&TypeName::from_name_token(type_name)).unwrap(), 0)) - } - ).collect_vec(); - unsized_final_types.insert(id, UnsizedUserType::new(id, attributes, location)); - }, - TopLevelTokens::Impl(i) => { - // TODO: Errors - let type_id = *type_names.get(&TypeName::from_string(i.name().clone())).unwrap(); - - for function in i.dissolve().2 { - function_names.insert(function.name().clone(), (type_id, function_id)); - function_id += 1; - unprocessed_functions.push((type_id, function)); - } - }, - TopLevelTokens::Function(f) => { - function_names.insert(f.name().clone(), (0, function_id)); - function_id += 1; - unprocessed_functions.push((0, f)); - } - }; - } - - let mut final_types: HashMap = HashMap::new(); - - while !unsized_final_types.is_empty() { - let next_type_id = *unsized_final_types.keys().next().unwrap(); - let unsized_type = unsized_final_types.remove(&next_type_id).unwrap(); - resolve_types(unsized_type, &mut final_types, &mut unsized_final_types, &mut Vec::new()); - } -} - -fn resolve_types(unsized_type: UnsizedUserType, final_types: &mut HashMap, unsized_types: &mut HashMap, path: &mut Vec) -> usize { - let (id, attributes, location) = unsized_type.dissolve(); - - if path.contains(&id) { - // TODO: Circular type def error - todo!(); - } - path.push(id); - - let mut size: usize = 0; - let mut processed_attributes: Vec<(usize, String, TypeRef)> = Vec::new(); - - for (attribute_name, attribute_type) in attributes { - let offset = size; - - if *attribute_type.indirection() != 0 { - size += POINTER_SIZE; - } - else if let Some(sized_type) = final_types.get(&attribute_type.type_id()) { - size += sized_type.size(); - } - else { - let unsized_type = unsized_types.remove(&attribute_type.type_id()).unwrap(); - size += resolve_types(unsized_type, final_types, unsized_types, path); - } - - processed_attributes.push((offset, attribute_name, attribute_type)); - } - - final_types.insert(id, UserType::new(id, size, processed_attributes, location)); +pub fn resolve(ast: Vec) { + let (sized_types, type_names, unprocessed_functions) = resolve_names(ast); - size + resolve_function_contents(sized_types, type_names, unprocessed_functions); } \ No newline at end of file diff --git a/src/root/name_resolver/resolve_function_contents.rs b/src/root/name_resolver/resolve_function_contents.rs new file mode 100644 index 0000000..3bd8372 --- /dev/null +++ b/src/root/name_resolver/resolve_function_contents.rs @@ -0,0 +1,14 @@ +use std::collections::HashMap; +use crate::root::name_resolver::resolve::TypeRef; +use crate::root::name_resolver::resolve_names::{TypeName, UserType}; +use crate::root::parser::parse_function::FunctionToken; + +struct Function { + +} + +pub fn resolve_function_contents(types: HashMap, type_names: HashMap, functions: Vec<(isize, FunctionToken)>) -> Vec { + // 1st pass - Convert let a = + + todo!() +} \ No newline at end of file diff --git a/src/root/name_resolver/resolve_type_sizes.rs b/src/root/name_resolver/resolve_type_sizes.rs new file mode 100644 index 0000000..6fde14c --- /dev/null +++ b/src/root/name_resolver/resolve_type_sizes.rs @@ -0,0 +1,53 @@ +use std::collections::HashMap; +use derive_getters::{Dissolve, Getters}; +use crate::root::name_resolver::resolve::TypeRef; +use crate::root::name_resolver::resolve_names::UserType; +use crate::root::parser::parse::Location; +use crate::root::POINTER_SIZE; + +#[derive(Dissolve)] +pub struct UnsizedUserType { + id: isize, + attributes: Vec<(String, TypeRef)>, + location: Location +} + +impl UnsizedUserType { + pub fn new(id: isize, attributes: Vec<(String, TypeRef)>, location: Location) -> UnsizedUserType { + UnsizedUserType { id, attributes, location } + } +} + +pub fn resolve_type_sizes(unsized_type: UnsizedUserType, final_types: &mut HashMap, unsized_types: &mut HashMap, path: &mut Vec) -> usize { + let (id, attributes, location) = unsized_type.dissolve(); + + if path.contains(&id) { + // TODO: Circular type def error + todo!(); + } + path.push(id); + + let mut size: usize = 0; + let mut processed_attributes: Vec<(usize, String, TypeRef)> = Vec::new(); + + for (attribute_name, attribute_type) in attributes { + let offset = size; + + if *attribute_type.indirection() != 0 { + size += POINTER_SIZE; + } + else if let Some(sized_type) = final_types.get(&attribute_type.type_id()) { + size += sized_type.size(); + } + else { + let unsized_type = unsized_types.remove(&attribute_type.type_id()).unwrap(); + size += resolve_type_sizes(unsized_type, final_types, unsized_types, path); + } + + processed_attributes.push((offset, attribute_name, attribute_type)); + } + + final_types.insert(id, UserType::new(id, size, processed_attributes, location)); + + size +} diff --git a/src/root/parser/parse_toplevel.rs b/src/root/parser/parse_toplevel.rs index 25c7926..923ed25 100644 --- a/src/root/parser/parse_toplevel.rs +++ b/src/root/parser/parse_toplevel.rs @@ -2,7 +2,6 @@ use crate::root::parser::parse::{ParseResult, Span}; use crate::root::parser::parse_function::{parse_function, test_parse_function, FunctionToken}; use crate::root::parser::parse_impl::{parse_impl, test_parse_impl, ImplToken}; use crate::root::parser::parse_struct::{parse_struct, test_parse_struct, StructToken}; -use crate::root::parser::parse_util; use nom::branch::alt; use nom::Parser; use crate::root::parser::parse_util::discard_ignored; diff --git a/src/root/parser/parse_util.rs b/src/root/parser/parse_util.rs index 7843874..25fb081 100644 --- a/src/root/parser/parse_util.rs +++ b/src/root/parser/parse_util.rs @@ -1,10 +1,11 @@ use nom::bytes::complete::{take_till, take_while}; use nom::error::{ErrorKind, ParseError}; -use nom::sequence::Tuple; -use nom::{IResult, InputTakeAtPosition, Parser}; +use nom::Parser; use nom::character::complete::multispace1; use nom::Err::Error; -use nom_supreme::error::{BaseErrorKind, Expectation}; + +use super::parse::ErrorTree; +use super::{parse::{ParseResult, Span}, parse_comments}; pub fn take_whitespace(s: Span) -> ParseResult { take_while(|c: char| c.is_whitespace())(s) diff --git a/src/root/utils.rs b/src/root/utils.rs index b5bee7d..a1d0385 100644 --- a/src/root/utils.rs +++ b/src/root/utils.rs @@ -1,5 +1,3 @@ -use crate::root::name_resolver::processor::ProcessorError; -use crate::root::parser::parse::ParseError; use std::io; use std::io::ErrorKind; use std::ops::{Add, Rem, Sub}; @@ -44,21 +42,3 @@ pub fn align + Rem + Add>( ) -> T { num + (alignment - (num % alignment)) % alignment } - -pub enum AnyError { - Parse(ParseError), - Processing(ProcessorError), - Other, -} - -impl From for AnyError { - fn from(value: ParseError) -> Self { - AnyError::Parse(value) - } -} - -impl From for AnyError { - fn from(value: ProcessorError) -> Self { - AnyError::Processing(value) - } -}