Skip to content

Commit

Permalink
Extract typechecking into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Apr 21, 2024
1 parent 5d3683b commit e5fc2bb
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 307 deletions.
5 changes: 3 additions & 2 deletions src/compiler_top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tree_sitter::Parser;
use crate::{
errors::ErrorCollector,
file_position::FileText,
flattening::{initialization::gather_initial_file_data, FlattenedModule},
flattening::{initialization::gather_initial_file_data, typechecking::typecheck_all_modules, FlattenedModule},
instantiation::InstantiatedModule,
linker::{FileData, FileUUID, Linker, ModuleUUID}
};
Expand Down Expand Up @@ -52,13 +52,14 @@ pub fn recompile_all(linker : &mut Linker) {
println!("Flattening {}", md.link_info.name);

let flattened = FlattenedModule::flatten(&linker, md);
println!("Typechecking {}", &md.link_info.name);

let md = &mut linker.modules[id]; // Convert to mutable ptr
md.flattened = flattened;
md.instantiations.clear_instances();
}

typecheck_all_modules(linker);

// Can't merge these loops, because instantiation can only be done once all modules have been type checked
for (id, _md) in &linker.modules {
//md.print_flattened_module();
Expand Down
14 changes: 8 additions & 6 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ pub struct Port {

#[derive(Debug)]
pub struct Interface {
pub ports_for_this_interface : PortIDRange,
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 {
const MAIN_INTERFACE_ID : InterfaceID = InterfaceID::from_hidden_value(0);

/// 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 {
Expand Down Expand Up @@ -81,21 +83,21 @@ pub fn gather_initial_file_data(builder : &mut FileBuilder) {
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();

let ports_start_at = ports.get_next_alloc_id();

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);
func_call_inputs = gather_decl_names_in_list(IdentifierType::Input, ModulePorts::MAIN_INTERFACE_ID, &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);
func_call_outputs = gather_decl_names_in_list(IdentifierType::Output, ModulePorts::MAIN_INTERFACE_ID, &mut ports, cursor, builder.file_text);
}
}

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

let md = Module{
link_info: LinkInfo {
Expand Down
Loading

0 comments on commit e5fc2bb

Please sign in to comment.