-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
265 lines (234 loc) · 8.07 KB
/
Copy pathmain.rs
File metadata and controls
265 lines (234 loc) · 8.07 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
mod ast;
mod rules;
mod tokenizer;
pub use crate::tokenizer::{Assoc, DataStore, MathFunction, MathOperator, Token, tokenize};
pub struct Shunter<'a> {
_ds: &'a DataStore,
}
impl<'a> Shunter<'a> {
pub fn new(ds: &'a DataStore) -> Self {
Self { _ds: ds }
}
pub fn shunt(&self, tokens: Vec<Token>) -> Vec<Token> {
let mut output = Vec::new();
let mut operator_stack = Vec::new();
let mut arity_stack = Vec::new();
for i in 0..tokens.len() {
let token = &tokens[i];
let prev = if i > 0 { Some(&tokens[i - 1]) } else { None };
let next = if i + 1 < tokens.len() {
Some(&tokens[i + 1])
} else {
None
};
let p_is_ending = prev.map_or(false, |p| {
p.is_number()
|| p.is_variable()
|| p.is_rp()
|| (if let Token::Function(f, _) = p {
f.is_constant()
} else {
false
})
});
let t_is_starting =
token.is_number() || token.is_variable() || token.is_lp() || token.is_function();
let implicit_mult = p_is_ending && t_is_starting;
// Unary minus handling: -term -> -1 * term
if let Token::Operator(MathOperator::Subtract, _) = token {
let is_unary = prev.map_or(true, |p| p.is_operator() || p.is_lp() || p.is_comma());
if is_unary {
if let Some(ahead) = next {
if ahead.is_variable()
|| ahead.is_lp()
|| ahead.is_function()
|| ahead.is_number()
{
output.push(Token::Number("-1".to_string()));
self.push_operator(
&mut output,
&mut operator_stack,
&mut arity_stack,
Token::Operator(MathOperator::Multiply, 2),
);
continue;
}
}
}
}
if implicit_mult {
self.push_operator(
&mut output,
&mut operator_stack,
&mut arity_stack,
Token::Operator(MathOperator::Multiply, 2),
);
}
// Normal token handling
self.handle_token(
&mut output,
&mut operator_stack,
&mut arity_stack,
token.clone(),
);
}
while let Some(op) = operator_stack.pop() {
output.push(self.finalize_arity(op, &mut arity_stack));
}
output
}
fn handle_token(
&self,
output: &mut Vec<Token>,
stack: &mut Vec<Token>,
arity: &mut Vec<usize>,
token: Token,
) {
match token {
Token::Number(_) | Token::Variable(_) => {
output.push(token);
}
Token::Function(func, _) => {
stack.push(Token::Function(func, 0));
arity.push(if func.is_constant() { 0 } else { 1 });
}
Token::Operator(_, _) => {
self.push_operator(output, stack, arity, token);
}
Token::LParen(ref v) => {
if v == "{" {
stack.push(Token::Function(MathFunction::List, 0));
arity.push(1);
stack.push(Token::LParen("(".to_string()));
} else {
stack.push(token);
}
}
Token::RParen(_) => {
while let Some(top) = stack.last() {
if top.is_lp() {
break;
}
let op = stack.pop().unwrap();
output.push(self.finalize_arity(op, arity));
}
stack.pop(); // Pop LParen
}
Token::Comma => {
while let Some(top) = stack.last() {
if top.is_lp() {
break;
}
let op = stack.pop().unwrap();
output.push(self.finalize_arity(op, arity));
}
if let Some(a) = arity.last_mut() {
*a += 1;
}
}
}
}
fn push_operator(
&self,
output: &mut Vec<Token>,
stack: &mut Vec<Token>,
arity: &mut Vec<usize>,
token: Token,
) {
let (op_type, _) = match token {
Token::Operator(t, a) => (t, a),
_ => panic!("Expected operator, got {:?}", token),
};
while let Some(top) = stack.last() {
if top.is_lp() {
break;
}
if let Token::Function(_, _) = top {
let op = stack.pop().unwrap();
output.push(self.finalize_arity(op, arity));
continue;
}
if let Token::Operator(top_type, _) = top {
if top_type.weight() > op_type.weight()
|| (top_type.weight() == op_type.weight() && op_type.assoc() == Assoc::Left)
{
let op = stack.pop().unwrap();
output.push(self.finalize_arity(op, arity));
} else {
break;
}
} else {
break;
}
}
stack.push(token);
}
fn finalize_arity(&self, mut token: Token, arity: &mut Vec<usize>) -> Token {
if token.is_function() {
token.set_arity(arity.pop().unwrap_or(0));
}
token
}
}
use std::io::{self, Write};
use std::time::Instant;
fn main() {
let ds = DataStore::default();
let mut engine = rules::RuleEngine::new();
for rule in rules::standard_rules() {
engine.add_rule(rule);
}
loop {
print!(">");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input.eq_ignore_ascii_case("q") || input.eq_ignore_ascii_case("quit") {
break;
}
if input.is_empty() {
continue;
}
// Tokenize
let start_tokenize = Instant::now();
let tokens = tokenize(input, &ds);
let time_tokenize = start_tokenize.elapsed();
// Shunt
let shunter = Shunter::new(&ds);
let start_shunt = Instant::now();
let rpn = shunter.shunt(tokens);
let time_shunt = start_shunt.elapsed();
// AST
let start_ast = Instant::now();
let ast = match ast::build_ast(rpn) {
Ok(node) => node,
Err(e) => {
println!("Failed to parse AST: {:?}", e);
continue;
}
};
let time_ast = start_ast.elapsed();
// Rules Engine
let mut local_engine = rules::RuleEngine::new();
for rule in rules::standard_rules() {
local_engine.add_rule(rule);
}
let start_rules = Instant::now();
let simplified_ast = local_engine.simplify(ast);
let time_rules = start_rules.elapsed();
println!("\n--- Results ---");
println!("Answer: {}", simplified_ast);
println!("Phase Timings:");
println!(" Tokenizer: {:?}", time_tokenize);
println!(" Shunting: {:?}", time_shunt);
println!(" AST Builder: {:?}", time_ast);
println!(" Rule Engine: {:?}", time_rules);
println!("Rules Applied ({} total):", local_engine.logs.len());
for log in &local_engine.logs {
println!(" [{}]", log.rule_name);
println!(" Before: {}", log.before);
println!(" After: {}", log.after);
}
}
}