diff --git a/dm/src/run.rs b/dm/src/run.rs index 4e5dbe3..4079786 100644 --- a/dm/src/run.rs +++ b/dm/src/run.rs @@ -5,6 +5,7 @@ use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use dm_std_lib::add_all_std_core; use dmc_lib::constants_table::ConstantsTable; use dmc_lib::diagnostic::Diagnostic; +use dmc_lib::intrinsics::{insert_intrinsic_symbols, insert_intrinsic_types}; use dmc_lib::parser::parse_compilation_unit; use dmc_lib::symbol_table::SymbolTable; use dmc_lib::types_table::TypesTable; @@ -43,11 +44,15 @@ fn run( let mut compilation_unit = parse_compilation_unit(&input)?; let mut symbol_table = SymbolTable::new(); + symbol_table.push_module_scope("global scope"); + insert_intrinsic_symbols(&mut symbol_table); let mut types_table = TypesTable::new(); + insert_intrinsic_types(&symbol_table, &mut types_table); compilation_unit.init_scopes(&mut symbol_table); compilation_unit.gather_symbols_into(&mut symbol_table)?; compilation_unit.check_names(&mut symbol_table)?; + compilation_unit.gather_types_into(&symbol_table, &mut types_table)?; compilation_unit.type_check(&symbol_table, &mut types_table)?; let (ir_classes, mut ir_functions) = compilation_unit.to_ir(&symbol_table, &types_table); diff --git a/dmc-lib/src/ast/extern_function.rs b/dmc-lib/src/ast/extern_function.rs index 408206b..4ab26d4 100644 --- a/dmc-lib/src/ast/extern_function.rs +++ b/dmc-lib/src/ast/extern_function.rs @@ -7,6 +7,7 @@ use crate::source_range::SourceRange; use crate::symbol::Symbol; use crate::symbol::function_symbol::FunctionSymbol; use crate::symbol_table::SymbolTable; +use crate::type_info::TypeInfo; use crate::types_table::TypesTable; use crate::{diagnostics_result, handle_diagnostics}; use std::rc::Rc; @@ -42,12 +43,13 @@ impl ExternFunction { pub fn init_scopes(&mut self, symbol_table: &mut SymbolTable, container_scope: usize) { self.scope_id = Some(container_scope); - symbol_table.push_function_scope(&format!("extern_function_scope({})", self.declared_name)); + let function_scope = symbol_table + .push_function_scope(&format!("extern_function_scope({})", self.declared_name)); for parameter in &mut self.parameters { - parameter.init_scopes(symbol_table, container_scope); + parameter.init_scopes(symbol_table, function_scope); } - self.return_type.init_scopes(symbol_table, container_scope); + self.return_type.init_scopes(symbol_table, function_scope); symbol_table.pop_scope(); } @@ -85,6 +87,14 @@ impl ExternFunction { let function_symbol = symbol_table .get_function_symbol_owned(self.scope_id.unwrap(), self.declared_name()) .unwrap(); + + // self function type + types_table.function_types_mut().insert( + function_symbol.clone(), + TypeInfo::Function(function_symbol.clone()), + ); + + // return type (temporary) let resolved_return_type = self .return_type .type_info(symbol_table, types_table) diff --git a/dmc-lib/src/ast/identifier.rs b/dmc-lib/src/ast/identifier.rs index b20e9c2..610ca25 100644 --- a/dmc-lib/src/ast/identifier.rs +++ b/dmc-lib/src/ast/identifier.rs @@ -268,9 +268,13 @@ impl Identifier { ExpressibleSymbol::Field(field_symbol) => { types_table.field_types().get(&field_symbol).unwrap() } - ExpressibleSymbol::Function(function_symbol) => { - types_table.function_types().get(&function_symbol).unwrap() - } + ExpressibleSymbol::Function(function_symbol) => types_table + .function_types() + .get(&function_symbol) + .expect(&format!( + "Unable to get function type for {:?}", + function_symbol + )), ExpressibleSymbol::Parameter(parameter_symbol) => types_table .parameter_types() .get(¶meter_symbol) diff --git a/dmc-lib/src/intrinsics/mod.rs b/dmc-lib/src/intrinsics/mod.rs index e679482..24ac57c 100644 --- a/dmc-lib/src/intrinsics/mod.rs +++ b/dmc-lib/src/intrinsics/mod.rs @@ -23,6 +23,8 @@ pub fn insert_intrinsic_symbols(symbol_table: &mut SymbolTable) { create_simple_primitive("Int"), create_simple_primitive("Double"), create_simple_primitive("String"), + create_simple_primitive("Void"), + create_simple_primitive("Any"), ]; for primitive in primitives { symbol_table.insert_class_symbol(Rc::new(primitive)); @@ -30,13 +32,15 @@ pub fn insert_intrinsic_symbols(symbol_table: &mut SymbolTable) { } pub fn insert_intrinsic_types(symbol_table: &SymbolTable, types_table: &mut TypesTable) { - let primitives = ["Int", "Double", "String"]; + let primitives = ["Int", "Double", "String", "Void", "Any"]; for primitive in primitives { let symbol = symbol_table.get_class_symbol(0, primitive).unwrap().clone(); let type_info = match primitive { "Int" => TypeInfo::Integer, "Double" => TypeInfo::Double, "String" => TypeInfo::String, + "Void" => TypeInfo::Void, + "Any" => TypeInfo::Any, _ => unreachable!(), }; types_table.class_types_mut().insert(symbol, type_info); diff --git a/dmc-lib/src/symbol/function_symbol.rs b/dmc-lib/src/symbol/function_symbol.rs index 32f2815..f092bf3 100644 --- a/dmc-lib/src/symbol/function_symbol.rs +++ b/dmc-lib/src/symbol/function_symbol.rs @@ -1,5 +1,7 @@ +use crate::ast::fqn_util::fqn_parts_to_string; use crate::source_range::SourceRange; use crate::symbol::parameter_symbol::ParameterSymbol; +use std::fmt::{Debug, Formatter}; use std::hash::Hash; use std::rc::Rc; @@ -80,3 +82,13 @@ impl Hash for FunctionSymbol { self.scope_id.hash(state); } } + +impl Debug for FunctionSymbol { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "FunctionSymbol({})", + fqn_parts_to_string(&self.fqn_parts) + ) + } +}