-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateStd.js
63 lines (47 loc) · 1.61 KB
/
generateStd.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require("fs");
const path = require("path");
console.log();
console.log("Generating STD HashTable");
const content = fs.readFileSync(path.resolve(__dirname, "core", "src", "RedeStd.c"));
/**
*
* @param {String} str
* @returns {BigInt}
*/
const hash = (str) => {
let hash = BigInt(5381);
for(let ch of str) {
hash = ((hash << BigInt(5)) + hash) + BigInt(ch.charCodeAt(0)); /* hash * 33 + c */
}
return hash;
}
const functions = Array.from(content.toString().matchAll(/int Rede_std_(\w+)\(const/g)).map(item => item[1]);
let size = BigInt(functions.length);
const indexToName = new Map();
while(true) {
let success = true;
for(let funcName of functions) {
const funcIndex = hash(funcName) % size;
if(indexToName.has(funcIndex)) {
indexToName.clear();
size++;
success = false;
break;
}
indexToName.set(funcIndex, funcName);
}
if(success) break;
}
console.log(`HashTable size: ${size}`);
console.log(`Indexes:`);
console.log(indexToName);
console.log();
const tableItems = Array.from(indexToName.entries()).map(([index, name]) => ({ name, index }));
const result = `#include "RedeStdTable.h"
${tableItems.map(item => `int Rede_std_${item.name}(const RedeFunctionArgs* args, RedeVariable* result);`).join("\n")}
size_t Rede_std_functions_size = ${size};
RedeStdFunction Rede_std_functions[${size}] = {
${tableItems.map(item => `[${item.index}] = { "${item.name}", Rede_std_${item.name} }`).join(",\n ")}
};
`
fs.writeFileSync(path.resolve(__dirname, "./core/RedeStdTable.gen.c"), result);