use std::rc::Rc;
use crate::{lang::*, InternalString};
use crate::{RantFunctionHandle, RantList, RantMap, RantValue};
use super::{RuntimeResult, VM, resolver::Weights};
pub enum Intent {
PrintLast,
ReturnLast,
ContinueLast,
BreakLast,
ImportLastAsModule { module_name: String, descope: usize },
TickCurrentBlock,
SetVar { vname: Identifier, access_kind: VarAccessMode, },
DefVar { vname: Identifier, access_kind: VarAccessMode, is_const: bool },
BuildDynamicGetter {
path: Rc<AccessPath>,
dynamic_key_count: usize,
pending_exprs: Vec<Rc<Sequence>>,
override_print: bool,
prefer_function: bool,
fallback: Option<Rc<Sequence>>,
},
GetValue {
path: Rc<AccessPath>,
dynamic_key_count: usize,
override_print: bool,
prefer_function: bool,
fallback: Option<Rc<Sequence>>,
},
BuildDynamicSetter {
path: Rc<AccessPath>,
write_mode: VarWriteMode,
expr_count: usize,
pending_exprs: Vec<Rc<Sequence>>,
val_source: SetterValueSource,
},
SetValue {
path: Rc<AccessPath>,
write_mode: VarWriteMode,
expr_count: usize
},
Invoke {
arg_exprs: Rc<Vec<ArgumentExpr>>,
arg_eval_count: usize,
is_temporal: bool,
},
InvokePipeStep {
steps: Rc<Vec<FunctionCall>>,
step_index: usize,
state: InvokePipeStepState,
pipeval: Option<RantValue>,
assignment_pipe: Option<Rc<AssignmentPipeTarget>>,
},
CreateDefaultArgs { context: RantFunctionHandle, default_arg_exprs: Vec<(Rc<Sequence>, usize)>, eval_index: usize, },
Call { argc: usize, override_print: bool },
CallOperand { sequence: Rc<Sequence> },
CallTemporal { func: RantFunctionHandle, arg_exprs: Rc<Vec<ArgumentExpr>>, args: Rc<Vec<RantValue>>, temporal_state: TemporalSpreadState, },
BuildList { init: Rc<Vec<Rc<Sequence>>>, index: usize, list: RantList },
BuildTuple { init: Rc<Vec<Rc<Sequence>>>, index: usize, items: Vec<RantValue> },
BuildMap { init: Rc<Vec<(MapKeyExpr, Rc<Sequence>)>>, pair_index: usize, map: RantMap },
BuildWeightedBlock { block: Rc<Block>, weights: Weights, pop_next_weight: bool, },
RuntimeCall { function: Box<dyn FnOnce(&mut VM) -> RuntimeResult<()>>, interrupt: bool },
DropStaleUnwinds,
Add,
Subtract,
Multiply,
Divide,
Modulo,
Power,
LogicNot,
Negate,
LogicShortCircuit { on_truthiness: bool, short_circuit_result: LogicShortCircuitHandling, gen_op_intent: Box<dyn FnOnce() -> Intent>, rhs: Rc<Sequence> },
LogicAnd,
LogicOr,
LogicXor,
Equals,
NotEquals,
Greater,
GreaterOrEqual,
Less,
LessOrEqual,
CheckCondition { conditions: Rc<Vec<(Rc<Sequence>, Rc<Block>)>>, fallback: Option<Rc<Block>>, index: usize },
}
impl Intent {
pub(crate) fn name(&self) -> &'static str {
match self {
Self::PrintLast => "print",
Self::TickCurrentBlock => "check_block",
Self::SetVar { .. } => "set_var",
Self::DefVar { .. } => "def_var",
Self::BuildDynamicGetter { .. } => "build_dyn_getter",
Self::GetValue { .. } => "get_value",
Self::BuildDynamicSetter { .. } => "build_dyn_setter",
Self::SetValue { .. } => "set_value",
Self::Invoke { .. } => "invoke",
Self::InvokePipeStep { .. } => "invoke_pipe_step",
Self::Call { .. } => "call",
Self::CallOperand { .. } => "call_operand",
Self::CallTemporal { .. } => "call_temporal",
Self::BuildList { .. } => "build_list",
Self::BuildTuple { .. } => "build_tuple",
Self::BuildMap { .. } => "build_map",
Self::ImportLastAsModule { .. } => "load_module",
Self::RuntimeCall { .. } => "runtime_call",
Self::DropStaleUnwinds => "drop_stale_unwinds",
Self::ReturnLast => "return_last",
Self::ContinueLast => "continue_last",
Self::BreakLast => "break_last",
Self::BuildWeightedBlock { .. } => "build_weighted_block",
Self::CreateDefaultArgs { .. } => "create_default_args",
Self::Add => "add",
Self::Subtract => "subtract",
Self::Multiply => "multiply",
Self::Divide => "divide",
Self::Modulo => "modulo",
Self::Power => "power",
Self::Negate => "negate",
Self::LogicShortCircuit { .. } => "logic_short_circuit",
Self::LogicNot => "not",
Self::LogicAnd => "and",
Self::LogicOr => "or",
Self::LogicXor => "xor",
Self::Equals => "equals",
Self::NotEquals => "not_equals",
Self::Less => "less",
Self::LessOrEqual => "less_or_equal",
Self::Greater => "greater",
Self::GreaterOrEqual => "greater_or_equal",
Self::CheckCondition { .. } => "conditional",
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum LogicShortCircuitHandling {
Passthrough,
OverrideWith(bool),
}
#[derive(Debug)]
pub enum InvokePipeStepState {
EvaluatingFunc,
EvaluatingArgs {
num_evaluated: usize
},
PreTemporalCall {
step_function: RantFunctionHandle,
temporal_state: TemporalSpreadState,
args: Vec<RantValue>,
},
PreCall {
step_function: RantFunctionHandle,
args: Vec<RantValue>,
},
PostCall,
PostTemporalCall {
step_function: RantFunctionHandle,
temporal_state: TemporalSpreadState,
args: Vec<RantValue>,
}
}
#[derive(Debug, Copy, Clone)]
pub enum VarWriteMode {
SetOnly,
Define,
DefineConst,
}
#[derive(Debug)]
pub enum SetterKey<'a> {
Index(i64),
Slice(Slice),
KeyRef(&'a str),
KeyString(InternalString),
}
#[derive(Debug)]
pub enum SetterValueSource {
FromExpression(Rc<Sequence>),
FromValue(RantValue),
FromStack
}