WIP.
This commit is contained in:
parent
639d052e9c
commit
4dd3140538
@ -540,7 +540,6 @@ impl Class {
|
||||
nodes_to_symbols: &NodesToSymbols,
|
||||
symbols_to_types: &SymbolsToTypes,
|
||||
nodes_to_types: &NodesToTypes,
|
||||
function_return_types: &FunctionReturnTypes,
|
||||
) -> (IrClass, Vec<IrFunction>) {
|
||||
let mut ir_functions = Vec::new();
|
||||
|
||||
@ -564,7 +563,6 @@ impl Class {
|
||||
nodes_to_symbols,
|
||||
symbols_to_types,
|
||||
nodes_to_types,
|
||||
function_return_types,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ use crate::ast::function::Function;
|
||||
use crate::ast::helpers::{
|
||||
collect_diagnostics_into_mut, insert_declared_types_into, insert_resolved_types_into,
|
||||
};
|
||||
use crate::ast::{FunctionReturnTypes, NodesToSymbols, NodesToTypes, SymbolsToTypes};
|
||||
use crate::ast::{NodesToSymbols, NodesToTypes, SymbolsToTypes};
|
||||
use crate::compile_pipeline::FileId;
|
||||
use crate::diagnostic::{Diagnostic, Diagnostics};
|
||||
use crate::ir::ir_class::IrClass;
|
||||
@ -250,7 +250,6 @@ impl CompilationUnit {
|
||||
nodes_to_symbols: &NodesToSymbols,
|
||||
symbols_to_types: &SymbolsToTypes,
|
||||
nodes_to_types: &NodesToTypes,
|
||||
function_return_types: &FunctionReturnTypes,
|
||||
) -> (Vec<IrClass>, Vec<IrFunction>) {
|
||||
let mut ir_classes = Vec::new();
|
||||
let mut ir_functions = Vec::new();
|
||||
@ -260,17 +259,12 @@ impl CompilationUnit {
|
||||
nodes_to_symbols,
|
||||
symbols_to_types,
|
||||
nodes_to_types,
|
||||
function_return_types,
|
||||
));
|
||||
}
|
||||
|
||||
for class in &self.classes {
|
||||
let (ir_class, mut class_ir_functions) = class.lower_to_ir(
|
||||
nodes_to_symbols,
|
||||
symbols_to_types,
|
||||
nodes_to_types,
|
||||
function_return_types,
|
||||
);
|
||||
let (ir_class, mut class_ir_functions) =
|
||||
class.lower_to_ir(nodes_to_symbols, symbols_to_types, nodes_to_types);
|
||||
ir_classes.push(ir_class);
|
||||
ir_functions.append(&mut class_ir_functions);
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ use crate::ast::ir_builder::IrBuilder;
|
||||
use crate::ast::parameter::Parameter;
|
||||
use crate::ast::statement::Statement;
|
||||
use crate::ast::type_use::TypeUse;
|
||||
use crate::ast::{FunctionReturnTypes, NodeId, NodesToSymbols, NodesToTypes, SymbolsToTypes};
|
||||
use crate::ast::{NodeId, NodesToSymbols, NodesToTypes, SymbolsToTypes};
|
||||
use crate::diagnostic::{Diagnostic, Diagnostics};
|
||||
use crate::ir::ir_function::IrFunction;
|
||||
use crate::ir::ir_parameter::IrParameter;
|
||||
@ -216,18 +216,33 @@ impl Function {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn declared_types(&self, names_table: &NodesToSymbols) -> (SymbolsToTypes, Diagnostics) {
|
||||
pub fn declared_types(
|
||||
&self,
|
||||
nodes_to_symbols: &NodesToSymbols,
|
||||
) -> (SymbolsToTypes, Diagnostics) {
|
||||
let mut diagnostics = Diagnostics::new();
|
||||
let mut declared_types = SymbolsToTypes::new();
|
||||
let mut symbols_to_types = SymbolsToTypes::new();
|
||||
|
||||
for parameter in &self.parameters {
|
||||
let symbol = names_table.get(¶meter.node_id()).unwrap();
|
||||
let (type_info, mut ds) = parameter.declared_type(names_table);
|
||||
declared_types.insert(symbol.clone(), type_info);
|
||||
let symbol = nodes_to_symbols.get(¶meter.node_id()).unwrap();
|
||||
let (type_info, mut ds) = parameter.declared_type(nodes_to_symbols);
|
||||
symbols_to_types.insert(symbol.clone(), type_info);
|
||||
diagnostics.append(&mut ds);
|
||||
}
|
||||
|
||||
(declared_types, diagnostics)
|
||||
// Insert return type as the type of this function symbol
|
||||
let symbol = nodes_to_symbols.get(&self.node_id).unwrap().clone();
|
||||
let type_info = match &self.return_type {
|
||||
None => TypeInfo::Void,
|
||||
Some(type_use) => {
|
||||
let (type_info, mut ds) = type_use.declared_type(nodes_to_symbols);
|
||||
diagnostics.append(&mut ds);
|
||||
type_info
|
||||
}
|
||||
};
|
||||
symbols_to_types.insert(symbol, type_info);
|
||||
|
||||
(symbols_to_types, diagnostics)
|
||||
}
|
||||
|
||||
pub fn resolve_types(
|
||||
@ -462,7 +477,6 @@ impl Function {
|
||||
nodes_to_symbols: &NodesToSymbols,
|
||||
symbols_to_types: &SymbolsToTypes,
|
||||
nodes_to_types: &NodesToTypes,
|
||||
function_return_types: &FunctionReturnTypes,
|
||||
) -> IrFunction {
|
||||
let mut builder = IrBuilder::new();
|
||||
|
||||
@ -488,7 +502,9 @@ impl Function {
|
||||
let entry_block_id = builder.new_block();
|
||||
|
||||
// lower statements
|
||||
let return_type_info = function_return_types.get(function_symbol).unwrap();
|
||||
let return_type_info = symbols_to_types
|
||||
.get(&Symbol::Function(function_symbol.clone()))
|
||||
.unwrap();
|
||||
let should_return_value = !matches!(return_type_info, TypeInfo::Void);
|
||||
for (i, statement) in self.statements.iter().enumerate() {
|
||||
let is_last = i == self.statements.len() - 1;
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
use crate::ast::compilation_unit::CompilationUnit;
|
||||
use crate::diagnostic::Diagnostics;
|
||||
use crate::ir::ir_class::IrClass;
|
||||
use crate::ir::ir_function::IrFunction;
|
||||
use crate::parser::parse_compilation_unit;
|
||||
use crate::symbol_table::SymbolTable;
|
||||
use crate::symbol_table::util::try_insert_symbols_into;
|
||||
@ -26,7 +28,9 @@ fn parse_compilation_units(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile_compilation_units(inputs: &HashMap<FileId, &str>) -> Result<(), Diagnostics> {
|
||||
pub fn compile_compilation_units(
|
||||
inputs: &HashMap<FileId, &str>,
|
||||
) -> Result<(Vec<IrClass>, Vec<IrFunction>), Diagnostics> {
|
||||
let mut compilation_units = parse_compilation_units(inputs)?;
|
||||
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
@ -44,6 +48,8 @@ pub fn compile_compilation_units(inputs: &HashMap<FileId, &str>) -> Result<(), D
|
||||
try_insert_symbols_into(all_symbols, &mut symbol_table)?;
|
||||
|
||||
// now we can just finish each compilation unit, since we have the symbols
|
||||
let mut ir_classes = Vec::new();
|
||||
let mut ir_functions = Vec::new();
|
||||
let mut diagnostics = Vec::new();
|
||||
for compilation_unit in compilation_units.values() {
|
||||
let (nodes_to_symbols, mut ds) = compilation_unit.resolve_names(&mut symbol_table);
|
||||
@ -61,7 +67,7 @@ pub fn compile_compilation_units(inputs: &HashMap<FileId, &str>) -> Result<(), D
|
||||
continue;
|
||||
}
|
||||
|
||||
let (sts, resolved_types, mut ds) =
|
||||
let (sts, nodes_to_types, mut ds) =
|
||||
compilation_unit.resolve_types(&nodes_to_symbols, &symbols_to_types);
|
||||
if !ds.is_empty() {
|
||||
diagnostics.append(&mut ds);
|
||||
@ -72,7 +78,12 @@ pub fn compile_compilation_units(inputs: &HashMap<FileId, &str>) -> Result<(), D
|
||||
for (symbol, type_info) in sts {
|
||||
symbols_to_types.insert(symbol, type_info);
|
||||
}
|
||||
|
||||
let (mut classes, mut functions) =
|
||||
compilation_unit.lower_to_ir(&nodes_to_symbols, &symbols_to_types, &nodes_to_types);
|
||||
ir_classes.append(&mut classes);
|
||||
ir_functions.append(&mut functions);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok((ir_classes, ir_functions))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user