Skip to content

Commit

Permalink
added partial local context, functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 28, 2024
1 parent 09b1904 commit 901776c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
36 changes: 32 additions & 4 deletions src/compiler/rst/compilation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ impl CompilationContext {

local_context.bind_constant(constant);
}

pub(crate) fn get_local_constant(&self, context_id: u64, name: &String) -> Option<&ConstantBinding> {
let local_context = self.local_contexts.get(&context_id)?;

let local_constant = local_context.get_constant(context_id, name);
let local_constant = local_context.get_constant(name);

if local_constant.is_none() {
for parent in &local_context.parents {
Expand All @@ -79,12 +80,39 @@ impl CompilationContext {
self.function_table.insert(function.name.clone(), function);
}

pub(crate) fn get_function(&self, name: &String) -> Option<&FunctionBinding> {
return self.function_table.get(name);
pub(crate) fn bind_local_function(&mut self, context_id: u64, function: FunctionBinding) {
if !self.local_contexts.contains_key(&context_id) {
self.local_contexts.insert(context_id, LocalCompilationContext::new());
}

let mut local_context: &mut LocalCompilationContext = self.local_contexts.get_mut(&context_id)
.expect("prechecked but value is still missing");

local_context.bind_function(function);
}

pub(crate) fn get_local_function(&self, context_id: u64, name: &String) -> Option<&FunctionBinding> {
let local_context = self.local_contexts.get(&context_id)?;

let local_function = local_context.get_function(name);

if local_function.is_none() {
for parent in &local_context.parents {
let parent_function = self.get_local_function(*parent, name);
if parent_function.is_some() {
return parent_function
}
}
} else {
return local_function
}

return None;
}
}

impl LocalCompilationContext {

fn new() -> LocalCompilationContext {
return LocalCompilationContext {
constant_table: HashMap::new(),
Expand All @@ -97,7 +125,7 @@ impl LocalCompilationContext {
self.constant_table.insert(constant.name.clone(), constant);
}

fn get_constant(&self, context_id: u64, name: &String) -> Option<&ConstantBinding> {
fn get_constant(&self, name: &String) -> Option<&ConstantBinding> {
return self.constant_table.get(name);
}

Expand Down
6 changes: 4 additions & 2 deletions src/compiler/rst/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl RstCompiler<'_> {
mut root_context: &mut CompilationContext,
) -> Result<(), RstCompilerError> {
Self::build_context_constants_into(context_id, &cst, &mut root_context)?;
Self::build_context_functions_into(&cst, &mut root_context)?;
Self::build_context_functions_into(context_id, &cst, &mut root_context)?;
Ok(())
}

Expand All @@ -121,11 +121,13 @@ impl RstCompiler<'_> {
}

fn build_context_functions_into(
context_id: u64,
cst: &&CstFunctionStatement,
root_context: &mut CompilationContext,
) -> Result<(), RstCompilerError> {
for function in &cst.functions {
root_context.bind_function(
root_context.bind_local_function(
context_id,
FunctionBinding {
identifier: next_identifier(),
name: function.name.clone(),
Expand Down

0 comments on commit 901776c

Please sign in to comment.