@sdeverywhere/compile 0.7.25 → 0.7.26
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 +2 -3
- package/src/_shared/bufx.js +66 -0
- package/src/_shared/helpers.js +1 -1
- package/src/_shared/read-dat.js +1 -1
- package/src/_shared/subscript.js +1 -17
- package/src/model/model.js +2 -12
- package/src/parse-and-generate.js +3 -7
package/package.json
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeverywhere/compile",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.26",
|
|
4
4
|
"description": "The core Vensim to C compiler for the SDEverywhere tool suite.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@sdeverywhere/parse": "^0.1.2",
|
|
9
|
-
"bufx": "^1.0.5",
|
|
10
9
|
"byline": "^5.0.0",
|
|
11
10
|
"csv-parse": "^5.3.3",
|
|
12
|
-
"js-yaml": "^3.13.1",
|
|
13
11
|
"ramda": "^0.27.0",
|
|
12
|
+
"strip-bom": "^5.0.0",
|
|
14
13
|
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
|
|
15
14
|
},
|
|
16
15
|
"author": "Climate Interactive",
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as fs from 'node:fs'
|
|
2
|
+
import stripBom from 'strip-bom'
|
|
3
|
+
|
|
4
|
+
// Numeric value of a string or number
|
|
5
|
+
let num = x => (typeof x === 'number' ? x : Number.parseFloat(x))
|
|
6
|
+
// Split a string into lines that may have Windows, Unix, or old Mac line endings.
|
|
7
|
+
let lines = s => s.split(/\r\n|\n|\r/)
|
|
8
|
+
// Print a string to the console
|
|
9
|
+
let print = s => {
|
|
10
|
+
console.log(s)
|
|
11
|
+
}
|
|
12
|
+
// Read a UTF-8 file into a string. Strip the BOM if present.
|
|
13
|
+
let read = pathname => stripBom(fs.readFileSync(pathname, 'utf8'))
|
|
14
|
+
// Write a string to a UTF-8 file
|
|
15
|
+
let write = (s, pathname) => {
|
|
16
|
+
fs.writeFileSync(pathname, s, { encoding: 'utf8' })
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Output buffer
|
|
20
|
+
let bufs = { _: '' }
|
|
21
|
+
// Open a buffer for writing
|
|
22
|
+
let open = channel => (bufs[channel] = '')
|
|
23
|
+
// Emit a string to a buffer
|
|
24
|
+
let emit = (a, channel = null) => {
|
|
25
|
+
channel = channel || '_'
|
|
26
|
+
bufs[channel] += a
|
|
27
|
+
}
|
|
28
|
+
// Emit a string to a buffer terminated by a newline
|
|
29
|
+
let emitLine = (a, channel = null) => {
|
|
30
|
+
channel = channel || '_'
|
|
31
|
+
bufs[channel] += a + '\n'
|
|
32
|
+
}
|
|
33
|
+
// Print a buffer to the console
|
|
34
|
+
let printBuf = (channel = null) => {
|
|
35
|
+
channel = channel || '_'
|
|
36
|
+
print(bufs[channel])
|
|
37
|
+
}
|
|
38
|
+
// Write a buffer to a file
|
|
39
|
+
let writeBuf = (pathname, channel = null) => {
|
|
40
|
+
channel = channel || '_'
|
|
41
|
+
write(bufs[channel], pathname)
|
|
42
|
+
}
|
|
43
|
+
// Get buffer contents as a string
|
|
44
|
+
let getBuf = (channel = null) => {
|
|
45
|
+
channel = channel || '_'
|
|
46
|
+
return bufs[channel]
|
|
47
|
+
}
|
|
48
|
+
// Clear a buffer
|
|
49
|
+
let clearBuf = (channel = null) => {
|
|
50
|
+
channel = channel || '_'
|
|
51
|
+
bufs[channel] = ''
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default {
|
|
55
|
+
clearBuf,
|
|
56
|
+
emit,
|
|
57
|
+
emitLine,
|
|
58
|
+
getBuf,
|
|
59
|
+
lines,
|
|
60
|
+
num,
|
|
61
|
+
open,
|
|
62
|
+
printBuf,
|
|
63
|
+
read,
|
|
64
|
+
write,
|
|
65
|
+
writeBuf
|
|
66
|
+
}
|
package/src/_shared/helpers.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as fs from 'node:fs'
|
|
2
2
|
import util from 'util'
|
|
3
|
-
import B from 'bufx'
|
|
4
3
|
import { parse as parseCsv } from 'csv-parse/sync'
|
|
5
4
|
import * as R from 'ramda'
|
|
6
5
|
import XLSX from 'xlsx'
|
|
6
|
+
import B from './bufx.js'
|
|
7
7
|
|
|
8
8
|
import { canonicalId, canonicalVarId } from '@sdeverywhere/parse'
|
|
9
9
|
|
package/src/_shared/read-dat.js
CHANGED
package/src/_shared/subscript.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import util from 'util'
|
|
2
|
-
import B from 'bufx'
|
|
3
|
-
import yaml from 'js-yaml'
|
|
4
2
|
import * as R from 'ramda'
|
|
3
|
+
import B from './bufx.js'
|
|
5
4
|
import { canonicalName, vlog } from './helpers.js'
|
|
6
5
|
|
|
7
6
|
// A subscript is a dimension or an index.
|
|
@@ -218,21 +217,6 @@ export function printSubscripts() {
|
|
|
218
217
|
}
|
|
219
218
|
return B.getBuf()
|
|
220
219
|
}
|
|
221
|
-
export function yamlSubsList() {
|
|
222
|
-
let subs = {}
|
|
223
|
-
for (let [k, v] of subscripts) {
|
|
224
|
-
subs[k] = v
|
|
225
|
-
}
|
|
226
|
-
return yaml.safeDump(subs)
|
|
227
|
-
}
|
|
228
|
-
export function loadSubscriptsFromYaml(yamlSubs) {
|
|
229
|
-
// Load the subscripts map from subscripts serialized to a YAML file by yamlSubsList.
|
|
230
|
-
// This function should be called instead of adding subscripts through the constructor.
|
|
231
|
-
let subs = yaml.safeLoad(yamlSubs)
|
|
232
|
-
for (const k in subs) {
|
|
233
|
-
subscripts.set(k, subs[k])
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
220
|
export function extractMarkedDims(subscripts) {
|
|
237
221
|
// Extract all marked dimensions and update subscripts.
|
|
238
222
|
let dims = []
|
package/src/model/model.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import B from 'bufx'
|
|
2
|
-
import yaml from 'js-yaml'
|
|
3
1
|
import * as R from 'ramda'
|
|
4
2
|
|
|
3
|
+
import B from '../_shared/bufx.js'
|
|
5
4
|
import { canonicalVensimName, decanonicalize, isIterable, strlist, vlog, vsort } from '../_shared/helpers.js'
|
|
6
5
|
import {
|
|
7
6
|
addIndex,
|
|
@@ -946,14 +945,6 @@ function printVarList() {
|
|
|
946
945
|
}
|
|
947
946
|
return B.getBuf()
|
|
948
947
|
}
|
|
949
|
-
function yamlVarList() {
|
|
950
|
-
// Print selected properties of all variable objects to a YAML string.
|
|
951
|
-
let vars = R.sortBy(
|
|
952
|
-
R.prop('refId'),
|
|
953
|
-
R.map(v => filterVar(v), variables)
|
|
954
|
-
)
|
|
955
|
-
return yaml.safeDump(vars)
|
|
956
|
-
}
|
|
957
948
|
function printVar(v) {
|
|
958
949
|
let nonAtoA = isNonAtoAName(v.varName) ? ' (non-apply-to-all)' : ''
|
|
959
950
|
B.emitLine(`${v.modelLHS}: ${v.varType}${nonAtoA}`)
|
|
@@ -1348,6 +1339,5 @@ export default {
|
|
|
1348
1339
|
varsWithName,
|
|
1349
1340
|
varWithName,
|
|
1350
1341
|
varWithRefId,
|
|
1351
|
-
vensimName
|
|
1352
|
-
yamlVarList
|
|
1342
|
+
vensimName
|
|
1353
1343
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// Copyright (c) 2022 Climate Interactive / New Venture Fund
|
|
2
2
|
|
|
3
3
|
import path from 'path'
|
|
4
|
-
import B from 'bufx'
|
|
5
4
|
|
|
6
5
|
import { parseVensimModel } from '@sdeverywhere/parse'
|
|
7
6
|
|
|
7
|
+
import B from './_shared/bufx.js'
|
|
8
8
|
import { readXlsx } from './_shared/helpers.js'
|
|
9
9
|
import { readDat } from './_shared/read-dat.js'
|
|
10
|
-
import { printSubscripts
|
|
10
|
+
import { printSubscripts } from './_shared/subscript.js'
|
|
11
11
|
import { cName } from './_shared/var-names.js'
|
|
12
12
|
import Model from './model/model.js'
|
|
13
13
|
import { getDirectSubscripts } from './model/read-subscripts.js'
|
|
@@ -21,7 +21,7 @@ import { generateCode } from './generate/gen-code.js'
|
|
|
21
21
|
* - If `operations` has 'generateC', the generated C code will be written to `buildDir`.
|
|
22
22
|
* - If `operations` has 'generateJS', the generated JS code will be written to `buildDir`.
|
|
23
23
|
* - If `operations` has 'printVarList', variables and subscripts will be written to
|
|
24
|
-
* txt
|
|
24
|
+
* txt and json files under `buildDir`.
|
|
25
25
|
* - If `operations` has 'printRefIdTest', reference identifiers will be printed to the console.
|
|
26
26
|
* - If `operations` has 'convertNames', no output will be generated, but the results of model
|
|
27
27
|
* analysis will be available.
|
|
@@ -92,10 +92,6 @@ export async function parseAndGenerate(input, spec, operations, modelDirname, mo
|
|
|
92
92
|
writeOutput(`${modelName}_vars.txt`, Model.printVarList())
|
|
93
93
|
// Write subscripts to a text file.
|
|
94
94
|
writeOutput(`${modelName}_subs.txt`, printSubscripts())
|
|
95
|
-
// Write variables to a YAML file.
|
|
96
|
-
writeOutput(`${modelName}_vars.yaml`, Model.yamlVarList())
|
|
97
|
-
// Write subscripts to a YAML file.
|
|
98
|
-
writeOutput(`${modelName}_subs.yaml`, yamlSubsList())
|
|
99
95
|
// Write variables and subscripts to a JSON file.
|
|
100
96
|
const jsonList = Model.jsonList()
|
|
101
97
|
writeOutput(`${modelName}.json`, JSON.stringify(jsonList.full, null, 2))
|