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
266
267
268
269
270
271
272
273
274
275
276
277
278
extern crate proc_macro;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate lazy_static;
use proc_macro::TokenStream;
use proc_macro2::TokenNode;
use syn::{Data, Type, Fields, Expr};
use std::sync::{Mutex, RwLock};
use std::io::Write;
use std::fs::File;
mod filewriter;
use filewriter::*;
lazy_static! {
static ref EXPORT_NAME: RwLock<String> = {
RwLock::new("TranslateOutput".to_string())
};
static ref CLOSED_FLAG: Mutex<bool> = {
Mutex::new(false)
};
static ref CPPFILE: Mutex<File> = {
let en = EXPORT_NAME.read().unwrap();
let mut file = File::create(format!("./target/TranslateOutput/{}.h", *en)).unwrap();
write!(file, "#include <stdbool.h>\n#include <cstdint>\n\n").unwrap();
write!(file, "#ifndef {}_H\n", *en).unwrap();
write!(file, "#define {}_H\n\n", *en).unwrap();
Mutex::new(file)
};
static ref CSFILE: Mutex<File> = {
let en = EXPORT_NAME.read().unwrap();
let mut file = File::create(format!("./target/TranslateOutput/{}.cs", *en)).unwrap();
write!(file, "using System;\n").unwrap();
write!(file, "using System.Linq;\n").unwrap();
write!(file, "using System.Runtime.InteropServices;\n\n").unwrap();
write!(file, "namespace {}\n{{\n", *en).unwrap();
Mutex::new(file)
};
static ref PYFILE: Mutex<File> = {
let en = EXPORT_NAME.read().unwrap();
let file = File::create(format!("./target/TranslateOutput/{}.py", *en)).unwrap();
Mutex::new(file)
};
}
#[proc_macro_derive(Translate)]
pub fn translate(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_translate(ast);
let empty_tokens = quote!{
};
empty_tokens.parse().unwrap()
}
fn get_array_len(arr: syn::TypeArray) -> Option<u64> {
match arr.len {
Expr::Lit(l) => {
match l.lit {
syn::Lit::Int(int) => {
Some(int.value())
}
_ => {
None
}
}
},
_ => None
}
}
fn check_magic_struct(id: &syn::Ident) -> bool {
if id.to_string() == "__FinalizeTranslatorStruct__" {
let mut cppfile = CPPFILE.lock().unwrap();
let mut pyfile = PYFILE.lock().unwrap();
let mut csfile = CSFILE.lock().unwrap();
close_file(&mut *cppfile, LanguageType::CPP);
close_file(&mut *pyfile, LanguageType::Python);
close_file(&mut *csfile, LanguageType::CSharp);
let mut cf = CLOSED_FLAG.lock().unwrap();
*cf = true;
return true;
}
return false;
}
fn check_finalized() {
let cf = CLOSED_FLAG.lock().unwrap();
if *cf {
panic!("translator files are closed, please make sure that __FinalizeTranslatorStruct__ is the last struct translated");
}
}
fn impl_translate(ast: syn::DeriveInput) {
check_finalized();
let mut is_reprc: bool = false;
for attr in ast.attrs {
for token in attr.tts.into_iter() {
match token.kind {
TokenNode::Group(_, ts) => {
for t in ts.into_iter() {
match t.kind {
TokenNode::Term(t) => {
is_reprc = t.as_str() == "C";
},
_ => {}
}
}
}
_ => {}
};
}
}
if check_magic_struct(&ast.ident) || !is_reprc {
return;
}
if let Data::Struct(ds) = ast.data {
let _ = create_directory();
let mut cppfile = CPPFILE.lock().unwrap();
let mut pyfile = PYFILE.lock().unwrap();
let mut csfile = CSFILE.lock().unwrap();
start_struct(&mut *cppfile, LanguageType::CPP, ast.ident);
start_struct(&mut *pyfile, LanguageType::Python, ast.ident);
start_struct(&mut *csfile, LanguageType::CSharp, ast.ident);
if let Fields::Named(fieldsnamed) = ds.fields {
for field in fieldsnamed.named {
match field.ty {
Type::Array(array) => {
if let Some(len) = get_array_len(array.clone()) {
match *array.elem {
Type::Array(_a) => {
panic!("2+D Arrays not currently implemented")
},
Type::Ptr(_p) => {
panic!("Pointer Arrays not currently implemented")
},
Type::Path(p) => {
add_array(&mut cppfile, LanguageType::CPP,
field.ident.unwrap(), len,
p.path.segments.iter().last().unwrap().ident);
add_array(&mut csfile, LanguageType::CSharp,
field.ident.unwrap(), len,
p.path.segments.iter().last().unwrap().ident);
add_array(&mut pyfile, LanguageType::Python,
field.ident.unwrap(), len,
p.path.segments.iter().last().unwrap().ident);
},
_ => {}
}
}
},
Type::Ptr(ptr) => {
match *ptr.elem {
Type::Array(_array) => {
panic!("pointer to an array not currently implemented");
},
Type::Ptr(_ptr) => {
panic!("double+ pointers not currently implemented");
},
Type::Path(p) => {
add_pointer(&mut cppfile, LanguageType::CPP, field.ident.unwrap(), p.path.segments.iter().last().unwrap().ident);
add_pointer(&mut pyfile, LanguageType::Python, field.ident.unwrap(), p.path.segments.iter().last().unwrap().ident);
add_pointer(&mut csfile, LanguageType::CSharp, field.ident.unwrap(), p.path.segments.iter().last().unwrap().ident);
},
_ => {
panic!("pointer type not implemented");
}
};
},
Type::Path(typepath)=> {
let segment = typepath.path.segments.iter().last().unwrap();
add_simple_type(&mut cppfile, LanguageType::CPP, field.ident.unwrap(), segment.ident);
add_simple_type(&mut csfile, LanguageType::CSharp, field.ident.unwrap(), segment.ident);
add_simple_type(&mut pyfile, LanguageType::Python, field.ident.unwrap(), segment.ident);
},
_ => {println!("{} is an unsupported type", field.ident.unwrap())}
}
}
}
close_struct(&mut *cppfile, LanguageType::CPP, ast.ident);
close_struct(&mut *pyfile, LanguageType::Python, ast.ident);
close_struct(&mut *csfile, LanguageType::CSharp, ast.ident);
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}