@sdeverywhere/compile 0.7.0 → 0.7.1

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdeverywhere/compile",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
5
5
  "type": "module",
6
6
  "files": [
@@ -14,6 +14,8 @@ let nextTmpVarSeq = 1
14
14
  let nextLookupVarSeq = 1
15
15
  // next sequence number for generated fixed delay variable names
16
16
  let nextFixedDelayVarSeq = 1
17
+ // next sequence number for generated depreciation variable names
18
+ let nextDepreciationVarSeq = 1
17
19
  // next sequence number for generated level variable names
18
20
  let nextLevelVarSeq = 1
19
21
  // next sequence number for generated aux variable names
@@ -73,6 +75,10 @@ export let newFixedDelayVarName = () => {
73
75
  // Return a unique fixed delay variable name
74
76
  return `_fixed_delay${nextFixedDelayVarSeq++}`
75
77
  }
78
+ export let newDepreciationVarName = () => {
79
+ // Return a unique depreciation variable name
80
+ return `_depreciation${nextDepreciationVarSeq++}`
81
+ }
76
82
  export let newLevelVarName = (basename = null, levelNumber = 0) => {
77
83
  // Return a unique level variable name.
78
84
  let levelName = basename || nextLevelVarSeq++
@@ -217,6 +217,7 @@ ${postStep}
217
217
  function declSection() {
218
218
  // Emit a declaration for each variable in the model.
219
219
  let fixedDelayDecls = ''
220
+ let depreciationDecls = ''
220
221
  let decl = v => {
221
222
  // Build a C array declaration for the variable v.
222
223
  // This uses the subscript family for each dimension, which may overallocate
@@ -229,6 +230,12 @@ ${postStep}
229
230
  family => `[${sub(family).size}]`,
230
231
  families
231
232
  ).join('')};`
233
+ } else if (v.isDepreciation()) {
234
+ // Add the associated Depreciation var decl.
235
+ depreciationDecls += `\nDepreciation* ${v.depreciationVarName}${R.map(
236
+ family => `[${sub(family).size}]`,
237
+ families
238
+ ).join('')};`
232
239
  }
233
240
  return varType + v.varName + R.map(family => `[${sub(family).size}]`, families).join('')
234
241
  }
@@ -239,7 +246,7 @@ ${postStep}
239
246
  asort,
240
247
  lines
241
248
  )
242
- return decls(Model.allVars()) + fixedDelayDecls
249
+ return decls(Model.allVars()) + fixedDelayDecls + depreciationDecls
243
250
  }
244
251
  function internalVarsSection() {
245
252
  // Declare internal variables to run the model.
@@ -810,7 +810,13 @@ export default class EquationGen extends ModelReader {
810
810
  let exprs = ctx.expr()
811
811
  let fn = this.currentFunctionName()
812
812
  // Split level functions into init and eval expressions.
813
- if (fn === '_INTEG' || fn === '_SAMPLE_IF_TRUE' || fn === '_ACTIVE_INITIAL' || fn === '_DELAY_FIXED') {
813
+ if (
814
+ fn === '_INTEG' ||
815
+ fn === '_SAMPLE_IF_TRUE' ||
816
+ fn === '_ACTIVE_INITIAL' ||
817
+ fn === '_DELAY_FIXED' ||
818
+ fn === '_DEPRECIATE_STRAIGHTLINE'
819
+ ) {
814
820
  if (this.mode.startsWith('init')) {
815
821
  // Get the index of the argument holding the initial value.
816
822
  let i = 0
@@ -818,10 +824,13 @@ export default class EquationGen extends ModelReader {
818
824
  i = 1
819
825
  } else if (fn === '_SAMPLE_IF_TRUE' || fn === '_DELAY_FIXED') {
820
826
  i = 2
827
+ } else if (fn === '_DEPRECIATE_STRAIGHTLINE') {
828
+ i = 3
821
829
  }
822
830
  this.setArgIndex(i)
823
831
  exprs[i].accept(this)
824
- // For DELAY FIXED, also initialize the support struct out of band, as it is not a Vensim var.
832
+ // For DELAY FIXED and DEPRECIATE STRAIGHTLINE, also initialize the support struct
833
+ // out of band, as they are not Vensim vars.
825
834
  if (fn === '_DELAY_FIXED') {
826
835
  let fixedDelay = `${this.var.fixedDelayVarName}${this.lhsSubscriptGen(this.var.subscripts)}`
827
836
  this.emit(`;\n ${fixedDelay} = __new_fixed_delay(${fixedDelay}, `)
@@ -831,6 +840,15 @@ export default class EquationGen extends ModelReader {
831
840
  this.setArgIndex(2)
832
841
  exprs[2].accept(this)
833
842
  this.emit(')')
843
+ } else if (fn === '_DEPRECIATE_STRAIGHTLINE') {
844
+ let depreciation = `${this.var.depreciationVarName}${this.lhsSubscriptGen(this.var.subscripts)}`
845
+ this.emit(`;\n ${depreciation} = __new_depreciation(${depreciation}, `)
846
+ this.setArgIndex(1)
847
+ exprs[1].accept(this)
848
+ this.emit(', ')
849
+ this.setArgIndex(2)
850
+ exprs[3].accept(this)
851
+ this.emit(')')
834
852
  }
835
853
  } else {
836
854
  // We are in eval mode, not init mode.
@@ -844,6 +862,12 @@ export default class EquationGen extends ModelReader {
844
862
  exprs[0].accept(this)
845
863
  this.emit(', ')
846
864
  this.emit(`${this.var.fixedDelayVarName}${this.lhsSubscriptGen(this.var.subscripts)}`)
865
+ } else if (fn === '_DEPRECIATE_STRAIGHTLINE') {
866
+ // For DEPRECIATE STRAIGHTLINE, emit the first arg followed by the Depreciation support var.
867
+ this.setArgIndex(0)
868
+ exprs[0].accept(this)
869
+ this.emit(', ')
870
+ this.emit(`${this.var.depreciationVarName}${this.lhsSubscriptGen(this.var.subscripts)}`)
847
871
  } else {
848
872
  // Emit the variable LHS as the first arg at eval time, giving the current value for the level.
849
873
  this.emit(this.lhs)
@@ -16,6 +16,7 @@ import {
16
16
  newLevelVarName,
17
17
  newLookupVarName,
18
18
  newFixedDelayVarName,
19
+ newDepreciationVarName,
19
20
  cartesianProductOf
20
21
  } from '../_shared/helpers.js'
21
22
  import {
@@ -109,6 +110,10 @@ export default class EquationReader extends ModelReader {
109
110
  this.var.varType = 'data'
110
111
  } else if (fn === '_GET_DIRECT_CONSTANTS') {
111
112
  this.var.varType = 'const'
113
+ } else if (fn === '_DEPRECIATE_STRAIGHTLINE') {
114
+ this.var.hasInitValue = true
115
+ this.var.varSubtype = 'depreciation'
116
+ this.var.depreciationVarName = canonicalName(newDepreciationVarName())
112
117
  }
113
118
  super.visitCall(ctx)
114
119
  this.callStack.pop()
@@ -249,6 +254,10 @@ export default class EquationReader extends ModelReader {
249
254
  this.addReferencesToList(this.var.initReferences)
250
255
  } else if (this.argIndexForFunctionName('_DELAY_FIXED') === 2) {
251
256
  this.addReferencesToList(this.var.initReferences)
257
+ } else if (this.argIndexForFunctionName('_DEPRECIATE_STRAIGHTLINE') === 1) {
258
+ this.addReferencesToList(this.var.initReferences)
259
+ } else if (this.argIndexForFunctionName('_DEPRECIATE_STRAIGHTLINE') === 2) {
260
+ this.addReferencesToList(this.var.initReferences)
252
261
  } else if (this.argIndexForFunctionName('_ACTIVE_INITIAL') === 1) {
253
262
  this.addReferencesToList(this.var.initReferences)
254
263
  } else if (this.argIndexForFunctionName('_SAMPLE_IF_TRUE') === 2) {
@@ -45,6 +45,8 @@ export default class Variable {
45
45
  this.delayTimeVarName = ''
46
46
  // DELAY FIXED calls generate a FixedDelay support var.
47
47
  this.fixedDelayVarName = ''
48
+ // DEPRECIATE STRAIGHTLINE calls generate a Depreciation support var.
49
+ this.depreciationVarName = ''
48
50
  // Variables generated by special expansions are not included in output.
49
51
  this.includeInOutput = true
50
52
  }
@@ -99,6 +101,9 @@ export default class Variable {
99
101
  isFixedDelay() {
100
102
  return this.varSubtype === 'fixedDelay'
101
103
  }
104
+ isDepreciation() {
105
+ return this.varSubtype === 'depreciation'
106
+ }
102
107
  isInitial() {
103
108
  return this.varType === 'initial'
104
109
  }