pub use crate::prelude::*;
use crate::{
ast::{
resolved_variable::{ResolvedVariable, VariableId},
spanned::Spanned,
type_expressions::TypeExpression,
},
global::{
operators::{
ArithmeticUnaryOperator, BinaryOperator, ComparisonOperator,
UnaryOperator, modification::ModificationOperator,
},
stack_index::StackIndex,
},
libs::core::core_lib_id::CoreLibId,
shared_values::{
PointerAddress, ReferenceMutability, SelfOwnedPointerAddress,
SharedContainerMutability,
},
types::{
r#type::Type, type_definition::callable::CallableKind,
type_definition_with_metadata::LocalReferenceMutability,
},
values::{
core_value::CoreValue,
core_value_with_classification::CoreValueWithClassification,
core_values::{
self,
boolean::Boolean,
decimal::{Decimal, typed_decimal::TypedDecimal},
endpoint::Endpoint,
instant::Instant,
integer::{Integer, typed_integer::TypedInteger},
text::Text,
},
value::Value,
value_container::ValueContainer,
},
};
use core::{
fmt::Display,
ops::{self, Neg},
};
#[derive(Clone, Debug)]
pub struct DatexExpression {
pub data: Box<DatexExpressionData>,
pub span: ops::Range<usize>,
pub ty: Option<Type>,
}
impl Default for DatexExpression {
fn default() -> Self {
DatexExpression {
data: Box::new(DatexExpressionData::Statements(Statements {
statements: Vec::new(),
is_terminated: false,
unbounded: None,
})),
span: 0..0,
ty: None,
}
}
}
impl DatexExpression {
pub fn new(data: DatexExpressionData, span: ops::Range<usize>) -> Self {
DatexExpression {
data: Box::new(data),
span,
ty: None,
}
}
pub fn data(&self) -> &DatexExpressionData {
&self.data
}
pub fn data_mut(&mut self) -> &mut DatexExpressionData {
&mut self.data
}
pub fn span(&self) -> &ops::Range<usize> {
&self.span
}
pub fn ty(&self) -> Option<&Type> {
self.ty.as_ref()
}
}
impl PartialEq for DatexExpression {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
impl Eq for DatexExpression {}
#[derive(Clone, Debug, PartialEq)]
#[derive(Default)]
pub enum DatexExpressionData {
#[default]
Noop,
Recover,
NativeImplementationIndicator,
Null,
OmitRecursive,
Boolean(Boolean),
Text(Text),
Decimal(Decimal),
TypedDecimal(TypedDecimal),
Integer(Integer),
Instant(Instant),
Range(RangeDeclaration),
TypedInteger(TypedInteger),
Identifier(String),
Endpoint(Endpoint),
List(List),
Map(Map),
Sender,
Receiver,
Statements(Statements),
RequestSharedRef(RequestSharedRef),
MoveSharedValue(SelfOwnedPointerAddress),
ResolveCoreLibId(CoreLibId),
Compile(CompileExpression),
Conditional(Conditional),
VariableDeclaration(VariableDeclaration),
VariableAssignment(VariableAssignment),
VariableAccess(VariableAccess),
TypeDeclaration(TypeDeclarationExpression),
EntityDeclaration(EntityDeclarationExpression),
TypeExpression(TypeExpression),
CallableDeclaration(Box<CallableDeclaration>),
CreateShared(CreateShared),
DeriveRef(DeriveRef),
DeriveSharedRef(DeriveSharedRef),
CreateMut(CreateMut),
Unbox(Unbox),
Clone(CloneExpression),
StackIndex(StackIndex),
Tag(TagExpression),
RootPropertyAccess(RootPropertyAccess),
StackAssignment(StackAssignment),
StackListAssignment(StackListAssignment),
BinaryOperation(BinaryOperation),
ComparisonOperation(ComparisonOperation),
UnboxAssignment(UnboxAssignment),
UnboxSlotAssignment(UnboxSlotAssignment),
PropertyAssignment(PropertyAssignment),
UnaryOperation(UnaryOperation),
Apply(Apply),
InterfaceMethodCall(InterfaceMethodCall),
PropertyAccess(PropertyAccess),
GenericInstantiation(GenericInstantiation),
Placeholder(ValueAccessType),
RemoteExecution(RemoteExecution),
VariantAccess(VariantAccess),
EntityValue(EntityValueExpression),
}
impl DatexExpressionData {
pub fn callable_declaration(
callable_declaration: CallableDeclaration,
) -> Self {
DatexExpressionData::CallableDeclaration(Box::new(callable_declaration))
}
}
impl Spanned for DatexExpressionData {
type Output = DatexExpression;
fn with_span<T: Into<ops::Range<usize>>>(self, span: T) -> Self::Output {
DatexExpression {
data: Box::new(self),
span: span.into(),
ty: None,
}
}
fn with_default_span(self) -> Self::Output {
DatexExpression {
data: Box::new(self),
span: (0..0),
ty: None,
}
}
}
impl Spanned for Box<DatexExpressionData> {
type Output = DatexExpression;
fn with_span<T: Into<ops::Range<usize>>>(self, span: T) -> Self::Output {
DatexExpression {
data: self,
span: span.into(),
ty: None,
}
}
fn with_default_span(self) -> Self::Output {
DatexExpression {
data: self,
span: (0..0),
ty: None,
}
}
}
impl TryFrom<&DatexExpressionData> for ValueContainer {
type Error = ();
fn try_from(expr: &DatexExpressionData) -> Result<Self, Self::Error> {
Ok(match expr {
DatexExpressionData::UnaryOperation(UnaryOperation {
operator,
expression,
}) => {
let value = ValueContainer::try_from(expression.data())?;
match value {
ValueContainer::Local(Value::Core(
CoreValueWithClassification {
inner: CoreValue::Integer(_) | CoreValue::Decimal(_),
..
},
)) => match operator {
UnaryOperator::Arithmetic(
ArithmeticUnaryOperator::Plus,
) => value,
UnaryOperator::Arithmetic(
ArithmeticUnaryOperator::Minus,
) => value.neg().map_err(|_| ())?,
_ => Err(())?,
},
_ => Err(())?,
}
}
DatexExpressionData::Null => ValueContainer::Local(Value::null()),
DatexExpressionData::Boolean(b) => ValueContainer::from(b.clone()),
DatexExpressionData::Text(s) => ValueContainer::from(s.clone()),
DatexExpressionData::Decimal(d) => ValueContainer::from(d.clone()),
DatexExpressionData::Integer(i) => ValueContainer::from(i.clone()),
DatexExpressionData::TypedInteger(i) => {
ValueContainer::from(i.clone())
}
DatexExpressionData::TypedDecimal(d) => {
ValueContainer::from(d.clone())
}
DatexExpressionData::Endpoint(e) => ValueContainer::from(e.clone()),
DatexExpressionData::List(list) => {
let entries = list
.items
.iter()
.map(|e| ValueContainer::try_from(e.data()))
.collect::<Result<Vec<ValueContainer>, ()>>()?;
ValueContainer::from(core_values::list::List::from(entries))
}
DatexExpressionData::Map(pairs) => {
let entries = pairs
.entries
.iter()
.map(|(k, v)| {
let key = ValueContainer::try_from(k.data())?;
let value = ValueContainer::try_from(v.data())?;
Ok((key, value))
})
.collect::<Result<Vec<(ValueContainer, ValueContainer)>, ()>>()?;
ValueContainer::from(core_values::map::Map::from(entries))
}
_ => Err(())?,
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct BinaryOperation {
pub operator: BinaryOperator,
pub left: DatexExpression,
pub right: DatexExpression,
pub ty: Option<Type>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RangeDeclaration {
pub start: DatexExpression,
pub end: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ComparisonOperation {
pub operator: ComparisonOperator,
pub left: DatexExpression,
pub right: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnboxAssignment {
pub operator: Option<ModificationOperator>,
pub unbox_expression: DatexExpression,
pub assigned_expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnboxSlotAssignment {
pub operator: Option<ModificationOperator>,
pub stack_index: StackIndex,
pub assigned_expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyAssignment {
pub operator: Option<ModificationOperator>,
pub base: DatexExpression,
pub property: DatexExpression,
pub assigned_expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Conditional {
pub condition: DatexExpression,
pub then_branch: DatexExpression,
pub else_branch: Option<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TypeDeclarationExpression {
pub id: Option<VariableId>,
pub name: String, pub definition: TypeExpression,
pub hoisted: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnaryOperation {
pub operator: UnaryOperator,
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq, Default, Hash, Eq)]
pub enum ValueAccessType {
SharedRefMut,
SharedRef,
Clone, Borrow,
#[default]
MoveOrCopy,
}
impl From<&ReferenceMutability> for ValueAccessType {
fn from(mutability: &ReferenceMutability) -> Self {
match mutability {
ReferenceMutability::Mutable => ValueAccessType::SharedRefMut,
ReferenceMutability::Immutable => ValueAccessType::SharedRef,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Apply {
pub base: DatexExpression,
pub arguments: Vec<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct InterfaceMethodCall {
pub target: DatexExpression,
pub method_name: String,
pub arguments: Vec<DatexExpression>,
}
impl InterfaceMethodCall {
pub fn new(
target: DatexExpression,
method_name: String,
arguments: Vec<DatexExpression>,
) -> Self {
InterfaceMethodCall {
target,
method_name,
arguments,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyAccess {
pub base: DatexExpression,
pub property: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GenericInstantiation {
pub base: DatexExpression,
pub generic_arguments: Vec<TypeExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RemoteExecution {
pub left: DatexExpression,
pub right: DatexExpression,
pub injected_variable_count: Option<u32>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnboundedStatement {
pub is_first: bool,
pub is_last: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Statements {
pub statements: Vec<DatexExpression>,
pub is_terminated: bool,
pub unbounded: Option<UnboundedStatement>,
}
impl Statements {
pub fn empty() -> Self {
Statements {
statements: Vec::new(),
is_terminated: true,
unbounded: None,
}
}
pub fn new_terminated(statements: Vec<DatexExpression>) -> Self {
Statements {
statements,
is_terminated: true,
unbounded: None,
}
}
pub fn new_unterminated(statements: Vec<DatexExpression>) -> Self {
Statements {
statements,
is_terminated: false,
unbounded: None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariableDeclaration {
pub id: Option<VariableId>,
pub kind: VariableKind,
pub name: String,
pub type_annotation: Option<TypeExpression>,
pub init_expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariableAssignment {
pub id: Option<VariableId>,
pub name: String,
pub operator: Option<ModificationOperator>,
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EntityDeclarationExpression {
pub id: Option<VariableId>,
pub name: String,
pub definition: TypeExpression,
pub hoisted: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CompileExpression {
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariableAccess {
pub id: VariableId,
pub name: String,
pub access_type: ValueAccessType,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RequestSharedRef {
pub mutability: ReferenceMutability,
pub address: PointerAddress,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CallableDeclaration {
pub signature: CallableSignature,
pub body: DatexExpression,
pub injected_variable_count: Option<u32>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CallableSignature {
pub name: Option<String>,
pub kind: CallableKind,
pub requires_async: bool,
pub parameters: Vec<(String, TypeExpression)>,
pub rest_parameter: Option<(String, TypeExpression)>,
pub return_type: Option<TypeExpression>,
pub yeet_type: Option<TypeExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct List {
pub items: Vec<DatexExpression>,
}
impl List {
pub fn new(items: Vec<DatexExpression>) -> Self {
List { items }
}
}
impl FromIterator<DatexExpression> for List {
fn from_iter<T: IntoIterator<Item = DatexExpression>>(iter: T) -> Self {
let items = iter.into_iter().collect();
List { items }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Map {
pub entries: Vec<(DatexExpression, DatexExpression)>,
}
impl FromIterator<(DatexExpression, DatexExpression)> for Map {
fn from_iter<T: IntoIterator<Item = (DatexExpression, DatexExpression)>>(
iter: T,
) -> Self {
let entries = iter.into_iter().collect();
Map { entries }
}
}
impl Map {
pub fn new(entries: Vec<(DatexExpression, DatexExpression)>) -> Self {
Map { entries }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VariableKind {
Const,
Var,
}
impl Display for VariableKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
VariableKind::Const => core::write!(f, "const"),
VariableKind::Var => core::write!(f, "var"),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TagExpression {
pub tag: String,
pub expression: Option<DatexExpression>,
}
impl Display for TagExpression {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::write!(f, "#{}", self.tag)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct RootPropertyAccess {
pub property_name: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct StackAssignment {
pub index: StackIndex,
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct StackListAssignment {
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariantAccess {
pub name: String,
pub variant: String,
pub base: ResolvedVariable,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EntityValueExpression {
pub entity_name: String,
pub entity_address: Option<PointerAddress>,
pub value: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Unbox {
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CloneExpression {
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DeriveRef {
pub mutability: LocalReferenceMutability,
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateShared {
pub mutability: SharedContainerMutability,
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DeriveSharedRef {
pub mutability: ReferenceMutability,
pub expression: DatexExpression,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateMut {
pub expression: DatexExpression,
}