-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathTermWriter.scala
More file actions
166 lines (142 loc) · 5.52 KB
/
Copy pathTermWriter.scala
File metadata and controls
166 lines (142 loc) · 5.52 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2011-2019 ETH Zurich.
package viper.silicon.logger.writer
import spray.json.{JsArray, JsNull, JsObject, JsString, JsValue}
import viper.silicon.state.Identifier
import viper.silicon.state.terms._
/**
*
*/
object TermWriter {
private def binary(op: String, lhs: JsValue, rhs: JsValue): JsValue =
JsObject(
"type" -> JsString("binary"),
"op" -> JsString(op),
"lhs" -> lhs,
"rhs" -> rhs
)
private def unary(op: String, p: JsValue) =
JsObject(
"type" -> JsString("unary"),
"op" -> JsString(op),
"p" -> p
)
private def variable(id: Identifier, sort: Sort) =
JsObject(
"type" -> JsString("variable"),
"id" -> JsString(id.name),
"sort" -> toJSON(sort)
)
def toJSON(sort: Sort): JsValue = {
def s(name: String, elementsSort: Sort) = JsObject(
"id" -> JsString(name),
"elementsSort" -> toJSON(elementsSort)
)
sort match {
case sorts.Unit => JsObject("id" -> JsString("Unit"))
case sorts.Seq(elementsSort) => s("Seq", elementsSort)
case sorts.Set(elementsSort) => s("Set", elementsSort)
case sorts.Multiset(elementsSort) => s("Multiset", elementsSort)
case sorts.UserSort(id) => JsObject("id" -> JsString("UserSort"), "name" -> JsString(id.name))
case sorts.FieldValueFunction(codomainSort, fieldName) => JsObject("id" -> JsString("FVF"),
"fieldName" -> JsString(fieldName), "elementSort" -> toJSON(codomainSort))
case sorts.PredicateSnapFunction(codomainSort, predName) => JsObject("id" -> JsString("PSF"),
"predName" -> JsString(predName), "elementSort" -> toJSON(codomainSort))
case simple => JsObject("id" -> JsString(simple.id.name))
}
}
def toJSON(term: Term): JsValue = term match {
case b: BinaryOp[Term@unchecked] => binary(b.op, toJSON(b.p0), toJSON(b.p1))
case u: UnaryOp[Term@unchecked] => unary(u.op, toJSON(u.p))
// TODO: do we need triggers and isGlobal?
case Quantification(quantifier, vars, body, _, name, _, _) =>
JsObject(
"type" -> JsString("quantification"),
"quantifier" -> JsString(quantifier.toString),
"vars" -> JsArray((vars map toJSON).toVector),
"body" -> toJSON(body),
"name" -> (if (name != null) JsString(name) else JsNull)
)
case a @ App(applicable, args) =>
JsObject(
"type" -> JsString("application"),
"applicable" -> JsString(applicable.id.name),
"args" -> JsArray((args map toJSON).toVector),
"sort" -> toJSON(a.sort)
)
case Lookup(field, fieldValueFunction, receiver) =>
JsObject(
"type" -> JsString("lookup"),
"field" -> JsString(field),
"fieldValueFunction" -> toJSON(fieldValueFunction),
"receiver" -> toJSON(receiver)
)
case PredicateLookup(predicate, predicateSnapFunction, args) =>
JsObject(
"type" -> JsString("predicateLookup"),
"predicate" -> JsString(predicate),
"predicateSnapFunction" -> toJSON(predicateSnapFunction),
"args" -> JsArray((args map toJSON).toVector)
)
case And(terms) => JsObject("type" -> JsString("and"), "terms" -> JsArray((terms map toJSON).toVector))
case Or(terms) => JsObject("type" -> JsString("or"), "terms" -> JsArray((terms map toJSON).toVector))
case Distinct(symbols) =>
JsObject(
"type" -> JsString("distinct"),
"symbols" -> JsArray((symbols map (s => JsString(s.id.name))).toVector)
)
case Ite(cond, thenBranch, elseBranch) =>
JsObject(
"type" -> JsString("ite"),
"cond" -> toJSON(cond),
"thenBranch" -> toJSON(thenBranch),
"elseBranch" -> toJSON(elseBranch)
)
case Var(id, sort) => variable(id, sort)
case SortWrapper(t, sort) =>
JsObject(
"type" -> JsString("sortWrapper"),
"term" -> toJSON(t),
"sort" -> toJSON(sort)
)
case Let(bindings, body) =>
val bs = bindings map { case (v, t) => JsObject("var" -> toJSON(v), "value" -> toJSON(t)) }
JsObject(
"type" -> JsString("let"),
"bindings" -> JsArray(bs.toVector),
"body" -> toJSON(body)
)
case l: Literal =>
JsObject(
"type" -> JsString("literal"),
"sort" -> toJSON(l.sort),
"value" -> JsString(l.toString)
)
// PermLiteral is not actually a Literal. This case can actually be reached
// and we map PermLiterals to normal literals.
case p: PermLiteral =>
JsObject(
"type" -> JsString("literal"),
"sort" -> toJSON(sorts.Perm),
"value" -> JsString(p.toString)
)
case SeqRanged(p0, p1) => JsObject("type" -> JsString("seqRanged"), "lhs" -> toJSON(p0), "rhs" -> toJSON(p1))
case SeqSingleton(p) => JsObject("type" -> JsString("seqSingleton"), "value" -> toJSON(p))
case SeqUpdate(seq, index, value) => JsObject(
"type" -> JsString("seqUpdate"),
"seq" -> toJSON(seq),
"index" -> toJSON(index),
"value" -> toJSON(value)
)
case SingletonSet(p) => JsObject("type" -> JsString("singletonSet"), "value" -> toJSON(p))
case SingletonMultiset(p) => JsObject("type" -> JsString("singletonMultiset"), "value" -> toJSON(p))
// TODO: What about domains?
case other => JsObject(
"type" -> JsString("unstructrured"),
"value" -> JsString(other.toString)
)
}
}