1use crate::{Expr, Name};
6use std::collections::HashMap;
7
8#[allow(dead_code)]
10pub struct WindowIterator<'a, T> {
11 pub(super) data: &'a [T],
12 pub(super) pos: usize,
13 pub(super) window: usize,
14}
15#[allow(dead_code)]
16impl<'a, T> WindowIterator<'a, T> {
17 pub fn new(data: &'a [T], window: usize) -> Self {
19 Self {
20 data,
21 pos: 0,
22 window,
23 }
24 }
25}
26#[allow(dead_code)]
28pub struct MinHeap<T: Ord> {
29 data: Vec<T>,
30}
31#[allow(dead_code)]
32impl<T: Ord> MinHeap<T> {
33 pub fn new() -> Self {
35 Self { data: Vec::new() }
36 }
37 pub fn push(&mut self, val: T) {
39 self.data.push(val);
40 self.sift_up(self.data.len() - 1);
41 }
42 pub fn pop(&mut self) -> Option<T> {
44 if self.data.is_empty() {
45 return None;
46 }
47 let n = self.data.len();
48 self.data.swap(0, n - 1);
49 let min = self.data.pop();
50 if !self.data.is_empty() {
51 self.sift_down(0);
52 }
53 min
54 }
55 pub fn peek(&self) -> Option<&T> {
57 self.data.first()
58 }
59 pub fn len(&self) -> usize {
61 self.data.len()
62 }
63 pub fn is_empty(&self) -> bool {
65 self.data.is_empty()
66 }
67 fn sift_up(&mut self, mut i: usize) {
68 while i > 0 {
69 let parent = (i - 1) / 2;
70 if self.data[i] < self.data[parent] {
71 self.data.swap(i, parent);
72 i = parent;
73 } else {
74 break;
75 }
76 }
77 }
78 fn sift_down(&mut self, mut i: usize) {
79 let n = self.data.len();
80 loop {
81 let left = 2 * i + 1;
82 let right = 2 * i + 2;
83 let mut smallest = i;
84 if left < n && self.data[left] < self.data[smallest] {
85 smallest = left;
86 }
87 if right < n && self.data[right] < self.data[smallest] {
88 smallest = right;
89 }
90 if smallest == i {
91 break;
92 }
93 self.data.swap(i, smallest);
94 i = smallest;
95 }
96 }
97}
98#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
100pub enum FfiSafety {
101 Safe,
103 Unsafe,
105 System,
107}
108#[allow(dead_code)]
110pub struct Stopwatch {
111 start: std::time::Instant,
112 splits: Vec<f64>,
113}
114#[allow(dead_code)]
115impl Stopwatch {
116 pub fn start() -> Self {
118 Self {
119 start: std::time::Instant::now(),
120 splits: Vec::new(),
121 }
122 }
123 pub fn split(&mut self) {
125 self.splits.push(self.elapsed_ms());
126 }
127 pub fn elapsed_ms(&self) -> f64 {
129 self.start.elapsed().as_secs_f64() * 1000.0
130 }
131 pub fn splits(&self) -> &[f64] {
133 &self.splits
134 }
135 pub fn num_splits(&self) -> usize {
137 self.splits.len()
138 }
139}
140#[allow(dead_code)]
142pub struct ConfigNode {
143 key: String,
144 value: Option<String>,
145 children: Vec<ConfigNode>,
146}
147#[allow(dead_code)]
148impl ConfigNode {
149 pub fn leaf(key: impl Into<String>, value: impl Into<String>) -> Self {
151 Self {
152 key: key.into(),
153 value: Some(value.into()),
154 children: Vec::new(),
155 }
156 }
157 pub fn section(key: impl Into<String>) -> Self {
159 Self {
160 key: key.into(),
161 value: None,
162 children: Vec::new(),
163 }
164 }
165 pub fn add_child(&mut self, child: ConfigNode) {
167 self.children.push(child);
168 }
169 pub fn key(&self) -> &str {
171 &self.key
172 }
173 pub fn value(&self) -> Option<&str> {
175 self.value.as_deref()
176 }
177 pub fn num_children(&self) -> usize {
179 self.children.len()
180 }
181 pub fn lookup(&self, path: &str) -> Option<&str> {
183 let mut parts = path.splitn(2, '.');
184 let head = parts.next()?;
185 let tail = parts.next();
186 if head != self.key {
187 return None;
188 }
189 match tail {
190 None => self.value.as_deref(),
191 Some(rest) => self.children.iter().find_map(|c| c.lookup_relative(rest)),
192 }
193 }
194 fn lookup_relative(&self, path: &str) -> Option<&str> {
195 let mut parts = path.splitn(2, '.');
196 let head = parts.next()?;
197 let tail = parts.next();
198 if head != self.key {
199 return None;
200 }
201 match tail {
202 None => self.value.as_deref(),
203 Some(rest) => self.children.iter().find_map(|c| c.lookup_relative(rest)),
204 }
205 }
206}
207#[allow(dead_code)]
209pub struct SparseVec<T: Default + Clone + PartialEq> {
210 entries: std::collections::HashMap<usize, T>,
211 default_: T,
212 logical_len: usize,
213}
214#[allow(dead_code)]
215impl<T: Default + Clone + PartialEq> SparseVec<T> {
216 pub fn new(len: usize) -> Self {
218 Self {
219 entries: std::collections::HashMap::new(),
220 default_: T::default(),
221 logical_len: len,
222 }
223 }
224 pub fn set(&mut self, idx: usize, val: T) {
226 if val == self.default_ {
227 self.entries.remove(&idx);
228 } else {
229 self.entries.insert(idx, val);
230 }
231 }
232 pub fn get(&self, idx: usize) -> &T {
234 self.entries.get(&idx).unwrap_or(&self.default_)
235 }
236 pub fn len(&self) -> usize {
238 self.logical_len
239 }
240 pub fn is_empty(&self) -> bool {
242 self.len() == 0
243 }
244 pub fn nnz(&self) -> usize {
246 self.entries.len()
247 }
248}
249#[allow(dead_code)]
251pub struct Fixture {
252 data: std::collections::HashMap<String, String>,
253}
254#[allow(dead_code)]
255impl Fixture {
256 pub fn new() -> Self {
258 Self {
259 data: std::collections::HashMap::new(),
260 }
261 }
262 pub fn set(&mut self, key: impl Into<String>, val: impl Into<String>) {
264 self.data.insert(key.into(), val.into());
265 }
266 pub fn get(&self, key: &str) -> Option<&str> {
268 self.data.get(key).map(|s| s.as_str())
269 }
270 pub fn len(&self) -> usize {
272 self.data.len()
273 }
274 pub fn is_empty(&self) -> bool {
276 self.len() == 0
277 }
278}
279#[allow(dead_code)]
281pub struct WriteOnce<T> {
282 value: std::cell::Cell<Option<T>>,
283}
284#[allow(dead_code)]
285impl<T: Copy> WriteOnce<T> {
286 pub fn new() -> Self {
288 Self {
289 value: std::cell::Cell::new(None),
290 }
291 }
292 pub fn write(&self, val: T) -> bool {
294 if self.value.get().is_some() {
295 return false;
296 }
297 self.value.set(Some(val));
298 true
299 }
300 pub fn read(&self) -> Option<T> {
302 self.value.get()
303 }
304 pub fn is_written(&self) -> bool {
306 self.value.get().is_some()
307 }
308}
309#[allow(dead_code)]
311pub struct PathBuf {
312 components: Vec<String>,
313}
314#[allow(dead_code)]
315impl PathBuf {
316 pub fn new() -> Self {
318 Self {
319 components: Vec::new(),
320 }
321 }
322 pub fn push(&mut self, comp: impl Into<String>) {
324 self.components.push(comp.into());
325 }
326 pub fn pop(&mut self) {
328 self.components.pop();
329 }
330 pub fn as_str(&self) -> String {
332 self.components.join("/")
333 }
334 pub fn depth(&self) -> usize {
336 self.components.len()
337 }
338 pub fn clear(&mut self) {
340 self.components.clear();
341 }
342}
343#[allow(dead_code)]
345#[allow(missing_docs)]
346pub struct RewriteRule {
347 pub name: String,
349 pub lhs: String,
351 pub rhs: String,
353 pub conditional: bool,
355}
356#[allow(dead_code)]
357impl RewriteRule {
358 pub fn unconditional(
360 name: impl Into<String>,
361 lhs: impl Into<String>,
362 rhs: impl Into<String>,
363 ) -> Self {
364 Self {
365 name: name.into(),
366 lhs: lhs.into(),
367 rhs: rhs.into(),
368 conditional: false,
369 }
370 }
371 pub fn conditional(
373 name: impl Into<String>,
374 lhs: impl Into<String>,
375 rhs: impl Into<String>,
376 ) -> Self {
377 Self {
378 name: name.into(),
379 lhs: lhs.into(),
380 rhs: rhs.into(),
381 conditional: true,
382 }
383 }
384 pub fn display(&self) -> String {
386 format!("{}: {} → {}", self.name, self.lhs, self.rhs)
387 }
388}
389#[allow(dead_code)]
391#[allow(missing_docs)]
392pub enum DecisionNode {
393 Leaf(String),
395 Branch {
397 key: String,
398 val: String,
399 yes_branch: Box<DecisionNode>,
400 no_branch: Box<DecisionNode>,
401 },
402}
403#[allow(dead_code)]
404impl DecisionNode {
405 pub fn evaluate(&self, ctx: &std::collections::HashMap<String, String>) -> &str {
407 match self {
408 DecisionNode::Leaf(action) => action.as_str(),
409 DecisionNode::Branch {
410 key,
411 val,
412 yes_branch,
413 no_branch,
414 } => {
415 let actual = ctx.get(key).map(|s| s.as_str()).unwrap_or("");
416 if actual == val.as_str() {
417 yes_branch.evaluate(ctx)
418 } else {
419 no_branch.evaluate(ctx)
420 }
421 }
422 }
423 }
424 pub fn depth(&self) -> usize {
426 match self {
427 DecisionNode::Leaf(_) => 0,
428 DecisionNode::Branch {
429 yes_branch,
430 no_branch,
431 ..
432 } => 1 + yes_branch.depth().max(no_branch.depth()),
433 }
434 }
435}
436#[allow(dead_code)]
438pub struct SlidingSum {
439 window: Vec<f64>,
440 capacity: usize,
441 pos: usize,
442 sum: f64,
443 count: usize,
444}
445#[allow(dead_code)]
446impl SlidingSum {
447 pub fn new(capacity: usize) -> Self {
449 Self {
450 window: vec![0.0; capacity],
451 capacity,
452 pos: 0,
453 sum: 0.0,
454 count: 0,
455 }
456 }
457 pub fn push(&mut self, val: f64) {
459 let oldest = self.window[self.pos];
460 self.sum -= oldest;
461 self.sum += val;
462 self.window[self.pos] = val;
463 self.pos = (self.pos + 1) % self.capacity;
464 if self.count < self.capacity {
465 self.count += 1;
466 }
467 }
468 pub fn sum(&self) -> f64 {
470 self.sum
471 }
472 pub fn mean(&self) -> Option<f64> {
474 if self.count == 0 {
475 None
476 } else {
477 Some(self.sum / self.count as f64)
478 }
479 }
480 pub fn count(&self) -> usize {
482 self.count
483 }
484}
485#[derive(Clone, Debug, PartialEq)]
487pub enum FfiValue {
488 Bool(bool),
490 UInt(u64),
492 Int(i64),
494 Float(f64),
496 Str(String),
498 Bytes(Vec<u8>),
500 Unit,
502}
503impl FfiValue {
504 pub fn try_from_expr(expr: &Expr, ty: &FfiType) -> Result<Self, FfiError> {
506 match (expr, ty) {
507 (Expr::Lit(crate::Literal::Nat(n)), FfiType::UInt64) => Ok(FfiValue::UInt(*n)),
508 (Expr::Lit(crate::Literal::Nat(n)), FfiType::UInt32) => {
509 if *n <= u32::MAX as u64 {
510 Ok(FfiValue::UInt(*n))
511 } else {
512 Err(FfiError::ValueOutOfRange(format!(
513 "u32 range exceeded: {}",
514 n
515 )))
516 }
517 }
518 (Expr::Lit(crate::Literal::Nat(n)), FfiType::Int64) => {
519 if *n <= i64::MAX as u64 {
520 Ok(FfiValue::Int(*n as i64))
521 } else {
522 Err(FfiError::ValueOutOfRange(format!(
523 "i64 range exceeded: {}",
524 n
525 )))
526 }
527 }
528 (Expr::Lit(crate::Literal::Str(s)), FfiType::String) => Ok(FfiValue::Str(s.clone())),
529 _ => Err(FfiError::TypeMismatch(format!(
530 "Cannot convert {:?} to {:?}",
531 expr, ty
532 ))),
533 }
534 }
535 pub fn to_expr(&self) -> Expr {
537 match self {
538 FfiValue::Bool(b) => Expr::Const(
539 if *b {
540 Name::str("True")
541 } else {
542 Name::str("False")
543 },
544 vec![],
545 ),
546 FfiValue::UInt(n) => Expr::Lit(crate::Literal::Nat(*n)),
547 FfiValue::Int(n) => {
548 if *n >= 0 {
549 Expr::Lit(crate::Literal::Nat(*n as u64))
550 } else {
551 Expr::Const(Name::str("Int.neg"), vec![])
552 }
553 }
554 FfiValue::Float(f) => Expr::Lit(crate::Literal::Str(f.to_string())),
555 FfiValue::Str(s) => Expr::Lit(crate::Literal::Str(s.clone())),
556 FfiValue::Bytes(bs) => Expr::Lit(crate::Literal::Str(
557 bs.iter().map(|b| format!("{:02x}", b)).collect::<String>(),
558 )),
559 FfiValue::Unit => Expr::Sort(crate::Level::zero()),
560 }
561 }
562}
563#[allow(dead_code)]
565pub struct StatSummary {
566 count: u64,
567 sum: f64,
568 min: f64,
569 max: f64,
570}
571#[allow(dead_code)]
572impl StatSummary {
573 pub fn new() -> Self {
575 Self {
576 count: 0,
577 sum: 0.0,
578 min: f64::INFINITY,
579 max: f64::NEG_INFINITY,
580 }
581 }
582 pub fn record(&mut self, val: f64) {
584 self.count += 1;
585 self.sum += val;
586 if val < self.min {
587 self.min = val;
588 }
589 if val > self.max {
590 self.max = val;
591 }
592 }
593 pub fn mean(&self) -> Option<f64> {
595 if self.count == 0 {
596 None
597 } else {
598 Some(self.sum / self.count as f64)
599 }
600 }
601 pub fn min(&self) -> Option<f64> {
603 if self.count == 0 {
604 None
605 } else {
606 Some(self.min)
607 }
608 }
609 pub fn max(&self) -> Option<f64> {
611 if self.count == 0 {
612 None
613 } else {
614 Some(self.max)
615 }
616 }
617 pub fn count(&self) -> u64 {
619 self.count
620 }
621}
622#[derive(Clone, PartialEq, Eq, Hash, Debug)]
626pub enum FfiType {
627 Bool,
629 UInt8,
631 UInt16,
633 UInt32,
635 UInt64,
637 Int8,
639 Int16,
641 Int32,
643 Int64,
645 Float32,
647 Float64,
649 String,
651 ByteArray,
653 Unit,
655 Ptr(Box<FfiType>),
657 Fn(Vec<FfiType>, Box<FfiType>),
659 OxiLean(String),
661}
662impl FfiType {
663 pub fn is_ffi_safe(&self) -> bool {
665 match self {
666 FfiType::Bool
667 | FfiType::UInt8
668 | FfiType::UInt16
669 | FfiType::UInt32
670 | FfiType::UInt64
671 | FfiType::Int8
672 | FfiType::Int16
673 | FfiType::Int32
674 | FfiType::Int64
675 | FfiType::Float32
676 | FfiType::Float64
677 | FfiType::String
678 | FfiType::ByteArray
679 | FfiType::Unit => true,
680 FfiType::Ptr(inner) => inner.is_ffi_safe(),
681 FfiType::Fn(params, ret) => params.iter().all(|t| t.is_ffi_safe()) && ret.is_ffi_safe(),
682 FfiType::OxiLean(_) => false,
683 }
684 }
685 pub fn size_bytes(&self) -> Option<usize> {
687 match self {
688 FfiType::Bool => Some(1),
689 FfiType::UInt8 | FfiType::Int8 => Some(1),
690 FfiType::UInt16 | FfiType::Int16 => Some(2),
691 FfiType::UInt32 | FfiType::Int32 => Some(4),
692 FfiType::UInt64 | FfiType::Int64 => Some(8),
693 FfiType::Float32 => Some(4),
694 FfiType::Float64 => Some(8),
695 FfiType::Unit => Some(0),
696 FfiType::Ptr(_) => Some(std::mem::size_of::<*const ()>()),
697 FfiType::String | FfiType::ByteArray => None,
698 FfiType::Fn(_, _) => Some(std::mem::size_of::<*const ()>()),
699 FfiType::OxiLean(_) => None,
700 }
701 }
702}
703#[allow(dead_code)]
705pub struct TransformStat {
706 before: StatSummary,
707 after: StatSummary,
708}
709#[allow(dead_code)]
710impl TransformStat {
711 pub fn new() -> Self {
713 Self {
714 before: StatSummary::new(),
715 after: StatSummary::new(),
716 }
717 }
718 pub fn record_before(&mut self, v: f64) {
720 self.before.record(v);
721 }
722 pub fn record_after(&mut self, v: f64) {
724 self.after.record(v);
725 }
726 pub fn mean_ratio(&self) -> Option<f64> {
728 let b = self.before.mean()?;
729 let a = self.after.mean()?;
730 if b.abs() < f64::EPSILON {
731 return None;
732 }
733 Some(a / b)
734 }
735}
736#[allow(dead_code)]
738pub struct StringPool {
739 free: Vec<String>,
740}
741#[allow(dead_code)]
742impl StringPool {
743 pub fn new() -> Self {
745 Self { free: Vec::new() }
746 }
747 pub fn take(&mut self) -> String {
749 self.free.pop().unwrap_or_default()
750 }
751 pub fn give(&mut self, mut s: String) {
753 s.clear();
754 self.free.push(s);
755 }
756 pub fn free_count(&self) -> usize {
758 self.free.len()
759 }
760}
761#[allow(dead_code)]
763pub struct SmallMap<K: Ord + Clone, V: Clone> {
764 entries: Vec<(K, V)>,
765}
766#[allow(dead_code)]
767impl<K: Ord + Clone, V: Clone> SmallMap<K, V> {
768 pub fn new() -> Self {
770 Self {
771 entries: Vec::new(),
772 }
773 }
774 pub fn insert(&mut self, key: K, val: V) {
776 match self.entries.binary_search_by_key(&&key, |(k, _)| k) {
777 Ok(i) => self.entries[i].1 = val,
778 Err(i) => self.entries.insert(i, (key, val)),
779 }
780 }
781 pub fn get(&self, key: &K) -> Option<&V> {
783 self.entries
784 .binary_search_by_key(&key, |(k, _)| k)
785 .ok()
786 .map(|i| &self.entries[i].1)
787 }
788 pub fn len(&self) -> usize {
790 self.entries.len()
791 }
792 pub fn is_empty(&self) -> bool {
794 self.entries.is_empty()
795 }
796 pub fn keys(&self) -> Vec<&K> {
798 self.entries.iter().map(|(k, _)| k).collect()
799 }
800 pub fn values(&self) -> Vec<&V> {
802 self.entries.iter().map(|(_, v)| v).collect()
803 }
804}
805#[allow(dead_code)]
807pub enum Either2<A, B> {
808 First(A),
810 Second(B),
812}
813#[allow(dead_code)]
814impl<A, B> Either2<A, B> {
815 pub fn is_first(&self) -> bool {
817 matches!(self, Either2::First(_))
818 }
819 pub fn is_second(&self) -> bool {
821 matches!(self, Either2::Second(_))
822 }
823 pub fn first(self) -> Option<A> {
825 match self {
826 Either2::First(a) => Some(a),
827 _ => None,
828 }
829 }
830 pub fn second(self) -> Option<B> {
832 match self {
833 Either2::Second(b) => Some(b),
834 _ => None,
835 }
836 }
837 pub fn map_first<C, F: FnOnce(A) -> C>(self, f: F) -> Either2<C, B> {
839 match self {
840 Either2::First(a) => Either2::First(f(a)),
841 Either2::Second(b) => Either2::Second(b),
842 }
843 }
844}
845#[allow(dead_code)]
847pub struct SimpleDag {
848 edges: Vec<Vec<usize>>,
850}
851#[allow(dead_code)]
852impl SimpleDag {
853 pub fn new(n: usize) -> Self {
855 Self {
856 edges: vec![Vec::new(); n],
857 }
858 }
859 pub fn add_edge(&mut self, from: usize, to: usize) {
861 if from < self.edges.len() {
862 self.edges[from].push(to);
863 }
864 }
865 pub fn successors(&self, node: usize) -> &[usize] {
867 self.edges.get(node).map(|v| v.as_slice()).unwrap_or(&[])
868 }
869 pub fn can_reach(&self, from: usize, to: usize) -> bool {
871 let mut visited = vec![false; self.edges.len()];
872 self.dfs(from, to, &mut visited)
873 }
874 fn dfs(&self, cur: usize, target: usize, visited: &mut Vec<bool>) -> bool {
875 if cur == target {
876 return true;
877 }
878 if cur >= visited.len() || visited[cur] {
879 return false;
880 }
881 visited[cur] = true;
882 for &next in self.successors(cur) {
883 if self.dfs(next, target, visited) {
884 return true;
885 }
886 }
887 false
888 }
889 pub fn topological_sort(&self) -> Option<Vec<usize>> {
891 let n = self.edges.len();
892 let mut in_degree = vec![0usize; n];
893 for succs in &self.edges {
894 for &s in succs {
895 if s < n {
896 in_degree[s] += 1;
897 }
898 }
899 }
900 let mut queue: std::collections::VecDeque<usize> =
901 (0..n).filter(|&i| in_degree[i] == 0).collect();
902 let mut order = Vec::new();
903 while let Some(node) = queue.pop_front() {
904 order.push(node);
905 for &s in self.successors(node) {
906 if s < n {
907 in_degree[s] -= 1;
908 if in_degree[s] == 0 {
909 queue.push_back(s);
910 }
911 }
912 }
913 }
914 if order.len() == n {
915 Some(order)
916 } else {
917 None
918 }
919 }
920 pub fn num_nodes(&self) -> usize {
922 self.edges.len()
923 }
924}
925#[derive(Clone, Debug)]
927pub struct FfiSignature {
928 pub params: Vec<FfiType>,
930 pub ret_type: Box<FfiType>,
932}
933impl FfiSignature {
934 pub fn new(params: Vec<FfiType>, ret_type: Box<FfiType>) -> Self {
936 FfiSignature { params, ret_type }
937 }
938 pub fn validate(&self) -> Result<(), FfiError> {
940 for param in &self.params {
941 if !param.is_ffi_safe() {
942 return Err(FfiError::InvalidSignature(format!(
943 "Unsafe parameter type: {}",
944 param
945 )));
946 }
947 }
948 if !self.ret_type.is_ffi_safe() {
949 return Err(FfiError::InvalidSignature(format!(
950 "Unsafe return type: {}",
951 self.ret_type
952 )));
953 }
954 Ok(())
955 }
956 pub fn matches_expr(&self, expr: &Expr) -> bool {
958 matches!(expr, Expr::Const(_, _))
959 }
960}
961#[allow(dead_code)]
963pub struct NonEmptyVec<T> {
964 head: T,
965 tail: Vec<T>,
966}
967#[allow(dead_code)]
968impl<T> NonEmptyVec<T> {
969 pub fn singleton(val: T) -> Self {
971 Self {
972 head: val,
973 tail: Vec::new(),
974 }
975 }
976 pub fn push(&mut self, val: T) {
978 self.tail.push(val);
979 }
980 pub fn first(&self) -> &T {
982 &self.head
983 }
984 pub fn last(&self) -> &T {
986 self.tail.last().unwrap_or(&self.head)
987 }
988 pub fn len(&self) -> usize {
990 1 + self.tail.len()
991 }
992 pub fn is_empty(&self) -> bool {
994 self.len() == 0
995 }
996 pub fn to_vec(&self) -> Vec<&T> {
998 let mut v = vec![&self.head];
999 v.extend(self.tail.iter());
1000 v
1001 }
1002}
1003#[allow(dead_code)]
1005pub struct StackCalc {
1006 stack: Vec<i64>,
1007}
1008#[allow(dead_code)]
1009impl StackCalc {
1010 pub fn new() -> Self {
1012 Self { stack: Vec::new() }
1013 }
1014 pub fn push(&mut self, n: i64) {
1016 self.stack.push(n);
1017 }
1018 pub fn add(&mut self) {
1020 let b = self
1021 .stack
1022 .pop()
1023 .expect("stack must have at least two values for add");
1024 let a = self
1025 .stack
1026 .pop()
1027 .expect("stack must have at least two values for add");
1028 self.stack.push(a + b);
1029 }
1030 pub fn sub(&mut self) {
1032 let b = self
1033 .stack
1034 .pop()
1035 .expect("stack must have at least two values for sub");
1036 let a = self
1037 .stack
1038 .pop()
1039 .expect("stack must have at least two values for sub");
1040 self.stack.push(a - b);
1041 }
1042 pub fn mul(&mut self) {
1044 let b = self
1045 .stack
1046 .pop()
1047 .expect("stack must have at least two values for mul");
1048 let a = self
1049 .stack
1050 .pop()
1051 .expect("stack must have at least two values for mul");
1052 self.stack.push(a * b);
1053 }
1054 pub fn peek(&self) -> Option<i64> {
1056 self.stack.last().copied()
1057 }
1058 pub fn depth(&self) -> usize {
1060 self.stack.len()
1061 }
1062}
1063#[allow(dead_code)]
1065pub struct LabelSet {
1066 labels: Vec<String>,
1067}
1068#[allow(dead_code)]
1069impl LabelSet {
1070 pub fn new() -> Self {
1072 Self { labels: Vec::new() }
1073 }
1074 pub fn add(&mut self, label: impl Into<String>) {
1076 let s = label.into();
1077 if !self.labels.contains(&s) {
1078 self.labels.push(s);
1079 }
1080 }
1081 pub fn has(&self, label: &str) -> bool {
1083 self.labels.iter().any(|l| l == label)
1084 }
1085 pub fn count(&self) -> usize {
1087 self.labels.len()
1088 }
1089 pub fn all(&self) -> &[String] {
1091 &self.labels
1092 }
1093}
1094#[allow(dead_code)]
1096pub struct VersionedRecord<T: Clone> {
1097 history: Vec<T>,
1098}
1099#[allow(dead_code)]
1100impl<T: Clone> VersionedRecord<T> {
1101 pub fn new(initial: T) -> Self {
1103 Self {
1104 history: vec![initial],
1105 }
1106 }
1107 pub fn update(&mut self, val: T) {
1109 self.history.push(val);
1110 }
1111 pub fn current(&self) -> &T {
1113 self.history
1114 .last()
1115 .expect("VersionedRecord history is always non-empty after construction")
1116 }
1117 pub fn at_version(&self, n: usize) -> Option<&T> {
1119 self.history.get(n)
1120 }
1121 pub fn version(&self) -> usize {
1123 self.history.len() - 1
1124 }
1125 pub fn has_history(&self) -> bool {
1127 self.history.len() > 1
1128 }
1129}
1130#[allow(dead_code)]
1132pub struct RewriteRuleSet {
1133 rules: Vec<RewriteRule>,
1134}
1135#[allow(dead_code)]
1136impl RewriteRuleSet {
1137 pub fn new() -> Self {
1139 Self { rules: Vec::new() }
1140 }
1141 pub fn add(&mut self, rule: RewriteRule) {
1143 self.rules.push(rule);
1144 }
1145 pub fn len(&self) -> usize {
1147 self.rules.len()
1148 }
1149 pub fn is_empty(&self) -> bool {
1151 self.rules.is_empty()
1152 }
1153 pub fn conditional_rules(&self) -> Vec<&RewriteRule> {
1155 self.rules.iter().filter(|r| r.conditional).collect()
1156 }
1157 pub fn unconditional_rules(&self) -> Vec<&RewriteRule> {
1159 self.rules.iter().filter(|r| !r.conditional).collect()
1160 }
1161 pub fn get(&self, name: &str) -> Option<&RewriteRule> {
1163 self.rules.iter().find(|r| r.name == name)
1164 }
1165}
1166#[derive(Clone, Debug)]
1168pub struct ExternDecl {
1169 pub name: Name,
1171 pub type_expr: Expr,
1173 pub lib_name: String,
1175 pub symbol_name: String,
1177 pub safety: FfiSafety,
1179 pub calling_convention: CallingConvention,
1181 pub signature: FfiSignature,
1183}
1184impl ExternDecl {
1185 pub fn new(
1187 name: Name,
1188 type_expr: Expr,
1189 lib_name: String,
1190 symbol_name: String,
1191 safety: FfiSafety,
1192 calling_convention: CallingConvention,
1193 signature: FfiSignature,
1194 ) -> Self {
1195 ExternDecl {
1196 name,
1197 type_expr,
1198 lib_name,
1199 symbol_name,
1200 safety,
1201 calling_convention,
1202 signature,
1203 }
1204 }
1205 pub fn validate(&self) -> Result<(), FfiError> {
1207 self.signature.validate()?;
1208 if self.lib_name.is_empty() {
1209 return Err(FfiError::InvalidSignature(
1210 "Library name cannot be empty".to_string(),
1211 ));
1212 }
1213 if self.symbol_name.is_empty() {
1214 return Err(FfiError::InvalidSignature(
1215 "Symbol name cannot be empty".to_string(),
1216 ));
1217 }
1218 Ok(())
1219 }
1220}
1221pub struct BuiltinExterns;
1223impl BuiltinExterns {
1224 pub fn register_builtins(registry: &mut ExternRegistry) -> Result<(), FfiError> {
1226 Self::register_io(registry)?;
1227 Self::register_string(registry)?;
1228 Self::register_arithmetic(registry)?;
1229 Ok(())
1230 }
1231 pub(crate) fn register_io(registry: &mut ExternRegistry) -> Result<(), FfiError> {
1233 registry.register(ExternDecl::new(
1234 Name::str("builtin_print"),
1235 Expr::Const(Name::str("String"), vec![]),
1236 "libc".to_string(),
1237 "puts".to_string(),
1238 FfiSafety::System,
1239 CallingConvention::C,
1240 FfiSignature::new(vec![FfiType::String], Box::new(FfiType::Int32)),
1241 ))?;
1242 registry.register(ExternDecl::new(
1243 Name::str("builtin_print_int"),
1244 Expr::Const(Name::str("Nat"), vec![]),
1245 "libc".to_string(),
1246 "printf".to_string(),
1247 FfiSafety::System,
1248 CallingConvention::C,
1249 FfiSignature::new(
1250 vec![FfiType::String, FfiType::UInt64],
1251 Box::new(FfiType::Int32),
1252 ),
1253 ))?;
1254 Ok(())
1255 }
1256 pub(crate) fn register_string(registry: &mut ExternRegistry) -> Result<(), FfiError> {
1258 registry.register(ExternDecl::new(
1259 Name::str("builtin_strlen"),
1260 Expr::Const(Name::str("String"), vec![]),
1261 "libc".to_string(),
1262 "strlen".to_string(),
1263 FfiSafety::Unsafe,
1264 CallingConvention::C,
1265 FfiSignature::new(vec![FfiType::String], Box::new(FfiType::UInt64)),
1266 ))?;
1267 registry.register(ExternDecl::new(
1268 Name::str("builtin_strcmp"),
1269 Expr::Const(Name::str("String"), vec![]),
1270 "libc".to_string(),
1271 "strcmp".to_string(),
1272 FfiSafety::Unsafe,
1273 CallingConvention::C,
1274 FfiSignature::new(
1275 vec![FfiType::String, FfiType::String],
1276 Box::new(FfiType::Int32),
1277 ),
1278 ))?;
1279 Ok(())
1280 }
1281 pub(crate) fn register_arithmetic(registry: &mut ExternRegistry) -> Result<(), FfiError> {
1283 registry.register(ExternDecl::new(
1284 Name::str("builtin_abs"),
1285 Expr::Const(Name::str("Int"), vec![]),
1286 "libc".to_string(),
1287 "abs".to_string(),
1288 FfiSafety::Safe,
1289 CallingConvention::C,
1290 FfiSignature::new(vec![FfiType::Int32], Box::new(FfiType::Int32)),
1291 ))?;
1292 registry.register(ExternDecl::new(
1293 Name::str("builtin_sqrt"),
1294 Expr::Const(Name::str("Float"), vec![]),
1295 "libm".to_string(),
1296 "sqrt".to_string(),
1297 FfiSafety::Safe,
1298 CallingConvention::C,
1299 FfiSignature::new(vec![FfiType::Float64], Box::new(FfiType::Float64)),
1300 ))?;
1301 Ok(())
1302 }
1303}
1304#[derive(Clone, Debug, PartialEq, Eq)]
1306pub struct LibraryVersion {
1307 pub name: String,
1309 pub major: u32,
1311 pub minor: u32,
1313 pub patch: u32,
1315}
1316impl LibraryVersion {
1317 pub fn new(name: &str, major: u32, minor: u32, patch: u32) -> Self {
1319 Self {
1320 name: name.to_string(),
1321 major,
1322 minor,
1323 patch,
1324 }
1325 }
1326 pub fn at_least(&self, major: u32, minor: u32, patch: u32) -> bool {
1328 (self.major, self.minor, self.patch) >= (major, minor, patch)
1329 }
1330}
1331#[derive(Clone, Debug)]
1333pub struct SymbolMetadata {
1334 pub symbol: String,
1336 pub library: String,
1338 pub weak: bool,
1340 pub thread_local: bool,
1342}
1343impl SymbolMetadata {
1344 pub fn new(symbol: &str, library: &str) -> Self {
1346 Self {
1347 symbol: symbol.to_string(),
1348 library: library.to_string(),
1349 weak: false,
1350 thread_local: false,
1351 }
1352 }
1353 pub fn with_weak(mut self) -> Self {
1355 self.weak = true;
1356 self
1357 }
1358 pub fn with_thread_local(mut self) -> Self {
1360 self.thread_local = true;
1361 self
1362 }
1363}
1364#[allow(dead_code)]
1366pub struct RawFnPtr {
1367 ptr: usize,
1369 arity: usize,
1370 name: String,
1371}
1372#[allow(dead_code)]
1373impl RawFnPtr {
1374 pub fn new(ptr: usize, arity: usize, name: impl Into<String>) -> Self {
1376 Self {
1377 ptr,
1378 arity,
1379 name: name.into(),
1380 }
1381 }
1382 pub fn arity(&self) -> usize {
1384 self.arity
1385 }
1386 pub fn name(&self) -> &str {
1388 &self.name
1389 }
1390 pub fn raw(&self) -> usize {
1392 self.ptr
1393 }
1394}
1395#[allow(dead_code)]
1397pub struct TransitiveClosure {
1398 adj: Vec<Vec<usize>>,
1399 n: usize,
1400}
1401#[allow(dead_code)]
1402impl TransitiveClosure {
1403 pub fn new(n: usize) -> Self {
1405 Self {
1406 adj: vec![Vec::new(); n],
1407 n,
1408 }
1409 }
1410 pub fn add_edge(&mut self, from: usize, to: usize) {
1412 if from < self.n {
1413 self.adj[from].push(to);
1414 }
1415 }
1416 pub fn reachable_from(&self, start: usize) -> Vec<usize> {
1418 let mut visited = vec![false; self.n];
1419 let mut queue = std::collections::VecDeque::new();
1420 queue.push_back(start);
1421 while let Some(node) = queue.pop_front() {
1422 if node >= self.n || visited[node] {
1423 continue;
1424 }
1425 visited[node] = true;
1426 for &next in &self.adj[node] {
1427 queue.push_back(next);
1428 }
1429 }
1430 (0..self.n).filter(|&i| visited[i]).collect()
1431 }
1432 pub fn can_reach(&self, from: usize, to: usize) -> bool {
1434 self.reachable_from(from).contains(&to)
1435 }
1436}
1437#[allow(dead_code)]
1439pub struct FlatSubstitution {
1440 pairs: Vec<(String, String)>,
1441}
1442#[allow(dead_code)]
1443impl FlatSubstitution {
1444 pub fn new() -> Self {
1446 Self { pairs: Vec::new() }
1447 }
1448 pub fn add(&mut self, from: impl Into<String>, to: impl Into<String>) {
1450 self.pairs.push((from.into(), to.into()));
1451 }
1452 pub fn apply(&self, s: &str) -> String {
1454 let mut result = s.to_string();
1455 for (from, to) in &self.pairs {
1456 result = result.replace(from.as_str(), to.as_str());
1457 }
1458 result
1459 }
1460 pub fn len(&self) -> usize {
1462 self.pairs.len()
1463 }
1464 pub fn is_empty(&self) -> bool {
1466 self.pairs.is_empty()
1467 }
1468}
1469pub struct ExternRegistry {
1471 decls: HashMap<(String, String), ExternDecl>,
1473 name_map: HashMap<String, (String, String)>,
1475}
1476impl ExternRegistry {
1477 pub fn new() -> Self {
1479 ExternRegistry {
1480 decls: HashMap::new(),
1481 name_map: HashMap::new(),
1482 }
1483 }
1484 pub fn register(&mut self, decl: ExternDecl) -> Result<(), FfiError> {
1486 decl.validate()?;
1487 let key = (decl.lib_name.clone(), decl.symbol_name.clone());
1488 if self.decls.contains_key(&key) {
1489 return Err(FfiError::DuplicateSymbol(format!(
1490 "{}::{}",
1491 decl.lib_name, decl.symbol_name
1492 )));
1493 }
1494 let name_str = decl.name.to_string();
1495 if self.name_map.contains_key(&name_str) {
1496 return Err(FfiError::DuplicateSymbol(name_str));
1497 }
1498 self.name_map.insert(name_str, key.clone());
1499 self.decls.insert(key, decl);
1500 Ok(())
1501 }
1502 pub fn lookup(&self, name: &Name) -> Result<&ExternDecl, FfiError> {
1504 let name_str = name.to_string();
1505 let key = self
1506 .name_map
1507 .get(&name_str)
1508 .ok_or_else(|| FfiError::SymbolNotFound(name_str.clone()))?;
1509 self.decls
1510 .get(key)
1511 .ok_or(FfiError::SymbolNotFound(name_str))
1512 }
1513 pub fn lookup_by_symbol(
1515 &self,
1516 lib_name: &str,
1517 symbol_name: &str,
1518 ) -> Result<&ExternDecl, FfiError> {
1519 let key = (lib_name.to_string(), symbol_name.to_string());
1520 self.decls
1521 .get(&key)
1522 .ok_or_else(|| FfiError::SymbolNotFound(format!("{}::{}", lib_name, symbol_name)))
1523 }
1524 pub fn validate_all(&self) -> Result<(), FfiError> {
1526 for decl in self.decls.values() {
1527 decl.validate()?;
1528 }
1529 Ok(())
1530 }
1531 pub fn all_decls(&self) -> impl Iterator<Item = &ExternDecl> {
1533 self.decls.values()
1534 }
1535 pub fn count(&self) -> usize {
1537 self.decls.len()
1538 }
1539}
1540#[allow(dead_code)]
1542pub struct TokenBucket {
1543 capacity: u64,
1544 tokens: u64,
1545 refill_per_ms: u64,
1546 last_refill: std::time::Instant,
1547}
1548#[allow(dead_code)]
1549impl TokenBucket {
1550 pub fn new(capacity: u64, refill_per_ms: u64) -> Self {
1552 Self {
1553 capacity,
1554 tokens: capacity,
1555 refill_per_ms,
1556 last_refill: std::time::Instant::now(),
1557 }
1558 }
1559 pub fn try_consume(&mut self, n: u64) -> bool {
1561 self.refill();
1562 if self.tokens >= n {
1563 self.tokens -= n;
1564 true
1565 } else {
1566 false
1567 }
1568 }
1569 fn refill(&mut self) {
1570 let now = std::time::Instant::now();
1571 let elapsed_ms = now.duration_since(self.last_refill).as_millis() as u64;
1572 if elapsed_ms > 0 {
1573 let new_tokens = elapsed_ms * self.refill_per_ms;
1574 self.tokens = (self.tokens + new_tokens).min(self.capacity);
1575 self.last_refill = now;
1576 }
1577 }
1578 pub fn available(&self) -> u64 {
1580 self.tokens
1581 }
1582 pub fn capacity(&self) -> u64 {
1584 self.capacity
1585 }
1586}
1587#[allow(dead_code)]
1589pub struct PrefixCounter {
1590 children: std::collections::HashMap<char, PrefixCounter>,
1591 count: usize,
1592}
1593#[allow(dead_code)]
1594impl PrefixCounter {
1595 pub fn new() -> Self {
1597 Self {
1598 children: std::collections::HashMap::new(),
1599 count: 0,
1600 }
1601 }
1602 pub fn record(&mut self, s: &str) {
1604 self.count += 1;
1605 let mut node = self;
1606 for c in s.chars() {
1607 node = node.children.entry(c).or_default();
1608 node.count += 1;
1609 }
1610 }
1611 pub fn count_with_prefix(&self, prefix: &str) -> usize {
1613 let mut node = self;
1614 for c in prefix.chars() {
1615 match node.children.get(&c) {
1616 Some(n) => node = n,
1617 None => return 0,
1618 }
1619 }
1620 node.count
1621 }
1622}
1623#[allow(dead_code)]
1625pub struct FocusStack<T> {
1626 items: Vec<T>,
1627}
1628#[allow(dead_code)]
1629impl<T> FocusStack<T> {
1630 pub fn new() -> Self {
1632 Self { items: Vec::new() }
1633 }
1634 pub fn focus(&mut self, item: T) {
1636 self.items.push(item);
1637 }
1638 pub fn blur(&mut self) -> Option<T> {
1640 self.items.pop()
1641 }
1642 pub fn current(&self) -> Option<&T> {
1644 self.items.last()
1645 }
1646 pub fn depth(&self) -> usize {
1648 self.items.len()
1649 }
1650 pub fn is_empty(&self) -> bool {
1652 self.items.is_empty()
1653 }
1654}
1655#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
1657pub enum CallingConvention {
1658 Rust,
1660 C,
1662 System,
1664}
1665#[derive(Clone, PartialEq, Eq, Hash, Debug)]
1667pub enum FfiError {
1668 SymbolNotFound(String),
1670 LibraryNotFound(String),
1672 TypeMismatch(String),
1674 ValueOutOfRange(String),
1676 InvalidSignature(String),
1678 DuplicateSymbol(String),
1680 ValidationFailed(String),
1682}
1683#[derive(Clone, Debug, Default)]
1685pub struct LibraryManifest {
1686 pub entries: Vec<LibraryVersion>,
1688}
1689impl LibraryManifest {
1690 pub fn new() -> Self {
1692 Self::default()
1693 }
1694 pub fn require(&mut self, lib: LibraryVersion) {
1696 self.entries.push(lib);
1697 }
1698 pub fn requires_lib(&self, name: &str) -> bool {
1700 self.entries.iter().any(|l| l.name == name)
1701 }
1702 pub fn len(&self) -> usize {
1704 self.entries.len()
1705 }
1706 pub fn is_empty(&self) -> bool {
1708 self.entries.is_empty()
1709 }
1710}