-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypecheck.effekt
More file actions
234 lines (223 loc) · 6.77 KB
/
typecheck.effekt
File metadata and controls
234 lines (223 loc) · 6.77 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
module typecheck
import pylang/interp
import pylang/pyconv
import pylang/syntax
import args
import io/error
import io/filesystem
import json
import map
import set
import scanner
type Type {
TInt()
TBool()
TStr()
TUnit()
TError()
}
def toStr(t: Type) = t match {
case TInt() => "TInt"
case TBool() => "TBool"
case TStr() => "TStr"
case TUnit() => "TUnit"
case TError() => "TError"
}
def println(ts: Set[Type]) = {
ts.foreach { t => println(" " ++ toStr(t)) }
}
def typeEquals(t1: Type, t2: Type): Bool = {
(t1, t2) match {
case (TInt(), TInt()) => true
case (TBool(), TBool()) => true
case (TStr(), TStr()) => true
case (TUnit(), TUnit()) => true
case (TError(), TError()) => true
case _ => false
}
}
def canCompare(t1: Type, t2: Type): Bool = {
(t1, t2) match {
case (TInt(), TInt()) => true
case (TInt(), TBool()) => true
case (TBool(), TInt()) => true
case (TBool(), TBool()) => true
case (TStr(), TStr()) => true
case _ => println("Error Comparing: " ++ toStr(t1) ++ ", " ++ toStr(t2)); false
}
}
def mergeStates(st1: State[Set[Type]], st2: State[Set[Type]]): State[Set[Type]] = {
val m1 = st1 match { case St(a) => a }
val m2 = st2 match { case St(a) => a }
var merged = m1
map::foreach(m2) { (k, newTypes) =>
val existing = map::get[String, Set[Type]](merged, k) match {
case Some(types) => types
case None() => set::emptyGeneric()
}
val updated = set::union(existing, newTypes)
merged = merged.put(k, updated)
}
St(merged)
St(merged)
}
def run(prog: Prog, init: State[Set[Type]]): (Set[Type], State[Set[Type]]) = {
try {
with eval_forwards[Set[Type], State[Set[Type]]]
eval((prog, init))
}
with cstI[Set[Type], State[Set[Type]]] {(st, v) =>
val ts = v match {
case I(_) => [TInt()]
case B(_) => [TBool()]
case Str(_) => [TStr()]
case U() => [TUnit()]
}
resume((set::fromListGeneric(ts), st))
}
with varI[Set[Type], State[Set[Type]]] { (st, x) =>
val m: Map[String, Set[Type]] = st match {case St(a) => a}
val ts = map::get[String, Set[Type]](m, x) match {
case Some(types) => types
case None() => set::fromListGeneric([TError()])
}
resume((ts, st))
}
with asgnI[Set[Type], State[Set[Type]]] { (st, x, ts) =>
val m = st match {case St(a) => a}
val newState = m.put(x, ts)
resume((set::fromListGeneric([TUnit()]), St(newState)))
}
with binopI[Set[Type], State[Set[Type]]] { (st, op, ts1, ts2) =>
op match {
case PlusOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
(t1, t2) match {
case (TInt(), TInt()) => TInt()
case (TInt(), TBool()) => TInt()
case (TBool(), TInt()) => TInt()
case (TStr(), TStr()) => TStr()
case _ => println("Error Adding: " ++ toStr(t1) ++ ", " ++ toStr(t2)); TError()
}
}
}
resume((set::fromListGeneric(result), st))
case MinusOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
(t1, t2) match {
case (TInt(), TInt()) => TInt()
case (TInt(), TBool()) => TInt()
case (TBool(), TInt()) => TInt()
case _ => println("Error Subtracting: " ++ toStr(t1) ++ ", " ++ toStr(t2)); TError()
}
}
}
resume((set::fromListGeneric(result), st))
case EqOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
(t1, t2) match {
case (TInt(), TInt()) => TBool()
case (TBool(), TBool()) => TBool()
case (TStr(), TStr()) => TBool()
case _ => println("Error Comparing: " ++ toStr(t1) ++ ", " ++ toStr(t2)); TError()
}
}
}
resume((set::fromListGeneric(result), st))
case LtOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
if (canCompare(t1, t2)) TBool() else TError()
}
}
resume((set::fromListGeneric(result), st))
case GtOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
if (canCompare(t1, t2)) TBool() else TError()
}
}
resume((set::fromListGeneric(result), st))
case LeOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
if (canCompare(t1, t2)) TBool() else TError()
}
}
resume((set::fromListGeneric(result), st))
case GeOp() =>
val result = ts1.toList.flatMap { t1 =>
ts2.toList.map { t2 =>
if (canCompare(t1, t2)) TBool() else TError()
}
}
resume((set::fromListGeneric(result), st))
}
}
with unopI[Set[Type], State[Set[Type]]] { (st, op, ts) =>
op match {
case NotOp() =>
val result = ts.toList.map { t =>
t match {
case TBool() => TBool()
case TInt() => TBool()
case TStr() => TBool()
case _ => TError()
}
}
resume((set::fromListGeneric(result), st))
}
}
with ifI[Set[Type], State[Set[Type]]] {(stT, stF, ts, tsT, tsF) =>
val result: Set[Type] = set::union(tsT, tsF)
val merged = mergeStates(stT, stF)
resume((result, merged))
}
with whileI[Set[Type], State[Set[Type]]] {(stG, stB, ts, tsB) =>
val merged = mergeStates(stG, stB)
resume((set::fromListGeneric([TUnit()]), merged))
}
with recurse[Set[Type]] {(ts) =>
resume(false)
}
with branch[Set[Type]] {(ts) =>
resume(-1)
}
with seqI[Set[Type], State[Set[Type]]] {(_, st2, _, ts2) => resume((ts2, st2))}
with skipI[Set[Type], State[Set[Type]]] {(st) => resume((set::fromListGeneric([TUnit()]), st))}
}
def main() = {
with on[WrongFormat].panic
with on[IOError].panic
val init: Map[String, Set[Type]] = map::empty(box compareStringBytes)
// parse files to evaluate
val file: List[String] = args::commandLineArgs()
file match {
case Cons(_, _) => {
// for each file passed in
file.foreach{ (f) =>
// read json ast
val jsonFile: String = filesystem::readFile(f)
feed(jsonFile) {
with scanner[Char]
// decode into effekt
val (_, jsonVal) = build { decodeJson() }
// parse to Prog
val parsed = pythonAstToProg(jsonVal)
// evaluate
val (_, s) = run(parsed, St(init))
// extract end state
val m = s match {case St(a) => a}
// print output
println("---------")
m.foreach { (k,v) => println(k ++ " : {"); println(v); println("}")}
println("---------")
}
}
}
case Nil() => println("no file passed in")
}
}