-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (35 loc) · 1.21 KB
/
index.js
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
const fs = require("fs");
let fileLoc = process.argv[2];//the first argument after 'node index.js'
fs.readFileSync(fileLoc).toString().split('\n').forEach(function (line) {
if(line.indexOf("(") >= 0)
console.log(handleLine(line));
});
function handleLine(line){
line = changeType(line,"const ", "");
let returnType = line.split(" ")[0];
let name = line.split("(")[0].split(" ")[1];
let args = line.split("(")[1].split(")")[0].split(",");
line = "declare function " + name + "(";
for(i = 0; i < args.length; i++){
let a = args[i].trim().split(" ");
line += a[1] + ": " + changeTypes(a[0]) + ", ";
}
if(args.length > 0)
line = line.substring(0, line.length - 2);
line += "): " + changeTypes(returnType);
line += ";";
return line;
}
function changeTypes(line){
line = changeType(line, "uint64_t\\*", "usize");//pointer
line = changeType(line, "uint32_t\\*", "usize");
line = changeType(line, "uint32_t", "u32");
line = changeType(line, "uint64_t", "u64");
line = changeType(line, "int32_t", "i32");
line = changeType(line, "void\\*", "usize");
line = changeType(line, "const", "");
return line;
}
function changeType(line, original, target){
return line.replace(new RegExp(original, 'g'), target);
}