@sdeverywhere/compile 0.7.34 → 0.7.36

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.
Files changed (2) hide show
  1. package/dist/index.d.ts +394 -0
  2. package/package.json +7 -5
@@ -0,0 +1,394 @@
1
+ import { preprocessVensimModel } from "@sdeverywhere/parse";
2
+ //#region dts-tmp/_shared/model-spec.d.ts
3
+ /**
4
+ * A variable name as used in the modeling tool, for example `Some Var[DimA]` as used in
5
+ * a Vensim model.
6
+ */
7
+ export type VarName = string;
8
+ /**
9
+ * A variable identifier in the canonical format used internally by SDEverywhere, for
10
+ * example `_some_var`. These are derived from a `VarName` by lowercasing the name and
11
+ * replacing special characters with underscores.
12
+ */
13
+ export type VarId = string;
14
+ /**
15
+ * A dimension (subscript range) identifier in the canonical format used internally by
16
+ * SDEverywhere, for example `_dima`.
17
+ */
18
+ export type DimId = string;
19
+ /**
20
+ * Describes a `dat` file that provides data for exogenous data variables in the model.
21
+ *
22
+ * This can either be:
23
+ * - a plain file name (relative to the model directory), for example `data.dat`, or
24
+ * - an object with a single key/value pair, where the key is a prefix that is prepended
25
+ * to each variable name read from the file, and the value is the file name, for
26
+ * example `{ "prefix ": "data.dat" }`.
27
+ */
28
+ export type DatFileSpec = string | {
29
+ [varNamePrefix: string]: string;
30
+ };
31
+ /**
32
+ * Describes a model (e.g., a Vensim mdl file) and the input/output variables that should
33
+ * be included in the model generated by SDEverywhere.
34
+ *
35
+ * This is the type of the object that is parsed from a `spec.json` file (as passed to the
36
+ * `sde generate` command using the `--spec` argument) and that is accepted by the
37
+ * `parseAndGenerate` function.
38
+ *
39
+ * All properties are optional. If neither `inputVarNames` nor `outputVarNames` is
40
+ * provided, the generated model will include all variables from the model and will not
41
+ * allow any inputs to be set at runtime.
42
+ */
43
+ export type ModelSpec = {
44
+ /**
45
+ * The input variables for the model, using the
46
+ * variable names as they appear in the modeling tool.
47
+ *
48
+ * When this is provided, only the listed variables can be set at runtime, and any
49
+ * variables that are not needed to compute the configured `outputVarNames` will be pruned
50
+ * from the generated model.
51
+ */
52
+ inputVarNames?: VarName[];
53
+ /**
54
+ * The output variables for the model, using the
55
+ * variable names as they appear in the modeling tool.
56
+ *
57
+ * It is customary to include `Time` as the first output variable.
58
+ *
59
+ * When this is provided, only the listed variables (plus the variables needed to compute
60
+ * them) will be included in the generated model.
61
+ */
62
+ outputVarNames?: VarName[];
63
+ /**
64
+ * The `dat` files that provide the data for exogenous
65
+ * data variables in the model.
66
+ *
67
+ * Each entry is resolved relative to the model directory (i.e., the directory that is
68
+ * passed using the `--datadir` argument, which defaults to the directory that contains the
69
+ * model file).
70
+ */
71
+ datFiles?: DatFileSpec[];
72
+ /**
73
+ * The mapping of data tag to
74
+ * Excel workbook file name, used to resolve the data for `GET DIRECT DATA`,
75
+ * `GET DIRECT CONSTANTS`, and `GET DIRECT LOOKUPS` calls in the model.
76
+ *
77
+ * Each key is the tag that appears in the model equation (for example, `?data`), and each
78
+ * value is the name of an `xlsx` file that is resolved relative to the model directory.
79
+ */
80
+ directData?: {
81
+ [dataTag: string]: string;
82
+ };
83
+ /**
84
+ * The mapping of dimension
85
+ * name to family name, used when SDEverywhere cannot infer the family for a dimension
86
+ * from the model alone.
87
+ *
88
+ * Both the keys and the values use the dimension names as they appear in the modeling tool
89
+ * (they are converted to canonical form when the spec file is read).
90
+ */
91
+ dimensionFamilies?: {
92
+ [dimName: string]: string;
93
+ };
94
+ /**
95
+ * The mapping of
96
+ * variable identifier to the dimension(s) on which that variable should be separated
97
+ * into one variable instance per subscript.
98
+ *
99
+ * Separating a variable is sometimes necessary to break a dependency cycle that would
100
+ * otherwise prevent the model from being evaluated. Each value can be either a single
101
+ * dimension identifier or an array of dimension identifiers.
102
+ */
103
+ specialSeparationDims?: {
104
+ [varId: string]: string | string[];
105
+ };
106
+ /**
107
+ * The dimensions for which all
108
+ * variables should be separated into one variable instance per subscript.
109
+ *
110
+ * This is a convenience alternative to `specialSeparationDims` that avoids the need to
111
+ * list each affected variable. Each entry can be either a single dimension identifier or
112
+ * an array of dimension identifiers; a variable is separated only if every dimension in
113
+ * the entry appears on the left-hand side of its equation.
114
+ */
115
+ separateAllVarsWithDims?: (DimId | DimId[])[];
116
+ /**
117
+ * Whether to bundle a model listing with the generated
118
+ * model.
119
+ *
120
+ * If undefined, defaults to false.
121
+ *
122
+ * When this is true, a model listing will be bundled with the generated model to allow the
123
+ * `runtime` package to resolve variables that are referenced by name or identifier. This
124
+ * listing will increase the size of the generated model, so it is recommended to set this
125
+ * to true only if it is needed.
126
+ */
127
+ bundleListing?: boolean;
128
+ /**
129
+ * Whether to allow constants to be
130
+ * overridden at runtime using `setConstant`.
131
+ *
132
+ * If undefined or false, the generated model will implement `setConstant` as a no-op,
133
+ * meaning that constants cannot be overridden at runtime.
134
+ *
135
+ * If true, all constants in the generated model will be available to be overridden.
136
+ *
137
+ * If an array is provided, only those variable names in the array will be available to be
138
+ * overridden.
139
+ */
140
+ customConstants?: boolean | VarName[];
141
+ /**
142
+ * Whether to allow lookups to be
143
+ * overridden at runtime using `setLookup`.
144
+ *
145
+ * If undefined or false, the generated model will implement `setLookup` as a no-op,
146
+ * meaning that lookups cannot be overridden at runtime.
147
+ *
148
+ * If true, all lookups in the generated model will be available to be overridden.
149
+ *
150
+ * If an array is provided, only those variable names in the array will be available to be
151
+ * overridden.
152
+ */
153
+ customLookups?: boolean | VarName[];
154
+ /**
155
+ * Whether to allow for capturing the data
156
+ * for arbitrary variables at runtime (including variables that are not configured in the
157
+ * `outputVarNames` array).
158
+ *
159
+ * If undefined or false, the generated model will implement `storeOutput` as a no-op,
160
+ * meaning that the data for arbitrary variables cannot be captured at runtime.
161
+ *
162
+ * If true, all variables in the generated model will be available to be captured at
163
+ * runtime.
164
+ *
165
+ * If an array is provided, only those variable names in the array will be available to be
166
+ * captured at runtime.
167
+ */
168
+ customOutputs?: boolean | VarName[];
169
+ /**
170
+ * The `dat` files that provide the data for
171
+ * exogenous data variables in the model.
172
+ *
173
+ * DEPRECATED: Use `datFiles` instead. This property is still honored (but is ignored if
174
+ * `datFiles` is also provided) and will be removed in a future release.
175
+ */
176
+ externalDatfiles?: DatFileSpec[];
177
+ /**
178
+ * An optional descriptive name for the model.
179
+ *
180
+ * This is not currently used by SDEverywhere, but is allowed (and is included in many
181
+ * existing `spec.json` files) as a way to document what the model is.
182
+ */
183
+ name?: string;
184
+ /**
185
+ * The input variable identifiers for the model, in
186
+ * canonical form.
187
+ *
188
+ * This is derived from `inputVarNames` while the model is being read, and is not intended
189
+ * to be set in a `spec.json` file.
190
+ */
191
+ inputVars?: VarId[];
192
+ /**
193
+ * The output variable identifiers for the model, in
194
+ * canonical form.
195
+ *
196
+ * This is derived from `outputVarNames` while the model is being read, and is not intended
197
+ * to be set in a `spec.json` file.
198
+ */
199
+ outputVars?: VarId[];
200
+ };
201
+ //#endregion
202
+ //#region dts-tmp/parse-and-generate.d.ts
203
+ /**
204
+ * The kind of model that is being parsed.
205
+ *
206
+ * @typedef {'vensim' | 'xmile'} ModelKind
207
+ */
208
+ /**
209
+ * A parsed tree representation of a model, along with the kind of model that was parsed.
210
+ *
211
+ * @typedef {Object} ParsedModel
212
+ * @property {ModelKind} kind The kind of model that was parsed.
213
+ * @property {import('@sdeverywhere/parse').Model} root The root of the parsed model AST.
214
+ */
215
+ /**
216
+ * An operation that can be performed by `parseAndGenerate`.
217
+ *
218
+ * - `generateC` writes the generated C code to the build directory.
219
+ * - `generateJS` writes the generated JS code to the build directory.
220
+ * - `printVarList` writes variables and subscripts to txt and json files under the build
221
+ * directory.
222
+ * - `printRefIdTest` prints reference identifiers to the console.
223
+ * - `printRefGraph` prints the variable dependency graph to the console.
224
+ * - `convertNames` generates no output, but makes the results of model analysis available.
225
+ *
226
+ * @typedef {'generateC' | 'generateJS' | 'printVarList' | 'printRefIdTest' | 'printRefGraph' | 'convertNames'} GenerateOperation
227
+ */
228
+ /**
229
+ * Parse a Vensim or XMILE model and generate C code.
230
+ *
231
+ * This is the primary entrypoint for the `sde generate` command.
232
+ *
233
+ * - If `operations` has 'generateC', the generated C code will be written to `buildDir`.
234
+ * - If `operations` has 'generateJS', the generated JS code will be written to `buildDir`.
235
+ * - If `operations` has 'printVarList', variables and subscripts will be written to
236
+ * txt and json files under `buildDir`.
237
+ * - If `operations` has 'printRefIdTest', reference identifiers will be printed to the console.
238
+ * - If `operations` has 'convertNames', no output will be generated, but the results of model
239
+ * analysis will be available.
240
+ *
241
+ * @param {string} input The preprocessed Vensim or XMILE model text.
242
+ * @param {ModelKind} modelKind The kind of model to parse.
243
+ * @param {import('./_shared/model-spec.js').ModelSpec} spec The model spec (from the JSON file).
244
+ * @param {GenerateOperation[]} operations The set of operations to perform. If the array is
245
+ * empty, the model will be read but no operation will be performed.
246
+ * @param {string} modelDirname The absolute path to the directory containing data (dat, xlsx, csv)
247
+ * files that are referenced by the model. These files will be resolved relative to this directory.
248
+ * @param {string} modelName The model name (without the mdl extension).
249
+ * @param {string} buildDir The output directory where the C or list files will be written.
250
+ * @param {string} [varname] The variable name passed to the 'sde causes' command.
251
+ * @return {Promise<string>} A promise that resolves with the generated C or JS code.
252
+ */
253
+ export declare function parseAndGenerate(input: string, modelKind: ModelKind, spec: ModelSpec, operations: GenerateOperation[], modelDirname: string, modelName: string, buildDir: string, varname?: string): Promise<string>;
254
+ /**
255
+ * Read the variable names from the given file, convert them to their
256
+ * C or Vensim representation, and print the results to the console.
257
+ *
258
+ * This is used only to implement the `sde names` command.
259
+ *
260
+ * @param {string} namesPathname The path to the file containing variables names.
261
+ * @param {'to-c' | 'to-vensim'} operation The conversion to perform.
262
+ * @return {void}
263
+ */
264
+ export declare function printNames(namesPathname: string, operation: 'to-c' | 'to-vensim'): void;
265
+ /**
266
+ * Read and parse the given model text and return the parsed model structure.
267
+ *
268
+ * @param {string} input The string containing the model text.
269
+ * @param {ModelKind} modelKind The kind of model to parse.
270
+ * @param {string} [modelDir] The absolute path to the directory containing data (dat, xlsx, csv)
271
+ * files that are referenced by the model. These files will be resolved relative to this directory.
272
+ * @param {Object} [options] The options that control parsing.
273
+ * @param {boolean} [options.sort] Whether to sort definitions alphabetically in the preprocess step.
274
+ * @return {ParsedModel} A parsed tree representation of the model.
275
+ */
276
+ export declare function parseModel(input: string, modelKind: ModelKind, modelDir?: string, options?: {
277
+ sort?: boolean;
278
+ }): ParsedModel;
279
+ /**
280
+ * The kind of model that is being parsed.
281
+ */
282
+ export type ModelKind = 'vensim' | 'xmile';
283
+ /**
284
+ * A parsed tree representation of a model, along with the kind of model that was parsed.
285
+ */
286
+ export type ParsedModel = {
287
+ /**
288
+ * The kind of model that was parsed.
289
+ */
290
+ kind: ModelKind;
291
+ /**
292
+ * The root of the parsed model AST.
293
+ */
294
+ root: import('@sdeverywhere/parse').Model;
295
+ };
296
+ /**
297
+ * An operation that can be performed by `parseAndGenerate`.
298
+ *
299
+ * - `generateC` writes the generated C code to the build directory.
300
+ * - `generateJS` writes the generated JS code to the build directory.
301
+ * - `printVarList` writes variables and subscripts to txt and json files under the build
302
+ * directory.
303
+ * - `printRefIdTest` prints reference identifiers to the console.
304
+ * - `printRefGraph` prints the variable dependency graph to the console.
305
+ * - `convertNames` generates no output, but makes the results of model analysis available.
306
+ */
307
+ export type GenerateOperation = 'generateC' | 'generateJS' | 'printVarList' | 'printRefIdTest' | 'printRefGraph' | 'convertNames';
308
+ //#endregion
309
+ //#region dts-tmp/_shared/read-dat.d.ts
310
+ /**
311
+ * The datasets read from external `dat` files, keyed by variable identifier. Each
312
+ * dataset is a map of time to value.
313
+ *
314
+ * @typedef {Map<import('./model-spec.js').VarId, Map<number, number>>} ExtData
315
+ */
316
+ /**
317
+ * Read a Vensim `dat` file with static data and return a Map.
318
+ * Each dataset consists of a key (the variable name in the canonical
319
+ * format used by SDE) and a map of time/value pairs.
320
+ *
321
+ * @param {string} pathname The absolute path to the dat file.
322
+ * @param {string} [prefix] An optional prefix string prepended to var names.
323
+ * @return {Promise<ExtData>} A promise that resolves with a Map containing the datasets.
324
+ */
325
+ export declare function readDat(pathname: string, prefix?: string): Promise<ExtData>;
326
+ /**
327
+ * The datasets read from external `dat` files, keyed by variable identifier. Each
328
+ * dataset is a map of time to value.
329
+ */
330
+ export type ExtData = Map<VarId, Map<number, number>>;
331
+ //#endregion
332
+ //#region dts-tmp/_shared/helpers.d.ts
333
+ export declare function canonicalName(name: VarName): VarId;
334
+ //#endregion
335
+ //#region dts-tmp/generate/gen-code.d.ts
336
+ /**
337
+ * Generate code from the given parsed model.
338
+ *
339
+ * @param {import('../parse-and-generate.js').ParsedModel} parsedModel The parsed model structure.
340
+ * @param {Object} opts The options that control code generation.
341
+ * @param {import('../_shared/model-spec.js').ModelSpec} opts.spec The parsed `spec.json` object.
342
+ * @param {import('../parse-and-generate.js').GenerateOperation[]} opts.operations The array
343
+ * of operations to perform.
344
+ * @param {import('../_shared/read-dat.js').ExtData} [opts.extData] The map of datasets from
345
+ * external `.dat` files.
346
+ * @param {Map<string, any>} [opts.directData] The mapping of dataset name used in a
347
+ * `GET DIRECT DATA` call (e.g., `?data`) to the tabular data contained in the loaded
348
+ * data file.
349
+ * @param {string} [opts.modelDirname] The absolute path to the directory containing data
350
+ * (dat, xlsx, csv) files that are referenced by the model. This path is used for
351
+ * resolving data files for `GET DIRECT SUBSCRIPT` calls.
352
+ * @param {string} [opts.varname] The variable name passed to the `sde causes` command.
353
+ * @returns {string} A string containing the generated code.
354
+ */
355
+ export declare function generateCode(parsedModel: ParsedModel, opts: {
356
+ spec: ModelSpec;
357
+ operations: GenerateOperation[];
358
+ extData?: ExtData;
359
+ directData?: Map<string, any>;
360
+ modelDirname?: string;
361
+ varname?: string;
362
+ }): string;
363
+ //#endregion
364
+ //#region dts-tmp/index.d.ts
365
+ /**
366
+ * @hidden This is not yet part of the public API; it is exposed only for use
367
+ * in the experimental playground app.
368
+ */
369
+ export declare function resetState(): void;
370
+ /**
371
+ * @hidden This is not yet part of the public API; it is exposed only for use
372
+ * in the experimental playground app.
373
+ *
374
+ * @param {string} mdlContent The string containing the Vensim model text.
375
+ * @param {string} [modelDir] The absolute path to the directory containing data files.
376
+ * @return {import('./parse-and-generate.js').ParsedModel} A parsed tree representation of the model.
377
+ */
378
+ export declare function parseInlineVensimModel(mdlContent: string, modelDir?: string): ParsedModel;
379
+ /**
380
+ * @hidden This is not yet part of the public API; it is exposed only for use
381
+ * in the experimental playground app.
382
+ *
383
+ * @param {string} mdlContent The string containing the XMILE model text.
384
+ * @param {string} [modelDir] The absolute path to the directory containing data files.
385
+ * @return {import('./parse-and-generate.js').ParsedModel} A parsed tree representation of the model.
386
+ */
387
+ export declare function parseInlineXmileModel(mdlContent: string, modelDir?: string): ParsedModel;
388
+ /**
389
+ * @hidden This is not yet part of the public API; it is exposed only for use
390
+ * in the experimental playground app.
391
+ */
392
+ export declare function getModelListing(): any;
393
+ //#endregion
394
+ export { preprocessVensimModel };
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@sdeverywhere/compile",
3
- "version": "0.7.34",
3
+ "version": "0.7.36",
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
  "types": "./dist/index.d.ts",
8
8
  "dependencies": {
9
- "@sdeverywhere/parse": "^0.1.6",
9
+ "@sdeverywhere/parse": "^0.1.7",
10
10
  "byline": "^5.0.0",
11
- "csv-parse": "^5.3.3",
11
+ "csv-parse": "^7.0.2",
12
12
  "fflate": "^0.8.3",
13
13
  "ramda": "^0.27.0",
14
14
  "strip-bom": "^5.0.0"
@@ -25,8 +25,10 @@
25
25
  "url": "https://github.com/climateinteractive/SDEverywhere/issues"
26
26
  },
27
27
  "scripts": {
28
- "clean": "rm -rf dist",
29
- "build": "tsup",
28
+ "clean": "rm -rf dts-tmp dist",
29
+ "build:dts": "tsc -p tsconfig-build.json --emitDeclarationOnly --outDir dts-tmp",
30
+ "build:bundle": "tsdown",
31
+ "build": "run-s build:dts build:bundle",
30
32
  "lint": "eslint . --max-warnings 0",
31
33
  "prettier:check": "prettier --check .",
32
34
  "prettier:fix": "prettier --write .",