-
Notifications
You must be signed in to change notification settings - Fork 1
/
REPL.scala
97 lines (82 loc) · 3.03 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
import cats.effect.*
import cats.effect.std.Console as CE_Console
import fs2.concurrent.SignallingRef
import cats.syntax.all.*
import cats.Show
object Colors:
def red[A](s: A)(using show: Show[A] = Show.fromToString[A]): String =
Console.RED + show.show(s) + Console.RESET
def green[A](s: A)(using show: Show[A] = Show.fromToString[A]): String =
Console.GREEN + show.show(s) + Console.RESET
def yellow[A](s: A)(using show: Show[A] = Show.fromToString[A]): String =
Console.YELLOW + show.show(s) + Console.RESET
end Colors
class REPL(linesIn: fs2.Stream[IO, String], out: String => IO[Unit]):
def outLine[A](s: A)(using show: Show[A] = Show.fromToString) =
out(show.show(s) + "\n")
def run =
import Colors.*
val state = (
Program[WithSpan](statements = Vector.empty, text = ""),
Map.empty[String, Int]
)
fs2.Stream.eval(IO.ref(state)).flatMap { ref =>
fs2.Stream.eval(IO.deferred[Either[Throwable, Unit]]).flatMap { halt =>
val prompt = out(yellow("> "))
val intro =
outLine("Welcome to Quickmaffs REPL") *> prompt
def process(line: String) =
QuickmaffsParser.parseExpr(line) match
case Left(err) =>
QuickmaffsParser.parseStatement(line) match
case Left(err) =>
outLine(red("Parsing error"))
case Right(statement) =>
ref.get.flatMap { case (prog, state) =>
val newProg = Program(prog.statements :+ statement, "")
QuickmaffsCompiler.compile(newProg) match
case Left(errs) =>
errs.traverse(msg => outLine(red("! " + msg.message)))
case Right(idx) =>
QuickmaffsEvaluator.evaluate(newProg) match
case Right(interpreted) =>
ref.set(newProg -> interpreted)
case Left(err) =>
outLine(red("!! " + err.message))
end match
}
case Right(expr) =>
ref.get.map(_._2).flatMap { state =>
QuickmaffsEvaluator.evaluate(expr, state) match
case Right(value) =>
outLine(green(value))
case Left(err) =>
outLine(red("! " + err.message))
}
end match
end process
fs2.Stream.eval(intro) ++
linesIn
.map(_.trim)
.evalMap {
case e if Set("quit", "exit", "stop").contains(e.toLowerCase()) =>
halt.complete(Right(()))
case other =>
process(other) *> prompt
}
.interruptWhen(halt)
}
}
end run
end REPL
object REPL extends IOApp.Simple:
def run =
REPL(
fs2.io
.stdin[IO](128)
.through(fs2.text.utf8.decode)
.through(fs2.text.lines),
cats.effect.std.Console[IO].print
).run.compile.drain
end run
end REPL