Skip to content

Commit

Permalink
Continued import implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Aug 10, 2024
1 parent 921b588 commit 526a8f8
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 78 deletions.
35 changes: 22 additions & 13 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
@@ -1,4 +1,4 @@
use std/linked_list.why;
use std/;


fn main() -> int {
Expand Down
27 changes: 25 additions & 2 deletions src/root/compiler/compile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;

#[cfg(debug_assertions)]
use itertools::Itertools;
use std::collections::HashMap;
use std::io::{stdout, Write};
use std::thread;
use std::time::{Duration, Instant};

use crate::root::compiler::compile_function::compile_function;
use crate::root::compiler::global_tracker::GlobalTracker;
Expand All @@ -19,10 +21,14 @@ pub fn compile(
path_storage: &PathStorage,
) -> Result<String, WErr> {
let mut unprocessed_functions = unprocessed_functions;
// TODO: Write assembly to disk asynchronously while compiling
let mut compiled_functions = new_hashmap();
let mut compiled_len = 0usize;

let mut open_set = new_hashset();
let mut compiled_count: usize = 0;
let mut last_shown = Instant::now();

open_set.insert(FunctionID(0)); // Start with main
let mut global_tracker = GlobalTracker::new(path_storage);

Expand All @@ -31,6 +37,8 @@ pub fn compile(

let current_function = *open_set.iter().next().unwrap();
open_set.remove(&current_function);

compiled_count += 1;

let Some(current_function_token) = unprocessed_functions.remove(&current_function) else {
continue; // Inline function
Expand All @@ -51,7 +59,22 @@ pub fn compile(
open_set.insert(*called);
}
}

if Instant::now() - last_shown > Duration::from_millis(1000) {
print!(
"\n - {}/{} Functions Compiled",
compiled_count,
open_set.len() + compiled_count
);
last_shown = Instant::now();
}
}
print!(
"\n - {}/{} Functions Compiled",
compiled_count,
open_set.len() + compiled_count
);
println!();

let mut s = String::with_capacity(compiled_len);

Expand Down
2 changes: 1 addition & 1 deletion src/root/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod parse_blocks;
pub mod parse_comments;
pub mod parse_function;
pub mod parse_impl;
mod parse_imports;
pub mod parse_name;
pub mod parse_name_old;
pub mod parse_parameters;
Expand All @@ -14,4 +15,3 @@ pub mod parse_toplevel;
pub mod parse_util;
pub mod path_storage;
pub mod soft_alt;
mod use_parser;
4 changes: 2 additions & 2 deletions src/root/parser/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::root::errors::parser_errors::ParseError;
use crate::root::errors::WErr;
use crate::root::parser::handle_errors::handle_error;
use crate::root::parser::location::Location;
use crate::root::parser::parse_imports::parse_imports;
use crate::root::parser::parse_toplevel;
use crate::root::parser::parse_toplevel::TopLevelTokens;
use crate::root::parser::path_storage::{FileID, PathStorage};
use crate::root::parser::use_parser::parse_uses;

pub type Span<'a> = LocatedSpan<&'a str, FileID>;

Expand All @@ -35,7 +35,7 @@ pub fn parse(path_storage: &mut PathStorage) -> Result<Vec<TopLevelTokens>, WErr

let base = Span::new_extra(&text, file_id);

let (after_use, new_files) = handle_error(parse_uses(base, path_storage), path_storage)?;
let (after_use, new_files) = handle_error(parse_imports(base, path_storage, file_id), path_storage)?;
path_queue.extend(new_files);

let res = parse_toplevel::parse_toplevel(after_use);
Expand Down
16 changes: 11 additions & 5 deletions src/root/parser/use_parser.rs → src/root/parser/parse_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ use crate::root::parser::parse::{ErrorTree, ParseResult, Span};
use crate::root::parser::parse_util::discard_ignored;
use crate::root::parser::path_storage::{FileID, PathStorage};

pub fn parse_uses<'a>(
pub fn parse_imports<'a>(
s: Span<'a>,
path_storage: &mut PathStorage,
current_file: FileID,
) -> ParseResult<'a, Span<'a>, Vec<(FileID, Location)>> {
let mut s = s;
let mut found_paths = Vec::new();
loop {
let (ns, _) = discard_ignored(s)?;
let Ok((ns, _)) = tag::<_, _, ErrorTree>("use")(ns) else {
let mut is_use = true;

let Ok((ns, _)) = tag::<_, _, ErrorTree>("use")(ns).or_else(|_| {
is_use = false;
tag::<_, _, ErrorTree>("import")(ns)
}) else {
return Ok((ns, found_paths));
};

Expand All @@ -37,9 +43,9 @@ pub fn parse_uses<'a>(
));
}

let (_, (id, new)) = path_storage.get_file_path_id_checked(path)?;

if new {
let (_, ids) = path_storage.get_id_and_add_to_file(current_file, is_use, path)?;
for id in ids {
found_paths.push((id, Location::from_span(&path)));
}

Expand Down
Loading

0 comments on commit 526a8f8

Please sign in to comment.