@sdeverywhere/compile 0.7.27 → 0.7.28

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  import path from 'path'
4
4
 
5
- import { parseVensimModel } from '@sdeverywhere/parse'
5
+ import { parseVensimModel, parseXmileModel } from '@sdeverywhere/parse'
6
6
 
7
7
  import B from './_shared/bufx.js'
8
8
  import { readXlsx } from './_shared/helpers.js'
@@ -14,7 +14,7 @@ import { getDirectSubscripts } from './model/read-subscripts.js'
14
14
  import { generateCode } from './generate/gen-code.js'
15
15
 
16
16
  /**
17
- * Parse a Vensim model and generate C code.
17
+ * Parse a Vensim or XMILE model and generate C code.
18
18
  *
19
19
  * This is the primary entrypoint for the `sde generate` command.
20
20
  *
@@ -26,7 +26,8 @@ import { generateCode } from './generate/gen-code.js'
26
26
  * - If `operations` has 'convertNames', no output will be generated, but the results of model
27
27
  * analysis will be available.
28
28
  *
29
- * @param {string} input The preprocessed Vensim model text.
29
+ * @param {string} input The preprocessed Vensim or XMILE model text.
30
+ * @param {string} modelKind The kind of model to parse, either 'vensim' or 'xmile'.
30
31
  * @param {*} spec The model spec (from the JSON file).
31
32
  * @param {string[]} operations The set of operations to perform; can include 'generateC', 'generateJS',
32
33
  * 'printVarList', 'printRefIdTest', 'convertNames'. If the array is empty, the model will be
@@ -38,7 +39,7 @@ import { generateCode } from './generate/gen-code.js'
38
39
  * @param {string} [varname] The variable name passed to the 'sde causes' command.
39
40
  * @return A string containing the generated C code.
40
41
  */
41
- export async function parseAndGenerate(input, spec, operations, modelDirname, modelName, buildDir, varname) {
42
+ export async function parseAndGenerate(input, modelKind, spec, operations, modelDirname, modelName, buildDir, varname) {
42
43
  // Read time series from external DAT files into a single object.
43
44
  // externalDatfiles is an array of either filenames or objects
44
45
  // giving a variable name prefix as the key and a filename as the value.
@@ -69,7 +70,7 @@ export async function parseAndGenerate(input, spec, operations, modelDirname, mo
69
70
  }
70
71
 
71
72
  // Parse the model and generate code
72
- let parsedModel = parseModel(input, modelDirname)
73
+ let parsedModel = parseModel(input, modelKind, modelDirname)
73
74
  let code = generateCode(parsedModel, { spec, operations, extData, directData, modelDirname, varname })
74
75
 
75
76
  function writeOutput(filename, text) {
@@ -132,33 +133,44 @@ export function printNames(namesPathname, operation) {
132
133
  * TODO: Fix return type
133
134
  *
134
135
  * @param {string} input The string containing the model text.
136
+ * @param {string} modelKind The kind of model to parse, either 'vensim' or 'xmile'.
135
137
  * @param {string} modelDir The absolute path to the directory containing the mdl file.
136
138
  * The dat, xlsx, and csv files referenced by the model will be relative to this directory.
137
139
  * @param {Object} [options] The options that control parsing.
138
140
  * @param {boolean} options.sort Whether to sort definitions alphabetically in the preprocess step.
139
141
  * @return {*} A parsed tree representation of the model.
140
142
  */
141
- export function parseModel(input, modelDir, options) {
142
- // Prepare the parse context that provides access to external data files
143
- let parseContext /*: VensimParseContext*/
144
- if (modelDir) {
145
- parseContext = {
146
- getDirectSubscripts(fileName, tabOrDelimiter, firstCell, lastCell /*, prefix*/) {
147
- // Resolve the CSV file relative the model directory
148
- const csvPath = path.resolve(modelDir, fileName)
149
-
150
- // Read the subscripts from the CSV file
151
- return getDirectSubscripts(csvPath, tabOrDelimiter, firstCell, lastCell)
143
+ export function parseModel(input, modelKind, modelDir, options) {
144
+ if (modelKind === 'vensim') {
145
+ // Prepare the parse context that provides access to external data files
146
+ let parseContext /*: VensimParseContext*/
147
+ if (modelDir) {
148
+ parseContext = {
149
+ getDirectSubscripts(fileName, tabOrDelimiter, firstCell, lastCell /*, prefix*/) {
150
+ // Resolve the CSV file relative the model directory
151
+ const csvPath = path.resolve(modelDir, fileName)
152
+
153
+ // Read the subscripts from the CSV file
154
+ return getDirectSubscripts(csvPath, tabOrDelimiter, firstCell, lastCell)
155
+ }
152
156
  }
153
157
  }
154
- }
155
158
 
156
- // Parse the model
157
- const sort = options?.sort === true
158
- const root = parseVensimModel(input, parseContext, sort)
159
+ // Parse the Vensim model
160
+ const sort = options?.sort === true
161
+ const root = parseVensimModel(input, parseContext, sort)
162
+
163
+ return {
164
+ kind: 'vensim',
165
+ root
166
+ }
167
+ } else {
168
+ // Parse the XMILE model
169
+ const root = parseXmileModel(input)
159
170
 
160
- return {
161
- kind: 'vensim',
162
- root
171
+ return {
172
+ kind: 'xmile',
173
+ root
174
+ }
163
175
  }
164
176
  }