Skip to content

Commit 319039c

Browse files
Add tparams to constructors (#1104)
The Core Parser still needs to handle tparams, but I was unsure how to add that functionality.
1 parent c9f93a1 commit 319039c

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package effekt
2+
package core
3+
4+
import effekt.core.Type.*
5+
6+
class TypeInferenceTests extends CoreTests {
7+
8+
val SomeC: Id = Id("Some")
9+
val NoneC: Id = Id("None")
10+
val OptionId: Id = Id("Option")
11+
def OptionT(tpe: core.ValueType): core.ValueType.Data =
12+
core.ValueType.Data(OptionId, List(tpe))
13+
14+
test("infer Make type") {
15+
val intOptionTy = OptionT(TInt)
16+
17+
// Some[Int](42)
18+
val option = Make(intOptionTy, SomeC, List(), List(Literal(42, TInt)))
19+
20+
// Option[Int]
21+
val expected = intOptionTy
22+
23+
assertEquals(option.tpe, expected)
24+
}
25+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class CoreParsers(names: Names) extends EffektLexers {
267267
`interface` ~> id ~ maybeTypeParams ~ (`{` ~/> many(property) <~ `}`) ^^ Declaration.Interface.apply
268268

269269
lazy val constructor: P[Constructor] =
270-
id ~ valueParams ^^ { case id ~ params => Constructor(id, params.map(p => Field(p.id, p.tpe))) }
270+
id ~ maybeTypeParams ~ valueParams ^^ { case id ~ tparams ~ params => Constructor(id, tparams, params.map(p => Field(p.id, p.tpe))) }
271271

272272
lazy val property: P[Property] =
273273
id ~ (`:` ~> blockType) ^^ Property.apply

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ object PolymorphismBoxing extends Phase[CoreTransformed, CoreTransformed] {
9494
}
9595

9696
def transform(constructor: Constructor)(using Context, DeclarationContext): Constructor = constructor match {
97-
case Constructor(id, fields) => Constructor(id, fields map transform)
97+
case Constructor(id, tparams, fields) => Constructor(id, tparams, fields map transform)
9898
}
9999

100100
def transform(property: Property)(using Context, DeclarationContext): Property = property match {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
145145
}
146146

147147
def toDoc(c: Constructor): Doc = c match {
148-
case Constructor(id, fields) => toDoc(id) <> parens(fields.map(toDoc))
148+
case Constructor(id, tparams, fields) => toDoc(id) <> brackets(tparams.map(toDoc)) <> parens(fields.map(toDoc))
149149
}
150150
def toDoc(f: Field): Doc = f match {
151151
case Field(name, tpe) => toDoc(name) <> ":" <+> toDoc(tpe)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
144144
}.toList ++ exports.namespaces.values.flatMap(transform)
145145

146146
def transform(c: symbols.Constructor)(using Context): core.Constructor =
147-
core.Constructor(c, c.fields.map(f => core.Field(f, transform(f.returnType))))
147+
core.Constructor(c, c.tparams, c.fields.map(f => core.Field(f, transform(f.returnType))))
148148

149149
def transform(tree: source.Stmt)(using Context): Stmt = tree match {
150150
// { e; stmt } --> { let _ = e; stmt }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ enum Declaration extends Tree {
123123
}
124124
export Declaration.*
125125

126-
case class Constructor(id: Id, fields: List[Field]) extends Tree
126+
case class Constructor(id: Id, tparams: List[Id], fields: List[Field]) extends Tree
127127
case class Field(id: Id, tpe: ValueType) extends Tree
128128
case class Property(id: Id, tpe: BlockType) extends Tree
129129

0 commit comments

Comments
 (0)