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 25, 2024
1 parent 0c9f217 commit 0d61600
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 22 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nom = "7.1.3"
nom_locate = "4.2.0"
nom-supreme = "0.8.0"
substring = "1.4.5"
accessors-rs = "0.1.0"
derive-getters = "0.3.0"

[profile.release]
opt-level = 3
Expand Down
105 changes: 103 additions & 2 deletions src/root/name_resolver/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,106 @@
use crate::root::parser::parse_toplevel::TopLevelToken;
use std::collections::HashMap;

pub fn resolve_names(ast: Vec<TopLevelToken>) {
use derive_getters::Getters;

use crate::root::parser::{parse::Location, parse_function::FunctionToken, parse_toplevel::TopLevelTokens};

#[derive(Getters)]
struct TypeRef {
type_id: isize,
indirection: usize
}

impl TypeRef {
pub fn new(type_id: isize, indirection: usize) -> TypeRef {
TypeRef { type_id, indirection }
}
}

struct UnsizedUserType {
id: isize,
attributes: Vec<(String, TypeRef)>,
location: Location
}

impl UnsizedUserType {
pub fn new(id: isize, attributes: Vec<TypeRef>, location: Location) -> UnsizedUserType {
UnsizedUserType { id, attributes, location }
}
}

struct UserType {
id: isize,
size: usize,
attributes: Vec<(usize, String, TypeRef)>,
location: 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<String, 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) => {
// TODO: Name collision error
if type_names.contains_key(s.name()) {
panic!();
}
type_names.insert(s.name().clone(), 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(&name).unwrap();
unsized_final_types.insert(id, UnsizedUserType::new(id, attributes, location));
},
TopLevelTokens::Impl(i) => {
// TODO: Errors
let type_id = *type_names.get(i.name()).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();
}

fn resolve_types(id: isize, unsized_type: UnsizedUserType, final_types: &mut HashMap<isize, UserType>, path: &mut Vec<isize>) {
if path.contains(&id) {
// TODO: Circular type def
panic!()
}

}
2 changes: 1 addition & 1 deletion src/root/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod parse_arguments;
mod parse_blocks;
mod parse_comments;
mod parse_error;
mod parse_function;
pub mod parse_function;
mod parse_impl;
mod parse_name;
mod parse_parameters;
Expand Down
3 changes: 2 additions & 1 deletion src/root/parser/parse_function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use derive_getters::Getters;
use nom::sequence::Tuple;
use nom::Parser;
use nom_supreme::tag::complete::tag;
Expand All @@ -23,7 +24,7 @@ mod parse_operator;
mod parse_return;
mod parse_while;

#[derive(Debug)]
#[derive(Debug, Getters)]
pub struct FunctionToken {
location: Location,
name: String,
Expand Down
5 changes: 2 additions & 3 deletions src/root/parser/parse_function/parse_operator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span};
use accessors_rs::Accessors;
use derive_getters::Getters;
use nom::error::{ErrorKind, ParseError};
use nom::Err::Error;
use nom::Parser;
Expand Down Expand Up @@ -33,9 +33,8 @@ pub fn get_priority(operator: &OperatorTokens) -> usize {
panic!()
}

#[derive(Debug, Clone, Accessors)]
#[derive(Debug, Clone, Getters)]
pub struct OperatorToken {
#[accessors(get)]
location: Location,
operator: OperatorTokens,
}
Expand Down
4 changes: 3 additions & 1 deletion src/root/parser/parse_impl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use derive_getters::{Dissolve, Getters};
use nom::sequence::Tuple;
use nom::Parser;
use nom_supreme::tag::complete::tag;
Expand All @@ -9,13 +10,14 @@ use crate::root::parser::parse_name::{NameToken, parse_simple_name};
use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn};
use crate::root::parser::parse_util::{discard_ignored, require_ignored};

#[derive(Debug)]
#[derive(Debug, Getters, Dissolve)]
pub struct ImplToken {
location: Location,
name: String,
functions: Vec<FunctionToken>,
}


pub fn test_parse_impl<'a>(s: Span<'a>) -> ParseResult<Span, ToplevelTestFn<'a>> {
match (tag("impl"), require_ignored).parse(s) {
Ok(_) => Ok((s, |x| {
Expand Down
3 changes: 2 additions & 1 deletion src/root/parser/parse_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span};
use crate::root::parser::parse_name::{parse_simple_name, NameToken};
use crate::root::parser::parse_parameters::{parse_parameters, Parameters};
use crate::root::parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn};
use derive_getters::{Dissolve, Getters};
use nom::character::complete::{ satisfy};
use nom::sequence::Tuple;
use nom::Err::Error;
Expand All @@ -12,7 +13,7 @@ use substring::Substring;
use crate::root::parser::parse_blocks::default_section;
use crate::root::parser::parse_util::{discard_ignored, require_ignored};

#[derive(Debug)]
#[derive(Debug, Getters, Dissolve)]
pub struct StructToken {
location: Location,
name: String,
Expand Down

0 comments on commit 0d61600

Please sign in to comment.