AuroraForEach/forEach.js

86 lines
2.6 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_FE_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): ""
forN(n, 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
if (subN == 1) {
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}, ...) prefix(${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`
}
}
})
var GET_MACRO_PARAMS = formatNParamPattern(n, "_")
textBuffer += `#define AU_GET_MACRO${suffix}(${GET_MACRO_PARAMS}, NAME,...) NAME\n`
var params = formatNParamPatternReverseSuffix(n, "AU_FE_", suffix)
var finalLine = `#define AU_FOR_EACH${suffix}(action, ...) AU_FE_EXPAND(AU_GET_MACRO${suffix}(_whydoweneedthis, __VA_ARGS__, ${params})(action,__VA_ARGS__))`
textBuffer += finalLine
textBuffer += "\n\n"
})
fs.writeFileSync("./AuroraForEach.hpp", textBuffer)