Skip to content

Commit

Permalink
fix: record variants of a subscripted variable in removeUnusedVariabl…
Browse files Browse the repository at this point in the history
…es (#65)

Fixes #64
  • Loading branch information
chrispcampbell authored Feb 2, 2021
1 parent d4bf555 commit f6d7035
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
18 changes: 18 additions & 0 deletions models/prune/prune.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ With Look1 at t1 = WITH LOOKUP ( 1, ([(0,0)-(2,2)],(0,0),(1,1),(2,2)) )
With Look2 at t1 = WITH LOOKUP ( 1, ([(0,0)-(2,2)],(0,0),(1,1),(2,2)) )
~~|

Constant Partial 1 = 1
~~|

Constant Partial 2 = 2
~~|

Initial Partial[C1] =
INITIAL( Constant Partial 1 )
~~|

Initial Partial[C2] =
INITIAL( Constant Partial 2 )
~~|

Partial[C2] =
Initial Partial[C2]
~~|

********************************************************
.Control
********************************************************~
Expand Down
4 changes: 4 additions & 0 deletions models/prune/prune_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ expect_present "__lookup1"
expect_present "_look1"
expect_present "_look1_value_at_t1"
expect_present "_with_look1_at_t1"
expect_present "_constant_partial_1"
expect_present "_constant_partial_2"
expect_present "_initial_partial"
expect_present "_partial"

# Verify that unreferenced variables do not appear in the generated C file
expect_not_present "_input_3"
Expand Down
23 changes: 12 additions & 11 deletions models/prune/prune_spec.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "prune",
"inputVars": [
"_input_1",
"_input_2"
"inputVarNames": [
"Input 1",
"Input 2"
],
"outputVars": [
"_time",
"_input_1_and_2_total",
"_simple_totals",
"_a_totals",
"_b1_totals",
"_look1_value_at_t1",
"_with_look1_at_t1"
"outputVarNames": [
"Time",
"Input 1 and 2 Total",
"Simple Totals",
"A Totals",
"B1 Totals",
"Look1 Value at t1",
"With Look1 at t1",
"Partial[C2]"
],
"externalDatfiles": [
"prune_data.dat"
Expand Down
15 changes: 11 additions & 4 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function removeUnusedVariables(spec) {
// Keep track of all variable names that are referenced somewhere. Note that we
// don't attempt to track specific "ref ids" (e.g. `_some_variable[_subscript]`)
// but instead just track generic variable names (e.g. `_some_variable`). This
// ensure that we include all subscripts for a variable, which might mean we
// ensures that we include all subscripts for a variable, which might mean we
// include some subscripts that aren't needed, but it is safer than trying to
// eliminate those and possibly omit something that is needed.
const referencedVarNames = []
Expand All @@ -259,7 +259,7 @@ function removeUnusedVariables(spec) {
}

// Add the given variable to the list of referenced variables, and do the same for
// some special things (i.e., lookups) that it might reference.a
// some special things (i.e., lookups) that it might reference.
const recordUsedVariable = v => {
// Add the variable to the list of referenced variables
recordUsedVarName(v.varName)
Expand Down Expand Up @@ -291,8 +291,15 @@ function removeUnusedVariables(spec) {
// that it references) as being "used".
const referencedRefIds = []
const recordRefsOfVariable = v => {
let refs = v.references.concat(v.initReferences)
for (const refId of refs) {
// If this variable is subscripted, we need to record all subscript variants;
// `refIdsWithName` will return those. We also need to record all variables
// that are referenced by this variable, either directly (`v.references`) or
// in an "INITIAL" expression (`v.initReferences`). It's OK if we end up with
// duplicates in this list, because we will examine each reference only once.
let refIds = refIdsWithName(v.varName)
refIds = refIds.concat(v.references)
refIds = refIds.concat(v.initReferences)
for (const refId of refIds) {
if (!referencedRefIds.includes(refId)) {
referencedRefIds.push(refId)
const refVar = varWithRefId(refId)
Expand Down

0 comments on commit f6d7035

Please sign in to comment.