@sdeverywhere/compile 0.7.1 → 0.7.2

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.1",
3
+ "version": "0.7.2",
4
4
  "description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
5
5
  "type": "module",
6
6
  "files": [
@@ -13,7 +13,7 @@
13
13
  "antlr4-vensim": "0.6.0",
14
14
  "bufx": "^1.0.5",
15
15
  "byline": "^5.0.0",
16
- "csv-parse": "^4.15.4",
16
+ "csv-parse": "^5.3.3",
17
17
  "js-yaml": "^3.13.1",
18
18
  "ramda": "^0.27.0",
19
19
  "split-string": "^6.0.0",
@@ -1,6 +1,6 @@
1
1
  import util from 'util'
2
2
  import B from 'bufx'
3
- import parseCsv from 'csv-parse/lib/sync.js'
3
+ import { parse as parseCsv } from 'csv-parse/sync'
4
4
  import R from 'ramda'
5
5
  import split from 'split-string'
6
6
  import XLSX from 'xlsx'
@@ -124,9 +124,29 @@ export let listConcat = (a, x, addSpaces = false) => {
124
124
  return a + (R.isEmpty(a) ? '' : `,${s}`) + x
125
125
  }
126
126
  }
127
+ // Convert a number or string into a C double constant string.
128
+ // A blank string is converted to zero, following Excel.
129
+ // A string that cannot be converted throws an exception.
127
130
  export let cdbl = x => {
128
- // Convert a number into a C double constant.
129
- let s = x.toString()
131
+ function throwError() {
132
+ throw new Error(`ERROR: cannot convert "${x}" to a number`)
133
+ }
134
+ let s = '0.0'
135
+ if (typeof x === 'number') {
136
+ s = x.toString()
137
+ } else if (typeof x === 'string') {
138
+ if (x.trim() !== '') {
139
+ let f = parseFloat(x)
140
+ if (!Number.isNaN(f)) {
141
+ s = f.toString()
142
+ } else {
143
+ throwError()
144
+ }
145
+ }
146
+ } else {
147
+ throwError()
148
+ }
149
+ // Format as a C double literal with a decimal point.
130
150
  if (!s.includes('.') && !s.toLowerCase().includes('e')) {
131
151
  s += '.0'
132
152
  }
@@ -207,8 +227,12 @@ export let readCsv = (pathname, delimiter = ',') => {
207
227
  skip_empty_lines: true,
208
228
  skip_lines_with_empty_values: true
209
229
  }
210
- let data = B.read(pathname)
211
- csv = parseCsv(data, CSV_PARSE_OPTS)
230
+ try {
231
+ let data = B.read(pathname)
232
+ csv = parseCsv(data, CSV_PARSE_OPTS)
233
+ } catch (err) {
234
+ console.error(`ERROR: readCsv ${pathname} ${err.message}`)
235
+ }
212
236
  csvData.set(pathname, csv)
213
237
  }
214
238
  return csv
@@ -383,7 +383,15 @@ export default class EquationGen extends ModelReader {
383
383
  let csvPathname = path.resolve(this.modelDirname, file)
384
384
  let data = readCsv(csvPathname, tab)
385
385
  if (data) {
386
- getCellValue = (c, r) => (data[r] != null && data[r][c] != null ? cdbl(data[r][c]) : null)
386
+ getCellValue = (c, r) => {
387
+ let value = '0.0'
388
+ try {
389
+ value = data[r] != null && data[r][c] != null ? cdbl(data[r][c]) : null
390
+ } catch (error) {
391
+ console.error(`${error.message} in ${csvPathname}`)
392
+ }
393
+ return value
394
+ }
387
395
  }
388
396
  }
389
397
  // If the data was found, convert it to a lookup.
@@ -463,7 +471,15 @@ export default class EquationGen extends ModelReader {
463
471
  let csvPathname = path.resolve(this.modelDirname, file)
464
472
  let data = readCsv(csvPathname, tab)
465
473
  if (data) {
466
- let getCellValue = (c, r) => (data[r] != null && data[r][c] != null ? cdbl(data[r][c]) : null)
474
+ let getCellValue = (c, r) => {
475
+ let value = '0.0'
476
+ try {
477
+ value = data[r] != null && data[r][c] != null ? cdbl(data[r][c]) : null
478
+ } catch (error) {
479
+ console.error(`${error.message} in ${csvPathname}`)
480
+ }
481
+ return value
482
+ }
467
483
  // Get C subscripts in text form for the LHS in normal order.
468
484
  let modelLHSReader = new ModelLHSReader()
469
485
  modelLHSReader.read(this.var.modelLHS)