diff --git a/generator/generator.js b/generator/generator.js index 16cf464..e6447b6 100644 --- a/generator/generator.js +++ b/generator/generator.js @@ -106,7 +106,16 @@ const addResource = (map, fileName, generator) => { // No; add to the map const suffix = fileName.trim().replace(/\W+/g, '_'); - const variable = /^[^A-Za-z_]/.test(suffix) ? '_' + suffix : suffix; + const variableCandidate = /^[^A-Za-z_]/.test(suffix) ? '_' + suffix : suffix; + + // Check if the variable name is duplicated + // TODO: Cache the existing variables to speed up the verification + let variable = variableCandidate; + let variableSuffix = 1; + const existingVariables = Object.values(map).map(o => o.variable); + while (existingVariables.includes(variable)) { + variable = variableCandidate + '_' + variableSuffix++; + } map[fileName] = { variable, diff --git a/package.json b/package.json index 2f8ca1e..06159a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "choice4genesis", - "version": "0.14.1", + "version": "0.14.2", "description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ", "main": "index.js", "targets": {