mech_core/program/
mod.rs

1use crate::*;
2use std::io::Cursor;
3#[cfg(not(feature = "no_std"))]
4use std::collections::HashSet;
5#[cfg(feature = "no_std")]
6use hashbrown::HashSet;
7
8#[cfg(any(feature = "compiler", feature = "program"))]
9pub mod compiler;
10#[cfg(feature = "symbol_table")]
11pub mod symbol_table;
12#[cfg(feature = "program")]
13pub mod program;
14
15#[cfg(any(feature = "compiler", feature = "program"))]
16pub use self::compiler::*;
17#[cfg(feature = "symbol_table")]
18pub use self::symbol_table::*;
19#[cfg(feature = "program")]
20pub use self::program::*;
21
22// Program State
23// ----------------------------------------------------------------------------
24
25pub type Dictionary = HashMap<u64,String>;
26pub type KindTable = HashMap<u64, ValueKind>;
27#[cfg(feature = "enum")]
28pub type EnumTable = HashMap<u64, MechEnum>;
29
30pub struct ProgramState {
31  #[cfg(feature = "symbol_table")]
32  pub symbol_table: SymbolTableRef,
33  #[cfg(feature = "functions")]
34  pub functions: FunctionsRef,
35  #[cfg(feature = "functions")]
36  pub plan: Plan,
37  pub kinds: KindTable,
38  #[cfg(feature = "enum")]
39  pub enums: EnumTable,
40  pub dictionary: Ref<Dictionary>,
41}
42
43impl ProgramState {
44  pub fn new() -> ProgramState {
45    ProgramState {
46      #[cfg(feature = "symbol_table")]
47      symbol_table: Ref::new(SymbolTable::new()),
48      #[cfg(feature = "functions")]
49      functions: Ref::new(Functions::new()),
50      #[cfg(feature = "functions")]
51      plan: Plan::new(),
52      kinds: KindTable::new(),
53      #[cfg(feature = "enum")]
54      enums: EnumTable::new(),
55      dictionary: Ref::new(Dictionary::new()),
56    }
57  }
58
59  #[cfg(feature = "symbol_table")]
60  pub fn get_symbol(&self, id: u64) -> Option<Ref<Value>> {
61    let syms = self.symbol_table.borrow();
62    syms.get(id)
63  }
64
65      
66  #[cfg(feature = "functions")]
67  pub fn add_plan_step(&self, step: Box<dyn MechFunction>) {
68    let mut plan_brrw = self.plan.borrow_mut();
69    plan_brrw.push(step);
70  }
71
72  #[cfg(feature = "functions")]
73  pub fn insert_function(&self, fxn: FunctionDefinition) {
74    let mut fxns_brrw = self.functions.borrow_mut();
75    fxns_brrw.functions.insert(fxn.id, fxn);
76  }
77
78  #[cfg(feature = "symbol_table")]
79  pub fn save_symbol(&self, id: u64, name: String, value: Value, mutable: bool) -> ValRef {
80    let mut symbols_brrw = self.symbol_table.borrow_mut();
81    let val_ref = symbols_brrw.insert(id,value,mutable);
82    let mut dict_brrw = symbols_brrw.dictionary.borrow_mut();
83    dict_brrw.insert(id,name);
84    val_ref
85  }
86
87}
88
89pub fn parse_version_to_u16(s: &str) -> Option<u16> {
90  let parts: Vec<&str> = s.split('.').collect();
91  if parts.len() != 3 { return None; }
92
93  let major = parts[0].parse::<u16>().ok()?;
94  let minor = parts[1].parse::<u16>().ok()?;
95  let patch = parts[2].parse::<u16>().ok()?; // parse to u16 to check bounds easily
96
97  if major > 0b111 { return None; }    // 3 bits => 0..7
98  if minor > 0b1_1111 { return None; } // 5 bits => 0..31
99  if patch > 0xFF { return None; }     // 8 bits => 0..255
100
101  // Pack: major in bits 15..13, minor in bits 12..8, patch in bits 7..0
102  let encoded = (major << 13) | (minor << 8) | patch;
103  Some(encoded as u16)
104}