@sdeverywhere/compile 0.7.2 → 0.7.3

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.2",
3
+ "version": "0.7.3",
4
4
  "description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
5
5
  "type": "module",
6
6
  "files": [
@@ -225,7 +225,7 @@ export let readCsv = (pathname, delimiter = ',') => {
225
225
  columns: false,
226
226
  trim: true,
227
227
  skip_empty_lines: true,
228
- skip_lines_with_empty_values: true
228
+ skip_records_with_empty_values: true
229
229
  }
230
230
  try {
231
231
  let data = B.read(pathname)
@@ -42,6 +42,7 @@ let codeGenerator = (parseTree, opts) => {
42
42
  } else if (operation === 'generateC') {
43
43
  // Generate code for each variable in the proper order.
44
44
  let code = emitDeclCode()
45
+ code += emitGetControlValuesCode()
45
46
  code += emitInitLookupsCode()
46
47
  code += emitInitConstantsCode()
47
48
  code += emitInitLevelsCode()
@@ -79,6 +80,55 @@ ${dimensionMappingsSection()}
79
80
  ${section(Model.lookupVars())}
80
81
  ${section(Model.dataVars())}
81
82
 
83
+ `
84
+ }
85
+
86
+ //
87
+ // Control value getters section
88
+ //
89
+ function emitGetControlValuesCode() {
90
+ function getConstTimeValue(vensimName) {
91
+ const v = Model.varWithName(Model.cName(vensimName))
92
+ if (v && v.varType === 'const') {
93
+ return v.modelFormula
94
+ } else {
95
+ throw new Error(`SDE only supports ${vensimName} defined as a constant value`)
96
+ }
97
+ }
98
+
99
+ function getSaveperValue() {
100
+ const v = Model.varWithName('_saveper')
101
+ if (v && v.varType === 'const') {
102
+ return v.modelFormula
103
+ } else if (v && v.varType === 'aux' && v.modelFormula === 'TIME STEP') {
104
+ return getConstTimeValue('TIME STEP')
105
+ } else {
106
+ throw new Error(`SDE only supports SAVEPER defined as TIME STEP or a constant value`)
107
+ }
108
+ }
109
+
110
+ // For now we only allow models that have:
111
+ // INITIAL TIME = a constant value
112
+ // FINAL TIME = a constant value
113
+ // SAVEPER = a constant value or constant `TIME STEP`
114
+ // If the model does not meet these expectations, we throw an error.
115
+ // TODO: Loosen this up to allow for certain common constant expressions
116
+ const initialTimeValue = getConstTimeValue('INITIAL TIME')
117
+ const finalTimeValue = getConstTimeValue('FINAL TIME')
118
+ const saveperValue = getSaveperValue('SAVEPER')
119
+
120
+ return `
121
+ // Control parameter accessors
122
+ double getInitialTime() {
123
+ return ${initialTimeValue};
124
+ }
125
+ double getFinalTime() {
126
+ return ${finalTimeValue};
127
+ }
128
+ double getSaveper() {
129
+ return ${saveperValue};
130
+ }
131
+
82
132
  `
83
133
  }
84
134