Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inspection function for syntax tree nodes #200

Merged
merged 11 commits into from
Jan 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.collection.mutable.StringBuilder as StringBuilder
import scala.collection.mutable.Map as MutMap
import scala.collection.mutable.Set as MutSet
import scala.collection.mutable.ArrayBuffer as ArrayBuffer
import mlscript.codegen.Helpers.inspect as showStructure
import mlscript.codegen.CodeGenError
import mlscript.compiler.mono.MonomorphError

Expand Down Expand Up @@ -748,7 +747,7 @@ class ClassLifter(logDebugMsg: Boolean = false) {

def liftTypingUnit(rawUnit: TypingUnit): TypingUnit = {
log("=========================\n")
log(s"lifting: \n${showStructure(rawUnit)}\n")
log(s"lifting: \n$rawUnit\n")
retSeq = Nil
globalFunctions.clear()
val re = liftEntities(rawUnit.entities)(using emptyCtx, Map(), Map(), None)
Expand Down
3 changes: 1 addition & 2 deletions compiler/shared/main/scala/mlscript/compiler/Helpers.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mlscript
package compiler

import mlscript.codegen.Helpers.inspect as showStructure
import mlscript.compiler.mono.{Monomorph, MonomorphError}
import scala.collection.mutable.ArrayBuffer

Expand All @@ -16,7 +15,7 @@ object Helpers:
case (None, Fld(FldFlags(_, spec, _), Var(name))) => Some((spec, Expr.Ref(name)))
case (Some(Var(name)), Fld(FldFlags(_, spec, _), _)) => Some((spec, Expr.Ref(name)))
case _ => throw new MonomorphError(
s"only `Var` can be parameters but we meet ${showStructure(term)}"
s"only `Var` can be parameters but we meet $term"
)
}
case _ => throw MonomorphError("expect the list of parameters to be a `Tup`")
Expand Down
12 changes: 6 additions & 6 deletions compiler/shared/main/scala/mlscript/compiler/PrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mlscript.compiler.debug.DebugOutput

// For pretty printing terms in debug output.
object PrettyPrinter:
def show(term: Term): DebugOutput = DebugOutput.Code(term.toString.linesIterator.toList)
def show(term: Term): DebugOutput = DebugOutput.Code(term.showDbg.linesIterator.toList)
def show(unit: TypingUnit): DebugOutput = DebugOutput.Code(showTypingUnit(unit, 0).linesIterator.toList)
def show(funDef: NuFunDef): DebugOutput = DebugOutput.Code(showFunDef(funDef).linesIterator.toList)
def show(tyDef: NuTypeDef): DebugOutput = DebugOutput.Code(showTypeDef(tyDef, 0).linesIterator.toList)
Expand All @@ -16,7 +16,7 @@ object PrettyPrinter:
case term: Term => show(term)
case tyDef: NuTypeDef => showTypeDef(tyDef)
case funDef: NuFunDef => showFunDef(funDef)
case others => others.toString()
case others => others.showDbg
}.mkString("{", "; ", "}")
if (singleLine.length < 60)
singleLine
Expand All @@ -26,7 +26,7 @@ object PrettyPrinter:
case term: Term => show(term)
case tyDef: NuTypeDef => showTypeDef(tyDef)
case funDef: NuFunDef => showFunDef(funDef)
case others => others.toString()
case others => others.showDbg
}.map(indentStr + " " + _).mkString("{\n", "\n", s"\n$indentStr}")

def showFunDef(funDef: NuFunDef): String =
Expand All @@ -40,15 +40,15 @@ object PrettyPrinter:
then ""
else funDef.tparams.map(_.name).mkString("[", ", ", "]"))
+ " = "
+ funDef.rhs.fold(_.toString, _.show(newDefs = true))
+ funDef.rhs.fold(_.showDbg, _.show(newDefs = true))

def showTypeDef(tyDef: NuTypeDef, indent: Int = 0): String =
s"${tyDef.kind.str} ${tyDef.nme.name}"
+ (if tyDef.tparams.isEmpty
then ""
else tyDef.tparams.map(_._2.name).mkString("[", ",", "]"))
+ tyDef.params.fold("")(params => s"($params)")
+ tyDef.params.fold("")(params => s"(${params.showDbg})")
+ (if tyDef.parents.isEmpty
then ""
else ": " + tyDef.parents.map(_.toString).mkString(", "))
else ": " + tyDef.parents.map(_.showDbg).mkString(", "))
+ showTypingUnit(tyDef.body, indent + 1)
12 changes: 4 additions & 8 deletions compiler/shared/test/diff/LambLift.mls
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ fun foo() =
(new Foo()).bar
local(1)
foo()
//│ Parsed:
//│ TypingUnit(NuFunDef(None, foo, None, [], Lam(Tup(), Blk(...))), App(Var(foo), Tup()))
//│
//│ Lifted:
//│ TypingUnit {
//│ class Foo$1([x,]) {fun bar = () => +((this).x, foo$1(),)}
Expand All @@ -27,8 +26,7 @@ foo()
fun foo(f) =
f(1)
foo(x => x+1)
//│ Parsed:
//│ TypingUnit(NuFunDef(None, foo, None, [], Lam(Tup(_: Var(f)), Blk(...))), App(Var(foo), Tup(_: Lam(Tup(_: Var(x)), App(Var(+), Tup(_: Var(x), _: IntLit(1)))))))
//│
//│ Lifted:
//│ TypingUnit {
//│ class Lambda1$2$1([]) {fun apply = (x,) => +(x, 1,)}
Expand All @@ -45,8 +43,7 @@ fun foo(x) =
f(x)
bar(y => y+x)
foo(1)
//│ Parsed:
//│ TypingUnit(NuFunDef(None, foo, None, [], Lam(Tup(_: Var(x)), Blk(...))), App(Var(foo), Tup(_: IntLit(1))))
//│
//│ Lifted:
//│ TypingUnit {
//│ class Lambda1$3$1([x,]) {fun apply = (y,) => +(y, (this).x,)}
Expand All @@ -67,8 +64,7 @@ class A(y: Int){
fun app(a) =
foo(z => a.bar(z))
app(new A(1))
//│ Parsed:
//│ TypingUnit(NuFunDef(None, foo, None, [], Lam(Tup(_: Var(f)), Blk(...))), NuTypeDef(class, A, (), Tup(y: Var(Int)), (), None, None, TypingUnit(NuFunDef(None, bar, None, [], Lam(Tup(_: Var(z)), App(Var(+), Tup(_: Var(y), _: Var(z))))))), NuFunDef(None, app, None, [], Lam(Tup(_: Var(a)), Blk(...))), App(Var(app), Tup(_: App(NuNew(Var(A)), Tup(_: IntLit(1))))))
//│
//│ Lifted:
//│ TypingUnit {
//│ class A$1([y: Int,]) {fun bar = (z,) => +((this).y, z,)}
Expand Down
15 changes: 5 additions & 10 deletions compiler/shared/test/diff/LiftType.mls
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ 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, None, [], Lam(Tup(f: Lam(Tup(_: Var(A)), Var(A))), Asc(App(Var(f), Tup(_: NuNew(Var(A)))), 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,]) {}
Expand All @@ -25,8 +24,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, None, [], Var(x)))), NuTypeDef(class, B, (), Tup(), (), None, None, TypingUnit(NuFunDef(None, foo, None, [], Var(y)))), NuFunDef(None, foo, None, [], Lam(Tup(any: Tup(_: Var(A), _: Var(B))), Asc(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 @@ -44,8 +42,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, 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(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 @@ -63,8 +60,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, 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: Tup(_: Var(A), _: TyApp(Var(B), List(TypeName(A))))), Asc(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 @@ -85,8 +81,7 @@ 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, 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.
//│
Expand Down
Loading
Loading