use crate::wit_type::WitType;
use crate::ValueAndType;
use crate::{ComponentDependencyKey, ParsedFunctionSite, VariableId, WitTypeWithUnit};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq)]
pub enum RibIR {
PushLit(ValueAndType),
AssignVar(VariableId),
LoadVar(VariableId),
CreateAndPushRecord(WitType),
UpdateRecord(String),
PushList(WitType, usize),
PushTuple(WitType, usize),
PushSome(WitType),
PushNone(Option<WitType>), PushOkResult(WitType),
PushErrResult(WitType),
PushFlag(ValueAndType), SelectField(String),
SelectIndex(usize), SelectIndexV1,
EqualTo,
GreaterThan,
And,
Or,
LessThan,
GreaterThanOrEqualTo,
LessThanOrEqualTo,
IsEmpty,
JumpIfFalse(InstructionId),
Jump(InstructionId),
Label(InstructionId),
Deconstruct,
CreateFunctionName(ParsedFunctionSite, FunctionReferenceType),
InvokeFunction(
ComponentDependencyKey,
InstanceVariable,
usize,
WitTypeWithUnit,
),
PushVariant(String, WitType), PushEnum(String, WitType),
Throw(String),
GetTag,
Concat(usize),
Plus(WitType),
Minus(WitType),
Divide(WitType),
Multiply(WitType),
Negate,
ToIterator,
CreateSink(WitType),
AdvanceIterator,
PushToSink,
SinkToList,
Length,
GenerateWorkerName(Option<VariableId>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum InstanceVariable {
WitResource(VariableId),
WitWorker(VariableId),
}
impl RibIR {
pub fn get_instruction_id(&self) -> Option<InstructionId> {
match self {
RibIR::Label(id) => Some(id.clone()),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum FunctionReferenceType {
Function { function: String },
RawResourceConstructor { resource: String },
RawResourceDrop { resource: String },
RawResourceMethod { resource: String, method: String },
RawResourceStaticMethod { resource: String, method: String },
}
#[derive(Debug, Clone, PartialEq, Hash, Eq, Serialize, Deserialize)]
pub struct InstructionId {
pub index: usize,
}
impl InstructionId {
pub fn new(index: usize) -> Self {
InstructionId { index }
}
pub fn init() -> Self {
InstructionId { index: 0 }
}
pub fn increment(&self) -> InstructionId {
InstructionId {
index: self.index + 1,
}
}
pub fn increment_mut(&mut self) -> InstructionId {
self.index += 1;
self.clone()
}
}