@sdeverywhere/compile 0.7.17 → 0.7.19

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.
@@ -1,88 +0,0 @@
1
- import * as R from 'ramda'
2
-
3
- import { canonicalName } from '../_shared/helpers.js'
4
- import { sub, isDimension } from '../_shared/subscript.js'
5
- import ModelReader from '../parse/model-reader.js'
6
- import { createParser } from '../parse/parser.js'
7
-
8
- //
9
- // ModelLHSReader parses the LHS of a var in Vensim format and
10
- // constructs a list of var names with indices for subscripted vars.
11
- //
12
- export default class ModelLHSReader extends ModelReader {
13
- constructor() {
14
- super()
15
- this.varName = ''
16
- this.modelLHSList = []
17
- this.modelSubscripts = []
18
- }
19
- read(modelLHS) {
20
- // Parse a model LHS and return the var name without subscripts.
21
- // The names function may be called on this object to retrieve expanded subscript names.
22
- let parser = createParser(modelLHS)
23
- let tree = parser.lhs()
24
- this.visitLhs(tree)
25
- return this.varName
26
- }
27
- names() {
28
- // Expand dimensions in a subscripted var into individual vars with indices.
29
- if (this.modelLHSList.length > 0) {
30
- return this.modelLHSList
31
- } else {
32
- return [this.varName]
33
- }
34
- }
35
- visitLhs(ctx) {
36
- let varName = ctx.Id().getText()
37
- this.varName = varName
38
- super.visitLhs(ctx)
39
- }
40
- visitSubscriptList(ctx) {
41
- // Construct the modelLHSList array with the LHS expanded into an entry for each index
42
- // in the same format as Vensim log files.
43
- let subscripts = R.map(id => id.getText(), ctx.Id())
44
- this.modelSubscripts = subscripts.map(s => canonicalName(s))
45
- let modelLHSInds = dim => {
46
- // Construct the model indices for a dimension.
47
- // If the subscript range contains a dimension, expand it into index names in place.
48
- let indNames = R.map(subscriptModelName => {
49
- let subscript = canonicalName(subscriptModelName)
50
- if (isDimension(subscript)) {
51
- return sub(subscript).modelValue
52
- } else {
53
- return subscriptModelName
54
- }
55
- }, dim.modelValue)
56
- return R.flatten(indNames)
57
- }
58
- let expandLHSDims = (a, subscripts) => {
59
- // Recursively emit an LHS with Vensim names for each index in LHS dimensions.
60
- // Accumulate expanded subscripts in the "a" variable.
61
- if (subscripts.length === 0) {
62
- this.modelLHSList.push(`${this.varName}[${a.join(',')}]`)
63
- } else {
64
- // Expand the first subscript into the accumulator.
65
- let firstSub = canonicalName(R.head(subscripts))
66
- if (isDimension(firstSub)) {
67
- // Emit each index in a dimension subscript.
68
- for (let subscriptModelName of sub(firstSub).modelValue) {
69
- if (isDimension(canonicalName(subscriptModelName))) {
70
- // Expand a subdimension found in a dimension subscript value.
71
- for (let ind of modelLHSInds(sub(canonicalName(subscriptModelName)))) {
72
- expandLHSDims(a.concat(ind), R.tail(subscripts))
73
- }
74
- } else {
75
- // Expand an index subscript in a dimension directly.
76
- expandLHSDims(a.concat(subscriptModelName), R.tail(subscripts))
77
- }
78
- }
79
- } else {
80
- // Emit an index subscript directly.
81
- expandLHSDims(a.concat(R.head(subscripts)), R.tail(subscripts))
82
- }
83
- }
84
- }
85
- expandLHSDims([], subscripts)
86
- return this.modelLHSList
87
- }
88
- }