@sccharts-lab/engine 0.0.0-stage → 0.1.0

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 (279) hide show
  1. package/LICENSE +277 -0
  2. package/README.md +125 -2
  3. package/dist/codegen/expr.d.ts +36 -0
  4. package/dist/codegen/expr.js +252 -0
  5. package/dist/codegen/expr.js.map +1 -0
  6. package/dist/codegen/index.d.ts +4 -0
  7. package/dist/codegen/index.js +238 -0
  8. package/dist/codegen/index.js.map +1 -0
  9. package/dist/codegen/layout.d.ts +57 -0
  10. package/dist/codegen/layout.js +306 -0
  11. package/dist/codegen/layout.js.map +1 -0
  12. package/dist/codegen/logic.d.ts +31 -0
  13. package/dist/codegen/logic.js +258 -0
  14. package/dist/codegen/logic.js.map +1 -0
  15. package/dist/codegen/methods.d.ts +17 -0
  16. package/dist/codegen/methods.js +242 -0
  17. package/dist/codegen/methods.js.map +1 -0
  18. package/dist/codegen/reset.d.ts +6 -0
  19. package/dist/codegen/reset.js +110 -0
  20. package/dist/codegen/reset.js.map +1 -0
  21. package/dist/codegen/types.d.ts +81 -0
  22. package/dist/codegen/types.js +3 -0
  23. package/dist/codegen/types.js.map +1 -0
  24. package/dist/format.d.ts +136 -0
  25. package/dist/format.js +872 -0
  26. package/dist/format.js.map +1 -0
  27. package/dist/index.d.ts +120 -0
  28. package/dist/index.js +507 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/internal.d.ts +1 -0
  31. package/dist/internal.js +4 -0
  32. package/dist/internal.js.map +1 -0
  33. package/dist/parser/ids.d.ts +4 -0
  34. package/dist/parser/ids.js +172 -0
  35. package/dist/parser/ids.js.map +1 -0
  36. package/dist/parser/imports.d.ts +2 -0
  37. package/dist/parser/imports.js +28 -0
  38. package/dist/parser/imports.js.map +1 -0
  39. package/dist/parser/interface.d.ts +10 -0
  40. package/dist/parser/interface.js +93 -0
  41. package/dist/parser/interface.js.map +1 -0
  42. package/dist/parser/kielerWording.d.ts +2 -0
  43. package/dist/parser/kielerWording.js +196 -0
  44. package/dist/parser/kielerWording.js.map +1 -0
  45. package/dist/parser/lexer.d.ts +25 -0
  46. package/dist/parser/lexer.js +334 -0
  47. package/dist/parser/lexer.js.map +1 -0
  48. package/dist/parser/parser.d.ts +29 -0
  49. package/dist/parser/parser.js +3249 -0
  50. package/dist/parser/parser.js.map +1 -0
  51. package/dist/parser/resolve.d.ts +46 -0
  52. package/dist/parser/resolve.js +919 -0
  53. package/dist/parser/resolve.js.map +1 -0
  54. package/dist/parser/source.d.ts +15 -0
  55. package/dist/parser/source.js +42 -0
  56. package/dist/parser/source.js.map +1 -0
  57. package/dist/parser/validate.d.ts +5 -0
  58. package/dist/parser/validate.js +1430 -0
  59. package/dist/parser/validate.js.map +1 -0
  60. package/dist/sim/abortRegions.d.ts +9 -0
  61. package/dist/sim/abortRegions.js +192 -0
  62. package/dist/sim/abortRegions.js.map +1 -0
  63. package/dist/sim/analysis.d.ts +113 -0
  64. package/dist/sim/analysis.js +1104 -0
  65. package/dist/sim/analysis.js.map +1 -0
  66. package/dist/sim/codegen.d.ts +30 -0
  67. package/dist/sim/codegen.js +730 -0
  68. package/dist/sim/codegen.js.map +1 -0
  69. package/dist/sim/compile.d.ts +43 -0
  70. package/dist/sim/compile.js +1062 -0
  71. package/dist/sim/compile.js.map +1 -0
  72. package/dist/sim/deps.d.ts +73 -0
  73. package/dist/sim/deps.js +481 -0
  74. package/dist/sim/deps.js.map +1 -0
  75. package/dist/sim/describe.d.ts +22 -0
  76. package/dist/sim/describe.js +272 -0
  77. package/dist/sim/describe.js.map +1 -0
  78. package/dist/sim/describePreemption.d.ts +22 -0
  79. package/dist/sim/describePreemption.js +90 -0
  80. package/dist/sim/describePreemption.js.map +1 -0
  81. package/dist/sim/describeStructure.d.ts +75 -0
  82. package/dist/sim/describeStructure.js +293 -0
  83. package/dist/sim/describeStructure.js.map +1 -0
  84. package/dist/sim/eval.d.ts +99 -0
  85. package/dist/sim/eval.js +644 -0
  86. package/dist/sim/eval.js.map +1 -0
  87. package/dist/sim/exec.d.ts +212 -0
  88. package/dist/sim/exec.js +874 -0
  89. package/dist/sim/exec.js.map +1 -0
  90. package/dist/sim/fold.d.ts +2 -0
  91. package/dist/sim/fold.js +40 -0
  92. package/dist/sim/fold.js.map +1 -0
  93. package/dist/sim/goblock.d.ts +16 -0
  94. package/dist/sim/goblock.js +171 -0
  95. package/dist/sim/goblock.js.map +1 -0
  96. package/dist/sim/guardIndices.d.ts +13 -0
  97. package/dist/sim/guardIndices.js +137 -0
  98. package/dist/sim/guardIndices.js.map +1 -0
  99. package/dist/sim/index.d.ts +43 -0
  100. package/dist/sim/index.js +138 -0
  101. package/dist/sim/index.js.map +1 -0
  102. package/dist/sim/lower.d.ts +33 -0
  103. package/dist/sim/lower.js +716 -0
  104. package/dist/sim/lower.js.map +1 -0
  105. package/dist/sim/memory.d.ts +47 -0
  106. package/dist/sim/memory.js +328 -0
  107. package/dist/sim/memory.js.map +1 -0
  108. package/dist/sim/model.d.ts +487 -0
  109. package/dist/sim/model.js +772 -0
  110. package/dist/sim/model.js.map +1 -0
  111. package/dist/sim/passes/abort.d.ts +2 -0
  112. package/dist/sim/passes/abort.js +269 -0
  113. package/dist/sim/passes/abort.js.map +1 -0
  114. package/dist/sim/passes/abortRegion.d.ts +10 -0
  115. package/dist/sim/passes/abortRegion.js +62 -0
  116. package/dist/sim/passes/abortRegion.js.map +1 -0
  117. package/dist/sim/passes/arrayAssignment.d.ts +2 -0
  118. package/dist/sim/passes/arrayAssignment.js +147 -0
  119. package/dist/sim/passes/arrayAssignment.js.map +1 -0
  120. package/dist/sim/passes/codeEffects.d.ts +2 -0
  121. package/dist/sim/passes/codeEffects.js +14 -0
  122. package/dist/sim/passes/codeEffects.js.map +1 -0
  123. package/dist/sim/passes/complexFinalState.d.ts +4 -0
  124. package/dist/sim/passes/complexFinalState.js +77 -0
  125. package/dist/sim/passes/complexFinalState.js.map +1 -0
  126. package/dist/sim/passes/connector.d.ts +2 -0
  127. package/dist/sim/passes/connector.js +18 -0
  128. package/dist/sim/passes/connector.js.map +1 -0
  129. package/dist/sim/passes/contained.d.ts +37 -0
  130. package/dist/sim/passes/contained.js +198 -0
  131. package/dist/sim/passes/contained.js.map +1 -0
  132. package/dist/sim/passes/context.d.ts +39 -0
  133. package/dist/sim/passes/context.js +3 -0
  134. package/dist/sim/passes/context.js.map +1 -0
  135. package/dist/sim/passes/countDelay.d.ts +2 -0
  136. package/dist/sim/passes/countDelay.js +51 -0
  137. package/dist/sim/passes/countDelay.js.map +1 -0
  138. package/dist/sim/passes/dataflow.d.ts +11 -0
  139. package/dist/sim/passes/dataflow.js +319 -0
  140. package/dist/sim/passes/dataflow.js.map +1 -0
  141. package/dist/sim/passes/deferred.d.ts +2 -0
  142. package/dist/sim/passes/deferred.js +345 -0
  143. package/dist/sim/passes/deferred.js.map +1 -0
  144. package/dist/sim/passes/detect.d.ts +10 -0
  145. package/dist/sim/passes/detect.js +33 -0
  146. package/dist/sim/passes/detect.js.map +1 -0
  147. package/dist/sim/passes/during.d.ts +2 -0
  148. package/dist/sim/passes/during.js +67 -0
  149. package/dist/sim/passes/during.js.map +1 -0
  150. package/dist/sim/passes/entry.d.ts +5 -0
  151. package/dist/sim/passes/entry.js +93 -0
  152. package/dist/sim/passes/entry.js.map +1 -0
  153. package/dist/sim/passes/enum.d.ts +2 -0
  154. package/dist/sim/passes/enum.js +22 -0
  155. package/dist/sim/passes/enum.js.map +1 -0
  156. package/dist/sim/passes/exit.d.ts +4 -0
  157. package/dist/sim/passes/exit.js +120 -0
  158. package/dist/sim/passes/exit.js.map +1 -0
  159. package/dist/sim/passes/finalRegion.d.ts +2 -0
  160. package/dist/sim/passes/finalRegion.js +21 -0
  161. package/dist/sim/passes/finalRegion.js.map +1 -0
  162. package/dist/sim/passes/followedBy.d.ts +2 -0
  163. package/dist/sim/passes/followedBy.js +78 -0
  164. package/dist/sim/passes/followedBy.js.map +1 -0
  165. package/dist/sim/passes/forRegion.d.ts +9 -0
  166. package/dist/sim/passes/forRegion.js +230 -0
  167. package/dist/sim/passes/forRegion.js.map +1 -0
  168. package/dist/sim/passes/history.d.ts +12 -0
  169. package/dist/sim/passes/history.js +115 -0
  170. package/dist/sim/passes/history.js.map +1 -0
  171. package/dist/sim/passes/hostCode.d.ts +2 -0
  172. package/dist/sim/passes/hostCode.js +152 -0
  173. package/dist/sim/passes/hostCode.js.map +1 -0
  174. package/dist/sim/passes/index.d.ts +4 -0
  175. package/dist/sim/passes/index.js +84 -0
  176. package/dist/sim/passes/index.js.map +1 -0
  177. package/dist/sim/passes/inheritance.d.ts +8 -0
  178. package/dist/sim/passes/inheritance.js +233 -0
  179. package/dist/sim/passes/inheritance.js.map +1 -0
  180. package/dist/sim/passes/initOperator.d.ts +2 -0
  181. package/dist/sim/passes/initOperator.js +68 -0
  182. package/dist/sim/passes/initOperator.js.map +1 -0
  183. package/dist/sim/passes/initialization.d.ts +2 -0
  184. package/dist/sim/passes/initialization.js +69 -0
  185. package/dist/sim/passes/initialization.js.map +1 -0
  186. package/dist/sim/passes/ode.d.ts +2 -0
  187. package/dist/sim/passes/ode.js +14 -0
  188. package/dist/sim/passes/ode.js.map +1 -0
  189. package/dist/sim/passes/period.d.ts +2 -0
  190. package/dist/sim/passes/period.js +106 -0
  191. package/dist/sim/passes/period.js.map +1 -0
  192. package/dist/sim/passes/prTransitions.d.ts +2 -0
  193. package/dist/sim/passes/prTransitions.js +12 -0
  194. package/dist/sim/passes/prTransitions.js.map +1 -0
  195. package/dist/sim/passes/pre.d.ts +4 -0
  196. package/dist/sim/passes/pre.js +326 -0
  197. package/dist/sim/passes/pre.js.map +1 -0
  198. package/dist/sim/passes/quirks.d.ts +2 -0
  199. package/dist/sim/passes/quirks.js +169 -0
  200. package/dist/sim/passes/quirks.js.map +1 -0
  201. package/dist/sim/passes/reference.d.ts +60 -0
  202. package/dist/sim/passes/reference.js +386 -0
  203. package/dist/sim/passes/reference.js.map +1 -0
  204. package/dist/sim/passes/regionActions.d.ts +2 -0
  205. package/dist/sim/passes/regionActions.js +56 -0
  206. package/dist/sim/passes/regionActions.js.map +1 -0
  207. package/dist/sim/passes/sequentialFollowedBy.d.ts +2 -0
  208. package/dist/sim/passes/sequentialFollowedBy.js +72 -0
  209. package/dist/sim/passes/sequentialFollowedBy.js.map +1 -0
  210. package/dist/sim/passes/signal.d.ts +2 -0
  211. package/dist/sim/passes/signal.js +230 -0
  212. package/dist/sim/passes/signal.js.map +1 -0
  213. package/dist/sim/passes/static.d.ts +5 -0
  214. package/dist/sim/passes/static.js +67 -0
  215. package/dist/sim/passes/static.js.map +1 -0
  216. package/dist/sim/passes/suspend.d.ts +10 -0
  217. package/dist/sim/passes/suspend.js +71 -0
  218. package/dist/sim/passes/suspend.js.map +1 -0
  219. package/dist/sim/passes/termination.d.ts +9 -0
  220. package/dist/sim/passes/termination.js +69 -0
  221. package/dist/sim/passes/termination.js.map +1 -0
  222. package/dist/sim/passes/timedAutomata.d.ts +10 -0
  223. package/dist/sim/passes/timedAutomata.js +437 -0
  224. package/dist/sim/passes/timedAutomata.js.map +1 -0
  225. package/dist/sim/passes/unpackClasses.d.ts +2 -0
  226. package/dist/sim/passes/unpackClasses.js +54 -0
  227. package/dist/sim/passes/unpackClasses.js.map +1 -0
  228. package/dist/sim/passes/userSchedule.d.ts +2 -0
  229. package/dist/sim/passes/userSchedule.js +43 -0
  230. package/dist/sim/passes/userSchedule.js.map +1 -0
  231. package/dist/sim/passes/weakSuspend.d.ts +2 -0
  232. package/dist/sim/passes/weakSuspend.js +145 -0
  233. package/dist/sim/passes/weakSuspend.js.map +1 -0
  234. package/dist/sim/preemptionLog.d.ts +41 -0
  235. package/dist/sim/preemptionLog.js +133 -0
  236. package/dist/sim/preemptionLog.js.map +1 -0
  237. package/dist/sim/random.d.ts +12 -0
  238. package/dist/sim/random.js +40 -0
  239. package/dist/sim/random.js.map +1 -0
  240. package/dist/sim/reasons.d.ts +29 -0
  241. package/dist/sim/reasons.js +168 -0
  242. package/dist/sim/reasons.js.map +1 -0
  243. package/dist/sim/scg.d.ts +124 -0
  244. package/dist/sim/scg.js +723 -0
  245. package/dist/sim/scg.js.map +1 -0
  246. package/dist/sim/sfr.d.ts +4 -0
  247. package/dist/sim/sfr.js +264 -0
  248. package/dist/sim/sfr.js.map +1 -0
  249. package/dist/sim/stringConditions.d.ts +3 -0
  250. package/dist/sim/stringConditions.js +42 -0
  251. package/dist/sim/stringConditions.js.map +1 -0
  252. package/dist/sim/structure/copy.d.ts +4 -0
  253. package/dist/sim/structure/copy.js +140 -0
  254. package/dist/sim/structure/copy.js.map +1 -0
  255. package/dist/sim/structure/delays.d.ts +30 -0
  256. package/dist/sim/structure/delays.js +127 -0
  257. package/dist/sim/structure/delays.js.map +1 -0
  258. package/dist/sim/structure/instance.d.ts +15 -0
  259. package/dist/sim/structure/instance.js +69 -0
  260. package/dist/sim/structure/instance.js.map +1 -0
  261. package/dist/sim/structure/methods.d.ts +19 -0
  262. package/dist/sim/structure/methods.js +550 -0
  263. package/dist/sim/structure/methods.js.map +1 -0
  264. package/dist/sim/structure/rewrite.d.ts +20 -0
  265. package/dist/sim/structure/rewrite.js +113 -0
  266. package/dist/sim/structure/rewrite.js.map +1 -0
  267. package/dist/sim/structure/track.d.ts +22 -0
  268. package/dist/sim/structure/track.js +123 -0
  269. package/dist/sim/structure/track.js.map +1 -0
  270. package/dist/sim/tracker.d.ts +77 -0
  271. package/dist/sim/tracker.js +265 -0
  272. package/dist/sim/tracker.js.map +1 -0
  273. package/dist/sim/values.d.ts +31 -0
  274. package/dist/sim/values.js +103 -0
  275. package/dist/sim/values.js.map +1 -0
  276. package/dist/types.d.ts +670 -0
  277. package/dist/types.js +9 -0
  278. package/dist/types.js.map +1 -0
  279. package/package.json +58 -3
@@ -0,0 +1,716 @@
1
+ // Lowering: the resolved AST of one scchart (types.ts) becomes the simulator's working model (model.ts).
2
+ //
3
+ // Besides the plain copy this does what KIELER does before its own processors run, in the simulation chain:
4
+ // - Taken Transition Signaling (sccharts/processors/TakenTransitionSignaling.xtend, the first processor of
5
+ // de.cau.cs.kieler.sccharts.simulation.tts.netlist.c): every source transition gets a bookkeeping effect
6
+ // (here a `take` mark with KIELER's index), and the root gets an entry action that resets the counters.
7
+ // Both influence how KIELER's later processors restructure the chart, so they are kept as real effects.
8
+ // - Every source local action with effects gets an `action` mark as its first effect, so the simulator can
9
+ // tell which actions ran however the passes rearrange them.
10
+ // - Const inlining (processors/Const.xtend): references to constants become copies of their initial values,
11
+ // with constant folding in scopes that declare constants, so constants never exist at run time.
12
+ import { CAction, CRegion, CState, CTransition, CVar, copyExpr, setTarget, } from "./model.js";
13
+ import { foldConstants } from "./fold.js";
14
+ class Lowering {
15
+ index;
16
+ vars = new Map();
17
+ /** Class and struct instances (`class C { ... } c`): per instance id, its members by member id. */
18
+ instances = new Map();
19
+ states = new Map();
20
+ regions = new Map();
21
+ problems = [];
22
+ /** What KIELER rejects that only the lowering sees (`scchart(C).X` that cannot be copied). */
23
+ reports = [];
24
+ /** Consts whose initial value is being lowered (cycle guard). */
25
+ inlining = new Set();
26
+ /** Lowered initial value of each const (lowered on first use). */
27
+ constValues = new Map();
28
+ /** Source declaration of each const. */
29
+ constSource = new Map();
30
+ /** Scopes (source) that declare a constant: KIELER folds constant expressions there. */
31
+ constScopes = new Set();
32
+ /** `state(S)` in method bodies: the flag of each state S (KIELER's MethodSignaling, attachStateFlags). */
33
+ stateFlags = new Map();
34
+ constructor(index) {
35
+ this.index = index;
36
+ }
37
+ // ----- declarations -----
38
+ /**
39
+ * KIELER's Enum processor (sccharts/processors/Enum.xtend), done while lowering: a `ref E x` of a (non-host)
40
+ * enum E is an int variable, and a value `E.V` is the int literal of its position in E.
41
+ */
42
+ enumOf(d) {
43
+ if (d.declKind !== 'reference')
44
+ return undefined;
45
+ const target = this.index?.refTargets.get(d);
46
+ return target && target.kind === 'declaration' && target.declKind === 'enum' && !target.host ? target : undefined;
47
+ }
48
+ enumValue(r) {
49
+ if (!r.decl || !r.sub?.decl || !this.index)
50
+ return undefined;
51
+ const e = this.index.vos.get(r.decl)?.decl;
52
+ if (!e || e.declKind !== 'enum' || e.host)
53
+ return undefined;
54
+ const i = (e.members?.[0]?.valuedObjects ?? []).findIndex(vo => vo.id === r.sub.decl);
55
+ return i < 0 ? undefined : i;
56
+ }
57
+ declare(scope, declarations) {
58
+ for (const d of declarations) {
59
+ const enumRef = this.enumOf(d);
60
+ for (const vo of d.valuedObjects) {
61
+ const type = enumRef ? 'int' : d.type ?? (d.signal ? 'pure' : 'host');
62
+ const v = new CVar(vo.name, type);
63
+ v.hostType = d.hostType;
64
+ v.signal = d.signal;
65
+ v.input = d.input;
66
+ v.output = d.output;
67
+ v.const = d.const;
68
+ v.static = d.static;
69
+ v.extern = !!d.extern;
70
+ v.global = d.global;
71
+ v.combine = vo.combine;
72
+ v.decl = vo.id;
73
+ v.span = vo.nameSpan ?? vo.span;
74
+ v.scope = scope;
75
+ if (d.declKind !== 'variable' && !enumRef)
76
+ v.declKind = d.declKind;
77
+ // Annotations of the declaration (`@Passive clock t`), as `@name` tags.
78
+ for (const a of [...d.annotations, ...(vo.annotations ?? [])])
79
+ v.tags.add(`@${a.name}`);
80
+ scope.decls.push(v);
81
+ this.vars.set(vo.id, v);
82
+ if (d.declKind === 'class' || d.declKind === 'struct') {
83
+ if (d.host)
84
+ v.tags.add('#host');
85
+ else if (vo.cardinalities.length)
86
+ v.tags.add('#array');
87
+ else if ((d.members ?? []).some(m => m.declKind === 'class' || m.declKind === 'struct'))
88
+ v.tags.add('#nested');
89
+ else
90
+ this.declareMembers(scope, d, vo, v);
91
+ }
92
+ if (d.const && vo.initialValue) {
93
+ this.constSource.set(v, vo.initialValue);
94
+ this.constScopes.add(scope);
95
+ }
96
+ }
97
+ }
98
+ }
99
+ /**
100
+ * The members of a class or struct instance: variables of the declaring scope (named `<instance>.<member>`, so they
101
+ * never clash) and, for each method, a method of the instance whose body reads and writes these variables (KIELER
102
+ * keeps the instance as a C struct and its methods as C functions; the simulator runs them the same way).
103
+ */
104
+ declareMembers(scope, d, vo, inst) {
105
+ const members = new Map();
106
+ // @UnpackClass (KIELER's Unpack Classes processor): the members become variables `<instance>_<member>`.
107
+ const unpack = d.annotations.some(a => a.name === 'UnpackClass');
108
+ for (const m of d.members ?? []) {
109
+ for (const mvo of m.valuedObjects) {
110
+ if (m.declKind !== 'variable' && m.declKind !== 'method')
111
+ continue;
112
+ const v = new CVar(unpack ? `${vo.name}_${mvo.name}` : `${vo.name}.${mvo.name}`, m.declKind === 'method' ? 'host' : m.type ?? (m.signal ? 'pure' : 'host'));
113
+ v.display = unpack ? v.name : `${vo.name}.${mvo.name}`;
114
+ v.decl = mvo.id;
115
+ v.span = mvo.nameSpan ?? mvo.span;
116
+ v.scope = scope;
117
+ if (!unpack)
118
+ v.member = { of: inst, name: mvo.name };
119
+ // A const member is a constant of the type: not in KIELER's data pool (conformance structure/157).
120
+ if (m.const)
121
+ v.tags.add('#const-member');
122
+ if (m.declKind === 'method')
123
+ v.declKind = 'method';
124
+ v.hostType = m.hostType;
125
+ scope.decls.push(v);
126
+ members.set(mvo.id, v);
127
+ }
128
+ }
129
+ this.instances.set(vo.id, members);
130
+ }
131
+ /** Lowers the members' initial values and the methods of the instances of a class declaration. */
132
+ initialiseMembers(d) {
133
+ for (const vo of d.valuedObjects) {
134
+ const members = this.instances.get(vo.id);
135
+ if (!members)
136
+ continue;
137
+ // While lowering the instance's methods, the members' names mean the instance's members.
138
+ const saved = new Map();
139
+ for (const [id, v] of members) {
140
+ saved.set(id, this.vars.get(id));
141
+ this.vars.set(id, v);
142
+ }
143
+ for (const m of d.members ?? []) {
144
+ for (const mvo of m.valuedObjects) {
145
+ const v = members.get(mvo.id);
146
+ if (!v)
147
+ continue;
148
+ if (m.declKind === 'variable') {
149
+ v.dims = mvo.cardinalities.map(c => this.expr(c));
150
+ if (mvo.initialValue)
151
+ v.init = this.expr(mvo.initialValue);
152
+ }
153
+ else if (m.declKind === 'method') {
154
+ v.method = this.method(m);
155
+ }
156
+ }
157
+ }
158
+ for (const [id, v] of saved) {
159
+ if (v)
160
+ this.vars.set(id, v);
161
+ else
162
+ this.vars.delete(id);
163
+ }
164
+ }
165
+ }
166
+ /** Second step: initial values and cardinalities (after every scope declared its variables). */
167
+ initialise(declarations) {
168
+ for (const d of declarations) {
169
+ if (d.declKind === 'class' || d.declKind === 'struct')
170
+ this.initialiseMembers(d);
171
+ for (const vo of d.valuedObjects) {
172
+ const v = this.vars.get(vo.id);
173
+ v.dims = vo.cardinalities.map(c => this.expr(c));
174
+ if (vo.initialValue && !d.const)
175
+ v.init = this.expr(vo.initialValue);
176
+ else if (vo.initialValue && d.const)
177
+ v.init = this.constValue(v);
178
+ }
179
+ if (d.declKind === 'reference' && d.referenceTarget && !d.extern && !this.enumOf(d)) {
180
+ for (const vo of d.valuedObjects) {
181
+ this.vars.get(vo.id).instance = { target: d.referenceTarget, args: this.args(vo.parameters ?? []) };
182
+ }
183
+ }
184
+ if (d.declKind === 'method' && d.valuedObjects.length) {
185
+ const v = this.vars.get(d.valuedObjects[0].id);
186
+ v.method = this.method(d);
187
+ }
188
+ }
189
+ }
190
+ /** Arguments of a reference, lowered in the referencing scope (structure.md S-6). */
191
+ args(parameters) {
192
+ return parameters.map(p => ({ expr: this.expr(p.expression), binding: p.binding, span: p.span }));
193
+ }
194
+ // ----- methods and SCL code (structure.md S-43, S-44) -----
195
+ /** Variables of a method frame or code block: registered for name resolution, but in no scope. */
196
+ frameVars(declarations, into) {
197
+ const inits = [];
198
+ for (const d of declarations) {
199
+ for (const vo of d.valuedObjects) {
200
+ const v = new CVar(vo.name, d.type ?? 'host');
201
+ v.hostType = d.hostType;
202
+ v.decl = vo.id;
203
+ v.span = vo.nameSpan ?? vo.span;
204
+ v.generated = true;
205
+ v.role = 'frame';
206
+ this.vars.set(vo.id, v);
207
+ into.push(v);
208
+ }
209
+ }
210
+ for (const d of declarations) {
211
+ for (const vo of d.valuedObjects) {
212
+ const v = this.vars.get(vo.id);
213
+ v.dims = vo.cardinalities.map(c => this.expr(c));
214
+ if (vo.initialValue)
215
+ inits.push({ v, init: this.expr(vo.initialValue), span: vo.span });
216
+ }
217
+ }
218
+ return inits;
219
+ }
220
+ method(d) {
221
+ const params = [];
222
+ const locals = [];
223
+ for (const p of d.parameters ?? [])
224
+ this.frameVars([p], params);
225
+ const by = d.valuedObjects[0]?.id;
226
+ const body = d.body ? this.block(d.body.declarations, d.body.statements, locals, by) : [];
227
+ const rt = d.returnType;
228
+ const returnType = !rt || rt === 'void' ? 'void'
229
+ : ['bool', 'int', 'float', 'string', 'pure', 'host'].find(t => t === rt) ?? 'host';
230
+ const inline = d.annotations.some(a => a.name.toLowerCase() === 'inline');
231
+ return { params, locals, body, returnType, by, span: d.span, ...(inline ? { inline } : {}) };
232
+ }
233
+ /** An SCL block: its declarations (initialisations become statements where they are written), then statements. */
234
+ block(declarations, statements, locals, by) {
235
+ const out = [];
236
+ for (const i of this.frameVars(declarations, locals)) {
237
+ out.push({ k: 'do', f: { k: 'assign', ref: { k: 'ref', v: i.v, idx: [], span: i.span }, op: '=', expr: i.init, o: { by, span: i.span, hidden: true } } });
238
+ }
239
+ for (const s of statements)
240
+ out.push(...this.statement(s, locals, by));
241
+ return out;
242
+ }
243
+ statement(s, locals, by) {
244
+ switch (s.kind) {
245
+ case 'assign':
246
+ case 'postfix':
247
+ case 'call':
248
+ case 'print':
249
+ case 'randomize':
250
+ case 'hostcode':
251
+ return [{ k: 'do', f: this.effect(s, by ?? '') }];
252
+ case 'return': return [{ k: 'return', expr: s.expression ? this.expr(s.expression) : undefined, span: s.span }];
253
+ case 'block': return this.block(s.declarations, s.statements, locals, by);
254
+ case 'if': return [{
255
+ k: 'if', cond: this.expr(s.condition), span: s.span,
256
+ then: this.block(s.declarations, s.statements, locals, by),
257
+ else: s.else ? this.block(s.else.declarations, s.else.statements, locals, by) : undefined,
258
+ }];
259
+ case 'while': return [{ k: 'while', cond: this.expr(s.condition), body: this.block(s.declarations, s.statements, locals, by), span: s.span }];
260
+ case 'for': {
261
+ let init = [];
262
+ if (s.init?.kind === 'declaration')
263
+ init = this.block([s.init], [], locals, by);
264
+ else if (s.init)
265
+ init = this.statement(s.init, locals, by);
266
+ return [{
267
+ k: 'for', init, cond: s.condition ? this.expr(s.condition) : undefined, span: s.span,
268
+ update: s.update ? this.statement(s.update, locals, by) : [],
269
+ body: this.block(s.declarations, s.statements, locals, by),
270
+ }];
271
+ }
272
+ case 'expression': return [{ k: 'expr', e: this.expr(s.expression), span: s.span }];
273
+ case 'label': return [{ k: 'label', name: s.name, span: s.span }];
274
+ case 'goto': return [{ k: 'goto', target: s.target, span: s.span }];
275
+ }
276
+ }
277
+ /**
278
+ * KIELER's SCL Code Effects processor (sccharts/processors/SclCodeEffect.xtend) turns `do { ... }` into a void
279
+ * method `_effect<n>` of the state (an action's state; for a transition, the state containing its source) and
280
+ * calls it. Done here, while lowering, because the method needs the block's own declarations.
281
+ */
282
+ codeEffect(f, by, owner) {
283
+ const v = new CVar(`_effect${this.effectCount++}`, 'host');
284
+ v.declKind = 'method';
285
+ v.generated = true;
286
+ v.span = f.span;
287
+ const locals = [];
288
+ v.method = { params: [], locals, body: this.block(f.declarations ?? [], f.statements ?? [], locals, by), returnType: 'void', by, span: f.span };
289
+ if (owner) {
290
+ v.scope = owner;
291
+ owner.decls.push(v);
292
+ }
293
+ return {
294
+ k: 'call', o: { by, span: f.span },
295
+ call: { k: 'call', name: v.name, args: [], target: { k: 'ref', v, idx: [], span: f.span }, span: f.span },
296
+ };
297
+ }
298
+ effectCount = 0;
299
+ /**
300
+ * `scchart(C).X`, a constant of another chart (KIELER's StaticAccess, processors/StaticAccess.xtend): KIELER copies
301
+ * X's declaration into the simulated chart and its Const processor inlines the copy's initial value. Only the
302
+ * declaration is copied: an initial value that names another constant of C points out of the model, which KIELER
303
+ * rejects (`static-access`, compile.ts); a subscript is dropped with the access, so an array element becomes the
304
+ * whole array (gcc rejects the vector in C).
305
+ */
306
+ staticConst(e) {
307
+ const sub = e.sub;
308
+ const entry = sub.decl ? this.index?.vos.get(sub.decl) : undefined;
309
+ const init = entry?.vo?.initialValue;
310
+ if (!entry?.decl?.const || !init)
311
+ return { k: 'unsupported', what: e.text, span: e.span };
312
+ const names = new Set();
313
+ const visit = (x) => {
314
+ if (x.kind === 'ref') {
315
+ names.add(x.name);
316
+ x.indices.forEach(visit);
317
+ }
318
+ else if (x.kind === 'op')
319
+ x.args.forEach(visit);
320
+ else if (x.kind === 'vector')
321
+ x.values.forEach(visit);
322
+ };
323
+ visit(init);
324
+ const constant = sub.name;
325
+ if (names.size) {
326
+ const first = [...names][0];
327
+ this.reports.push({
328
+ severity: 'error', code: 'static-access', span: e.span,
329
+ message: `\`${e.text}\` cannot be copied: the value of \`${constant}\` uses \`${first}\`, another name of \`${e.target}\``,
330
+ kieler: `The valued object reference points to a valued object (${first}) that is not contained in the model!\nReferences objects are not contained in the resource!`,
331
+ hint: `KIELER copies only the declaration of \`${constant}\` into this chart, and \`${first}\` stays behind. Give \`${constant}\` a value without names (\`const int ${constant} = 6\`), or declare the constant in this chart.`,
332
+ });
333
+ return { k: 'lit', t: 'int', v: 0, span: e.span };
334
+ }
335
+ if (sub.indices.length && init.kind === 'vector') {
336
+ this.reports.push({
337
+ severity: 'error', code: 'static-access', span: e.span,
338
+ message: `\`${e.text}\` does not work: KIELER drops the index and reads the whole array \`${constant}\`, which the C code cannot use here`,
339
+ kieler: `expected expression before ‘{’ token\nCompiler did not return success (exit value != 0)`,
340
+ hint: `Declare the array in this chart (\`const int ${constant}[${init.values.length}] = ...\`), or give the element a constant of its own in \`${e.target}\`.`,
341
+ });
342
+ return { k: 'lit', t: 'int', v: 0, span: e.span };
343
+ }
344
+ if (sub.indices.length || sub.sub)
345
+ return { k: 'unsupported', what: e.text, span: e.span };
346
+ const value = foldConstants(this.expr(init));
347
+ return { ...value, span: e.span };
348
+ }
349
+ /**
350
+ * KIELER's MethodSignaling (processors/MethodSignaling.xtend, run at the start of Reference): `state(S)` in a
351
+ * method body reads a flag `_<region>_<S>_active` (bool, false at first) declared in the state around S's region,
352
+ * which an entry action of S sets and an exit action of S clears (attachStateFlags).
353
+ */
354
+ stateFlag(state, text) {
355
+ let v = this.stateFlags.get(state);
356
+ if (!v) {
357
+ const source = this.index?.states.get(state);
358
+ const region = source ? this.index?.parentRegion.get(source) : undefined;
359
+ v = new CVar(`_${region?.name || 'default'}_${source?.name ?? state}_active`, 'bool');
360
+ v.generated = true;
361
+ v.display = text;
362
+ v.init = { k: 'lit', t: 'bool', v: false };
363
+ this.stateFlags.set(state, v);
364
+ }
365
+ return v;
366
+ }
367
+ /** Declares the `state(S)` flags and gives each S its entry and exit action (stateFlag). */
368
+ attachStateFlags() {
369
+ for (const [id, v] of this.stateFlags) {
370
+ const target = this.states.get(id);
371
+ const owner = target?.parent?.parent;
372
+ if (!target || !owner)
373
+ continue;
374
+ v.scope = owner;
375
+ owner.decls.push(v);
376
+ const set = (kind, value) => {
377
+ const a = new CAction(kind);
378
+ a.about = id;
379
+ a.effects.push({ k: 'assign', ref: { k: 'ref', v, idx: [] }, op: '=', expr: { k: 'lit', t: 'bool', v: value }, o: { hidden: true, by: id } });
380
+ target.actions.push(a);
381
+ };
382
+ set('entry', true);
383
+ set('exit', false);
384
+ }
385
+ }
386
+ constValue(v) {
387
+ const done = this.constValues.get(v);
388
+ if (done)
389
+ return copyExpr(done);
390
+ const source = this.constSource.get(v);
391
+ if (!source || this.inlining.has(v))
392
+ return { k: 'unsupported', what: `const ${v.name}` };
393
+ this.inlining.add(v);
394
+ const value = foldConstants(this.expr(source));
395
+ this.inlining.delete(v);
396
+ this.constValues.set(v, value);
397
+ return copyExpr(value);
398
+ }
399
+ // ----- expressions -----
400
+ expr(e) {
401
+ switch (e.kind) {
402
+ case 'bool': return { k: 'lit', t: 'bool', v: e.value, span: e.span };
403
+ case 'int': return { k: 'lit', t: 'int', v: e.value | 0, span: e.span };
404
+ // SCGTransformation: createFloatValue(Float.valueOf(value.floatValue())): every float literal is single precision.
405
+ case 'float': return { k: 'lit', t: 'float', v: Math.fround(e.value), span: e.span };
406
+ case 'string': return { k: 'lit', t: 'string', v: e.value, span: e.span };
407
+ case 'null': return { k: 'null', span: e.span };
408
+ case 'ref': return this.refExpr(e);
409
+ case 'op': return { k: 'op', op: e.op, args: e.args.map(a => this.expr(a)), span: e.span };
410
+ case 'vector': return { k: 'vector', values: e.values.map(a => this.expr(a)), range: e.range, span: e.span };
411
+ case 'random': return { k: 'random', span: e.span };
412
+ case 'hostcode': return { k: 'host', text: e.text, span: e.span };
413
+ case 'ignore': return { k: 'ignore', span: e.span };
414
+ case 'call': return {
415
+ k: 'call', name: e.name, args: e.args.map(a => this.expr(a)), extern: e.extern,
416
+ target: e.target ? this.ref(e.target) : undefined, paramAccess: e.paramAccess, span: e.span,
417
+ };
418
+ case 'special': {
419
+ // `scchart(Decl).Bit.ZERO`: an enum value of another chart is its position, as `Bit.ZERO` (S-58).
420
+ const value = e.access === 'scchart' && e.sub ? this.enumValue(e.sub) : undefined;
421
+ if (value !== undefined)
422
+ return { k: 'lit', t: 'int', v: value, span: e.span };
423
+ if (e.access === 'scchart' && e.sub)
424
+ return this.staticConst(e);
425
+ if (e.access === 'state' && e.state)
426
+ return { k: 'ref', v: this.stateFlag(e.state, e.text), idx: [], span: e.span };
427
+ return { k: 'unsupported', what: e.text, span: e.span };
428
+ }
429
+ default: return { k: 'unsupported', what: e.kind, span: e.span };
430
+ }
431
+ }
432
+ refExpr(r) {
433
+ const enumValue = this.enumValue(r);
434
+ if (enumValue !== undefined)
435
+ return { k: 'lit', t: 'int', v: enumValue, span: r.span };
436
+ const v = r.decl ? this.vars.get(r.decl) : undefined;
437
+ if (v?.const) {
438
+ let value = this.constValue(v);
439
+ for (const i of r.indices) {
440
+ const index = foldConstants(this.expr(i));
441
+ if (value.k === 'vector' && index.k === 'lit' && typeof index.v === 'number' && index.v >= 0 && index.v < value.values.length) {
442
+ value = copyExpr(value.values[index.v]);
443
+ }
444
+ else {
445
+ return { k: 'unsupported', what: `const array element ${v.name}`, span: r.span };
446
+ }
447
+ }
448
+ return { ...value, span: r.span };
449
+ }
450
+ return this.ref(r);
451
+ }
452
+ ref(r) {
453
+ // A member of a class instance (`c.x`, `c.inc()`): the instance's member variable or method.
454
+ const members = r.decl ? this.instances.get(r.decl) : undefined;
455
+ const member = members && r.sub?.decl ? members.get(r.sub.decl) : undefined;
456
+ if (member) {
457
+ const out = { k: 'ref', v: member, idx: [...r.indices, ...r.sub.indices].map(i => this.expr(i)), span: r.span };
458
+ if (r.sub.sub)
459
+ out.sub = this.ref(r.sub.sub);
460
+ return out;
461
+ }
462
+ let v = r.decl ? this.vars.get(r.decl) : undefined;
463
+ if (!v) {
464
+ // Not resolved (the front end reports it) or a member of a type the simulator does not model.
465
+ v = new CVar(r.name, 'host');
466
+ v.declKind = 'unresolved';
467
+ if (r.decl)
468
+ v.decl = r.decl;
469
+ }
470
+ const out = { k: 'ref', v, idx: r.indices.map(i => this.expr(i)), span: r.span };
471
+ if (r.sub)
472
+ out.sub = this.ref(r.sub);
473
+ return out;
474
+ }
475
+ // ----- effects -----
476
+ effect(f, by, owner) {
477
+ const o = { by, span: f.span };
478
+ switch (f.kind) {
479
+ case 'assign': return { k: 'assign', ref: this.ref(f.target), op: f.operator, expr: this.expr(f.expression), o };
480
+ case 'postfix': return { k: 'assign', ref: this.ref(f.target), op: f.operator, o };
481
+ case 'emit': return { k: 'emit', ref: this.ref(f.target), value: f.value ? this.expr(f.value) : undefined, o };
482
+ case 'print': return { k: 'print', args: f.args.map(a => this.expr(a)), o };
483
+ case 'call': return {
484
+ k: 'call', o,
485
+ call: { k: 'call', name: f.name, args: f.args.map(a => this.expr(a)), extern: f.extern, target: f.target ? this.ref(f.target) : undefined, paramAccess: f.paramAccess, span: f.span },
486
+ };
487
+ case 'randomize': return { k: 'randomize', o };
488
+ case 'hostcode': return { k: 'host', text: f.text, o };
489
+ case 'code': return this.codeEffect(f, by, owner);
490
+ }
491
+ }
492
+ // ----- structure -----
493
+ state(s, parent) {
494
+ const c = new CState(s.name);
495
+ c.label = s.label;
496
+ c.parent = parent;
497
+ c.initial = s.initial;
498
+ c.final = s.final;
499
+ c.connector = s.connector;
500
+ c.violation = s.violation;
501
+ c.root = s.isRoot;
502
+ c.origin = s.id;
503
+ if (s.reference)
504
+ c.ext.reference = s.reference;
505
+ if (s.callsReference)
506
+ c.ext.calls = true;
507
+ if (s.baseStates?.length)
508
+ c.ext.baseStates = s.baseStates;
509
+ this.states.set(s.id, c);
510
+ this.declare(c, s.declarations);
511
+ for (const r of s.regions)
512
+ c.regions.push(this.region(r, c));
513
+ for (const a of s.actions)
514
+ c.actions.push(this.action(a));
515
+ return c;
516
+ }
517
+ region(r, parent) {
518
+ const c = new CRegion();
519
+ c.name = r.name;
520
+ c.label = r.label;
521
+ c.parent = parent;
522
+ c.origin = r.id;
523
+ c.dataflow = r.regionType === 'dataflow';
524
+ c.final = !!r.final;
525
+ c.abort = !!r.abort;
526
+ c.override = !!r.override;
527
+ if (r.forLoop) {
528
+ c.ext.forLoop = r.forLoop;
529
+ // The counter (resolved to `<regionId>$<name>`, parser/ids.ts counterId) is a variable of the region
530
+ // until the For pass replaces it by a literal in each copy.
531
+ const counter = new CVar(r.forLoop.variable, 'int');
532
+ counter.declKind = 'counter';
533
+ counter.decl = `${r.id}$${r.forLoop.variable}`;
534
+ counter.span = r.forLoop.span;
535
+ counter.scope = c;
536
+ this.vars.set(counter.decl, counter);
537
+ c.ext.forCounter = counter;
538
+ }
539
+ if (r.equations.length)
540
+ c.ext.equations = r.equations;
541
+ if (r.reference)
542
+ c.ext.reference = r.reference;
543
+ if (r.once)
544
+ c.ext.once = true;
545
+ this.regions.set(r.id, c);
546
+ this.declare(c, r.declarations);
547
+ for (const s of r.states)
548
+ c.states.push(this.state(s, c));
549
+ for (const a of r.actions)
550
+ c.actions.push(this.action(a));
551
+ return c;
552
+ }
553
+ action(a) {
554
+ const c = new CAction(a.kind);
555
+ c.delay = a.delay ?? 'undefined';
556
+ if (a.preemption)
557
+ c.preemption = a.preemption;
558
+ c.weak = !!a.weak;
559
+ c.triggerDelay = a.triggerDelay;
560
+ c.origin = a.id;
561
+ c.span = a.span;
562
+ c.annotations = a.annotations.map(x => x.name);
563
+ c.src = a;
564
+ return c;
565
+ }
566
+ /** Lowers the triggers and effects of every action and transition (second step). */
567
+ contents(source, target) {
568
+ this.initialise(source.declarations);
569
+ if (source.reference)
570
+ target.ext.args = this.args(source.reference.parameters);
571
+ this.actionContents(source.actions, target.actions, target);
572
+ source.regions.forEach((r, i) => {
573
+ const cr = target.regions[i];
574
+ this.initialise(r.declarations);
575
+ if (r.forLoop) {
576
+ // structure.md S-21: `for i : a to b` (int literals) or `for i : ARRAY` (its first dimension).
577
+ cr.ext.forFrom = this.expr(r.forLoop.from);
578
+ if (r.forLoop.to)
579
+ cr.ext.forTo = this.expr(r.forLoop.to);
580
+ }
581
+ if (r.regionType === 'dataflow') {
582
+ // Equations, lowered like effects (passes/dataflow.ts turns them into control flow, structure.md S-25).
583
+ const eqs = r.equations.map(eq => ({
584
+ target: this.ref(eq.target), op: eq.operator, expr: this.expr(eq.expression),
585
+ sequential: !!eq.sequential, call: eq.callArgs !== undefined, span: eq.span,
586
+ }));
587
+ cr.ext.eqs = eqs;
588
+ }
589
+ this.actionContents(r.actions, cr.actions, target);
590
+ r.states.forEach((s, j) => this.contents(s, cr.states[j]));
591
+ });
592
+ }
593
+ actionContents(source, target, owner) {
594
+ source.forEach((a, i) => {
595
+ const c = target[i];
596
+ if (a.trigger)
597
+ c.trigger = this.expr(a.trigger);
598
+ c.effects = a.effects.map(f => this.effect(f, a.id, owner));
599
+ delete c.src;
600
+ });
601
+ }
602
+ /** Transitions, after all states exist (targets may be anywhere in the region). */
603
+ transitions(source) {
604
+ const c = this.states.get(source.id);
605
+ for (const t of source.transitions)
606
+ c.out.push(this.transition(t, c));
607
+ for (const r of source.regions)
608
+ for (const s of r.states)
609
+ this.transitions(s);
610
+ }
611
+ transition(t, source) {
612
+ const c = new CTransition();
613
+ c.source = source;
614
+ const target = t.target ? this.states.get(t.target) : undefined;
615
+ if (target)
616
+ setTarget(c, target);
617
+ else {
618
+ this.problems.push({ message: `The target ${t.targetName} of ${t.id} does not exist`, span: t.targetSpan, what: 'target' });
619
+ setTarget(c, source);
620
+ }
621
+ c.delay = t.delay ?? 'undefined';
622
+ c.preemption = t.preemption;
623
+ c.triggerDelay = t.triggerDelay;
624
+ c.history = t.history;
625
+ c.deferred = t.deferred;
626
+ c.origin = t.id;
627
+ c.span = t.span;
628
+ if (t.trigger)
629
+ c.trigger = this.expr(t.trigger);
630
+ c.effects = t.effects.map(f => this.effect(f, t.id, source.parent?.parent ?? source));
631
+ return c;
632
+ }
633
+ }
634
+ /** Lowers the scchart `chart` (with the resolved index from compile()). */
635
+ export function lowerChart(chart, index) {
636
+ const l = new Lowering(index);
637
+ const root = l.state(chart, undefined);
638
+ root.root = true;
639
+ l.contents(chart, root);
640
+ l.transitions(chart);
641
+ l.attachStateFlags();
642
+ // Constant folding where KIELER's Const processor evaluates expressions (scopes that declare constants).
643
+ for (const scope of l.constScopes)
644
+ foldScope(scope);
645
+ // Marks for source local actions (first effect; only actions that have effects).
646
+ for (const s of l.states.values())
647
+ markActions(s);
648
+ for (const r of l.regions.values())
649
+ markActions(r);
650
+ // Taken Transition Signaling, on the source model: pre-order over the states below the root.
651
+ const takenOrder = [];
652
+ const visit = (s, include) => {
653
+ if (include) {
654
+ for (const t of s.transitions) {
655
+ const ct = l.states.get(s.id).out.find(x => x.origin === t.id);
656
+ if (ct)
657
+ ct.effects.unshift({ k: 'mark', m: { kind: 'take', transition: t.id, index: takenOrder.length }, o: { by: t.id, hidden: true } });
658
+ takenOrder.push(t.id);
659
+ }
660
+ }
661
+ for (const r of s.regions)
662
+ for (const c of r.states)
663
+ visit(c, true);
664
+ };
665
+ visit(chart, false);
666
+ if (takenOrder.length > 0) {
667
+ // USE_VALUE_CHANGE_SIGNALING: the counters are reset by a root entry action (appended to the root's actions).
668
+ const reset = new CAction('entry');
669
+ reset.effects.push({ k: 'mark', m: { kind: 'ttsReset' }, o: { hidden: true } });
670
+ root.actions.push(reset);
671
+ }
672
+ return { root, vars: l.vars, states: l.states, regions: l.regions, takenOrder, problems: l.problems, reports: l.reports };
673
+ }
674
+ function markActions(scope) {
675
+ for (const a of scope.actions) {
676
+ if (a.origin && a.effects.length > 0)
677
+ a.effects.unshift({ k: 'mark', m: { kind: 'action', action: a.origin }, o: { by: a.origin, hidden: true } });
678
+ }
679
+ }
680
+ function foldScope(scope) {
681
+ const visitScope = (s) => {
682
+ for (const v of s.decls) {
683
+ if (v.init)
684
+ v.init = foldConstants(v.init);
685
+ v.dims = v.dims.map(foldConstants);
686
+ }
687
+ for (const a of s.actions) {
688
+ if (a.trigger)
689
+ a.trigger = foldConstants(a.trigger);
690
+ a.effects = a.effects.map(foldEffect);
691
+ }
692
+ if (s instanceof CState) {
693
+ for (const t of s.out) {
694
+ if (t.trigger)
695
+ t.trigger = foldConstants(t.trigger);
696
+ t.effects = t.effects.map(foldEffect);
697
+ }
698
+ for (const r of s.regions)
699
+ visitScope(r);
700
+ }
701
+ else {
702
+ for (const c of s.states)
703
+ visitScope(c);
704
+ }
705
+ };
706
+ visitScope(scope);
707
+ }
708
+ function foldEffect(f) {
709
+ switch (f.k) {
710
+ case 'assign': return { ...f, ref: { ...f.ref, idx: f.ref.idx.map(foldConstants) }, expr: f.expr ? foldConstants(f.expr) : undefined };
711
+ case 'emit': return { ...f, ref: { ...f.ref, idx: f.ref.idx.map(foldConstants) }, value: f.value ? foldConstants(f.value) : undefined };
712
+ case 'print': return { ...f, args: f.args.map(foldConstants) };
713
+ default: return f;
714
+ }
715
+ }
716
+ //# sourceMappingURL=lower.js.map