Skip to content

Commit 4716b1a

Browse files
authored
Fix: Splices with non-String type break if there is no splice (#901)
Fixes #892
1 parent 55e373b commit 4716b1a

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

effekt/shared/src/main/scala/effekt/RecursiveDescent.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,10 +1030,9 @@ class RecursiveDescent(positions: Positions, tokens: Seq[Token], source: Source)
10301030
def templateString(): Term =
10311031
nonterminal:
10321032
backtrack(idRef()) ~ template() match {
1033-
// We do not need to apply any transformation if there are no splices
1034-
case _ ~ Template(str :: Nil, Nil) => StringLit(str)
1035-
case _ ~ Template(strs, Nil) => fail("Cannot occur")
1036-
// s"a${x}b${y}" ~> s { do literal("a"); do splice(x); do literal("b"); do splice(y) }
1033+
// We do not need to apply any transformation if there are no splices _and_ no custom handler id is given
1034+
case None ~ Template(str :: Nil, Nil) => StringLit(str)
1035+
// s"a${x}b${y}" ~> s { do literal("a"); do splice(x); do literal("b"); do splice(y); return () }
10371036
case id ~ Template(strs, args) =>
10381037
val target = id.getOrElse(IdRef(Nil, "s"))
10391038
val doLits = strs.map { s =>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
GET https://api.effekt-lang.org/users/effekt/resource/42
2-
Fix point combinator: \ f -> (\ x -> f x x) \ x -> f x x
2+
Fix point combinator: \ f -> (\ x -> f x x) \ x -> f x x
3+
12

examples/pos/string_interpolation.effekt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ def pretty { prog: () => Unit / {literal, splice[Expr]} }: String = {
2828
}
2929
}
3030

31+
def len { prog: () => Unit / {literal} }: Int = {
32+
try {
33+
prog()
34+
0
35+
} with literal { s =>
36+
s.length
37+
}
38+
}
39+
3140
def main() = {
3241
val domain = "https://api.effekt-lang.org"
3342
val user = "effekt"
@@ -36,4 +45,6 @@ def main() = {
3645

3746
val fixpoint = Abs("f", App(Abs("x", App(Var("f"), App(Var("x"), Var("x")))), Abs("x", App(Var("f"), App(Var("x"), Var("x"))))))
3847
println(pretty"Fix point combinator: ${fixpoint}")
48+
49+
println(show(len"hello, world"))
3950
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
42
2+
[WARNING] Frobnicators have been jabberwocked!
3+
-1
4+
[ERROR] Stuff went wrong!
5+
[ERROR] Aborting!
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
effect log(msg: String): Unit
2+
3+
def error { body: => Unit / literal }: Unit / log =
4+
try body() with literal { x => do log("[ERROR] " ++ x); resume(()) }
5+
6+
def warn { body: => Unit / literal }: Unit / log =
7+
try body() with literal { x => do log("[WARNING] " ++ x); resume(()) }
8+
9+
def doc { body: => Unit / {literal} }: Unit =
10+
try body() with literal { _ => resume(()) }
11+
12+
def main() = try {
13+
doc"This is the doc string for my main function!"
14+
15+
println(42)
16+
warn"Frobnicators have been jabberwocked!"
17+
18+
println(-1)
19+
error"Stuff went wrong!"
20+
error"Aborting!"
21+
} with log { msg => println(msg); resume(()) }

0 commit comments

Comments
 (0)