AuroraForEach/forEach.js
2021-10-06 22:37:04 +01:00

104 lines
3.8 KiB
JavaScript

var fs = require("fs")
var n = 100
var subn = 10
function forN(n, cb) {
for (var i = 0; i < n; i++) {
cb(i)
}
}
function formatNJoin(n, start, join) {
return start + [...Array(n).keys()].join(join)
}
function formatNJoinReverse(n, start, join) {
return start + [...Array(n).keys()].reverse().join(join)
}
function formatNJoinReverseSuffix(n, start, join, suffix) {
return start + [...Array(n).keys()].reverse().join(suffix + join) + suffix
}
function formatNJoinReverse(n, start, join) {
return start + [...Array(n).keys()].reverse().join(join)
}
function formatNParamPattern(n, join) {
return formatNJoin(n, join, ", " + join)
}
function formatNParamPatternReverse(n, join) {
return formatNJoinReverse(n, join, ", " + join)
}
function formatNParamPatternReverseSuffix(n, join, suffix) {
return formatNJoinReverseSuffix(n, join, ", " + join, suffix)
}
var textBuffer = ""
textBuffer +=
`/**
* X-Macro alternative. Defines AU_FOR_EACH_N(expandable, ...args*N)
* Autogenerated by forEach.js; do not modify"
*/
#define AU_FE_EXPAND(exp) exp
`
forN(subn, function(subN) {
if (!subN) return
var suffix = subN != 1 ? "_" + (subN): ""
var suffix2 = subN != 1 ? "_" + (subN): ""
var re = Math.min(127 - 3, n * subN)
forN(re, function(i) {
if (i == 0) textBuffer += `#define AU_FE_0${suffix}(prefix) \n`
else {
var X = formatNParamPattern(subN, "X")
var iM1 = i - 1
var iM1Translated = i - subN
var iM1Translated2 = (i - 1) - subN
if (subN == 1) {
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}, ...) prefix(${X}) AU_FE_EXPAND(AU_FE_${iM1}${suffix}(prefix, __VA_ARGS__))\n`
textBuffer += `#define AU_FE_${i}_FIRST${suffix}(first, prefix, ${X}, ...) first(${X}) AU_FE_EXPAND(AU_FE_${iM1}${suffix}(prefix, __VA_ARGS__))\n`
} else if (i % subN == 0) {
if (i == subN) {
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}) prefix(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(prefix))\n`
} else {
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}, ...) prefix(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(prefix, __VA_ARGS__))\n`
}
} else {
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}, ...) Macro Jump Error\n`
}
if (subN != 1) {
if ((i + 1) % (subN) == 0) {
if ((i + 1) == subN) {
textBuffer += `#define AU_FE_${i}_FIRST${suffix2}(first, prefix, ${X}) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated2}${suffix}(prefix))\n`
} else {
textBuffer += `#define AU_FE_${i}_FIRST${suffix2}(first, prefix, ${X}, ...) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated2}${suffix}(prefix, __VA_ARGS__))\n`
}
}
}
}
})
var GET_MACRO_PARAMS = formatNParamPattern(re, "_")
textBuffer += `#define AU_GET_MACRO${suffix}(${GET_MACRO_PARAMS}, NAME,...) NAME\n`
var params = formatNParamPatternReverseSuffix(re, "AU_FE_", suffix)
var params2 = formatNParamPatternReverseSuffix(re, "AU_FE_", "_FIRST" + suffix)
textBuffer += `#define AU_FOR_EACH${suffix}(action, ...) AU_FE_EXPAND(AU_GET_MACRO${suffix}(_whydoweneedthis, __VA_ARGS__, ${params})(action,__VA_ARGS__))\n`
textBuffer += `#define AU_FOR_EACH_FIRST${suffix}(action, ...) AU_FE_EXPAND(AU_GET_MACRO${suffix}(_whydoweneedthis, __VA_ARGS__, ${params2})(action,__VA_ARGS__))`
textBuffer += "\n\n"
})
fs.writeFileSync("./Include/AuroraForEach.hpp", textBuffer)