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
use token::*;
use ast::*;
use ast::ASTField::*;
use ast::ASTItemType::*;
use ast::ASTAction::*;
// TODO: Fix complexity
#[cfg_attr(feature="clippy", allow(cyclomatic_complexity))]
pub fn parse(tokens: &[Token]) -> AST {
let mut ast: AST = vec! { };
let mut stack: Vec<ASTLiteral> = vec! { };
macro_rules! move_stack_to_ast {
() => {
for item in &stack {
ast.push(Box::new(ASTItem(Action(PushLiteralToStack(ASTLiteral::from(item.clone()))))));
}
stack.clear();
}
}
let mut tokens_iter = tokens.iter();
while let Some(token) = tokens_iter.next() {
macro_rules! collect_group {
($group_type: expr, $start_token: pat, $end_token: pat) => {
// Keep count of the amount of brackets
let (mut open, mut close) = (1, 0);
// Get the name of the group
let token = tokens_iter.next().expect("Failed to get group name");
let group_name = match token.clone() {
Token::Name(name) => name,
_ => panic!("Group name is not a Name"),
};
// A vector to store the inner tokens in
let mut new_tokens: Vec<Token> = vec! { };
// As long as there are more open brackets than closing brackets
while open > close {
if let Some(new_token) = tokens_iter.next() {
// Find matching brackets
match *new_token {
$start_token => open += 1,
$end_token => close += 1,
_ => (),
}
// Don't include closing bracket to new tokens
if open != close {
new_tokens.push(new_token.clone());
}
}
else {
panic!("Not enough closing brackets!");
}
}
// Generate new AST for inner group
let new_ast = parse(&new_tokens);
// Push the inner AST to the main AST
ast.push(Box::new(ASTField::ASTGroup($group_type, group_name, new_ast)));
}
}
// Collect class
if let Token::StartClass = *token {
collect_group!(ASTGroupType::Class(), Token::StartClass, Token::EndClass);
continue;
}
// Collect function
if let Token::StartFunction = *token {
collect_group!(ASTGroupType::Function(), Token::StartFunction, Token::EndFunction);
continue;
}
// Collect while
if let Token::StartWhile = *token {
collect_group!(ASTGroupType::While(), Token::StartWhile, Token::EndWhile);
continue;
}
if let (Some(astaction), might_change_stack) = match *token {
Token::StartClass | Token::EndClass
| Token::StartFunction | Token::EndFunction
| Token::StartWhile | Token::EndWhile
=> {
(None, true)
},
Token::Comment(ref val) => {
// A comment may be pushed to the AST directly,
// because no further parsing is needed
ast.push(Box::new(ASTItem(Literal(ASTLiteral::Comment(val.clone())))));
(None, false)
},
Token::Name(_) | Token::String(_) | Token::Number(_) => {
stack.push(ASTLiteral::from(token.clone()));
(None, false)
},
Token::PopValue => {
if stack.len() >= 1 {
stack.pop();
(None, false)
}
else {
(Some(ASTAction::PopValueFromStack()), true)
}
},
Token::SwapTwo => {
if stack.len() >= 2 {
let y = stack.pop().unwrap();
let x = stack.pop().unwrap();
stack.push(y);
stack.push(x);
(None, true)
}
else {
(Some(SwapTwoFromStack()), true)
}
},
Token::Return => {
(Some(Return()), true)
},
Token::Assign => {
if stack.len() >= 2 {
let value = stack.pop().unwrap();
let name = stack.pop().unwrap();
(Some(Assign(name, value)), false)
}
else {
(Some(AssignFromStack()), true)
}
},
Token::NewInstance => {
if stack.len() >= 2 {
let cname = stack.pop().unwrap();
let name = stack.pop().unwrap();
(Some(NewInstance(name, cname)), false)
}
else {
(Some(NewInstanceFromStack()), true)
}
},
Token::RetrieveFunction => {
if stack.len() >= 2 {
let fname = stack.pop().unwrap();
let oname = stack.pop().unwrap();
(Some(RetrieveFunction(oname, fname)), true)
}
else {
(Some(RetrieveFunctionFromStack()), true)
}
},
Token::PopAndRunFunction => {
(Some(PopAndRunFunction()), true)
},
Token::RetrieveFromName => {
if stack.len() >= 1 {
let name = stack.pop().unwrap();
(Some(RetrieveFromName(name)), true)
}
else {
(Some(RetrieveFromNameFromStack()), true)
}
},
Token::AssignCurrent => {
if stack.len() >= 1 {
let name = stack.pop().unwrap();
(Some(AssignCurrent(name)), false)
}
else {
(Some(AssignCurrentFromStack()), true)
}
},
} {
if might_change_stack {
move_stack_to_ast!();
}
ast.push(Box::new(ASTItem(Action(astaction))));
}
}
if ! stack.is_empty() {
eprintln!("Stack not empty, pushing to AST! (This might be an error)");
move_stack_to_ast!();
}
ast
}