Skip to content

Commit

Permalink
Improved name evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 5, 2024
1 parent 3e44ea6 commit d15ab64
Show file tree
Hide file tree
Showing 21 changed files with 470 additions and 254 deletions.
28 changes: 25 additions & 3 deletions .idea/workspace.xml

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

2 changes: 1 addition & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ struct A {
b: int
}

fn main() -> intt {
fn main() !-> int {
return 12;
}
2 changes: 1 addition & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn main_args(args: Args) -> Result<(), WError> {

print!("Parsing... ");
time!(
let parsed = parse(PathBuf::from(&args.input)).unwrap();
let parsed = parse(PathBuf::from(&args.input))?;
);

print!("Resolving Names... ");
Expand Down
32 changes: 2 additions & 30 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
use crate::root::parser::parse::Location;
use crate::root::shared::types::Type;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_name::UnresolvedNameToken;
use crate::root::parser::parse_struct::StructToken;
use crate::root::shared::common::{AddressedTypeRef, FunctionID, TypeID, TypeRef};

Expand Down Expand Up @@ -168,31 +167,14 @@ impl GlobalDefinitionTable {
}

pub fn add_type(&mut self, given_id: TypeID, definition: Box<dyn Type>) {
// TODO: handle collisions
self.type_definitions.insert(given_id, definition);
}

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)) =
local_variable_table.get_ref_and_type(
temp_name, &self.type_definitions
) {
return NameResult::Variable(a, t);
}
pub fn resolve_name(&self, name: &UnresolvedNameToken, local_variable_table: &LocalVariableTable) -> NameResult {
todo!()
}

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

if let Some((a, t)) =
local_variable_table.get_ref_and_type(
temp_name, &self.type_definitions
) {
return NameResult::Variable(a, t);
}
pub fn resolve_global_name(&self, name: &UnresolvedNameToken) -> NameResult {
todo!()
}

Expand Down Expand Up @@ -225,16 +207,6 @@ impl GlobalDefinitionTable {
return Err(WError::n(NRErrors::FunctionSubname(next.clone(), method_name.clone()), location.clone()));
}

// match connector {
// NameConnectors::NonStatic => {
// if !*function_signatures.get(function).unwrap().has_self() {
//
// return Err(());
// }
// }
// NameConnectors::Static => {}
// }

NameResultId::Function(*function)
}
else {
Expand Down
1 change: 0 additions & 1 deletion src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::root::name_resolver::resolve_type_sizes::{resolve_type_sizes, Unsized
use crate::root::parser::parse::Location;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::parser::parse_function::parse_literal::LiteralToken;
use crate::root::parser::parse_name::UnresolvedNameToken;
use crate::root::parser::parse_toplevel::TopLevelTokens;
use crate::root::shared::common::{LocalAddress, TypeRef};
use crate::root::shared::common::{ByteSize, FunctionID, TypeID};
Expand Down
3 changes: 2 additions & 1 deletion src/root/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ pub mod parse_blocks;
pub mod parse_comments;
pub mod parse_function;
pub mod parse_impl;
pub mod parse_name;
pub mod parse_name_old;
pub mod parse_parameters;
pub mod parse_struct;
pub mod parse_toplevel;
pub mod parse_util;
pub mod soft_alt;
mod parse_name;
8 changes: 6 additions & 2 deletions src/root/parser/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::path::PathBuf;
use std::rc::Rc;
use color_print::cformat;
use derive_getters::Getters;
use crate::root::errors::WError;
use crate::root::parser::parse_toplevel::TopLevelTokens;

pub type Span<'a> = LocatedSpan<&'a str, &'a Rc<PathBuf>>;
Expand Down Expand Up @@ -121,12 +122,15 @@ impl Display for Location {
}
}

pub fn parse(path: PathBuf) -> Result<Vec<TopLevelTokens>, ()> {
pub fn parse(path: PathBuf) -> Result<Vec<TopLevelTokens>, WError> {
let text = fs::read_to_string(&path).unwrap();
let path = Rc::new(path);
let base = Span::new_extra(&text, &path);

let (remaining, output) = parse_toplevel::parse_toplevel(base).map_err(|_| ())?;
let (remaining, output) = match parse_toplevel::parse_toplevel(base) {
Ok(v) => v,
Err(_) => todo!()
};
debug_assert!(remaining.is_empty());

Ok(output)
Expand Down
5 changes: 3 additions & 2 deletions src/root/parser/parse_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use nom::bytes::complete::{take_until, take_until1};
use nom::InputTake;
use crate::root::parser::parse::{ErrorTree, ParseResult, Span};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable};
use crate::root::parser::parse_name::SimpleNameToken;

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

Ok((s, args))
Ok(((), args))
}
25 changes: 13 additions & 12 deletions src/root/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use nom_supreme::tag::complete::tag;
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_evaluable::{FullNameWithIndirectionToken, parse_full_name};
use crate::root::parser::parse_function::parse_line::{LineTokens, parse_lines};
use crate::root::parser::parse_name::{parse_full_name, parse_simple_name, UnresolvedNameToken};
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_util::{discard_ignored, require_ignored};
Expand All @@ -26,8 +27,8 @@ pub mod parse_while;
#[derive(Debug, Getters, Dissolve)]
pub struct FunctionToken {
location: Location,
name: String,
return_type: Option<(UnresolvedNameToken, Location)>,
name: SimpleNameToken,
return_type: Option<FullNameWithIndirectionToken>,
parameters: Parameters,
lines: Vec<LineTokens>,
}
Expand All @@ -41,16 +42,16 @@ pub fn test_parse_function<'a>(s: Span<'a>) -> ParseResult<Span, ToplevelTestFn<
}
}

pub fn parse_function(s: Span, allow_self: Option<UnresolvedNameToken>) -> ParseResult<Span, FunctionToken> {
pub fn parse_function<'a, 'b>(s: Span<'a>, allow_self: Option<&'b SimpleNameToken>) -> ParseResult<'a, Span<'a>, 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 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 @@ -60,21 +61,21 @@ pub fn parse_function(s: Span, allow_self: Option<UnresolvedNameToken>) -> Parse
let (s, return_type) = if let Ok((s, _)) = tag::<_, _, ErrorTree>("->")(s) {
let (s, _) = discard_ignored(s)?;
let location = Location::from_span(&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, location)))
let (s, return_type) = parse_full_name(s, allow_self)?;
(discard_ignored(s)?.0, Some(return_type))
} else {
(s, None)
};

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

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

Ok((
s,
FunctionToken {
location,
name: name.to_string(),
name,
return_type,
parameters,
lines,
Expand Down
10 changes: 5 additions & 5 deletions src/root/parser/parse_function/parse_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use nom::sequence::Tuple;
use nom_supreme::tag::complete::tag;
use crate::root::parser::parse::{Location, ParseResult, Span};
use crate::root::parser::parse_function::parse_assigner::{AssignmentOperatorToken, parse_assigner};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable};
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, FullNameWithIndirectionToken, parse_evaluable, parse_full_name};
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::{UnresolvedNameToken, parse_full_name};
use crate::root::parser::parse_name::SimpleNameToken;
use crate::root::parser::parse_util::discard_ignored;

#[derive(Debug)]
pub struct AssignmentToken {
location: Location,
name: UnresolvedNameToken,
name: FullNameWithIndirectionToken,
assignment_operator: AssignmentOperatorToken,
value: EvaluableToken,
}
Expand All @@ -27,10 +27,10 @@ pub fn test_parse_assignment<'a, 'b>(s: Span<'a>) -> ParseResult<'a, Span<'a>, L
Ok((s, |x, c| parse_assignment(x, c).map(|(s, x)| (s, LineTokens::Assignment(x)))))
}

pub fn parse_assignment<'a, 'b>(s: Span<'a>, containing_class: Option<&'b str>) -> ParseResult<'a, Span<'a>, AssignmentToken> {
pub fn parse_assignment<'a, 'b>(s: Span<'a>, containing_class: Option<&SimpleNameToken>) -> ParseResult<'a, Span<'a>, AssignmentToken> {
let (s, _) = discard_ignored(s)?;
let location = Location::from_span(&s);
let (s, n) = parse_full_name(s, containing_class.and_then(|s| Some(s.to_string())))?;
let (s, n) = parse_full_name(s, containing_class)?;
let (s, _) = discard_ignored(s)?;
let (s, a) = parse_assigner(s)?;
let (s, _) = discard_ignored(s)?;
Expand Down
Loading

0 comments on commit d15ab64

Please sign in to comment.