Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
var anonymCnt: Int = 0
var clsCnt: Int = 0
val logOutput: StringBuilder = new StringBuilder
val primiTypes = new mlscript.Typer(false, false, false).primitiveTypes
val primiTypes = new mlscript.Typer(false, false, false, true).primitiveTypes

private def log(str: String): Unit = {
logOutput.append(str)
Expand Down Expand Up @@ -88,7 +88,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {

private def getFields(etts: List[Statement]): Set[Var] = {
etts.flatMap{
case NuFunDef(_, nm, _, _) => Some(nm)
case NuFunDef(_, nm, _, _, _) => Some(nm)
case nuty: NuTypeDef => Some(Var(nuty.name))
case Let(_, name, _, _) => Some(name)
case _ => None
Expand Down Expand Up @@ -163,7 +163,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {
case Lam(lhs, rhs) =>
val lhsVs = getFreeVars(lhs)
getFreeVars(rhs)(using ctx ++ lhsVs) -+ lhsVs
case NuFunDef(_, vm, tps, Left(trm)) =>
case NuFunDef(_, vm, _, tps, Left(trm)) =>
getFreeVars(trm).extV(vm).extT(tps)
case OpApp(_, trm) => getFreeVars(trm)
case Sel(trm, _) => getFreeVars(trm)
Expand Down Expand Up @@ -542,7 +542,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {

private def liftFunc(func: NuFunDef)(using ctx: LocalContext, cache: ClassCache, outer: Option[ClassInfoCache]): (NuFunDef, LocalContext) = {
log(s"liftFunc $func under $ctx # $cache # $outer")
val NuFunDef(rec, nm, tpVs, body) = func
val NuFunDef(rec, nm, sn, tpVs, body) = func
body match {
case Left(value) =>
val ret = liftTerm(value)(using ctx.addV(nm).addT(tpVs))
Expand Down
17 changes: 11 additions & 6 deletions compiler/shared/test/diff/LiftType.mls
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:NewParser
:NewDefs
:ParseOnly

class CTX{
Expand All @@ -8,14 +8,15 @@ class CTX{
//│ |#class| |CTX|{|→|#class| |A| |{||}|↵|#fun| |foo|(|f|#:| |A| |=>| |A|)|#:| |(|A| |=>| |A|)| |=>| |A| |#=| |f|(|#new| |A|)|←|↵|}|
//│ Parsed: {class CTX() {class A() {}; fun foo = (f: (A,) => A,) => f (new A() {},) : (A -> A) -> A}}
//│ Parsed:
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit()), NuFunDef(None, foo, [], Lam(Tup(f: Lam(Tup(_: Var(A)), Var(A))), Asc(App(Var(f), Tup(_: New(Some((TypeName(A),)), TypingUnit(List())))), Function(Tuple(List((None,Field(None,Function(Tuple(List((None,Field(None,TypeName(A))))),TypeName(A)))))),TypeName(A))))))))
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit()), NuFunDef(None, foo, None, [], Lam(Tup(f: Lam(Tup(_: Var(A)), Var(A))), Asc(App(Var(f), Tup(_: New(Some((TypeName(A),)), TypingUnit(List())))), Function(Tuple(List((None,Field(None,Function(Tuple(List((None,Field(None,TypeName(A))))),TypeName(A)))))),TypeName(A))))))))
//│ Lifted:
//│ TypingUnit {
//│ class CTX$1_A$2(par$CTX$1,) {}
//│ class CTX$1() {
//│ fun foo = (f: (CTX$1_A$2,) => CTX$1_A$2,) => f (new CTX$1_A$2(this,) {},) : (CTX$1_A$2 -> CTX$1_A$2) -> CTX$1_A$2
//│ }
//│ }
//│

class CTX(x, y){
class A{ fun foo = x}
Expand All @@ -25,7 +26,7 @@ class CTX(x, y){
//│ |#class| |CTX|(|x|,| |y|)|{|→|#class| |A|{| |#fun| |foo| |#=| |x|}|↵|#class| |B|#:| |A| |{| |#fun| |foo| |#=| |y|}|↵|#fun| |foo|(|any|#:| |(|A|,| |B|)|)|#:| |(|B|,| |A|)| |#=| |(|any|._2|,| |any|._1|)|←|↵|}|
//│ Parsed: {class CTX(x, y,) {class A() {fun foo = x}; class B(): A {fun foo = y}; fun foo = (any: '(' A, B, ')',) => '(' (any)._2, (any)._1, ')' : [B, A]}}
//│ Parsed:
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(_: Var(x), _: Var(y)), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, [], Var(x)))), NuTypeDef(class, B, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, [], Var(y)))), NuFunDef(None, foo, [], Lam(Tup(any: Bra(rcd = false, Tup(_: Var(A), _: Var(B)))), Asc(Bra(rcd = false, Tup(_: Sel(Var(any), _2), _: Sel(Var(any), _1))), Tuple(List((None,Field(None,TypeName(B))), (None,Field(None,TypeName(A)))))))))))
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(_: Var(x), _: Var(y)), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(x)))), NuTypeDef(class, B, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(y)))), NuFunDef(None, foo, None, [], Lam(Tup(any: Bra(rcd = false, Tup(_: Var(A), _: Var(B)))), Asc(Bra(rcd = false, Tup(_: Sel(Var(any), _2), _: Sel(Var(any), _1))), Tuple(List((None,Field(None,TypeName(B))), (None,Field(None,TypeName(A)))))))))))
//│ Lifted:
//│ TypingUnit {
//│ class CTX$1_A$2(par$CTX$1,) {fun foo = ((this).par$CTX$1).x}
Expand All @@ -34,6 +35,7 @@ class CTX(x, y){
//│ fun foo = (any: '(' CTX$1_A$2, CTX$1_B$3, ')',) => '(' (any)._2, (any)._1, ')' : [CTX$1_B$3, CTX$1_A$2]
//│ }
//│ }
//│

class CTX(x, y){
class A{ fun foo = x}
Expand All @@ -43,7 +45,7 @@ class CTX(x, y){
//│ |#class| |CTX|(|x|,| |y|)|{|→|#class| |A|{| |#fun| |foo| |#=| |x|}|↵|#class| |B|#:| |A| |{| |#fun| |foo| |#=| |y|}|↵|#fun| |foo|(|any|#:| |{|p1|#:| |A|,| |p2|#:| |B|}|)|#:| |(|B|,| |A|)| |#=| |(|any|.p2|,| |any|.p1|)|←|↵|}|
//│ Parsed: {class CTX(x, y,) {class A() {fun foo = x}; class B(): A {fun foo = y}; fun foo = (any: '{' {p1: A, p2: B} '}',) => '(' (any).p2, (any).p1, ')' : [B, A]}}
//│ Parsed:
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(_: Var(x), _: Var(y)), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, [], Var(x)))), NuTypeDef(class, B, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, [], Var(y)))), NuFunDef(None, foo, [], Lam(Tup(any: Bra(rcd = true, Rcd(Var(p1) = Var(A), Var(p2) = Var(B)))), Asc(Bra(rcd = false, Tup(_: Sel(Var(any), p2), _: Sel(Var(any), p1))), Tuple(List((None,Field(None,TypeName(B))), (None,Field(None,TypeName(A)))))))))))
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(_: Var(x), _: Var(y)), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(x)))), NuTypeDef(class, B, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(y)))), NuFunDef(None, foo, None, [], Lam(Tup(any: Bra(rcd = true, Rcd(Var(p1) = Var(A), Var(p2) = Var(B)))), Asc(Bra(rcd = false, Tup(_: Sel(Var(any), p2), _: Sel(Var(any), p1))), Tuple(List((None,Field(None,TypeName(B))), (None,Field(None,TypeName(A)))))))))))
//│ Lifted:
//│ TypingUnit {
//│ class CTX$1_A$2(par$CTX$1,) {fun foo = ((this).par$CTX$1).x}
Expand All @@ -52,6 +54,7 @@ class CTX(x, y){
//│ fun foo = (any: '{' {p1: CTX$1_A$2, p2: CTX$1_B$3} '}',) => '(' (any).p2, (any).p1, ')' : [CTX$1_B$3, CTX$1_A$2]
//│ }
//│ }
//│

class CTX(x, y){
class A{ fun foo = x}
Expand All @@ -61,7 +64,7 @@ class CTX(x, y){
//│ |#class| |CTX|(|x|,| |y|)|{|→|#class| |A|{| |#fun| |foo| |#=| |x|}|↵|#class| |B|‹|T|›| |{| |#fun| |foo| |#=| |y|}|↵|#fun| |foo|(|any|#:| |(|A|,| |B|‹|A|›|)|)|#:| |(|(|B|‹|A|›|,| |A|)|,| |A|)| |#=| |(|any|,| |any|._1|)|←|↵|}|
//│ Parsed: {class CTX(x, y,) {class A() {fun foo = x}; class B‹T›() {fun foo = y}; fun foo = (any: '(' A, B‹A›, ')',) => '(' any, (any)._1, ')' : [[B[A], A], A]}}
//│ Parsed:
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(_: Var(x), _: Var(y)), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, [], Var(x)))), NuTypeDef(class, B, ((None,TypeName(T))), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, [], Var(y)))), NuFunDef(None, foo, [], Lam(Tup(any: Bra(rcd = false, Tup(_: Var(A), _: TyApp(Var(B), List(TypeName(A)))))), Asc(Bra(rcd = false, Tup(_: Var(any), _: Sel(Var(any), _1))), Tuple(List((None,Field(None,Tuple(List((None,Field(None,AppliedType(TypeName(B),List(TypeName(A))))), (None,Field(None,TypeName(A))))))), (None,Field(None,TypeName(A)))))))))))
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(_: Var(x), _: Var(y)), (), None, None, TypingUnit(NuTypeDef(class, A, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(x)))), NuTypeDef(class, B, ((None,TypeName(T))), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(y)))), NuFunDef(None, foo, None, [], Lam(Tup(any: Bra(rcd = false, Tup(_: Var(A), _: TyApp(Var(B), List(TypeName(A)))))), Asc(Bra(rcd = false, Tup(_: Var(any), _: Sel(Var(any), _1))), Tuple(List((None,Field(None,Tuple(List((None,Field(None,AppliedType(TypeName(B),List(TypeName(A))))), (None,Field(None,TypeName(A))))))), (None,Field(None,TypeName(A)))))))))))
//│ Lifted:
//│ TypingUnit {
//│ class CTX$1_A$2(par$CTX$1,) {fun foo = ((this).par$CTX$1).x}
Expand All @@ -70,6 +73,7 @@ class CTX(x, y){
//│ fun foo = (any: '(' CTX$1_A$2, CTX$1_B$3‹CTX$1_A$2›, ')',) => '(' any, (any)._1, ')' : [[CTX$1_B$3[CTX$1_A$2], CTX$1_A$2], CTX$1_A$2]
//│ }
//│ }
//│

class CTX{
fun ctx(x,y) =
Expand All @@ -82,7 +86,8 @@ class CTX{
//│ |#class| |CTX|{|→|#fun| |ctx|(|x|,|y|)| |#=| |→|#class| |A|{| |#fun| |foo| |#=| |x| |}|↵|#fun| |bar|‹|T|›|(|any|#:| |T|)|#:| |A| |#=| |→|#let| |x| |#=| |#new| |T|↵|#new| |A|←|↵|(|#new| |CTX|)|.bar|‹|CTX|›|←|←|↵|}|
//│ Parsed: {class CTX() {fun ctx = (x, y,) => {class A() {fun foo = x}; fun bar = (any: T,) => {let x = new T() {}; new A() {}} : A; ('(' new CTX() {} ')').bar‹CTX›}}}
//│ Parsed:
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, ctx, [], Lam(Tup(_: Var(x), _: Var(y)), Blk(...))))))
//│ TypingUnit(NuTypeDef(class, CTX, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, ctx, None, [], Lam(Tup(_: Var(x), _: Var(y)), Blk(...))))))
//│ Lifted:
//│ Lifting failed: mlscript.codegen.CodeGenError: Cannot find type T. Class values are not supported in lifter.
//│

Loading