Skip to content

Commit

Permalink
Merge branch 'new-definition-typing' into ucs-paper
Browse files Browse the repository at this point in the history
  • Loading branch information
chengluyu authored May 5, 2023
2 parents 2ff4c7b + 88df416 commit ee1ab19
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 14 deletions.
8 changes: 4 additions & 4 deletions shared/src/main/scala/mlscript/codegen/Codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,10 @@ final case class JSClassNewDecl(
if (s.isEmpty) s"${p._1}"
else s"${p._1}, $s")
nestedTypes.foreach(t => buffer += s" #$t;")
if (!privateMem.isEmpty) {
privateMem.foreach(f => buffer += s" #${f};")
privateMem.foreach(f => buffer += s" get ${f}() { return this.#${f}; }")
}
privateMem.distinct.foreach(f => {
buffer += s" #${f};"
buffer += s" get ${f}() { return this.#${f}; }"
})
buffer += s" constructor($params) {"
if (`extends`.isDefined) {
val sf = superFields.iterator.zipWithIndex.foldLeft("")((res, p) =>
Expand Down
2 changes: 1 addition & 1 deletion shared/src/test/diff/codegen/ConstructorStmt.mls
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ class Baz {
//│ if (this.#Baz === undefined) {
//│ class Baz {
//│ #x;
//│ #y;
//│ get x() { return this.#x; }
//│ #y;
//│ get y() { return this.#y; }
//│ constructor() {
//│ this.#x = 123;
Expand Down
131 changes: 131 additions & 0 deletions shared/src/test/diff/codegen/FieldOverride.mls
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
:NewParser
:NewDefs

:js
class C(a: int) { let a = 1 }
//│ class C(a: int) {
//│ let a: 1
//│ }
//│ // Prelude
//│ let res;
//│ class TypingUnit {
//│ #C;
//│ constructor() {
//│ }
//│ get C() {
//│ const outer = this;
//│ if (this.#C === undefined) {
//│ class C {
//│ #a;
//│ get a() { return this.#a; }
//│ constructor(a) {
//│ this.#a = a;
//│ this.#a = 1;
//│ const a1 = this.#a;
//│ }
//│ };
//│ this.#C = ((a) => new C(a));
//│ this.#C.class = C;
//│ }
//│ return this.#C;
//│ }
//│ }
//│ const typing_unit = new TypingUnit;
//│ globalThis.C = typing_unit.C;
//│ // End of generated code

// should return 1
let a = C(2)
a.a
//│ let a: C
//│ 1
//│ a
//│ = C {}
//│ res
//│ = 1

:js
class C2(a: int, b: int) {
let a = b + 1
let b = a + 1
}
//│ class C2(a: int, b: int) {
//│ let a: int
//│ let b: int
//│ }
//│ // Prelude
//│ class TypingUnit2 {
//│ #C2;
//│ constructor() {
//│ }
//│ get C2() {
//│ const outer = this;
//│ if (this.#C2 === undefined) {
//│ class C2 {
//│ #a;
//│ get a() { return this.#a; }
//│ #b;
//│ get b() { return this.#b; }
//│ constructor(a, b) {
//│ this.#a = a;
//│ this.#b = b;
//│ this.#a = b + 1;
//│ const a1 = this.#a;
//│ this.#b = a1 + 1;
//│ const b1 = this.#b;
//│ }
//│ };
//│ this.#C2 = ((a, b) => new C2(a, b));
//│ this.#C2.class = C2;
//│ }
//│ return this.#C2;
//│ }
//│ }
//│ const typing_unit2 = new TypingUnit2;
//│ globalThis.C2 = typing_unit2.C2;
//│ // End of generated code

let c2 = C2(1, 2)
c2.a
c2.b
//│ let c2: C2
//│ int
//│ c2
//│ = C2 {}
//│ res
//│ = 3
//│ res
//│ = 4

class C3(a: int) {
let a = 42
class C4(a: int) {
let a = 44
}
}
//│ class C3(a: int) {
//│ class C4(a: int) {
//│ let a: 44
//│ }
//│ let a: 42
//│ }

:e
let c3 = C3(1)
let c4 = c3.C4(2)
c3.a
c4.a
//│ ╔══[ERROR] access to class member not yet supported
//│ ║ l.115: let c4 = c3.C4(2)
//│ ╙── ^^^
//│ let c3: C3
//│ let c4: error
//│ error
//│ c3
//│ = C3 {}
//│ c4
//│ = C4 {}
//│ res
//│ = 42
//│ res
//│ = 44
4 changes: 2 additions & 2 deletions shared/src/test/diff/codegen/Mixin.mls
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Lit(n: int)
//│ if (this.#Add === undefined) {
//│ class Add {
//│ #lhs;
//│ #rhs;
//│ get lhs() { return this.#lhs; }
//│ #rhs;
//│ get rhs() { return this.#rhs; }
//│ constructor(lhs, rhs) {
//│ this.#lhs = lhs;
Expand Down Expand Up @@ -66,8 +66,8 @@ class Lit(n: int)
//│ │ │ if (this.#Add === undefined) {
//│ │ │ class Add {
//│ │ │ #lhs;
//│ │ │ #rhs;
//│ │ │ get lhs() { return this.#lhs; }
//│ │ │ #rhs;
//│ │ │ get rhs() { return this.#rhs; }
//│ │ │ constructor(lhs, rhs) {
//│ │ │ this.#lhs = lhs;
Expand Down
12 changes: 6 additions & 6 deletions shared/src/test/diff/codegen/Nested.mls
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ module H {
//│ if (this.#J === undefined) {
//│ class J {
//│ #x;
//│ #i;
//│ get x() { return this.#x; }
//│ #i;
//│ get i() { return this.#i; }
//│ constructor(x) {
//│ this.#x = x;
Expand Down Expand Up @@ -666,8 +666,8 @@ ij.incY
//│ class I {
//│ #J;
//│ #x;
//│ #y;
//│ get x() { return this.#x; }
//│ #y;
//│ get y() { return this.#y; }
//│ constructor(x) {
//│ this.#x = x;
Expand All @@ -679,8 +679,8 @@ ij.incY
//│ if (this.#J === undefined) {
//│ class J {
//│ #x;
//│ #y;
//│ get x() { return this.#x; }
//│ #y;
//│ get y() { return this.#y; }
//│ constructor(x) {
//│ this.#x = x;
Expand Down Expand Up @@ -1125,8 +1125,8 @@ I(1).J(3).a
//│ class I {
//│ #J;
//│ #x;
//│ #y;
//│ get x() { return this.#x; }
//│ #y;
//│ get y() { return this.#y; }
//│ constructor(x) {
//│ this.#x = x;
Expand All @@ -1138,8 +1138,8 @@ I(1).J(3).a
//│ if (this.#J === undefined) {
//│ class J {
//│ #z;
//│ #a;
//│ get z() { return this.#z; }
//│ #a;
//│ get a() { return this.#a; }
//│ constructor(z) {
//│ this.#z = z;
Expand Down Expand Up @@ -1378,8 +1378,8 @@ class Outer1(outer: int) {
//│ if (this.#Outer2 === undefined) {
//│ class Outer2 {
//│ #x;
//│ #outer;
//│ get x() { return this.#x; }
//│ #outer;
//│ get outer() { return this.#outer; }
//│ constructor(x) {
//│ this.#x = x;
Expand Down
2 changes: 1 addition & 1 deletion shared/src/test/diff/codegen/Super.mls
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ mixin Foo1 {
//│ const outer = this;
//│ return (class Foo1 extends base {
//│ #foo0;
//│ #foo1;
//│ get foo0() { return this.#foo0; }
//│ #foo1;
//│ get foo1() { return this.#foo1; }
//│ constructor(...rest) {
//│ super(...rest);
Expand Down

0 comments on commit ee1ab19

Please sign in to comment.