Skip to content

Commit 4ac5772

Browse files
committed
Fix statement codegen for quote
1 parent b2fae3e commit 4ac5772

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,7 @@ abstract class JSBackend(allowUnresolvedSymbols: Bool) {
308308
else Blk(stmts.map {
309309
case t: Term =>
310310
desugarQuote(t)(blkScope, isQuoted, freeVars)
311-
case nd @ NuFunDef(isLetRec, nme, symbol, tparams, rhs) => // * TODO: other cases in addition to let?
312-
NuFunDef(isLetRec, nme, symbol, tparams, rhs match {
313-
case L(t) => L(desugarQuote(t)(blkScope, isQuoted, freeVars))
314-
case R(t) => R(t)
315-
})(nd.declareLoc, nd.virtualLoc, nd.mutLoc, nd.signature, nd.outer, nd.genField, nd.annotations)
316-
case s => throw CodeGenError(s"statement $s is not supported in quasiquotes")
311+
case s => desugarStatementForQuote(s)(blkScope, isQuoted, freeVars)
317312
})
318313
case Tup(eles) =>
319314
def toVar(b: Bool) = if (b) Var("true") else Var("false")
@@ -354,6 +349,25 @@ abstract class JSBackend(allowUnresolvedSymbols: Bool) {
354349
throw CodeGenError("this quote syntax is not supported yet.")
355350
}
356351

352+
private def desugarStatementForQuote(s: Statement)(implicit scope: Scope, isQuoted: Bool, freeVars: FreeVars): Statement = s match {
353+
case nd @ NuFunDef(isLetRec, nme, symbol, tparams, rhs) =>
354+
NuFunDef(isLetRec, nme, symbol, tparams, rhs match {
355+
case L(t) => L(desugarQuote(t))
356+
case R(t) => R(t)
357+
})(nd.declareLoc, nd.virtualLoc, nd.mutLoc, nd.signature, nd.outer, nd.genField)
358+
case nt @ NuTypeDef(kind, nme, tparams, params, ctor, sig, parents, superAnnot, thisAnnot, TypingUnit(body)) =>
359+
NuTypeDef(kind, nme, tparams, params, ctor.map(c => desugarStatementForQuote(c) match {
360+
case c: Constructor => c
361+
case _ => die
362+
}), sig, parents.map(p => desugarQuote(p)), superAnnot, thisAnnot, TypingUnit(body.map(s => desugarStatementForQuote(s))))(nt.declareLoc, nt.abstractLoc)
363+
case Constructor(ps, body) => Constructor(ps, desugarQuote(body) match {
364+
case b: Blk => b
365+
case _ => die
366+
})
367+
case t: Term => desugarQuote(t)
368+
case _: LetS | _: DataDefn | _: DatatypeDefn | _: TypeDef | _: Def => die // * Impossible. newDef is true
369+
}
370+
357371
/**
358372
* Translate MLscript terms into JavaScript expressions.
359373
*/

shared/src/test/diff/qq/Codegen.mls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,30 @@ x `=>
372372
`0
373373
v.value
374374
//│ Code[forall 'a. 'a -> 'a, ??y & ~??x]
375+
376+
:ne
377+
x `=>
378+
class A(y: Code[Int, nothing]) {
379+
val z = x `+ y
380+
}
381+
A(`0).z
382+
//│ Code[Int -> Int, nothing]
383+
384+
:ne
385+
x `=>
386+
class A() {
387+
constructor() {
388+
log(x)
389+
}
390+
}
391+
new A(), x
392+
//│ Code[forall 'a. 'a -> 'a, nothing]
393+
394+
class Foo(x: Code[Int, anything])
395+
//│ class Foo(x: Code[Int, anything])
396+
397+
:ne
398+
x `=>
399+
class B() extends Foo(x)
400+
`x
401+
//│ Code[forall 'a. (Int & 'a) -> 'a, nothing]

0 commit comments

Comments
 (0)