-
Notifications
You must be signed in to change notification settings - Fork 9
/
quine.effekt
69 lines (65 loc) · 1.78 KB
/
quine.effekt
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
import mutable/array
import text/string
interface Quine {
def line(source: String): Unit
}
def sub(string: String, from: String, to: String): String = {
var result = ""
var parts = string.split(from)
each(0, parts.size) { i =>
if (i > 0) {
result = result ++ to
}
result = result ++ unsafeGet(parts, i)
}
result
}
def main() = {
try { quine() } with Quine {
def line(source) = resume(source.println)
}
try { quine() } with Quine {
def line(source) = {
resume((
" do line(\"" ++ source.sub("\\", "\\\\").sub("\"", "\\\"") ++ "\")"
).println)
}
}
println("}")
}
def quine(): Unit / { Quine } = {
do line("import mutable/array")
do line("import text/string")
do line("")
do line("interface Quine {")
do line(" def line(source: String): Unit")
do line("}")
do line("")
do line("def sub(string: String, from: String, to: String): String = {")
do line(" var result = \"\"")
do line(" var parts = string.split(from)")
do line(" each(0, parts.size) { i =>")
do line(" if (i > 0) {")
do line(" result = result ++ to")
do line(" }")
do line(" result = result ++ unsafeGet(parts, i)")
do line(" }")
do line(" result")
do line("}")
do line("")
do line("def main() = {")
do line(" try { quine() } with Quine {")
do line(" def line(source) = resume(source.println)")
do line(" }")
do line(" try { quine() } with Quine {")
do line(" def line(source) = {")
do line(" resume((")
do line(" \" do line(\\\"\" ++ source.sub(\"\\\\\", \"\\\\\\\\\").sub(\"\\\"\", \"\\\\\\\"\") ++ \"\\\")\"")
do line(" ).println)")
do line(" }")
do line(" }")
do line(" println(\"}\")")
do line("}")
do line("")
do line("def quine(): Unit / { Quine } = {")
}