Skip to content

Commit

Permalink
migrated scope to use default instead of new
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jul 11, 2024
1 parent 44d832c commit cde55f7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/compiler/native_fn/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ pub(crate) struct NativeFunctionIndex {
functions: Vec<NativeFunction>,
}

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);
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl RstCompiler<'_> {

pub(crate) fn compile(&self, cst: &CstFile) -> Result<HexoFile, Error> {
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())?;

Expand Down Expand Up @@ -153,10 +153,9 @@ impl RstCompiler<'_> {
fn build_scope(
&self,
scope_id: HexoId,
file_path: &Path,
cst: &CstFunctionStatement,
) -> Result<CompilationScope, Error> {
let mut root_scope = CompilationScope::new(file_path);
let mut root_scope = CompilationScope::default();

self.build_scope_into(scope_id, &cst, &mut root_scope)?;

Expand Down
23 changes: 5 additions & 18 deletions src/compiler/rst/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,26 @@ pub(crate) struct FunctionBinding {
pub(crate) emits: Vec<CstEmitStatement>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub(crate) struct LocalCompilationScope {
constant_table: HashMap<String, ConstantBinding>,
function_table: HashMap<String, FunctionBinding>,
parents: Vec<HexoId>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub(crate) struct CompilationScope {
local_scopes: HashMap<HexoId, LocalCompilationScope>,
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -122,7 +116,7 @@ impl CompilationScope {
pub(crate) fn bind_parents(&mut self, scope_id: HexoId, parents: Vec<HexoId>) {
self.local_scopes
.entry(scope_id)
.or_insert_with(LocalCompilationScope::new);
.or_insert_with(LocalCompilationScope::default);

let local_scope: &mut LocalCompilationScope = self
.local_scopes
Expand All @@ -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);
Expand Down

0 comments on commit cde55f7

Please sign in to comment.