duckscript/types/
runtime.rs1#[cfg(test)]
7#[path = "./runtime_test.rs"]
8mod runtime_test;
9
10use crate::types::command::Commands;
11use crate::types::env::Env;
12use crate::types::instruction::Instruction;
13use std::any::Any;
14use std::cell::RefCell;
15use std::collections::{HashMap, HashSet};
16use std::rc::Rc;
17
18#[derive(Debug, Clone)]
20pub enum StateValue {
21 Boolean(bool),
23 Number(isize),
25 UnsignedNumber(usize),
27 Number32Bit(i32),
29 UnsignedNumber32Bit(u32),
31 Number64Bit(i64),
33 UnsignedNumber64Bit(u64),
35 String(String),
37 ByteArray(Vec<u8>),
39 List(Vec<StateValue>),
41 Set(HashSet<String>),
43 SubState(HashMap<String, StateValue>),
45 Any(Rc<RefCell<dyn Any>>),
47}
48
49#[derive(Clone)]
51pub struct Context {
52 pub variables: HashMap<String, String>,
54 pub state: HashMap<String, StateValue>,
56 pub commands: Commands,
58}
59
60impl Context {
61 pub fn new() -> Context {
63 Context {
64 variables: HashMap::new(),
65 state: HashMap::new(),
66 commands: Commands::new(),
67 }
68 }
69}
70
71pub struct Runtime {
73 pub instructions: Option<Vec<Instruction>>,
75 pub label_to_line: HashMap<String, usize>,
77 pub context: Context,
79 pub env: Env,
81}
82
83impl Runtime {
84 pub fn new(context: Context, env: Option<Env>) -> Runtime {
86 Runtime {
87 instructions: None,
88 label_to_line: HashMap::new(),
89 context,
90 env: match env {
91 Some(value) => value,
92 None => Env::default(),
93 },
94 }
95 }
96}