Skip to content

Commit 186257b

Browse files
Move builtins resolution to effekt.symbols.builtins
1 parent d2b54b7 commit 186257b

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

effekt/jvm/src/test/scala/effekt/core/TestRenamer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package effekt.core
22

33
import effekt.{Template, core, symbols}
4+
import effekt.symbols.builtins
45

56
/**
67
* Freshens bound names in a given term for tests.
@@ -45,7 +46,7 @@ class TestRenamer(names: Names = Names(Map.empty)) extends core.Tree.Rewrite {
4546
// We use a separate pass to collect all top-level ids, so that we can distinguish them from free variables.
4647
override def id: PartialFunction[core.Id, core.Id] = {
4748
case id =>
48-
if (isBuiltin(id)) {
49+
if (builtins.isCoreBuiltin(id)) {
4950
// builtin, do not rename
5051
id
5152
} else if (toplevelScope.contains(id)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33

44
import effekt.core.Type.{PromptSymbol, ResumeSymbol}
55
import effekt.source.{FeatureFlag, Span}
6+
import effekt.symbols.builtins
67
import effekt.util.messages.{ErrorReporter, ParseError}
78
import kiama.parsing.{NoSuccess, ParseResult, Parsers, Success}
89
import kiama.util.{Position, Range, Source, StringSource}
@@ -11,7 +12,7 @@ class Names(private var knownNames: Map[String, Id]) {
1112
private val Suffix = """^(.*)\$(\d+)$""".r
1213

1314
def idFor(name: String): Id =
14-
builtinSymbolFromString(name).getOrElse {
15+
builtins.coreBuiltinSymbolFromString(name).getOrElse {
1516
knownNames.getOrElse(name, {
1617
val id = name match {
1718
case Suffix(base, n) => Id(base, n.toInt)

effekt/shared/src/main/scala/effekt/core/PrettyPrinter.scala

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
112112
//def toDoc(n: Name): Doc = n.toString
113113

114114
def toDoc(s: symbols.Symbol): Doc = {
115-
builtinSymbolToString(s).getOrElse(s.name.name ++ "$" ++ s.id.toString)
115+
builtins.coreBuiltinSymbolToString(s).getOrElse(s.name.name ++ "$" ++ s.id.toString)
116116
}
117117

118118
def toDoc(e: Expr): Doc = e match {
@@ -323,19 +323,3 @@ object PrettyPrinter extends ParenPrettyPrinter {
323323
multi <> string(s) <> multi
324324
}
325325
}
326-
327-
val builtins: Map[String, symbols.Symbol] = {
328-
symbols.builtins.rootTypes
329-
++ symbols.builtins.rootCaptures
330-
+ ("Resume" -> ResumeSymbol)
331-
+ ("Prompt" -> PromptSymbol)
332-
+ ("Ref" -> effekt.symbols.builtins.TState.interface)
333-
}
334-
335-
def isBuiltin(s: symbols.Symbol): Boolean =
336-
builtins.contains(s.name.name) && builtins(s.name.name) == s
337-
338-
def builtinSymbolToString(s: symbols.Symbol): Option[String] =
339-
if isBuiltin(s) then Some(s.name.name) else None
340-
341-
def builtinSymbolFromString(s: String): Option[symbols.Symbol] = builtins.get(s)

effekt/shared/src/main/scala/effekt/symbols/builtins.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package symbols
33

44
import effekt.source.{Many, ModuleDecl, NoSource, Span}
55
import effekt.context.Context
6+
import effekt.core.Type.{PromptSymbol, ResumeSymbol}
67
import effekt.symbols.ErrorMessageInterpolator
78
import effekt.util.messages.ErrorMessageReifier
89
import kiama.util.StringSource
@@ -101,4 +102,20 @@ object builtins {
101102
lazy val rootBindings: Bindings =
102103
Bindings(Map.empty, rootTypes, rootCaptures, Map("effekt" -> Bindings(Map.empty, rootTypes, rootCaptures, Map.empty)))
103104

105+
// All built-in symbols that can occur in core programs
106+
val coreBuiltins: Map[String, symbols.Symbol] = {
107+
symbols.builtins.rootTypes
108+
++ symbols.builtins.rootCaptures
109+
+ ("Resume" -> ResumeSymbol)
110+
+ ("Prompt" -> PromptSymbol)
111+
+ ("Ref" -> effekt.symbols.builtins.TState.interface)
112+
}
113+
114+
def isCoreBuiltin(s: symbols.Symbol): Boolean =
115+
coreBuiltins.contains(s.name.name) && coreBuiltins(s.name.name) == s
116+
117+
def coreBuiltinSymbolToString(s: symbols.Symbol): Option[String] =
118+
if isCoreBuiltin(s) then Some(s.name.name) else None
119+
120+
def coreBuiltinSymbolFromString(s: String): Option[symbols.Symbol] = coreBuiltins.get(s)
104121
}

0 commit comments

Comments
 (0)