Skip to content

Commit 553126f

Browse files
committed
adding classes for IR
1 parent 63ed0d1 commit 553126f

File tree

9 files changed

+296
-0
lines changed

9 files changed

+296
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.lflang.ir
2+
3+
enum class ConnectionKind {
4+
CONNECTION,
5+
DELAYED_CONNECTION,
6+
PHYSICAL_CONNECTION,
7+
FEDERATED_CONNECTION,
8+
}
9+
10+
data class Connection(
11+
val source: PortRef,
12+
val target: PortRef,
13+
val kind: ConnectionKind
14+
)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.lflang.ir
2+
3+
data class LocationInformation(val file: String, val line: Int, val column: Int)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.lflang.ir
2+
3+
abstract class Port : Trigger() {
4+
abstract val dataType: TargetCode
5+
}
6+
7+
data class PortRef(val name: String)
8+
9+
class InputPort(
10+
override val lfName: String,
11+
override val kind: TriggerKind = TriggerKind.INPUT,
12+
override val dataType: TargetCode,
13+
val incomingConnections: List<Connection>
14+
) : Port() {
15+
16+
}
17+
18+
class OutputPort(
19+
override val lfName: String,
20+
override val kind: TriggerKind = TriggerKind.OUTPUT,
21+
override val dataType: TargetCode,
22+
val outgoingConnections: List<Connection>
23+
) : Port() {
24+
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.lflang.ir
2+
3+
data class Reaction(
4+
/** Index in the containing reactor. */
5+
val idx: Int,
6+
/** Target code for the reaction body. */
7+
val body: TargetCode,
8+
9+
val triggers: List<TriggerRef>,
10+
val uses: List<TriggerRef>,
11+
val effects: List<TriggerRef>,
12+
13+
/** Location metadata. */
14+
val loc: LocationInformation,
15+
) {
16+
17+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.lflang.ir
2+
3+
data class TargetCode(val code: String)
4+
5+
data class StateVariable(
6+
/**
7+
* Identifier of the state var. From within a reaction
8+
* body, the state var is accessible as a field with type
9+
* [type] declared on the `self` struct.
10+
*/
11+
val lfName: String,
12+
/** Rust static type of the struct field. Must be `Sized`. */
13+
val type: TargetCode,
14+
/** The field initializer, a Rust expression. */
15+
val init: TargetCode
16+
)
17+
18+
data class ConstructorParameters(
19+
val lfName: String,
20+
val type: TargetCode,
21+
val defaultValue: TargetCode?,
22+
val isTime: Boolean,
23+
val defaultValueAsTimeValue: TimeValue?,
24+
)
25+
26+
27+
class Reactor (
28+
/** Name of the reactor in LF. By LF conventions, this is a PascalCase identifier. */
29+
val lfName: String,
30+
31+
val fullyQualifiedName: String,
32+
33+
/** Whether this is the main reactor. */
34+
val isMain: Boolean,
35+
36+
/** List of preambles, will be outputted at the top of the file. */
37+
val preambles: List<TargetCode>,
38+
39+
/** List of ctor parameters. */
40+
val ctorParams: List<ConstructorParameters>,
41+
/** List of state variables. */
42+
val stateVars: List<StateVariable>,
43+
44+
/** Other reactor components, like actions, timers, port references, ports. */
45+
val triggers: Set<Trigger>,
46+
// TODO: maybe split this into Input and Output ports
47+
val ports: List<Port>,
48+
49+
/** A list of reactions, in order of their [ReactionInfo.idx]. */
50+
val reactions: List<Reaction>,
51+
52+
//val typeParamList: List<TypeParamInfo>,
53+
54+
/**
55+
* List of reactor instances that are directly created by this reactor.
56+
* Each of them is a named item accessible from the assemble method (well,
57+
* their ports are anyway).
58+
*/
59+
val childReactors: List<Reactor>,
60+
61+
/**
62+
* List of connections between ports that are made within
63+
* the body of this reactor instance.
64+
*/
65+
val connections: List<Connection>,
66+
val location: LocationInformation,
67+
68+
) {
69+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.lflang.ir
2+
3+
interface TargetType {
4+
fun toTargetCode(): String
5+
}
6+
7+
8+
class StringType : TargetType {
9+
override fun toTargetCode(): String = "char*"
10+
}
11+
12+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.lflang.ir
2+
3+
import kotlin.time.Duration
4+
5+
data class TimeValue(val timeInNanoseconds: Duration)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.lflang.ir
2+
3+
4+
enum class TriggerKind {
5+
TIMER,
6+
ACTION,
7+
INPUT,
8+
OUTPUT,
9+
STARTUP,
10+
SHUTDOWN
11+
}
12+
13+
data class TriggerRef(val name: String);
14+
15+
abstract class Trigger {
16+
abstract val lfName: String
17+
abstract val kind: TriggerKind
18+
}
19+
20+
class Timer(
21+
override val lfName: String,
22+
override val kind: TriggerKind = TriggerKind.TIMER,
23+
val offset: TimeValue,
24+
val period: TimeValue,
25+
) : Trigger() {
26+
27+
}
28+
29+
class Action(
30+
override val lfName: String,
31+
override val kind: TriggerKind = TriggerKind.ACTION,
32+
) : Trigger() {
33+
}

0 commit comments

Comments
 (0)