datex-core 0.0.13

The DATEX Core Rust implementation
Documentation
use crate::{
    ast::expressions::VariableKind,
    collections::HashMap,
    compiler::{
        Variable,
        precompiler::{
            precompiled_ast::RichAst, scope_stack::PrecompilerScopeStack,
        },
    },
    global::{
        protocol_structures::injected_values::{
            InjectedValueDeclaration, InjectedValueType,
        },
        stack_index::StackIndex,
    },
    runtime::execution::context::ExecutionMode,
};
use core::cell::RefCell;

#[derive(Debug, Default, Clone)]
pub struct PrecompilerData {
    // precompiler ast metadata
    pub rich_ast: RichAst,
    // precompiler scope stack
    pub precompiler_scope_stack: RefCell<PrecompilerScopeStack>,
}

use crate::prelude::*;

#[derive(Debug, Clone)]
pub struct ExternalParentScope {
    pub scope: Box<CompilationScope>,

    /// mapping for injected variables from parent scope stack index to child stack index
    pub injected_variables_map: HashMap<StackIndex, StackIndex>,
    /// list of injected values with parent stack index and variable type. The index in the vector is the child stack index (starting from 0)
    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(),
        }
    }

    /// Tries to resolve a variable from the external parent scope (or any other parent scope recursively)
    /// Maps the stack index of the parent scope to a local index of the child scope
    pub fn resolve_variable_name(
        &mut self,
        name: &str,
        value_type: InjectedValueType,
        injected_variables_index_offset: StackIndex,
    ) -> Result<Option<(StackIndex, VariableKind)>, ()> {
        let variable =
            self.scope.resolve_variable_name(name, Some(value_type))?;
        if let Some((variable_parent_index, variable_kind)) = variable {
            // parent variable already registered
            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();
                // update declaration type for max ownership
                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)));
            }
            // otherwise, map variable and store mapping
            // start at injected_variables_index_offset
            let child_stack_index = StackIndex(
                injected_variables_index_offset.0
                    + 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 {
    /// List of variables, mapped by name to their slot address and type.
    variables: HashMap<String, Variable>,
    /// parent scope, accessible from a child scope
    parent_scope: Option<Box<CompilationScope>>,
    /// scope of a parent context, e.g. when inside a block scope for remote execution calls or function bodies
    external_parent_scope_data: Option<ExternalParentScope>,
    /// next available index that can be allocated in the stack
    next_stack_index: StackIndex,
    /// offset from which to start stack index for injected values. Default 0, incremented to make room e.g. for function parameters
    injected_variables_index_offset: StackIndex,

    // ------- Data only relevant for the root scope (FIXME: refactor?) -------
    /// optional precompiler data, only on the root scope
    pub precompiler_data: Option<PrecompilerData>,
    /// The execution mode of the scope.
    /// When the mode is set to Unbounded, the outer statements block will be an unbounded statement block.
    pub execution_mode: ExecutionMode,
    /// If was_used is true, the scope has been used for compilation and should not be reused if once is true.
    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),
            injected_variables_index_offset: 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,
        injected_variables_index_offset: StackIndex,
    ) -> CompilationScope {
        CompilationScope {
            external_parent_scope_data: Some(ExternalParentScope::new(
                parent_context,
            )),
            next_stack_index: initial_stack_index,
            injected_variables_index_offset,
            ..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
    }

    /// Returns the stack index for a variable in this scope or potentially in the parent scope.
    /// The returned tuple contains the slot address, variable type, and a boolean indicating if it
    /// is a local variable (false) or from a parent scope (true).
    /// Returns an error if the variables comes from an external parent scope, but no slot type is provided to downgrade the virtual slot.
    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,
                    self.injected_variables_index_offset,
                )
            } else {
                Err(())
            }
        } else if let Some(parent) = &mut self.parent_scope {
            parent.resolve_variable_name(name, slot_type)
        } else {
            Ok(None)
        }
    }

    /// Returns the virtual slot address for a variable in this scope or potentially in the parent scope.
    /// The returned tuple contains the slot address, variable type, and a boolean indicating if it
    /// is a local variable (false) or from a parent scope (true).
    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()
    }

    /// Creates a new `CompileScope` that is a child of the current scope.
    pub fn push(self) -> CompilationScope {
        CompilationScope {
            next_stack_index: self.next_stack_index,
            injected_variables_index_offset: self
                .injected_variables_index_offset,
            parent_scope: Some(Box::new(self)),
            external_parent_scope_data: None,
            variables: HashMap::new(),
            precompiler_data: None,
            execution_mode: ExecutionMode::Static,
            was_used: false,
        }
    }

    /// Drops the current scope and returns to the parent scope
    pub fn pop(mut self) -> Option<CompilationScope> {
        Some(*(self.parent_scope.take()?))
    }

    /// Returns the external parent scope if it exists
    pub fn pop_external(self) -> Option<ExternalParentScope> {
        self.external_parent_scope_data
    }
}