Skip to content

Commit ed1efa5

Browse files
committed
Implement code generation for the new syntax
1 parent 4c11d87 commit ed1efa5

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

shared/src/main/scala/mlscript/JSBackend.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,13 @@ class JSBackend(allowUnresolvedSymbols: Boolean) {
240240
case N => throw CodeGenError(s"if expression has not been not desugared")
241241
case S(term) => translateTerm(term)
242242
}
243-
case _: Bind | _: Test | If(_, _) | New(_, _) | TyApp(_, _) | _: Splc =>
243+
case New(N, TypingUnit(Nil)) => JSRecord(Nil)
244+
case New(S(TypeName(className) -> Tup(args)), TypingUnit(Nil)) =>
245+
val callee = translateVar(className, true)
246+
callee(args.map { case (_, Fld(_, _, arg)) => translateTerm(arg) }: _*)
247+
case New(_, TypingUnit(_)) =>
248+
throw CodeGenError("custom class body is not supported yet")
249+
case _: Bind | _: Test | If(_, _) | TyApp(_, _) | _: Splc =>
244250
throw CodeGenError(s"cannot generate code for term ${inspect(term)}")
245251
}
246252

shared/src/test/diff/codegen/Terms.mls

+8-2
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,17 @@ case A { a = 0 } of
344344

345345
// With
346346
{} with {}
347-
:js
348347
rcd1 = { x = "a"; y = "b" }
349348
rcd2 = rcd1 with { x = 1; z = 2 }
350349
(rcd1.x, rcd1.y, rcd2.x, rcd2.y, rcd2.z)
351-
//│ /!\ Parse error: Expected end-of-input:1:12, found ":js;rcd1 =" at l.347:1: :js
350+
//│ res: anything
351+
//│ = {}
352+
//│ rcd1: {x: "a", y: "b"}
353+
//│ = { x: 'a', y: 'b' }
354+
//│ rcd2: {x: 1, y: "b", z: 2}
355+
//│ = { x: 1, y: 'b', z: 2 }
356+
//│ res: ("a", "b", 1, "b", 2,)
357+
//│ = [ 'a', 'b', 1, 'b', 2 ]
352358

353359
class M: {mut x : int}
354360
//│ Defined class M

shared/src/test/diff/nu/New.mls

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ let f = Foo(1)
1010
//│ f: Foo & {x: 1}
1111
//│ = Foo { x: 1 }
1212

13-
:ge
1413
let f = new Foo(1)
1514
//│ f: Foo & {x: 1}
16-
//│ Code generation encountered an error:
17-
//│ cannot generate code for term New(Some((TypeName(Foo),1,)), TypingUnit(List()))
15+
//│ = Foo { x: 1 }
1816

1917
if f is Foo(a) then a else 0
2018
//│ res: 1
@@ -38,3 +36,11 @@ test(f)
3836
//│ res: 1
3937
//│ = 1
4038

39+
class Point(x, y)
40+
//│ Defined class Point
41+
//│ Point: ('x, 'y,) -> (Point with {x: 'x, y: 'y})
42+
//│ = [Function: Point1]
43+
44+
let origin = new Point(0, 0)
45+
//│ origin: Point & {x: 0, y: 0}
46+
//│ = Point { x: 0, y: 0 }

0 commit comments

Comments
 (0)