1+ package org.lflang.ir
2+
3+ import org.lflang.allPreambles
4+ import org.lflang.allStateVars
5+ import org.lflang.generator.LocationInfo
6+ import org.lflang.generator.locationInfo
7+ import org.lflang.generator.uc.UcParameterGenerator.Companion.targetType
8+ import org.lflang.generator.uc.name
9+ import org.lflang.indexInContainer
10+ import org.lflang.isOfTimeType
11+ import org.lflang.lf.Action
12+ import org.lflang.lf.Code
13+ import org.lflang.lf.Connection
14+ import org.lflang.lf.Initializer
15+ import org.lflang.lf.Input
16+ import org.lflang.lf.Parameter
17+ import org.lflang.lf.Preamble
18+ import org.lflang.lf.Reaction
19+ import org.lflang.lf.Reactor
20+ import org.lflang.lf.StateVar
21+ import org.lflang.lf.Time
22+ import org.lflang.lf.Timer
23+ import org.lflang.lf.TriggerRef
24+ import org.lflang.lf.VarRef
25+ import org.lflang.toText
26+ import javax.management.ConstructorParameters
27+ import kotlin.coroutines.Continuation
28+ import kotlin.time.Duration.Companion.nanoseconds
29+
30+
31+ fun fromAst (time : Time ) : TimeValue = TimeValue (time.interval.nanoseconds)
32+
33+ fun Action.toIR () : org.lflang.ir.Action = Action (this .name)
34+
35+ fun Timer.toIR () : org.lflang.ir.Timer = Timer (
36+ lfName = this .name,
37+ offset = fromAst(this .offset as Time ),
38+ period = fromAst(this .period as Time )
39+ )
40+
41+ fun Preamble.toIR () : TargetCode = TargetCode (code= this .code.toString())
42+ fun Code.toIR () : TargetCode = TargetCode (code= this .body.toString())
43+ fun String.toIR (): TargetCode = TargetCode (code= this )
44+
45+ fun Parameter.toIR () : org.lflang.ir.ConstructorParameters = ConstructorParameters (
46+ lfName = this .name,
47+ type= TargetCode (code = this .targetType.toString()),
48+ defaultValue = this .init .toText().toIR(),
49+ isTime = this .isOfTimeType,
50+ defaultValueAsTimeValue = null , // TODO: FIX ME
51+ )
52+
53+ fun Initializer.toIR () : TargetCode = TargetCode (code= this .toString())
54+
55+ fun StateVar.toIR () : StateVariable = StateVariable (
56+ lfName = this .name,
57+ type = TargetCode (code = this .type.toString()),
58+ init = this .init .toIR()
59+ )
60+
61+ fun TriggerRef.toIR () : org.lflang.ir.TriggerRef = TriggerRef (
62+ name = this .name
63+ )
64+
65+ fun LocationInfo.toIR (): LocationInformation = LocationInformation (
66+ file = this .fileName,
67+ line = this .line,
68+ column = this .column
69+ )
70+
71+ fun Reaction.toIR () : org.lflang.ir.Reaction = Reaction (
72+ idx = this .indexInContainer,
73+ body = this .code.toIR(),
74+ triggers = this .triggers.map { it.toIR() },
75+ uses = this .sources.map { it.toIR() },
76+ effects = this .effects.map { it.toIR() },
77+ loc = this .locationInfo().toIR(),
78+ )
79+
80+ fun VarRef.toIR () : PortRef = PortRef (
81+ name = this .name
82+ )
83+
84+ fun Connection.toIR () : List <org.lflang.ir.Connection > {
85+ var conns = mutableListOf< org.lflang.ir.Connection > ();
86+ for (left in this .leftPorts) {
87+ for (right in this .rightPorts) {
88+ conns.add(Connection (
89+ source = left.toIR(),
90+ target = right.toIR(),
91+ kind = if (this .isPhysical) ConnectionKind .PHYSICAL_CONNECTION else (if (this .delay == null ) ConnectionKind .CONNECTION else ConnectionKind .DELAYED_CONNECTION ),
92+ ))
93+ }
94+ }
95+ return conns;
96+ }
97+
98+ fun Input.toIR () : InputPort = InputPort (
99+ lfName = this .name,
100+ dataType = this .type.toIR(),
101+ incomingConnections = this .
102+ )
103+
104+
105+ fun Reactor.toIR () : org.lflang.ir.Reactor = Reactor (
106+ lfName = this .name,
107+ fullyQualifiedName = this .name,
108+ isMain = this .isMain,
109+ preambles = this .allPreambles.map { it.toIR() },
110+ ctorParams = this .parameters.map { it.toIR() },
111+ stateVars = this .allStateVars.map { it.toIR() },
112+ triggers = this .actions.map { it.toIR() } + this .timers.map { it.toIR() } + this .inputs.map { it.toIR() },
113+ ports = TODO (),
114+ reactions = this .reactions.map { it.toIR() },
115+ childReactors = TODO (),
116+ connections = this .connections.fold(mutableListOf< org.lflang.ir.Connection > ()) { (acc, it) -> acc.addAll(it.toIR()); acc },
117+ location = this .locationInfo().toIR(),
118+ )
0 commit comments