diff options
Diffstat (limited to 'src/js/mapvalues.js')
| -rw-r--r-- | src/js/mapvalues.js | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/js/mapvalues.js b/src/js/mapvalues.js new file mode 100644 index 0000000..f701ba2 --- /dev/null +++ b/src/js/mapvalues.js @@ -0,0 +1,22 @@ +// reads the values of a machine map from its memmap.h. the #define lines may use +// other names and small sums (ZFP + 2, DSLO + 1100), so each value is evaluated +// with its names substituted. shared by extract_genome.js and extract_lsp.js. +const fs = require('fs'); + +function mapValues(mapPath, names) { + const text = fs.readFileSync(mapPath, 'utf8'); + const defs = {}; + for (const m of text.matchAll(/^#define[ \t]+(\w+)[ \t]+(.+)$/gm)) defs[m[1]] = m[2].trim(); + function value(name, depth = 0) { + if (depth > 20) throw new Error('map loop at ' + name); + const expr = defs[name]; + if (expr === undefined) throw new Error(mapPath + ' has no ' + name); + const sub = expr.replace(/\b[A-Za-z_]\w*\b/g, id => '(' + value(id, depth + 1) + ')'); + return Function('return (' + sub + ');')(); + } + const out = {}; + for (const k of names) out[k] = value(k); + return out; +} + +module.exports = { mapValues }; |
