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

Fix the res running variable is not consistent with DiffTests #113

Merged
merged 1 commit into from
Jul 12, 2022
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
16 changes: 9 additions & 7 deletions shared/src/main/scala/mlscript/JSBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,8 @@ class JSTestBackend extends JSBackend {
((JSIdent("globalThis").member(sym.runtimeName) := (expr match {
case t: JSArrowFn => t.toFuncExpr(S(sym.runtimeName))
case t => t
})) ::
(resultIdent := JSIdent(sym.runtimeName)) ::
Nil)
})) :: Nil),
sym.runtimeName
)
case L(reason) => JSTestBackend.AbortedQuery(reason)
}
Expand All @@ -606,7 +605,9 @@ class JSTestBackend extends JSBackend {
case term: Term =>
try {
val body = translateTerm(term)(scope)
JSTestBackend.CodeQuery(scope.tempVars.emit(), (resultIdent := body) :: Nil)
val res = JSTestBackend.CodeQuery(scope.tempVars.emit(), (resultIdent := body) :: Nil)
scope.refreshRes()
res
} catch {
case e: UnimplementedError => JSTestBackend.AbortedQuery(e.getMessage())
case e: Throwable => throw e
Expand Down Expand Up @@ -641,18 +642,19 @@ object JSTestBackend {
/**
* The entry generates meaningful code.
*/
final case class CodeQuery(prelude: Ls[Str], code: Ls[Str]) extends Query {
final case class CodeQuery(prelude: Ls[Str], code: Ls[Str], res: Str) extends Query {

}

object CodeQuery {
def apply(decls: Opt[JSLetDecl], stmts: Ls[JSStmt]): CodeQuery =
def apply(decls: Opt[JSLetDecl], stmts: Ls[JSStmt], res: Str = "res"): CodeQuery =
CodeQuery(
decls match {
case S(stmt) => stmt.toSourceCode.toLines
case N => Nil
},
SourceCode.fromStmts(stmts).toLines
SourceCode.fromStmts(stmts).toLines,
res
)
}

Expand Down
5 changes: 5 additions & 0 deletions shared/src/main/scala/mlscript/codegen/Scope.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import scala.reflect.ClassTag
import mlscript.{TypeName, Top, Bot, TypeDef, Als, Trt, Cls}
import mlscript.MethodDef
import mlscript.Term
import mlscript.utils.AnyOps

class Scope(name: Str, enclosing: Opt[Scope]) {
private val lexicalTypeSymbols = scala.collection.mutable.HashMap[Str, TypeSymbol]()
Expand Down Expand Up @@ -301,6 +302,10 @@ class Scope(name: Str, enclosing: Opt[Scope]) {
* Shorthands for deriving function scopes.
*/
def derive(name: Str, params: Ls[Str]): Scope = Scope(name, params, this)

def refreshRes(): Unit = {
NeilKleistGao marked this conversation as resolved.
Show resolved Hide resolved
lexicalValueSymbols("res") = ValueSymbol("res", "res")
}
}

object Scope {
Expand Down
13 changes: 3 additions & 10 deletions shared/src/test/diff/codegen/Classes.mls
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def Box value = Box { inner = value }
//│ globalThis.Box1 = function Box1(value) {
//│ return new Box({ inner: value });
//│ };
//│ res = Box1;
//│ // End of generated code
//│ Box: 'inner -> Box['inner]
//│ = [Function: Box1]
Expand All @@ -43,10 +42,8 @@ def box1 = Box 1
def box2 = box1.Map (fun x -> add x 1)
//│ // Query 1
//│ globalThis.box1 = Box1(1);
//│ res = box1;
//│ // Query 2
//│ globalThis.box2 = box1.Map((x) => add(x)(1));
//│ res = box2;
//│ // End of generated code
//│ box1: Box[1]
//│ = Box { inner: 1 }
Expand Down Expand Up @@ -92,7 +89,6 @@ def MyBox inner info = MyBox { inner; info }
//│ info: info
//│ }));
//│ };
//│ res = MyBox1;
//│ // End of generated code
//│ MyBox: (int & 'inner) -> (string & 'info) -> (MyBox with {info: 'info, inner: 'inner})
//│ = [Function: MyBox1]
Expand All @@ -105,15 +101,12 @@ mb2 = mb.Map (fun x -> x * 3)
mb2.Get
//│ // Query 1
//│ globalThis.mb = MyBox1(1)("hello");
//│ res = mb;
//│ // Query 2
//│ globalThis.mb1 = mb.Inc;
//│ res = mb1;
//│ // Query 3
//│ res = mb1.Get;
//│ // Query 4
//│ globalThis.mb2 = mb1.Map((x) => x * 3);
//│ res = mb2;
//│ // Query 5
//│ res = mb2.Get;
//│ // End of generated code
Expand Down Expand Up @@ -168,13 +161,13 @@ def f: Box[?] -> int
:e
f(Box{})
//│ ╔══[ERROR] Type mismatch in application:
//│ ║ l.169: f(Box{})
//│ ║ l.162: f(Box{})
//│ ║ ^^^^^^^^
//│ ╟── record literal of type `anything` does not match type `nothing`
//│ ║ l.169: f(Box{})
//│ ║ l.162: f(Box{})
//│ ║ ^^
//│ ╟── Note: constraint arises from type wildcard:
//│ ║ l.164: def f: Box[?] -> int
//│ ║ l.157: def f: Box[?] -> int
//│ ╙── ^
//│ res: error | int
//│ = <no result>
Expand Down
3 changes: 0 additions & 3 deletions shared/src/test/diff/codegen/Declare.mls
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def q = 0
//│ // Query 1 aborted: b and a are not implemented
//│ // Query 2
//│ globalThis.q = 0;
//│ res = q;
//│ // End of generated code
//│ c: int
//│ = <no result>
Expand Down Expand Up @@ -87,10 +86,8 @@ def break = 1
def str = "str"
//│ // Query 1
//│ globalThis.break1 = 1;
//│ res = break1;
//│ // Query 2
//│ globalThis.str = "str";
//│ res = str;
//│ // End of generated code
//│ break: 1
//│ = 1
Expand Down
1 change: 0 additions & 1 deletion shared/src/test/diff/codegen/Inheritance.mls
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ checkS p && checkR p && checkQ p && checkP p
//│ r: 0,
//│ p: 0
//│ });
//│ res = p;
//│ // Query 2
//│ res = checkS(p) && checkR(p) && checkQ(p) && checkP(p);
//│ // End of generated code
Expand Down
1 change: 0 additions & 1 deletion shared/src/test/diff/codegen/Parentheses.mls
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ x = 0
x > 0 && x < 0
//│ // Query 1
//│ globalThis.x = 0;
//│ res = x;
//│ // Query 2
//│ res = x > 0 && x < 0;
//│ // End of generated code
Expand Down
2 changes: 0 additions & 2 deletions shared/src/test/diff/codegen/Polyfill.mls
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def add x y = x + y + x + y
//│ globalThis.add = function add(x) {
//│ return (y) => x + y + x + y;
//│ };
//│ res = add;
//│ // End of generated code
//│ add: int -> int -> int
//│ = [Function: add]
Expand Down Expand Up @@ -86,7 +85,6 @@ n = 123 with { x = 1 }
//│ }
//│ // Query 1
//│ globalThis.n = withConstruct(123, { x: 1 });
//│ res = n;
//│ // End of generated code
//│ n: 123 & {x: 1}
//│ = [Number: 123] { x: 1 }
Expand Down
3 changes: 0 additions & 3 deletions shared/src/test/diff/codegen/Recursion.mls
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ rec def pow0 n x =
//│ globalThis.pow0 = function pow0(n) {
//│ return (x) => n > 0 ? pow0(n - 1)(x) * x : 1;
//│ };
//│ res = pow0;
//│ // End of generated code
//│ pow0: int -> int -> int
//│ = [Function: pow0]
Expand All @@ -31,7 +30,6 @@ def pow1 x =
//│ return n > 0 ? go(n - 1) * x : 1;
//│ }));
//│ };
//│ res = pow1;
//│ // End of generated code
//│ pow1: int -> int -> int
//│ = [Function: pow1]
Expand All @@ -46,7 +44,6 @@ rec def pow2 n = { call = fun x -> if n > 0 then (pow2 (n - 1)).call x * x else
//│ globalThis.pow2 = function pow2(n) {
//│ return { call: (x) => n > 0 ? pow2(n - 1).call(x) * x : 1 };
//│ };
//│ res = pow2;
//│ // End of generated code
//│ pow2: int -> {call: int -> int}
//│ = [Function: pow2]
Expand Down
5 changes: 2 additions & 3 deletions shared/src/test/diff/codegen/Res.mls
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ res == 4
def res = 0
//│ // Query 1
//│ globalThis.res1 = 0;
//│ res = res1;
//│ // End of generated code
//│ res: 0
//│ = 0
Expand All @@ -74,7 +73,7 @@ res + 1
:js
res + 1
//│ // Query 1
//│ res = res1 + 1;
//│ res = res + 1;
//│ // End of generated code
//│ res: int
//│ = 1
//│ = 2
9 changes: 0 additions & 9 deletions shared/src/test/diff/codegen/Terms.mls
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ f 0
//│ globalThis.f = function f(x) {
//│ return x;
//│ };
//│ res = f;
//│ // Query 2
//│ res = f(0);
//│ // End of generated code
Expand Down Expand Up @@ -248,7 +247,6 @@ def f x = case x of { }
//│ throw new Error("non-exhaustive case expression");
//│ })());
//│ };
//│ res = f1;
//│ // End of generated code
//│ f: nothing -> nothing
//│ = [Function: f1]
Expand Down Expand Up @@ -358,13 +356,11 @@ rcd2 = rcd1 with { x = 1; z = 2 }
//│ x: "a",
//│ y: "b"
//│ };
//│ res = rcd1;
//│ // Query 2
//│ globalThis.rcd2 = withConstruct(rcd1, {
//│ x: 1,
//│ z: 2
//│ });
//│ res = rcd2;
//│ // Query 3
//│ res = [
//│ rcd1.x,
Expand All @@ -390,7 +386,6 @@ a1.x <- 666
a1.x
//│ // Query 1
//│ globalThis.a1 = new M({ x: 233 });
//│ res = a1;
//│ // Query 2
//│ res = (a1.x = 666, []);
//│ // Query 3
Expand All @@ -412,7 +407,6 @@ t1 = (mut "hello", mut true)
//│ "hello",
//│ true
//│ ];
//│ res = t1;
//│ // End of generated code
//│ t1: (mut 'a, mut 'b,)
//│ where
Expand Down Expand Up @@ -443,7 +437,6 @@ muta1 = (mut 1, mut 2, mut 3, mut 4)
//│ 3,
//│ 4
//│ ];
//│ res = muta1;
//│ // End of generated code
//│ muta1: MutArray[int]
//│ = <missing implementation>
Expand Down Expand Up @@ -476,7 +469,6 @@ xpp a1
//│ globalThis.xpp = function xpp(rc) {
//│ return ((_) => rc)((rc.x = rc.x + 1, []));
//│ };
//│ res = xpp;
//│ // Query 2
//│ res = xpp(a1);
//│ // End of generated code
Expand All @@ -495,7 +487,6 @@ tu._2
//│ [],
//│ 2
//│ ];
//│ res = tu;
//│ // Query 2
//│ res = (tu["_1"] = (tu["_2"] = 3, []), []);
//│ // Query 3
Expand Down
16 changes: 7 additions & 9 deletions shared/src/test/diff/codegen/TraitMethods.mls
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class B: A & T1
a = A{}
//│ // Query 1
//│ globalThis.a = new A({});
//│ res = a;
//│ // End of generated code
//│ ╔══[ERROR] Instantiation of an abstract type is forbidden
//│ ║ l.101: a = A{}
Expand Down Expand Up @@ -129,7 +128,6 @@ b = B{}
foo b
//│ // Query 1
//│ globalThis.b = new B({});
//│ res = b;
//│ // Query 2
//│ res = foo(b);
//│ // End of generated code
Expand All @@ -153,16 +151,16 @@ class BBB: AAA
method F: int
method G: 'a -> 'a
//│ ╔══[ERROR] Overriding method AAA.F without an overriding definition is not allowed.
//│ ║ l.153: method F: int
//│ ║ l.151: method F: int
//│ ║ ^^^^^^
//│ ╟── Note: method definition inherited from
//│ ║ l.149: method F = 1
//│ ║ l.147: method F = 1
//│ ╙── ^^^^^
//│ ╔══[ERROR] Overriding method AAA.G without an overriding definition is not allowed.
//│ ║ l.154: method G: 'a -> 'a
//│ ║ l.152: method G: 'a -> 'a
//│ ║ ^^^^^^^^^^^
//│ ╟── Note: method definition inherited from
//│ ║ l.151: method G x = x
//│ ║ l.149: method G x = x
//│ ╙── ^^^^^^^
//│ Defined type alias Id[+X]
//│ Defined class AAA
Expand Down Expand Up @@ -193,7 +191,7 @@ C.Foo
:e
fun x -> x.Foo
//│ ╔══[ERROR] Implicit call to method Foo is forbidden because it is ambiguous.
//│ ║ l.194: fun x -> x.Foo
//│ ║ l.192: fun x -> x.Foo
//│ ║ ^^^^^
//│ ╟── Unrelated methods named Foo are defined by:
//│ ╟── • trait T0
Expand All @@ -203,10 +201,10 @@ fun x -> x.Foo
//│ ║ l.41: trait T1
//│ ║ ^^
//│ ╟── • trait T3
//│ ║ l.178: trait T3
//│ ║ l.176: trait T3
//│ ║ ^^
//│ ╟── • trait T4
//│ ║ l.180: trait T4
//│ ║ l.178: trait T4
//│ ╙── ^^
//│ res: anything -> error
//│ = [Function: res]
Expand Down
Loading