pub use crate::prelude::*;
use crate::{
ast::{
resolved_variable::{ResolvedVariable, VariableId},
spanned::Spanned,
type_expressions::TypeExpression,
},
global::{
operators::{
ArithmeticUnaryOperator, BinaryOperator, ComparisonOperator,
UnaryOperator, assignment::AssignmentOperator,
},
protocol_structures::instruction_data::StackIndex,
},
libs::core::core_lib_id::CoreLibId,
shared_values::{
PointerAddress, ReferenceMutability, SharedContainerMutability,
},
types::{
r#type::Type, type_definition::callable::CallableKind,
type_definition_with_metadata::LocalReferenceMutability,
},
values::{
core_value::CoreValue,
core_values::{
self,
boolean::Boolean,
decimal::{Decimal, typed_decimal::TypedDecimal},
endpoint::Endpoint,
integer::{Integer, typed_integer::TypedInteger},
text::Text,
},
value::Value,
value_container::ValueContainer,
},
};
use core::{fmt::Display, ops, ops::Neg};
#[derive(Clone, Debug)]
pub struct DatexExpression {
pub data: DatexExpressionData,
pub span: ops::Range<usize>,
pub ty: Option<Type>,
}
impl Default for DatexExpression {
fn default() -> Self {
DatexExpression {
data: DatexExpressionData::Statements(Statements {
statements: Vec::new(),
is_terminated: false,
unbounded: None,
}),
span: 0..0,
ty: None,
}
}
}
impl DatexExpression {
pub fn new(
data: DatexExpressionData,
span: core::ops::Range<usize>,
) -> Self {
DatexExpression {
data,
span,
ty: None,
}
}
}
impl PartialEq for DatexExpression {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Default)]
pub enum DatexExpressionData {
#[default]
Noop,
Recover,
NativeImplementationIndicator,
Null,
Boolean(Boolean),
Text(Text),
Decimal(Decimal),
TypedDecimal(TypedDecimal),
Integer(Integer),
Range(RangeDeclaration),
TypedInteger(TypedInteger),
Identifier(String),
Endpoint(Endpoint),
List(List),
Map(Map),
Statements(Statements),
RequestSharedRef(RequestSharedRef),
ResolveCoreLibId(CoreLibId),
Compile(CompileExpression),
Conditional(Conditional),
VariableDeclaration(VariableDeclaration),
VariableAssignment(VariableAssignment),
VariableAccess(VariableAccess),
TypeDeclaration(TypeDeclaration),
TypeExpression(TypeExpression),
CallableDeclaration(Box<CallableDeclaration>),
CreateShared(CreateShared),
GetRef(GetRef),
GetSharedRef(GetSharedRef),
CreateMut(CreateMut),
Unbox(Unbox),
Clone(CloneExpression),
StackIndex(StackIndex),
Tag(TagExpression),
RootPropertyAccess(RootPropertyAccess),
SlotAssignment(StackAssignment),
BinaryOperation(BinaryOperation),
ComparisonOperation(ComparisonOperation),
UnboxAssignment(UnboxAssignment),
UnboxSlotAssignment(UnboxSlotAssignment),
PropertyAssignment(PropertyAssignment),
UnaryOperation(UnaryOperation),
Apply(Apply),
PropertyAccess(PropertyAccess),
GenericInstantiation(GenericInstantiation),
Placeholder(ValueAccessType),
RemoteExecution(RemoteExecution),
VariantAccess(VariantAccess),
}
impl Spanned for DatexExpressionData {
type Output = DatexExpression;
fn with_span<T: Into<core::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 {
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: Box<DatexExpression>,
pub right: Box<DatexExpression>,
pub ty: Option<Type>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RangeDeclaration {
pub start: Box<DatexExpression>,
pub end: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ComparisonOperation {
pub operator: ComparisonOperator,
pub left: Box<DatexExpression>,
pub right: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnboxAssignment {
pub operator: Option<AssignmentOperator>,
pub unbox_expression: Box<DatexExpression>,
pub assigned_expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnboxSlotAssignment {
pub operator: Option<AssignmentOperator>,
pub stack_index: StackIndex,
pub assigned_expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyAssignment {
pub operator: Option<AssignmentOperator>,
pub base: Box<DatexExpression>,
pub property: Box<DatexExpression>,
pub assigned_expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Conditional {
pub condition: Box<DatexExpression>,
pub then_branch: Box<DatexExpression>,
pub else_branch: Option<Box<DatexExpression>>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum TypeDeclarationKind {
Nominal,
Alias,
}
impl Display for TypeDeclarationKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
TypeDeclarationKind::Nominal => core::write!(f, "type"),
TypeDeclarationKind::Alias => core::write!(f, "typealias"),
}
}
}
impl TypeDeclarationKind {
pub fn is_nominal(&self) -> bool {
matches!(self, TypeDeclarationKind::Nominal)
}
pub fn is_alias(&self) -> bool {
matches!(self, TypeDeclarationKind::Alias)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TypeDeclaration {
pub id: Option<VariableId>,
pub name: String, pub definition: TypeExpression,
pub hoisted: bool,
pub kind: TypeDeclarationKind,
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnaryOperation {
pub operator: UnaryOperator,
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq, Default)]
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: Box<DatexExpression>,
pub arguments: Vec<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyAccess {
pub base: Box<DatexExpression>,
pub property: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GenericInstantiation {
pub base: Box<DatexExpression>,
pub generic_arguments: Vec<TypeExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RemoteExecution {
pub left: Box<DatexExpression>,
pub right: Box<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: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariableAssignment {
pub id: Option<VariableId>,
pub name: String,
pub operator: Option<AssignmentOperator>,
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CompileExpression {
pub expression: Box<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 name: Option<String>,
pub kind: CallableKind,
pub parameters: Vec<(String, TypeExpression)>,
pub rest_parameter: Option<(String, TypeExpression)>,
pub return_type: Option<TypeExpression>,
pub yeet_type: Option<TypeExpression>,
pub body: Box<DatexExpression>,
pub injected_variable_count: Option<u32>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct List {
pub items: Vec<DatexExpression>,
}
impl List {
pub fn new(items: Vec<DatexExpression>) -> Self {
List { items }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Map {
pub entries: Vec<(DatexExpression, DatexExpression)>,
}
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<Box<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: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VariantAccess {
pub name: String,
pub variant: String,
pub base: ResolvedVariable,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Unbox {
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CloneExpression {
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GetRef {
pub mutability: LocalReferenceMutability,
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateShared {
pub mutability: SharedContainerMutability,
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GetSharedRef {
pub mutability: ReferenceMutability,
pub expression: Box<DatexExpression>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CreateMut {
pub expression: Box<DatexExpression>,
}