AuroraForEach/forEach.js

139 lines
6.0 KiB
JavaScript
Raw Normal View History

2021-10-07 16:11:30 +00:00
const fs = require("fs")
2021-10-15 00:55:48 +00:00
const kWantMaxSupportedElements = 100
const kMaxPairedElements = 10
2021-10-07 16:11:30 +00:00
const kMacroParamLimit = 127
2021-10-05 20:14:54 +00:00
function forN(n, cb) {
for (var i = 0; i < n; i++) {
cb(i)
}
}
2021-10-07 16:28:16 +00:00
function genArray(n) {
return [...Array(n).keys()]
}
2021-10-05 20:14:54 +00:00
function formatNJoin(n, start, join) {
2021-10-07 16:28:16 +00:00
return start + genArray(n).join(join)
2021-10-05 20:14:54 +00:00
}
function formatNJoinReverseSuffix(n, start, join, suffix) {
2021-10-07 16:28:16 +00:00
return start + genArray(n).reverse().join(suffix + join) + suffix
2021-10-05 20:14:54 +00:00
}
function formatNParamPattern(n, join) {
return formatNJoin(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)
2021-10-05 20:14:54 +00:00
* Autogenerated by forEach.js; do not modify"
*/
#define AU_FE_EXPAND(exp) exp
2021-10-06 23:40:55 +00:00
#define AU_FE_ERROR(exp) ERROR. CHECK PARAMETER COUNT. DID YOU FORGET A COMMA?
2021-10-05 20:14:54 +00:00
`
2021-10-15 00:55:48 +00:00
forN(kMaxPairedElements, function(nElements) {
if (!nElements) return
2021-10-07 16:28:16 +00:00
var suffix = nElements != 1 ? "_" + (nElements): ""
2021-10-07 16:11:30 +00:00
2021-10-15 00:55:48 +00:00
var nElementsMulNCapped = Math.min(kMacroParamLimit - 3, kWantMaxSupportedElements * nElements)
2021-10-07 16:28:16 +00:00
var ignoreList = {}
forN(nElementsMulNCapped, function(i) {
2021-10-07 16:11:30 +00:00
if (i == 0) {
textBuffer += `#define AU_FE_0${suffix}(expandable) \n`
2021-10-28 20:12:39 +00:00
ignoreList[`AU_FE_0_FIRST${suffix}`] = true
ignoreList[`AU_FE_0_THAT${suffix}`] = true
2021-10-07 16:28:16 +00:00
return
}
var X = formatNParamPattern(nElements, "X")
2021-10-07 16:28:16 +00:00
var iM1 = i - 1
var iM1Translated = i - nElements
2021-10-15 00:55:48 +00:00
if (i == nElements - 1) {
// Edge case: the trailing macro does not do anything
// We could hack in an _LAST callback here, should a need for it arise
textBuffer += `#define AU_FE_${i}${suffix}(expandable)\n`
} else if ((i == 1) && (nElements != 1)) {
2021-10-15 00:55:48 +00:00
// Edge case: FOR_EACH_N(EXPANDABLE, ), where N has to be defined/has an element count of > 1, this is always illegal
// However, we just ignore empty element expansions
textBuffer += `#define AU_FE_${i}${suffix}(expandable, empty)\n`
} else if (i % nElements == 0) {
if (i == nElements) {
2021-10-15 00:55:48 +00:00
// Edge case: the last element does not accept any parameters
2021-10-28 20:12:39 +00:00
textBuffer += `#define AU_FE_${i}${suffix}(expandable, ${X}) expandable(${X})\n`
} else {
textBuffer += `#define AU_FE_${i}${suffix}(expandable, ${X}, ...) expandable(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(expandable, __VA_ARGS__))\n`
2021-10-05 20:14:54 +00:00
}
2021-10-07 16:28:16 +00:00
} else {
ignoreList[`AU_FE_${i}${suffix}`] = true
}
var I2 = i + 1
2021-10-28 20:12:39 +00:00
// Similar to above
if (i < nElements) {
ignoreList[`AU_FE_${i}_THAT${suffix}`] = true
} else if (i % nElements == 0) {
var offByOne = iM1Translated + 1
if (i == nElements) {
// Edge case: the last element does not accept any parameters
ignoreList[`AU_FE_${I2}_THAT${suffix}`] = true // ? dont question it
textBuffer += `#define AU_FE_${I2}_THAT${suffix}(expandable, ctx, ${X}) expandable(ctx, ${X})\n`
} else {
textBuffer += `#define AU_FE_${I2}_THAT${suffix}(expandable, ctx, ${X}, ...) expandable(ctx, ${X}) AU_FE_EXPAND(AU_FE_${offByOne}_THAT${suffix}(expandable, ctx, __VA_ARGS__))\n`
}
} else {
ignoreList[`AU_FE_${I2}_THAT${suffix}`] = true
}
if (i == 1) {
textBuffer += `#define AU_FE_${i}_FIRST${suffix}(first, second) \n`
} else if (i < nElements) {
2021-10-28 20:12:39 +00:00
ignoreList[`AU_FE_${i}_FIRST${suffix}`] = true
} else if ((i % (nElements) == 0)) {
if (i == nElements) {
2021-10-28 20:12:39 +00:00
ignoreList[`AU_FE_${i}_FIRST${suffix}`] = true // ? dont question it
textBuffer += `#define AU_FE_${I2}_FIRST${suffix}(first, second, ${X}) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(second))\n`
2021-10-06 23:40:55 +00:00
} else {
textBuffer += `#define AU_FE_${I2}_FIRST${suffix}(first, second, ${X}, ...) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(second, __VA_ARGS__))\n`
2021-10-07 16:28:16 +00:00
}
2021-10-15 00:55:48 +00:00
} else {
2021-10-28 20:12:39 +00:00
ignoreList[`AU_FE_${I2}_FIRST${suffix}`] = true
2021-10-05 20:14:54 +00:00
}
})
2021-10-15 00:55:48 +00:00
// Defines a macro that effectively choses a macro given an array of macros based on the number of input arguments
var getMacroParams = formatNParamPattern(nElementsMulNCapped, "_")
2021-10-07 16:11:30 +00:00
textBuffer += `#define AU_GET_MACRO${suffix}(${getMacroParams}, NAME,...) NAME\n`
2021-10-05 20:14:54 +00:00
2021-10-15 00:55:48 +00:00
// Stringify the parameter portion of the prototype
var params = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", suffix)
var params2 = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", "_FIRST" + suffix)
2021-10-28 20:12:39 +00:00
var params3 = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", "_THAT" + suffix)
2021-10-06 23:40:55 +00:00
2021-10-15 00:55:48 +00:00
// Replace missing macro paramaters with the AU_FE_ERROR macro
2021-10-28 20:12:39 +00:00
params = params .split(", ").map((str) => { return ignoreList[str] ? "AU_FE_ERROR" : str}).join(", ")
params2 = params2.split(", ").map((str) => { return ignoreList[str] ? "AU_FE_ERROR" : str}).join(", ")
params3 = params3.split(", ").map((str) => { return ignoreList[str] ? "AU_FE_ERROR" : str}).join(", ")
2021-10-06 21:37:04 +00:00
2021-10-28 20:12:39 +00:00
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__))\n`
textBuffer += `#define AU_FOR_EACH_THAT${suffix}(action, ...) AU_FE_EXPAND(AU_GET_MACRO${suffix}(_whydoweneedthis, __VA_ARGS__, ${params3})(action,__VA_ARGS__))\n`
2021-10-05 20:14:54 +00:00
textBuffer += "\n\n"
})
2021-10-15 00:55:48 +00:00
fs.writeFileSync("./Include/AuroraForEach.hpp", textBuffer)