@sdeverywhere/compile 0.7.12 → 0.7.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/model/model.js +18 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdeverywhere/compile",
3
- "version": "0.7.12",
3
+ "version": "0.7.13",
4
4
  "description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -395,14 +395,12 @@ function removeUnusedVariables(spec) {
395
395
  // ensures that we include all subscripts for a variable, which might mean we
396
396
  // include some subscripts that aren't needed, but it is safer than trying to
397
397
  // eliminate those and possibly omit something that is needed.
398
- const referencedVarNames = []
398
+ const referencedVarNames = new Set()
399
399
 
400
400
  // Add the given variable name to the list of referenced variables, if it's not
401
401
  // already there.
402
402
  const recordUsedVarName = varName => {
403
- if (!referencedVarNames.includes(varName)) {
404
- referencedVarNames.push(varName)
405
- }
403
+ referencedVarNames.add(varName)
406
404
  }
407
405
 
408
406
  // Add the given variable to the list of referenced variables, and do the same for
@@ -443,16 +441,21 @@ function removeUnusedVariables(spec) {
443
441
  // that are referenced by this variable, either directly (`v.references`) or
444
442
  // in an "INITIAL" expression (`v.initReferences`). It's OK if we end up with
445
443
  // duplicates in this list, because we will examine each reference only once.
446
- let refIds = refIdsWithName(v.varName)
447
- refIds = refIds.concat(v.references)
448
- refIds = refIds.concat(v.initReferences)
449
- for (const refId of refIds) {
444
+ let refStack = []
445
+ function pushRefs(v) {
446
+ refStack.push(...refIdsWithName(v.varName))
447
+ refStack.push(...v.references)
448
+ refStack.push(...v.initReferences)
449
+ }
450
+ pushRefs(v)
451
+ while (refStack.length > 0) {
452
+ const refId = refStack.pop()
450
453
  if (!referencedRefIds.has(refId)) {
451
454
  referencedRefIds.add(refId)
452
455
  const refVar = varWithRefId(refId)
453
456
  if (refVar) {
454
457
  recordUsedVariable(refVar)
455
- recordRefsOfVariable(refVar)
458
+ pushRefs(refVar)
456
459
  } else {
457
460
  throw new Error(`No var found for ${refId} when recording references for ${v.varName}`)
458
461
  }
@@ -468,7 +471,11 @@ function removeUnusedVariables(spec) {
468
471
 
469
472
  // Keep all input variables
470
473
  for (const inputVarName of spec.inputVars) {
471
- for (const v of varsWithName(inputVarName)) {
474
+ // The inputVars can include a raw index, e.g. `_input_var[0]`,
475
+ // which isn't an actual "ref id", so we'll just derive the
476
+ // var name by chopping off the index part.
477
+ const inputVarBaseName = inputVarName.split('[')[0]
478
+ for (const v of varsWithName(inputVarBaseName)) {
472
479
  recordUsedVariable(v)
473
480
  }
474
481
  }
@@ -486,7 +493,7 @@ function removeUnusedVariables(spec) {
486
493
  }
487
494
 
488
495
  // Filter out unneeded variables so we're left with the minimal set of variables to emit
489
- variables = R.filter(v => referencedVarNames.includes(v.varName), variables)
496
+ variables = R.filter(v => referencedVarNames.has(v.varName), variables)
490
497
 
491
498
  // Rebuild the variables-by-name map
492
499
  variablesByName.clear()