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
#![doc(html_root_url = "https://docs.rs/macro_helper/0.1.1/")]
#![no_std]
extern crate proc_macro;
extern crate alloc;
use proc_macro::TokenStream;
use core::fmt::Write;
use alloc::{ string::{String, ToString}, vec::Vec, format };
fn ops(input: TokenStream) -> TokenStream {
let input = input.to_string();
let mut operators = String::with_capacity(input.len());
let mut err = String::with_capacity(64);
let mut it: Vec<String> = input.split(|c: char| {
match c {
'+' => {operators.push('+'); true},
'-' => {operators.push('-'); true},
'*' => {operators.push('*'); true},
'/' => {operators.push('/'); true},
'%' => {operators.push('%'); true},
'^' => {operators.push('^'); true},
_ => false
}
}).map(|e| e.chars().filter(|c| c.is_numeric()).collect() ).collect();
let mut result: isize = it[0].parse().unwrap_or_else(|_| {
err.push_str("compile_error!(\"Error at parsing first operand.\")");
0
});
if err != "" {
return err.parse().unwrap();
}
for (i, (e, op)) in it.drain(1..).zip(operators.chars()).enumerate() {
if e == "" {
return format!("compile_error!(\"Error at parsing operand number {}.\")", i+1).parse().unwrap()
}
let e: isize = match e.parse() {
Ok(i) => i,
Err(_) => return format!("compile_error!(\"Error operand number {} overflows an isize.\")", i+1).parse().unwrap(),
};
match op {
'+' => result = match result.checked_add(e) {
Some(i) => i,
None => return format!("compile_error!(\"operation number {} failed.\")", i+1).parse().unwrap(),
},
'-' => result = match result.checked_sub(e) {
Some(i) => i,
None => return format!("compile_error!(\"operation number {} failed.\")", i+1).parse().unwrap(),
},
'*' => result = match result.checked_mul(e) {
Some(i) => i,
None => return format!("compile_error!(\"operation number {} failed.\")", i+1).parse().unwrap(),
},
'/' => result = match result.checked_div(e) {
Some(i) => i,
None => return format!("compile_error!(\"operation number {} failed.\")", i+1).parse().unwrap(),
},
'%' => result = match result.checked_rem(e) {
Some(i) => i,
None => return format!("compile_error!(\"operation number {} failed.\")", i+1).parse().unwrap(),
},
'^' => result = match result.checked_pow(e as u32) {
Some(i) => i,
None => return format!("compile_error!(\"operation number {} failed.\")", i+1).parse().unwrap(),
},
_ => ()
}
}
result.to_string().parse().unwrap()
}
#[proc_macro]
pub fn expansion_bucle(input: TokenStream) -> TokenStream {
let mut expansion = String::new();
let input = input.to_string();
let index = input.rfind("=>").unwrap_or_else(|| 1);
let (mut code, mut repetitions) = input.split_at(index);
repetitions = (&repetitions[2..]).trim();
let mathematic = repetitions.contains(|c: char| {
match c {
'+' => true,
'-' => true,
'*' => true,
'/' => true,
'%' => true,
'^' => true,
_ => false
}
});
let rep: TokenStream = repetitions.to_string().parse().unwrap();
let repetitions = if mathematic {
ops(rep).to_string().parse::<usize>().unwrap()
} else {
repetitions.parse::<usize>().unwrap()
};
code = code.trim();
if repetitions == 0 {
return expansion.parse().unwrap();
}
expansion.reserve(code.len()*repetitions);
for _ in 0..repetitions {
expansion.push_str(code);
}
expansion.parse().unwrap()
}
#[proc_macro]
pub fn expansion_bucle_counted(input: TokenStream) -> TokenStream {
let mut expansion = String::new();
let input = input.to_string();
let index = input.rfind("=>").unwrap_or_else(|| 1);
let (mut code, repetitions) = input.split_at(index);
let repetitions = (&repetitions[2..]).trim();
let mathematic = repetitions.contains(|c: char| {
match c {
'+' => true,
'-' => true,
'*' => true,
'/' => true,
'%' => true,
'^' => true,
_ => false
}
});
let rep: TokenStream = repetitions.to_string().parse().unwrap();
let repetitions = if mathematic {
ops(rep).to_string().parse::<usize>().unwrap()
} else {
repetitions.parse::<usize>().unwrap()
};
code = code.trim();
if repetitions == 0 {
return expansion.parse().unwrap();
}
expansion.reserve((code.len()+25)*repetitions);
for i in 0..repetitions {
writeln!(expansion, "let rep_count = {};", i).unwrap();
expansion.push_str(code);
}
expansion.parse().unwrap()
}
#[proc_macro]
pub fn as_ref_token(input: TokenStream) -> TokenStream {
let input = input.to_string();
let inputt = input.trim();
let bool1 = input.contains('&');
if bool1 && !inputt.contains("&mut") {
inputt.parse().unwrap()
} else {
let index = if bool1 { inputt.rfind(' ').unwrap()+1 } else { 0 };
format!("&{}", &inputt[index..]).parse().unwrap()
}
}
#[proc_macro]
pub fn as_mut_ref_token(input: TokenStream) -> TokenStream {
let input = input.to_string();
let inputt = input.trim();
let bool1 = input.contains('&');
if bool1 && inputt.contains("&mut") {
inputt.parse().unwrap()
} else {
let a = if bool1 {1} else {0};
format!("&mut {}", &inputt[a..]).parse().unwrap()
}
}
#[proc_macro]
pub fn eval_encoded(input: TokenStream) -> TokenStream {
let input = input.to_string();
(&input[1..input.len()-1]).to_string().parse().unwrap()
}