use crate::{
ast::expressions::VariableKind,
collections::HashMap,
compiler::{
Variable,
precompiler::{
precompiled_ast::RichAst, scope_stack::PrecompilerScopeStack,
},
},
global::protocol_structures::{
injected_values::{InjectedValueDeclaration, InjectedValueType},
instruction_data::StackIndex,
},
runtime::execution::context::ExecutionMode,
};
use core::cell::RefCell;
#[derive(Debug, Default, Clone)]
pub struct PrecompilerData {
pub rich_ast: RichAst,
pub precompiler_scope_stack: RefCell<PrecompilerScopeStack>,
}
use crate::prelude::*;
#[derive(Debug, Clone)]
pub struct ExternalParentScope {
pub scope: Box<CompilationScope>,
pub injected_variables_map: HashMap<StackIndex, StackIndex>,
pub injected_values: Vec<InjectedValueDeclaration>,
}
impl ExternalParentScope {
pub fn new(scope: CompilationScope) -> Self {
Self {
scope: Box::new(scope),
injected_variables_map: HashMap::new(),
injected_values: Vec::new(),
}
}
pub fn resolve_variable_name(
&mut self,
name: &str,
value_type: InjectedValueType,
) -> Result<Option<(StackIndex, VariableKind)>, ()> {
let variable =
self.scope.resolve_variable_name(name, Some(value_type))?;
if let Some((variable_parent_index, variable_kind)) = variable {
if let Some(variable_child_index) =
self.injected_variables_map.get(&variable_parent_index)
{
let declaration = self
.injected_values
.get_mut(variable_child_index.0 as usize)
.unwrap();
if match (value_type, declaration.ty) {
(
InjectedValueType::Local(new),
InjectedValueType::Local(current),
) => new > current,
(
InjectedValueType::Shared(new),
InjectedValueType::Shared(current),
) => new > current,
_ => unreachable!("injected value type mismatch"),
} {
declaration.ty = value_type;
}
return Ok(Some((*variable_child_index, variable_kind)));
}
let child_stack_index =
StackIndex(self.injected_variables_map.len() as u32);
self.injected_variables_map
.insert(variable_parent_index, child_stack_index);
self.injected_values.push(InjectedValueDeclaration {
index: variable_parent_index,
ty: value_type,
});
Ok(Some((child_stack_index, variable_kind)))
} else {
Ok(None)
}
}
}
#[derive(Debug, Clone)]
pub struct CompilationScope {
variables: HashMap<String, Variable>,
parent_scope: Option<Box<CompilationScope>>,
external_parent_scope_data: Option<ExternalParentScope>,
next_stack_index: StackIndex,
pub precompiler_data: Option<PrecompilerData>,
pub execution_mode: ExecutionMode,
pub was_used: bool,
}
impl Default for CompilationScope {
fn default() -> Self {
CompilationScope {
variables: HashMap::new(),
parent_scope: None,
external_parent_scope_data: None,
next_stack_index: StackIndex(0),
precompiler_data: Some(PrecompilerData::default()),
execution_mode: ExecutionMode::Static,
was_used: false,
}
}
}
impl CompilationScope {
pub fn new(execution_mode: ExecutionMode) -> CompilationScope {
CompilationScope {
execution_mode,
..CompilationScope::default()
}
}
pub fn new_with_external_parent_scope(
parent_context: CompilationScope,
initial_stack_index: StackIndex,
) -> CompilationScope {
CompilationScope {
external_parent_scope_data: Some(ExternalParentScope::new(
parent_context,
)),
next_stack_index: initial_stack_index,
..CompilationScope::default()
}
}
pub fn mark_as_last_execution(&mut self) {
match self.execution_mode {
ExecutionMode::Static => {
panic!(
"mark_as_last_execution can only be called for Unbounded execution modes"
);
}
ExecutionMode::Unbounded { .. } => {
self.execution_mode =
ExecutionMode::Unbounded { has_next: false };
}
_ => {}
}
}
pub fn has_external_parent_scope(&self) -> bool {
self.external_parent_scope_data.is_some()
}
pub fn register_variable_slot(&mut self, variable: Variable) {
self.variables.insert(variable.name.clone(), variable);
}
pub fn get_next_stack_index(&mut self) -> StackIndex {
let index = self.next_stack_index;
self.next_stack_index += 1;
index
}
pub fn resolve_variable_name(
&mut self,
name: &str,
slot_type: Option<InjectedValueType>,
) -> Result<Option<(StackIndex, VariableKind)>, ()> {
if let Some(variable) = self.variables.get(name) {
Ok(Some((variable.index, variable.kind)))
} else if let Some(external_parent) =
&mut self.external_parent_scope_data
{
if let Some(slot_type) = slot_type {
external_parent.resolve_variable_name(name, slot_type)
} else {
Err(())
}
} else if let Some(parent) = &mut self.parent_scope {
parent.resolve_variable_name(name, slot_type)
} else {
Ok(None)
}
}
pub fn resolve_variable_name_with_slot_type(
&mut self,
name: &str,
slot_type: InjectedValueType,
) -> Option<(StackIndex, VariableKind)> {
self.resolve_variable_name(name, Some(slot_type)).unwrap()
}
pub fn push(self) -> CompilationScope {
CompilationScope {
next_stack_index: self.next_stack_index,
parent_scope: Some(Box::new(self)),
external_parent_scope_data: None,
variables: HashMap::new(),
precompiler_data: None,
execution_mode: ExecutionMode::Static,
was_used: false,
}
}
pub fn pop(mut self) -> Option<CompilationScope> {
Some(*(self.parent_scope.take()?))
}
pub fn pop_external(self) -> Option<ExternalParentScope> {
self.external_parent_scope_data
}
}