Skip to content

Commit

Permalink
Fix LSP crash when editing. Future proof a little
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed May 6, 2024
1 parent 7c921ea commit e642ef9
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 14 deletions.
30 changes: 20 additions & 10 deletions multiply_add.sus
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,12 @@ module generative : int i -> int o, int o2 {
o2 = a[a[0]]
}

module add_stuff_to_indices : int[10] values -> int[10] added_values {
int[5] arr
module add_indices_to_array :
int[10] values -> int[10] added_values {

for int i in 0..10 {
int t = values[i]
added_values[i] = t + i

int tt = arr[i] + values[0]
}
}

Expand Down Expand Up @@ -368,7 +367,6 @@ module first_bit_idx_6 : bool[6] bits -> int first, bool all_zeros {

}


module multiply_add_with_latencies : int a'0, int b'0, int c'0 -> int r'0 {
int tmp'1 = multiply(a, b)
reg r = tmp + c
Expand Down Expand Up @@ -472,13 +470,13 @@ module conflicting_latency_declarations : int a'0 -> int x'1 {
}

module bad_cycle : int a -> int r {
state int test
initial test = 0
state int state_reg
initial state_reg = 0

reg int new_test = test + a
test = new_test
reg int new_value = state_reg + a
state_reg = new_value

r = new_test
r = new_value
}

module module_taking_time :
Expand Down Expand Up @@ -567,3 +565,15 @@ module contains_submodule_submodule : int a, int b, int c -> int r {
reg r = tmp + c
}


module xor2 : bool x1, bool x2 -> bool y {
bool w1 = !x1
bool w2 = !x2

bool w3 = x1 & w2
bool w4 = x2 & w1

y = w3 | w4
}


7 changes: 7 additions & 0 deletions resetNormalizer.sus
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module contains_submodule_submodule : int a, int b, int c -> int r {
// Temp val
int tmp = submodule(a, b)
doNothing()
reg r = tmp + c
}



module beeep {
Expand Down
6 changes: 6 additions & 0 deletions src/compiler_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub fn update_file(text : String, file_id : FileUUID, linker : &mut Linker) {
}

pub fn recompile_all(linker : &mut Linker) {
// First reset all modules back to post-gather_initial_file_data
for (_, md) in &mut linker.modules {
md.link_info.reset_to(md.link_info.after_initial_parse_cp);
md.instantiations.clear_instances()
}

flatten_all_modules(linker);
typecheck_all_modules(linker);

Expand Down
9 changes: 8 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::cell::{RefCell, Cell};

use crate::{linker::FileUUID, file_position::Span};
use crate::{file_position::Span, linker::{checkpoint::ErrorCheckpoint, FileUUID}};

#[derive(Debug,Clone,PartialEq,Eq)]
pub enum ErrorLevel {
Expand Down Expand Up @@ -49,6 +49,13 @@ impl ErrorCollector {
self.file_len = file_len;
self.errors.get_mut().clear();
}
pub fn reset_to(&mut self, checkpoint : ErrorCheckpoint) {
self.errors.borrow_mut().truncate(checkpoint.0);
self.did_error.set(checkpoint.1);
}
pub fn checkpoint(&self) -> ErrorCheckpoint {
ErrorCheckpoint(self.errors.borrow().len(), self.did_error.get())
}

pub fn new_for_same_file_clean_did_error(&self) -> Self {
Self{errors : RefCell::new(Vec::new()), file : self.file, file_len : self.file_len, did_error : Cell::new(false)}
Expand Down
8 changes: 6 additions & 2 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
file_position::{FileText, Span},
flattening::Module,
instantiation::InstantiationList,
linker::{FileBuilder, LinkInfo, ResolvedGlobals},
linker::{checkpoint::CheckPoint, FileBuilder, LinkInfo, ResolvedGlobals},
parser::Cursor
};

Expand Down Expand Up @@ -110,6 +110,9 @@ pub fn gather_initial_file_data(builder : &mut FileBuilder) {

interfaces.alloc(Interface{func_call_inputs, func_call_outputs, ports_for_this_interface : ports.range_since(ports_start_at)});

let resolved_globals = ResolvedGlobals::empty();
let after_initial_parse_cp = CheckPoint::checkpoint(&parsing_errors, &resolved_globals);

let md = Module{
link_info: LinkInfo {
documentation: cursor.extract_gathered_comments(),
Expand All @@ -118,7 +121,8 @@ pub fn gather_initial_file_data(builder : &mut FileBuilder) {
name_span,
span,
errors : parsing_errors,
resolved_globals : ResolvedGlobals::empty()
resolved_globals,
after_initial_parse_cp
},
instructions : FlatAlloc::new(),
module_ports : ModulePorts{
Expand Down
32 changes: 32 additions & 0 deletions src/linker/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::errors::ErrorCollector;

use super::{LinkInfo, ResolvedGlobals};

#[derive(Debug,Clone,Copy)]
pub struct ErrorCheckpoint(pub usize, pub bool);

#[derive(Debug,Clone,Copy)]
pub struct ResolvedGlobalsCheckpoint(pub usize, pub bool);


#[derive(Debug,Clone,Copy)]
pub struct CheckPoint {
errors_cp : ErrorCheckpoint,
resolved_globals_cp : ResolvedGlobalsCheckpoint
}

impl CheckPoint {
pub fn checkpoint(errors : &ErrorCollector, resolved_globals : &ResolvedGlobals) -> CheckPoint {
CheckPoint {
errors_cp : errors.checkpoint(),
resolved_globals_cp : resolved_globals.checkpoint()
}
}
}

impl LinkInfo {
pub fn reset_to(&mut self, cp : CheckPoint) {
self.errors.reset_to(cp.errors_cp);
self.resolved_globals.reset_to(cp.resolved_globals_cp);
}
}
8 changes: 7 additions & 1 deletion src/linker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod checkpoint;
mod resolver;
pub use resolver::*;

Expand All @@ -16,6 +17,8 @@ use crate::{
value::{TypedValue, Value}
};

use self::checkpoint::CheckPoint;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ModuleUUIDMarker;
impl UUIDMarker for ModuleUUIDMarker {const DISPLAY_NAME : &'static str = "module_";}
Expand Down Expand Up @@ -72,7 +75,10 @@ pub struct LinkInfo {
pub span : Span,
pub documentation : Documentation,
pub errors : ErrorCollector,
pub resolved_globals : ResolvedGlobals
pub resolved_globals : ResolvedGlobals,

/// Reset checkpoints. These are to reset errors and resolved_globals
pub after_initial_parse_cp : CheckPoint
}

impl LinkInfo {
Expand Down
9 changes: 9 additions & 0 deletions src/linker/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use std::ops::Deref;

use self::checkpoint::ResolvedGlobalsCheckpoint;

use super::*;


Expand All @@ -19,6 +21,13 @@ impl ResolvedGlobals {
pub fn is_untouched(&self) -> bool {
self.referenced_globals.is_empty() && self.all_resolved
}
pub fn reset_to(&mut self, checkpoint : ResolvedGlobalsCheckpoint) {
self.referenced_globals.truncate(checkpoint.0);
self.all_resolved = checkpoint.1;
}
pub fn checkpoint(&self) -> ResolvedGlobalsCheckpoint {
ResolvedGlobalsCheckpoint(self.referenced_globals.len(), self.all_resolved)
}
}


Expand Down
1 change: 1 addition & 0 deletions tinyTestFile.sus
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module named_ports : int in_port -> int out_port, int out_port_two {
out_port = in_port
out_port_two = in_port
Expand Down

0 comments on commit e642ef9

Please sign in to comment.