-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExprToString.scala
214 lines (187 loc) · 7.77 KB
/
ExprToString.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package edg.compiler
import edgir.expr.expr
import edgir.lit.lit
import edgir.ref.ref
import edg.wir.{DesignPath, IndirectDesignPath}
import scala.collection.Set
/**
* Convenience wrapper around ExprToString without needing to construct a new object each time.
*/
object ExprToString {
private val instance = new ExprToString()
def apply(item: expr.ValueExpr): String = instance.map(item)
def apply(item: ref.LocalPath): String = instance.mapRef(item)
def apply(item: lit.ValueLit): String = instance.mapLiteral(item)
}
/**
* Converts an Expr to a human-readable string
*/
class ExprToString() extends ValueExprMap[String] {
override def mapLiteral(literal: lit.ValueLit): String = literal.`type` match {
case lit.ValueLit.Type.Floating(literal) => literal.`val`.toString
case lit.ValueLit.Type.Integer(literal) => literal.`val`.toString
case lit.ValueLit.Type.Boolean(literal) => literal.`val`.toString
case lit.ValueLit.Type.Text(literal) => literal.`val`
case lit.ValueLit.Type.Range(literal) => s"(${mapLiteral(literal.getMinimum)}, ${mapLiteral(literal.getMaximum)})"
case lit.ValueLit.Type.Array(array) =>
val arrayElts = array.elts.map(mapLiteral)
s"[${arrayElts.mkString(", ")}]"
case lit.ValueLit.Type.Struct(value) => "unsupported struct"
case lit.ValueLit.Type.Empty => "(empty)"
}
private object BinaryExprOp {
import expr.BinaryExpr.Op
object InfixOp {
def unapply(op: Op): Option[String] = op match {
case Op.ADD => Some("+")
case Op.MULT => Some("×")
case Op.AND => Some("&&")
case Op.OR => Some("||")
case Op.XOR => Some("^")
case Op.IMPLIES => Some("⇒")
case Op.EQ => Some("==")
case Op.NEQ => Some("≠")
case Op.GT => Some(">")
case Op.GTE => Some("≥")
case Op.LT => Some("<")
case Op.LTE => Some("≤")
case Op.MAX | Op.MIN => None
case Op.INTERSECTION => Some("∩")
case Op.HULL => Some("h∪")
case Op.WITHIN => Some("⊆")
case Op.RANGE => None
case Op.UNDEFINED | Op.Unrecognized(_) => None
}
}
object PrefixOp {
def unapply(op: Op): Option[String] = op match {
case Op.ADD | Op.MULT => None
case Op.AND | Op.OR | Op.XOR | Op.IMPLIES | Op.EQ | Op.NEQ => None
case Op.GT | Op.GTE | Op.LT | Op.LTE => None
case Op.MAX => Some("max")
case Op.MIN => Some("min")
case Op.INTERSECTION | Op.HULL | Op.WITHIN => None
case Op.RANGE => Some("range")
case Op.UNDEFINED | Op.Unrecognized(_) => None
}
}
}
override def mapBinary(binary: expr.BinaryExpr,
lhs: String, rhs: String): String = binary.op match {
case BinaryExprOp.InfixOp(op) => s"($lhs $op $rhs)"
case BinaryExprOp.PrefixOp(op) => s"$op($lhs, $rhs)"
case op => s"unknown[$op]($lhs, $rhs)"
}
private object BinarySetExprOp {
import expr.BinarySetExpr.Op
object InfixOp {
def unapply(op: Op): Option[String] = op match {
case Op.ADD => Some("+")
case Op.MULT => Some("×")
case Op.UNDEFINED | Op.Unrecognized(_) => None
}
}
object PrefixOp {
def unapply(op: Op): Option[String] = op match {
case Op.ADD | Op.MULT => None
case Op.UNDEFINED | Op.Unrecognized(_) => None
}
}
}
override def mapBinarySet(binarySet: expr.BinarySetExpr,
lhsset: String, rhs: String): String = binarySet.op match {
case BinarySetExprOp.InfixOp(op) => s"($lhsset $op $rhs)"
case BinarySetExprOp.PrefixOp(op) => s"$op($lhsset, $rhs)"
case op => s"unknown[$op]($lhsset, $rhs)"
}
private object UnaryExprOp {
import expr.UnaryExpr.Op
def unapply(op: Op): Option[String] = op match {
case Op.NEGATE => Some("negate")
case Op.NOT => Some("not")
case Op.INVERT => Some("invert")
case Op.MIN => Some("min")
case Op.MAX => Some("max")
case Op.CENTER => Some("center")
case Op.WIDTH => Some("width")
case Op.UNDEFINED | Op.Unrecognized(_) => None
}
}
override def mapUnary(unary: expr.UnaryExpr, `val`: String): String = unary.op match {
case UnaryExprOp(op) => s"$op(${`val`})"
case op => s"unknown[$op](${`val`})"
}
private object UnarySetExprOp {
import expr.UnarySetExpr.Op
def unapply(op: Op): Option[String] = op match {
case Op.SUM => Some("sum")
case Op.ALL_TRUE => Some("allTrue")
case Op.ANY_TRUE => Some("anyTrue")
case Op.ALL_EQ => Some("allEqual")
case Op.ALL_UNIQUE => Some("allUnique")
case Op.MAXIMUM => Some("max")
case Op.MINIMUM => Some("min")
case Op.SET_EXTRACT => Some("setExtract")
case Op.INTERSECTION => Some("intersection")
case Op.HULL => Some("hull")
case Op.NEGATE => Some("negate")
case Op.INVERT => Some("invert")
case Op.UNDEFINED | Op.Unrecognized(_) => None
}
}
override def mapUnarySet(unarySet: expr.UnarySetExpr, vals: String): String = unarySet.op match {
case UnarySetExprOp(op) => s"$op(${vals})"
case op => s"unknown[$op](${vals})"
}
override def mapArray(array: expr.ArrayExpr, vals: Seq[String]): String = {
s"array(${vals.mkString(", ")})"
}
override def mapStruct(struct: expr.StructExpr, vals: Map[String, String]): String = {
val valsStr = vals.map { case (k, v) => s"$k: $v" }
s"struct(${valsStr.mkString(", ")})"
}
override def mapRange(range: expr.RangeExpr,
minimum: String, maximum: String): String = {
s"range($minimum, $maximum)"
}
override def mapIfThenElse(ite: expr.IfThenElseExpr, cond: String,
tru: String, fal: String): String = {
s"($cond? $tru : $fal)"
}
override def mapExtract(extract: expr.ExtractExpr,
container: String, index: String): String = {
s"$container[$index]"
}
override def mapMapExtract(mapExtract: expr.MapExtractExpr): String = {
s"${map(mapExtract.getContainer)}[∀ ${mapRef(mapExtract.getPath)}]"
}
override def mapConnected(connected: expr.ConnectedExpr, blockPort: String, linkPort: String): String = {
s"connected($blockPort, $linkPort)"
}
override def mapExported(exported: expr.ExportedExpr, exteriorPort: String, internalBlockPort: String): String = {
s"exported($exteriorPort, $internalBlockPort)"
}
override def mapExportedTunnel(exported: expr.ExportedExpr, exteriorPort: String, internalBlockPort: String): String = {
s"exportedTunnel($exteriorPort, $internalBlockPort)"
}
override def mapAssign(assign: expr.AssignExpr, src: String): String =
s"${mapRef(assign.getDst)} ⇐ $src"
override def mapAssignTunnel(assign: expr.AssignExpr, src: String): String =
s"${mapRef(assign.getDst)} ⇐() $src"
override def mapRef(path: ref.LocalPath): String = {
path.steps.map { _.step match {
case ref.LocalStep.Step.Name(name) => name
case ref.LocalStep.Step.Allocate("") => "(allocate)"
case ref.LocalStep.Step.Allocate(suggestedName) => s"(allocate: $suggestedName)"
case ref.LocalStep.Step.Empty => "(empty)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.UNDEFINED) => "(undefined)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.CONNECTED_LINK) => "(connectedLink)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.IS_CONNECTED) => "(isConnected)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.LENGTH) => "(length)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.ELEMENTS) => "(elements)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.ALLOCATED) => "(allocated)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.NAME) => "(name)"
case ref.LocalStep.Step.ReservedParam(ref.Reserved.Unrecognized(op)) => s"(unrecognized[$op])"
} }.mkString(".")
}
}