1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#[derive(Copy, Clone, Debug)]
pub struct Symbol {
pub val: char,
pub state: bool,
}
impl PartialEq for Symbol {
fn eq(&self, other: &Symbol) -> bool {
self.val == other.val && self.state == other.state
}
}
pub struct Term {
pub symbols: Vec<Symbol>,
}
impl Term {
pub fn new(symbol: Symbol) -> Term {
let mut t = Term {
symbols: Vec::new(),
};
t.symbols.push(symbol);
return t;
}
pub fn contains(&self, sym: &Symbol) -> bool {
for symbol in self.symbols.iter() {
if symbol == sym {
return true;
}
}
return false;
}
pub fn insert(&mut self, sym: Symbol) -> bool {
if self.contains(&sym) {
return false;
}
self.symbols.push(sym);
return true;
}
pub fn remove(&mut self, sym: Symbol) -> bool {
if !self.contains(&sym) {
return false;
}
let mut newvec: Vec<Symbol> = Vec::new();
for s in &self.symbols {
if sym != *s {
newvec.push(*s);
}
}
self.symbols = newvec;
return true;
}
pub fn merge(&mut self, other: Term) {
let ovec: Vec<Symbol> = other.symbols;
for sym in ovec.iter() {
self.insert(sym.clone());
}
}
}
#[macro_export]
macro_rules! term {
( $( $x:expr ),* ) => {{
let mut ts: Vec<Symbol> = Vec::new();
$( ts.push(symbol!($x)); )*
Term { symbols: ts }
}};
}
#[macro_export]
macro_rules! symbol {
($x:expr) => {{
if $x.starts_with('!') {
let ch = $x.chars().nth(1).unwrap();
Symbol { val: ch, state: false }
} else {
let ch = $x.chars().nth(0).unwrap();
Symbol { val: ch, state: true }
}
}};
}