Skip to content

Commit 74652cd

Browse files
Rename literal effect to write (#1294)
Co-authored-by: Tim Süberkrüb <dev@timsueberkrueb.io>
1 parent 881b143 commit 74652cd

7 files changed

Lines changed: 62 additions & 75 deletions

File tree

effekt/jvm/src/test/scala/effekt/LSPTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ class LSPTests extends FunSuite {
811811
raw"""effect raise(): Unit
812812
|
813813
|↑
814-
|def foo() = { do raise(); 5 }
814+
|def boo() = { do raise(); 5 }
815815
| ↑
816816
|
817817
|↑

effekt/shared/src/main/scala/effekt/Parser.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ class Parser(tokens: Seq[Token], source: Source) {
13501350
backtrack(idRef()) ~ template() match {
13511351
// We do not need to apply any transformation if there are no splices _and_ no custom handler id is given
13521352
case Maybe(None, _) ~ SpannedTemplate(str :: Nil, Nil) => StringLit(str.unspan, str.span)
1353-
// s"a${x}b${y}" ~> s { do literal("a"); do splice(x); do literal("b"); do splice(y); return () }
1353+
// s"a${x}b${y}" ~> s { do write("a"); do splice(x); do write("b"); do splice(y); return () }
13541354
case Maybe(id, range) ~ SpannedTemplate(strs, args) =>
13551355
val target = id match {
13561356
case Some(id) => id
@@ -1362,7 +1362,7 @@ class Parser(tokens: Seq[Token], source: Source) {
13621362
}
13631363
val doLits = strs.map { s =>
13641364
Do(
1365-
IdRef(Nil, "literal", s.span.synthesized),
1365+
IdRef(Nil, "write", s.span.synthesized),
13661366
Nil,
13671367
List(ValueArg.Unnamed(StringLit(s.unspan, s.span))),
13681368
Nil,

examples/benchmarks/other/nbe.effekt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,14 @@ def parse(): Term / { read[Bool], stop } = {
9494
}
9595

9696
/// helper function for pretty string interpolation of terms
97-
def pretty { s: () => Unit / { literal, splice[Term] } }: String = {
97+
def pretty { stream: () => Unit / { write, splice[Term] } }: String = {
9898
with stringBuffer
99-
100-
try { s(); do flush() }
101-
with literal { l => resume(do write(l)) }
102-
with splice[Term] { t =>
103-
t match {
104-
case Abs(n, t) => do write("λ" ++ n.show ++ pretty".${t}")
105-
case App(a, b) => do write(pretty"(${a} ${b})")
106-
case Var(v) => do write(v.show)
107-
}
108-
resume(())
99+
with splicing[Term] {
100+
case Abs(n, t) => do write("λ" ++ n.show ++ pretty".${t}")
101+
case App(a, b) => do write(pretty"(${a} ${b})")
102+
case Var(v) => do write(v.show)
109103
}
104+
stream()
110105
}
111106

112107
/// convert char stream to bit stream, skipping non-bit chars

examples/pos/string_interpolation.effekt

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,26 @@ type Expr {
44
App(fn: Expr, arg: Expr)
55
}
66

7-
def pretty { prog: () => Unit / {literal, splice[Expr]} }: String = {
7+
def pretty { prog: () => Unit / {write, splice[Expr]} }: String = {
88
with stringBuffer
9-
try {
10-
prog()
11-
do flush()
12-
} with literal { s =>
13-
resume(do write(s))
14-
} with splice[Expr] { expr =>
15-
expr match {
16-
case Var(id) =>
17-
do write(id)
18-
case App(Abs(param, body), arg) =>
19-
do write(pretty"(${Abs(param, body)}) ${arg}")
20-
case App(fn, arg) =>
21-
do write(pretty"${fn} ${arg}")
22-
case Abs(param, body) =>
23-
do write(s"\\ ${param} -> " ++ pretty"${body}")
24-
}
25-
resume(())
9+
with splicing[Expr] {
10+
case Var(id) =>
11+
do write(id)
12+
case App(Abs(param, body), arg) =>
13+
do write(pretty"(${Abs(param, body)}) ${arg}")
14+
case App(fn, arg) =>
15+
do write(pretty"${fn} ${arg}")
16+
case Abs(param, body) =>
17+
do write(s"\\ ${param} -> " ++ pretty"${body}")
2618
}
19+
prog()
2720
}
2821

29-
def len { prog: () => Unit / {literal} }: Int = {
22+
def len { prog: () => Unit / write }: Int = {
3023
try {
3124
prog()
3225
0
33-
} with literal { s =>
26+
} with write { s =>
3427
s.length
3528
}
3629
}

examples/pos/string_interpolation_literal.effekt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
effect log(msg: String): Unit
22

3-
def error { body: => Unit / literal }: Unit / log =
4-
try body() with literal { x => do log("[ERROR] " ++ x); resume(()) }
3+
def error { body: => Unit / write }: Unit / log =
4+
try body() with write { x => do log("[ERROR] " ++ x); resume(()) }
55

6-
def warn { body: => Unit / literal }: Unit / log =
7-
try body() with literal { x => do log("[WARNING] " ++ x); resume(()) }
6+
def warn { body: => Unit / write }: Unit / log =
7+
try body() with write { x => do log("[WARNING] " ++ x); resume(()) }
88

9-
def doc { body: => Unit / {literal} }: Unit =
10-
try body() with literal { _ => resume(()) }
9+
def doc { body: => Unit / write }: Unit =
10+
try body() with write { _ => resume(()) }
1111

1212
def main() = try {
1313
doc"This is the doc string for my main function!"

libraries/common/effekt.effekt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,18 @@ def locally[R] { p: => R } : R = p()
790790
//
791791
// "foo ${42} bar ${43}"
792792
//
793-
// to the following stream of `literal`s and `splice[Int]`s:
793+
// to the following stream of `write`s and `splice[Int]`s:
794794
//
795-
// do literal("foo "); do splice(42); do literal(" bar "); do splice(43)
795+
// do write("foo "); do splice(42); do write(" bar "); do splice(43)
796796
//
797797
// The stream is wrapped into a handler function, which defaults to `stringbuffer::s`
798798
//
799799
// s { ... }
800800
//
801801
// but can be specified by prefixing the string interpolation `custom"..."`.
802802

803-
effect literal(s: String): Unit
803+
effect write(s: String): Unit
804+
804805
effect splice[A](x: A): Unit
805806

806807

libraries/common/stringbuffer.effekt

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,58 @@ module stringbuffer
33
import effekt
44
import bytearray
55

6-
interface StringBuffer {
7-
def write(str: String): Unit
8-
def flush(): String
9-
}
6+
effect flush(): String
7+
8+
def stringBuffer { program: () => Unit / write }: String =
9+
flushableStringBuffer { program(); do flush() }
1010

11-
def stringBuffer[A] { prog: => A / StringBuffer }: A = {
11+
def flushableStringBuffer[A] { program: () => A / { write, flush } }: A = {
1212
val initialCapacity = 128
1313
var buffer = bytearray::allocate(initialCapacity)
1414
// next free index to write to
15-
var pos = 0
15+
var position = 0
1616

17-
def ensureCapacity(sizeToAdd: Int): Unit = {
18-
val cap = buffer.size - pos
19-
if (sizeToAdd <= cap) ()
17+
def ensureCapacity(required: Int): Unit = {
18+
val capacity = buffer.size - position
19+
if (required <= capacity) ()
2020
else {
2121
// Double the capacity while ensuring the required capacity
22-
val newSize = max(buffer.size * 2, buffer.size + sizeToAdd)
22+
val newSize = max(buffer.size * 2, buffer.size + required)
2323
buffer = buffer.resize(newSize)
2424
}
2525
}
2626

27-
try { prog() }
28-
with StringBuffer {
29-
def write(str) = {
30-
val bytes = fromString(str)
31-
ensureCapacity(bytes.size)
32-
bytes.foreach { b =>
33-
buffer.set(pos, b)
34-
pos = pos + 1
35-
}
36-
resume(())
37-
}
38-
def flush() = {
39-
// resize (& copy) buffer to strip trailing zeros that otherwise would be converted into 0x00 characters
40-
val str = bytearray::resize(buffer, pos).toString()
41-
// NOTE: Keep the `buffer` as-is (no wipe, no realloc),
42-
// just reset the `pos` in case we want to use it again.
43-
pos = 0
44-
resume(str)
27+
try {
28+
program()
29+
} with write { string =>
30+
val bytes = fromString(string)
31+
ensureCapacity(bytes.size)
32+
bytes.foreach { b =>
33+
buffer.set(position, b)
34+
position = position + 1
4535
}
36+
resume(())
37+
} with flush {
38+
val string = buffer.resize(position).toString()
39+
position = 0
40+
resume(string)
4641
}
4742
}
4843

44+
def splicing[T] { writer: T => Unit } { program: () => Unit / splice[T] }: Unit =
45+
try { program() } with splice[T] { t => resume(writer(t)) }
46+
47+
4948
/// Handler for string interpolation using a string buffer
50-
def s { prog: () => Unit / { literal, splice[String] } }: String =
49+
def s { program: () => Unit / { write, splice[String] } }: String =
5150
stringBuffer {
52-
try { prog(); do flush() }
51+
try { program() }
5352
with splice[String] { x => resume(do write(x)) }
54-
with literal { s => resume(do write(s)) }
5553
}
5654

5755
namespace examples {
5856
def main() = {
59-
with stringBuffer
57+
with flushableStringBuffer
6058

6159
do write("hello")
6260
do write(", world")

0 commit comments

Comments
 (0)