-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
125 lines (116 loc) · 3.69 KB
/
mod.rs
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
use crate::{
objects::Expression,
objects::Request,
out::{ErrorType, EvalResult},
token::tokentype::TokenType,
tree::{Node, Tree},
};
impl Node {
fn branches(&self) -> Vec<&Node> {
match self {
Self::Binary(lhs, _, rhs) => vec![&lhs, &rhs],
Self::Unary(_, node) => vec![&node],
Self::Func(_, nodes) => {
(*nodes.iter().map(|x| &(**x)).collect::<Vec<&Node>>()).to_vec()
}
Self::Literal(_) | Self::Var(_) => vec![],
Self::Union(nodes) => (*nodes.iter().map(|x| &(**x)).collect::<Vec<&Node>>()).to_vec(),
}
}
}
/// Converts a tree into a comprehensible request by the user.
pub fn interpret_tree(tree: &Tree) -> EvalResult<Request> {
let equal_tokens = match_all(&tree.0, 0, &|node| match node {
Expression::Binary(_, token_type, _) => *token_type == TokenType::Equal,
_ => false,
});
if let Some(equals) = equal_tokens {
// Function or var declaration
if equals.len() > 1 {
return Err(ErrorType::InvalidTokenPosition {
token: TokenType::Equal,
});
}
let equal_node_info = &equals[0];
// Avoid equals in brackets
if equal_node_info.depth != 0 {
return Err(ErrorType::InvalidTokenPosition {
token: TokenType::Equal,
});
}
let (left, right) = match equal_node_info.node {
Expression::Binary(left_expr, operator, right_expr) => {
if *operator != TokenType::Equal {
return Err(ErrorType::InternalError {
message: "operator was not of expected type".to_owned(),
});
} else {
(&(**left_expr), &(**right_expr))
}
}
_ => {
return Err(ErrorType::InternalError {
message: "node was not of expected type".to_owned(),
})
}
};
match left {
Expression::Var(identifier) => {
return Ok(Request::VarDeclaration(
identifier.clone(),
Box::new(right.clone()),
))
}
Expression::Func(identifier, arguments_node) => {
let mut params: Vec<String> = vec![];
for node_box in arguments_node {
let node = &**node_box;
match node {
Expression::Var(arg_name) => params.push(arg_name.clone()),
_ => return Err(ErrorType::InvalidDeclaration),
}
}
return Ok(Request::FuncDeclaration(
identifier.clone(),
params,
Box::new(right.clone()),
));
}
_ => return Err(ErrorType::InvalidDeclaration),
}
} else {
return Ok(Request::Evaluation(Box::new(tree.0.clone())));
}
}
#[derive(Clone, Debug)]
struct NodeInfo<'a> {
node: &'a Node,
depth: u32,
}
/// Finds all matches within a tree.
fn match_all<'a, P>(
start: &'a Node,
starting_depth: u32,
predicate: &P,
) -> Option<Vec<NodeInfo<'a>>>
where
P: Fn(&Node) -> bool,
{
let mut out = vec![];
if predicate(start) {
out.push(NodeInfo {
node: start,
depth: starting_depth,
});
}
for branch in start.branches() {
if let Some(results) = match_all(branch, starting_depth + 1, predicate) {
out = [out, results].concat();
}
}
if out.is_empty() {
None
} else {
Some(out)
}
}