datex-core 0.0.14

The DATEX Core Rust implementation
Documentation
use crate::{
    core_compiler::{
        to_instructions::{ToInstructions, yield_all},
        value_visitor::ValueVisitor,
    },
    instruction::regular_instruction::RegularInstruction,
    libs::core::core_lib_id::{CoreLibId, CoreLibIdIndex},
    prelude::*,
    runtime::cache::shared_references_cache::SharedReferencesCache,
    values::core_value::CoreValue,
};

impl ToInstructions for CoreValue {
    fn to_instructions<'a>(
        &'a self,
        visitor: &'a mut dyn ValueVisitor,
        cache: &'a mut SharedReferencesCache,
    ) -> Box<dyn Iterator<Item = crate::instruction::Instruction> + 'a> {
        Box::new(gen move {
            match self {
                CoreValue::Uninitialized => unreachable!(),
                CoreValue::Null => yield RegularInstruction::null().into(),
                CoreValue::Boolean(boolean) => {
                    yield_all!(boolean.to_instructions(visitor, cache));
                }
                CoreValue::Integer(integer) => {
                    yield_all!(integer.to_instructions(visitor, cache));
                }
                CoreValue::TypedInteger(typed_integer) => {
                    yield_all!(typed_integer.to_instructions(visitor, cache));
                }
                CoreValue::Decimal(decimal) => {
                    yield_all!(decimal.to_instructions(visitor, cache));
                }
                CoreValue::TypedDecimal(typed_decimal) => {
                    yield_all!(typed_decimal.to_instructions(visitor, cache));
                }
                CoreValue::Text(text) => {
                    yield_all!(text.to_instructions(visitor, cache));
                }
                CoreValue::Endpoint(endpoint) => {
                    yield_all!(endpoint.to_instructions(visitor, cache));
                }
                CoreValue::List(list) => {
                    yield_all!(list.to_instructions(visitor, cache));
                }
                CoreValue::Map(map) => {
                    yield_all!(map.to_instructions(visitor, cache));
                }
                CoreValue::Type(ty) => {
                    if let Some(core_id) = ty.try_as_core_lib_type() {
                        // Shortcut for core library types
                        yield RegularInstruction::get_core_lib_value(
                            CoreLibIdIndex::from(CoreLibId::Type(core_id)),
                        )
                        .into()
                    } else {
                        // Switch to type space
                        yield RegularInstruction::type_expression().into();
                        yield_all!(ty.to_instructions(visitor, cache));
                    }
                }
                CoreValue::EntityTypeDefinition(entity_type_definition) => {
                    yield_all!(
                        entity_type_definition.to_instructions(visitor, cache)
                    );
                }
                CoreValue::Callable(callable) => {
                    yield_all!(callable.to_instructions(visitor, cache));
                }
                CoreValue::Range(range) => {
                    yield_all!(range.to_instructions(visitor, cache));
                }
                CoreValue::Box(value_container) => {
                    yield RegularInstruction::boxed_value().into();
                    yield_all!(value_container.to_instructions(visitor, cache));
                }
                CoreValue::Instant(instant) => {
                    yield_all!(instant.to_instructions(visitor, cache));
                }
                CoreValue::Sender(sender) => {
                    yield_all!(sender.to_instructions(visitor, cache));
                }
                CoreValue::Receiver(receiver) => {
                    yield_all!(receiver.to_instructions(visitor, cache));
                }
            }
        })
    }
}