diff --git a/src/compiler/native_fn/index.rs b/src/compiler/native_fn/index.rs index 4b3327b..1673e59 100644 --- a/src/compiler/native_fn/index.rs +++ b/src/compiler/native_fn/index.rs @@ -6,13 +6,15 @@ pub(crate) struct NativeFunctionIndex { functions: Vec, } -impl NativeFunctionIndex { - - pub(crate) fn new() -> NativeFunctionIndex { +impl Default for NativeFunctionIndex { + fn default() -> Self { NativeFunctionIndex { functions: Self::create_native_functions(), } } +} + +impl NativeFunctionIndex { pub(crate) fn find(&self, name: String) -> Option<&NativeFunction> { return self.functions.iter().find(|f| f.signature().name() == name); diff --git a/src/compiler/rst/compiler.rs b/src/compiler/rst/compiler.rs index 268e8de..2c4acc0 100644 --- a/src/compiler/rst/compiler.rs +++ b/src/compiler/rst/compiler.rs @@ -25,7 +25,7 @@ impl RstCompiler<'_> { pub(crate) fn compile(&self, cst: &CstFile) -> Result { let scope_id = HexoId::next(); - let mut scope = self.build_scope(scope_id, cst.path(), cst.main())?; + let mut scope = self.build_scope(scope_id, cst.main())?; let bb = self.build_bytes(scope_id, &mut scope, cst.main().emits())?; @@ -153,10 +153,9 @@ impl RstCompiler<'_> { fn build_scope( &self, scope_id: HexoId, - file_path: &Path, cst: &CstFunctionStatement, ) -> Result { - let mut root_scope = CompilationScope::new(file_path); + let mut root_scope = CompilationScope::default(); self.build_scope_into(scope_id, &cst, &mut root_scope)?; diff --git a/src/compiler/rst/scope.rs b/src/compiler/rst/scope.rs index 86703f7..099f3f4 100644 --- a/src/compiler/rst/scope.rs +++ b/src/compiler/rst/scope.rs @@ -19,32 +19,26 @@ pub(crate) struct FunctionBinding { pub(crate) emits: Vec, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub(crate) struct LocalCompilationScope { constant_table: HashMap, function_table: HashMap, parents: Vec, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub(crate) struct CompilationScope { local_scopes: HashMap, native_function_index: NativeFunctionIndex, } impl CompilationScope { - pub(crate) fn new(path: &Path) -> CompilationScope { - CompilationScope { - local_scopes: HashMap::new(), - native_function_index: NativeFunctionIndex::new(), - } - } // region constant pub(crate) fn bind_local_constant(&mut self, scope_id: HexoId, constant: ConstantBinding) { self.local_scopes .entry(scope_id) - .or_insert_with(LocalCompilationScope::new); + .or_insert_with(LocalCompilationScope::default); let local_scope: &mut LocalCompilationScope = self .local_scopes @@ -82,7 +76,7 @@ impl CompilationScope { pub(crate) fn bind_local_function(&mut self, scope_id: HexoId, function: FunctionBinding) { self.local_scopes .entry(scope_id) - .or_insert_with(LocalCompilationScope::new); + .or_insert_with(LocalCompilationScope::default); let local_scope: &mut LocalCompilationScope = self .local_scopes @@ -122,7 +116,7 @@ impl CompilationScope { pub(crate) fn bind_parents(&mut self, scope_id: HexoId, parents: Vec) { self.local_scopes .entry(scope_id) - .or_insert_with(LocalCompilationScope::new); + .or_insert_with(LocalCompilationScope::default); let local_scope: &mut LocalCompilationScope = self .local_scopes @@ -136,13 +130,6 @@ impl CompilationScope { } impl LocalCompilationScope { - fn new() -> LocalCompilationScope { - LocalCompilationScope { - constant_table: HashMap::new(), - function_table: HashMap::new(), - parents: Vec::new(), - } - } fn bind_constant(&mut self, constant: ConstantBinding) { self.constant_table.insert(constant.name.clone(), constant);