Clean up forEach.js

This commit is contained in:
Reece Wilson 2021-10-15 01:55:48 +01:00
parent 6bd871e404
commit 849db18d4e

View File

@ -1,6 +1,6 @@
const fs = require("fs") const fs = require("fs")
const n = 100 const kWantMaxSupportedElements = 100
const subn = 10 const kMaxPairedElements = 10
const kMacroParamLimit = 127 const kMacroParamLimit = 127
function forN(n, cb) { function forN(n, cb) {
@ -17,27 +17,14 @@ function formatNJoin(n, start, join) {
return start + genArray(n).join(join) return start + genArray(n).join(join)
} }
function formatNJoinReverse(n, start, join) {
return start + genArray(n).reverse().join(join)
}
function formatNJoinReverseSuffix(n, start, join, suffix) { function formatNJoinReverseSuffix(n, start, join, suffix) {
return start + genArray(n).reverse().join(suffix + join) + suffix return start + genArray(n).reverse().join(suffix + join) + suffix
} }
function formatNJoinReverse(n, start, join) {
return start + genArray(n).reverse().join(join)
}
function formatNParamPattern(n, join) { function formatNParamPattern(n, join) {
return formatNJoin(n, join, ", " + join) return formatNJoin(n, join, ", " + join)
} }
function formatNParamPatternReverse(n, join) {
return formatNJoinReverse(n, join, ", " + join)
}
function formatNParamPatternReverseSuffix(n, join, suffix) { function formatNParamPatternReverseSuffix(n, join, suffix) {
return formatNJoinReverseSuffix(n, join, ", " + join, suffix) return formatNJoinReverseSuffix(n, join, ", " + join, suffix)
} }
@ -54,12 +41,12 @@ textBuffer +=
#define AU_FE_ERROR(exp) ERROR. CHECK PARAMETER COUNT. DID YOU FORGET A COMMA? #define AU_FE_ERROR(exp) ERROR. CHECK PARAMETER COUNT. DID YOU FORGET A COMMA?
` `
forN(subn, function(nElements) { forN(kMaxPairedElements, function(nElements) {
if (!nElements) return if (!nElements) return
var suffix = nElements != 1 ? "_" + (nElements): "" var suffix = nElements != 1 ? "_" + (nElements): ""
var nElementsMulNCapped = Math.min(kMacroParamLimit - 3, n * nElements) var nElementsMulNCapped = Math.min(kMacroParamLimit - 3, kWantMaxSupportedElements * nElements)
var ignoreList = {} var ignoreList = {}
var ignoreListFirst = {} var ignoreListFirst = {}
@ -75,23 +62,26 @@ forN(subn, function(nElements) {
var iM1 = i - 1 var iM1 = i - 1
var iM1Translated = i - nElements var iM1Translated = i - nElements
if (i == nElements - 1) { 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` textBuffer += `#define AU_FE_${i}${suffix}(expandable)\n`
} else if ((i == 1) && (nElements != 1)) { } else if ((i == 1) && (nElements != 1)) {
// Edge case: FOR_EACH_N(EXPANDABLE, ), where N has to be defined/has an element count of > 1, this is always illegal // 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 // However, we just ignore empty element expansions
textBuffer += `#define AU_FE_${i}${suffix}(expandable, empty)\n` textBuffer += `#define AU_FE_${i}${suffix}(expandable, empty)\n`
} else if (i % nElements == 0) { } else if (i % nElements == 0) {
if (i == nElements) { if (i == nElements) {
// Edge case: the last element does not accept any parameters
textBuffer += `#define AU_FE_${i}${suffix}(expandable, ${X}) expandable(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(expandable))\n` textBuffer += `#define AU_FE_${i}${suffix}(expandable, ${X}) expandable(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(expandable))\n`
} else { } else {
textBuffer += `#define AU_FE_${i}${suffix}(expandable, ${X}, ...) expandable(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(expandable, __VA_ARGS__))\n` textBuffer += `#define AU_FE_${i}${suffix}(expandable, ${X}, ...) expandable(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(expandable, __VA_ARGS__))\n`
} }
} else { } else {
ignoreList[`AU_FE_${i}${suffix}`] = true ignoreList[`AU_FE_${i}${suffix}`] = true
} }
// Similar to above
var I2 = i + 1 var I2 = i + 1
if (i == 1) { if (i == 1) {
textBuffer += `#define AU_FE_${i}_FIRST${suffix}(first, second) \n` textBuffer += `#define AU_FE_${i}_FIRST${suffix}(first, second) \n`
@ -99,22 +89,25 @@ forN(subn, function(nElements) {
ignoreListFirst[`AU_FE_${i}_FIRST${suffix}`] = true ignoreListFirst[`AU_FE_${i}_FIRST${suffix}`] = true
} else if ((i % (nElements) == 0)) { } else if ((i % (nElements) == 0)) {
if (i == nElements) { if (i == nElements) {
ignoreListFirst[`AU_FE_${i}_FIRST${suffix}`] = true ignoreListFirst[`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` textBuffer += `#define AU_FE_${I2}_FIRST${suffix}(first, second, ${X}) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(second))\n`
} else { } else {
textBuffer += `#define AU_FE_${I2}_FIRST${suffix}(first, second, ${X}, ...) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(second, __VA_ARGS__))\n` textBuffer += `#define AU_FE_${I2}_FIRST${suffix}(first, second, ${X}, ...) first(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(second, __VA_ARGS__))\n`
} }
} else { } else {
ignoreListFirst[`AU_FE_${I2}_FIRST${suffix}`] = true ignoreListFirst[`AU_FE_${I2}_FIRST${suffix}`] = true
} }
}) })
// Defines a macro that effectively choses a macro given an array of macros based on the number of input arguments
var getMacroParams = formatNParamPattern(nElementsMulNCapped, "_") var getMacroParams = formatNParamPattern(nElementsMulNCapped, "_")
textBuffer += `#define AU_GET_MACRO${suffix}(${getMacroParams}, NAME,...) NAME\n` textBuffer += `#define AU_GET_MACRO${suffix}(${getMacroParams}, NAME,...) NAME\n`
// Stringify the parameter portion of the prototype
var params = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", suffix) var params = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", suffix)
var params2 = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", "_FIRST" + suffix) var params2 = formatNParamPatternReverseSuffix(nElementsMulNCapped, "AU_FE_", "_FIRST" + suffix)
// Replace missing macro paramaters with the AU_FE_ERROR macro
params = params.split(", ").map((str) => { return ignoreList[str] ? "AU_FE_ERROR" : str}).join(", ") params = params.split(", ").map((str) => { return ignoreList[str] ? "AU_FE_ERROR" : str}).join(", ")
params2 = params2.split(", ").map((str) => { return ignoreListFirst[str] ? "AU_FE_ERROR" : str}).join(", ") params2 = params2.split(", ").map((str) => { return ignoreListFirst[str] ? "AU_FE_ERROR" : str}).join(", ")
@ -124,5 +117,4 @@ forN(subn, function(nElements) {
textBuffer += "\n\n" textBuffer += "\n\n"
}) })
fs.writeFileSync("./Include/AuroraForEach.hpp", textBuffer) fs.writeFileSync("./Include/AuroraForEach.hpp", textBuffer)