AuroraForEach/forEach.js

113 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-10-05 20:14:54 +00:00
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)
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
`
forN(subn, function(subN) {
if (!subN) return
var suffix = subN != 1 ? "_" + (subN): ""
2021-10-06 21:37:04 +00:00
var suffix2 = subN != 1 ? "_" + (subN): ""
2021-10-06 20:43:22 +00:00
var re = Math.min(127 - 3, n * subN)
2021-10-06 23:40:55 +00:00
var ignoreList = {}
var ignoreList2 = {}
forN(re, function(i) {
2021-10-05 20:14:54 +00:00
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
2021-10-06 21:37:04 +00:00
var iM1Translated2 = (i - 1) - subN
if (subN == 1) {
2021-10-06 21:37:04 +00:00
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) {
2021-10-06 21:37:04 +00:00
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}) prefix(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(prefix))\n`
} else {
2021-10-06 21:37:04 +00:00
textBuffer += `#define AU_FE_${i}${suffix}(prefix, ${X}, ...) prefix(${X}) AU_FE_EXPAND(AU_FE_${iM1Translated}${suffix}(prefix, __VA_ARGS__))\n`
2021-10-05 20:14:54 +00:00
}
2021-10-06 21:37:04 +00:00
} else {
2021-10-06 23:40:55 +00:00
ignoreList[`AU_FE_${i}${suffix}`] = true
2021-10-05 20:14:54 +00:00
}
2021-10-06 21:37:04 +00:00
var I2 = i + 1
if (i < subN) {
textBuffer += `#define AU_FE_${i}_FIRST${suffix2}(first, prefix, ${X}) \n`
} else if (I2 % (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`
}
2021-10-06 23:40:55 +00:00
} else {
ignoreList2[`AU_FE_${i}_FIRST${suffix2}`] = true
}
2021-10-05 20:14:54 +00:00
}
})
var GET_MACRO_PARAMS = formatNParamPattern(re, "_")
2021-10-05 20:14:54 +00:00
textBuffer += `#define AU_GET_MACRO${suffix}(${GET_MACRO_PARAMS}, NAME,...) NAME\n`
var params = formatNParamPatternReverseSuffix(re, "AU_FE_", suffix)
2021-10-06 21:37:04 +00:00
var params2 = formatNParamPatternReverseSuffix(re, "AU_FE_", "_FIRST" + suffix)
2021-10-06 23:40:55 +00:00
params = params.split(", ").map((str) => { return ignoreList[str] ? "AU_FE_ERROR" : str}).join(", ")
params2 = params2.split(", ").map((str) => { return ignoreList2[str] ? "AU_FE_ERROR" : str}).join(", ")
2021-10-06 21:37:04 +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__))`
2021-10-05 20:14:54 +00:00
textBuffer += "\n\n"
})
fs.writeFileSync("./Include/AuroraForEach.hpp", textBuffer)
2021-10-05 20:14:54 +00:00