-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepl.scala
214 lines (181 loc) · 5.48 KB
/
Repl.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 bisquit
package repl
import errors._
import nodes._
import parser._
import prelude._
import printer._
import runtime._
import scope._
import typechecker._
import java.io._
import scala.sys.process._
import scala.language.postfixOps
import scala.language.implicitConversions
enum Mode {
case Parse
case Ir
case Type
case Eval
}
class Repl(
var scope: Scope = Map(),
in: InputStream = System.in,
out: PrintStream = System.out,
) {
val promptPrefix = ""
val promptStart = s"${promptPrefix}> "
val promptCont = s"${" " * promptPrefix.size}| "
val input = new InputStreamReader(in)
val reader = new BufferedReader(input)
val buff = new StringBuilder
val fileName = "<repl>"
var modules: Modules = Prelude
var newLine: Boolean = false
val version = ("git log -1 --format='%H'" !!).trim()
val initCommand = "import Prelude exposing (...)"
def run(): Unit =
out.println(s"Bisquit version $version")
out.println("Run 'exit' to exit.")
out.println(s"\n$promptStart$initCommand")
process(initCommand)
while (true) {
if newLine
then out.print("\n")
newLine = false
if buff.isEmpty
then out.print(promptStart)
buff.append(reader.readLine()).toString match {
case "" =>
case "exit" => return
case code =>
if process(code)
then buff.clear
else buff.append("\n")
}
}
def process(origCode: String): Boolean =
mode(origCode) match {
case (Mode.Parse, code) =>
parseIt(code) { expr =>
out.println(expr)
newLine = true
}
case (Mode.Ir, code) =>
parseIt(code) {
case expr: Expression =>
out.println(pass1(expr))
newLine = true
case stmt: Statement =>
out.println(stmt)
newLine = true
}
case (Mode.Type, code) =>
parseIt(code) {
case expr: Expression =>
typeIt(expr, code) { (_, ty) =>
out.println(s": ${formatted(ty)}")
newLine = true
}
case stmt: Statement =>
typeIt(stmt, code) { (_, ty) =>
out.println(s": ${formatted(ty)}")
newLine = true
}
}
case (Mode.Eval, code) =>
parseIt(code) {
case expr: Expression =>
typeIt(expr, code) { (_, ty) =>
evalIt(expr, code) { value =>
out.println(s"= ${formatted(value, lvl = 3, short = true)} : ${formatted(ty)}")
newLine = true
}
}
case stmt: Statement =>
stmt match {
case module: Module =>
doIt(module, code) {
out.println("< ok")
newLine = true
}
case ymport @ Import(name, _, _) if name == InternalModuleName =>
doIt(ymport, code) {
out.println("< ok")
newLine = true
}
case ymport @ Import(name, _, _) =>
doIt(ymport, code, modules.removed(name)) {
out.println("< ok")
newLine = true
}
case stmt =>
typeIt(stmt, code) { (_, ty) =>
doIt(stmt, code) {
out.println(s": ${formatted(ty)}")
newLine = true
}
}
}
}
}
def parseIt(code: String)(ok: (Expression | Statement) => Unit): Boolean = {
for res <- parse(code, fileName) do
res match {
case Right(expr) => ok(expr)
case Left(_: UnexpectedEOF) =>
return false
case Left(err) =>
out.println(s"${errorType(err)}: ${formatted(err, code)}")
newLine = true
return true
}
true
}
def typeIt(stmt: Statement, code: String)(ok: (Expression, Type) => Unit) = {
infer(stmt, scope, Substitution()) match {
case Right(ty) =>
stmt match {
case Definition(_, value) => ok(value, ty)
case _ => ok(Bool(true), BoolType())
}
case Left(err) =>
out.println(s"${errorType(err)}: ${formatted(err, code)}")
newLine = true
}
}
def typeIt(expr: Expression, code: String)(ok: (Expression, Type) => Unit) = {
val ir = pass1(expr)
infer(ir, scope, Substitution()) match {
case Right(ty) => ok(expr, ty)
case Left(err) =>
out.println(s"${errorType(err)}: ${formatted(err, code)}")
newLine = true
}
}
def evalIt(expr: Expression, code: String)(ok: Value => Unit) =
val ir = pass1(expr)
eval(ir, scope) match {
case Right(value) => ok(value)
case Left(err) =>
out.println(s"${errorType(err)}: ${formatted(err, code)}")
newLine = true
}
def doIt(stmt: Statement, code: String, currModules: Modules = modules)(ok: => Unit) =
eval(stmt, scope, currModules) match {
case Right((newScope, newModules)) =>
scope = newScope
modules = newModules
ok
case Left(err) =>
out.println(s"${errorType(err)}: ${formatted(err, code)}")
newLine = true
}
def mode(code: String): (Mode, String) =
(code.split(" ").toList) match {
case (":parse" :: rest) => (Mode.Parse, rest.mkString(" "))
case (":type" :: rest) => (Mode.Type, rest.mkString(" "))
case (":ir" :: rest) => (Mode.Ir, rest.mkString(" "))
case _ => (Mode.Eval, code)
}
}