Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Apr 26, 2024
1 parent 0b31b1a commit 93c98f2
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 221 deletions.
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions .idea/git_toolbox_prj.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/misc.xml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/whython-8.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/root.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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... ");
Expand Down
5 changes: 4 additions & 1 deletion src/root/name_resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod resolve;
pub mod resolve;
pub mod resolve_names;
pub mod resolve_type_sizes;
mod resolve_function_contents;
165 changes: 7 additions & 158 deletions src/root/name_resolver/resolve.rs
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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<isize>
}

// ! Intentionally unoptimised
pub fn resolve_names(ast: Vec<TopLevelTokens>) {
// ! User types > 1; Bultin Types < -1
let mut type_names: HashMap<TypeName, isize> = HashMap::new();
let mut type_id: isize = 1;
// ! (Name, (type, id)) - Type 0 means global
let mut function_names: HashMap<String, (isize, isize)> = 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<isize, UnsizedUserType> = 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<isize, UserType> = 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<isize, UserType>, unsized_types: &mut HashMap<isize, UnsizedUserType>, path: &mut Vec<isize>) -> 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<TopLevelTokens>) {
let (sized_types, type_names, unprocessed_functions) = resolve_names(ast);

size
resolve_function_contents(sized_types, type_names, unprocessed_functions);
}
14 changes: 14 additions & 0 deletions src/root/name_resolver/resolve_function_contents.rs
Original file line number Diff line number Diff line change
@@ -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<isize, UserType>, type_names: HashMap<TypeName, isize>, functions: Vec<(isize, FunctionToken)>) -> Vec<Function> {
// 1st pass - Convert let a =

todo!()
}
53 changes: 53 additions & 0 deletions src/root/name_resolver/resolve_type_sizes.rs
Original file line number Diff line number Diff line change
@@ -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<isize, UserType>, unsized_types: &mut HashMap<isize, UnsizedUserType>, path: &mut Vec<isize>) -> 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
}
1 change: 0 additions & 1 deletion src/root/parser/parse_toplevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/root/parser/parse_util.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading

0 comments on commit 93c98f2

Please sign in to comment.