Skip to content

Commit

Permalink
Improved name system
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 2, 2024
1 parent 3e9c2a8 commit 0e1d9bd
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

43 changes: 32 additions & 11 deletions .idea/workspace.xml

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

26 changes: 16 additions & 10 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use std::collections::HashMap;
use crate::root::name_resolver::resolve::{AddressedTypeRef, FunctionSignature};
use crate::root::name_resolver::resolve_names::Type;
use crate::root::parser::parse_name::NameToken;
use crate::root::parser::parse_name::UnresolvedNameToken;

/// Contents of a `DefinitionTable`
enum DefinitionTableEntry {
Table(Box<DefinitionTable>),
Function(isize),
Type(isize),
}

/// Recursive tree containing all named objects/functions/types
struct DefinitionTable {
table: HashMap<String, DefinitionTableEntry>
}

struct VariableTable {
/// Function-local table of defined variables. Only used within function processing
struct LocalVariableTable {
table: HashMap<String, AddressedTypeRef>
}

impl VariableTable {
impl LocalVariableTable {
pub fn new() { todo!(); }

pub fn get_ref(&self, name: &str) -> Option<AddressedTypeRef> {
Expand All @@ -44,33 +47,36 @@ impl VariableTable {
}
}

struct FullNameTable {
struct GlobalDefinitionTable {
builtin_type_definitions: HashMap<isize, Box<dyn Type>>,
global_type_definitions: HashMap<isize, Box<dyn Type>>,
global_function_signatures: HashMap<isize, FunctionSignature>,
// global_variables
global_definition_table: DefinitionTable,
local_variable_table: VariableTable
definition_table: DefinitionTable
}


enum NameResult<'a> {
Function(&'a FunctionSignature),
Type(&'a dyn Type),
Variable(AddressedTypeRef, &'a dyn Type)
}

impl FullNameTable {
impl GlobalDefinitionTable {
pub fn new() { todo!(); }

pub fn resolve_name(&self, name: &NameToken) -> NameResult {
pub fn resolve_local_name(&self, name: &UnresolvedNameToken, local_variable_table: &LocalVariableTable) -> NameResult {
let temp_name = &name.names()[0].1;

if let Some((a, t)) =
self.local_variable_table.get_ref_and_type(
local_variable_table.get_ref_and_type(
temp_name, &[&self.global_type_definitions, &self.builtin_type_definitions]
) {
return NameResult::Variable(a, t);
}
todo!()
}

pub fn resolve_global_name(&self, name: &UnresolvedNameToken) -> NameResult {
todo!()
}
}
8 changes: 5 additions & 3 deletions src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::root::name_resolver::resolve::TypeRef;
use crate::root::name_resolver::resolve_type_sizes::{resolve_type_sizes, UnsizedUserType};
use crate::root::parser::parse::Location;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_name::NameToken;
use crate::root::parser::parse_name::UnresolvedNameToken;
use crate::root::parser::parse_toplevel::TopLevelTokens;

#[derive(Hash)]
Expand All @@ -19,8 +19,8 @@ impl TypeName {
TypeName { name }
}

pub fn from_name_token(name: NameToken) -> TypeName {
TypeName { name: name.dissolve().1 }
pub fn from_name_token(name: UnresolvedNameToken) -> TypeName {
TypeName { name: name.dissolve().4 }
}
}

Expand All @@ -34,6 +34,8 @@ impl Eq for TypeName {}

pub trait Type {}


/// A whython-code-defined type
#[derive(Getters)]
pub struct UserType {
id: isize,
Expand Down
3 changes: 2 additions & 1 deletion src/root/parser/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use nom_supreme::error::GenericErrorTree;
use std::fs;
use std::path::PathBuf;
use std::rc::Rc;
use derive_getters::Getters;
use crate::root::parser::parse_toplevel::TopLevelTokens;

pub type Span<'a> = LocatedSpan<&'a str, &'a Rc<PathBuf>>;
Expand All @@ -17,7 +18,7 @@ pub type ErrorTree<'a> = GenericErrorTree<
Box<dyn std::error::Error + Send + Sync + 'static>,
>;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Getters)]
pub struct Location {
path: Rc<PathBuf>,
offset: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/root/parser/parse_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nom::InputTake;
use crate::root::parser::parse::{ErrorTree, ParseResult, Span};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable};

pub fn parse_arguments(s: Span) -> ParseResult<Span, Vec<EvaluableToken>> {
pub fn parse_arguments<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, Vec<EvaluableToken>> {
let mut s = s;
let mut args = Vec::new();
let mut last = false;
Expand All @@ -17,7 +17,7 @@ pub fn parse_arguments(s: Span) -> ParseResult<Span, Vec<EvaluableToken>> {
s.take_split(s.len())
};

args.push(parse_evaluable(section, false)?.1);
args.push(parse_evaluable(section, containing_class, false)?.1);

s = ns;
if last {
Expand Down
14 changes: 9 additions & 5 deletions src/root/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use substring::Substring;
use crate::root::parser::parse::{ErrorTree, Location, ParseResult, Span};
use crate::root::parser::parse_blocks::default_section;
use crate::root::parser::parse_function::parse_line::{parse_lines, LineTokens};
use crate::root::parser::parse_name::{parse_full_name, parse_simple_name, NameToken};
use crate::root::parser::parse_name::{parse_full_name, parse_simple_name, UnresolvedNameToken};
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};
Expand All @@ -28,7 +28,7 @@ pub mod parse_while;
pub struct FunctionToken {
location: Location,
name: String,
return_type: Option<NameToken>,
return_type: Option<UnresolvedNameToken>,
parameters: Parameters,
lines: Vec<LineTokens>,
}
Expand All @@ -42,12 +42,16 @@ pub fn test_parse_function<'a>(s: Span<'a>) -> ParseResult<Span, ToplevelTestFn<
}
}

pub fn parse_function(s: Span, allow_self: Option<NameToken>) -> ParseResult<Span, FunctionToken> {
pub fn parse_function(s: Span, allow_self: Option<UnresolvedNameToken>) -> ParseResult<Span, FunctionToken> {
let location = Location::from_span(&s);
let (s, _) = tag("fn").parse(s)?;
let (s, _) = require_ignored(s)?;
let (s, name) = parse_simple_name(s)?;
let (s, _) = discard_ignored(s)?;
let c_owned = allow_self.as_ref().and_then(|s| Some(s.base().to_string()));
let containing_class = if let Some(s) = &c_owned {
Some(s.as_str())
} else { None };

let (s, contents) = default_section(s, '(')?;
let (_, parameters) = parse_parameters(contents, allow_self)?;
Expand All @@ -56,15 +60,15 @@ pub fn parse_function(s: Span, allow_self: Option<NameToken>) -> ParseResult<Spa

let (s, return_type) = if let Ok((s, _)) = tag::<_, _, ErrorTree>("->")(s) {
let (s, _) = discard_ignored(s)?;
let (s, return_type) = parse_full_name(s)?;
let (s, return_type) = parse_full_name(s, containing_class.and_then(|s| Some(s.to_string())))?;
(discard_ignored(s)?.0, Some(return_type))
} else {
(s, None)
};

let (s, contents) = default_section(s, '{')?;

let (_, lines) = parse_lines(contents)?;
let (_, lines) = parse_lines(contents, containing_class)?;

Ok((
s,
Expand Down
16 changes: 8 additions & 8 deletions src/root/parser/parse_function/parse_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ use crate::root::parser::parse_function::parse_assigner::{AssignmentOperatorToke
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable};
use crate::root::parser::parse_function::parse_initialisation::parse_initialisation;
use crate::root::parser::parse_function::parse_line::{LineTestFn, LineTokens};
use crate::root::parser::parse_name::{NameToken, parse_full_name};
use crate::root::parser::parse_name::{UnresolvedNameToken, parse_full_name};
use crate::root::parser::parse_util::discard_ignored;

#[derive(Debug)]
pub struct AssignmentToken {
location: Location,
name: NameToken,
name: UnresolvedNameToken,
assignment_operator: AssignmentOperatorToken,
value: EvaluableToken,
}

// TODO: Find good way to implement?
pub fn test_parse_assignment<'a>(s: Span<'a>) -> ParseResult<Span, LineTestFn<'a>> {
pub fn test_parse_assignment<'a, 'b>(s: Span<'a>) -> ParseResult<'a, Span<'a>, LineTestFn<'a, 'b>> {
let (s, _) = discard_ignored(s)?;
let (s, _) = parse_full_name(s)?;
let (s, _) = parse_full_name(s, None)?;
let (s, _) = discard_ignored(s)?;
let (s, _) = parse_assigner(s)?;

Ok((s, |x| parse_assignment(x).map(|(s, x)| (s, LineTokens::Assignment(x)))))
Ok((s, |x, c| parse_assignment(x, c).map(|(s, x)| (s, LineTokens::Assignment(x)))))
}

pub fn parse_assignment(s: Span) -> ParseResult<Span, AssignmentToken> {
pub fn parse_assignment<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, AssignmentToken> {
let (s, _) = discard_ignored(s)?;
let location = Location::from_span(&s);
let (s, n) = parse_full_name(s)?;
let (s, n) = parse_full_name(s, containing_class.and_then(|s| Some(s.to_string())))?;
let (s, _) = discard_ignored(s)?;
let (s, a) = parse_assigner(s)?;
let (s, _) = discard_ignored(s)?;
let (s, e) = parse_evaluable(s, true)?;
let (s, e) = parse_evaluable(s, containing_class, true)?;
Ok((s, AssignmentToken {
location,
name: n,
Expand Down
4 changes: 2 additions & 2 deletions src/root/parser/parse_function/parse_break.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub struct BreakToken {
location: Location,
}

pub fn test_parse_break<'a>(s: Span<'a>) -> ParseResult<Span, LineTestFn<'a>> {
pub fn test_parse_break<'a, 'b>(s: Span<'a>) -> ParseResult<Span, LineTestFn<'a, 'b>> {
match tag("break")(s) {
Ok(_) => Ok((s, |x| {
Ok(_) => Ok((s, |x, _| {
parse_break(x).map(|(s, x)| (s, LineTokens::Break(x)))
})),
Err(e) => Err(e),
Expand Down
10 changes: 5 additions & 5 deletions src/root/parser/parse_function/parse_evaluable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::root::parser::parse_function::parse_literal::{
parse_literal, LiteralToken, LiteralTokens,
};
use crate::root::parser::parse_function::parse_operator::{parse_operator, OperatorToken};
use crate::root::parser::parse_name::{parse_full_name, NameToken};
use crate::root::parser::parse_name::{parse_full_name, UnresolvedNameToken};
use b_box::b;
use nom::branch::alt;
use nom::character::complete::{char};
Expand All @@ -25,7 +25,7 @@ pub fn temp_from_token(s: Span, token: EvaluableTokens) -> TempEvaluableTokens {

#[derive(Debug)]
enum EvaluableTokens {
Name(NameToken),
Name(UnresolvedNameToken),
Literal(LiteralToken),
InfixOperator(Box<EvaluableToken>, OperatorToken, Box<EvaluableToken>),
PrefixOperator(OperatorToken, Box<EvaluableToken>),
Expand All @@ -37,7 +37,7 @@ enum TempEvaluableTokens {
Operator(OperatorToken),
}

pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span, EvaluableToken> {
pub fn parse_evaluable<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>, semicolon_terminated: bool) -> ParseResult<'a, Span<'a>, EvaluableToken> {
assert!(!s.is_empty());
let mut s = s;

Expand Down Expand Up @@ -66,7 +66,7 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
}

let ns = if let Ok((ns, _)) = default_section(s, '(') {
let (ns, evaluable) = parse_evaluable(ns, false)?;
let (ns, evaluable) = parse_evaluable(ns, containing_class.clone(), false)?;
evaluables.push(TempEvaluableTokens::EvaluableToken(evaluable));
ns
} else {
Expand All @@ -77,7 +77,7 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
},
|x| parse_operator(x).map(|(s, t)| (s, TempEvaluableTokens::Operator(t))),
|x| {
parse_full_name(x)
parse_full_name(x, containing_class.and_then(|s| Some(s.to_string())))
.map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Name(t))))
},
))(ns)?;
Expand Down
Loading

0 comments on commit 0e1d9bd

Please sign in to comment.