@sdeverywhere/compile 0.7.11 → 0.7.12

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.11",
3
+ "version": "0.7.12",
4
4
  "description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -375,7 +375,3 @@ export let vlog = (title, value, depth = 1) => {
375
375
  console.trace()
376
376
  }
377
377
  }
378
- export let abend = error => {
379
- console.error(error)
380
- process.exit(1)
381
- }
@@ -35,8 +35,16 @@ export async function readDat(pathname, prefix = '') {
35
35
  }
36
36
  }
37
37
 
38
- return new Promise(resolve => {
39
- let stream = byline(fs.createReadStream(pathname, 'utf8'))
38
+ return new Promise((resolve, reject) => {
39
+ // Errors from the read stream aren't propagated by the byline package
40
+ // so we attach the error handler to `readStream` rather than to `stream`
41
+ let readStream = fs.createReadStream(pathname, 'utf8')
42
+ let stream = byline(readStream)
43
+ readStream.on('error', e => {
44
+ stream.destroy()
45
+ reject(new Error(`Failed to read dat file: ${e.message}`))
46
+ })
47
+
40
48
  stream.on('data', line => {
41
49
  let values = splitDatLine(line)
42
50
  if (values.length === 1) {
@@ -63,6 +71,7 @@ export async function readDat(pathname, prefix = '') {
63
71
  lineNum++
64
72
  // if (lineNum % 1e5 === 0) console.log(num(lineNum).format('0,0'))
65
73
  })
74
+
66
75
  stream.on('end', () => {
67
76
  addValues()
68
77
  resolve(log)
@@ -1,6 +1,6 @@
1
1
  import * as R from 'ramda'
2
2
 
3
- import { asort, lines, strlist, abend, mapIndexed } from '../_shared/helpers.js'
3
+ import { asort, lines, strlist, mapIndexed } from '../_shared/helpers.js'
4
4
  import { sub, allDimensions, allMappings, subscriptFamilies } from '../_shared/subscript.js'
5
5
  import Model from '../model/model.js'
6
6
 
@@ -37,30 +37,26 @@ let codeGenerator = (parsedModel, opts) => {
37
37
  function generate() {
38
38
  // Read variables and subscript ranges from the model parse tree.
39
39
  // This is the main entry point for code generation and is called just once.
40
- try {
41
- Model.read(parsedModel, spec, extData, directData, modelDirname)
42
- // In list mode, print variables to the console instead of generating code.
43
- if (operations.includes('printRefIdTest')) {
44
- Model.printRefIdTest()
45
- }
46
- if (operations.includes('printRefGraph')) {
47
- Model.printRefGraph(opts.varname)
48
- }
49
- if (operations.includes('convertNames')) {
50
- // Do not generate output, but leave the results of model analysis.
51
- }
52
- if (operations.includes('generateC')) {
53
- // Generate code for each variable in the proper order.
54
- let code = emitDeclCode()
55
- code += emitInitLookupsCode()
56
- code += emitInitConstantsCode()
57
- code += emitInitLevelsCode()
58
- code += emitEvalCode()
59
- code += emitIOCode()
60
- return code
61
- }
62
- } catch (e) {
63
- abend(e)
40
+ Model.read(parsedModel, spec, extData, directData, modelDirname)
41
+ // In list mode, print variables to the console instead of generating code.
42
+ if (operations.includes('printRefIdTest')) {
43
+ Model.printRefIdTest()
44
+ }
45
+ if (operations.includes('printRefGraph')) {
46
+ Model.printRefGraph(opts.varname)
47
+ }
48
+ if (operations.includes('convertNames')) {
49
+ // Do not generate output, but leave the results of model analysis.
50
+ }
51
+ if (operations.includes('generateC')) {
52
+ // Generate code for each variable in the proper order.
53
+ let code = emitDeclCode()
54
+ code += emitInitLookupsCode()
55
+ code += emitInitConstantsCode()
56
+ code += emitInitLevelsCode()
57
+ code += emitEvalCode()
58
+ code += emitIOCode()
59
+ return code
64
60
  }
65
61
  }
66
62
 
@@ -454,9 +454,7 @@ function removeUnusedVariables(spec) {
454
454
  recordUsedVariable(refVar)
455
455
  recordRefsOfVariable(refVar)
456
456
  } else {
457
- console.error(`No var found for ${refId}`)
458
- console.error(v)
459
- process.exit(1)
457
+ throw new Error(`No var found for ${refId} when recording references for ${v.varName}`)
460
458
  }
461
459
  }
462
460
  }
@@ -877,13 +875,7 @@ function sortVarsOfType(varType) {
877
875
  // Sort into an lhs dependency list.
878
876
  if (PRINT_AUX_GRAPH) printDepsGraph(graph, 'AUX')
879
877
  if (PRINT_LEVEL_GRAPH) printDepsGraph(graph, 'LEVEL')
880
- let deps
881
- try {
882
- deps = toposort(graph).reverse()
883
- } catch (e) {
884
- console.error(e.message)
885
- process.exit(1)
886
- }
878
+ let deps = toposort(graph).reverse()
887
879
 
888
880
  // Turn the dependency-sorted var name list into a var list.
889
881
  let sortedVars = varsOfType(
@@ -978,13 +970,7 @@ function sortInitVars() {
978
970
  if (PRINT_INIT_GRAPH) printDepsGraph(graph, 'INIT')
979
971
 
980
972
  // Sort into a reference id dependency list.
981
- let deps
982
- try {
983
- deps = toposort(graph).reverse()
984
- } catch (e) {
985
- console.error(e.message)
986
- process.exit(1)
987
- }
973
+ let deps = toposort(graph).reverse()
988
974
 
989
975
  // Turn the reference id list into a var list.
990
976
  let sortedVars = R.map(refId => varWithRefId(refId), deps)
@@ -42,7 +42,7 @@ function toposort(nodes, edges) {
42
42
  } catch (e) {
43
43
  nodeRep = ''
44
44
  }
45
- throw new Error('toposort cyclic dependency:\n' + [...predecessors].join(' →\n') + nodeRep)
45
+ throw new Error('Found cyclic dependency during toposort:\n' + [...predecessors].join(' →\n') + nodeRep)
46
46
  }
47
47
 
48
48
  if (!nodesHash.has(node)) {