@sdeverywhere/compile 0.7.12 → 0.7.14
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.
package/package.json
CHANGED
|
@@ -43,7 +43,7 @@ export function expandVarNames(canonical) {
|
|
|
43
43
|
* @returns {string[]} An array of expanded names for the given variable.
|
|
44
44
|
*/
|
|
45
45
|
function namesForVar(v) {
|
|
46
|
-
if (process.env.SDE_NONPUBLIC_USE_NEW_PARSE
|
|
46
|
+
if (process.env.SDE_NONPUBLIC_USE_NEW_PARSE === '0') {
|
|
47
47
|
// TODO: When the old parsing code is active, use the old ModelLHSReader. This code path
|
|
48
48
|
// will be removed when the old parsing code is removed.
|
|
49
49
|
let modelLHSReader = new ModelLHSReader()
|
package/src/model/model.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
496
|
+
variables = R.filter(v => referencedVarNames.has(v.varName), variables)
|
|
490
497
|
|
|
491
498
|
// Rebuild the variables-by-name map
|
|
492
499
|
variablesByName.clear()
|
|
@@ -793,7 +800,7 @@ function vensimName(cVarName) {
|
|
|
793
800
|
function cName(vensimVarName) {
|
|
794
801
|
// Convert a Vensim variable name to a C name.
|
|
795
802
|
// This function requires model analysis to be completed first when the variable has subscripts.
|
|
796
|
-
if (process.env.SDE_NONPUBLIC_USE_NEW_PARSE
|
|
803
|
+
if (process.env.SDE_NONPUBLIC_USE_NEW_PARSE === '0') {
|
|
797
804
|
// TODO: For now we use the legacy VarNameReader when the old parser is active; this
|
|
798
805
|
// code will be removed once the old parser is removed
|
|
799
806
|
return new VarNameReader().read(vensimVarName)
|
|
@@ -131,7 +131,7 @@ export function printNames(namesPathname, operation) {
|
|
|
131
131
|
* @return {*} A parsed tree representation of the model.
|
|
132
132
|
*/
|
|
133
133
|
export function parseModel(input, modelDir, sort = false) {
|
|
134
|
-
if (process.env.SDE_NONPUBLIC_USE_NEW_PARSE
|
|
134
|
+
if (process.env.SDE_NONPUBLIC_USE_NEW_PARSE === '0') {
|
|
135
135
|
// Use the legacy parser
|
|
136
136
|
return {
|
|
137
137
|
kind: 'vensim-legacy',
|