Skip to content

Commit

Permalink
Fix codegen error for global functions (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao authored Nov 30, 2023
1 parent b8db6a1 commit 4909975
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 103 deletions.
20 changes: 10 additions & 10 deletions shared/src/main/scala/mlscript/JSBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,16 @@ abstract class JSTestBackend extends JSBackend(allowUnresolvedSymbols = false) {
case _ => die
}

otherStmts.foreach {
case fd @ NuFunDef(isLetRec, Var(nme), symNme, _, L(body)) =>
val isByname = isLetRec.isEmpty
val isByvalueRecIn = if (isByname) None else Some(true)
val bodyIsLam = body match { case _: Lam => true case _ => false }
val symb = symNme.map(_.name)
scope.declareValue(nme, isByvalueRecIn, bodyIsLam, symb)
case _ => ()
}

// don't pass `otherStmts` to the top-level module, because we need to execute them one by one later
val topModule = topLevelScope.declareTopModule("TypingUnit", Nil, typeDefs, true)
val moduleIns = topLevelScope.declareValue("typing_unit", Some(false), false, N)
Expand All @@ -1416,16 +1426,6 @@ abstract class JSTestBackend extends JSBackend(allowUnresolvedSymbols = false) {
(zeroWidthSpace + JSIdent("e") + zeroWidthSpace).log() :: Nil
)

otherStmts.foreach {
case fd @ NuFunDef(isLetRec, Var(nme), symNme, _, L(body)) if isLetRec.getOrElse(true) =>
val isByname = isLetRec.isEmpty
val isByvalueRecIn = if (isByname) None else Some(true)
val bodyIsLam = body match { case _: Lam => true case _ => false }
val symb = symNme.map(_.name)
scope.declareValue(nme, isByvalueRecIn, bodyIsLam, symb)
case _ => ()
}

// TODO Improve: (Lionel) I find this logic very strange! What's going on here?
// Why are we declaring some things above AND below?
// Why does the fact that a binding is recursive affect its declaration in the OUTER scope?
Expand Down
8 changes: 4 additions & 4 deletions shared/src/test/diff/codegen/AuxiliaryConstructors.mls
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,11 @@ n.ll
//│ class TypingUnit19 {}
//│ const typing_unit19 = new TypingUnit19;
//│ // Query 1
//│ globalThis.m = g(1);
//│ globalThis.m1 = g(1);
//│ // Query 2
//│ globalThis.n = m(2);
//│ globalThis.n1 = m1(2);
//│ // Query 3
//│ res = n.ll;
//│ res = n1.ll;
//│ // End of generated code
//│ m
//│ = [Function (anonymous)]
Expand All @@ -487,7 +487,7 @@ let mm = new M()
//│ class TypingUnit21 {}
//│ const typing_unit21 = new TypingUnit21;
//│ // Query 1
//│ globalThis.mm = new M.class();
//│ globalThis.mm1 = new M.class();
//│ // End of generated code
//│ mm
//│ = M {}
Expand Down
10 changes: 5 additions & 5 deletions shared/src/test/diff/codegen/ConstructorStmt.mls
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ let aa = A(42)
//│ class TypingUnit5 {}
//│ const typing_unit5 = new TypingUnit5;
//│ // Query 1
//│ globalThis.aa = A(42);
//│ globalThis.aa1 = A(42);
//│ // End of generated code
//│ aa
//│ = A {}
Expand All @@ -119,7 +119,7 @@ aa
//│ class TypingUnit6 {}
//│ const typing_unit6 = new TypingUnit6;
//│ // Query 1
//│ res = aa;
//│ res = aa1;
//│ // End of generated code
//│ res
//│ = A {}
Expand All @@ -131,7 +131,7 @@ let ab = A(0)
//│ class TypingUnit7 {}
//│ const typing_unit7 = new TypingUnit7;
//│ // Query 1
//│ globalThis.ab = A(0);
//│ globalThis.ab1 = A(0);
//│ // End of generated code
//│ ab
//│ = A {}
Expand Down Expand Up @@ -408,9 +408,9 @@ www.add(42)
//│ class TypingUnit15 {}
//│ const typing_unit15 = new TypingUnit15;
//│ // Query 1
//│ globalThis.www = W();
//│ globalThis.www1 = W();
//│ // Query 2
//│ res = www.add(42);
//│ res = www1.add(42);
//│ // End of generated code
//│ www
//│ = W {}
Expand Down
63 changes: 57 additions & 6 deletions shared/src/test/diff/codegen/Modules.mls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module test { // hello
//│ }


:ge // * FIXME
:js
fun y = 1
module Foo {
Expand All @@ -19,10 +18,36 @@ module Foo {
//│ module Foo {
//│ fun x: 1
//│ }
//│ Code generation encountered an error:
//│ unresolved symbol y
//│ // Prelude
//│ class TypingUnit1 {
//│ #Foo;
//│ constructor() {
//│ }
//│ get Foo() {
//│ const qualifier = this;
//│ if (this.#Foo === undefined) {
//│ class Foo {
//│ constructor() {
//│ }
//│ get x() {
//│ return y();
//│ }
//│ }
//│ this.#Foo = new Foo();
//│ this.#Foo.class = Foo;
//│ }
//│ return this.#Foo;
//│ }
//│ }
//│ const typing_unit1 = new TypingUnit1;
//│ globalThis.Foo = typing_unit1.Foo;
//│ // Query 1
//│ globalThis.y = function y() {
//│ return 1;
//│ };
//│ // End of generated code


:ge // * FIXME
:js
module Foo {
fun x = y
Expand All @@ -32,7 +57,33 @@ fun y = 1
//│ fun x: 1
//│ }
//│ fun y: 1
//│ Code generation encountered an error:
//│ unresolved symbol y
//│ // Prelude
//│ class TypingUnit2 {
//│ #Foo;
//│ constructor() {
//│ }
//│ get Foo() {
//│ const qualifier = this;
//│ if (this.#Foo === undefined) {
//│ class Foo {
//│ constructor() {
//│ }
//│ get x() {
//│ return y1();
//│ }
//│ }
//│ this.#Foo = new Foo();
//│ this.#Foo.class = Foo;
//│ }
//│ return this.#Foo;
//│ }
//│ }
//│ const typing_unit2 = new TypingUnit2;
//│ globalThis.Foo = typing_unit2.Foo;
//│ // Query 1
//│ globalThis.y1 = function y1() {
//│ return 1;
//│ };
//│ // End of generated code


36 changes: 18 additions & 18 deletions shared/src/test/diff/codegen/Nested.mls
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ bb.b
//│ class TypingUnit1 {}
//│ const typing_unit1 = new TypingUnit1;
//│ // Query 1
//│ globalThis.bb = A.B(A.a);
//│ globalThis.bb1 = A.B(A.a);
//│ // Query 2
//│ res = bb.b;
//│ res = bb1.b;
//│ // End of generated code
//│ bb
//│ = B {}
Expand Down Expand Up @@ -241,9 +241,9 @@ ee.x
//│ class TypingUnit5 {}
//│ const typing_unit5 = new TypingUnit5;
//│ // Query 1
//│ globalThis.ee = D.createE(42);
//│ globalThis.ee1 = D.createE(42);
//│ // Query 2
//│ res = ee.x;
//│ res = ee1.x;
//│ // End of generated code
//│ ee
//│ = E {}
Expand Down Expand Up @@ -361,13 +361,13 @@ gg.sum
//│ class TypingUnit7 {}
//│ const typing_unit7 = new TypingUnit7;
//│ // Query 1
//│ globalThis.es = E(1);
//│ globalThis.es1 = E(1);
//│ // Query 2
//│ globalThis.fff = es.F(2);
//│ globalThis.fff1 = es1.F(2);
//│ // Query 3
//│ globalThis.gg = fff.G(3);
//│ globalThis.gg1 = fff1.G(3);
//│ // Query 4
//│ res = gg.sum;
//│ res = gg1.sum;
//│ // End of generated code
//│ es
//│ = E {}
Expand Down Expand Up @@ -559,11 +559,11 @@ i.x
//│ class TypingUnit10 {}
//│ const typing_unit10 = new TypingUnit10;
//│ // Query 1
//│ globalThis.jj = G.H.J(42);
//│ globalThis.jj1 = G.H.J(42);
//│ // Query 2
//│ globalThis.i = jj.ii(2);
//│ globalThis.i1 = jj1.ii(2);
//│ // Query 3
//│ res = i.x;
//│ res = i1.x;
//│ // End of generated code
//│ jj
//│ = J {}
Expand Down Expand Up @@ -666,9 +666,9 @@ j.i.x
//│ class TypingUnit12 {}
//│ const typing_unit12 = new TypingUnit12;
//│ // Query 1
//│ globalThis.j = H.J(42);
//│ globalThis.j1 = H.J(42);
//│ // Query 2
//│ res = j.i.x;
//│ res = j1.i.x;
//│ // End of generated code
//│ j
//│ = J {}
Expand Down Expand Up @@ -757,11 +757,11 @@ ij.incY
//│ const typing_unit13 = new TypingUnit13;
//│ globalThis.I = typing_unit13.I;
//│ // Query 1
//│ globalThis.i1 = I(1);
//│ globalThis.i3 = I(1);
//│ // Query 2
//│ globalThis.ij = i1.J(0);
//│ globalThis.ij1 = i3.J(0);
//│ // Query 3
//│ res = ij.incY;
//│ res = ij1.incY;
//│ // End of generated code
//│ i
//│ = I {}
Expand Down Expand Up @@ -890,9 +890,9 @@ let n = J.N(2)
//│ class TypingUnit15 {}
//│ const typing_unit15 = new TypingUnit15;
//│ // Query 1
//│ globalThis.m = J.M();
//│ globalThis.m1 = J.M();
//│ // Query 2
//│ globalThis.n = J.N(2);
//│ globalThis.n1 = J.N(2);
//│ // End of generated code
//│ m
//│ = M {}
Expand Down
4 changes: 2 additions & 2 deletions shared/src/test/diff/codegen/New.mls
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let c = C
//│ class TypingUnit3 {}
//│ const typing_unit3 = new TypingUnit3;
//│ // Query 1
//│ globalThis.c = C;
//│ globalThis.c1 = C;
//│ // End of generated code
//│ c
//│ = [class C]
Expand Down Expand Up @@ -93,7 +93,7 @@ let c = C
//│ class TypingUnit8 {}
//│ const typing_unit8 = new TypingUnit8;
//│ // Query 1
//│ globalThis.c1 = C;
//│ globalThis.c3 = C;
//│ // End of generated code
//│ c
//│ = [Function (anonymous)] {
Expand Down
2 changes: 1 addition & 1 deletion shared/src/test/diff/codegen/NewMatching.mls
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fun foo(s) =
//│ // Query 1
//│ globalThis.foo = function foo(s) {
//│ return ((() => {
//│ return s instanceof Some.class ? (([t]) => ((b) => b + t.x)(s2.value))(Some.unapply(s)) : 0;
//│ return s instanceof Some.class ? (([t]) => ((b) => b + t.x)(s21.value))(Some.unapply(s)) : 0;
//│ })());
//│ };
//│ // End of generated code
Expand Down
4 changes: 2 additions & 2 deletions shared/src/test/diff/codegen/NuReplHost.mls
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let r = foo(1)
//│ └─┬ Query 2/2
//│ ├── Prelude: <empty>
//│ ├── Code:
//│ ├── globalThis.r = foo(1);
//│ ├── globalThis.r1 = foo(1);
//│ └── Reply: [runtime error] Error: an error was thrown
//│ r
//│ Runtime error:
Expand All @@ -44,7 +44,7 @@ r
//│ nothing
//│ res
//│ Runtime error:
//│ ReferenceError: r is not defined
//│ ReferenceError: r1 is not defined



10 changes: 5 additions & 5 deletions shared/src/test/diff/codegen/SymbolicOps.mls
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let r = succ >> succ
//│ class TypingUnit1 {}
//│ const typing_unit1 = new TypingUnit1;
//│ // Query 1
//│ globalThis.r = compose(succ, succ);
//│ globalThis.r1 = compose(succ, succ);
//│ // End of generated code
//│ r
//│ = [Function (anonymous)]
Expand Down Expand Up @@ -65,7 +65,7 @@ let f = (>>)
//│ class TypingUnit7 {}
//│ const typing_unit7 = new TypingUnit7;
//│ // Query 1
//│ globalThis.f = compose;
//│ globalThis.f1 = compose;
//│ // End of generated code
//│ f
//│ = [Function: compose]
Expand Down Expand Up @@ -315,7 +315,7 @@ fun (:-D) dd(a, b) = a + b
val (->) f(x, y) = [x, y]
//│ val (->) f: forall 'a 'b. ('a, 'b) -> ['a, 'b]
//│ f
//│ = [Function: f1]
//│ = [Function: f3]

12 -> 34
//│ [12, 34]
Expand All @@ -326,7 +326,7 @@ val (->) f(x, y) = [x, y]
let (->) _ = f
//│ let (->) _: forall 'a 'b. ('a, 'b) -> ['a, 'b]
//│ _
//│ = [Function: f1]
//│ = [Function: f3]

:js
12 -> 34
Expand All @@ -335,7 +335,7 @@ let (->) _ = f
//│ class TypingUnit42 {}
//│ const typing_unit42 = new TypingUnit42;
//│ // Query 1
//│ res = _(12, 34);
//│ res = _1(12, 34);
//│ // End of generated code
//│ res
//│ = [ 12, 34 ]
Expand Down
2 changes: 1 addition & 1 deletion shared/src/test/diff/mlscript/Sequence.mls
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let test(x) = log(x); x + 1
//│ let test: Int -> Int
//│ test
//│ = [Function: test]
//│ = [Function: test1]

test(log("here we go"); 123)
//│ Int
Expand Down
4 changes: 2 additions & 2 deletions shared/src/test/diff/nu/FlatIndentFuns.mls
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ y =>
x + y
//│ let r: Int -> Int -> Int
//│ r
//│ = [Function: r]
//│ = [Function: r1]

r(1)(2)
//│ Int
Expand Down Expand Up @@ -59,6 +59,6 @@ x + y
//│ ╙──
//│ let r: Int -> Int -> Int
//│ r
//│ = [Function: r1]
//│ = [Function: r3]


Loading

0 comments on commit 4909975

Please sign in to comment.