Skip to content

Commit

Permalink
Add module port names in initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Apr 21, 2024
1 parent 8490a4d commit 5d3683b
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 74 deletions.
9 changes: 9 additions & 0 deletions src/arena_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub struct UUIDRange<IndexMarker : UUIDMarker>(pub UUID<IndexMarker>, pub UUID<I
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct UUIDRangeIter<IndexMarker : UUIDMarker>(UUID<IndexMarker>, UUID<IndexMarker>);

impl<IndexMarker : UUIDMarker> UUIDRange<IndexMarker> {
pub fn empty() -> Self {
UUIDRange(UUID(0, PhantomData), UUID(0, PhantomData))
}
}

impl<IndexMarker : UUIDMarker> Iterator for UUIDRange<IndexMarker> {
type Item = UUID<IndexMarker>;

Expand Down Expand Up @@ -408,6 +414,9 @@ impl<T, IndexMarker : UUIDMarker> FlatAlloc<T, IndexMarker> {
pub fn iter_mut<'a>(&'a mut self) -> FlatAllocIterMut<'a, T, IndexMarker> {
self.into_iter()
}
pub fn range_since(&self, id : UUID<IndexMarker>) -> UUIDRange<IndexMarker> {
UUIDRange(id, UUID(self.data.len(), PhantomData))
}
}

impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for FlatAlloc<T, IndexMarker> {
Expand Down
9 changes: 1 addition & 8 deletions src/compiler_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::{
file_position::FileText,
flattening::{initialization::gather_initial_file_data, FlattenedModule},
instantiation::InstantiatedModule,
linker::{FileData, FileUUID, Linker, ModuleUUID},
parser::report_all_tree_errors
linker::{FileData, FileUUID, Linker, ModuleUUID}
};

pub fn add_file(text : String, linker : &mut Linker) -> FileUUID {
Expand All @@ -24,10 +23,6 @@ pub fn add_file(text : String, linker : &mut Linker) -> FileUUID {
associated_values : Vec::new()
});

let file_data = &linker.files[file_id];

report_all_tree_errors(&file_data.file_text, &file_data.tree, &file_data.parsing_errors);

let mut builder = linker.get_file_builder(file_id);
gather_initial_file_data(&mut builder);

Expand All @@ -45,8 +40,6 @@ pub fn update_file(text : String, file_id : FileUUID, linker : &mut Linker) {
file_data.file_text = FileText::new(text);
file_data.tree = tree;

report_all_tree_errors(&file_data.file_text, &file_data.tree, &file_data.parsing_errors);

let mut builder = linker.get_file_builder(file_id);
gather_initial_file_data(&mut builder);
}
Expand Down
6 changes: 5 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ impl ErrorCollector {
self.errors.get_mut().clear();
}

pub fn new_for_same_file(&self) -> Self {
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)}
}

pub fn new_for_same_file_inherit_did_error(&self) -> Self {
Self{errors : RefCell::new(Vec::new()), file : self.file, file_len : self.file_len, did_error : self.did_error.clone()}
}

Expand Down
145 changes: 125 additions & 20 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,137 @@

use sus_proc_macro::{field, kind};


use crate::{
errors::ErrorCollector,
flattening::{FlattenedModule, Module},
instantiation::InstantiationList,
linker::{FileBuilder, LinkInfo},
parser::Cursor
arena_alloc::{FlatAlloc, UUIDMarker, UUIDRange, UUID}, file_position::{FileText, Span}, flattening::{FlattenedModule, Module}, instantiation::InstantiationList, linker::{FileBuilder, LinkInfo}, parser::Cursor
};

use super::IdentifierType;



#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PortIDMarker;
impl UUIDMarker for PortIDMarker {const DISPLAY_NAME : &'static str = "port_";}
pub type PortID = UUID<PortIDMarker>;

pub type PortIDRange = UUIDRange<PortIDMarker>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InterfaceIDMarker;
impl UUIDMarker for InterfaceIDMarker {const DISPLAY_NAME : &'static str = "port_";}
pub type InterfaceID = UUID<InterfaceIDMarker>;


#[derive(Debug)]
pub struct Port {
pub name : String,
pub name_span : Span,
pub decl_span : Span,
pub id_typ : IdentifierType,
pub interface : InterfaceID
}

#[derive(Debug)]
pub struct Interface {
pub func_call_inputs : PortIDRange,
pub func_call_outputs : PortIDRange
}


#[derive(Debug)]
pub struct ModulePorts {
pub ports : FlatAlloc<Port, PortIDMarker>,
pub interfaces : FlatAlloc<Interface, InterfaceIDMarker>
}

impl ModulePorts {
/// Get a port by the given name. Returns None if it does not exist
pub fn get_port_by_name(&self, name : &str) -> Option<PortID> {
for (id, data) in &self.ports {
if data.name == name {
return Some(id)
}
}
return None
}

/// This function is intended to retrieve a known port while walking the syntax tree. panics if the port doesn't exist
pub fn get_port_by_decl_span(&self, span : Span) -> PortID {
for (id, data) in &self.ports {
if data.decl_span == span {
return id
}
}
unreachable!()
}
}

pub fn gather_initial_file_data(builder : &mut FileBuilder) {
let mut walker = Cursor::new_at_root(builder.tree, builder.file_text);
walker.list(kind!("source_file"), |cursor| {
let mut cursor = Cursor::new_at_root(builder.tree, builder.file_text);
cursor.list_and_report_errors(kind!("source_file"), &builder.other_parsing_errors, |cursor| {
let (kind, span) = cursor.kind_span();
assert!(kind == kind!("module"));
let name_span = cursor.go_down_no_check(|cursor| cursor.field_span(field!("name"), kind!("identifier")));
let md = Module{
link_info: LinkInfo {
documentation: cursor.extract_gathered_comments(),
file: builder.file_id,
name: builder.file_text[name_span].to_owned(),
name_span,
span
match kind {
kind!("module") => {
let parsing_errors = builder.other_parsing_errors.new_for_same_file_inherit_did_error();
cursor.report_all_decendant_errors(&parsing_errors);
cursor.go_down_no_check(|cursor| {
let name_span = cursor.field_span(field!("name"), kind!("identifier"));

let mut ports = FlatAlloc::new();
let mut interfaces = FlatAlloc::new();

let main_interface = interfaces.get_next_alloc_id();

let mut func_call_inputs = PortIDRange::empty();
let mut func_call_outputs = PortIDRange::empty();

if cursor.optional_field(field!("interface_ports")) {
if cursor.optional_field(field!("inputs")) {
func_call_inputs = gather_decl_names_in_list(IdentifierType::Input, main_interface, &mut ports, cursor, builder.file_text);
}
if cursor.optional_field(field!("outputs")) {
func_call_outputs = gather_decl_names_in_list(IdentifierType::Output, main_interface, &mut ports, cursor, builder.file_text);
}
}

interfaces.alloc(Interface{func_call_inputs, func_call_outputs});

let md = Module{
link_info: LinkInfo {
documentation: cursor.extract_gathered_comments(),
file: builder.file_id,
name: builder.file_text[name_span].to_owned(),
name_span,
span
},
flattened: FlattenedModule::empty(parsing_errors.new_for_same_file_inherit_did_error()),
module_ports : ModulePorts{
ports,
interfaces
},
parsing_errors,
instantiations: InstantiationList::new()
};

builder.add_module(md);
});
},
flattened: FlattenedModule::empty(ErrorCollector::new(builder.file_id, builder.file_text.len())),
instantiations: InstantiationList::new()
};
_other => cursor.could_not_match()
}
});
}

builder.add_module(md);
fn gather_decl_names_in_list(id_typ: IdentifierType, interface : InterfaceID, ports: &mut FlatAlloc<Port, PortIDMarker>, cursor: &mut Cursor, file_text : &FileText) -> PortIDRange {
let list_start_at = ports.get_next_alloc_id();
cursor.list(kind!("declaration_list"), |cursor| {
let decl_span = cursor.span();
cursor.go_down(kind!("declaration"), |cursor| {
cursor.field(field!("type"));
let name_span = cursor.field_span(field!("name"), kind!("identifier"));
let name = file_text[name_span].to_owned();
ports.alloc(Port{name, name_span, decl_span, id_typ, interface})
});
});
ports.range_since(list_start_at)
}
6 changes: 5 additions & 1 deletion src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
value::Value
};

use self::name_context::LocalVariableContext;
use self::{initialization::ModulePorts, name_context::LocalVariableContext};



Expand Down Expand Up @@ -90,6 +90,10 @@ impl<ID : Clone + Copy> InterfacePorts<ID> {
pub struct Module {
pub link_info : LinkInfo,

pub parsing_errors : ErrorCollector,

pub module_ports : ModulePorts,

pub flattened : FlattenedModule,

pub instantiations : InstantiationList
Expand Down
2 changes: 1 addition & 1 deletion src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ impl InstantiationList {
specified_latencies : Vec::new(),
flattened : &flattened,
linker : linker,
errors : flattened.errors.new_for_same_file()
errors : flattened.errors.new_for_same_file_inherit_did_error()
};

let interface = context.instantiate_full();
Expand Down
3 changes: 3 additions & 0 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ impl Linker {
match v {
NameElem::Module(md_id) => {
let md = &self.modules[*md_id];
errors.ingest(&md.parsing_errors);
errors.ingest(&md.flattened.errors);
md.instantiations.collect_errors(errors);
}
Expand Down Expand Up @@ -342,6 +343,7 @@ impl Linker {
file_id,
tree: &file_data.tree,
file_text: &file_data.file_text,
other_parsing_errors : &file_data.parsing_errors,
associated_values: &mut file_data.associated_values,
global_namespace: &mut self.global_namespace,
types: &mut self.types,
Expand All @@ -357,6 +359,7 @@ pub struct FileBuilder<'linker> {
pub file_id : FileUUID,
pub tree : &'linker Tree,
pub file_text : &'linker FileText,
pub other_parsing_errors : &'linker ErrorCollector,
associated_values : &'linker mut Vec<NameElem>,
global_namespace : &'linker mut HashMap<String, NamespaceElement>,
#[allow(dead_code)]
Expand Down
Loading

0 comments on commit 5d3683b

Please sign in to comment.