From bf9e386b991f1e12aa3966631b528716587a1b99 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Tue, 11 Nov 2025 17:01:02 +0800 Subject: [PATCH 01/72] Add scoped definition --- .../shared/src/main/scala/hkmc2/codegen/Block.scala | 13 +++++++++++++ .../main/scala/hkmc2/codegen/BlockTransformer.scala | 3 +++ .../main/scala/hkmc2/codegen/BlockTraverser.scala | 1 + .../src/main/scala/hkmc2/codegen/js/JSBuilder.scala | 8 ++++++++ 4 files changed, 25 insertions(+) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 65df96ab47..985f1f7d78 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,6 +49,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars + case Scoped(syms, body) => syms.toSet ++ body.definedVars lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -62,6 +63,7 @@ sealed abstract class Block extends Product: case TryBlock(sub, fin, rst) => 1 + sub.size + fin.size + rst.size case Label(_, _, bod, rst) => 1 + bod.size + rst.size case HandleBlock(lhs, res, par, args, cls, handlers, bdy, rst) => 1 + handlers.map(_.body.size).sum + bdy.size + rst.size + case Scoped(_, body) => 1 + body.size // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match @@ -101,6 +103,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) + case Scoped(syms, body) => body.freeVars -- syms.toSet case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -121,6 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) + case Scoped(syms, body) => body.freeVars -- syms.toSet case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match @@ -133,6 +137,7 @@ sealed abstract class Block extends Product: case Define(d, rest) => d.subBlocks ::: rest :: Nil case HandleBlock(_, _, par, args, _, handlers, body, rest) => par.subBlocks ++ args.flatMap(_.subBlocks) ++ handlers.map(_.body) :+ body :+ rest case Label(_, _, body, rest) => body :: rest :: Nil + case Scoped(_, body) => body :: Nil // TODO rm Lam from values and thus the need for these cases case Return(r, _) => r.subBlocks @@ -247,6 +252,12 @@ sealed abstract class Block extends Product: then this else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) + case Scoped(syms, body) => + val newBody = body.flatten(k) + if newBody is body + then this + else Scoped(syms, newBody) + case e: End => k(e) case t: BlockTail => this @@ -272,6 +283,8 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail +case class Scoped(syms: Ls[Local], body: Block) extends BlockTail + // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala index c1b753f5c5..22c5ab5fab 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala @@ -95,6 +95,9 @@ class BlockTransformer(subst: SymbolSubst): if (lhs2 is lhs) && (fld2 is fld) && (rhs2 is rhs) && (rest2 is rest) then b else AssignDynField(lhs2, fld2, arrayIdx, rhs2, rest2) + case Scoped(s, bd) => + val nb = applySubBlock(bd) + if nb is bd then b else Scoped(s, nb) def applyRcdArg(rcdArg: RcdArg)(k: RcdArg => Block): Block = val RcdArg(idx, p) = rcdArg diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala index ab1ce80d3a..67e95f12c5 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala @@ -51,6 +51,7 @@ class BlockTraverser: applyResult(rhs) applyPath(fld) applySubBlock(rest) + case Scoped(_, body) => applySubBlock(body) def applyResult(r: Result): Unit = r match case r @ Call(fun, args) => applyPath(fun); args.foreach(applyArg) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index f9a6858107..f33e8427a0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -501,6 +501,14 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: braced(returningTerm(fin, endSemi = false)) } # ${ returningTerm(rst, endSemi).stripBreaks}" + + case Scoped(syms, body) => + val vars = syms.toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) + (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc";\n") :: returningTerm(body, endSemi) // case _ => ??? From 887ca0b25514f53ac07cb6d5b5ae10ca889cf0f2 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 14 Nov 2025 15:12:19 +0800 Subject: [PATCH 02/72] WIP: Try to get symbols for scoped --- hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala | 8 ++++---- .../src/main/scala/hkmc2/codegen/HandlerLowering.scala | 1 + hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala | 2 +- hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala | 1 + .../src/main/scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../src/main/scala/hkmc2/codegen/llir/Builder.scala | 1 + .../main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala | 1 + hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala | 7 +++++++ .../src/test/scala/hkmc2/JSBackendDiffMaker.scala | 1 + 9 files changed, 18 insertions(+), 6 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 985f1f7d78..b34cf37802 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,7 +49,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - case Scoped(syms, body) => syms.toSet ++ body.definedVars + case Scoped(syms, body) => body.definedVars -- syms lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -103,7 +103,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body) => body.freeVars -- syms.toSet + case Scoped(syms, body) => body.freeVars -- syms case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -124,7 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body) => body.freeVars -- syms.toSet + case Scoped(syms, body) => body.freeVarsLLIR -- syms case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match @@ -283,7 +283,7 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: Ls[Local], body: Block) extends BlockTail +case class Scoped(syms: Set[Local], body: Block) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index 16cd54c33c..caba043f65 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -417,6 +417,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, // ignored cases case TryBlock(sub, finallyDo, rest) => ??? // ignore case Throw(_) => PartRet(blk, Nil) + case Scoped(_, body) => go(body) case _: HandleBlock => lastWords("unexpected handleBlock") // already translated at this point val PartRet(head, states) = go(blk)(using labelIds, N) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 5fa8efc9ce..7d7712d7eb 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -959,7 +959,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - (paramLists, returnedTerm(bodyTerm)) + (paramLists, Scoped(bodyTerm.definedSyms, returnedTerm(bodyTerm))) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala index 6339500106..6b8a682868 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala @@ -55,6 +55,7 @@ object Printer: doc"set ${mkDocument(lhs)}.${nme.name} = ${mkDocument(rhs)} in # ${mkDocument(rest)}" case Define(defn, rest) => doc"define ${mkDocument(defn)} in # ${mkDocument(rest)}" + case Scoped(_, body) => mkDocument(body) case End("") => doc"end" case End(msg) => doc"end ${msg}" case _ => TODO(blk) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index f33e8427a0..93c6eb0937 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -508,7 +508,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") - :: doc";\n") :: returningTerm(body, endSemi) + :: doc";") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala index 0199cb0da3..540f6dda29 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala @@ -512,6 +512,7 @@ final class LlirBuilder(using Elaborator.State)(tl: TraceLogger, uid: FreshInt): bBlock(rest)(k)(ct) case Define(_: ClsLikeDefn, rest) => bBlock(rest)(k)(ct) case End(msg) => k(Expr.Literal(Tree.UnitLit(false))) + case Scoped(_, body) => bBlock(body)(k)(ct) case _: Block => val docBlock = blk.showAsTree bErrStop(msg"Unsupported block: $docBlock") diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala index 967bbee669..1347a1a36e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala @@ -564,6 +564,7 @@ class WatBuilder(using TraceLogger, State) extends CodeBuilder: `return`(S(resWat)) + case Scoped(_, body) => returningTerm(body) case End(_) => nop case t => diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 79ae65ed97..c0f0d227d8 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -363,6 +363,13 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdField(field, rhs) => RcdField(field.mkClone, rhs.mkClone) case RcdSpread(rcd) => RcdSpread(rcd.mkClone) case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) + + lazy val definedSyms: Set[Symbol] = this match + case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty + case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + case LetDecl(sym, annotations) => Set(sym) + case DefineVar(sym, rhs) => Set(sym) + case _ => Set.empty // TODO: add other cases def describe: Str = val desc = this match diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 2bf32e5879..99fb532cd6 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -114,6 +114,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: case Return(res, implct) => assert(implct) Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case Scoped(_, body) => ??? // TODO case tl: (Throw | Break | Continue) => tl ) if showLoweredTree.isSet then From 148006b9498f18a67bea27793308999c9f95e26f Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 16 Nov 2025 18:08:45 +0800 Subject: [PATCH 03/72] wip --- .../src/main/scala/hkmc2/codegen/Block.scala | 2 +- .../main/scala/hkmc2/codegen/Lowering.scala | 5 +-- .../src/main/scala/hkmc2/semantics/Term.scala | 18 +++++++++- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 36 ++++++++++++++----- 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index b34cf37802..36b717b9fd 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -63,7 +63,7 @@ sealed abstract class Block extends Product: case TryBlock(sub, fin, rst) => 1 + sub.size + fin.size + rst.size case Label(_, _, bod, rst) => 1 + bod.size + rst.size case HandleBlock(lhs, res, par, args, cls, handlers, bdy, rst) => 1 + handlers.map(_.body.size).sum + bdy.size + rst.size - case Scoped(_, body) => 1 + body.size + case Scoped(_, body) => body.size // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 7d7712d7eb..b8f9e33ddf 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -123,7 +123,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - blockImpl(imps ::: funs ::: rest, res)(k) + val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) + Scoped(definedVars.toSet, blockImpl(imps ::: funs ::: rest, res)(k)) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = stats match @@ -959,7 +960,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - (paramLists, Scoped(bodyTerm.definedSyms, returnedTerm(bodyTerm))) + (paramLists, returnedTerm(bodyTerm)) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index c0f0d227d8..da9a546848 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -368,7 +368,23 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) case LetDecl(sym, annotations) => Set(sym) - case DefineVar(sym, rhs) => Set(sym) + // TODO: make sure + case tdef: TermDefinition => Set(tdef.sym) + case ModuleOrObjectDef(owner, sym, bsym, tparams, paramsOpt, auxParams, ext, kind, body, companion, annotations) => Set(sym) + case PatternDef(owner, sym, bsym, tparams, parameters, patternParams, extractionParams, pattern, annotations) => Set(bsym) + case ClassDef.Parameterized(owner, kind, sym, bsym, tparams, params, auxParams, ext, body, companion, annotations) => Set(bsym) + case ClassDef.Plain(owner, kind, sym, bsym, tparams, ext, body, companion, annotations) => Set(bsym) + case TypeDef(sym, bsym, tparams, rhs, companion, annotations) => Set(bsym) + case Import(sym, str, file) => Set(sym) + + // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. + // And including the sym here may cause error for delayed init in a function: + // ``` + // let x + // fun f() = + // x = 2 + // ``` + // case DefineVar(sym, rhs) => Set(sym) case _ => Set.empty // TODO: add other cases def describe: Str = diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 99fb532cd6..0471aabd0c 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -108,15 +108,24 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: with JSBuilderArgNumSanityChecks val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) - val le = lowered0.copy(main = lowered0.main.mapTail: - case e: End => + + // TODO: remove mutation + var toplvlDefinedVars = Set.empty[Symbol] + def assignResSym(b: Block, toplvl: Boolean): Block = + b.mapTail: + case e: End => Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - case Return(res, implct) => - assert(implct) - Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case Scoped(_, body) => ??? // TODO - case tl: (Throw | Break | Continue) => tl - ) + case Return(res, implct) => + assert(implct) + Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case s@Scoped(xs, body) => + if toplvl then + toplvlDefinedVars = xs + assignResSym(body, false) + else + Scoped(xs, assignResSym(body, false)) + case tl: (Throw | Break | Continue) => tl + val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then output(s"Lowered:") output(le.showAsTree) @@ -132,9 +141,18 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: output(s"Pretty Lowered:") output(Printer.mkDocument(le)(using summon[Raise], nestedScp).mkString()) + // NOTE: `blockPreamble` should still take care of those vars that are generated during or after the lowering stage, + // while `Scoped` more reflects the declared vars in the source (or elaborated?) level? + val varsFromScopedStr = + val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) + (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc";").stripBreaks.mkString(100) val (pre, js) = nestedScp.givenIn: jsb.worksheet(le) - val preStr = pre.stripBreaks.mkString(100) + val preStr = pre.stripBreaks.mkString(100) + varsFromScopedStr val jsStr = js.stripBreaks.mkString(100) if showSanitizedJS.isSet then output(s"JS:") From b28875ae0781b0914dee03de837f83631e228e5e Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 16 Nov 2025 22:51:22 +0800 Subject: [PATCH 04/72] wip and some tests --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 9 +- .../src/test/mlscript/codegen/Scoped.mls | 125 ++++++++++++++++++ .../test/scala/hkmc2/JSBackendDiffMaker.scala | 3 +- 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 hkmc2/shared/src/test/mlscript/codegen/Scoped.mls diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 93c6eb0937..255182698a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -503,7 +503,13 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - val vars = syms.toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) + val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => + if scope.lookup(l).isDefined then + raise: + WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) + None + else + Some(l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme @@ -597,6 +603,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: :: doc";" def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = + // println(s"$t :::::::: ${t.definedVars}") blockPreamble(t.definedVars) :: returningTerm(t, endSemi) def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn: diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls new file mode 100644 index 0000000000..8c0a51a762 --- /dev/null +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -0,0 +1,125 @@ +:js + + +// only the `let x` should be declared in the top level of repl? +// now `y` is declared in the "correct" place, but not `tmp` because `tmp` is +// handled by `blockPreamble` +:showRepl +:sjs +let x = + let y = 1 + 3 * 4 + y + 4 +//│ JS (unsanitized): +//│ let tmp; let x; let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ REPL> Sending: block$res1 = undefined +//│ REPL> Collected: +//│ > undefined +//│ REPL> Sending: let tmp;let x;try { let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; block$res1 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Collected: +//│ > undefined +//│ REPL> Parsed: +//│ > undefined +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Collected: +//│ > undefined +//│ > Unit {} +//│ REPL> Parsed: +//│ > undefined +//│ > Unit {} +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Collected: +//│ > 17 +//│ > Unit {} +//│ REPL> Parsed: +//│ > 17 +//│ > Unit {} +//│ x = 17 + + + + +// `a` and `b` is declared in the correct places +:sjs +fun f() = + if true then + let a = 4 + a + 4 + else + let b = 5 + b + 9 +//│ JS (unsanitized): +//│ let f; +//│ f = function f() { +//│ let scrut; +//│ scrut = true; +//│ if (scrut === true) { let a; a = 4; return a + 4 } else { let b; b = 5; return b + 9 } +//│ }; + + + + + + +// `let a` is declared in the correct place, but not `lambda` +// NOTE: `tmp1` and `tmp2` should indeed be declared in the top level of the function body +// but not `lambda` +:sjs +fun g(x, y, z) = + 3 + 4 * if + x then 0 + y then 0 + z then + let a = 1 + (_ + a) +//│ JS (unsanitized): +//│ let g; +//│ g = function g(x1, y1, z) { +//│ let tmp1, tmp2, lambda; +//│ split_root$: { +//│ split_1$: { +//│ if (x1 === true) { +//│ break split_1$ +//│ } else { +//│ if (y1 === true) { +//│ break split_1$ +//│ } else { +//│ if (z === true) { +//│ let a; +//│ a = 1; +//│ lambda = (undefined, function (_0) { +//│ return _0 + a +//│ }); +//│ tmp1 = lambda; +//│ break split_root$ +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ } +//│ } +//│ } +//│ tmp1 = 0; +//│ } +//│ tmp2 = 4 * tmp1; +//│ return 3 + tmp2 +//│ }; + + +// TODO: +:e +module M +//│ FAILURE: Unexpected exception +//│ /!!!\ Uncaught error: java.lang.AssertionError: assertion failed: (module:M,Scope: +//│ parent = N +//│ curThis = S of S of globalThis:globalThis +//│ bindings = HashMap($block$res -> block$res4, class:M -> M1, $block$res -> block$res2, $runtime -> runtime, member:g -> g, $definitionMetadata -> definitionMetadata, $prettyPrint -> prettyPrint, $Term -> Term, $Block -> Block, $Shape -> Shape, $block$res -> block$res, x -> x, y -> y, member:Predef -> Predef, $block$res -> block$res3, $block$res -> block$res1, $tmp -> tmp, member:f -> f, module:M -> M)) +//│ at: scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) +//│ at: hkmc2.utils.Scope.addToBindings(Scope.scala:50) +//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$2(JSBuilder.scala:556) +//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$adapted$1(JSBuilder.scala:560) +//│ at: hkmc2.utils.TraceLogger.trace(TraceLogger.scala:17) +//│ at: hkmc2.codegen.js.JSBuilder.go$1(JSBuilder.scala:560) +//│ at: hkmc2.codegen.js.JSBuilder.reserveNames(JSBuilder.scala:561) +//│ at: hkmc2.codegen.js.JSBuilder.worksheet(JSBuilder.scala:589) +//│ at: hkmc2.JSBackendDiffMaker.processTerm(JSBackendDiffMaker.scala:155) +//│ at: hkmc2.BbmlDiffMaker.processTerm(BbmlDiffMaker.scala:36) + diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 0471aabd0c..2c9c910fd6 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -128,7 +128,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then output(s"Lowered:") - output(le.showAsTree) + output(lowered0.showAsTree) // * We used to do this to avoid needlessly generating new variable names in separate blocks: // val nestedScp = baseScp.nest @@ -143,6 +143,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: // NOTE: `blockPreamble` should still take care of those vars that are generated during or after the lowering stage, // while `Scoped` more reflects the declared vars in the source (or elaborated?) level? + // Or, during the lowering, we should also find the correct tmp vars and add them to the set in `Scoped` val varsFromScopedStr = val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) (if vars.isEmpty then doc"" else From 5f9e63c768d98191c509d81e4d7da2fb4c5dfe41 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Mon, 17 Nov 2025 19:02:39 +0800 Subject: [PATCH 05/72] WIP: Try --- .../src/main/scala/hkmc2/codegen/Block.scala | 27 +++++++- .../main/scala/hkmc2/codegen/Lowering.scala | 6 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 19 +++--- .../src/main/scala/hkmc2/semantics/Term.scala | 19 +----- .../src/test/mlscript-compile/Predef.mjs | 6 +- .../src/test/mlscript-compile/Runtime.mjs | 27 +++++--- .../src/test/mlscript/basics/LazySpreads.mls | 10 +-- .../src/test/mlscript/bbml/bbCodeGen.mls | 6 +- .../src/test/mlscript/bbml/bbGetters.mls | 8 ++- .../src/test/mlscript/codegen/BasicTerms.mls | 12 +--- .../src/test/mlscript/codegen/CaseOfCase.mls | 3 +- .../src/test/mlscript/codegen/ClassInFun.mls | 5 +- .../test/mlscript/codegen/ClassMatching.mls | 9 ++- .../test/mlscript/codegen/FieldSymbols.mls | 67 +++++++++---------- .../src/test/mlscript/codegen/FunInClass.mls | 6 +- .../src/test/mlscript/codegen/Getters.mls | 18 +++-- .../test/mlscript/codegen/PlainClasses.mls | 19 +++--- .../src/test/mlscript/codegen/Scoped.mls | 66 ++++++++++-------- .../src/test/mlscript/codegen/SetIn.mls | 20 +++--- .../src/test/mlscript/codegen/Throw.mls | 2 +- .../src/test/mlscript/codegen/While.mls | 24 ++++--- .../src/test/mlscript/lifter/ClassInFun.mls | 1 + .../src/test/mlscript/lifter/FunInFun.mls | 7 +- .../shared/src/test/mlscript/lifter/Loops.mls | 3 +- .../src/test/mlscript/lifter/Mutation.mls | 1 + .../src/test/mlscript/objbuf/Mutation.mls | 3 +- .../ucs/normalization/Deduplication.mls | 3 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 9 ++- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 61 ++++++++++------- 29 files changed, 267 insertions(+), 200 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 36b717b9fd..9d63e962ad 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,7 +49,27 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - case Scoped(syms, body) => body.definedVars -- syms + // cannot use `body.definedVars - syms`. lifter computes captures based on it? + case Scoped(syms, body) => body.definedVars + + // TODO: remove it + lazy val tempVars: Set[Local] = this match + case _: Return | _: Throw => Set.empty + case Begin(sub, rst) => sub.tempVars ++ rst.tempVars + case Assign(l: TempSymbol, r, rst) => rst.tempVars + l + case Assign(l, r, rst) => rst.tempVars + case AssignField(l, n, r, rst) => rst.tempVars + case AssignDynField(l, n, ai, r, rst) => rst.tempVars + case Match(scrut, arms, dflt, rst) => + arms.flatMap(_._2.tempVars).toSet ++ dflt.toList.flatMap(_.tempVars) ++ rst.tempVars + case End(_) => Set.empty + case Break(_) => Set.empty + case Continue(_) => Set.empty + case Define(defn, rst) => rst.tempVars + case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.tempVars + case TryBlock(sub, fin, rst) => sub.tempVars ++ fin.tempVars ++ rst.tempVars + case Label(lbl, _, bod, rst) => bod.tempVars ++ rst.tempVars + case Scoped(syms, body) => body.tempVars lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -67,6 +87,7 @@ sealed abstract class Block extends Product: // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match + case Scoped(syms, body) => Scoped(syms, body.mapTail(f)) case b: BlockTail => f(b) case Begin(sub, rst) => Begin(sub, rst.mapTail(f)) case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f)) @@ -103,7 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body) => body.freeVars -- syms + case Scoped(syms, body) => body.freeVars case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -124,7 +145,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body) => body.freeVarsLLIR -- syms + case Scoped(syms, body) => body.freeVarsLLIR case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index b8f9e33ddf..cd343d9e77 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -123,8 +123,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) - Scoped(definedVars.toSet, blockImpl(imps ::: funs ::: rest, res)(k)) + blockImpl(imps ::: funs ::: rest, res)(k) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = stats match @@ -960,7 +959,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - (paramLists, returnedTerm(bodyTerm)) + val body = returnedTerm(bodyTerm) + (paramLists, Scoped(bodyTerm.definedSyms ++ body.tempVars, body)) // TODO: move it to block function def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 255182698a..34393f141d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -503,13 +503,8 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => - if scope.lookup(l).isDefined then - raise: - WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) - None - else - Some(l -> scope.allocateName(l)) + // TODO: we should remove `syms.filter` after temp vars can be correctly handled. + val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme @@ -585,11 +580,17 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case N => doc"" ) + // TODO: get rid of it? + private def getDefinedVars(blk: Block): Set[Local] = blk match + case Scoped(syms, body) => body.definedVars -- syms + case _ => blk.definedVars + + def worksheet(p: Program)(using Raise, Scope): (Document, Document) = reserveNames(p) lazy val imps = p.imports.map: i => doc"""${getVar(i._1, N)} = await import("${i._2.toString}").then(m => m.default ?? m);""" - blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVars.toSeq) -> + blockPreamble(p.imports.map(_._1).toSeq ++ getDefinedVars(p.main).toSeq) -> (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) def blockPreamble(ss: Iterable[Symbol])(using Raise, Scope): Document = @@ -604,7 +605,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = // println(s"$t :::::::: ${t.definedVars}") - blockPreamble(t.definedVars) :: returningTerm(t, endSemi) + blockPreamble(getDefinedVars(t)) :: returningTerm(t, endSemi) def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn: block(t, endSemi) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index da9a546848..34596bd89a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -368,24 +368,7 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) case LetDecl(sym, annotations) => Set(sym) - // TODO: make sure - case tdef: TermDefinition => Set(tdef.sym) - case ModuleOrObjectDef(owner, sym, bsym, tparams, paramsOpt, auxParams, ext, kind, body, companion, annotations) => Set(sym) - case PatternDef(owner, sym, bsym, tparams, parameters, patternParams, extractionParams, pattern, annotations) => Set(bsym) - case ClassDef.Parameterized(owner, kind, sym, bsym, tparams, params, auxParams, ext, body, companion, annotations) => Set(bsym) - case ClassDef.Plain(owner, kind, sym, bsym, tparams, ext, body, companion, annotations) => Set(bsym) - case TypeDef(sym, bsym, tparams, rhs, companion, annotations) => Set(bsym) - case Import(sym, str, file) => Set(sym) - - // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. - // And including the sym here may cause error for delayed init in a function: - // ``` - // let x - // fun f() = - // x = 2 - // ``` - // case DefineVar(sym, rhs) => Set(sym) - case _ => Set.empty // TODO: add other cases + case _ => Set.empty // TODO def describe: Str = val desc = this match diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index a4ce900a78..b9b7d0cb68 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -124,7 +124,8 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; + let i, init; + let len, scrut, scrut1, tmp, tmp1, tmp2, tmp3; len = rest.length; scrut = len == 0; if (scrut === true) { @@ -152,7 +153,8 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda, tmp; + let lambda; + let tmp; lambda = (undefined, function (acc, x) { let tmp1, tmp2, tmp3; if (typeof x === 'string') { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index e5b5a48eeb..4f14cc8cc5 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -197,7 +197,8 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let scrut, prev, tmp; + let prev; + let scrut, tmp; scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -415,7 +416,8 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let name, lambda; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; tmp = got < expected; lambda = (undefined, function () { let lambda1; @@ -526,7 +528,8 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; + let cur, locals, curLocals, loc, loc1, localsMsg, lambda; + let msg, curHandler, atTail, scrut, scrut1, scrut2, scrut3, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; msg = header; curHandler = tr.contTrace; atTail = true; @@ -622,7 +625,8 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + let result, lambda; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; if (cont instanceof Runtime.FunctionContFrame.class) { tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; @@ -668,7 +672,8 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + let result, lambda; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; if (cont instanceof Runtime.HandlerContFrame.class) { result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { @@ -727,7 +732,8 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; + let vis, hl, cur; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; if (contTrace instanceof Runtime.ContTrace.class) { tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; @@ -814,7 +820,8 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let nxt, scrut, tmp, tmp1; + let nxt; + let scrut, tmp, tmp1; tmp2: while (true) { if (cur instanceof Runtime.EffectSig.class) { nxt = Runtime.handleEffect(cur); @@ -958,7 +965,8 @@ globalThis.Object.freeze(class Runtime { return tmp4 } static checkDepth() { - let scrut, tmp, lambda; + let lambda; + let scrut, tmp; tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -971,7 +979,8 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, scrut, saved, tmp, tmp1; + let saved; + let result, scrut, tmp, tmp1; Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index 26fb4b1450..a90ca7695e 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -21,7 +21,8 @@ fun buildPalindrome = case //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; +//│ let n; +//│ let tmp3, tmp4, tmp5; //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -52,7 +53,8 @@ fun sum2 = case //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; +//│ let x, xs, y; +//│ let lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { @@ -80,13 +82,13 @@ sum2(arr) :e fun f(..xs) = xs //│ ╔══[ERROR] Lazy spread parameters not allowed. -//│ ║ l.81: fun f(..xs) = xs +//│ ║ l.83: fun f(..xs) = xs //│ ╙── ^^^^ :ge sum2(..arr) //│ ╔══[COMPILATION ERROR] Lazy spreads are not supported in call arguments -//│ ║ l.87: sum2(..arr) +//│ ║ l.89: sum2(..arr) //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function expected 1 argument but got 2 diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 19e96e6493..eed563d668 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -151,7 +151,8 @@ fun pow(x) = case //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; +//│ let n; +//│ let tmp, tmp1, tmp2; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -207,7 +208,8 @@ fun fact = case //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; +//│ let n; +//│ let tmp1, tmp2, tmp3; //│ if (caseScrut === 0) { //│ return 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index d72caafeeb..2f5baa282d 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -89,7 +89,8 @@ fun test2() = //│ JS (unsanitized): //│ let test22; //│ test22 = function test2() { -//│ let funny, tmp1; +//│ let funny; +//│ let tmp1; //│ funny = function funny() { //│ let lambda, lambda1; //│ lambda = (undefined, function (caseScrut) { @@ -100,7 +101,8 @@ fun test2() = //│ } //│ }); //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp2, tmp3, tmp4; +//│ let n; +//│ let tmp2, tmp3, tmp4; //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; @@ -130,7 +132,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.131: print("Hi") +//│ ║ l.133: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index ad80485e1d..668826abb5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -24,11 +24,8 @@ //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Assign: -//│ lhs = $block$res -//│ rhs = Lit of IntLit of 2 -//│ rest = Return: \ -//│ res = Lit of UnitLit of false +//│ main = Return: +//│ res = Lit of IntLit of 2 //│ implct = true //│ = 2 @@ -56,11 +53,8 @@ print("Hi") //│ Arg: //│ spread = N //│ value = Lit of StrLit of "Hi" -//│ rest = Assign: \ -//│ lhs = $block$res -//│ rhs = Lit of IntLit of 2 //│ rest = Return: \ -//│ res = Lit of UnitLit of false +//│ res = Lit of IntLit of 2 //│ implct = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 22009ea6bb..30f7fee561 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -17,7 +17,8 @@ fun test(x) = //│ JS (unsanitized): //│ let test; //│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; +//│ let v, v1; +//│ let scrut, argument0$, argument0$1, tmp, tmp1; //│ if (x instanceof Some1.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index b5c1b26b34..7e7c2c81f6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -39,7 +39,8 @@ fun test(x) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(x) { -//│ let Foo2, tmp; +//│ let Foo2; +//│ let tmp; //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -68,7 +69,7 @@ test(123) :re test() //│ ╔══[ERROR] Expected 1 arguments, got 0 -//│ ║ l.69: test() +//│ ║ l.70: test() //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Function 'test' expected 1 argument but got 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 55c80c83c3..19eb06aa44 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -55,7 +55,8 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; +//│ let x4; +//│ let argument0$4; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -120,7 +121,8 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f4; //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp5; +//│ let x4; +//│ let scrut1, argument0$4, tmp5; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some1.class) { @@ -166,7 +168,8 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f5; //│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; +//│ let u, a, b; +//│ let argument0$4, argument1$; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 5699747d40..3ea2b5151a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -98,41 +98,40 @@ case //│ sign = N //│ modulefulness = Modulefulness of N //│ restParam = N -//│ body = Match: -//│ scrut = Ref of caseScrut -//│ arms = Ls of -//│ Tuple2: -//│ _1 = Cls: -//│ cls = class:Foo -//│ path = Select{class:Foo}: -//│ qual = Ref of member:Foo -//│ name = Ident of "class" -//│ _2 = Assign: -//│ lhs = $argument0$ -//│ rhs = Select{class:Foo.x}: -//│ qual = Ref of caseScrut -//│ name = Ident of "x" -//│ rest = Assign: \ -//│ lhs = a -//│ rhs = Ref of $argument0$ -//│ rest = Return: \ -//│ res = Ref of a -//│ implct = false -//│ dflt = S of Throw of Instantiate: -//│ mut = false -//│ cls = Select: -//│ qual = Ref of globalThis:globalThis -//│ name = Ident of "Error" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "match error" -//│ rest = End of "" -//│ rest = Assign: \ -//│ lhs = $block$res -//│ rhs = Ref of member:lambda +//│ body = Scoped: +//│ syms = Set($argument0$) +//│ body = Match: +//│ scrut = Ref of caseScrut +//│ arms = Ls of +//│ Tuple2: +//│ _1 = Cls: +//│ cls = class:Foo +//│ path = Select{class:Foo}: +//│ qual = Ref of member:Foo +//│ name = Ident of "class" +//│ _2 = Assign: +//│ lhs = $argument0$ +//│ rhs = Select{class:Foo.x}: +//│ qual = Ref of caseScrut +//│ name = Ident of "x" +//│ rest = Assign: \ +//│ lhs = a +//│ rhs = Ref of $argument0$ +//│ rest = Return: \ +//│ res = Ref of a +//│ implct = false +//│ dflt = S of Throw of Instantiate: +//│ mut = false +//│ cls = Select: +//│ qual = Ref of globalThis:globalThis +//│ name = Ident of "Error" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "match error" +//│ rest = End of "" //│ rest = Return: \ -//│ res = Lit of UnitLit of false +//│ res = Ref of member:lambda //│ implct = true //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 6fef9160a3..5faa54a309 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -81,7 +81,8 @@ fun test(a) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1; +//│ let C11, C21; +//│ let tmp, tmp1; //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -154,7 +155,8 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz, tmp; +//│ let bar, baz; +//│ let tmp; //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 6a73053cbf..128459ca14 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -39,7 +39,8 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let whoops, tmp1, tmp2; +//│ let whoops; +//│ let tmp1, tmp2; //│ whoops = function whoops() { //│ let tmp3; //│ tmp3 = Predef.print("ok"); @@ -137,7 +138,8 @@ fun test() = //│ JS (unsanitized): //│ let test1; //│ test1 = function test() { -//│ let whoops, tmp1; +//│ let whoops; +//│ let tmp1; //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -212,7 +214,8 @@ fun a() = //│ return 1 //│ }; //│ c = function c() { -//│ let d, tmp2, tmp3; +//│ let d; +//│ let tmp2, tmp3; //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -241,7 +244,8 @@ fun b() = //│ return 1 //│ }; //│ d = function d() { -//│ let c1, tmp3; +//│ let c1; +//│ let tmp3; //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -264,12 +268,14 @@ fun c() = //│ JS (unsanitized): //│ let c; //│ c = function c() { -//│ let d, f, tmp4; +//│ let d, f; +//│ let tmp4; //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e, tmp5, tmp6; +//│ let e; +//│ let tmp5, tmp6; //│ e = function e() { //│ return 1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 3c18631157..006ecea04e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -74,7 +74,8 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let Foo5, tmp; +//│ let Foo5; +//│ let tmp; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -98,8 +99,8 @@ let t = test() :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.99: new t -//│ ║ ^ +//│ ║ l.100: new t +//│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): //│ globalThis.Object.freeze(new t()) @@ -115,7 +116,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.116: new t() +//│ ║ l.117: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -237,10 +238,10 @@ class Foo with val x = 2 //│ ╔══[ERROR] Multiple definitions of symbol 'x' //│ ╟── defined here -//│ ║ l.236: val x = 1 +//│ ║ l.237: val x = 1 //│ ║ ^^^^^^^^^ //│ ╟── defined here -//│ ║ l.237: val x = 2 +//│ ║ l.238: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo12; @@ -261,10 +262,10 @@ class Foo with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.262: let x = 2 +//│ ║ l.263: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.261: val x = 1 +//│ ║ l.262: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo14; @@ -291,7 +292,7 @@ class Foo with :e class Foo with val x = 1 //│ ╔══[ERROR] Illegal body of class definition (should be a block; found term definition). -//│ ║ l.292: class Foo with val x = 1 +//│ ║ l.293: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo16; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 8c0a51a762..2d9dca8bfd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -1,6 +1,33 @@ :js +:sjs +fun foo() = + let x = 1 + fun bar() = + let y = 2 + x + y + bar() +//│ JS (unsanitized): +//│ let foo; +//│ foo = function foo() { +//│ let bar; +//│ let x; +//│ bar = function bar() { let y; y = 2; return x + y }; +//│ x = 1; +//│ return bar() +//│ }; + + +let x = 1 +//│ x = 1 + +:sjs +fun foo() = + set x = 2 +//│ JS (unsanitized): +//│ let foo1; foo1 = function foo() { x = 2; return runtime.Unit }; + // only the `let x` should be declared in the top level of repl? // now `y` is declared in the "correct" place, but not `tmp` because `tmp` is // handled by `blockPreamble` @@ -10,23 +37,23 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let tmp; let x; let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; -//│ REPL> Sending: block$res1 = undefined +//│ let x1, y, tmp; tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; +//│ REPL> Sending: block$res4 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let tmp;let x;try { let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; block$res1 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let x1, y, tmp;try { tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; block$res4 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: //│ > undefined -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res4)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ > Unit {} //│ REPL> Parsed: //│ > undefined //│ > Unit {} -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > 17 //│ > Unit {} @@ -50,9 +77,10 @@ fun f() = //│ JS (unsanitized): //│ let f; //│ f = function f() { +//│ let a, b; //│ let scrut; //│ scrut = true; -//│ if (scrut === true) { let a; a = 4; return a + 4 } else { let b; b = 5; return b + 9 } +//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -73,18 +101,18 @@ fun g(x, y, z) = (_ + a) //│ JS (unsanitized): //│ let g; -//│ g = function g(x1, y1, z) { -//│ let tmp1, tmp2, lambda; +//│ g = function g(x2, y1, z) { +//│ let a, lambda; +//│ let tmp1, tmp2; //│ split_root$: { //│ split_1$: { -//│ if (x1 === true) { +//│ if (x2 === true) { //│ break split_1$ //│ } else { //│ if (y1 === true) { //│ break split_1$ //│ } else { //│ if (z === true) { -//│ let a; //│ a = 1; //│ lambda = (undefined, function (_0) { //│ return _0 + a @@ -104,22 +132,6 @@ fun g(x, y, z) = //│ }; -// TODO: -:e + module M -//│ FAILURE: Unexpected exception -//│ /!!!\ Uncaught error: java.lang.AssertionError: assertion failed: (module:M,Scope: -//│ parent = N -//│ curThis = S of S of globalThis:globalThis -//│ bindings = HashMap($block$res -> block$res4, class:M -> M1, $block$res -> block$res2, $runtime -> runtime, member:g -> g, $definitionMetadata -> definitionMetadata, $prettyPrint -> prettyPrint, $Term -> Term, $Block -> Block, $Shape -> Shape, $block$res -> block$res, x -> x, y -> y, member:Predef -> Predef, $block$res -> block$res3, $block$res -> block$res1, $tmp -> tmp, member:f -> f, module:M -> M)) -//│ at: scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) -//│ at: hkmc2.utils.Scope.addToBindings(Scope.scala:50) -//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$2(JSBuilder.scala:556) -//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$adapted$1(JSBuilder.scala:560) -//│ at: hkmc2.utils.TraceLogger.trace(TraceLogger.scala:17) -//│ at: hkmc2.codegen.js.JSBuilder.go$1(JSBuilder.scala:560) -//│ at: hkmc2.codegen.js.JSBuilder.reserveNames(JSBuilder.scala:561) -//│ at: hkmc2.codegen.js.JSBuilder.worksheet(JSBuilder.scala:589) -//│ at: hkmc2.JSBackendDiffMaker.processTerm(JSBackendDiffMaker.scala:155) -//│ at: hkmc2.BbmlDiffMaker.processTerm(BbmlDiffMaker.scala:36) diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 0f46270d22..b1c2122e16 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -71,18 +71,19 @@ example() //│ JS (unsanitized): //│ let example2; //│ example2 = function example() { -//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, get_x1; +//│ let get_x; +//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ x2 = 0; -//│ get_x1 = function get_x() { +//│ get_x = function get_x() { //│ return x2 //│ }; -//│ get_x = get_x1; +//│ get_x1 = get_x; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; //│ x2 = tmp5; //│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x()); +//│ tmp7 = runtime.safeCall(get_x1()); //│ tmp8 = Predef.print(tmp7); //│ tmp9 = (tmp6 , tmp8); //│ tmp4 = tmp9; @@ -90,7 +91,7 @@ example() //│ x2 = old1; //│ } //│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x()); +//│ tmp11 = runtime.safeCall(get_x1()); //│ return Predef.print(tmp11) //│ }; //│ example2() @@ -112,12 +113,13 @@ example() //│ JS (unsanitized): //│ let example3; //│ example3 = function example() { -//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, get_x1; +//│ let get_x; +//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; //│ x2 = 0; -//│ get_x1 = function get_x() { +//│ get_x = function get_x() { //│ return x2 //│ }; -//│ get_x = get_x1; +//│ get_x1 = get_x; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; @@ -130,7 +132,7 @@ example() //│ } //│ y = tmp4; //│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x()); +//│ tmp9 = runtime.safeCall(get_x1()); //│ tmp10 = Predef.print(tmp9); //│ return y //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 4ef0cd898a..60ab9f2c5c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { throw globalThis.Error("e") }; f2(1) +//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 9ec87b064d..6dd3fd47b9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -105,7 +105,8 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let i2, scrut3, tmp19, tmp20; +//│ let i2; +//│ let scrut3, tmp19, tmp20; //│ tmp21: while (true) { //│ i2 = 0; //│ scrut3 = i2 < 10; @@ -197,7 +198,8 @@ fun f(ls) = //│ JS (unsanitized): //│ let f; //│ f = function f(ls) { -//│ let tl, h, argument0$, argument1$, tmp28; +//│ let tl, h; +//│ let argument0$, argument1$, tmp28; //│ tmp29: while (true) { //│ if (ls instanceof Cons1.class) { //│ argument0$ = ls.hd; @@ -274,10 +276,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.274: then 0(0) +//│ ║ l.276: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.273: while print("Hello World"); false +//│ ║ l.275: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -287,12 +289,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.286: while { print("Hello World"), false } +//│ ║ l.288: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.287: then 0(0) +//│ ║ l.289: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.288: else 1 +//│ ║ l.290: else 1 //│ ╙── ^^^^ :fixme @@ -302,14 +304,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.300: print("Hello World") +//│ ║ l.302: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.301: false +//│ ║ l.303: false //│ ║ ^^^^^^^^^ -//│ ║ l.302: then 0(0) +//│ ║ l.304: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.303: else 1 +//│ ║ l.305: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 6d604a7de6..df45c1fece 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -187,6 +187,7 @@ f().foo() //│ f5 = function f() { //│ let x, y, z, tmp6, tmp7, capture; //│ capture = new f$capture3(null); +//│ let w; //│ x = 1; //│ y = 10; //│ z = 10; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index dfd2bc3676..a249f407b4 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -59,7 +59,8 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let used2, unused2, foo, tmp, g$here; +//│ let g$here; +//│ let used2, unused2, foo, tmp; //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); @@ -281,6 +282,7 @@ g()(1) //│ g6 = function g() { //│ let capture, f$here; //│ capture = new g$capture1(null); +//│ let y1; //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here @@ -380,7 +382,8 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let y1, scrut, g$here; +//│ let g$here; +//│ let y1, scrut; //│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index a49b86a560..4c400e4dc6 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -53,7 +53,8 @@ fun foo() = //│ } //│ }); //│ foo2 = function foo() { -//│ let x, scrut, tmp2, tmp3, lambda$here; +//│ let lambda$here; +//│ let x, scrut, tmp2, tmp3; //│ x = 1; //│ tmp4: while (true) { //│ scrut = true; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 85b58b0d92..bfe2893f41 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -34,6 +34,7 @@ fun foo() = //│ foo = function foo() { //│ let tmp, capture, bar$here; //│ capture = new foo$capture1(null); +//│ let x; //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); //│ tmp = runtime.safeCall(xs.push(bar$here)); diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index bb16dac916..809b3151c6 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -42,7 +42,8 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let tmp, tmp1, idx1, idx2, idx3, idx4, idx5; +//│ let idx1, idx2, idx3, idx4, idx5; +//│ let tmp, tmp1; //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; //│ idx5 = idx + 0; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 4bba48a888..8f8affbdbb 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -336,7 +336,8 @@ fun foo(x, y, z) = if x is //│ JS (unsanitized): //│ let foo2; //│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; +//│ let value; +//│ let tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 33339b6e3a..9533eb3dde 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -8,7 +8,8 @@ fun nonsense(xs) = if xs is //│ JS (unsanitized): //│ let nonsense; //│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; +//│ let ys; +//│ let middleElements; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -29,7 +30,8 @@ fun lead_and_last(xs) = if xs is //│ JS (unsanitized): //│ let lead_and_last; //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; +//│ let x, ys, y; +//│ let lastElement0$, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -63,7 +65,8 @@ fun nested_tuple_patterns(xs) = if xs is //│ JS (unsanitized): //│ let nested_tuple_patterns; //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; +//│ let x, y, w, z; +//│ let lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 2c9c910fd6..ca5f1fb528 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -108,24 +108,35 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: with JSBuilderArgNumSanityChecks val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) - - // TODO: remove mutation - var toplvlDefinedVars = Set.empty[Symbol] - def assignResSym(b: Block, toplvl: Boolean): Block = - b.mapTail: - case e: End => + val le = lowered0.copy(main = lowered0.main.mapTail: + case e: End => Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - case Return(res, implct) => - assert(implct) - Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case s@Scoped(xs, body) => - if toplvl then - toplvlDefinedVars = xs - assignResSym(body, false) - else - Scoped(xs, assignResSym(body, false)) - case tl: (Throw | Break | Continue) => tl - val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + case Return(res, implct) => + assert(implct) + Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case Scoped(_, body) => ??? // TODO + case tl: (Throw | Break | Continue) => tl + ) + + // TODO: re-implement + // TODO: remove mutation + // var toplvlDefinedVars = Set.empty[Symbol] + // def assignResSym(b: Block, toplvl: Boolean): Block = + // b.mapTail: + // case e: End => + // Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + // case Return(res, implct) => + // assert(implct) + // Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + // case s@Scoped(xs, body) => + // if toplvl then + // toplvlDefinedVars = xs + // assignResSym(body, false) + // else + // Scoped(xs, assignResSym(body, false)) + // case tl: (Throw | Break | Continue) => tl + // val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + if showLoweredTree.isSet then output(s"Lowered:") output(lowered0.showAsTree) @@ -144,16 +155,16 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: // NOTE: `blockPreamble` should still take care of those vars that are generated during or after the lowering stage, // while `Scoped` more reflects the declared vars in the source (or elaborated?) level? // Or, during the lowering, we should also find the correct tmp vars and add them to the set in `Scoped` - val varsFromScopedStr = - val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) - (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc";").stripBreaks.mkString(100) + // val varsFromScopedStr = + // val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) + // (if vars.isEmpty then doc"" else + // doc" # let " :: vars.map: (_, nme) => + // nme + // .toList.mkDocument(", ") + // :: doc";").stripBreaks.mkString(100) val (pre, js) = nestedScp.givenIn: jsb.worksheet(le) - val preStr = pre.stripBreaks.mkString(100) + varsFromScopedStr + val preStr = pre.stripBreaks.mkString(100) //+ varsFromScopedStr val jsStr = js.stripBreaks.mkString(100) if showSanitizedJS.isSet then output(s"JS:") From cf9d78f79bfbf56149ab806726241fb280107f80 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Tue, 18 Nov 2025 16:14:45 +0800 Subject: [PATCH 06/72] Try to use a pass to insert Scoped --- .../shared/src/main/scala/hkmc2/Config.scala | 2 + .../src/main/scala/hkmc2/codegen/Block.scala | 32 +++------- .../main/scala/hkmc2/codegen/Lowering.scala | 46 +++++++++++++-- .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../src/main/scala/hkmc2/semantics/Term.scala | 6 -- .../src/test/mlscript-compile/Predef.mjs | 6 +- .../src/test/mlscript-compile/Runtime.mjs | 27 +++------ .../basics/CompanionModules_Classes.mls | 3 +- .../shared/src/test/mlscript/basics/Drop.mls | 5 +- .../src/test/mlscript/basics/LazySpreads.mls | 10 ++-- .../test/mlscript/basics/MultiParamLists.mls | 39 +++++++------ .../mlscript/basics/MultilineExpressions.mls | 17 +++--- .../src/test/mlscript/bbml/bbCodeGen.mls | 12 ++-- .../src/test/mlscript/bbml/bbGetters.mls | 7 +-- .../src/test/mlscript/codegen/Arrays.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 28 ++++----- .../src/test/mlscript/codegen/CaseOfCase.mls | 3 +- .../src/test/mlscript/codegen/ClassInFun.mls | 5 +- .../test/mlscript/codegen/ClassMatching.mls | 12 ++-- .../src/test/mlscript/codegen/ConsoleLog.mls | 16 ++--- .../test/mlscript/codegen/DelayedLetInit.mls | 8 ++- .../src/test/mlscript/codegen/EarlyReturn.mls | 3 +- .../test/mlscript/codegen/FieldSymbols.mls | 58 ++++++++++--------- .../src/test/mlscript/codegen/Formatting.mls | 24 +++++--- .../src/test/mlscript/codegen/FunInClass.mls | 6 +- .../src/test/mlscript/codegen/Getters.mls | 31 +++++----- .../src/test/mlscript/codegen/Hygiene.mls | 2 +- .../test/mlscript/codegen/InlineLambdas.mls | 18 +++--- .../test/mlscript/codegen/MergeMatchArms.mls | 25 ++++---- .../test/mlscript/codegen/ParamClasses.mls | 2 +- .../src/test/mlscript/codegen/PartialApps.mls | 2 +- .../test/mlscript/codegen/PlainClasses.mls | 12 ++-- .../shared/src/test/mlscript/codegen/Pwd.mls | 17 +++--- .../src/test/mlscript/codegen/Quasiquotes.mls | 44 +++++++------- .../src/test/mlscript/codegen/RandomStuff.mls | 2 +- .../src/test/mlscript/codegen/Scoped.mls | 8 +-- .../src/test/mlscript/codegen/SetIn.mls | 25 ++++---- .../src/test/mlscript/codegen/Throw.mls | 2 +- .../src/test/mlscript/codegen/TraceLog.mls | 3 +- .../src/test/mlscript/codegen/While.mls | 24 ++++---- .../src/test/mlscript/ctx/EtaExpansion.mls | 11 ++-- .../src/test/mlscript/handlers/Debugging.mls | 16 ++--- .../mlscript/handlers/RecursiveHandlers.mls | 3 +- .../test/mlscript/handlers/SetInHandlers.mls | 3 +- .../src/test/mlscript/lifter/ClassInFun.mls | 4 +- .../src/test/mlscript/lifter/FunInFun.mls | 31 +++++----- .../shared/src/test/mlscript/lifter/Loops.mls | 4 +- .../test/mlscript/lifter/ModulesObjects.mls | 3 +- .../src/test/mlscript/lifter/Mutation.mls | 4 +- .../test/mlscript/lifter/StackSafetyLift.mls | 3 +- .../src/test/mlscript/objbuf/Mutation.mls | 3 +- .../test/mlscript/std/FingerTreeListTest.mls | 3 +- .../ucs/normalization/Deduplication.mls | 15 +++-- .../test/mlscript/ucs/patterns/RestTuple.mls | 9 +-- .../src/test/mlscript/ups/MatchResult.mls | 3 +- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 42 +++++--------- .../src/test/scala/hkmc2/LlirDiffMaker.scala | 1 - .../src/test/scala/hkmc2/MLsDiffMaker.scala | 3 + 58 files changed, 389 insertions(+), 368 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/Config.scala b/hkmc2/shared/src/main/scala/hkmc2/Config.scala index d0a684622b..db827c709e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/Config.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/Config.scala @@ -21,6 +21,7 @@ case class Config( effectHandlers: Opt[EffectHandlers], liftDefns: Opt[LiftDefns], stageCode: Bool, + llir: Bool, target: CompilationTarget, ): @@ -37,6 +38,7 @@ object Config: effectHandlers = N, liftDefns = N, stageCode = false, + llir = false, target = CompilationTarget.JS ) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 9d63e962ad..c22f503ba6 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -51,25 +51,6 @@ sealed abstract class Block extends Product: case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars // cannot use `body.definedVars - syms`. lifter computes captures based on it? case Scoped(syms, body) => body.definedVars - - // TODO: remove it - lazy val tempVars: Set[Local] = this match - case _: Return | _: Throw => Set.empty - case Begin(sub, rst) => sub.tempVars ++ rst.tempVars - case Assign(l: TempSymbol, r, rst) => rst.tempVars + l - case Assign(l, r, rst) => rst.tempVars - case AssignField(l, n, r, rst) => rst.tempVars - case AssignDynField(l, n, ai, r, rst) => rst.tempVars - case Match(scrut, arms, dflt, rst) => - arms.flatMap(_._2.tempVars).toSet ++ dflt.toList.flatMap(_.tempVars) ++ rst.tempVars - case End(_) => Set.empty - case Break(_) => Set.empty - case Continue(_) => Set.empty - case Define(defn, rst) => rst.tempVars - case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.tempVars - case TryBlock(sub, fin, rst) => sub.tempVars ++ fin.tempVars ++ rst.tempVars - case Label(lbl, _, bod, rst) => bod.tempVars ++ rst.tempVars - case Scoped(syms, body) => body.tempVars lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -87,7 +68,6 @@ sealed abstract class Block extends Product: // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match - case Scoped(syms, body) => Scoped(syms, body.mapTail(f)) case b: BlockTail => f(b) case Begin(sub, rst) => Begin(sub, rst.mapTail(f)) case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f)) @@ -274,10 +254,14 @@ sealed abstract class Block extends Product: else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) case Scoped(syms, body) => - val newBody = body.flatten(k) - if newBody is body - then this - else Scoped(syms, newBody) + if syms.isEmpty then body.flatten(k) + else + body.flatten(k) match + case Scoped(syms2, body) => Scoped(syms ++ syms2, body) + case newBody => + if newBody is body + then this + else Scoped(syms, newBody) case e: End => k(e) case t: BlockTail => this diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index cd343d9e77..4c82140263 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -935,9 +935,13 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val merged = MergeMatchArmTransformer.applyBlock(bufferable) - val res = - if config.stageCode then Instrumentation(using summon).applyBlock(merged) + val scoped = + if !config.llir then setupScoped(merged)(using imps.map(_.sym).toSet) else merged + + val res = + if config.stageCode then Instrumentation(using summon).applyBlock(scoped) + else scoped Program( imps.map(imp => imp.sym -> imp.str), @@ -949,6 +953,41 @@ class Lowering()(using Config, TL, Raise, State, Ctx): subTerm(prefix): p => val selRes = TempSymbol(N, "selRes") k(Select(p, nme)(sym)) + + def setupScoped(blk: Block)(using syms: Set[Local]): Block = blk match + case tail: BlockTail => tail + case Match(scrut, arms, dflt, rest) => + Match(scrut, arms.map { + case (c, b) => c -> setupScoped(b) + }, dflt.map(setupScoped), setupScoped(rest)) + case Label(s, loop, body, rest) => + Label(s, loop, setupScoped(body), setupScoped(rest)) + case Begin(sub, rest) => + Begin(setupScoped(sub), setupScoped(rest)) + case TryBlock(sub, fd, rest) => + TryBlock(setupScoped(sub), setupScoped(fd), setupScoped(rest)) + case Assign(lhs, rhs, rest) if syms(lhs) => Assign(lhs, rhs, setupScoped(rest)) + case Assign(lhs, rhs, rest) => + val newSyms = syms + lhs + Scoped(Set(lhs), Assign(lhs, rhs, setupScoped(rest)(using newSyms))).flattened + case assign @ AssignField(lhs, nme, rhs, rest) => AssignField(lhs, nme, rhs, setupScoped(rest))(assign.symbol) + case AssignDynField(lhs, fld, arrayIdx, rhs, rest) => AssignDynField(lhs, fld, arrayIdx, rhs, setupScoped(rest)) + case HandleBlock(lhs, res, par, args, cls, handlers, body, rest) => + HandleBlock(lhs, res, par, args, cls, handlers, setupScoped(body), setupScoped(rest)) + case Define(defn, rest) => + val newDefn = defn match { + case d: ValDefn => d + case FunDefn(owner, sym, params, body) => + val newSyms = syms ++ params.flatMap(_.params.map(_.sym)) + FunDefn(owner, sym, params, setupScoped(body)(using newSyms)) + case ClsLikeDefn(owner, isym, sym, k, paramsOpt, auxParams, parentPath, methods, + privateFields, publicFields, preCtor, ctor, companion, bufferable) => + ClsLikeDefn(owner, isym, sym, k, paramsOpt, auxParams, parentPath, methods, + privateFields, publicFields, preCtor, ctor, companion, bufferable) + } + val newSyms = syms + newDefn.sym + Define(newDefn, setupScoped(rest)(using newSyms)) + final def setupFunctionOrByNameDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = @@ -959,8 +998,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - val body = returnedTerm(bodyTerm) - (paramLists, Scoped(bodyTerm.definedSyms ++ body.tempVars, body)) // TODO: move it to block function + (paramLists, returnedTerm(bodyTerm)) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 34393f141d..32cd6a351d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -503,7 +503,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - // TODO: we should remove `syms.filter` after temp vars can be correctly handled. + // still need to keep the filter for diff test val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 34596bd89a..79ae65ed97 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -363,12 +363,6 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdField(field, rhs) => RcdField(field.mkClone, rhs.mkClone) case RcdSpread(rcd) => RcdSpread(rcd.mkClone) case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) - - lazy val definedSyms: Set[Symbol] = this match - case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty - case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) - case LetDecl(sym, annotations) => Set(sym) - case _ => Set.empty // TODO def describe: Str = val desc = this match diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index b9b7d0cb68..a4ce900a78 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -124,8 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let i, init; - let len, scrut, scrut1, tmp, tmp1, tmp2, tmp3; + let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; len = rest.length; scrut = len == 0; if (scrut === true) { @@ -153,8 +152,7 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda; - let tmp; + let lambda, tmp; lambda = (undefined, function (acc, x) { let tmp1, tmp2, tmp3; if (typeof x === 'string') { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 4f14cc8cc5..e5b5a48eeb 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -197,8 +197,7 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let prev; - let scrut, tmp; + let scrut, prev, tmp; scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -416,8 +415,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let name, lambda; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; tmp = got < expected; lambda = (undefined, function () { let lambda1; @@ -528,8 +526,7 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let cur, locals, curLocals, loc, loc1, localsMsg, lambda; - let msg, curHandler, atTail, scrut, scrut1, scrut2, scrut3, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; + let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; msg = header; curHandler = tr.contTrace; atTail = true; @@ -625,8 +622,7 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, lambda; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; if (cont instanceof Runtime.FunctionContFrame.class) { tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; @@ -672,8 +668,7 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; if (cont instanceof Runtime.HandlerContFrame.class) { result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { @@ -732,8 +727,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let vis, hl, cur; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; + let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; if (contTrace instanceof Runtime.ContTrace.class) { tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; @@ -820,8 +814,7 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let nxt; - let scrut, tmp, tmp1; + let nxt, scrut, tmp, tmp1; tmp2: while (true) { if (cur instanceof Runtime.EffectSig.class) { nxt = Runtime.handleEffect(cur); @@ -965,8 +958,7 @@ globalThis.Object.freeze(class Runtime { return tmp4 } static checkDepth() { - let lambda; - let scrut, tmp; + let scrut, tmp, lambda; tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -979,8 +971,7 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let saved; - let result, scrut, tmp, tmp1; + let result, scrut, saved, tmp, tmp1; Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 6cef84654f..9d0f039450 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -87,7 +87,8 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1, tmp1; +//│ let Foo1; +//│ let tmp1; //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index 9d957653bc..12bae13d9f 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,14 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp; let tmp1; tmp1 = 2 + 2; tmp = Predef.id(tmp1); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; +//│ let b, tmp2, tmp3; +//│ let a; //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index a90ca7695e..26fb4b1450 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -21,8 +21,7 @@ fun buildPalindrome = case //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp3, tmp4, tmp5; +//│ let n, tmp3, tmp4, tmp5; //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -53,8 +52,7 @@ fun sum2 = case //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y; -//│ let lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { @@ -82,13 +80,13 @@ sum2(arr) :e fun f(..xs) = xs //│ ╔══[ERROR] Lazy spread parameters not allowed. -//│ ║ l.83: fun f(..xs) = xs +//│ ║ l.81: fun f(..xs) = xs //│ ╙── ^^^^ :ge sum2(..arr) //│ ╔══[COMPILATION ERROR] Lazy spreads are not supported in call arguments -//│ ║ l.89: sum2(..arr) +//│ ║ l.87: sum2(..arr) //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function expected 1 argument but got 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 178203f9e4..0e177a12fa 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -35,18 +35,19 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ let tmp1, tmp2, tmp3; -//│ tmp1 = 10 * n1; -//│ tmp2 = tmp1 + n2; -//│ tmp3 = 10 * tmp2; -//│ return tmp3 + n3 +//│ let tmp1, tmp2; +//│ let tmp3; +//│ tmp3 = 10 * n1; +//│ tmp1 = tmp3 + n2; +//│ tmp2 = 10 * tmp1; +//│ return tmp2 + n3 //│ } //│ } //│ }; f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) +//│ let tmp1; let tmp2; tmp2 = f2(4); tmp1 = runtime.safeCall(tmp2(2)); runtime.safeCall(tmp1(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 @@ -56,13 +57,14 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) //│ return (n2) => { //│ return (n3) => { //│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; -//│ tmp3 = 10 * n1; -//│ tmp4 = tmp3 + n2; -//│ tmp5 = 10 * tmp4; -//│ tmp6 = tmp5 + n3; -//│ tmp7 = 10 * tmp6; -//│ return tmp7 + n4 +//│ let tmp3, tmp4, tmp5, tmp6; +//│ let tmp7; +//│ tmp7 = 10 * n1; +//│ tmp3 = tmp7 + n2; +//│ tmp4 = 10 * tmp3; +//│ tmp5 = tmp4 + n3; +//│ tmp6 = 10 * tmp5; +//│ return tmp6 + n4 //│ } //│ } //│ } @@ -70,11 +72,12 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; -//│ tmp3 = f3(3); -//│ tmp4 = runtime.safeCall(tmp3(0)); -//│ tmp5 = runtime.safeCall(tmp4(3)); -//│ runtime.safeCall(tmp5(1)) +//│ let tmp3, tmp4; +//│ let tmp5; +//│ tmp5 = f3(3); +//│ tmp3 = runtime.safeCall(tmp5(0)); +//│ tmp4 = runtime.safeCall(tmp3(3)); +//│ runtime.safeCall(tmp4(1)) //│ = 3031 diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index 5025320ce4..41c2209da9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,7 +80,8 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; +//│ let scrut; +//│ let tmp17; //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { @@ -116,23 +117,23 @@ if 1 + 2 * 3 then 0 + 4 then 1 //│ ╔══[PARSE ERROR] Operator cannot be used inside this operator split -//│ ║ l.117: + 4 then 1 +//│ ║ l.118: + 4 then 1 //│ ║ ^ //│ ╟── as it has lower precedence than the splitting operator here -//│ ║ l.116: * 3 then 0 +//│ ║ l.117: * 3 then 0 //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected 'then' keyword in this operator split inner position -//│ ║ l.117: + 4 then 1 +//│ ║ l.118: + 4 then 1 //│ ║ ^^^^ //│ ╟── Note: the operator split starts here -//│ ║ l.116: * 3 then 0 +//│ ║ l.117: * 3 then 0 //│ ╙── ^ //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.115: if 1 + 2 +//│ ║ l.116: if 1 + 2 //│ ║ ^ -//│ ║ l.116: * 3 then 0 +//│ ║ l.117: * 3 then 0 //│ ║ ^^^^^^^^^^^^ -//│ ║ l.117: + 4 then 1 +//│ ║ l.118: + 4 then 1 //│ ╙── ^^^^^ :pt diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index eed563d668..b05e9f394d 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -151,8 +151,7 @@ fun pow(x) = case //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp, tmp1, tmp2; +//│ let n, tmp, tmp1, tmp2; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -208,8 +207,7 @@ fun fact = case //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp1, tmp2, tmp3; +//│ let n, tmp1, tmp2, tmp3; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -260,7 +258,8 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let x4, y; +//│ let y; +//│ let x4; //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); //│ y.value @@ -271,7 +270,8 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let x5, y1; +//│ let y1; +//│ let x5; //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); //│ y1.value = 0; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 2f5baa282d..134e59fd82 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -89,8 +89,7 @@ fun test2() = //│ JS (unsanitized): //│ let test22; //│ test22 = function test2() { -//│ let funny; -//│ let tmp1; +//│ let funny, tmp1; //│ funny = function funny() { //│ let lambda, lambda1; //│ lambda = (undefined, function (caseScrut) { @@ -101,8 +100,8 @@ fun test2() = //│ } //│ }); //│ lambda1 = (undefined, function (caseScrut) { -//│ let n; //│ let tmp2, tmp3, tmp4; +//│ let n; //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; @@ -132,7 +131,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.133: print("Hi") +//│ ║ l.132: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index 9cb5c5210c..e58e996967 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1; let tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index 668826abb5..1d5c601b03 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -43,19 +43,21 @@ print("Hi") //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Assign: -//│ lhs = $tmp -//│ rhs = Call: -//│ fun = Select{member:print}: -//│ qual = Ref of member:Predef -//│ name = Ident of "print" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "Hi" -//│ rest = Return: \ -//│ res = Lit of IntLit of 2 -//│ implct = true +//│ main = Scoped: +//│ syms = Set($tmp) +//│ body = Assign: +//│ lhs = $tmp +//│ rhs = Call: +//│ fun = Select{member:print}: +//│ qual = Ref of member:Predef +//│ name = Ident of "print" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "Hi" +//│ rest = Return: \ +//│ res = Lit of IntLit of 2 +//│ implct = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 30f7fee561..22009ea6bb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -17,8 +17,7 @@ fun test(x) = //│ JS (unsanitized): //│ let test; //│ test = function test(x) { -//│ let v, v1; -//│ let scrut, argument0$, argument0$1, tmp, tmp1; +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; //│ if (x instanceof Some1.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index 7e7c2c81f6..b5c1b26b34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -39,8 +39,7 @@ fun test(x) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(x) { -//│ let Foo2; -//│ let tmp; +//│ let Foo2, tmp; //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -69,7 +68,7 @@ test(123) :re test() //│ ╔══[ERROR] Expected 1 arguments, got 0 -//│ ║ l.70: test() +//│ ║ l.69: test() //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Function 'test' expected 1 argument but got 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 19eb06aa44..2591b12c47 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,7 +9,8 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; +//│ let x, argument0$; +//│ let scrut; //│ scrut = Some1(0); //│ if (scrut instanceof Some1.class) { //│ argument0$ = scrut.value; @@ -55,8 +56,7 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4; -//│ let argument0$4; +//│ let x4, argument0$4; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -121,8 +121,7 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f4; //│ f4 = function f(x3) { -//│ let x4; -//│ let scrut1, argument0$4, tmp5; +//│ let x4, scrut1, argument0$4, tmp5; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some1.class) { @@ -168,8 +167,7 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f5; //│ f5 = function f(x3) { -//│ let u, a, b; -//│ let argument0$4, argument1$; +//│ let u, a, b, argument0$4, argument1$; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index be5f1aee43..394fbe1234 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -14,9 +14,10 @@ console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; -//│ tmp = runtime.safeCall(globalThis.console.log("a")); -//│ tmp1 = runtime.safeCall(globalThis.console.log("b")); +//│ let tmp; +//│ let tmp1; +//│ tmp1 = runtime.safeCall(globalThis.console.log("a")); +//│ tmp = runtime.safeCall(globalThis.console.log("b")); //│ 123 //│ > a //│ > b @@ -49,12 +50,13 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3, tmp4; -//│ tmp2 = runtime.safeCall(globalThis.console.log("a")); +//│ let x, y, tmp2, tmp3; +//│ let tmp4; +//│ tmp4 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; -//│ tmp3 = runtime.safeCall(globalThis.console.log("b")); +//│ tmp2 = runtime.safeCall(globalThis.console.log("b")); //│ y = x + 1; -//│ tmp4 = runtime.safeCall(globalThis.console.log("c")); +//│ tmp3 = runtime.safeCall(globalThis.console.log("c")); //│ y * 2 //│ > a //│ > b diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index a4fddd8d26..56640d603c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -80,7 +80,8 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; +//│ let scrut; +//│ let foo1; //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +96,8 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; +//│ let scrut1; +//│ let foo2; //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -120,7 +122,7 @@ foo :fixme fun f() = foo = 0 //│ ╔══[PARSE ERROR] Expected end of input; found '=' keyword instead -//│ ║ l.121: fun f() = foo = 0 +//│ ║ l.123: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): //│ let f4; f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 797b5c0c47..f196de001c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -20,7 +20,8 @@ fun f(x) = //│ JS (unsanitized): //│ let f1; //│ f1 = function f(x) { -//│ let scrut, tmp, tmp1; +//│ let tmp, tmp1; +//│ let scrut; //│ scrut = x < 0; //│ if (scrut === true) { //│ tmp = Predef.print("whoops"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 3ea2b5151a..6af2a7a32f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -98,38 +98,40 @@ case //│ sign = N //│ modulefulness = Modulefulness of N //│ restParam = N -//│ body = Scoped: -//│ syms = Set($argument0$) -//│ body = Match: -//│ scrut = Ref of caseScrut -//│ arms = Ls of -//│ Tuple2: -//│ _1 = Cls: -//│ cls = class:Foo -//│ path = Select{class:Foo}: -//│ qual = Ref of member:Foo -//│ name = Ident of "class" -//│ _2 = Assign: +//│ body = Match: +//│ scrut = Ref of caseScrut +//│ arms = Ls of +//│ Tuple2: +//│ _1 = Cls: +//│ cls = class:Foo +//│ path = Select{class:Foo}: +//│ qual = Ref of member:Foo +//│ name = Ident of "class" +//│ _2 = Scoped: +//│ syms = Set($argument0$) +//│ body = Assign: //│ lhs = $argument0$ //│ rhs = Select{class:Foo.x}: //│ qual = Ref of caseScrut //│ name = Ident of "x" -//│ rest = Assign: \ -//│ lhs = a -//│ rhs = Ref of $argument0$ -//│ rest = Return: \ -//│ res = Ref of a -//│ implct = false -//│ dflt = S of Throw of Instantiate: -//│ mut = false -//│ cls = Select: -//│ qual = Ref of globalThis:globalThis -//│ name = Ident of "Error" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "match error" -//│ rest = End of "" +//│ rest = Scoped: \ +//│ syms = Set(a) +//│ body = Assign: +//│ lhs = a +//│ rhs = Ref of $argument0$ +//│ rest = Return: \ +//│ res = Ref of a +//│ implct = false +//│ dflt = S of Throw of Instantiate: +//│ mut = false +//│ cls = Select: +//│ qual = Ref of globalThis:globalThis +//│ name = Ident of "Error" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "match error" +//│ rest = End of "" //│ rest = Return: \ //│ res = Ref of member:lambda //│ implct = true diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index 85ea77ef62..732f21c79e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -15,7 +15,8 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; +//│ let b, tmp; +//│ let a; //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -23,7 +24,8 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; +//│ let b1, tmp1; +//│ let a1; //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -32,14 +34,16 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; +//│ let tmp2; +//│ let aaaa; //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let aaaaaaaaa, bbbb, tmp3; +//│ let bbbb, tmp3; +//│ let aaaaaaaaa; //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -50,7 +54,8 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; +//│ let bbbbbbbb, ccccccccc, dddddddddd, tmp4; +//│ let aaaaaaaaa1; //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -86,14 +91,15 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; -//│ tmp7 = globalThis.Object.freeze([ +//│ let tmp7; +//│ let tmp8; +//│ tmp8 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", //│ "ccccccccccccccccc", //│ "dddddddddddddddddd" //│ ]); -//│ tmp8 = globalThis.Object.freeze([ tmp7 ]); -//│ runtime.safeCall(discard(tmp8)) +//│ tmp7 = globalThis.Object.freeze([ tmp8 ]); +//│ runtime.safeCall(discard(tmp7)) diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 5faa54a309..6fef9160a3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -81,8 +81,7 @@ fun test(a) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(a) { -//│ let C11, C21; -//│ let tmp, tmp1; +//│ let C11, C21, tmp, tmp1; //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -155,8 +154,7 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz; -//│ let tmp; +//│ let bar, baz, tmp; //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 128459ca14..46d2b669a4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -39,8 +39,7 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let whoops; -//│ let tmp1, tmp2; +//│ let whoops, tmp1, tmp2; //│ whoops = function whoops() { //│ let tmp3; //│ tmp3 = Predef.print("ok"); @@ -138,8 +137,7 @@ fun test() = //│ JS (unsanitized): //│ let test1; //│ test1 = function test() { -//│ let whoops; -//│ let tmp1; +//│ let whoops, tmp1; //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -184,12 +182,13 @@ fun baz() = //│ return 2 //│ }; //│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3, tmp4; -//│ tmp1 = x + y; -//│ tmp2 = w(); -//│ tmp3 = tmp1 + tmp2; -//│ tmp4 = z(); -//│ return tmp3 + tmp4 +//│ let tmp1, tmp2, tmp3; +//│ let tmp4; +//│ tmp4 = x + y; +//│ tmp1 = w(); +//│ tmp2 = tmp4 + tmp1; +//│ tmp3 = z(); +//│ return tmp2 + tmp3 //│ }); //│ return lambda //│ }; @@ -214,8 +213,7 @@ fun a() = //│ return 1 //│ }; //│ c = function c() { -//│ let d; -//│ let tmp2, tmp3; +//│ let d, tmp2, tmp3; //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -244,8 +242,7 @@ fun b() = //│ return 1 //│ }; //│ d = function d() { -//│ let c1; -//│ let tmp3; +//│ let c1, tmp3; //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -268,14 +265,12 @@ fun c() = //│ JS (unsanitized): //│ let c; //│ c = function c() { -//│ let d, f; -//│ let tmp4; +//│ let d, f, tmp4; //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e; -//│ let tmp5, tmp6; +//│ let e, tmp5, tmp6; //│ e = function e() { //│ return 1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index d46187c0ac..a35ce88357 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -60,7 +60,7 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let x, f, x1, f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) +//│ let f, x, f1; let x1; x1 = 1; f1 = function f() { return x1 }; f = f1; x = 2; runtime.safeCall(f()) //│ = 1 //│ f = fun f //│ x = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index 37ffed6216..8ff1b3e343 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -5,12 +5,13 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; -//│ tmp = x + 1; +//│ let tmp, tmp1, tmp2; +//│ let tmp3; +//│ tmp3 = x + 1; +//│ tmp = tmp3 + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; -//│ tmp3 = tmp2 + 1; -//│ return tmp3 + 1 +//│ return tmp2 + 1 //│ }); //│ lambda(1) //│ = 6 @@ -20,13 +21,14 @@ //│ JS (unsanitized): //│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3, tmp4; -//│ tmp = x + 1; +//│ let tmp, tmp1, tmp2, tmp3; +//│ let tmp4; +//│ tmp4 = x + 1; +//│ tmp = tmp4 + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; //│ tmp3 = tmp2 + 1; -//│ tmp4 = tmp3 + 1; -//│ return tmp4 + 1 +//│ return tmp3 + 1 //│ }); //│ lambda1(1) //│ = 7 diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index bc312d91f9..8a89d8bd07 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -107,31 +107,32 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3, tmp4; -//│ tmp1 = 3; +//│ let x, tmp1, tmp2, tmp3; +//│ let tmp4; +//│ tmp4 = 3; //│ if (a instanceof A1.class) { -//│ tmp2 = 1; +//│ tmp1 = 1; //│ } else if (a instanceof B1.class) { -//│ tmp2 = 2; +//│ tmp1 = 2; //│ } else if (a instanceof C1.class) { -//│ tmp2 = 3; +//│ tmp1 = 3; //│ } else if (a instanceof D1.class) { //│ if (a instanceof A1.class) { -//│ tmp3 = 1 + tmp1; +//│ tmp2 = 1 + tmp4; //│ } else if (a instanceof B1.class) { -//│ tmp3 = 2 + tmp1; +//│ tmp2 = 2 + tmp4; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp4 = tmp3; -//│ tmp2 = Predef.print("done"); +//│ tmp3 = tmp2; +//│ tmp1 = Predef.print("done"); //│ } else if (a instanceof E1.class) { -//│ tmp4 = 5; -//│ tmp2 = Predef.print("done"); +//│ tmp3 = 5; +//│ tmp1 = Predef.print("done"); //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ x = tmp2; +//│ x = tmp1; //│ Predef.print(x) //│ > 1 //│ x = 1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 29994014fb..b4de5e4d59 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo5(tmp1, tmp2) +//│ let tmp1; let tmp2; tmp2 = Predef.print(1); tmp1 = Predef.print(2); Foo5(tmp2, tmp1) //│ > 1 //│ > 2 //│ = Foo((), ()) diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index ec8c349b28..b02697cac6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -254,7 +254,7 @@ let f = if _ then 1 else 0 //│ ║ l.249: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; +//│ let f15; let tmp21; tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 006ecea04e..2d626431ad 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -74,8 +74,7 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let Foo5; -//│ let tmp; +//│ let Foo5, tmp; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -99,8 +98,8 @@ let t = test() :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.100: new t -//│ ║ ^ +//│ ║ l.99: new t +//│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): //│ globalThis.Object.freeze(new t()) @@ -116,7 +115,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.117: new t() +//│ ║ l.116: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -218,7 +217,8 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let a, tmp, tmp1, tmp2, tmp3; +//│ let tmp, tmp1, tmp2, tmp3; +//│ let a; //│ a = globalThis.Object.freeze(new Foo10()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 4defe108c8..695963c4fe 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,17 +6,18 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; -//│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); -//│ folderName1 = runtime.safeCall(tmp.pop()); -//│ tmp1 = runtime.safeCall(globalThis.process.cwd()); -//│ tmp2 = runtime.safeCall(tmp1.split("/")); -//│ folderName2 = runtime.safeCall(tmp2.pop()); -//│ tmp3 = folderName2 === folderName1; +//│ let folderName1, folderName2, tmp, tmp1, tmp2, lambda; +//│ let tmp3; +//│ tmp3 = runtime.safeCall(globalThis.process.env.PWD.split("/")); +//│ folderName1 = runtime.safeCall(tmp3.pop()); +//│ tmp = runtime.safeCall(globalThis.process.cwd()); +//│ tmp1 = runtime.safeCall(tmp.split("/")); +//│ folderName2 = runtime.safeCall(tmp1.pop()); +//│ tmp2 = folderName2 === folderName1; //│ lambda = (undefined, function () { //│ return folderName2 === "shared" //│ }); -//│ runtime.short_or(tmp3, lambda) +//│ runtime.short_or(tmp2, lambda) //│ = true diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index 3e823a55c8..71fdb53d0c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,15 +76,16 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let x1, tmp6, tmp7, tmp8, arr2; -//│ tmp6 = globalThis.Object.freeze(new Term.Symbol("x")); -//│ x1 = globalThis.Object.freeze(new Term.Ref(tmp6)); -//│ tmp7 = Predef.print(x1); +//│ let x1, tmp6, tmp7, arr2; +//│ let tmp8; +//│ tmp8 = globalThis.Object.freeze(new Term.Symbol("x")); +//│ x1 = globalThis.Object.freeze(new Term.Ref(tmp8)); +//│ tmp6 = Predef.print(x1); //│ arr2 = globalThis.Object.freeze([ -//│ tmp6 +//│ tmp8 //│ ]); -//│ tmp8 = x1; -//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp8)) +//│ tmp7 = x1; +//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp7)) //│ > Ref(Symbol("x")) //│ = Lam([Symbol("x")], Ref(Symbol("x"))) @@ -118,20 +119,21 @@ f`(`0) :sjs `if `true then `true else `false //│ JS (unsanitized): -//│ let scrut, tmp29, tmp30, tmp31, tmp32, tmp33, tmp34, tmp35, tmp36, tmp37, tmp38, tmp39; -//│ tmp29 = globalThis.Object.freeze(new Term.Symbol("scrut")); -//│ scrut = globalThis.Object.freeze(new Term.Ref(tmp29)); -//│ tmp30 = globalThis.Object.freeze(new Term.Lit(true)); -//│ tmp33 = scrut; -//│ tmp34 = globalThis.Object.freeze(new Term.LitPattern(true)); -//│ tmp39 = globalThis.Object.freeze(new Term.Lit(true)); -//│ tmp35 = globalThis.Object.freeze(new Term.Else(tmp39)); -//│ tmp36 = globalThis.Object.freeze(new Term.Branch(tmp33, tmp34, tmp35)); -//│ tmp38 = globalThis.Object.freeze(new Term.Lit(false)); -//│ tmp37 = globalThis.Object.freeze(new Term.Else(tmp38)); -//│ tmp31 = globalThis.Object.freeze(new Term.Cons(tmp36, tmp37)); -//│ tmp32 = globalThis.Object.freeze(new Term.Let(tmp29, tmp30, tmp31)); -//│ globalThis.Object.freeze(new Term.IfLike(Term.Keyword.If, tmp32)) +//│ let scrut, tmp29, tmp30, tmp31, tmp32, tmp33, tmp34, tmp35, tmp36, tmp37, tmp38; +//│ let tmp39; +//│ tmp39 = globalThis.Object.freeze(new Term.Symbol("scrut")); +//│ scrut = globalThis.Object.freeze(new Term.Ref(tmp39)); +//│ tmp29 = globalThis.Object.freeze(new Term.Lit(true)); +//│ tmp32 = scrut; +//│ tmp33 = globalThis.Object.freeze(new Term.LitPattern(true)); +//│ tmp38 = globalThis.Object.freeze(new Term.Lit(true)); +//│ tmp34 = globalThis.Object.freeze(new Term.Else(tmp38)); +//│ tmp35 = globalThis.Object.freeze(new Term.Branch(tmp32, tmp33, tmp34)); +//│ tmp37 = globalThis.Object.freeze(new Term.Lit(false)); +//│ tmp36 = globalThis.Object.freeze(new Term.Else(tmp37)); +//│ tmp30 = globalThis.Object.freeze(new Term.Cons(tmp35, tmp36)); +//│ tmp31 = globalThis.Object.freeze(new Term.Let(tmp39, tmp29, tmp30)); +//│ globalThis.Object.freeze(new Term.IfLike(Term.Keyword.If, tmp31)) //│ = IfLike( //│ If, //│ Let( diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index 10940ea078..d71aee4d02 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -63,7 +63,7 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; f1 = 1; f = function f() { let tmp; tmp = f(); return tmp }; runtime.Unit +//│ let f; let f1; f1 = 1; f = function f() { let tmp; tmp = f(); return tmp }; runtime.Unit //│ f = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 2d9dca8bfd..218aa8a2cf 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -11,8 +11,7 @@ fun foo() = //│ JS (unsanitized): //│ let foo; //│ foo = function foo() { -//│ let bar; -//│ let x; +//│ let bar, x; //│ bar = function bar() { let y; y = 2; return x + y }; //│ x = 1; //│ return bar() @@ -37,7 +36,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x1, y, tmp; tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; +//│ let x1, y; let tmp; tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; //│ REPL> Sending: block$res4 = undefined //│ REPL> Collected: //│ > undefined @@ -102,8 +101,7 @@ fun g(x, y, z) = //│ JS (unsanitized): //│ let g; //│ g = function g(x2, y1, z) { -//│ let a, lambda; -//│ let tmp1, tmp2; +//│ let a, tmp1, tmp2, lambda; //│ split_root$: { //│ split_1$: { //│ if (x2 === true) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index b1c2122e16..489cd08f4d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -30,7 +30,8 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let old, tmp1, tmp2, tmp3; +//│ let tmp1, tmp2, tmp3; +//│ let old; //│ old = x1; //│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } //│ tmp1 @@ -71,19 +72,19 @@ example() //│ JS (unsanitized): //│ let example2; //│ example2 = function example() { -//│ let get_x; -//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; +//│ let get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, get_x1; +//│ let x2; //│ x2 = 0; -//│ get_x = function get_x() { +//│ get_x1 = function get_x() { //│ return x2 //│ }; -//│ get_x1 = get_x; +//│ get_x = get_x1; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; //│ x2 = tmp5; //│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x1()); +//│ tmp7 = runtime.safeCall(get_x()); //│ tmp8 = Predef.print(tmp7); //│ tmp9 = (tmp6 , tmp8); //│ tmp4 = tmp9; @@ -91,7 +92,7 @@ example() //│ x2 = old1; //│ } //│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x1()); +//│ tmp11 = runtime.safeCall(get_x()); //│ return Predef.print(tmp11) //│ }; //│ example2() @@ -113,13 +114,13 @@ example() //│ JS (unsanitized): //│ let example3; //│ example3 = function example() { -//│ let get_x; -//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; +//│ let get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, get_x1; +//│ let x2; //│ x2 = 0; -//│ get_x = function get_x() { +//│ get_x1 = function get_x() { //│ return x2 //│ }; -//│ get_x1 = get_x; +//│ get_x = get_x1; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; @@ -132,7 +133,7 @@ example() //│ } //│ y = tmp4; //│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x1()); +//│ tmp9 = runtime.safeCall(get_x()); //│ tmp10 = Predef.print(tmp9); //│ return y //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 60ab9f2c5c..4ef0cd898a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) +//│ let f2; f2 = function f(x) { throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 1b3593632c..ba70b84e49 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -9,7 +9,8 @@ fun fib(a) = if //│ JS (unsanitized): //│ let fib; //│ fib = function fib(a) { -//│ let scrut, tmp, tmp1, tmp2, tmp3; +//│ let tmp, tmp1, tmp2, tmp3; +//│ let scrut; //│ scrut = a <= 1; //│ if (scrut === true) { //│ return a diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 6dd3fd47b9..9ec87b064d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -105,8 +105,7 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let i2; -//│ let scrut3, tmp19, tmp20; +//│ let i2, scrut3, tmp19, tmp20; //│ tmp21: while (true) { //│ i2 = 0; //│ scrut3 = i2 < 10; @@ -198,8 +197,7 @@ fun f(ls) = //│ JS (unsanitized): //│ let f; //│ f = function f(ls) { -//│ let tl, h; -//│ let argument0$, argument1$, tmp28; +//│ let tl, h, argument0$, argument1$, tmp28; //│ tmp29: while (true) { //│ if (ls instanceof Cons1.class) { //│ argument0$ = ls.hd; @@ -276,10 +274,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.276: then 0(0) +//│ ║ l.274: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.275: while print("Hello World"); false +//│ ║ l.273: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -289,12 +287,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.288: while { print("Hello World"), false } +//│ ║ l.286: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.289: then 0(0) +//│ ║ l.287: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.290: else 1 +//│ ║ l.288: else 1 //│ ╙── ^^^^ :fixme @@ -304,14 +302,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.302: print("Hello World") +//│ ║ l.300: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.303: false +//│ ║ l.301: false //│ ║ ^^^^^^^^^ -//│ ║ l.304: then 0(0) +//│ ║ l.302: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.305: else 1 +//│ ║ l.303: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index b7e9ea0e36..adeb680886 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -77,11 +77,12 @@ let f3 = baz //│ f31 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1, tmp2; -//│ tmp = baz(x); -//│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); -//│ tmp2 = runtime.safeCall(tmp1(y)); -//│ return runtime.safeCall(tmp2(instance$Ident$_Num$_)) +//│ let tmp, tmp1; +//│ let tmp2; +//│ tmp2 = baz(x); +//│ tmp = runtime.safeCall(tmp2(instance$Ident$_Int$_)); +//│ tmp1 = runtime.safeCall(tmp(y)); +//│ return runtime.safeCall(tmp1(instance$Ident$_Num$_)) //│ }); //│ return lambda1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index de274fb30f..2fbfee161d 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -19,7 +19,8 @@ fun f() = //│ JS (unsanitized): //│ let f, getLocals2; //│ getLocals2 = function getLocals() { -//│ let prev, thisInfo, arr, tmp; +//│ let thisInfo, arr, tmp; +//│ let prev; //│ prev = []; //│ arr = globalThis.Object.freeze([]); //│ thisInfo = new runtime.FnLocalsInfo.class("\u2039top level\u203A", arr); @@ -29,7 +30,8 @@ fun f() = //│ f = function f() { //│ let i, j, k, scrut, tmp, tmp1, getLocals3, Cont$func$f$1, doUnwind; //│ getLocals3 = function getLocals() { -//│ let i1, j1, k1, prev, thisInfo, arr, tmp2; +//│ let i1, j1, k1, thisInfo, arr, tmp2; +//│ let prev; //│ prev = getLocals2(); //│ i1 = new runtime.LocalVarInfo.class("i", i); //│ j1 = new runtime.LocalVarInfo.class("j", j); @@ -126,8 +128,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:126:3) -//│ at lambda_test (Debugging.mls:123:3) +//│ at lambda (Debugging.mls:128:3) +//│ at lambda_test (Debugging.mls:125:3) import "../../mlscript-compile/Runtime.mls" @@ -206,9 +208,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:199:3) with locals: j=200 +//│ > at f (Debugging.mls:201:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:201:3) +//│ > at f (Debugging.mls:203:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:202:3) with locals: j=300 +//│ > at f (Debugging.mls:204:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 74770fcc40..4b37c2c874 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,7 +155,8 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let str, scrut, tmp9, tmp10, handleBlock$7; +//│ let scrut, tmp9, tmp10, handleBlock$7; +//│ let str; //│ str = ""; //│ scrut = true; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 722499592a..d373b0410e 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,7 +8,8 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let x, old, tmp, tmp1, tmp2; +//│ let old, tmp, tmp1, tmp2; +//│ let x; //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index df45c1fece..dcbf81e66e 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -185,9 +185,9 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let x, y, z, tmp6, tmp7, capture; +//│ let x, y, z, tmp6, tmp7; +//│ let capture; //│ capture = new f$capture3(null); -//│ let w; //│ x = 1; //│ y = 10; //│ z = 10; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index a249f407b4..14f4e9dbd8 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -59,8 +59,8 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let g$here; -//│ let used2, unused2, foo, tmp; +//│ let unused2, foo, tmp, g$here; +//│ let used2; //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); @@ -80,12 +80,13 @@ f(1,2,3,4,5,6) //│ JS (unsanitized): //│ let f4, g$4; //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; -//│ tmp = a1 + a2; -//│ tmp1 = tmp + a3; -//│ tmp2 = tmp1 + a4; -//│ tmp3 = tmp2 + a5; -//│ return tmp3 + a6 +//│ let tmp, tmp1, tmp2; +//│ let tmp3; +//│ tmp3 = a1 + a2; +//│ tmp = tmp3 + a3; +//│ tmp1 = tmp + a4; +//│ tmp2 = tmp1 + a5; +//│ return tmp2 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { //│ let tmp; @@ -182,7 +183,8 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let a1, tmp7, tmp8, capture; +//│ let a1, tmp7, tmp8; +//│ let capture; //│ capture = new f$capture5(mutated); //│ a1 = g$6(immutable, capture); //│ tmp7 = h$2(capture); @@ -280,9 +282,9 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { -//│ let capture, f$here; +//│ let f$here; +//│ let capture; //│ capture = new g$capture1(null); -//│ let y1; //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here @@ -382,8 +384,8 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let g$here; -//│ let y1, scrut; +//│ let scrut, g$here; +//│ let y1; //│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { @@ -427,7 +429,8 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let a1, tmp10, capture, g$here; +//│ let a1, tmp10, g$here; +//│ let capture; //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); //│ a1 = g$here; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 4c400e4dc6..f64cea9959 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -53,8 +53,8 @@ fun foo() = //│ } //│ }); //│ foo2 = function foo() { -//│ let lambda$here; -//│ let x, scrut, tmp2, tmp3; +//│ let scrut, tmp2, tmp3, lambda$here; +//│ let x; //│ x = 1; //│ tmp4: while (true) { //│ scrut = true; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index b27cccd164..4b807e12f3 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -145,7 +145,8 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let tmp, M$1, capture; +//│ let tmp, M$1; +//│ let capture; //│ capture = new foo$capture3(y); //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); //│ tmp = foo3$(M$1, x, capture); diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index bfe2893f41..0dcc90b512 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -32,9 +32,9 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let tmp, capture, bar$here; +//│ let tmp, bar$here; +//│ let capture; //│ capture = new foo$capture1(null); -//│ let x; //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); //│ tmp = runtime.safeCall(xs.push(bar$here)); diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 8b772c4748..06ec481e9c 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -148,7 +148,8 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1, curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1, stackDelayRes; +//│ let curDepth; //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index 809b3151c6..bb16dac916 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -42,8 +42,7 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let idx1, idx2, idx3, idx4, idx5; -//│ let tmp, tmp1; +//│ let tmp, tmp1, idx1, idx2, idx3, idx4, idx5; //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; //│ idx5 = idx + 0; diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 01db6e89df..3584ee8a24 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -135,7 +135,8 @@ fun popByIndex(start, end, acc, lft) = //│ JS (unsanitized): //│ let popByIndex; //│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let scrut, tmp34, tmp35, tmp36; +//│ let tmp34, tmp35, tmp36; +//│ let scrut; //│ scrut = start >= end; //│ if (scrut === true) { //│ return acc diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 8f8affbdbb..98981c7a54 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -16,13 +16,18 @@ if x === 0 then 1 else 2 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; +//│ let a, tmp; +//│ let scrut1; +//│ scrut1 = x === 0; +//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } +//│ a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; +//│ let tmp1; +//│ let scrut2; //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -336,8 +341,7 @@ fun foo(x, y, z) = if x is //│ JS (unsanitized): //│ let foo2; //│ foo2 = function foo(x1, y1, z1) { -//│ let value; -//│ let tmp6; +//│ let value, tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { @@ -465,7 +469,8 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; +//│ let y1, tmp6; +//│ let scrut3; //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 9533eb3dde..33339b6e3a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -8,8 +8,7 @@ fun nonsense(xs) = if xs is //│ JS (unsanitized): //│ let nonsense; //│ nonsense = function nonsense(xs) { -//│ let ys; -//│ let middleElements; +//│ let ys, middleElements; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -30,8 +29,7 @@ fun lead_and_last(xs) = if xs is //│ JS (unsanitized): //│ let lead_and_last; //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y; -//│ let lastElement0$, middleElements, element0$; +//│ let x, ys, y, lastElement0$, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -65,8 +63,7 @@ fun nested_tuple_patterns(xs) = if xs is //│ JS (unsanitized): //│ let nested_tuple_patterns; //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z; -//│ let lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index f385992ea7..a241eecb60 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -35,7 +35,8 @@ fun foo(x) = x is Cross //│ JS (unsanitized): //│ let foo; //│ foo = function foo(x2) { -//│ let unapplyResult, output, bindings; +//│ let output, bindings; +//│ let unapplyResult; //│ unapplyResult = runtime.safeCall(Cross1.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index ca5f1fb528..f3d5987365 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -108,34 +108,22 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: with JSBuilderArgNumSanityChecks val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) - val le = lowered0.copy(main = lowered0.main.mapTail: - case e: End => - Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - case Return(res, implct) => - assert(implct) - Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case Scoped(_, body) => ??? // TODO - case tl: (Throw | Break | Continue) => tl - ) - // TODO: re-implement - // TODO: remove mutation - // var toplvlDefinedVars = Set.empty[Symbol] - // def assignResSym(b: Block, toplvl: Boolean): Block = - // b.mapTail: - // case e: End => - // Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - // case Return(res, implct) => - // assert(implct) - // Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - // case s@Scoped(xs, body) => - // if toplvl then - // toplvlDefinedVars = xs - // assignResSym(body, false) - // else - // Scoped(xs, assignResSym(body, false)) - // case tl: (Throw | Break | Continue) => tl - // val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + def assignResSym(b: Block, toplvl: Boolean): Block = + b.mapTail: + case e: End => + Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + case Return(res, implct) => + assert(implct) + Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case s@Scoped(xs, body) => + if toplvl then + // toplvlDefinedVars = xs + assignResSym(body, false) + else + Scoped(xs, assignResSym(body, false)) + case tl: (Throw | Break | Continue) => tl + val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then output(s"Lowered:") diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala index dfe05c24e0..a85280301c 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala @@ -19,7 +19,6 @@ import hkmc2.semantics.Elaborator import scala.collection.mutable.ListBuffer abstract class LlirDiffMaker extends BbmlDiffMaker: - val llir = NullaryCommand("llir") val sllir = NullaryCommand("sllir") val intl = NullaryCommand("intl") val lprelude = NullaryCommand("lpre") diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala index e748836b81..f08ea6abbb 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala @@ -53,6 +53,8 @@ abstract class MLsDiffMaker extends DiffMaker: val ppLoweredTree = NullaryCommand("slot") val showContext = NullaryCommand("ctx") val parseOnly = NullaryCommand("parseOnly") + + val llir = NullaryCommand("llir") val typeCheck = FlagCommand(false, "typeCheck") @@ -97,6 +99,7 @@ abstract class MLsDiffMaker extends DiffMaker: )), liftDefns = Opt.when(liftDefns.isSet)(LiftDefns()), stageCode = stageCode.isSet, + llir = llir.isSet, target = if wasm.isSet then CompilationTarget.Wasm else CompilationTarget.JS, ) From 44f132f9ea5270d79023780682c3b9df8537ea91 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Wed, 19 Nov 2025 02:29:14 +0800 Subject: [PATCH 07/72] Revert "Try to use a pass to insert Scoped" This reverts commit cf9d78f79bfbf56149ab806726241fb280107f80. --- .../shared/src/main/scala/hkmc2/Config.scala | 2 - .../src/main/scala/hkmc2/codegen/Block.scala | 32 +++++++--- .../main/scala/hkmc2/codegen/Lowering.scala | 46 ++------------- .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../src/main/scala/hkmc2/semantics/Term.scala | 6 ++ .../src/test/mlscript-compile/Predef.mjs | 6 +- .../src/test/mlscript-compile/Runtime.mjs | 27 ++++++--- .../basics/CompanionModules_Classes.mls | 3 +- .../shared/src/test/mlscript/basics/Drop.mls | 5 +- .../src/test/mlscript/basics/LazySpreads.mls | 10 ++-- .../test/mlscript/basics/MultiParamLists.mls | 39 ++++++------- .../mlscript/basics/MultilineExpressions.mls | 17 +++--- .../src/test/mlscript/bbml/bbCodeGen.mls | 12 ++-- .../src/test/mlscript/bbml/bbGetters.mls | 7 ++- .../src/test/mlscript/codegen/Arrays.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 28 +++++---- .../src/test/mlscript/codegen/CaseOfCase.mls | 3 +- .../src/test/mlscript/codegen/ClassInFun.mls | 5 +- .../test/mlscript/codegen/ClassMatching.mls | 12 ++-- .../src/test/mlscript/codegen/ConsoleLog.mls | 16 +++-- .../test/mlscript/codegen/DelayedLetInit.mls | 8 +-- .../src/test/mlscript/codegen/EarlyReturn.mls | 3 +- .../test/mlscript/codegen/FieldSymbols.mls | 58 +++++++++---------- .../src/test/mlscript/codegen/Formatting.mls | 24 +++----- .../src/test/mlscript/codegen/FunInClass.mls | 6 +- .../src/test/mlscript/codegen/Getters.mls | 31 +++++----- .../src/test/mlscript/codegen/Hygiene.mls | 2 +- .../test/mlscript/codegen/InlineLambdas.mls | 18 +++--- .../test/mlscript/codegen/MergeMatchArms.mls | 25 ++++---- .../test/mlscript/codegen/ParamClasses.mls | 2 +- .../src/test/mlscript/codegen/PartialApps.mls | 2 +- .../test/mlscript/codegen/PlainClasses.mls | 12 ++-- .../shared/src/test/mlscript/codegen/Pwd.mls | 17 +++--- .../src/test/mlscript/codegen/Quasiquotes.mls | 44 +++++++------- .../src/test/mlscript/codegen/RandomStuff.mls | 2 +- .../src/test/mlscript/codegen/Scoped.mls | 8 ++- .../src/test/mlscript/codegen/SetIn.mls | 25 ++++---- .../src/test/mlscript/codegen/Throw.mls | 2 +- .../src/test/mlscript/codegen/TraceLog.mls | 3 +- .../src/test/mlscript/codegen/While.mls | 24 ++++---- .../src/test/mlscript/ctx/EtaExpansion.mls | 11 ++-- .../src/test/mlscript/handlers/Debugging.mls | 16 +++-- .../mlscript/handlers/RecursiveHandlers.mls | 3 +- .../test/mlscript/handlers/SetInHandlers.mls | 3 +- .../src/test/mlscript/lifter/ClassInFun.mls | 4 +- .../src/test/mlscript/lifter/FunInFun.mls | 31 +++++----- .../shared/src/test/mlscript/lifter/Loops.mls | 4 +- .../test/mlscript/lifter/ModulesObjects.mls | 3 +- .../src/test/mlscript/lifter/Mutation.mls | 4 +- .../test/mlscript/lifter/StackSafetyLift.mls | 3 +- .../src/test/mlscript/objbuf/Mutation.mls | 3 +- .../test/mlscript/std/FingerTreeListTest.mls | 3 +- .../ucs/normalization/Deduplication.mls | 15 ++--- .../test/mlscript/ucs/patterns/RestTuple.mls | 9 ++- .../src/test/mlscript/ups/MatchResult.mls | 3 +- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 42 +++++++++----- .../src/test/scala/hkmc2/LlirDiffMaker.scala | 1 + .../src/test/scala/hkmc2/MLsDiffMaker.scala | 3 - 58 files changed, 368 insertions(+), 389 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/Config.scala b/hkmc2/shared/src/main/scala/hkmc2/Config.scala index db827c709e..d0a684622b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/Config.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/Config.scala @@ -21,7 +21,6 @@ case class Config( effectHandlers: Opt[EffectHandlers], liftDefns: Opt[LiftDefns], stageCode: Bool, - llir: Bool, target: CompilationTarget, ): @@ -38,7 +37,6 @@ object Config: effectHandlers = N, liftDefns = N, stageCode = false, - llir = false, target = CompilationTarget.JS ) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index c22f503ba6..9d63e962ad 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -51,6 +51,25 @@ sealed abstract class Block extends Product: case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars // cannot use `body.definedVars - syms`. lifter computes captures based on it? case Scoped(syms, body) => body.definedVars + + // TODO: remove it + lazy val tempVars: Set[Local] = this match + case _: Return | _: Throw => Set.empty + case Begin(sub, rst) => sub.tempVars ++ rst.tempVars + case Assign(l: TempSymbol, r, rst) => rst.tempVars + l + case Assign(l, r, rst) => rst.tempVars + case AssignField(l, n, r, rst) => rst.tempVars + case AssignDynField(l, n, ai, r, rst) => rst.tempVars + case Match(scrut, arms, dflt, rst) => + arms.flatMap(_._2.tempVars).toSet ++ dflt.toList.flatMap(_.tempVars) ++ rst.tempVars + case End(_) => Set.empty + case Break(_) => Set.empty + case Continue(_) => Set.empty + case Define(defn, rst) => rst.tempVars + case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.tempVars + case TryBlock(sub, fin, rst) => sub.tempVars ++ fin.tempVars ++ rst.tempVars + case Label(lbl, _, bod, rst) => bod.tempVars ++ rst.tempVars + case Scoped(syms, body) => body.tempVars lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -68,6 +87,7 @@ sealed abstract class Block extends Product: // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match + case Scoped(syms, body) => Scoped(syms, body.mapTail(f)) case b: BlockTail => f(b) case Begin(sub, rst) => Begin(sub, rst.mapTail(f)) case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f)) @@ -254,14 +274,10 @@ sealed abstract class Block extends Product: else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) case Scoped(syms, body) => - if syms.isEmpty then body.flatten(k) - else - body.flatten(k) match - case Scoped(syms2, body) => Scoped(syms ++ syms2, body) - case newBody => - if newBody is body - then this - else Scoped(syms, newBody) + val newBody = body.flatten(k) + if newBody is body + then this + else Scoped(syms, newBody) case e: End => k(e) case t: BlockTail => this diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 4c82140263..cd343d9e77 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -935,13 +935,9 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val merged = MergeMatchArmTransformer.applyBlock(bufferable) - val scoped = - if !config.llir then setupScoped(merged)(using imps.map(_.sym).toSet) - else merged - val res = - if config.stageCode then Instrumentation(using summon).applyBlock(scoped) - else scoped + if config.stageCode then Instrumentation(using summon).applyBlock(merged) + else merged Program( imps.map(imp => imp.sym -> imp.str), @@ -953,41 +949,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): subTerm(prefix): p => val selRes = TempSymbol(N, "selRes") k(Select(p, nme)(sym)) - - def setupScoped(blk: Block)(using syms: Set[Local]): Block = blk match - case tail: BlockTail => tail - case Match(scrut, arms, dflt, rest) => - Match(scrut, arms.map { - case (c, b) => c -> setupScoped(b) - }, dflt.map(setupScoped), setupScoped(rest)) - case Label(s, loop, body, rest) => - Label(s, loop, setupScoped(body), setupScoped(rest)) - case Begin(sub, rest) => - Begin(setupScoped(sub), setupScoped(rest)) - case TryBlock(sub, fd, rest) => - TryBlock(setupScoped(sub), setupScoped(fd), setupScoped(rest)) - case Assign(lhs, rhs, rest) if syms(lhs) => Assign(lhs, rhs, setupScoped(rest)) - case Assign(lhs, rhs, rest) => - val newSyms = syms + lhs - Scoped(Set(lhs), Assign(lhs, rhs, setupScoped(rest)(using newSyms))).flattened - case assign @ AssignField(lhs, nme, rhs, rest) => AssignField(lhs, nme, rhs, setupScoped(rest))(assign.symbol) - case AssignDynField(lhs, fld, arrayIdx, rhs, rest) => AssignDynField(lhs, fld, arrayIdx, rhs, setupScoped(rest)) - case HandleBlock(lhs, res, par, args, cls, handlers, body, rest) => - HandleBlock(lhs, res, par, args, cls, handlers, setupScoped(body), setupScoped(rest)) - case Define(defn, rest) => - val newDefn = defn match { - case d: ValDefn => d - case FunDefn(owner, sym, params, body) => - val newSyms = syms ++ params.flatMap(_.params.map(_.sym)) - FunDefn(owner, sym, params, setupScoped(body)(using newSyms)) - case ClsLikeDefn(owner, isym, sym, k, paramsOpt, auxParams, parentPath, methods, - privateFields, publicFields, preCtor, ctor, companion, bufferable) => - ClsLikeDefn(owner, isym, sym, k, paramsOpt, auxParams, parentPath, methods, - privateFields, publicFields, preCtor, ctor, companion, bufferable) - } - val newSyms = syms + newDefn.sym - Define(newDefn, setupScoped(rest)(using newSyms)) - final def setupFunctionOrByNameDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = @@ -998,7 +959,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - (paramLists, returnedTerm(bodyTerm)) + val body = returnedTerm(bodyTerm) + (paramLists, Scoped(bodyTerm.definedSyms ++ body.tempVars, body)) // TODO: move it to block function def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 32cd6a351d..34393f141d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -503,7 +503,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - // still need to keep the filter for diff test + // TODO: we should remove `syms.filter` after temp vars can be correctly handled. val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 79ae65ed97..34596bd89a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -363,6 +363,12 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdField(field, rhs) => RcdField(field.mkClone, rhs.mkClone) case RcdSpread(rcd) => RcdSpread(rcd.mkClone) case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) + + lazy val definedSyms: Set[Symbol] = this match + case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty + case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + case LetDecl(sym, annotations) => Set(sym) + case _ => Set.empty // TODO def describe: Str = val desc = this match diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index a4ce900a78..b9b7d0cb68 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -124,7 +124,8 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; + let i, init; + let len, scrut, scrut1, tmp, tmp1, tmp2, tmp3; len = rest.length; scrut = len == 0; if (scrut === true) { @@ -152,7 +153,8 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda, tmp; + let lambda; + let tmp; lambda = (undefined, function (acc, x) { let tmp1, tmp2, tmp3; if (typeof x === 'string') { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index e5b5a48eeb..4f14cc8cc5 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -197,7 +197,8 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let scrut, prev, tmp; + let prev; + let scrut, tmp; scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -415,7 +416,8 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let name, lambda; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; tmp = got < expected; lambda = (undefined, function () { let lambda1; @@ -526,7 +528,8 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; + let cur, locals, curLocals, loc, loc1, localsMsg, lambda; + let msg, curHandler, atTail, scrut, scrut1, scrut2, scrut3, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; msg = header; curHandler = tr.contTrace; atTail = true; @@ -622,7 +625,8 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + let result, lambda; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; if (cont instanceof Runtime.FunctionContFrame.class) { tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; @@ -668,7 +672,8 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + let result, lambda; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; if (cont instanceof Runtime.HandlerContFrame.class) { result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { @@ -727,7 +732,8 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; + let vis, hl, cur; + let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; if (contTrace instanceof Runtime.ContTrace.class) { tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; @@ -814,7 +820,8 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let nxt, scrut, tmp, tmp1; + let nxt; + let scrut, tmp, tmp1; tmp2: while (true) { if (cur instanceof Runtime.EffectSig.class) { nxt = Runtime.handleEffect(cur); @@ -958,7 +965,8 @@ globalThis.Object.freeze(class Runtime { return tmp4 } static checkDepth() { - let scrut, tmp, lambda; + let lambda; + let scrut, tmp; tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -971,7 +979,8 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, scrut, saved, tmp, tmp1; + let saved; + let result, scrut, tmp, tmp1; Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 9d0f039450..6cef84654f 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -87,8 +87,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1; -//│ let tmp1; +//│ let Foo1, tmp1; //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index 12bae13d9f..9d957653bc 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,14 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp; let tmp1; tmp1 = 2 + 2; tmp = Predef.id(tmp1); runtime.Unit +//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let b, tmp2, tmp3; -//│ let a; +//│ let a, b, tmp2, tmp3; //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index 26fb4b1450..a90ca7695e 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -21,7 +21,8 @@ fun buildPalindrome = case //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; +//│ let n; +//│ let tmp3, tmp4, tmp5; //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -52,7 +53,8 @@ fun sum2 = case //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; +//│ let x, xs, y; +//│ let lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { @@ -80,13 +82,13 @@ sum2(arr) :e fun f(..xs) = xs //│ ╔══[ERROR] Lazy spread parameters not allowed. -//│ ║ l.81: fun f(..xs) = xs +//│ ║ l.83: fun f(..xs) = xs //│ ╙── ^^^^ :ge sum2(..arr) //│ ╔══[COMPILATION ERROR] Lazy spreads are not supported in call arguments -//│ ║ l.87: sum2(..arr) +//│ ║ l.89: sum2(..arr) //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function expected 1 argument but got 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 0e177a12fa..178203f9e4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -35,19 +35,18 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ let tmp1, tmp2; -//│ let tmp3; -//│ tmp3 = 10 * n1; -//│ tmp1 = tmp3 + n2; -//│ tmp2 = 10 * tmp1; -//│ return tmp2 + n3 +//│ let tmp1, tmp2, tmp3; +//│ tmp1 = 10 * n1; +//│ tmp2 = tmp1 + n2; +//│ tmp3 = 10 * tmp2; +//│ return tmp3 + n3 //│ } //│ } //│ }; f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1; let tmp2; tmp2 = f2(4); tmp1 = runtime.safeCall(tmp2(2)); runtime.safeCall(tmp1(0)) +//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 @@ -57,14 +56,13 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) //│ return (n2) => { //│ return (n3) => { //│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6; -//│ let tmp7; -//│ tmp7 = 10 * n1; -//│ tmp3 = tmp7 + n2; -//│ tmp4 = 10 * tmp3; -//│ tmp5 = tmp4 + n3; -//│ tmp6 = 10 * tmp5; -//│ return tmp6 + n4 +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; +//│ tmp3 = 10 * n1; +//│ tmp4 = tmp3 + n2; +//│ tmp5 = 10 * tmp4; +//│ tmp6 = tmp5 + n3; +//│ tmp7 = 10 * tmp6; +//│ return tmp7 + n4 //│ } //│ } //│ } @@ -72,12 +70,11 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4; -//│ let tmp5; -//│ tmp5 = f3(3); -//│ tmp3 = runtime.safeCall(tmp5(0)); -//│ tmp4 = runtime.safeCall(tmp3(3)); -//│ runtime.safeCall(tmp4(1)) +//│ let tmp3, tmp4, tmp5; +//│ tmp3 = f3(3); +//│ tmp4 = runtime.safeCall(tmp3(0)); +//│ tmp5 = runtime.safeCall(tmp4(3)); +//│ runtime.safeCall(tmp5(1)) //│ = 3031 diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index 41c2209da9..5025320ce4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,8 +80,7 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut; -//│ let tmp17; +//│ let scrut, tmp17; //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { @@ -117,23 +116,23 @@ if 1 + 2 * 3 then 0 + 4 then 1 //│ ╔══[PARSE ERROR] Operator cannot be used inside this operator split -//│ ║ l.118: + 4 then 1 +//│ ║ l.117: + 4 then 1 //│ ║ ^ //│ ╟── as it has lower precedence than the splitting operator here -//│ ║ l.117: * 3 then 0 +//│ ║ l.116: * 3 then 0 //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected 'then' keyword in this operator split inner position -//│ ║ l.118: + 4 then 1 +//│ ║ l.117: + 4 then 1 //│ ║ ^^^^ //│ ╟── Note: the operator split starts here -//│ ║ l.117: * 3 then 0 +//│ ║ l.116: * 3 then 0 //│ ╙── ^ //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.116: if 1 + 2 +//│ ║ l.115: if 1 + 2 //│ ║ ^ -//│ ║ l.117: * 3 then 0 +//│ ║ l.116: * 3 then 0 //│ ║ ^^^^^^^^^^^^ -//│ ║ l.118: + 4 then 1 +//│ ║ l.117: + 4 then 1 //│ ╙── ^^^^^ :pt diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index b05e9f394d..eed563d668 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -151,7 +151,8 @@ fun pow(x) = case //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; +//│ let n; +//│ let tmp, tmp1, tmp2; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -207,7 +208,8 @@ fun fact = case //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; +//│ let n; +//│ let tmp1, tmp2, tmp3; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -258,8 +260,7 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let y; -//│ let x4; +//│ let x4, y; //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); //│ y.value @@ -270,8 +271,7 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let y1; -//│ let x5; +//│ let x5, y1; //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); //│ y1.value = 0; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 134e59fd82..2f5baa282d 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -89,7 +89,8 @@ fun test2() = //│ JS (unsanitized): //│ let test22; //│ test22 = function test2() { -//│ let funny, tmp1; +//│ let funny; +//│ let tmp1; //│ funny = function funny() { //│ let lambda, lambda1; //│ lambda = (undefined, function (caseScrut) { @@ -100,8 +101,8 @@ fun test2() = //│ } //│ }); //│ lambda1 = (undefined, function (caseScrut) { -//│ let tmp2, tmp3, tmp4; //│ let n; +//│ let tmp2, tmp3, tmp4; //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; @@ -131,7 +132,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.132: print("Hi") +//│ ║ l.133: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index e58e996967..9cb5c5210c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1; let tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index 1d5c601b03..668826abb5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -43,21 +43,19 @@ print("Hi") //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: -//│ syms = Set($tmp) -//│ body = Assign: -//│ lhs = $tmp -//│ rhs = Call: -//│ fun = Select{member:print}: -//│ qual = Ref of member:Predef -//│ name = Ident of "print" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "Hi" -//│ rest = Return: \ -//│ res = Lit of IntLit of 2 -//│ implct = true +//│ main = Assign: +//│ lhs = $tmp +//│ rhs = Call: +//│ fun = Select{member:print}: +//│ qual = Ref of member:Predef +//│ name = Ident of "print" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "Hi" +//│ rest = Return: \ +//│ res = Lit of IntLit of 2 +//│ implct = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 22009ea6bb..30f7fee561 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -17,7 +17,8 @@ fun test(x) = //│ JS (unsanitized): //│ let test; //│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; +//│ let v, v1; +//│ let scrut, argument0$, argument0$1, tmp, tmp1; //│ if (x instanceof Some1.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index b5c1b26b34..7e7c2c81f6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -39,7 +39,8 @@ fun test(x) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(x) { -//│ let Foo2, tmp; +//│ let Foo2; +//│ let tmp; //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -68,7 +69,7 @@ test(123) :re test() //│ ╔══[ERROR] Expected 1 arguments, got 0 -//│ ║ l.69: test() +//│ ║ l.70: test() //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Function 'test' expected 1 argument but got 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 2591b12c47..19eb06aa44 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,8 +9,7 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let x, argument0$; -//│ let scrut; +//│ let scrut, x, argument0$; //│ scrut = Some1(0); //│ if (scrut instanceof Some1.class) { //│ argument0$ = scrut.value; @@ -56,7 +55,8 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; +//│ let x4; +//│ let argument0$4; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -121,7 +121,8 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f4; //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp5; +//│ let x4; +//│ let scrut1, argument0$4, tmp5; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some1.class) { @@ -167,7 +168,8 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f5; //│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; +//│ let u, a, b; +//│ let argument0$4, argument1$; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index 394fbe1234..be5f1aee43 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -14,10 +14,9 @@ console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp; -//│ let tmp1; -//│ tmp1 = runtime.safeCall(globalThis.console.log("a")); -//│ tmp = runtime.safeCall(globalThis.console.log("b")); +//│ let tmp, tmp1; +//│ tmp = runtime.safeCall(globalThis.console.log("a")); +//│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 //│ > a //│ > b @@ -50,13 +49,12 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3; -//│ let tmp4; -//│ tmp4 = runtime.safeCall(globalThis.console.log("a")); +//│ let x, y, tmp2, tmp3, tmp4; +//│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; -//│ tmp2 = runtime.safeCall(globalThis.console.log("b")); +//│ tmp3 = runtime.safeCall(globalThis.console.log("b")); //│ y = x + 1; -//│ tmp3 = runtime.safeCall(globalThis.console.log("c")); +//│ tmp4 = runtime.safeCall(globalThis.console.log("c")); //│ y * 2 //│ > a //│ > b diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index 56640d603c..a4fddd8d26 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -80,8 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let scrut; -//│ let foo1; +//│ let foo1, scrut; //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -96,8 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let scrut1; -//│ let foo2; +//│ let foo2, scrut1; //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -122,7 +120,7 @@ foo :fixme fun f() = foo = 0 //│ ╔══[PARSE ERROR] Expected end of input; found '=' keyword instead -//│ ║ l.123: fun f() = foo = 0 +//│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): //│ let f4; f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index f196de001c..797b5c0c47 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -20,8 +20,7 @@ fun f(x) = //│ JS (unsanitized): //│ let f1; //│ f1 = function f(x) { -//│ let tmp, tmp1; -//│ let scrut; +//│ let scrut, tmp, tmp1; //│ scrut = x < 0; //│ if (scrut === true) { //│ tmp = Predef.print("whoops"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 6af2a7a32f..3ea2b5151a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -98,40 +98,38 @@ case //│ sign = N //│ modulefulness = Modulefulness of N //│ restParam = N -//│ body = Match: -//│ scrut = Ref of caseScrut -//│ arms = Ls of -//│ Tuple2: -//│ _1 = Cls: -//│ cls = class:Foo -//│ path = Select{class:Foo}: -//│ qual = Ref of member:Foo -//│ name = Ident of "class" -//│ _2 = Scoped: -//│ syms = Set($argument0$) -//│ body = Assign: +//│ body = Scoped: +//│ syms = Set($argument0$) +//│ body = Match: +//│ scrut = Ref of caseScrut +//│ arms = Ls of +//│ Tuple2: +//│ _1 = Cls: +//│ cls = class:Foo +//│ path = Select{class:Foo}: +//│ qual = Ref of member:Foo +//│ name = Ident of "class" +//│ _2 = Assign: //│ lhs = $argument0$ //│ rhs = Select{class:Foo.x}: //│ qual = Ref of caseScrut //│ name = Ident of "x" -//│ rest = Scoped: \ -//│ syms = Set(a) -//│ body = Assign: -//│ lhs = a -//│ rhs = Ref of $argument0$ -//│ rest = Return: \ -//│ res = Ref of a -//│ implct = false -//│ dflt = S of Throw of Instantiate: -//│ mut = false -//│ cls = Select: -//│ qual = Ref of globalThis:globalThis -//│ name = Ident of "Error" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "match error" -//│ rest = End of "" +//│ rest = Assign: \ +//│ lhs = a +//│ rhs = Ref of $argument0$ +//│ rest = Return: \ +//│ res = Ref of a +//│ implct = false +//│ dflt = S of Throw of Instantiate: +//│ mut = false +//│ cls = Select: +//│ qual = Ref of globalThis:globalThis +//│ name = Ident of "Error" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "match error" +//│ rest = End of "" //│ rest = Return: \ //│ res = Ref of member:lambda //│ implct = true diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index 732f21c79e..85ea77ef62 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -15,8 +15,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let b, tmp; -//│ let a; +//│ let a, b, tmp; //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -24,8 +23,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let b1, tmp1; -//│ let a1; +//│ let a1, b1, tmp1; //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -34,16 +32,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let tmp2; -//│ let aaaa; +//│ let aaaa, tmp2; //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let bbbb, tmp3; -//│ let aaaaaaaaa; +//│ let aaaaaaaaa, bbbb, tmp3; //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -54,8 +50,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let bbbbbbbb, ccccccccc, dddddddddd, tmp4; -//│ let aaaaaaaaa1; +//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -91,15 +86,14 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7; -//│ let tmp8; -//│ tmp8 = globalThis.Object.freeze([ +//│ let tmp7, tmp8; +//│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", //│ "ccccccccccccccccc", //│ "dddddddddddddddddd" //│ ]); -//│ tmp7 = globalThis.Object.freeze([ tmp8 ]); -//│ runtime.safeCall(discard(tmp7)) +//│ tmp8 = globalThis.Object.freeze([ tmp7 ]); +//│ runtime.safeCall(discard(tmp8)) diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 6fef9160a3..5faa54a309 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -81,7 +81,8 @@ fun test(a) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1; +//│ let C11, C21; +//│ let tmp, tmp1; //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -154,7 +155,8 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz, tmp; +//│ let bar, baz; +//│ let tmp; //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 46d2b669a4..128459ca14 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -39,7 +39,8 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let whoops, tmp1, tmp2; +//│ let whoops; +//│ let tmp1, tmp2; //│ whoops = function whoops() { //│ let tmp3; //│ tmp3 = Predef.print("ok"); @@ -137,7 +138,8 @@ fun test() = //│ JS (unsanitized): //│ let test1; //│ test1 = function test() { -//│ let whoops, tmp1; +//│ let whoops; +//│ let tmp1; //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -182,13 +184,12 @@ fun baz() = //│ return 2 //│ }; //│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3; -//│ let tmp4; -//│ tmp4 = x + y; -//│ tmp1 = w(); -//│ tmp2 = tmp4 + tmp1; -//│ tmp3 = z(); -//│ return tmp2 + tmp3 +//│ let tmp1, tmp2, tmp3, tmp4; +//│ tmp1 = x + y; +//│ tmp2 = w(); +//│ tmp3 = tmp1 + tmp2; +//│ tmp4 = z(); +//│ return tmp3 + tmp4 //│ }); //│ return lambda //│ }; @@ -213,7 +214,8 @@ fun a() = //│ return 1 //│ }; //│ c = function c() { -//│ let d, tmp2, tmp3; +//│ let d; +//│ let tmp2, tmp3; //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -242,7 +244,8 @@ fun b() = //│ return 1 //│ }; //│ d = function d() { -//│ let c1, tmp3; +//│ let c1; +//│ let tmp3; //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -265,12 +268,14 @@ fun c() = //│ JS (unsanitized): //│ let c; //│ c = function c() { -//│ let d, f, tmp4; +//│ let d, f; +//│ let tmp4; //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e, tmp5, tmp6; +//│ let e; +//│ let tmp5, tmp6; //│ e = function e() { //│ return 1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index a35ce88357..d46187c0ac 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -60,7 +60,7 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let f, x, f1; let x1; x1 = 1; f1 = function f() { return x1 }; f = f1; x = 2; runtime.safeCall(f()) +//│ let x, f, x1, f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) //│ = 1 //│ f = fun f //│ x = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index 8ff1b3e343..37ffed6216 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -5,13 +5,12 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2; -//│ let tmp3; -//│ tmp3 = x + 1; -//│ tmp = tmp3 + 1; +//│ let tmp, tmp1, tmp2, tmp3; +//│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; -//│ return tmp2 + 1 +//│ tmp3 = tmp2 + 1; +//│ return tmp3 + 1 //│ }); //│ lambda(1) //│ = 6 @@ -21,14 +20,13 @@ //│ JS (unsanitized): //│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; -//│ let tmp4; -//│ tmp4 = x + 1; -//│ tmp = tmp4 + 1; +//│ let tmp, tmp1, tmp2, tmp3, tmp4; +//│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; //│ tmp3 = tmp2 + 1; -//│ return tmp3 + 1 +//│ tmp4 = tmp3 + 1; +//│ return tmp4 + 1 //│ }); //│ lambda1(1) //│ = 7 diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 8a89d8bd07..bc312d91f9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -107,32 +107,31 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3; -//│ let tmp4; -//│ tmp4 = 3; +//│ let x, tmp1, tmp2, tmp3, tmp4; +//│ tmp1 = 3; //│ if (a instanceof A1.class) { -//│ tmp1 = 1; +//│ tmp2 = 1; //│ } else if (a instanceof B1.class) { -//│ tmp1 = 2; +//│ tmp2 = 2; //│ } else if (a instanceof C1.class) { -//│ tmp1 = 3; +//│ tmp2 = 3; //│ } else if (a instanceof D1.class) { //│ if (a instanceof A1.class) { -//│ tmp2 = 1 + tmp4; +//│ tmp3 = 1 + tmp1; //│ } else if (a instanceof B1.class) { -//│ tmp2 = 2 + tmp4; +//│ tmp3 = 2 + tmp1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp3 = tmp2; -//│ tmp1 = Predef.print("done"); +//│ tmp4 = tmp3; +//│ tmp2 = Predef.print("done"); //│ } else if (a instanceof E1.class) { -//│ tmp3 = 5; -//│ tmp1 = Predef.print("done"); +//│ tmp4 = 5; +//│ tmp2 = Predef.print("done"); //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ x = tmp1; +//│ x = tmp2; //│ Predef.print(x) //│ > 1 //│ x = 1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index b4de5e4d59..29994014fb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1; let tmp2; tmp2 = Predef.print(1); tmp1 = Predef.print(2); Foo5(tmp2, tmp1) +//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo5(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index b02697cac6..ec8c349b28 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -254,7 +254,7 @@ let f = if _ then 1 else 0 //│ ║ l.249: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15; let tmp21; tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 2d626431ad..006ecea04e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -74,7 +74,8 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let Foo5, tmp; +//│ let Foo5; +//│ let tmp; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -98,8 +99,8 @@ let t = test() :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.99: new t -//│ ║ ^ +//│ ║ l.100: new t +//│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): //│ globalThis.Object.freeze(new t()) @@ -115,7 +116,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.116: new t() +//│ ║ l.117: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -217,8 +218,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let tmp, tmp1, tmp2, tmp3; -//│ let a; +//│ let a, tmp, tmp1, tmp2, tmp3; //│ a = globalThis.Object.freeze(new Foo10()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 695963c4fe..4defe108c8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,18 +6,17 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, lambda; -//│ let tmp3; -//│ tmp3 = runtime.safeCall(globalThis.process.env.PWD.split("/")); -//│ folderName1 = runtime.safeCall(tmp3.pop()); -//│ tmp = runtime.safeCall(globalThis.process.cwd()); -//│ tmp1 = runtime.safeCall(tmp.split("/")); -//│ folderName2 = runtime.safeCall(tmp1.pop()); -//│ tmp2 = folderName2 === folderName1; +//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; +//│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); +//│ folderName1 = runtime.safeCall(tmp.pop()); +//│ tmp1 = runtime.safeCall(globalThis.process.cwd()); +//│ tmp2 = runtime.safeCall(tmp1.split("/")); +//│ folderName2 = runtime.safeCall(tmp2.pop()); +//│ tmp3 = folderName2 === folderName1; //│ lambda = (undefined, function () { //│ return folderName2 === "shared" //│ }); -//│ runtime.short_or(tmp2, lambda) +//│ runtime.short_or(tmp3, lambda) //│ = true diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index 71fdb53d0c..3e823a55c8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,16 +76,15 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let x1, tmp6, tmp7, arr2; -//│ let tmp8; -//│ tmp8 = globalThis.Object.freeze(new Term.Symbol("x")); -//│ x1 = globalThis.Object.freeze(new Term.Ref(tmp8)); -//│ tmp6 = Predef.print(x1); +//│ let x1, tmp6, tmp7, tmp8, arr2; +//│ tmp6 = globalThis.Object.freeze(new Term.Symbol("x")); +//│ x1 = globalThis.Object.freeze(new Term.Ref(tmp6)); +//│ tmp7 = Predef.print(x1); //│ arr2 = globalThis.Object.freeze([ -//│ tmp8 +//│ tmp6 //│ ]); -//│ tmp7 = x1; -//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp7)) +//│ tmp8 = x1; +//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp8)) //│ > Ref(Symbol("x")) //│ = Lam([Symbol("x")], Ref(Symbol("x"))) @@ -119,21 +118,20 @@ f`(`0) :sjs `if `true then `true else `false //│ JS (unsanitized): -//│ let scrut, tmp29, tmp30, tmp31, tmp32, tmp33, tmp34, tmp35, tmp36, tmp37, tmp38; -//│ let tmp39; -//│ tmp39 = globalThis.Object.freeze(new Term.Symbol("scrut")); -//│ scrut = globalThis.Object.freeze(new Term.Ref(tmp39)); -//│ tmp29 = globalThis.Object.freeze(new Term.Lit(true)); -//│ tmp32 = scrut; -//│ tmp33 = globalThis.Object.freeze(new Term.LitPattern(true)); -//│ tmp38 = globalThis.Object.freeze(new Term.Lit(true)); -//│ tmp34 = globalThis.Object.freeze(new Term.Else(tmp38)); -//│ tmp35 = globalThis.Object.freeze(new Term.Branch(tmp32, tmp33, tmp34)); -//│ tmp37 = globalThis.Object.freeze(new Term.Lit(false)); -//│ tmp36 = globalThis.Object.freeze(new Term.Else(tmp37)); -//│ tmp30 = globalThis.Object.freeze(new Term.Cons(tmp35, tmp36)); -//│ tmp31 = globalThis.Object.freeze(new Term.Let(tmp39, tmp29, tmp30)); -//│ globalThis.Object.freeze(new Term.IfLike(Term.Keyword.If, tmp31)) +//│ let scrut, tmp29, tmp30, tmp31, tmp32, tmp33, tmp34, tmp35, tmp36, tmp37, tmp38, tmp39; +//│ tmp29 = globalThis.Object.freeze(new Term.Symbol("scrut")); +//│ scrut = globalThis.Object.freeze(new Term.Ref(tmp29)); +//│ tmp30 = globalThis.Object.freeze(new Term.Lit(true)); +//│ tmp33 = scrut; +//│ tmp34 = globalThis.Object.freeze(new Term.LitPattern(true)); +//│ tmp39 = globalThis.Object.freeze(new Term.Lit(true)); +//│ tmp35 = globalThis.Object.freeze(new Term.Else(tmp39)); +//│ tmp36 = globalThis.Object.freeze(new Term.Branch(tmp33, tmp34, tmp35)); +//│ tmp38 = globalThis.Object.freeze(new Term.Lit(false)); +//│ tmp37 = globalThis.Object.freeze(new Term.Else(tmp38)); +//│ tmp31 = globalThis.Object.freeze(new Term.Cons(tmp36, tmp37)); +//│ tmp32 = globalThis.Object.freeze(new Term.Let(tmp29, tmp30, tmp31)); +//│ globalThis.Object.freeze(new Term.IfLike(Term.Keyword.If, tmp32)) //│ = IfLike( //│ If, //│ Let( diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index d71aee4d02..10940ea078 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -63,7 +63,7 @@ do fun f = f () //│ JS (unsanitized): -//│ let f; let f1; f1 = 1; f = function f() { let tmp; tmp = f(); return tmp }; runtime.Unit +//│ let f, f1; f1 = 1; f = function f() { let tmp; tmp = f(); return tmp }; runtime.Unit //│ f = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 218aa8a2cf..2d9dca8bfd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -11,7 +11,8 @@ fun foo() = //│ JS (unsanitized): //│ let foo; //│ foo = function foo() { -//│ let bar, x; +//│ let bar; +//│ let x; //│ bar = function bar() { let y; y = 2; return x + y }; //│ x = 1; //│ return bar() @@ -36,7 +37,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x1, y; let tmp; tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; +//│ let x1, y, tmp; tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; //│ REPL> Sending: block$res4 = undefined //│ REPL> Collected: //│ > undefined @@ -101,7 +102,8 @@ fun g(x, y, z) = //│ JS (unsanitized): //│ let g; //│ g = function g(x2, y1, z) { -//│ let a, tmp1, tmp2, lambda; +//│ let a, lambda; +//│ let tmp1, tmp2; //│ split_root$: { //│ split_1$: { //│ if (x2 === true) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 489cd08f4d..b1c2122e16 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -30,8 +30,7 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let tmp1, tmp2, tmp3; -//│ let old; +//│ let old, tmp1, tmp2, tmp3; //│ old = x1; //│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } //│ tmp1 @@ -72,19 +71,19 @@ example() //│ JS (unsanitized): //│ let example2; //│ example2 = function example() { -//│ let get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, get_x1; -//│ let x2; +//│ let get_x; +//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ x2 = 0; -//│ get_x1 = function get_x() { +//│ get_x = function get_x() { //│ return x2 //│ }; -//│ get_x = get_x1; +//│ get_x1 = get_x; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; //│ x2 = tmp5; //│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x()); +//│ tmp7 = runtime.safeCall(get_x1()); //│ tmp8 = Predef.print(tmp7); //│ tmp9 = (tmp6 , tmp8); //│ tmp4 = tmp9; @@ -92,7 +91,7 @@ example() //│ x2 = old1; //│ } //│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x()); +//│ tmp11 = runtime.safeCall(get_x1()); //│ return Predef.print(tmp11) //│ }; //│ example2() @@ -114,13 +113,13 @@ example() //│ JS (unsanitized): //│ let example3; //│ example3 = function example() { -//│ let get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, get_x1; -//│ let x2; +//│ let get_x; +//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; //│ x2 = 0; -//│ get_x1 = function get_x() { +//│ get_x = function get_x() { //│ return x2 //│ }; -//│ get_x = get_x1; +//│ get_x1 = get_x; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; @@ -133,7 +132,7 @@ example() //│ } //│ y = tmp4; //│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x()); +//│ tmp9 = runtime.safeCall(get_x1()); //│ tmp10 = Predef.print(tmp9); //│ return y //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 4ef0cd898a..60ab9f2c5c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { throw globalThis.Error("e") }; f2(1) +//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index ba70b84e49..1b3593632c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -9,8 +9,7 @@ fun fib(a) = if //│ JS (unsanitized): //│ let fib; //│ fib = function fib(a) { -//│ let tmp, tmp1, tmp2, tmp3; -//│ let scrut; +//│ let scrut, tmp, tmp1, tmp2, tmp3; //│ scrut = a <= 1; //│ if (scrut === true) { //│ return a diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 9ec87b064d..6dd3fd47b9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -105,7 +105,8 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let i2, scrut3, tmp19, tmp20; +//│ let i2; +//│ let scrut3, tmp19, tmp20; //│ tmp21: while (true) { //│ i2 = 0; //│ scrut3 = i2 < 10; @@ -197,7 +198,8 @@ fun f(ls) = //│ JS (unsanitized): //│ let f; //│ f = function f(ls) { -//│ let tl, h, argument0$, argument1$, tmp28; +//│ let tl, h; +//│ let argument0$, argument1$, tmp28; //│ tmp29: while (true) { //│ if (ls instanceof Cons1.class) { //│ argument0$ = ls.hd; @@ -274,10 +276,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.274: then 0(0) +//│ ║ l.276: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.273: while print("Hello World"); false +//│ ║ l.275: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -287,12 +289,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.286: while { print("Hello World"), false } +//│ ║ l.288: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.287: then 0(0) +//│ ║ l.289: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.288: else 1 +//│ ║ l.290: else 1 //│ ╙── ^^^^ :fixme @@ -302,14 +304,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.300: print("Hello World") +//│ ║ l.302: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.301: false +//│ ║ l.303: false //│ ║ ^^^^^^^^^ -//│ ║ l.302: then 0(0) +//│ ║ l.304: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.303: else 1 +//│ ║ l.305: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index adeb680886..b7e9ea0e36 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -77,12 +77,11 @@ let f3 = baz //│ f31 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1; -//│ let tmp2; -//│ tmp2 = baz(x); -//│ tmp = runtime.safeCall(tmp2(instance$Ident$_Int$_)); -//│ tmp1 = runtime.safeCall(tmp(y)); -//│ return runtime.safeCall(tmp1(instance$Ident$_Num$_)) +//│ let tmp, tmp1, tmp2; +//│ tmp = baz(x); +//│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); +//│ tmp2 = runtime.safeCall(tmp1(y)); +//│ return runtime.safeCall(tmp2(instance$Ident$_Num$_)) //│ }); //│ return lambda1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index 2fbfee161d..de274fb30f 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -19,8 +19,7 @@ fun f() = //│ JS (unsanitized): //│ let f, getLocals2; //│ getLocals2 = function getLocals() { -//│ let thisInfo, arr, tmp; -//│ let prev; +//│ let prev, thisInfo, arr, tmp; //│ prev = []; //│ arr = globalThis.Object.freeze([]); //│ thisInfo = new runtime.FnLocalsInfo.class("\u2039top level\u203A", arr); @@ -30,8 +29,7 @@ fun f() = //│ f = function f() { //│ let i, j, k, scrut, tmp, tmp1, getLocals3, Cont$func$f$1, doUnwind; //│ getLocals3 = function getLocals() { -//│ let i1, j1, k1, thisInfo, arr, tmp2; -//│ let prev; +//│ let i1, j1, k1, prev, thisInfo, arr, tmp2; //│ prev = getLocals2(); //│ i1 = new runtime.LocalVarInfo.class("i", i); //│ j1 = new runtime.LocalVarInfo.class("j", j); @@ -128,8 +126,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:128:3) -//│ at lambda_test (Debugging.mls:125:3) +//│ at lambda (Debugging.mls:126:3) +//│ at lambda_test (Debugging.mls:123:3) import "../../mlscript-compile/Runtime.mls" @@ -208,9 +206,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:201:3) with locals: j=200 +//│ > at f (Debugging.mls:199:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:203:3) +//│ > at f (Debugging.mls:201:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:204:3) with locals: j=300 +//│ > at f (Debugging.mls:202:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 4b37c2c874..74770fcc40 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,8 +155,7 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let scrut, tmp9, tmp10, handleBlock$7; -//│ let str; +//│ let str, scrut, tmp9, tmp10, handleBlock$7; //│ str = ""; //│ scrut = true; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index d373b0410e..722499592a 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,8 +8,7 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let old, tmp, tmp1, tmp2; -//│ let x; +//│ let x, old, tmp, tmp1, tmp2; //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index dcbf81e66e..df45c1fece 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -185,9 +185,9 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let x, y, z, tmp6, tmp7; -//│ let capture; +//│ let x, y, z, tmp6, tmp7, capture; //│ capture = new f$capture3(null); +//│ let w; //│ x = 1; //│ y = 10; //│ z = 10; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index 14f4e9dbd8..a249f407b4 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -59,8 +59,8 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let unused2, foo, tmp, g$here; -//│ let used2; +//│ let g$here; +//│ let used2, unused2, foo, tmp; //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); @@ -80,13 +80,12 @@ f(1,2,3,4,5,6) //│ JS (unsanitized): //│ let f4, g$4; //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2; -//│ let tmp3; -//│ tmp3 = a1 + a2; -//│ tmp = tmp3 + a3; -//│ tmp1 = tmp + a4; -//│ tmp2 = tmp1 + a5; -//│ return tmp2 + a6 +//│ let tmp, tmp1, tmp2, tmp3; +//│ tmp = a1 + a2; +//│ tmp1 = tmp + a3; +//│ tmp2 = tmp1 + a4; +//│ tmp3 = tmp2 + a5; +//│ return tmp3 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { //│ let tmp; @@ -183,8 +182,7 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let a1, tmp7, tmp8; -//│ let capture; +//│ let a1, tmp7, tmp8, capture; //│ capture = new f$capture5(mutated); //│ a1 = g$6(immutable, capture); //│ tmp7 = h$2(capture); @@ -282,9 +280,9 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { -//│ let f$here; -//│ let capture; +//│ let capture, f$here; //│ capture = new g$capture1(null); +//│ let y1; //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here @@ -384,8 +382,8 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let scrut, g$here; -//│ let y1; +//│ let g$here; +//│ let y1, scrut; //│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { @@ -429,8 +427,7 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let a1, tmp10, g$here; -//│ let capture; +//│ let a1, tmp10, capture, g$here; //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); //│ a1 = g$here; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index f64cea9959..4c400e4dc6 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -53,8 +53,8 @@ fun foo() = //│ } //│ }); //│ foo2 = function foo() { -//│ let scrut, tmp2, tmp3, lambda$here; -//│ let x; +//│ let lambda$here; +//│ let x, scrut, tmp2, tmp3; //│ x = 1; //│ tmp4: while (true) { //│ scrut = true; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index 4b807e12f3..b27cccd164 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -145,8 +145,7 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let tmp, M$1; -//│ let capture; +//│ let tmp, M$1, capture; //│ capture = new foo$capture3(y); //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); //│ tmp = foo3$(M$1, x, capture); diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 0dcc90b512..bfe2893f41 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -32,9 +32,9 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let tmp, bar$here; -//│ let capture; +//│ let tmp, capture, bar$here; //│ capture = new foo$capture1(null); +//│ let x; //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); //│ tmp = runtime.safeCall(xs.push(bar$here)); diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 06ec481e9c..8b772c4748 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -148,8 +148,7 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1, stackDelayRes; -//│ let curDepth; +//│ let scrut, tmp, tmp1, curDepth, stackDelayRes; //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index bb16dac916..809b3151c6 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -42,7 +42,8 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let tmp, tmp1, idx1, idx2, idx3, idx4, idx5; +//│ let idx1, idx2, idx3, idx4, idx5; +//│ let tmp, tmp1; //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; //│ idx5 = idx + 0; diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 3584ee8a24..01db6e89df 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -135,8 +135,7 @@ fun popByIndex(start, end, acc, lft) = //│ JS (unsanitized): //│ let popByIndex; //│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let tmp34, tmp35, tmp36; -//│ let scrut; +//│ let scrut, tmp34, tmp35, tmp36; //│ scrut = start >= end; //│ if (scrut === true) { //│ return acc diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 98981c7a54..8f8affbdbb 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -16,18 +16,13 @@ if x === 0 then 1 else 2 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, tmp; -//│ let scrut1; -//│ scrut1 = x === 0; -//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } -//│ a = tmp; +//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let tmp1; -//│ let scrut2; +//│ let scrut2, tmp1; //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -341,7 +336,8 @@ fun foo(x, y, z) = if x is //│ JS (unsanitized): //│ let foo2; //│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; +//│ let value; +//│ let tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { @@ -469,8 +465,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, tmp6; -//│ let scrut3; +//│ let y1, scrut3, tmp6; //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 33339b6e3a..9533eb3dde 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -8,7 +8,8 @@ fun nonsense(xs) = if xs is //│ JS (unsanitized): //│ let nonsense; //│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; +//│ let ys; +//│ let middleElements; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -29,7 +30,8 @@ fun lead_and_last(xs) = if xs is //│ JS (unsanitized): //│ let lead_and_last; //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; +//│ let x, ys, y; +//│ let lastElement0$, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -63,7 +65,8 @@ fun nested_tuple_patterns(xs) = if xs is //│ JS (unsanitized): //│ let nested_tuple_patterns; //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; +//│ let x, y, w, z; +//│ let lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index a241eecb60..f385992ea7 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -35,8 +35,7 @@ fun foo(x) = x is Cross //│ JS (unsanitized): //│ let foo; //│ foo = function foo(x2) { -//│ let output, bindings; -//│ let unapplyResult; +//│ let unapplyResult, output, bindings; //│ unapplyResult = runtime.safeCall(Cross1.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index f3d5987365..ca5f1fb528 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -108,22 +108,34 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: with JSBuilderArgNumSanityChecks val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) + val le = lowered0.copy(main = lowered0.main.mapTail: + case e: End => + Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + case Return(res, implct) => + assert(implct) + Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case Scoped(_, body) => ??? // TODO + case tl: (Throw | Break | Continue) => tl + ) - def assignResSym(b: Block, toplvl: Boolean): Block = - b.mapTail: - case e: End => - Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - case Return(res, implct) => - assert(implct) - Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case s@Scoped(xs, body) => - if toplvl then - // toplvlDefinedVars = xs - assignResSym(body, false) - else - Scoped(xs, assignResSym(body, false)) - case tl: (Throw | Break | Continue) => tl - val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + // TODO: re-implement + // TODO: remove mutation + // var toplvlDefinedVars = Set.empty[Symbol] + // def assignResSym(b: Block, toplvl: Boolean): Block = + // b.mapTail: + // case e: End => + // Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + // case Return(res, implct) => + // assert(implct) + // Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + // case s@Scoped(xs, body) => + // if toplvl then + // toplvlDefinedVars = xs + // assignResSym(body, false) + // else + // Scoped(xs, assignResSym(body, false)) + // case tl: (Throw | Break | Continue) => tl + // val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then output(s"Lowered:") diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala index a85280301c..dfe05c24e0 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/LlirDiffMaker.scala @@ -19,6 +19,7 @@ import hkmc2.semantics.Elaborator import scala.collection.mutable.ListBuffer abstract class LlirDiffMaker extends BbmlDiffMaker: + val llir = NullaryCommand("llir") val sllir = NullaryCommand("sllir") val intl = NullaryCommand("intl") val lprelude = NullaryCommand("lpre") diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala index f08ea6abbb..e748836b81 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala @@ -53,8 +53,6 @@ abstract class MLsDiffMaker extends DiffMaker: val ppLoweredTree = NullaryCommand("slot") val showContext = NullaryCommand("ctx") val parseOnly = NullaryCommand("parseOnly") - - val llir = NullaryCommand("llir") val typeCheck = FlagCommand(false, "typeCheck") @@ -99,7 +97,6 @@ abstract class MLsDiffMaker extends DiffMaker: )), liftDefns = Opt.when(liftDefns.isSet)(LiftDefns()), stageCode = stageCode.isSet, - llir = llir.isSet, target = if wasm.isSet then CompilationTarget.Wasm else CompilationTarget.JS, ) From 07eff8fb3106f5dcc102839fbd39f9e1e9ca126d Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Wed, 19 Nov 2025 02:29:18 +0800 Subject: [PATCH 08/72] Revert "WIP: Try" This reverts commit 5f9e63c768d98191c509d81e4d7da2fb4c5dfe41. --- .../src/main/scala/hkmc2/codegen/Block.scala | 27 +------- .../main/scala/hkmc2/codegen/Lowering.scala | 6 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 19 +++--- .../src/main/scala/hkmc2/semantics/Term.scala | 19 +++++- .../src/test/mlscript-compile/Predef.mjs | 6 +- .../src/test/mlscript-compile/Runtime.mjs | 27 +++----- .../src/test/mlscript/basics/LazySpreads.mls | 10 ++- .../src/test/mlscript/bbml/bbCodeGen.mls | 6 +- .../src/test/mlscript/bbml/bbGetters.mls | 8 +-- .../src/test/mlscript/codegen/BasicTerms.mls | 12 +++- .../src/test/mlscript/codegen/CaseOfCase.mls | 3 +- .../src/test/mlscript/codegen/ClassInFun.mls | 5 +- .../test/mlscript/codegen/ClassMatching.mls | 9 +-- .../test/mlscript/codegen/FieldSymbols.mls | 67 ++++++++++--------- .../src/test/mlscript/codegen/FunInClass.mls | 6 +- .../src/test/mlscript/codegen/Getters.mls | 18 ++--- .../test/mlscript/codegen/PlainClasses.mls | 19 +++--- .../src/test/mlscript/codegen/Scoped.mls | 66 ++++++++---------- .../src/test/mlscript/codegen/SetIn.mls | 20 +++--- .../src/test/mlscript/codegen/Throw.mls | 2 +- .../src/test/mlscript/codegen/While.mls | 24 +++---- .../src/test/mlscript/lifter/ClassInFun.mls | 1 - .../src/test/mlscript/lifter/FunInFun.mls | 7 +- .../shared/src/test/mlscript/lifter/Loops.mls | 3 +- .../src/test/mlscript/lifter/Mutation.mls | 1 - .../src/test/mlscript/objbuf/Mutation.mls | 3 +- .../ucs/normalization/Deduplication.mls | 3 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 9 +-- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 61 +++++++---------- 29 files changed, 200 insertions(+), 267 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 9d63e962ad..36b717b9fd 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,27 +49,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - // cannot use `body.definedVars - syms`. lifter computes captures based on it? - case Scoped(syms, body) => body.definedVars - - // TODO: remove it - lazy val tempVars: Set[Local] = this match - case _: Return | _: Throw => Set.empty - case Begin(sub, rst) => sub.tempVars ++ rst.tempVars - case Assign(l: TempSymbol, r, rst) => rst.tempVars + l - case Assign(l, r, rst) => rst.tempVars - case AssignField(l, n, r, rst) => rst.tempVars - case AssignDynField(l, n, ai, r, rst) => rst.tempVars - case Match(scrut, arms, dflt, rst) => - arms.flatMap(_._2.tempVars).toSet ++ dflt.toList.flatMap(_.tempVars) ++ rst.tempVars - case End(_) => Set.empty - case Break(_) => Set.empty - case Continue(_) => Set.empty - case Define(defn, rst) => rst.tempVars - case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.tempVars - case TryBlock(sub, fin, rst) => sub.tempVars ++ fin.tempVars ++ rst.tempVars - case Label(lbl, _, bod, rst) => bod.tempVars ++ rst.tempVars - case Scoped(syms, body) => body.tempVars + case Scoped(syms, body) => body.definedVars -- syms lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -87,7 +67,6 @@ sealed abstract class Block extends Product: // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match - case Scoped(syms, body) => Scoped(syms, body.mapTail(f)) case b: BlockTail => f(b) case Begin(sub, rst) => Begin(sub, rst.mapTail(f)) case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f)) @@ -124,7 +103,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body) => body.freeVars + case Scoped(syms, body) => body.freeVars -- syms case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -145,7 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body) => body.freeVarsLLIR + case Scoped(syms, body) => body.freeVarsLLIR -- syms case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index cd343d9e77..b8f9e33ddf 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -123,7 +123,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - blockImpl(imps ::: funs ::: rest, res)(k) + val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) + Scoped(definedVars.toSet, blockImpl(imps ::: funs ::: rest, res)(k)) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = stats match @@ -959,8 +960,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - val body = returnedTerm(bodyTerm) - (paramLists, Scoped(bodyTerm.definedSyms ++ body.tempVars, body)) // TODO: move it to block function + (paramLists, returnedTerm(bodyTerm)) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 34393f141d..255182698a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -503,8 +503,13 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - // TODO: we should remove `syms.filter` after temp vars can be correctly handled. - val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) + val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => + if scope.lookup(l).isDefined then + raise: + WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) + None + else + Some(l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme @@ -580,17 +585,11 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case N => doc"" ) - // TODO: get rid of it? - private def getDefinedVars(blk: Block): Set[Local] = blk match - case Scoped(syms, body) => body.definedVars -- syms - case _ => blk.definedVars - - def worksheet(p: Program)(using Raise, Scope): (Document, Document) = reserveNames(p) lazy val imps = p.imports.map: i => doc"""${getVar(i._1, N)} = await import("${i._2.toString}").then(m => m.default ?? m);""" - blockPreamble(p.imports.map(_._1).toSeq ++ getDefinedVars(p.main).toSeq) -> + blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVars.toSeq) -> (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) def blockPreamble(ss: Iterable[Symbol])(using Raise, Scope): Document = @@ -605,7 +604,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = // println(s"$t :::::::: ${t.definedVars}") - blockPreamble(getDefinedVars(t)) :: returningTerm(t, endSemi) + blockPreamble(t.definedVars) :: returningTerm(t, endSemi) def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn: block(t, endSemi) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 34596bd89a..da9a546848 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -368,7 +368,24 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) case LetDecl(sym, annotations) => Set(sym) - case _ => Set.empty // TODO + // TODO: make sure + case tdef: TermDefinition => Set(tdef.sym) + case ModuleOrObjectDef(owner, sym, bsym, tparams, paramsOpt, auxParams, ext, kind, body, companion, annotations) => Set(sym) + case PatternDef(owner, sym, bsym, tparams, parameters, patternParams, extractionParams, pattern, annotations) => Set(bsym) + case ClassDef.Parameterized(owner, kind, sym, bsym, tparams, params, auxParams, ext, body, companion, annotations) => Set(bsym) + case ClassDef.Plain(owner, kind, sym, bsym, tparams, ext, body, companion, annotations) => Set(bsym) + case TypeDef(sym, bsym, tparams, rhs, companion, annotations) => Set(bsym) + case Import(sym, str, file) => Set(sym) + + // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. + // And including the sym here may cause error for delayed init in a function: + // ``` + // let x + // fun f() = + // x = 2 + // ``` + // case DefineVar(sym, rhs) => Set(sym) + case _ => Set.empty // TODO: add other cases def describe: Str = val desc = this match diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index b9b7d0cb68..a4ce900a78 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -124,8 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let i, init; - let len, scrut, scrut1, tmp, tmp1, tmp2, tmp3; + let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; len = rest.length; scrut = len == 0; if (scrut === true) { @@ -153,8 +152,7 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda; - let tmp; + let lambda, tmp; lambda = (undefined, function (acc, x) { let tmp1, tmp2, tmp3; if (typeof x === 'string') { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 4f14cc8cc5..e5b5a48eeb 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -197,8 +197,7 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let prev; - let scrut, tmp; + let scrut, prev, tmp; scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -416,8 +415,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let name, lambda; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; tmp = got < expected; lambda = (undefined, function () { let lambda1; @@ -528,8 +526,7 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let cur, locals, curLocals, loc, loc1, localsMsg, lambda; - let msg, curHandler, atTail, scrut, scrut1, scrut2, scrut3, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; + let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; msg = header; curHandler = tr.contTrace; atTail = true; @@ -625,8 +622,7 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, lambda; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; if (cont instanceof Runtime.FunctionContFrame.class) { tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; @@ -672,8 +668,7 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; if (cont instanceof Runtime.HandlerContFrame.class) { result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { @@ -732,8 +727,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let vis, hl, cur; - let scrut, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; + let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; if (contTrace instanceof Runtime.ContTrace.class) { tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; @@ -820,8 +814,7 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let nxt; - let scrut, tmp, tmp1; + let nxt, scrut, tmp, tmp1; tmp2: while (true) { if (cur instanceof Runtime.EffectSig.class) { nxt = Runtime.handleEffect(cur); @@ -965,8 +958,7 @@ globalThis.Object.freeze(class Runtime { return tmp4 } static checkDepth() { - let lambda; - let scrut, tmp; + let scrut, tmp, lambda; tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -979,8 +971,7 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let saved; - let result, scrut, tmp, tmp1; + let result, scrut, saved, tmp, tmp1; Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index a90ca7695e..26fb4b1450 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -21,8 +21,7 @@ fun buildPalindrome = case //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp3, tmp4, tmp5; +//│ let n, tmp3, tmp4, tmp5; //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -53,8 +52,7 @@ fun sum2 = case //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y; -//│ let lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { @@ -82,13 +80,13 @@ sum2(arr) :e fun f(..xs) = xs //│ ╔══[ERROR] Lazy spread parameters not allowed. -//│ ║ l.83: fun f(..xs) = xs +//│ ║ l.81: fun f(..xs) = xs //│ ╙── ^^^^ :ge sum2(..arr) //│ ╔══[COMPILATION ERROR] Lazy spreads are not supported in call arguments -//│ ║ l.89: sum2(..arr) +//│ ║ l.87: sum2(..arr) //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function expected 1 argument but got 2 diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index eed563d668..19e96e6493 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -151,8 +151,7 @@ fun pow(x) = case //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp, tmp1, tmp2; +//│ let n, tmp, tmp1, tmp2; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -208,8 +207,7 @@ fun fact = case //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp1, tmp2, tmp3; +//│ let n, tmp1, tmp2, tmp3; //│ if (caseScrut === 0) { //│ return 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 2f5baa282d..d72caafeeb 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -89,8 +89,7 @@ fun test2() = //│ JS (unsanitized): //│ let test22; //│ test22 = function test2() { -//│ let funny; -//│ let tmp1; +//│ let funny, tmp1; //│ funny = function funny() { //│ let lambda, lambda1; //│ lambda = (undefined, function (caseScrut) { @@ -101,8 +100,7 @@ fun test2() = //│ } //│ }); //│ lambda1 = (undefined, function (caseScrut) { -//│ let n; -//│ let tmp2, tmp3, tmp4; +//│ let n, tmp2, tmp3, tmp4; //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; @@ -132,7 +130,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.133: print("Hi") +//│ ║ l.131: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index 668826abb5..ad80485e1d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -24,8 +24,11 @@ //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Return: -//│ res = Lit of IntLit of 2 +//│ main = Assign: +//│ lhs = $block$res +//│ rhs = Lit of IntLit of 2 +//│ rest = Return: \ +//│ res = Lit of UnitLit of false //│ implct = true //│ = 2 @@ -53,8 +56,11 @@ print("Hi") //│ Arg: //│ spread = N //│ value = Lit of StrLit of "Hi" +//│ rest = Assign: \ +//│ lhs = $block$res +//│ rhs = Lit of IntLit of 2 //│ rest = Return: \ -//│ res = Lit of IntLit of 2 +//│ res = Lit of UnitLit of false //│ implct = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 30f7fee561..22009ea6bb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -17,8 +17,7 @@ fun test(x) = //│ JS (unsanitized): //│ let test; //│ test = function test(x) { -//│ let v, v1; -//│ let scrut, argument0$, argument0$1, tmp, tmp1; +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; //│ if (x instanceof Some1.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index 7e7c2c81f6..b5c1b26b34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -39,8 +39,7 @@ fun test(x) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(x) { -//│ let Foo2; -//│ let tmp; +//│ let Foo2, tmp; //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -69,7 +68,7 @@ test(123) :re test() //│ ╔══[ERROR] Expected 1 arguments, got 0 -//│ ║ l.70: test() +//│ ║ l.69: test() //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Function 'test' expected 1 argument but got 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 19eb06aa44..55c80c83c3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -55,8 +55,7 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4; -//│ let argument0$4; +//│ let x4, argument0$4; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -121,8 +120,7 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f4; //│ f4 = function f(x3) { -//│ let x4; -//│ let scrut1, argument0$4, tmp5; +//│ let x4, scrut1, argument0$4, tmp5; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some1.class) { @@ -168,8 +166,7 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f5; //│ f5 = function f(x3) { -//│ let u, a, b; -//│ let argument0$4, argument1$; +//│ let u, a, b, argument0$4, argument1$; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 3ea2b5151a..5699747d40 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -98,40 +98,41 @@ case //│ sign = N //│ modulefulness = Modulefulness of N //│ restParam = N -//│ body = Scoped: -//│ syms = Set($argument0$) -//│ body = Match: -//│ scrut = Ref of caseScrut -//│ arms = Ls of -//│ Tuple2: -//│ _1 = Cls: -//│ cls = class:Foo -//│ path = Select{class:Foo}: -//│ qual = Ref of member:Foo -//│ name = Ident of "class" -//│ _2 = Assign: -//│ lhs = $argument0$ -//│ rhs = Select{class:Foo.x}: -//│ qual = Ref of caseScrut -//│ name = Ident of "x" -//│ rest = Assign: \ -//│ lhs = a -//│ rhs = Ref of $argument0$ -//│ rest = Return: \ -//│ res = Ref of a -//│ implct = false -//│ dflt = S of Throw of Instantiate: -//│ mut = false -//│ cls = Select: -//│ qual = Ref of globalThis:globalThis -//│ name = Ident of "Error" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "match error" -//│ rest = End of "" +//│ body = Match: +//│ scrut = Ref of caseScrut +//│ arms = Ls of +//│ Tuple2: +//│ _1 = Cls: +//│ cls = class:Foo +//│ path = Select{class:Foo}: +//│ qual = Ref of member:Foo +//│ name = Ident of "class" +//│ _2 = Assign: +//│ lhs = $argument0$ +//│ rhs = Select{class:Foo.x}: +//│ qual = Ref of caseScrut +//│ name = Ident of "x" +//│ rest = Assign: \ +//│ lhs = a +//│ rhs = Ref of $argument0$ +//│ rest = Return: \ +//│ res = Ref of a +//│ implct = false +//│ dflt = S of Throw of Instantiate: +//│ mut = false +//│ cls = Select: +//│ qual = Ref of globalThis:globalThis +//│ name = Ident of "Error" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "match error" +//│ rest = End of "" +//│ rest = Assign: \ +//│ lhs = $block$res +//│ rhs = Ref of member:lambda //│ rest = Return: \ -//│ res = Ref of member:lambda +//│ res = Lit of UnitLit of false //│ implct = true //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 5faa54a309..6fef9160a3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -81,8 +81,7 @@ fun test(a) = //│ JS (unsanitized): //│ let test2; //│ test2 = function test(a) { -//│ let C11, C21; -//│ let tmp, tmp1; +//│ let C11, C21, tmp, tmp1; //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -155,8 +154,7 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz; -//│ let tmp; +//│ let bar, baz, tmp; //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 128459ca14..6a73053cbf 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -39,8 +39,7 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let whoops; -//│ let tmp1, tmp2; +//│ let whoops, tmp1, tmp2; //│ whoops = function whoops() { //│ let tmp3; //│ tmp3 = Predef.print("ok"); @@ -138,8 +137,7 @@ fun test() = //│ JS (unsanitized): //│ let test1; //│ test1 = function test() { -//│ let whoops; -//│ let tmp1; +//│ let whoops, tmp1; //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -214,8 +212,7 @@ fun a() = //│ return 1 //│ }; //│ c = function c() { -//│ let d; -//│ let tmp2, tmp3; +//│ let d, tmp2, tmp3; //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -244,8 +241,7 @@ fun b() = //│ return 1 //│ }; //│ d = function d() { -//│ let c1; -//│ let tmp3; +//│ let c1, tmp3; //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -268,14 +264,12 @@ fun c() = //│ JS (unsanitized): //│ let c; //│ c = function c() { -//│ let d, f; -//│ let tmp4; +//│ let d, f, tmp4; //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e; -//│ let tmp5, tmp6; +//│ let e, tmp5, tmp6; //│ e = function e() { //│ return 1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 006ecea04e..3c18631157 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -74,8 +74,7 @@ fun test() = //│ JS (unsanitized): //│ let test; //│ test = function test() { -//│ let Foo5; -//│ let tmp; +//│ let Foo5, tmp; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -99,8 +98,8 @@ let t = test() :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.100: new t -//│ ║ ^ +//│ ║ l.99: new t +//│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): //│ globalThis.Object.freeze(new t()) @@ -116,7 +115,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.117: new t() +//│ ║ l.116: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -238,10 +237,10 @@ class Foo with val x = 2 //│ ╔══[ERROR] Multiple definitions of symbol 'x' //│ ╟── defined here -//│ ║ l.237: val x = 1 +//│ ║ l.236: val x = 1 //│ ║ ^^^^^^^^^ //│ ╟── defined here -//│ ║ l.238: val x = 2 +//│ ║ l.237: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo12; @@ -262,10 +261,10 @@ class Foo with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.263: let x = 2 +//│ ║ l.262: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.262: val x = 1 +//│ ║ l.261: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo14; @@ -292,7 +291,7 @@ class Foo with :e class Foo with val x = 1 //│ ╔══[ERROR] Illegal body of class definition (should be a block; found term definition). -//│ ║ l.293: class Foo with val x = 1 +//│ ║ l.292: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo16; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 2d9dca8bfd..8c0a51a762 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -1,33 +1,6 @@ :js -:sjs -fun foo() = - let x = 1 - fun bar() = - let y = 2 - x + y - bar() -//│ JS (unsanitized): -//│ let foo; -//│ foo = function foo() { -//│ let bar; -//│ let x; -//│ bar = function bar() { let y; y = 2; return x + y }; -//│ x = 1; -//│ return bar() -//│ }; - - -let x = 1 -//│ x = 1 - -:sjs -fun foo() = - set x = 2 -//│ JS (unsanitized): -//│ let foo1; foo1 = function foo() { x = 2; return runtime.Unit }; - // only the `let x` should be declared in the top level of repl? // now `y` is declared in the "correct" place, but not `tmp` because `tmp` is // handled by `blockPreamble` @@ -37,23 +10,23 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x1, y, tmp; tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; -//│ REPL> Sending: block$res4 = undefined +//│ let tmp; let x; let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ REPL> Sending: block$res1 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let x1, y, tmp;try { tmp = 3 * 4; y = 1 + tmp; x1 = y + 4; block$res4 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let tmp;let x;try { let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; block$res1 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: //│ > undefined -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res4)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ > Unit {} //│ REPL> Parsed: //│ > undefined //│ > Unit {} -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > 17 //│ > Unit {} @@ -77,10 +50,9 @@ fun f() = //│ JS (unsanitized): //│ let f; //│ f = function f() { -//│ let a, b; //│ let scrut; //│ scrut = true; -//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ if (scrut === true) { let a; a = 4; return a + 4 } else { let b; b = 5; return b + 9 } //│ }; @@ -101,18 +73,18 @@ fun g(x, y, z) = (_ + a) //│ JS (unsanitized): //│ let g; -//│ g = function g(x2, y1, z) { -//│ let a, lambda; -//│ let tmp1, tmp2; +//│ g = function g(x1, y1, z) { +//│ let tmp1, tmp2, lambda; //│ split_root$: { //│ split_1$: { -//│ if (x2 === true) { +//│ if (x1 === true) { //│ break split_1$ //│ } else { //│ if (y1 === true) { //│ break split_1$ //│ } else { //│ if (z === true) { +//│ let a; //│ a = 1; //│ lambda = (undefined, function (_0) { //│ return _0 + a @@ -132,6 +104,22 @@ fun g(x, y, z) = //│ }; - +// TODO: +:e module M +//│ FAILURE: Unexpected exception +//│ /!!!\ Uncaught error: java.lang.AssertionError: assertion failed: (module:M,Scope: +//│ parent = N +//│ curThis = S of S of globalThis:globalThis +//│ bindings = HashMap($block$res -> block$res4, class:M -> M1, $block$res -> block$res2, $runtime -> runtime, member:g -> g, $definitionMetadata -> definitionMetadata, $prettyPrint -> prettyPrint, $Term -> Term, $Block -> Block, $Shape -> Shape, $block$res -> block$res, x -> x, y -> y, member:Predef -> Predef, $block$res -> block$res3, $block$res -> block$res1, $tmp -> tmp, member:f -> f, module:M -> M)) +//│ at: scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) +//│ at: hkmc2.utils.Scope.addToBindings(Scope.scala:50) +//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$2(JSBuilder.scala:556) +//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$adapted$1(JSBuilder.scala:560) +//│ at: hkmc2.utils.TraceLogger.trace(TraceLogger.scala:17) +//│ at: hkmc2.codegen.js.JSBuilder.go$1(JSBuilder.scala:560) +//│ at: hkmc2.codegen.js.JSBuilder.reserveNames(JSBuilder.scala:561) +//│ at: hkmc2.codegen.js.JSBuilder.worksheet(JSBuilder.scala:589) +//│ at: hkmc2.JSBackendDiffMaker.processTerm(JSBackendDiffMaker.scala:155) +//│ at: hkmc2.BbmlDiffMaker.processTerm(BbmlDiffMaker.scala:36) diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index b1c2122e16..0f46270d22 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -71,19 +71,18 @@ example() //│ JS (unsanitized): //│ let example2; //│ example2 = function example() { -//│ let get_x; -//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; +//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, get_x1; //│ x2 = 0; -//│ get_x = function get_x() { +//│ get_x1 = function get_x() { //│ return x2 //│ }; -//│ get_x1 = get_x; +//│ get_x = get_x1; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; //│ x2 = tmp5; //│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x1()); +//│ tmp7 = runtime.safeCall(get_x()); //│ tmp8 = Predef.print(tmp7); //│ tmp9 = (tmp6 , tmp8); //│ tmp4 = tmp9; @@ -91,7 +90,7 @@ example() //│ x2 = old1; //│ } //│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x1()); +//│ tmp11 = runtime.safeCall(get_x()); //│ return Predef.print(tmp11) //│ }; //│ example2() @@ -113,13 +112,12 @@ example() //│ JS (unsanitized): //│ let example3; //│ example3 = function example() { -//│ let get_x; -//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; +//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, get_x1; //│ x2 = 0; -//│ get_x = function get_x() { +//│ get_x1 = function get_x() { //│ return x2 //│ }; -//│ get_x1 = get_x; +//│ get_x = get_x1; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; @@ -132,7 +130,7 @@ example() //│ } //│ y = tmp4; //│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x1()); +//│ tmp9 = runtime.safeCall(get_x()); //│ tmp10 = Predef.print(tmp9); //│ return y //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 60ab9f2c5c..4ef0cd898a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) +//│ let f2; f2 = function f(x) { throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 6dd3fd47b9..9ec87b064d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -105,8 +105,7 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let i2; -//│ let scrut3, tmp19, tmp20; +//│ let i2, scrut3, tmp19, tmp20; //│ tmp21: while (true) { //│ i2 = 0; //│ scrut3 = i2 < 10; @@ -198,8 +197,7 @@ fun f(ls) = //│ JS (unsanitized): //│ let f; //│ f = function f(ls) { -//│ let tl, h; -//│ let argument0$, argument1$, tmp28; +//│ let tl, h, argument0$, argument1$, tmp28; //│ tmp29: while (true) { //│ if (ls instanceof Cons1.class) { //│ argument0$ = ls.hd; @@ -276,10 +274,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.276: then 0(0) +//│ ║ l.274: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.275: while print("Hello World"); false +//│ ║ l.273: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -289,12 +287,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.288: while { print("Hello World"), false } +//│ ║ l.286: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.289: then 0(0) +//│ ║ l.287: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.290: else 1 +//│ ║ l.288: else 1 //│ ╙── ^^^^ :fixme @@ -304,14 +302,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.302: print("Hello World") +//│ ║ l.300: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.303: false +//│ ║ l.301: false //│ ║ ^^^^^^^^^ -//│ ║ l.304: then 0(0) +//│ ║ l.302: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.305: else 1 +//│ ║ l.303: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index df45c1fece..6d604a7de6 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -187,7 +187,6 @@ f().foo() //│ f5 = function f() { //│ let x, y, z, tmp6, tmp7, capture; //│ capture = new f$capture3(null); -//│ let w; //│ x = 1; //│ y = 10; //│ z = 10; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index a249f407b4..dfd2bc3676 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -59,8 +59,7 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let g$here; -//│ let used2, unused2, foo, tmp; +//│ let used2, unused2, foo, tmp, g$here; //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); @@ -282,7 +281,6 @@ g()(1) //│ g6 = function g() { //│ let capture, f$here; //│ capture = new g$capture1(null); -//│ let y1; //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here @@ -382,8 +380,7 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let g$here; -//│ let y1, scrut; +//│ let y1, scrut, g$here; //│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 4c400e4dc6..a49b86a560 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -53,8 +53,7 @@ fun foo() = //│ } //│ }); //│ foo2 = function foo() { -//│ let lambda$here; -//│ let x, scrut, tmp2, tmp3; +//│ let x, scrut, tmp2, tmp3, lambda$here; //│ x = 1; //│ tmp4: while (true) { //│ scrut = true; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index bfe2893f41..85b58b0d92 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -34,7 +34,6 @@ fun foo() = //│ foo = function foo() { //│ let tmp, capture, bar$here; //│ capture = new foo$capture1(null); -//│ let x; //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); //│ tmp = runtime.safeCall(xs.push(bar$here)); diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index 809b3151c6..bb16dac916 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -42,8 +42,7 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let idx1, idx2, idx3, idx4, idx5; -//│ let tmp, tmp1; +//│ let tmp, tmp1, idx1, idx2, idx3, idx4, idx5; //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; //│ idx5 = idx + 0; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 8f8affbdbb..4bba48a888 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -336,8 +336,7 @@ fun foo(x, y, z) = if x is //│ JS (unsanitized): //│ let foo2; //│ foo2 = function foo(x1, y1, z1) { -//│ let value; -//│ let tmp6; +//│ let value, tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 9533eb3dde..33339b6e3a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -8,8 +8,7 @@ fun nonsense(xs) = if xs is //│ JS (unsanitized): //│ let nonsense; //│ nonsense = function nonsense(xs) { -//│ let ys; -//│ let middleElements; +//│ let ys, middleElements; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -30,8 +29,7 @@ fun lead_and_last(xs) = if xs is //│ JS (unsanitized): //│ let lead_and_last; //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y; -//│ let lastElement0$, middleElements, element0$; +//│ let x, ys, y, lastElement0$, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -65,8 +63,7 @@ fun nested_tuple_patterns(xs) = if xs is //│ JS (unsanitized): //│ let nested_tuple_patterns; //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z; -//│ let lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index ca5f1fb528..2c9c910fd6 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -108,35 +108,24 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: with JSBuilderArgNumSanityChecks val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) - val le = lowered0.copy(main = lowered0.main.mapTail: - case e: End => - Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - case Return(res, implct) => - assert(implct) - Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case Scoped(_, body) => ??? // TODO - case tl: (Throw | Break | Continue) => tl - ) - - // TODO: re-implement - // TODO: remove mutation - // var toplvlDefinedVars = Set.empty[Symbol] - // def assignResSym(b: Block, toplvl: Boolean): Block = - // b.mapTail: - // case e: End => - // Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - // case Return(res, implct) => - // assert(implct) - // Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - // case s@Scoped(xs, body) => - // if toplvl then - // toplvlDefinedVars = xs - // assignResSym(body, false) - // else - // Scoped(xs, assignResSym(body, false)) - // case tl: (Throw | Break | Continue) => tl - // val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + // TODO: remove mutation + var toplvlDefinedVars = Set.empty[Symbol] + def assignResSym(b: Block, toplvl: Boolean): Block = + b.mapTail: + case e: End => + Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + case Return(res, implct) => + assert(implct) + Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case s@Scoped(xs, body) => + if toplvl then + toplvlDefinedVars = xs + assignResSym(body, false) + else + Scoped(xs, assignResSym(body, false)) + case tl: (Throw | Break | Continue) => tl + val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then output(s"Lowered:") output(lowered0.showAsTree) @@ -155,16 +144,16 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: // NOTE: `blockPreamble` should still take care of those vars that are generated during or after the lowering stage, // while `Scoped` more reflects the declared vars in the source (or elaborated?) level? // Or, during the lowering, we should also find the correct tmp vars and add them to the set in `Scoped` - // val varsFromScopedStr = - // val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) - // (if vars.isEmpty then doc"" else - // doc" # let " :: vars.map: (_, nme) => - // nme - // .toList.mkDocument(", ") - // :: doc";").stripBreaks.mkString(100) + val varsFromScopedStr = + val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) + (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc";").stripBreaks.mkString(100) val (pre, js) = nestedScp.givenIn: jsb.worksheet(le) - val preStr = pre.stripBreaks.mkString(100) //+ varsFromScopedStr + val preStr = pre.stripBreaks.mkString(100) + varsFromScopedStr val jsStr = js.stripBreaks.mkString(100) if showSanitizedJS.isSet then output(s"JS:") From 3108bac2d194597dd0523692b8bfbfb70083073f Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 21 Nov 2025 04:58:22 +0800 Subject: [PATCH 09/72] wip: getting runtime errors of some thing undefined different match branch share the same `argumentn$` symbol, so maybe only nest scopes in patmat --- .../src/main/scala/hkmc2/codegen/Block.scala | 2 +- .../main/scala/hkmc2/codegen/Lowering.scala | 31 +- .../scala/hkmc2/semantics/SimpleSplit.scala | 10 + .../main/scala/hkmc2/semantics/Split.scala | 6 + .../src/main/scala/hkmc2/semantics/Term.scala | 27 +- .../hkmc2/semantics/ucs/Normalization.scala | 28 +- .../src/test/mlscript/codegen/Scoped.mls | 293 ++++++++++++++---- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 2 +- 8 files changed, 317 insertions(+), 82 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 36b717b9fd..4898ca55c1 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -283,7 +283,7 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: Set[Local], body: Block) extends BlockTail +case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index b8f9e33ddf..76308112f2 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -38,6 +38,7 @@ object Thrw extends TailOp: // * No longer in meaningful use and could be removed if we don't find a use for it: class Subst(initMap: Map[Local, Value]): val map = initMap + val definedSyms = collection.mutable.Set.empty[Symbol] /* def +(kv: (Local, Value)): Subst = kv match @@ -51,6 +52,7 @@ class Subst(initMap: Map[Local, Value]): case _ => v object Subst: val empty = Subst(Map.empty) + def newScope(using Subst) = new Subst(subst.map) def subst(using sub: Subst): Subst = sub end Subst @@ -123,8 +125,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) - Scoped(definedVars.toSet, blockImpl(imps ::: funs ::: rest, res)(k)) + blockImpl(imps ::: funs ::: rest, res)(k) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = stats match @@ -416,6 +417,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): // * (non-local functions are compiled into getter methods selected on some prefix) if td.params.isEmpty then val l = new TempSymbol(S(t)) + subst.definedSyms.add(l) return Assign(l, Call(Value.Ref(bs).withLocOf(ref), Nil)(true, true), k(Value.Ref(l))) case S(_) => () case N => () // TODO panic here; can only lower refs to elab'd symbols @@ -447,6 +449,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val isOr = sym is State.orSymbol if isAnd || isOr then val lamSym = BlockMemberSymbol("lambda", Nil, false) + subst.definedSyms.add(lamSym) val lamDef = FunDefn(N, lamSym, PlainParamList(Nil) :: Nil, returnedTerm(arg2)) Define( lamDef, @@ -566,6 +569,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): then k(Lambda(paramLists.head, bodyBlock)) else val lamSym = new BlockMemberSymbol("lambda", Nil, false) + subst.definedSyms.add(lamSym) val lamDef = FunDefn(N, lamSym, paramLists, bodyBlock) Define( lamDef, @@ -610,11 +614,14 @@ class Lowering()(using Config, TL, Raise, State, Ctx): inner => lowerArg(arg): asr2 => val ts = TempSymbol(N) + subst.definedSyms.add(ts) Assign(ts, Call(inner, asr2)(true, true), acc(Value.Ref(ts))) val ts = TempSymbol(N) + subst.definedSyms.add(ts) Assign(ts, Instantiate(mut, sr, asr), z(Value.Ref(ts))) case S((isym, rft)) => val sym = new BlockMemberSymbol(isym.name, Nil) + subst.definedSyms.add(sym) val (mtds, publicFlds, privateFlds, ctor) = gatherMembers(rft) val pctor = parentConstructor(cls, as) val clsDef = ClsLikeDefn(N, isym, sym, syntax.Cls, N, Nil, S(sr), @@ -700,7 +707,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): p.toLoc :: Nil, source = Diagnostic.Source.Compilation ) - + // NOTE: nothing in `quote...` is handled yet def quoteSplit(split: Split)(k: Result => Block)(using Subst): Block = split match case Split.Cons(Branch(scrutinee, pattern, continuation), tail) => quote(scrutinee): r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) @@ -873,6 +880,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): Begin(b, k(asr.reverse)) else val rcdSym = new TempSymbol(N, "rcd") + subst.definedSyms.add(rcdSym) Begin( b, Assign( @@ -902,18 +910,29 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case p: Path => k(p) case Lambda(params, body) => val lamSym = BlockMemberSymbol("lambda", Nil, false) + subst.definedSyms.add(lamSym) val lamDef = FunDefn(N, lamSym, params :: Nil, body) Define(lamDef, k(lamSym |> Value.Ref.apply)) case r => val l = new TempSymbol(N) + subst.definedSyms.add(l) Assign(l, r, k(l |> Value.Ref.apply)) + def inScopedBlock(definedSymsInElaborated: Set[Symbol])(using Subst)(mkBlock: Subst ?=> Block): Block = + Subst.newScope.givenIn: + val body = mkBlock + val scopedSyms = subst.definedSyms ++ definedSymsInElaborated + // println(subst.definedSyms) + // println(definedSymsInElaborated) + if scopedSyms.isEmpty then body else Scoped(scopedSyms, body) def program(main: st.Blk): Program = val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) - val blk = block(funs ::: rest, R(main.res))(ImplctRet)(using Subst.empty) + val blk = + inScopedBlock(main.stats.flatMap(_.definedSyms).toSet)(using Subst.empty): + block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -948,7 +967,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupSelection(prefix: Term, nme: Tree.Ident, sym: Opt[FieldSymbol])(k: Result => Block)(using Subst): Block = subTerm(prefix): p => - val selRes = TempSymbol(N, "selRes") k(Select(p, nme)(sym)) final def setupFunctionOrByNameDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) @@ -960,7 +978,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - (paramLists, returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(bodyTerm.definedSyms)(returnedTerm(bodyTerm)) + (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala index 42d25ca5a2..87dc6cea0d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala @@ -21,6 +21,11 @@ enum SimpleSplit extends AutoLocated with ProductWithTail: case Else(default: Term)(val kw: Opt[Keywrd[`else`.type | `then`.type | `do`.type]]) case End + def definedSyms: Set[Symbol] = this match + case Cons(branch, tail) => branch.definedSyms + case _ => Set.empty + + inline def ~:(head: SimpleSplit.Head): Cons = Cons(head, this) def ~~:(front: SimpleSplit): SimpleSplit = @@ -84,6 +89,11 @@ object SimpleSplit: case Match(scrutinee: Term.Ref, pattern: Pattern, consequent: SimpleSplit) case Let(binding: BlockLocalSymbol, term: Term) + def definedSyms: Set[Symbol] = this match + case Let(binding, term) => Set(binding) ++ term.definedSyms + case _ => Set.empty + + def subTerms: Ls[Term] = this match case Match(scrutinee, pattern, consequent) => scrutinee :: pattern.subTerms ::: consequent.subTerms diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 695e40453b..b35b82f253 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,6 +24,12 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End + def defineSyms: Set[Symbol] = this match + case Let(sym, term, tail) => Set(sym) ++ term.definedSyms ++ tail.defineSyms + case _ => Set.empty + + + inline def ~:(head: Branch): Split = Split.Cons(head, this) def mkClone(using State): Split = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index da9a546848..7f6bd056a6 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -364,19 +364,18 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdSpread(rcd) => RcdSpread(rcd.mkClone) case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) + // NOTE: for `codegen.Block.Scoped` + // that is why this lazy val "flattens" things like + // the `rhs` in `DefinedVars` and `Assgn` already lazy val definedSyms: Set[Symbol] = this match case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty - case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) case LetDecl(sym, annotations) => Set(sym) - // TODO: make sure - case tdef: TermDefinition => Set(tdef.sym) - case ModuleOrObjectDef(owner, sym, bsym, tparams, paramsOpt, auxParams, ext, kind, body, companion, annotations) => Set(sym) - case PatternDef(owner, sym, bsym, tparams, parameters, patternParams, extractionParams, pattern, annotations) => Set(bsym) - case ClassDef.Parameterized(owner, kind, sym, bsym, tparams, params, auxParams, ext, body, companion, annotations) => Set(bsym) - case ClassDef.Plain(owner, kind, sym, bsym, tparams, ext, body, companion, annotations) => Set(bsym) - case TypeDef(sym, bsym, tparams, rhs, companion, annotations) => Set(bsym) - case Import(sym, str, file) => Set(sym) - + case termdef: TermDefinition => Set(termdef.sym) + case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) + case imp: Import => Set(imp.sym) + case IfLike(_, split) => split.definedSyms + case SynthIf(split) => split.defineSyms // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. // And including the sym here may cause error for delayed init in a function: // ``` @@ -384,7 +383,13 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: // fun f() = // x = 2 // ``` - // case DefineVar(sym, rhs) => Set(sym) + // the definedSyms of the rhs of these two cases should be included + // because those needs to be included in the + // same current `Scoped` anyway + case DefineVar(_, rhs) => rhs.definedSyms + case Assgn(_, rhs) => rhs.definedSyms + + case _ => Set.empty // TODO: add other cases def describe: Str = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 773b73eb5b..bbd82d59b4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -247,7 +247,10 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e End() ) pat match - case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) + case FlatPattern.Lit(lit) => mkMatch( + Case.Lit(lit), + lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) + ) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = @@ -257,10 +260,15 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using Subst): Case -> Block = args match case Nil => - Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) + Case.Cls(ctorSym, st) -> locally: + // println(s":::::: ${tail.showAsTree} ${tail.defineSyms}") + lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) case (param, arg) :: args => val (cse, blk) = mkArgs(args) - (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + cse -> locally: + blk match + case Scoped(syms, blk) => Scoped(syms ++ Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + case _ => Scoped(Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) // Select the constructor's `.class` field. lazy val ctorTerm = ctor.symbol match @@ -283,21 +291,25 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctorTerm)(k(cls, cls.tree.clsParams)) case mod: ModuleOrObjectSymbol => subTerm_nonTail(ctorTerm)(k(mod, Nil)) - case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) + case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))) case FlatPattern.Record(entries) => val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - entries.foldRight(lowerSplit(tail, cont, topLevel = false)): + entries.foldRight(lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))): case ((fieldName, fieldSymbol), blk) => mkMatch( Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - Assign(fieldSymbol, Select(sr, fieldName)(N), blk) + blk match + case Scoped(syms, blk) => + Scoped(syms ++ Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) + case _ => + Scoped(Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) ) ) case Split.Else(els) => labels.get(els) match case S(label) => Break(label) - case N => term_nonTail(els)(cont.fold(identity, _(topLevel))) + case N => lowering.inScopedBlock(els.definedSyms)(term_nonTail(els)(cont.fold(identity, _(topLevel)))) case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) /** @@ -393,6 +405,8 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e Label(rootBreakLabel, false, mainBlock, End()) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) + if usesResTmp then + Subst.subst.definedSyms.add(l) val block = if kw === `while` then Begin(Label(loopLabel, true, body, End()), rest) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 8c0a51a762..f6128afab9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -1,44 +1,176 @@ :js +:lot +module M +class C +object O +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped(member:C__593, member:M__594, member:O__595): +//│ Define: +//│ defn = ClsLikeDefn: +//│ owner = N +//│ isym = class:M__604 +//│ sym = member:M__594 +//│ k = Cls +//│ paramsOpt = N +//│ auxParams = Nil +//│ parentPath = N +//│ methods = Nil +//│ privateFields = Nil +//│ publicFields = Nil +//│ preCtor = End of "" +//│ ctor = Return: +//│ res = Select{object:Unit}: +//│ qual = Ref of $runtime__5 +//│ name = Ident of "Unit" +//│ implct = true +//│ companion = S of ClsLikeBody: +//│ isym = module:M__596 +//│ methods = Nil +//│ privateFields = Nil +//│ publicFields = Nil +//│ ctor = End of "" +//│ bufferable = N +//│ rest = Define: \ +//│ defn = ClsLikeDefn: +//│ owner = N +//│ isym = class:C__598 +//│ sym = member:C__593 +//│ k = Cls +//│ paramsOpt = N +//│ auxParams = Nil +//│ parentPath = N +//│ methods = Nil +//│ privateFields = Nil +//│ publicFields = Nil +//│ preCtor = End of "" +//│ ctor = End of "" +//│ companion = N +//│ bufferable = N +//│ rest = Define: \ +//│ defn = ClsLikeDefn: +//│ owner = N +//│ isym = object:O__600 +//│ sym = member:O__595 +//│ k = Obj +//│ paramsOpt = N +//│ auxParams = Nil +//│ parentPath = N +//│ methods = Nil +//│ privateFields = Nil +//│ publicFields = Nil +//│ preCtor = End of "" +//│ ctor = End of "" +//│ companion = N +//│ bufferable = N +//│ rest = End of "" -// only the `let x` should be declared in the top level of repl? -// now `y` is declared in the "correct" place, but not `tmp` because `tmp` is -// handled by `blockPreamble` -:showRepl -:sjs + +// NOTE: only one symbol is in `Scoped` +:lot +module MM +class MM +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped(member:MM__606): +//│ Define: +//│ defn = ClsLikeDefn: +//│ owner = N +//│ isym = class:MM__609 +//│ sym = member:MM__606 +//│ k = Cls +//│ paramsOpt = N +//│ auxParams = Nil +//│ parentPath = N +//│ methods = Nil +//│ privateFields = Nil +//│ publicFields = Nil +//│ preCtor = End of "" +//│ ctor = End of "" +//│ companion = S of ClsLikeBody: +//│ isym = module:MM__607 +//│ methods = Nil +//│ privateFields = Nil +//│ publicFields = Nil +//│ ctor = End of "" +//│ bufferable = N +//│ rest = End of "" + +:lot +let k +val x +fun f +let y +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped(k__615, member:f__613, member:x__614, y__620): +//│ Assign: +//│ lhs = k__615 +//│ rhs = Lit of UnitLit of false +//│ rest = Assign: \ +//│ lhs = y__620 +//│ rhs = Lit of UnitLit of false +//│ rest = End of "" +//│ k = undefined +//│ y = undefined + + + + + +// nested lets should not create nested scopes +:lot let x = let y = 1 + 3 * 4 y + 4 -//│ JS (unsanitized): -//│ let tmp; let x; let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; -//│ REPL> Sending: block$res1 = undefined -//│ REPL> Collected: -//│ > undefined -//│ REPL> Sending: let tmp;let x;try { let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; block$res1 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } -//│ REPL> Collected: -//│ > undefined -//│ REPL> Parsed: -//│ > undefined -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } -//│ REPL> Collected: -//│ > undefined -//│ > Unit {} -//│ REPL> Parsed: -//│ > undefined -//│ > Unit {} -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } -//│ REPL> Collected: -//│ > 17 -//│ > Unit {} -//│ REPL> Parsed: -//│ > 17 -//│ > Unit {} +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped($tmp__630, x__623, y__624): +//│ Assign: +//│ lhs = $tmp__630 +//│ rhs = Call: +//│ fun = Ref of builtin:*__36 +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of IntLit of 3 +//│ Arg: +//│ spread = N +//│ value = Lit of IntLit of 4 +//│ rest = Assign: \ +//│ lhs = y__624 +//│ rhs = Call: +//│ fun = Ref of builtin:+__42 +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of IntLit of 1 +//│ Arg: +//│ spread = N +//│ value = Ref of $tmp__630 +//│ rest = Assign: \ +//│ lhs = x__623 +//│ rhs = Call: +//│ fun = Ref of builtin:+__42 +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Ref of y__624 +//│ Arg: +//│ spread = N +//│ value = Lit of IntLit of 4 +//│ rest = End of "" //│ x = 17 -// `a` and `b` is declared in the correct places +// `a` and `b` is declared inside the branches (some other passes on the block ir may need to merge them) :sjs fun f() = if true then @@ -48,8 +180,8 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; -//│ f = function f() { +//│ let f1; +//│ f1 = function f() { //│ let scrut; //│ scrut = true; //│ if (scrut === true) { let a; a = 4; return a + 4 } else { let b; b = 5; return b + 9 } @@ -61,8 +193,7 @@ fun f() = // `let a` is declared in the correct place, but not `lambda` -// NOTE: `tmp1` and `tmp2` should indeed be declared in the top level of the function body -// but not `lambda` +// TODO: this is because this `lambda` symbol is not created during lowering, but in a later pass called "LambdaRewriter" :sjs fun g(x, y, z) = 3 + 4 * if @@ -73,14 +204,15 @@ fun g(x, y, z) = (_ + a) //│ JS (unsanitized): //│ let g; -//│ g = function g(x1, y1, z) { -//│ let tmp1, tmp2, lambda; +//│ g = function g(x2, y2, z) { +//│ let lambda; +//│ let tmp1, tmp2; //│ split_root$: { //│ split_1$: { -//│ if (x1 === true) { +//│ if (x2 === true) { //│ break split_1$ //│ } else { -//│ if (y1 === true) { +//│ if (y2 === true) { //│ break split_1$ //│ } else { //│ if (z === true) { @@ -104,22 +236,71 @@ fun g(x, y, z) = //│ }; -// TODO: -:e -module M -//│ FAILURE: Unexpected exception -//│ /!!!\ Uncaught error: java.lang.AssertionError: assertion failed: (module:M,Scope: -//│ parent = N -//│ curThis = S of S of globalThis:globalThis -//│ bindings = HashMap($block$res -> block$res4, class:M -> M1, $block$res -> block$res2, $runtime -> runtime, member:g -> g, $definitionMetadata -> definitionMetadata, $prettyPrint -> prettyPrint, $Term -> Term, $Block -> Block, $Shape -> Shape, $block$res -> block$res, x -> x, y -> y, member:Predef -> Predef, $block$res -> block$res3, $block$res -> block$res1, $tmp -> tmp, member:f -> f, module:M -> M)) -//│ at: scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) -//│ at: hkmc2.utils.Scope.addToBindings(Scope.scala:50) -//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$2(JSBuilder.scala:556) -//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$adapted$1(JSBuilder.scala:560) -//│ at: hkmc2.utils.TraceLogger.trace(TraceLogger.scala:17) -//│ at: hkmc2.codegen.js.JSBuilder.go$1(JSBuilder.scala:560) -//│ at: hkmc2.codegen.js.JSBuilder.reserveNames(JSBuilder.scala:561) -//│ at: hkmc2.codegen.js.JSBuilder.worksheet(JSBuilder.scala:589) -//│ at: hkmc2.JSBackendDiffMaker.processTerm(JSBackendDiffMaker.scala:155) -//│ at: hkmc2.BbmlDiffMaker.processTerm(BbmlDiffMaker.scala:36) + +// the tempvar for `f()` is in the nested scope +:sjs +fun f() = + while x && y do + f() + 4 +//│ JS (unsanitized): +//│ let f2; +//│ f2 = function f() { +//│ let scrut, lambda, tmp1; +//│ tmp2: while (true) { +//│ lambda = (undefined, function () { +//│ return y +//│ }); +//│ scrut = runtime.short_and(x1, lambda); +//│ if (scrut === true) { +//│ let tmp3; +//│ tmp3 = f2(); +//│ tmp1 = tmp3 + 4; +//│ continue tmp2 +//│ } else { tmp1 = runtime.Unit; } +//│ break; +//│ } +//│ return tmp1 +//│ }; + + + +data class A(x, y) +data class B(y, z) + + +// `argument$` and `a, y, z` are all in nested scopes +:sjs +fun g() = if x is + A(a, B(y, z)) then false +//│ JS (unsanitized): +//│ let g1; +//│ g1 = function g() { +//│ let tmp1; +//│ split_root$: { +//│ split_default$: { +//│ if (x1 instanceof A.class) { +//│ let argument0$, argument1$; +//│ argument0$ = x1.x; +//│ argument1$ = x1.y; +//│ if (argument1$ instanceof B.class) { +//│ let y2, a, z, argument0$1, argument1$1; +//│ argument0$1 = argument1$.y; +//│ argument1$1 = argument1$.z; +//│ z = argument1$1; +//│ y2 = argument0$1; +//│ a = argument0$; +//│ tmp1 = false; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else { +//│ break split_default$ +//│ } +//│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ } +//│ return tmp1 +//│ }; + diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 2c9c910fd6..e8def6150f 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -110,7 +110,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: val lowered0 = low.program(blk) // TODO: remove mutation - var toplvlDefinedVars = Set.empty[Symbol] + var toplvlDefinedVars = collection.Set.empty[Symbol] def assignResSym(b: Block, toplvl: Boolean): Block = b.mapTail: case e: End => From ab0d660eec9fb45a86cd254aaa8a53a39a74d4b5 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 21 Nov 2025 05:14:28 +0800 Subject: [PATCH 10/72] wip: add known bugs in the tests --- .../src/test/mlscript/codegen/Scoped.mls | 135 ++++++++++++++---- 1 file changed, 106 insertions(+), 29 deletions(-) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index f6128afab9..0f378c87a4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -7,12 +7,13 @@ object O //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped(member:C__593, member:M__594, member:O__595): -//│ Define: +//│ main = Scoped: +//│ syms = HashSet(member:C, member:M, member:O) +//│ body = Define: //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = class:M__604 -//│ sym = member:M__594 +//│ isym = class:M +//│ sym = member:M //│ k = Cls //│ paramsOpt = N //│ auxParams = Nil @@ -23,11 +24,11 @@ object O //│ preCtor = End of "" //│ ctor = Return: //│ res = Select{object:Unit}: -//│ qual = Ref of $runtime__5 +//│ qual = Ref of $runtime //│ name = Ident of "Unit" //│ implct = true //│ companion = S of ClsLikeBody: -//│ isym = module:M__596 +//│ isym = module:M //│ methods = Nil //│ privateFields = Nil //│ publicFields = Nil @@ -36,8 +37,8 @@ object O //│ rest = Define: \ //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = class:C__598 -//│ sym = member:C__593 +//│ isym = class:C +//│ sym = member:C //│ k = Cls //│ paramsOpt = N //│ auxParams = Nil @@ -52,8 +53,8 @@ object O //│ rest = Define: \ //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = object:O__600 -//│ sym = member:O__595 +//│ isym = object:O +//│ sym = member:O //│ k = Obj //│ paramsOpt = N //│ auxParams = Nil @@ -75,12 +76,13 @@ class MM //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped(member:MM__606): -//│ Define: +//│ main = Scoped: +//│ syms = HashSet(member:MM) +//│ body = Define: //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = class:MM__609 -//│ sym = member:MM__606 +//│ isym = class:MM +//│ sym = member:MM //│ k = Cls //│ paramsOpt = N //│ auxParams = Nil @@ -91,7 +93,7 @@ class MM //│ preCtor = End of "" //│ ctor = End of "" //│ companion = S of ClsLikeBody: -//│ isym = module:MM__607 +//│ isym = module:MM //│ methods = Nil //│ privateFields = Nil //│ publicFields = Nil @@ -107,12 +109,13 @@ let y //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped(k__615, member:f__613, member:x__614, y__620): -//│ Assign: -//│ lhs = k__615 +//│ main = Scoped: +//│ syms = HashSet(y, member:f, member:x, k) +//│ body = Assign: +//│ lhs = k //│ rhs = Lit of UnitLit of false //│ rest = Assign: \ -//│ lhs = y__620 +//│ lhs = y //│ rhs = Lit of UnitLit of false //│ rest = End of "" //│ k = undefined @@ -130,11 +133,12 @@ let x = //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped($tmp__630, x__623, y__624): -//│ Assign: -//│ lhs = $tmp__630 +//│ main = Scoped: +//│ syms = HashSet(y, $tmp, x) +//│ body = Assign: +//│ lhs = $tmp //│ rhs = Call: -//│ fun = Ref of builtin:*__36 +//│ fun = Ref of builtin:* //│ args = Ls of //│ Arg: //│ spread = N @@ -143,24 +147,24 @@ let x = //│ spread = N //│ value = Lit of IntLit of 4 //│ rest = Assign: \ -//│ lhs = y__624 +//│ lhs = y //│ rhs = Call: -//│ fun = Ref of builtin:+__42 +//│ fun = Ref of builtin:+ //│ args = Ls of //│ Arg: //│ spread = N //│ value = Lit of IntLit of 1 //│ Arg: //│ spread = N -//│ value = Ref of $tmp__630 +//│ value = Ref of $tmp //│ rest = Assign: \ -//│ lhs = x__623 +//│ lhs = x //│ rhs = Call: -//│ fun = Ref of builtin:+__42 +//│ fun = Ref of builtin:+ //│ args = Ls of //│ Arg: //│ spread = N -//│ value = Ref of y__624 +//│ value = Ref of y //│ Arg: //│ spread = N //│ value = Lit of IntLit of 4 @@ -304,3 +308,76 @@ fun g() = if x is //│ }; + + + +// FIXME: `argument$` is shared across different branches... +:fixme +:sjs +fun f() = if x is + A(a, b) then true + B(c, d) then false +//│ ═══[WARNING] var $argument0$ in scoped is already allocated +//│ ═══[WARNING] var $argument1$ in scoped is already allocated +//│ JS (unsanitized): +//│ let f3; +//│ f3 = function f() { +//│ if (x1 instanceof A.class) { +//│ let a, b, argument0$, argument1$; +//│ argument0$ = x1.x; +//│ argument1$ = x1.y; +//│ b = argument1$; +//│ a = argument0$; +//│ return true +//│ } else if (x1 instanceof B.class) { +//│ let c, d; +//│ argument0$ = x1.y; +//│ argument1$ = x1.z; +//│ d = argument1$; +//│ c = argument0$; +//│ return false +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ }; +//│ ═══[WARNING] var $argument0$ in scoped is already allocated +//│ ═══[WARNING] var $argument1$ in scoped is already allocated + + +// FIXME: runtime error: `t` in `lscomp(t)` is not defined in runtime +// reason: the `let t` should be in an outer level +:sjs +fun lscomp(ls) = if ls is + A(_, t) and + false and true then x + else lscomp(t) +//│ JS (unsanitized): +//│ let lscomp; +//│ lscomp = function lscomp(ls) { +//│ let tmp1; +//│ split_root$: { +//│ split_1$: { +//│ if (ls instanceof A.class) { +//│ let t, scrut, argument0$, argument1$; +//│ argument0$ = ls.x; +//│ argument1$ = ls.y; +//│ t = argument1$; +//│ scrut = false; +//│ if (scrut === true) { +//│ let scrut1; +//│ scrut1 = true; +//│ if (scrut1 === true) { +//│ tmp1 = x1; +//│ break split_root$ +//│ } else { +//│ break split_1$ +//│ } +//│ } else { +//│ break split_1$ +//│ } +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ } +//│ tmp1 = lscomp(t); +//│ } +//│ return tmp1 +//│ }; From 79bd913117b3466eeec450ae903670b12733975b Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sat, 22 Nov 2025 22:51:27 +0800 Subject: [PATCH 11/72] wip wip --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 3 +- .../scala/hkmc2/semantics/SimpleSplit.scala | 9 ++ .../main/scala/hkmc2/semantics/Split.scala | 1 + .../src/main/scala/hkmc2/semantics/Term.scala | 4 +- .../hkmc2/semantics/ucs/Normalization.scala | 109 +++++++++++++++--- .../src/main/scala/hkmc2/utils/utils.scala | 5 +- 6 files changed, 109 insertions(+), 22 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 255182698a..769e51323b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -508,13 +508,14 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: raise: WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) None + // Some(l -> scope.lookup_!(l, N)) else Some(l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") - :: doc";") :: returningTerm(body, endSemi) + :: doc"; /* scoped */") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala index 87dc6cea0d..6c9cf3e18c 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala @@ -25,6 +25,11 @@ enum SimpleSplit extends AutoLocated with ProductWithTail: case Cons(branch, tail) => branch.definedSyms case _ => Set.empty + def definedSymsDeep: Set[Symbol] = this match + case Cons(branch, tail) => branch.definedSymsDeep ++ tail.definedSymsDeep + case Else(d) => d.definedSyms + case End => Set.empty + inline def ~:(head: SimpleSplit.Head): Cons = Cons(head, this) @@ -93,6 +98,10 @@ object SimpleSplit: case Let(binding, term) => Set(binding) ++ term.definedSyms case _ => Set.empty + def definedSymsDeep: Set[Symbol] = this match + case Let(binding, term) => Set(binding) ++ term.definedSyms + case Match(scrutinee, pattern, consequent) => consequent.definedSymsDeep + def subTerms: Ls[Term] = this match case Match(scrutinee, pattern, consequent) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index b35b82f253..f5d8e2858c 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -26,6 +26,7 @@ enum Split extends AutoLocated with ProductWithTail: def defineSyms: Set[Symbol] = this match case Let(sym, term, tail) => Set(sym) ++ term.definedSyms ++ tail.defineSyms + case Else(d) => d.definedSyms case _ => Set.empty diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 7f6bd056a6..70ffc7acd9 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -374,8 +374,10 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case termdef: TermDefinition => Set(termdef.sym) case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) case imp: Import => Set(imp.sym) - case IfLike(_, split) => split.definedSyms + case IfLike(Keyword.`while`, split) => split.definedSymsDeep + case IfLike(Keyword.`if`, split) => split.definedSymsDeep case SynthIf(split) => split.defineSyms + // case SynthIf(split) => ??? // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. // And including the sym here may cause error for delayed init in a function: // ``` diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index bbd82d59b4..33a5db7806 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -231,6 +231,89 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e val default = if throwCount > 1 then S(TempSymbol(N, s"split_default$$")) else N Labels(consequents, default) + // TODO: maybe cannot even use nested scopes + // miscompiles more complex pattern matchings that create `Label`s + // private def lowerSplitForWhile( + // split: Split, + // cont: (Result => Block) \/ (Bool => Result => Block), + // topLevel: Bool + // )(using labels: Labels)(using Subst): Block = split match + // case Split.Let(sym, trm, tl) => + // term_nonTail(trm): r => + // Assign(sym, r, lowerSplitForWhile(tl, cont, topLevel)) + // case Split.Cons(Branch(scrut, pat, tail), restSplit) => + // subTerm_nonTail(scrut): sr => + // tl.log(s"Binding scrut $scrut to $sr (${summon[Subst].map})") + // def mkMatch(cse: Case -> Block) = Match(sr, cse :: Nil, + // S(lowerSplitForWhile(restSplit, cont, topLevel = true)), + // End() + // ) + // pat match + // case FlatPattern.Lit(lit) => mkMatch( + // Case.Lit(lit), + // lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false)) + // ) + // case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => + // /** Make a continuation that creates the match. */ + // def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = + // val args = argsOpt.map(_.map(_._1)).getOrElse(Nil) + // // Normalization should reject cases where the user provides + // // more sub-patterns than there are actual class parameters. + // assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) + // def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using Subst): Case -> Block = args match + // case Nil => + // Case.Cls(ctorSym, st) -> locally: + // // println(s":::::: ${tail.showAsTree}\n >>>>>> ${tail.defineSyms}") + // lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false)) + // case (param, arg) :: args => + // val (cse, blk) = mkArgs(args) + // cse -> locally: + // blk match + // case Scoped(syms, blk) => Scoped(syms ++ Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + // case _ => Scoped(Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + // mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) + // // Select the constructor's `.class` field. + // lazy val ctorTerm = ctor.symbol match + // case S(mem: BlockMemberSymbol) => + // // If the class is declaration-only, we do not need to + // // select the class. + // if !mem.hasLiftedClass || mem.defn.exists(_.hasDeclareModifier.isDefined) then ctor + // else Term.SynthSel(ctor, Tree.Ident("class"))(mem.clsTree.orElse(mem.modOrObjTree).map(_.symbol), N).resolve + // case _ => ctor + // symbol match + // case cls: ClassSymbol if ctx.builtins.virtualClasses contains cls => + // // [invariant:0] Some classes (e.g., `Int`) from `Prelude` do + // // not exist at runtime. If we do lowering on `trm`, backends + // // (e.g., `JSBuilder`) will not be able to handle the corresponding selections. + // // In this case the second parameter of `Case.Cls` will not be used. + // // So we do not elaborate `ctor` when the `cls` is virtual + // // and use it `Predef.unreachable` here. + // k(cls, Nil)(unreachableFn) + // case cls: ClassSymbol => + // subTerm_nonTail(ctorTerm)(k(cls, cls.tree.clsParams)) + // case mod: ModuleOrObjectSymbol => + // subTerm_nonTail(ctorTerm)(k(mod, Nil)) + // case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false))) + // case FlatPattern.Record(entries) => + // val objectSym = ctx.builtins.Object + // mkMatch( // checking that we have an object + // Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), + // entries.foldRight(lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false))): + // case ((fieldName, fieldSymbol), blk) => + // mkMatch( + // Case.Field(fieldName, safe = true), // we know we have an object, no need to check again + // blk match + // case Scoped(syms, blk) => + // Scoped(syms ++ Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) + // case _ => + // Scoped(Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) + // ) + // ) + // case Split.Else(els) => labels.get(els) match + // case S(label) => Break(label) + // case N => lowering.inScopedBlock(els.definedSyms)(term_nonTail(els)(cont.fold(identity, _(topLevel)))) + // case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) + private def lowerSplit( split: Split, cont: (Result => Block) \/ (Bool => Result => Block), @@ -247,10 +330,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e End() ) pat match - case FlatPattern.Lit(lit) => mkMatch( - Case.Lit(lit), - lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) - ) + case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = @@ -260,15 +340,10 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using Subst): Case -> Block = args match case Nil => - Case.Cls(ctorSym, st) -> locally: - // println(s":::::: ${tail.showAsTree} ${tail.defineSyms}") - lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) + Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) case (param, arg) :: args => val (cse, blk) = mkArgs(args) - cse -> locally: - blk match - case Scoped(syms, blk) => Scoped(syms ++ Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) - case _ => Scoped(Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) // Select the constructor's `.class` field. lazy val ctorTerm = ctor.symbol match @@ -291,25 +366,21 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctorTerm)(k(cls, cls.tree.clsParams)) case mod: ModuleOrObjectSymbol => subTerm_nonTail(ctorTerm)(k(mod, Nil)) - case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))) + case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.Record(entries) => val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - entries.foldRight(lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))): + entries.foldRight(lowerSplit(tail, cont, topLevel = false)): case ((fieldName, fieldSymbol), blk) => mkMatch( Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - blk match - case Scoped(syms, blk) => - Scoped(syms ++ Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) - case _ => - Scoped(Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) + Assign(fieldSymbol, Select(sr, fieldName)(N), blk) ) ) case Split.Else(els) => labels.get(els) match case S(label) => Break(label) - case N => lowering.inScopedBlock(els.definedSyms)(term_nonTail(els)(cont.fold(identity, _(topLevel)))) + case N => term_nonTail(els)(cont.fold(identity, _(topLevel))) case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) /** diff --git a/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala b/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala index 83e442823f..1278030d9d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala @@ -62,7 +62,10 @@ extension (t: Product) val (sl, _, sc) = origin.fph.getLineColAt(start) val (el, _, ec) = origin.fph.getLineColAt(end) s"Loc at :$sl:$sc-$el:$ec" - + // case codegen.Scoped(syms, body) => + // s"Scoped(${syms.map(aux(_)).toArray.sortInPlace.mkString(", ")}):\n" + + // aux(body).indent(" ") + // case s: semantics.Symbol => s"${s.toString()}__${s.uid}" case t: Product => t.showAsTree(inTailPos, pre) case v => v.toString From c5d16e0dad276a89b56cf4de084524f83c513869 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 17:32:27 +0800 Subject: [PATCH 12/72] Revert "wip wip" This reverts commit 79bd913117b3466eeec450ae903670b12733975b. --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 3 +- .../scala/hkmc2/semantics/SimpleSplit.scala | 9 -- .../main/scala/hkmc2/semantics/Split.scala | 1 - .../src/main/scala/hkmc2/semantics/Term.scala | 4 +- .../hkmc2/semantics/ucs/Normalization.scala | 109 +++--------------- .../src/main/scala/hkmc2/utils/utils.scala | 5 +- 6 files changed, 22 insertions(+), 109 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 769e51323b..255182698a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -508,14 +508,13 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: raise: WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) None - // Some(l -> scope.lookup_!(l, N)) else Some(l -> scope.allocateName(l)) (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") - :: doc"; /* scoped */") :: returningTerm(body, endSemi) + :: doc";") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala index 6c9cf3e18c..87dc6cea0d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala @@ -25,11 +25,6 @@ enum SimpleSplit extends AutoLocated with ProductWithTail: case Cons(branch, tail) => branch.definedSyms case _ => Set.empty - def definedSymsDeep: Set[Symbol] = this match - case Cons(branch, tail) => branch.definedSymsDeep ++ tail.definedSymsDeep - case Else(d) => d.definedSyms - case End => Set.empty - inline def ~:(head: SimpleSplit.Head): Cons = Cons(head, this) @@ -98,10 +93,6 @@ object SimpleSplit: case Let(binding, term) => Set(binding) ++ term.definedSyms case _ => Set.empty - def definedSymsDeep: Set[Symbol] = this match - case Let(binding, term) => Set(binding) ++ term.definedSyms - case Match(scrutinee, pattern, consequent) => consequent.definedSymsDeep - def subTerms: Ls[Term] = this match case Match(scrutinee, pattern, consequent) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index f5d8e2858c..b35b82f253 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -26,7 +26,6 @@ enum Split extends AutoLocated with ProductWithTail: def defineSyms: Set[Symbol] = this match case Let(sym, term, tail) => Set(sym) ++ term.definedSyms ++ tail.defineSyms - case Else(d) => d.definedSyms case _ => Set.empty diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 70ffc7acd9..7f6bd056a6 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -374,10 +374,8 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case termdef: TermDefinition => Set(termdef.sym) case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) case imp: Import => Set(imp.sym) - case IfLike(Keyword.`while`, split) => split.definedSymsDeep - case IfLike(Keyword.`if`, split) => split.definedSymsDeep + case IfLike(_, split) => split.definedSyms case SynthIf(split) => split.defineSyms - // case SynthIf(split) => ??? // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. // And including the sym here may cause error for delayed init in a function: // ``` diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 33a5db7806..bbd82d59b4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -231,89 +231,6 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e val default = if throwCount > 1 then S(TempSymbol(N, s"split_default$$")) else N Labels(consequents, default) - // TODO: maybe cannot even use nested scopes - // miscompiles more complex pattern matchings that create `Label`s - // private def lowerSplitForWhile( - // split: Split, - // cont: (Result => Block) \/ (Bool => Result => Block), - // topLevel: Bool - // )(using labels: Labels)(using Subst): Block = split match - // case Split.Let(sym, trm, tl) => - // term_nonTail(trm): r => - // Assign(sym, r, lowerSplitForWhile(tl, cont, topLevel)) - // case Split.Cons(Branch(scrut, pat, tail), restSplit) => - // subTerm_nonTail(scrut): sr => - // tl.log(s"Binding scrut $scrut to $sr (${summon[Subst].map})") - // def mkMatch(cse: Case -> Block) = Match(sr, cse :: Nil, - // S(lowerSplitForWhile(restSplit, cont, topLevel = true)), - // End() - // ) - // pat match - // case FlatPattern.Lit(lit) => mkMatch( - // Case.Lit(lit), - // lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false)) - // ) - // case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => - // /** Make a continuation that creates the match. */ - // def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = - // val args = argsOpt.map(_.map(_._1)).getOrElse(Nil) - // // Normalization should reject cases where the user provides - // // more sub-patterns than there are actual class parameters. - // assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) - // def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using Subst): Case -> Block = args match - // case Nil => - // Case.Cls(ctorSym, st) -> locally: - // // println(s":::::: ${tail.showAsTree}\n >>>>>> ${tail.defineSyms}") - // lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false)) - // case (param, arg) :: args => - // val (cse, blk) = mkArgs(args) - // cse -> locally: - // blk match - // case Scoped(syms, blk) => Scoped(syms ++ Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) - // case _ => Scoped(Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) - // mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) - // // Select the constructor's `.class` field. - // lazy val ctorTerm = ctor.symbol match - // case S(mem: BlockMemberSymbol) => - // // If the class is declaration-only, we do not need to - // // select the class. - // if !mem.hasLiftedClass || mem.defn.exists(_.hasDeclareModifier.isDefined) then ctor - // else Term.SynthSel(ctor, Tree.Ident("class"))(mem.clsTree.orElse(mem.modOrObjTree).map(_.symbol), N).resolve - // case _ => ctor - // symbol match - // case cls: ClassSymbol if ctx.builtins.virtualClasses contains cls => - // // [invariant:0] Some classes (e.g., `Int`) from `Prelude` do - // // not exist at runtime. If we do lowering on `trm`, backends - // // (e.g., `JSBuilder`) will not be able to handle the corresponding selections. - // // In this case the second parameter of `Case.Cls` will not be used. - // // So we do not elaborate `ctor` when the `cls` is virtual - // // and use it `Predef.unreachable` here. - // k(cls, Nil)(unreachableFn) - // case cls: ClassSymbol => - // subTerm_nonTail(ctorTerm)(k(cls, cls.tree.clsParams)) - // case mod: ModuleOrObjectSymbol => - // subTerm_nonTail(ctorTerm)(k(mod, Nil)) - // case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false))) - // case FlatPattern.Record(entries) => - // val objectSym = ctx.builtins.Object - // mkMatch( // checking that we have an object - // Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - // entries.foldRight(lowering.inScopedBlock(tail.defineSyms)(lowerSplitForWhile(tail, cont, topLevel = false))): - // case ((fieldName, fieldSymbol), blk) => - // mkMatch( - // Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - // blk match - // case Scoped(syms, blk) => - // Scoped(syms ++ Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) - // case _ => - // Scoped(Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) - // ) - // ) - // case Split.Else(els) => labels.get(els) match - // case S(label) => Break(label) - // case N => lowering.inScopedBlock(els.definedSyms)(term_nonTail(els)(cont.fold(identity, _(topLevel)))) - // case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) - private def lowerSplit( split: Split, cont: (Result => Block) \/ (Bool => Result => Block), @@ -330,7 +247,10 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e End() ) pat match - case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) + case FlatPattern.Lit(lit) => mkMatch( + Case.Lit(lit), + lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) + ) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = @@ -340,10 +260,15 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using Subst): Case -> Block = args match case Nil => - Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) + Case.Cls(ctorSym, st) -> locally: + // println(s":::::: ${tail.showAsTree} ${tail.defineSyms}") + lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) case (param, arg) :: args => val (cse, blk) = mkArgs(args) - (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + cse -> locally: + blk match + case Scoped(syms, blk) => Scoped(syms ++ Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + case _ => Scoped(Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) // Select the constructor's `.class` field. lazy val ctorTerm = ctor.symbol match @@ -366,21 +291,25 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctorTerm)(k(cls, cls.tree.clsParams)) case mod: ModuleOrObjectSymbol => subTerm_nonTail(ctorTerm)(k(mod, Nil)) - case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) + case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))) case FlatPattern.Record(entries) => val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - entries.foldRight(lowerSplit(tail, cont, topLevel = false)): + entries.foldRight(lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))): case ((fieldName, fieldSymbol), blk) => mkMatch( Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - Assign(fieldSymbol, Select(sr, fieldName)(N), blk) + blk match + case Scoped(syms, blk) => + Scoped(syms ++ Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) + case _ => + Scoped(Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) ) ) case Split.Else(els) => labels.get(els) match case S(label) => Break(label) - case N => term_nonTail(els)(cont.fold(identity, _(topLevel))) + case N => lowering.inScopedBlock(els.definedSyms)(term_nonTail(els)(cont.fold(identity, _(topLevel)))) case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) /** diff --git a/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala b/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala index 1278030d9d..83e442823f 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/utils/utils.scala @@ -62,10 +62,7 @@ extension (t: Product) val (sl, _, sc) = origin.fph.getLineColAt(start) val (el, _, ec) = origin.fph.getLineColAt(end) s"Loc at :$sl:$sc-$el:$ec" - // case codegen.Scoped(syms, body) => - // s"Scoped(${syms.map(aux(_)).toArray.sortInPlace.mkString(", ")}):\n" + - // aux(body).indent(" ") - // case s: semantics.Symbol => s"${s.toString()}__${s.uid}" + case t: Product => t.showAsTree(inTailPos, pre) case v => v.toString From 69ad8901ff7db70ea077ccea551a418d2a5fbdb8 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 17:32:35 +0800 Subject: [PATCH 13/72] Revert "wip: add known bugs in the tests" This reverts commit ab0d660eec9fb45a86cd254aaa8a53a39a74d4b5. --- .../src/test/mlscript/codegen/Scoped.mls | 135 ++++-------------- 1 file changed, 29 insertions(+), 106 deletions(-) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 0f378c87a4..f6128afab9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -7,13 +7,12 @@ object O //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: -//│ syms = HashSet(member:C, member:M, member:O) -//│ body = Define: +//│ main = Scoped(member:C__593, member:M__594, member:O__595): +//│ Define: //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = class:M -//│ sym = member:M +//│ isym = class:M__604 +//│ sym = member:M__594 //│ k = Cls //│ paramsOpt = N //│ auxParams = Nil @@ -24,11 +23,11 @@ object O //│ preCtor = End of "" //│ ctor = Return: //│ res = Select{object:Unit}: -//│ qual = Ref of $runtime +//│ qual = Ref of $runtime__5 //│ name = Ident of "Unit" //│ implct = true //│ companion = S of ClsLikeBody: -//│ isym = module:M +//│ isym = module:M__596 //│ methods = Nil //│ privateFields = Nil //│ publicFields = Nil @@ -37,8 +36,8 @@ object O //│ rest = Define: \ //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = class:C -//│ sym = member:C +//│ isym = class:C__598 +//│ sym = member:C__593 //│ k = Cls //│ paramsOpt = N //│ auxParams = Nil @@ -53,8 +52,8 @@ object O //│ rest = Define: \ //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = object:O -//│ sym = member:O +//│ isym = object:O__600 +//│ sym = member:O__595 //│ k = Obj //│ paramsOpt = N //│ auxParams = Nil @@ -76,13 +75,12 @@ class MM //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: -//│ syms = HashSet(member:MM) -//│ body = Define: +//│ main = Scoped(member:MM__606): +//│ Define: //│ defn = ClsLikeDefn: //│ owner = N -//│ isym = class:MM -//│ sym = member:MM +//│ isym = class:MM__609 +//│ sym = member:MM__606 //│ k = Cls //│ paramsOpt = N //│ auxParams = Nil @@ -93,7 +91,7 @@ class MM //│ preCtor = End of "" //│ ctor = End of "" //│ companion = S of ClsLikeBody: -//│ isym = module:MM +//│ isym = module:MM__607 //│ methods = Nil //│ privateFields = Nil //│ publicFields = Nil @@ -109,13 +107,12 @@ let y //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: -//│ syms = HashSet(y, member:f, member:x, k) -//│ body = Assign: -//│ lhs = k +//│ main = Scoped(k__615, member:f__613, member:x__614, y__620): +//│ Assign: +//│ lhs = k__615 //│ rhs = Lit of UnitLit of false //│ rest = Assign: \ -//│ lhs = y +//│ lhs = y__620 //│ rhs = Lit of UnitLit of false //│ rest = End of "" //│ k = undefined @@ -133,12 +130,11 @@ let x = //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: -//│ syms = HashSet(y, $tmp, x) -//│ body = Assign: -//│ lhs = $tmp +//│ main = Scoped($tmp__630, x__623, y__624): +//│ Assign: +//│ lhs = $tmp__630 //│ rhs = Call: -//│ fun = Ref of builtin:* +//│ fun = Ref of builtin:*__36 //│ args = Ls of //│ Arg: //│ spread = N @@ -147,24 +143,24 @@ let x = //│ spread = N //│ value = Lit of IntLit of 4 //│ rest = Assign: \ -//│ lhs = y +//│ lhs = y__624 //│ rhs = Call: -//│ fun = Ref of builtin:+ +//│ fun = Ref of builtin:+__42 //│ args = Ls of //│ Arg: //│ spread = N //│ value = Lit of IntLit of 1 //│ Arg: //│ spread = N -//│ value = Ref of $tmp +//│ value = Ref of $tmp__630 //│ rest = Assign: \ -//│ lhs = x +//│ lhs = x__623 //│ rhs = Call: -//│ fun = Ref of builtin:+ +//│ fun = Ref of builtin:+__42 //│ args = Ls of //│ Arg: //│ spread = N -//│ value = Ref of y +//│ value = Ref of y__624 //│ Arg: //│ spread = N //│ value = Lit of IntLit of 4 @@ -308,76 +304,3 @@ fun g() = if x is //│ }; - - - -// FIXME: `argument$` is shared across different branches... -:fixme -:sjs -fun f() = if x is - A(a, b) then true - B(c, d) then false -//│ ═══[WARNING] var $argument0$ in scoped is already allocated -//│ ═══[WARNING] var $argument1$ in scoped is already allocated -//│ JS (unsanitized): -//│ let f3; -//│ f3 = function f() { -//│ if (x1 instanceof A.class) { -//│ let a, b, argument0$, argument1$; -//│ argument0$ = x1.x; -//│ argument1$ = x1.y; -//│ b = argument1$; -//│ a = argument0$; -//│ return true -//│ } else if (x1 instanceof B.class) { -//│ let c, d; -//│ argument0$ = x1.y; -//│ argument1$ = x1.z; -//│ d = argument1$; -//│ c = argument0$; -//│ return false -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ }; -//│ ═══[WARNING] var $argument0$ in scoped is already allocated -//│ ═══[WARNING] var $argument1$ in scoped is already allocated - - -// FIXME: runtime error: `t` in `lscomp(t)` is not defined in runtime -// reason: the `let t` should be in an outer level -:sjs -fun lscomp(ls) = if ls is - A(_, t) and - false and true then x - else lscomp(t) -//│ JS (unsanitized): -//│ let lscomp; -//│ lscomp = function lscomp(ls) { -//│ let tmp1; -//│ split_root$: { -//│ split_1$: { -//│ if (ls instanceof A.class) { -//│ let t, scrut, argument0$, argument1$; -//│ argument0$ = ls.x; -//│ argument1$ = ls.y; -//│ t = argument1$; -//│ scrut = false; -//│ if (scrut === true) { -//│ let scrut1; -//│ scrut1 = true; -//│ if (scrut1 === true) { -//│ tmp1 = x1; -//│ break split_root$ -//│ } else { -//│ break split_1$ -//│ } -//│ } else { -//│ break split_1$ -//│ } -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ } -//│ tmp1 = lscomp(t); -//│ } -//│ return tmp1 -//│ }; From aeb0c5afbb1496a5f51cc955e3bda71f5bc920a2 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 17:32:36 +0800 Subject: [PATCH 14/72] Revert "wip:" This reverts commit 3108bac2d194597dd0523692b8bfbfb70083073f. --- .../src/main/scala/hkmc2/codegen/Block.scala | 2 +- .../main/scala/hkmc2/codegen/Lowering.scala | 31 +- .../scala/hkmc2/semantics/SimpleSplit.scala | 10 - .../main/scala/hkmc2/semantics/Split.scala | 6 - .../src/main/scala/hkmc2/semantics/Term.scala | 27 +- .../hkmc2/semantics/ucs/Normalization.scala | 28 +- .../src/test/mlscript/codegen/Scoped.mls | 293 ++++-------------- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 2 +- 8 files changed, 82 insertions(+), 317 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 4898ca55c1..36b717b9fd 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -283,7 +283,7 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail +case class Scoped(syms: Set[Local], body: Block) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 76308112f2..b8f9e33ddf 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -38,7 +38,6 @@ object Thrw extends TailOp: // * No longer in meaningful use and could be removed if we don't find a use for it: class Subst(initMap: Map[Local, Value]): val map = initMap - val definedSyms = collection.mutable.Set.empty[Symbol] /* def +(kv: (Local, Value)): Subst = kv match @@ -52,7 +51,6 @@ class Subst(initMap: Map[Local, Value]): case _ => v object Subst: val empty = Subst(Map.empty) - def newScope(using Subst) = new Subst(subst.map) def subst(using sub: Subst): Subst = sub end Subst @@ -125,7 +123,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - blockImpl(imps ::: funs ::: rest, res)(k) + val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) + Scoped(definedVars.toSet, blockImpl(imps ::: funs ::: rest, res)(k)) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using Subst): Block = stats match @@ -417,7 +416,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): // * (non-local functions are compiled into getter methods selected on some prefix) if td.params.isEmpty then val l = new TempSymbol(S(t)) - subst.definedSyms.add(l) return Assign(l, Call(Value.Ref(bs).withLocOf(ref), Nil)(true, true), k(Value.Ref(l))) case S(_) => () case N => () // TODO panic here; can only lower refs to elab'd symbols @@ -449,7 +447,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val isOr = sym is State.orSymbol if isAnd || isOr then val lamSym = BlockMemberSymbol("lambda", Nil, false) - subst.definedSyms.add(lamSym) val lamDef = FunDefn(N, lamSym, PlainParamList(Nil) :: Nil, returnedTerm(arg2)) Define( lamDef, @@ -569,7 +566,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): then k(Lambda(paramLists.head, bodyBlock)) else val lamSym = new BlockMemberSymbol("lambda", Nil, false) - subst.definedSyms.add(lamSym) val lamDef = FunDefn(N, lamSym, paramLists, bodyBlock) Define( lamDef, @@ -614,14 +610,11 @@ class Lowering()(using Config, TL, Raise, State, Ctx): inner => lowerArg(arg): asr2 => val ts = TempSymbol(N) - subst.definedSyms.add(ts) Assign(ts, Call(inner, asr2)(true, true), acc(Value.Ref(ts))) val ts = TempSymbol(N) - subst.definedSyms.add(ts) Assign(ts, Instantiate(mut, sr, asr), z(Value.Ref(ts))) case S((isym, rft)) => val sym = new BlockMemberSymbol(isym.name, Nil) - subst.definedSyms.add(sym) val (mtds, publicFlds, privateFlds, ctor) = gatherMembers(rft) val pctor = parentConstructor(cls, as) val clsDef = ClsLikeDefn(N, isym, sym, syntax.Cls, N, Nil, S(sr), @@ -707,7 +700,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): p.toLoc :: Nil, source = Diagnostic.Source.Compilation ) - // NOTE: nothing in `quote...` is handled yet + def quoteSplit(split: Split)(k: Result => Block)(using Subst): Block = split match case Split.Cons(Branch(scrutinee, pattern, continuation), tail) => quote(scrutinee): r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) @@ -880,7 +873,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): Begin(b, k(asr.reverse)) else val rcdSym = new TempSymbol(N, "rcd") - subst.definedSyms.add(rcdSym) Begin( b, Assign( @@ -910,29 +902,18 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case p: Path => k(p) case Lambda(params, body) => val lamSym = BlockMemberSymbol("lambda", Nil, false) - subst.definedSyms.add(lamSym) val lamDef = FunDefn(N, lamSym, params :: Nil, body) Define(lamDef, k(lamSym |> Value.Ref.apply)) case r => val l = new TempSymbol(N) - subst.definedSyms.add(l) Assign(l, r, k(l |> Value.Ref.apply)) - def inScopedBlock(definedSymsInElaborated: Set[Symbol])(using Subst)(mkBlock: Subst ?=> Block): Block = - Subst.newScope.givenIn: - val body = mkBlock - val scopedSyms = subst.definedSyms ++ definedSymsInElaborated - // println(subst.definedSyms) - // println(definedSymsInElaborated) - if scopedSyms.isEmpty then body else Scoped(scopedSyms, body) def program(main: st.Blk): Program = val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) - val blk = - inScopedBlock(main.stats.flatMap(_.definedSyms).toSet)(using Subst.empty): - block(funs ::: rest, R(main.res))(ImplctRet) + val blk = block(funs ::: rest, R(main.res))(ImplctRet)(using Subst.empty) val desug = LambdaRewriter.desugar(blk) @@ -967,6 +948,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupSelection(prefix: Term, nme: Tree.Ident, sym: Opt[FieldSymbol])(k: Result => Block)(using Subst): Block = subTerm(prefix): p => + val selRes = TempSymbol(N, "selRes") k(Select(p, nme)(sym)) final def setupFunctionOrByNameDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) @@ -978,8 +960,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using Subst): (List[ParamList], Block) = - val scopedBody = inScopedBlock(bodyTerm.definedSyms)(returnedTerm(bodyTerm)) - (paramLists, scopedBody) + (paramLists, returnedTerm(bodyTerm)) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = annotations.foreach: diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala index 87dc6cea0d..42d25ca5a2 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/SimpleSplit.scala @@ -21,11 +21,6 @@ enum SimpleSplit extends AutoLocated with ProductWithTail: case Else(default: Term)(val kw: Opt[Keywrd[`else`.type | `then`.type | `do`.type]]) case End - def definedSyms: Set[Symbol] = this match - case Cons(branch, tail) => branch.definedSyms - case _ => Set.empty - - inline def ~:(head: SimpleSplit.Head): Cons = Cons(head, this) def ~~:(front: SimpleSplit): SimpleSplit = @@ -89,11 +84,6 @@ object SimpleSplit: case Match(scrutinee: Term.Ref, pattern: Pattern, consequent: SimpleSplit) case Let(binding: BlockLocalSymbol, term: Term) - def definedSyms: Set[Symbol] = this match - case Let(binding, term) => Set(binding) ++ term.definedSyms - case _ => Set.empty - - def subTerms: Ls[Term] = this match case Match(scrutinee, pattern, consequent) => scrutinee :: pattern.subTerms ::: consequent.subTerms diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index b35b82f253..695e40453b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,12 +24,6 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End - def defineSyms: Set[Symbol] = this match - case Let(sym, term, tail) => Set(sym) ++ term.definedSyms ++ tail.defineSyms - case _ => Set.empty - - - inline def ~:(head: Branch): Split = Split.Cons(head, this) def mkClone(using State): Split = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 7f6bd056a6..da9a546848 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -364,18 +364,19 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdSpread(rcd) => RcdSpread(rcd.mkClone) case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) - // NOTE: for `codegen.Block.Scoped` - // that is why this lazy val "flattens" things like - // the `rhs` in `DefinedVars` and `Assgn` already lazy val definedSyms: Set[Symbol] = this match case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty - case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) case LetDecl(sym, annotations) => Set(sym) - case termdef: TermDefinition => Set(termdef.sym) - case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) - case imp: Import => Set(imp.sym) - case IfLike(_, split) => split.definedSyms - case SynthIf(split) => split.defineSyms + // TODO: make sure + case tdef: TermDefinition => Set(tdef.sym) + case ModuleOrObjectDef(owner, sym, bsym, tparams, paramsOpt, auxParams, ext, kind, body, companion, annotations) => Set(sym) + case PatternDef(owner, sym, bsym, tparams, parameters, patternParams, extractionParams, pattern, annotations) => Set(bsym) + case ClassDef.Parameterized(owner, kind, sym, bsym, tparams, params, auxParams, ext, body, companion, annotations) => Set(bsym) + case ClassDef.Plain(owner, kind, sym, bsym, tparams, ext, body, companion, annotations) => Set(bsym) + case TypeDef(sym, bsym, tparams, rhs, companion, annotations) => Set(bsym) + case Import(sym, str, file) => Set(sym) + // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. // And including the sym here may cause error for delayed init in a function: // ``` @@ -383,13 +384,7 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: // fun f() = // x = 2 // ``` - // the definedSyms of the rhs of these two cases should be included - // because those needs to be included in the - // same current `Scoped` anyway - case DefineVar(_, rhs) => rhs.definedSyms - case Assgn(_, rhs) => rhs.definedSyms - - + // case DefineVar(sym, rhs) => Set(sym) case _ => Set.empty // TODO: add other cases def describe: Str = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index bbd82d59b4..773b73eb5b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -247,10 +247,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e End() ) pat match - case FlatPattern.Lit(lit) => mkMatch( - Case.Lit(lit), - lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) - ) + case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = @@ -260,15 +257,10 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using Subst): Case -> Block = args match case Nil => - Case.Cls(ctorSym, st) -> locally: - // println(s":::::: ${tail.showAsTree} ${tail.defineSyms}") - lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false)) + Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) case (param, arg) :: args => val (cse, blk) = mkArgs(args) - cse -> locally: - blk match - case Scoped(syms, blk) => Scoped(syms ++ Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) - case _ => Scoped(Set(arg), Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) // Select the constructor's `.class` field. lazy val ctorTerm = ctor.symbol match @@ -291,25 +283,21 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctorTerm)(k(cls, cls.tree.clsParams)) case mod: ModuleOrObjectSymbol => subTerm_nonTail(ctorTerm)(k(mod, Nil)) - case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))) + case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.Record(entries) => val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - entries.foldRight(lowering.inScopedBlock(tail.defineSyms)(lowerSplit(tail, cont, topLevel = false))): + entries.foldRight(lowerSplit(tail, cont, topLevel = false)): case ((fieldName, fieldSymbol), blk) => mkMatch( Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - blk match - case Scoped(syms, blk) => - Scoped(syms ++ Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) - case _ => - Scoped(Set(fieldSymbol), Assign(fieldSymbol, Select(sr, fieldName)(N), blk)) + Assign(fieldSymbol, Select(sr, fieldName)(N), blk) ) ) case Split.Else(els) => labels.get(els) match case S(label) => Break(label) - case N => lowering.inScopedBlock(els.definedSyms)(term_nonTail(els)(cont.fold(identity, _(topLevel)))) + case N => term_nonTail(els)(cont.fold(identity, _(topLevel))) case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) /** @@ -405,8 +393,6 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e Label(rootBreakLabel, false, mainBlock, End()) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) - if usesResTmp then - Subst.subst.definedSyms.add(l) val block = if kw === `while` then Begin(Label(loopLabel, true, body, End()), rest) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index f6128afab9..8c0a51a762 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -1,176 +1,44 @@ :js -:lot -module M -class C -object O -//│ Lowered: -//│ Program: -//│ imports = Nil -//│ main = Scoped(member:C__593, member:M__594, member:O__595): -//│ Define: -//│ defn = ClsLikeDefn: -//│ owner = N -//│ isym = class:M__604 -//│ sym = member:M__594 -//│ k = Cls -//│ paramsOpt = N -//│ auxParams = Nil -//│ parentPath = N -//│ methods = Nil -//│ privateFields = Nil -//│ publicFields = Nil -//│ preCtor = End of "" -//│ ctor = Return: -//│ res = Select{object:Unit}: -//│ qual = Ref of $runtime__5 -//│ name = Ident of "Unit" -//│ implct = true -//│ companion = S of ClsLikeBody: -//│ isym = module:M__596 -//│ methods = Nil -//│ privateFields = Nil -//│ publicFields = Nil -//│ ctor = End of "" -//│ bufferable = N -//│ rest = Define: \ -//│ defn = ClsLikeDefn: -//│ owner = N -//│ isym = class:C__598 -//│ sym = member:C__593 -//│ k = Cls -//│ paramsOpt = N -//│ auxParams = Nil -//│ parentPath = N -//│ methods = Nil -//│ privateFields = Nil -//│ publicFields = Nil -//│ preCtor = End of "" -//│ ctor = End of "" -//│ companion = N -//│ bufferable = N -//│ rest = Define: \ -//│ defn = ClsLikeDefn: -//│ owner = N -//│ isym = object:O__600 -//│ sym = member:O__595 -//│ k = Obj -//│ paramsOpt = N -//│ auxParams = Nil -//│ parentPath = N -//│ methods = Nil -//│ privateFields = Nil -//│ publicFields = Nil -//│ preCtor = End of "" -//│ ctor = End of "" -//│ companion = N -//│ bufferable = N -//│ rest = End of "" - - -// NOTE: only one symbol is in `Scoped` -:lot -module MM -class MM -//│ Lowered: -//│ Program: -//│ imports = Nil -//│ main = Scoped(member:MM__606): -//│ Define: -//│ defn = ClsLikeDefn: -//│ owner = N -//│ isym = class:MM__609 -//│ sym = member:MM__606 -//│ k = Cls -//│ paramsOpt = N -//│ auxParams = Nil -//│ parentPath = N -//│ methods = Nil -//│ privateFields = Nil -//│ publicFields = Nil -//│ preCtor = End of "" -//│ ctor = End of "" -//│ companion = S of ClsLikeBody: -//│ isym = module:MM__607 -//│ methods = Nil -//│ privateFields = Nil -//│ publicFields = Nil -//│ ctor = End of "" -//│ bufferable = N -//│ rest = End of "" - -:lot -let k -val x -fun f -let y -//│ Lowered: -//│ Program: -//│ imports = Nil -//│ main = Scoped(k__615, member:f__613, member:x__614, y__620): -//│ Assign: -//│ lhs = k__615 -//│ rhs = Lit of UnitLit of false -//│ rest = Assign: \ -//│ lhs = y__620 -//│ rhs = Lit of UnitLit of false -//│ rest = End of "" -//│ k = undefined -//│ y = undefined - - - - -// nested lets should not create nested scopes -:lot +// only the `let x` should be declared in the top level of repl? +// now `y` is declared in the "correct" place, but not `tmp` because `tmp` is +// handled by `blockPreamble` +:showRepl +:sjs let x = let y = 1 + 3 * 4 y + 4 -//│ Lowered: -//│ Program: -//│ imports = Nil -//│ main = Scoped($tmp__630, x__623, y__624): -//│ Assign: -//│ lhs = $tmp__630 -//│ rhs = Call: -//│ fun = Ref of builtin:*__36 -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of IntLit of 3 -//│ Arg: -//│ spread = N -//│ value = Lit of IntLit of 4 -//│ rest = Assign: \ -//│ lhs = y__624 -//│ rhs = Call: -//│ fun = Ref of builtin:+__42 -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of IntLit of 1 -//│ Arg: -//│ spread = N -//│ value = Ref of $tmp__630 -//│ rest = Assign: \ -//│ lhs = x__623 -//│ rhs = Call: -//│ fun = Ref of builtin:+__42 -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Ref of y__624 -//│ Arg: -//│ spread = N -//│ value = Lit of IntLit of 4 -//│ rest = End of "" +//│ JS (unsanitized): +//│ let tmp; let x; let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ REPL> Sending: block$res1 = undefined +//│ REPL> Collected: +//│ > undefined +//│ REPL> Sending: let tmp;let x;try { let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; block$res1 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Collected: +//│ > undefined +//│ REPL> Parsed: +//│ > undefined +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Collected: +//│ > undefined +//│ > Unit {} +//│ REPL> Parsed: +//│ > undefined +//│ > Unit {} +//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Collected: +//│ > 17 +//│ > Unit {} +//│ REPL> Parsed: +//│ > 17 +//│ > Unit {} //│ x = 17 -// `a` and `b` is declared inside the branches (some other passes on the block ir may need to merge them) +// `a` and `b` is declared in the correct places :sjs fun f() = if true then @@ -180,8 +48,8 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f1; -//│ f1 = function f() { +//│ let f; +//│ f = function f() { //│ let scrut; //│ scrut = true; //│ if (scrut === true) { let a; a = 4; return a + 4 } else { let b; b = 5; return b + 9 } @@ -193,7 +61,8 @@ fun f() = // `let a` is declared in the correct place, but not `lambda` -// TODO: this is because this `lambda` symbol is not created during lowering, but in a later pass called "LambdaRewriter" +// NOTE: `tmp1` and `tmp2` should indeed be declared in the top level of the function body +// but not `lambda` :sjs fun g(x, y, z) = 3 + 4 * if @@ -204,15 +73,14 @@ fun g(x, y, z) = (_ + a) //│ JS (unsanitized): //│ let g; -//│ g = function g(x2, y2, z) { -//│ let lambda; -//│ let tmp1, tmp2; +//│ g = function g(x1, y1, z) { +//│ let tmp1, tmp2, lambda; //│ split_root$: { //│ split_1$: { -//│ if (x2 === true) { +//│ if (x1 === true) { //│ break split_1$ //│ } else { -//│ if (y2 === true) { +//│ if (y1 === true) { //│ break split_1$ //│ } else { //│ if (z === true) { @@ -236,71 +104,22 @@ fun g(x, y, z) = //│ }; - -// the tempvar for `f()` is in the nested scope -:sjs -fun f() = - while x && y do - f() + 4 -//│ JS (unsanitized): -//│ let f2; -//│ f2 = function f() { -//│ let scrut, lambda, tmp1; -//│ tmp2: while (true) { -//│ lambda = (undefined, function () { -//│ return y -//│ }); -//│ scrut = runtime.short_and(x1, lambda); -//│ if (scrut === true) { -//│ let tmp3; -//│ tmp3 = f2(); -//│ tmp1 = tmp3 + 4; -//│ continue tmp2 -//│ } else { tmp1 = runtime.Unit; } -//│ break; -//│ } -//│ return tmp1 -//│ }; - - - -data class A(x, y) -data class B(y, z) - - -// `argument$` and `a, y, z` are all in nested scopes -:sjs -fun g() = if x is - A(a, B(y, z)) then false -//│ JS (unsanitized): -//│ let g1; -//│ g1 = function g() { -//│ let tmp1; -//│ split_root$: { -//│ split_default$: { -//│ if (x1 instanceof A.class) { -//│ let argument0$, argument1$; -//│ argument0$ = x1.x; -//│ argument1$ = x1.y; -//│ if (argument1$ instanceof B.class) { -//│ let y2, a, z, argument0$1, argument1$1; -//│ argument0$1 = argument1$.y; -//│ argument1$1 = argument1$.z; -//│ z = argument1$1; -//│ y2 = argument0$1; -//│ a = argument0$; -//│ tmp1 = false; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } -//│ } else { -//│ break split_default$ -//│ } -//│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); -//│ } -//│ return tmp1 -//│ }; - +// TODO: +:e +module M +//│ FAILURE: Unexpected exception +//│ /!!!\ Uncaught error: java.lang.AssertionError: assertion failed: (module:M,Scope: +//│ parent = N +//│ curThis = S of S of globalThis:globalThis +//│ bindings = HashMap($block$res -> block$res4, class:M -> M1, $block$res -> block$res2, $runtime -> runtime, member:g -> g, $definitionMetadata -> definitionMetadata, $prettyPrint -> prettyPrint, $Term -> Term, $Block -> Block, $Shape -> Shape, $block$res -> block$res, x -> x, y -> y, member:Predef -> Predef, $block$res -> block$res3, $block$res -> block$res1, $tmp -> tmp, member:f -> f, module:M -> M)) +//│ at: scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) +//│ at: hkmc2.utils.Scope.addToBindings(Scope.scala:50) +//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$2(JSBuilder.scala:556) +//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$adapted$1(JSBuilder.scala:560) +//│ at: hkmc2.utils.TraceLogger.trace(TraceLogger.scala:17) +//│ at: hkmc2.codegen.js.JSBuilder.go$1(JSBuilder.scala:560) +//│ at: hkmc2.codegen.js.JSBuilder.reserveNames(JSBuilder.scala:561) +//│ at: hkmc2.codegen.js.JSBuilder.worksheet(JSBuilder.scala:589) +//│ at: hkmc2.JSBackendDiffMaker.processTerm(JSBackendDiffMaker.scala:155) +//│ at: hkmc2.BbmlDiffMaker.processTerm(BbmlDiffMaker.scala:36) diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index e8def6150f..2c9c910fd6 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -110,7 +110,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: val lowered0 = low.program(blk) // TODO: remove mutation - var toplvlDefinedVars = collection.Set.empty[Symbol] + var toplvlDefinedVars = Set.empty[Symbol] def assignResSym(b: Block, toplvl: Boolean): Block = b.mapTail: case e: End => From 021c1a00a354f941db9348a733d7ac93cf3be246 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 17:45:34 +0800 Subject: [PATCH 15/72] use collection.Set --- hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala | 2 +- hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index e6bc92211d..552ed41cb0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -283,7 +283,7 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: Set[Local], body: Block) extends BlockTail +case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 02b5956d8d..08c772d47d 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -110,7 +110,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: val lowered0 = low.program(blk) // TODO: remove mutation - var toplvlDefinedVars = Set.empty[Symbol] + var toplvlDefinedVars = collection.Set.empty[Symbol] def assignResSym(b: Block, toplvl: Boolean): Block = b.mapTail: case e: End => From 0d4ae0549abd6b950a577714535e8c81b17643bd Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 19:41:30 +0800 Subject: [PATCH 16/72] wip: a single if in a nested block..? --- .../main/scala/hkmc2/codegen/Lowering.scala | 27 ++++++++++++++----- .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../main/scala/hkmc2/semantics/Split.scala | 16 +++++++++++ .../src/main/scala/hkmc2/semantics/Term.scala | 22 +++++++-------- .../hkmc2/semantics/ucs/Normalization.scala | 16 +++++++---- 5 files changed, 60 insertions(+), 23 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 98b20970e2..5976bb45d4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -36,8 +36,15 @@ object Thrw extends TailOp: // * No longer in meaningful use and could be removed if we don't find a use for it: -class LoweringCtx(initMap: Map[Local, Value], val mayRet: Bool): +class LoweringCtx( + initMap: Map[Local, Value], + val mayRet: Bool, + private val definedSymsDuringLowering: collection.mutable.Set[Symbol] +): val map = initMap + + def collectScopedSym(s: Symbol) = definedSymsDuringLowering.add(s) + def getCollectedSym: collection.Set[Symbol] = definedSymsDuringLowering /* def +(kv: (Local, Value)): Subst = kv match @@ -50,9 +57,10 @@ class LoweringCtx(initMap: Map[Local, Value], val mayRet: Bool): case Value.Ref(l, _) => map.getOrElse(l, v) case _ => v object LoweringCtx: - val empty = LoweringCtx(Map.empty, false) + val empty = LoweringCtx(Map.empty, false, collection.mutable.Set.empty) def subst(using sub: LoweringCtx): LoweringCtx = sub - def nestFunc(using sub: LoweringCtx): LoweringCtx = LoweringCtx(sub.map, true) + def nestFunc(using sub: LoweringCtx): LoweringCtx = LoweringCtx(sub.map, true, sub.definedSymsDuringLowering) + def nestScoped(using sub: LoweringCtx): LoweringCtx = LoweringCtx(sub.map, sub.mayRet, collection.mutable.Set.empty) end LoweringCtx import LoweringCtx.subst @@ -124,8 +132,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using LoweringCtx): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) - Scoped(definedVars.toSet, blockImpl(imps ::: funs ::: rest, res)(k)) + // val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) + blockImpl(imps ::: funs ::: rest, res)(k) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using LoweringCtx): Block = stats match @@ -995,9 +1003,16 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case ps => ps setupFunctionDef(physicalParams, bodyTerm, name) + def inScopedBlock(definedSymsInElaborated: Set[Symbol])(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = + LoweringCtx.nestScoped.givenIn: + val body = mkBlock + val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated + if scopedSyms.isEmpty then body else Scoped(scopedSyms, body) + def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = - (paramLists, returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(bodyTerm.definedSyms)(returnedTerm(bodyTerm)) + (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = def warn(annot: Annot, msg: Opt[Message] = N) = raise: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 7fbf6d2080..d699b94f32 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -521,7 +521,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") - :: doc";") :: returningTerm(body, endSemi) + :: doc"; /** scoped **/") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 695e40453b..8746d65a24 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,6 +24,22 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End + def definedSyms: Set[Symbol] = this match + case Cons(Branch(scrutinee, pattern, continuation), tail) => + tail.definedSyms ++ + locally: + continuation.definedSyms ++ + locally: + pattern match + case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) + case FlatPattern.Tuple(size, inf) => Set.empty // TODO: + case FlatPattern.Record(entries) => entries.unzip._2 + case FlatPattern.Lit(_) => Set.empty + case Let(sym, term, tail) => term.definedSyms ++ tail.definedSyms + sym + case Else(default) => default.definedSyms + case End => Set.empty + + inline def ~:(head: Branch): Split = Split.Cons(head, this) def mkClone(using State): Split = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index ea0352d28e..970e64d2b1 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -402,17 +402,13 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: lazy val definedSyms: Set[Symbol] = this match case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty - case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) case LetDecl(sym, annotations) => Set(sym) - // TODO: make sure - case tdef: TermDefinition => Set(tdef.sym) - case ModuleOrObjectDef(owner, sym, bsym, tparams, paramsOpt, auxParams, ext, kind, body, companion, annotations) => Set(sym) - case PatternDef(owner, sym, bsym, tparams, parameters, patternParams, extractionParams, pattern, annotations) => Set(bsym) - case ClassDef.Parameterized(owner, kind, sym, bsym, tparams, params, auxParams, ext, body, companion, annotations) => Set(bsym) - case ClassDef.Plain(owner, kind, sym, bsym, tparams, ext, body, companion, annotations) => Set(bsym) - case TypeDef(sym, bsym, tparams, rhs, companion, annotations) => Set(bsym) - case Import(sym, str, file) => Set(sym) - + case termdef: TermDefinition => Set(termdef.sym) + case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) + case imp: Import => Set(imp.sym) + // IfLikes get their own scope + case _: (IfLike | SynthIf) => Set.empty // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. // And including the sym here may cause error for delayed init in a function: // ``` @@ -420,7 +416,11 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: // fun f() = // x = 2 // ``` - // case DefineVar(sym, rhs) => Set(sym) + // the definedSyms of the rhs of these two cases should be included + // because those needs to be included in the + // same current `Scoped` anyway + case DefineVar(_, rhs) => rhs.definedSyms + case Assgn(_, rhs) => rhs.definedSyms case _ => Set.empty // TODO: add other cases def describe: Str = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 066c807d2d..9f816131a2 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -315,7 +315,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e def apply(split: Split)(k: Result => Block)(using Config, LoweringCtx): Block = this(split, `if`, N, k) - private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using Config, LoweringCtx) = + private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = LoweringCtx.nestScoped.givenIn: var usesResTmp = false // The symbol of the temporary variable for the result of the `if`-like term. // It will be created in one of the following situations. @@ -324,7 +324,9 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // 3. The term is a `while` and the result is used. lazy val l = usesResTmp = true - new TempSymbol(t) + val res = new TempSymbol(t) + outerCtx.collectScopedSym(res) + res // The symbol for the loop label if the term is a `while`. lazy val loopLabel = new TempSymbol(t) lazy val f = new BlockMemberSymbol("while", Nil, false) @@ -385,8 +387,12 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e case N => innerBlock // If there are shared consequents, we need a wrap the entire block in a // `Label` so that `Break`s in the shared consequents can jump to the end. - val body = if labels.isEmpty then mainBlock else - Label(rootBreakLabel, false, mainBlock, End()) + val body = + val beforeScoped = if labels.isEmpty then mainBlock else + Label(rootBreakLabel, false, mainBlock, End()) + Scoped( + LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + beforeScoped) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = @@ -397,7 +403,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e val loopEnd: Path = Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) val blk = blockBuilder - .assign(l, Value.Lit(Tree.UnitLit(false))) + // .assign(l, Value.Lit(Tree.UnitLit(false))) .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) if summon[LoweringCtx].mayRet then From f4ccdf57a22840e2328486dd78a3a876db5d5e14 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 19:55:41 +0800 Subject: [PATCH 17/72] wip --- .../shared/src/main/scala/hkmc2/codegen/Lowering.scala | 5 ++++- .../main/scala/hkmc2/semantics/ucs/Normalization.scala | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 5976bb45d4..620f5e3e7e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -1007,7 +1007,10 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - if scopedSyms.isEmpty then body else Scoped(scopedSyms, body) + possiblyScoped(scopedSyms, body) + + inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = + if syms.isEmpty then body else Scoped(syms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 9f816131a2..871bc52e52 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -388,11 +388,11 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // If there are shared consequents, we need a wrap the entire block in a // `Label` so that `Break`s in the shared consequents can jump to the end. val body = - val beforeScoped = if labels.isEmpty then mainBlock else - Label(rootBreakLabel, false, mainBlock, End()) - Scoped( - LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - beforeScoped) + val possiblyScoped = + lowering.possiblyScoped( + LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + mainBlock) + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = From e796c84fcab5f5041d4d34d7fcd0b65d6d4f1b0d Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 20:12:22 +0800 Subject: [PATCH 18/72] many collection of symbols created during lowering --- .../main/scala/hkmc2/codegen/Lowering.scala | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 620f5e3e7e..4954ba88fe 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -43,7 +43,7 @@ class LoweringCtx( ): val map = initMap - def collectScopedSym(s: Symbol) = definedSymsDuringLowering.add(s) + def collectScopedSym(s: Symbol*) = definedSymsDuringLowering.addAll(s) def getCollectedSym: collection.Set[Symbol] = definedSymsDuringLowering /* def +(kv: (Local, Value)): Subst = @@ -132,7 +132,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def block(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using LoweringCtx): Block = // TODO we should also isolate and reorder classes by inheritance topological sort val (imps, funs, rest) = splitBlock(stats, Nil, Nil, Nil) - // val definedVars = imps.flatMap(_.definedSyms) ::: funs.flatMap(_.definedSyms) ::: rest.flatMap(_.definedSyms) blockImpl(imps ::: funs ::: rest, res)(k) def blockImpl(stats: Ls[Statement], res: Rcd \/ Term)(k: Result => Block)(using LoweringCtx): Block = @@ -404,6 +403,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): // * (non-local functions are compiled into getter methods selected on some prefix) if td.params.isEmpty then val l = new TempSymbol(S(ref)) + subst.collectScopedSym(l) return Assign(l, Call(Value.Ref(bs, disamb).withLocOf(ref), Nil)(true, true, annots.contains(Annot.TailCall)), k(Value.Ref(l, disamb))) case S(_) => () case N => () // TODO panic here; can only lower refs to elab'd symbols @@ -477,6 +477,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val isOr = sym is State.orSymbol if isAnd || isOr then val lamSym = BlockMemberSymbol("lambda", Nil, false) + subst.collectScopedSym(lamSym) val lamDef = FunDefn.withFreshSymbol(N, lamSym, PlainParamList(Nil) :: Nil, returnedTerm(arg2))(isTailRec = false) Define( lamDef, @@ -572,6 +573,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): S(Handler(td.sym, resumeSym, paramLists, bodyBlock)) }.collect{ case Some(v) => v } val resSym = TempSymbol(S(t)) + subst.collectScopedSym(resSym) subTerm(rhs): par => subTerms(as): asr => HandleBlock(lhs, resSym, par, asr, cls, handlers, @@ -608,6 +610,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): then k(Lambda(paramLists.head, bodyBlock)) else val lamSym = new BlockMemberSymbol("lambda", Nil, false) + subst.collectScopedSym(lamSym) val lamDef = FunDefn.withFreshSymbol(N, lamSym, paramLists, bodyBlock)(isTailRec = false) Define( lamDef, @@ -658,11 +661,14 @@ class Lowering()(using Config, TL, Raise, State, Ctx): inner => lowerArg(arg): asr2 => val ts = TempSymbol(N) + subst.collectScopedSym(ts) Assign(ts, Call(inner, asr2)(true, true, false), acc(Value.Ref(ts))) val ts = TempSymbol(N) + subst.collectScopedSym(ts) Assign(ts, Instantiate(mut, sr, asr), z(Value.Ref(ts))) case S((isym, rft)) => val sym = new BlockMemberSymbol(isym.name, Nil) + subst.collectScopedSym(sym) val (mtds, publicFlds, privateFlds, ctor) = gatherMembers(rft) val pctor = parentConstructor(cls, as) val clsDef = ClsLikeDefn(N, isym, sym, syntax.Cls, N, Nil, S(sr), @@ -672,6 +678,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case Try(sub, finallyDo) => val l = new TempSymbol(S(sub)) + subst.collectScopedSym(l) TryBlock( subTerm_nonTail(sub)(p => Assign(l, p, End())), subTerm_nonTail(finallyDo)(_ => End()), @@ -744,6 +751,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): source = Diagnostic.Source.Compilation ) + // NOTE: nothing in `quote...` is handled yet def quoteSplit(split: Split)(k: Result => Block)(using LoweringCtx): Block = split match case Split.Cons(Branch(scrutinee, pattern, continuation), tail) => quote(scrutinee): r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) @@ -919,6 +927,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): Begin(b, k(asr.reverse)) else val rcdSym = new TempSymbol(N, "rcd") + subst.collectScopedSym(rcdSym) Begin( b, Assign( @@ -948,10 +957,12 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case p: Path => k(p) case Lambda(params, body) => val lamSym = BlockMemberSymbol("lambda", Nil, false) + subst.collectScopedSym(lamSym) val lamDef = FunDefn.withFreshSymbol(N, lamSym, params :: Nil, body)(isTailRec = false) Define(lamDef, k(Value.Ref(lamSym, N))) case r => val l = new TempSymbol(N) + subst.collectScopedSym(l) Assign(l, r, k(l |> Value.Ref.apply)) @@ -1063,6 +1074,7 @@ trait LoweringSelSanityChecks(using Config, TL, Raise, State) if !instrument then return super.setupSelection(prefix, nme, disamb)(k) subTerm(prefix): p => val selRes = TempSymbol(N, "selRes") + subst.collectScopedSym(selRes) // * We are careful to access `x.f` before `x.f$__checkNotMethod` in case `x` is, eg, `undefined` and // * the access should throw an error like `TypeError: Cannot read property 'f' of undefined`. val b0 = blockBuilder @@ -1070,14 +1082,17 @@ trait LoweringSelSanityChecks(using Config, TL, Raise, State) (if disamb.isDefined then // * If the symbol is known, the resolver will have already checked the access [invariant:1] b0 - else b0 - .assign(TempSymbol(N, "discarded"), Select(p, Tree.Ident(nme.name+"$__checkNotMethod"))(N))) - .ifthen(selRes.asPath, - Case.Lit(syntax.Tree.UnitLit(false)), - Throw(Instantiate(mut = false, Select(Value.Ref(State.globalThisSymbol), Tree.Ident("Error"))(N), - Value.Lit(syntax.Tree.StrLit(s"Access to required field '${nme.name}' yielded 'undefined'")).asArg :: Nil)) - ) - .rest(k(selRes.asPath)) + else + val discardedSym = TempSymbol(N, "discarded") + subst.collectScopedSym(discardedSym) + b0 + .assign(discardedSym, Select(p, Tree.Ident(nme.name+"$__checkNotMethod"))(N))) + .ifthen(selRes.asPath, + Case.Lit(syntax.Tree.UnitLit(false)), + Throw(Instantiate(mut = false, Select(Value.Ref(State.globalThisSymbol), Tree.Ident("Error"))(N), + Value.Lit(syntax.Tree.StrLit(s"Access to required field '${nme.name}' yielded 'undefined'")).asArg :: Nil)) + ) + .rest(k(selRes.asPath)) @@ -1129,12 +1144,21 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg") val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") + subst.collectScopedSym( + enterMsgSym, + prevIndentLvlSym, + resSym, + retMsgSym, + resInspectedSym) + subst.collectScopedSym(psInspectedSyms.unzip._2*) val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 then Arg(N, Value.Ref(s)) :: acc else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc + val tmp1, tmp2, tmp3 = TempSymbol(N) + subst.collectScopedSym(tmp1, tmp2, tmp3) assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) *) |>: @@ -1143,7 +1167,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) strConcatFn, Arg(N, Value.Lit(Tree.StrLit(s"CALL ${name.getOrElse("[arrow function]")}("))) :: psSymArgs ), - TempSymbol(N) -> pureCall(traceLogFn, Arg(N, Value.Ref(enterMsgSym)) :: Nil), + tmp1 -> pureCall(traceLogFn, Arg(N, Value.Ref(enterMsgSym)) :: Nil), prevIndentLvlSym -> pureCall(traceLogIndentFn, Nil) ) |>: term(bod)(r => @@ -1154,8 +1178,8 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) strConcatFn, Arg(N, Value.Lit(Tree.StrLit("=> "))) :: Arg(N, Value.Ref(resInspectedSym)) :: Nil ), - TempSymbol(N) -> pureCall(traceLogResetFn, Arg(N, Value.Ref(prevIndentLvlSym)) :: Nil), - TempSymbol(N) -> pureCall(traceLogFn, Arg(N, Value.Ref(retMsgSym)) :: Nil) + tmp2 -> pureCall(traceLogResetFn, Arg(N, Value.Ref(prevIndentLvlSym)) :: Nil), + tmp3 -> pureCall(traceLogFn, Arg(N, Value.Ref(retMsgSym)) :: Nil) ) |>: Ret(Value.Ref(resSym)) )(using LoweringCtx.nestFunc) From 95c3c12a3497a6f5ffacb54d45910aaf191be8f9 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 20:29:46 +0800 Subject: [PATCH 19/72] forgot to collect some symbols and create some scoped blocks --- .../main/scala/hkmc2/codegen/Lowering.scala | 4 +- .../hkmc2/semantics/ucs/Normalization.scala | 6 +- .../src/test/mlscript/codegen/Scoped.mls | 237 +++++++++++++----- 3 files changed, 187 insertions(+), 60 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 4954ba88fe..4e83aa6f0c 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -970,7 +970,9 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) - val blk = block(funs ::: rest, R(main.res))(ImplctRet)(using LoweringCtx.empty) + val blk = + inScopedBlock(main.stats.flatMap(_.definedSyms).toSet)(using LoweringCtx.empty): + block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 871bc52e52..70176ed9e7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -329,7 +329,10 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e res // The symbol for the loop label if the term is a `while`. lazy val loopLabel = new TempSymbol(t) - lazy val f = new BlockMemberSymbol("while", Nil, false) + lazy val f = + val res = new BlockMemberSymbol("while", Nil, false) + outerCtx.collectScopedSym(res) + res val normalized = tl.scoped("ucs:normalize"): normalize(inputSplit)(using VarSet()) tl.scoped("ucs:normalized"): @@ -400,6 +403,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e if config.rewriteWhileLoops then val loopResult = TempSymbol(N) val isReturned = TempSymbol(N) + outerCtx.collectScopedSym(loopResult, isReturned) val loopEnd: Path = Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) val blk = blockBuilder diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 8c0a51a762..ddaee39984 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -1,44 +1,15 @@ :js -// only the `let x` should be declared in the top level of repl? -// now `y` is declared in the "correct" place, but not `tmp` because `tmp` is -// handled by `blockPreamble` -:showRepl :sjs let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let tmp; let x; let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; -//│ REPL> Sending: block$res1 = undefined -//│ REPL> Collected: -//│ > undefined -//│ REPL> Sending: let tmp;let x;try { let y; tmp = 3 * 4; y = 1 + tmp; x = y + 4; block$res1 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } -//│ REPL> Collected: -//│ > undefined -//│ REPL> Parsed: -//│ > undefined -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(block$res1)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } -//│ REPL> Collected: -//│ > undefined -//│ > Unit {} -//│ REPL> Parsed: -//│ > undefined -//│ > Unit {} -//│ REPL> Sending: try { runtime.checkCall(runtime.printRaw(x)) } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } -//│ REPL> Collected: -//│ > 17 -//│ > Unit {} -//│ REPL> Parsed: -//│ > 17 -//│ > Unit {} +//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 - - -// `a` and `b` is declared in the correct places :sjs fun f() = if true then @@ -48,21 +19,19 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f() { -//│ let scrut; +//│ let while1; /** scoped **/ +//│ let scrut, a, b; /** scoped **/ //│ scrut = true; -//│ if (scrut === true) { let a; a = 4; return a + 4 } else { let b; b = 5; return b + 9 } +//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; - - - -// `let a` is declared in the correct place, but not `lambda` +// `let a` is declared in the correct place, +// but not `lambda` (because this is created in `LambdaRewriter`, a later pass) // NOTE: `tmp1` and `tmp2` should indeed be declared in the top level of the function body -// but not `lambda` :sjs fun g(x, y, z) = 3 + 4 * if @@ -72,10 +41,12 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; +//│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { -//│ let tmp1, tmp2, lambda; +//│ let lambda; +//│ let while1, tmp1, tmp2; /** scoped **/ //│ split_root$: { +//│ let a; /** scoped **/ //│ split_1$: { //│ if (x1 === true) { //│ break split_1$ @@ -84,7 +55,6 @@ fun g(x, y, z) = //│ break split_1$ //│ } else { //│ if (z === true) { -//│ let a; //│ a = 1; //│ lambda = (undefined, function (_0) { //│ return _0 + a @@ -104,22 +74,173 @@ fun g(x, y, z) = //│ }; -// TODO: -:e module M -//│ FAILURE: Unexpected exception -//│ /!!!\ Uncaught error: java.lang.AssertionError: assertion failed: (module:M,Scope: -//│ parent = N -//│ curThis = S of S of globalThis:globalThis -//│ bindings = HashMap($block$res -> block$res4, class:M -> M1, $block$res -> block$res2, $runtime -> runtime, member:g -> g, $definitionMetadata -> definitionMetadata, $prettyPrint -> prettyPrint, $Term -> Term, $Block -> Block, $Shape -> Shape, $block$res -> block$res, x -> x, y -> y, member:Predef -> Predef, $block$res -> block$res3, $block$res -> block$res1, $tmp -> tmp, member:f -> f, module:M -> M)) -//│ at: scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) -//│ at: hkmc2.utils.Scope.addToBindings(Scope.scala:50) -//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$2(JSBuilder.scala:556) -//│ at: hkmc2.codegen.js.JSBuilder.go$1$$anonfun$adapted$1(JSBuilder.scala:560) -//│ at: hkmc2.utils.TraceLogger.trace(TraceLogger.scala:17) -//│ at: hkmc2.codegen.js.JSBuilder.go$1(JSBuilder.scala:560) -//│ at: hkmc2.codegen.js.JSBuilder.reserveNames(JSBuilder.scala:561) -//│ at: hkmc2.codegen.js.JSBuilder.worksheet(JSBuilder.scala:589) -//│ at: hkmc2.JSBackendDiffMaker.processTerm(JSBackendDiffMaker.scala:155) -//│ at: hkmc2.BbmlDiffMaker.processTerm(BbmlDiffMaker.scala:36) +data class A(a, b) +data class B(a, b) +data object Nil + + +:sjs +fun f() = if x is + A(a, b) and a > b then true + B(c, d) then false +//│ JS (unsanitized): +//│ let f1; /** scoped **/ +//│ f1 = function f() { +//│ let while1, tmp1; /** scoped **/ +//│ split_root$: { +//│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ +//│ split_default$: { +//│ if (x instanceof A.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ b = argument1$; +//│ a = argument0$; +//│ scrut = a > b; +//│ if (scrut === true) { +//│ tmp1 = true; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else if (x instanceof B.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ d = argument1$; +//│ c = argument0$; +//│ tmp1 = false; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ } +//│ return tmp1 +//│ }; + + +:sjs +fun f() = if A(1, Nil) is + A(1, Nil) then 3 +//│ JS (unsanitized): +//│ let f2; /** scoped **/ +//│ f2 = function f() { +//│ let while1, tmp1; /** scoped **/ +//│ split_root$: { +//│ let scrut, argument0$, argument1$; /** scoped **/ +//│ split_default$: { +//│ scrut = A(1, Nil); +//│ if (scrut instanceof A.class) { +//│ argument0$ = scrut.a; +//│ argument1$ = scrut.b; +//│ if (argument0$ === 1) { +//│ if (argument1$ instanceof Nil.class) { +//│ tmp1 = 3; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else { +//│ break split_default$ +//│ } +//│ } else { +//│ break split_default$ +//│ } +//│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ } +//│ return tmp1 +//│ }; + + + +:sjs +fun f(x) = + x = while x is + A(a, _) then a() + B(a, _) then a() + else + x() + x +//│ JS (unsanitized): +//│ let f3; /** scoped **/ +//│ f3 = function f(x1) { +//│ let while1, tmp1, tmp2, tmp3; /** scoped **/ +//│ while1 = (undefined, function () { +//│ let a, a1, argument0$, argument1$; /** scoped **/ +//│ if (x1 instanceof A.class) { +//│ argument0$ = x1.a; +//│ argument1$ = x1.b; +//│ a = argument0$; +//│ tmp1 = runtime.safeCall(a()); +//│ return while1() +//│ } else if (x1 instanceof B.class) { +//│ argument0$ = x1.a; +//│ argument1$ = x1.b; +//│ a1 = argument0$; +//│ tmp1 = runtime.safeCall(a1()); +//│ return while1() +//│ } else { +//│ tmp1 = runtime.safeCall(x1()); +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { x1 = tmp1; return x1 } +//│ }; + + + +:sjs +fun f() = + let y = x + 1 + while y is + A(z, a) and + false and true do + set y = z + 1 + else + y = a +//│ JS (unsanitized): +//│ let f4; /** scoped **/ +//│ f4 = function f() { +//│ let y1, while1, tmp1, tmp2, tmp3; /** scoped **/ +//│ y1 = x + 1; +//│ while1 = (undefined, function () { +//│ split_root$: { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ +//│ split_1$: { +//│ if (y1 instanceof A.class) { +//│ argument0$ = y1.a; +//│ argument1$ = y1.b; +//│ a = argument1$; +//│ z = argument0$; +//│ scrut = false; +//│ if (scrut === true) { +//│ scrut1 = true; +//│ if (scrut1 === true) { +//│ tmp4 = z + 1; +//│ y1 = tmp4; +//│ tmp1 = runtime.Unit; +//│ return while1() +//│ } else { +//│ break split_1$ +//│ } +//│ } else { +//│ break split_1$ +//│ } +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ } +//│ y1 = a; +//│ tmp1 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ }; From 245b9d41856671b898748d215cb559e7d746abc4 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 23 Nov 2025 22:48:26 +0800 Subject: [PATCH 20/72] minor --- .../src/main/scala/hkmc2/semantics/ucs/Normalization.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 70176ed9e7..a7c628df92 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -342,7 +342,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e lazy val rootBreakLabel = new TempSymbol(N, "split_root$") lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) lazy val assignResult = (r: Result) => Assign(l, r, End()) - val loopCont = if config.rewriteWhileLoops + lazy val loopCont = if config.rewriteWhileLoops then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) else Continue(loopLabel) val cont = From 214fc2803b117ce6df0ac2a188541e704280b25b Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Mon, 24 Nov 2025 00:47:00 +0800 Subject: [PATCH 21/72] minor fix --- .../main/scala/hkmc2/codegen/Lowering.scala | 94 ++++++++++--------- .../src/main/scala/hkmc2/semantics/Term.scala | 2 +- .../src/test/mlscript/codegen/Scoped.mls | 11 +-- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 4e83aa6f0c..16d264cf85 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -971,7 +971,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) val blk = - inScopedBlock(main.stats.flatMap(_.definedSyms).toSet)(using LoweringCtx.empty): + inScopedBlock(main.stats.foldLeft(main.res.definedSyms)(_ ++ _.definedSyms))(using LoweringCtx.empty): block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -1140,51 +1140,53 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) go(paramLists.reverse, bod) def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") - val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") - val resSym = TempSymbol(N, dbgNme = "traceLogRes") - val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg") - val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) - val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") - subst.collectScopedSym( - enterMsgSym, - prevIndentLvlSym, - resSym, - retMsgSym, - resInspectedSym) - subst.collectScopedSym(psInspectedSyms.unzip._2*) - - val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): - case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 - then Arg(N, Value.Ref(s)) :: acc - else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc - - val tmp1, tmp2, tmp3 = TempSymbol(N) - subst.collectScopedSym(tmp1, tmp2, tmp3) - assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => - pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) - *) |>: - assignStmts( - enterMsgSym -> pureCall( - strConcatFn, - Arg(N, Value.Lit(Tree.StrLit(s"CALL ${name.getOrElse("[arrow function]")}("))) :: psSymArgs - ), - tmp1 -> pureCall(traceLogFn, Arg(N, Value.Ref(enterMsgSym)) :: Nil), - prevIndentLvlSym -> pureCall(traceLogIndentFn, Nil) - ) |>: - term(bod)(r => - assignStmts( - resSym -> r, - resInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(resSym)) :: Nil), - retMsgSym -> pureCall( - strConcatFn, - Arg(N, Value.Lit(Tree.StrLit("=> "))) :: Arg(N, Value.Ref(resInspectedSym)) :: Nil - ), - tmp2 -> pureCall(traceLogResetFn, Arg(N, Value.Ref(prevIndentLvlSym)) :: Nil), - tmp3 -> pureCall(traceLogFn, Arg(N, Value.Ref(retMsgSym)) :: Nil) - ) |>: - Ret(Value.Ref(resSym)) - )(using LoweringCtx.nestFunc) + inScopedBlock(bod.definedSyms): + val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") + val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") + val resSym = TempSymbol(N, dbgNme = "traceLogRes") + val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg") + val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) + val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") + subst.collectScopedSym( + enterMsgSym, + prevIndentLvlSym, + resSym, + retMsgSym, + resInspectedSym) + subst.collectScopedSym(psInspectedSyms.unzip._1*) + + val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): + case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 + then Arg(N, Value.Ref(s)) :: acc + else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc + + val tmp1, tmp2, tmp3 = TempSymbol(N) + subst.collectScopedSym(tmp1, tmp2, tmp3) + + assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => + pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) + *) |>: + assignStmts( + enterMsgSym -> pureCall( + strConcatFn, + Arg(N, Value.Lit(Tree.StrLit(s"CALL ${name.getOrElse("[arrow function]")}("))) :: psSymArgs + ), + tmp1 -> pureCall(traceLogFn, Arg(N, Value.Ref(enterMsgSym)) :: Nil), + prevIndentLvlSym -> pureCall(traceLogIndentFn, Nil) + ) |>: + term(bod)(r => + assignStmts( + resSym -> r, + resInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(resSym)) :: Nil), + retMsgSym -> pureCall( + strConcatFn, + Arg(N, Value.Lit(Tree.StrLit("=> "))) :: Arg(N, Value.Ref(resInspectedSym)) :: Nil + ), + tmp2 -> pureCall(traceLogResetFn, Arg(N, Value.Ref(prevIndentLvlSym)) :: Nil), + tmp3 -> pureCall(traceLogFn, Arg(N, Value.Ref(retMsgSym)) :: Nil) + ) |>: + Ret(Value.Ref(resSym)) + ) object TrivialStatementsAndMatch: diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 970e64d2b1..26bf577dc4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -406,7 +406,7 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case LetDecl(sym, annotations) => Set(sym) case termdef: TermDefinition => Set(termdef.sym) case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) - case imp: Import => Set(imp.sym) + // case imp: Import => Set(imp.sym) // IfLikes get their own scope case _: (IfLike | SynthIf) => Set.empty // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index ddaee39984..0e0843588a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -21,7 +21,6 @@ fun f() = //│ JS (unsanitized): //│ let f; /** scoped **/ //│ f = function f() { -//│ let while1; /** scoped **/ //│ let scrut, a, b; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } @@ -44,7 +43,7 @@ fun g(x, y, z) = //│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { //│ let lambda; -//│ let while1, tmp1, tmp2; /** scoped **/ +//│ let tmp1, tmp2; /** scoped **/ //│ split_root$: { //│ let a; /** scoped **/ //│ split_1$: { @@ -88,7 +87,7 @@ fun f() = if x is //│ JS (unsanitized): //│ let f1; /** scoped **/ //│ f1 = function f() { -//│ let while1, tmp1; /** scoped **/ +//│ let tmp1; /** scoped **/ //│ split_root$: { //│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ //│ split_default$: { @@ -127,7 +126,7 @@ fun f() = if A(1, Nil) is //│ JS (unsanitized): //│ let f2; /** scoped **/ //│ f2 = function f() { -//│ let while1, tmp1; /** scoped **/ +//│ let tmp1; /** scoped **/ //│ split_root$: { //│ let scrut, argument0$, argument1$; /** scoped **/ //│ split_default$: { @@ -167,7 +166,7 @@ fun f(x) = //│ JS (unsanitized): //│ let f3; /** scoped **/ //│ f3 = function f(x1) { -//│ let while1, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ while1 = (undefined, function () { //│ let a, a1, argument0$, argument1$; /** scoped **/ //│ if (x1 instanceof A.class) { @@ -206,7 +205,7 @@ fun f() = //│ JS (unsanitized): //│ let f4; /** scoped **/ //│ f4 = function f() { -//│ let y1, while1, tmp1, tmp2, tmp3; /** scoped **/ +//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ //│ y1 = x + 1; //│ while1 = (undefined, function () { //│ split_root$: { From 15f35bedcd0d9464a2cf4d7596bd0da74b89c409 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Mon, 24 Nov 2025 00:56:22 +0800 Subject: [PATCH 22/72] fix two tests --- .../test/mlscript/ctx/MissingDefinitions2.mls | 49 ++++--------------- .../mlscript/ups/syntax/MixedParameters.mls | 12 +++-- 2 files changed, 17 insertions(+), 44 deletions(-) diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index 2572b03c45..a58b71139f 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -16,66 +16,37 @@ x val x -:ge + +// :ge x -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' -//│ ║ l.20: x -//│ ║ ^ -//│ ╟── which references the symbol introduced here -//│ ║ l.17: val x -//│ ╙── ^^^^^ val x: Int -:ge +// :ge x -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' -//│ ║ l.32: x -//│ ║ ^ -//│ ╟── which references the symbol introduced here -//│ ║ l.29: val x: Int -//│ ╙── ^^^^^^^^^^ fun p: Int -:ge +:re p -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'p' -//│ ║ l.44: p -//│ ║ ^ -//│ ╟── which references the symbol introduced here -//│ ║ l.41: fun p: Int -//│ ╙── ^^^^^^^^^^ -//│ ═══[RUNTIME ERROR] ReferenceError: p is not defined +//│ ═══[RUNTIME ERROR] TypeError: p is not a function // :ctx fun (++) test: (Int, Int) -> Int -:ge +:re test(1, 2) -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'test' -//│ ║ l.58: test(1, 2) -//│ ║ ^^^^ -//│ ╟── which references the symbol introduced here -//│ ║ l.55: fun (++) test: (Int, Int) -> Int -//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ═══[RUNTIME ERROR] ReferenceError: test is not defined +//│ ═══[RUNTIME ERROR] TypeError: test is not a function :sjs -:ge +:re 1 ++ 1 -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'test' -//│ ║ l.69: 1 ++ 1 -//│ ║ ^^ -//│ ╟── which references the symbol introduced here -//│ ║ l.55: fun (++) test: (Int, Int) -> Int -//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let tmp2; tmp2 = test(); tmp2(1, 1) -//│ ═══[RUNTIME ERROR] ReferenceError: test is not defined +//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) +//│ ═══[RUNTIME ERROR] TypeError: test is not a function declare class C diff --git a/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls b/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls index 1b18c8c894..45139d43b0 100644 --- a/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls +++ b/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls @@ -38,13 +38,14 @@ if [0, 0] is P'(Bit, a, b) then "values: " + a + ", " + b //│ ═══[RUNTIME ERROR] Error: match error :fixme +:re // Order actually matters. But it seems that I didn't implement the error // reporting correctly. if [0, 0] is P(a, Bit, b) then "values: " + a + ", " + b //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'a' -//│ ║ l.43: if [0, 0] is P(a, Bit, b) then "values: " + a + ", " + b +//│ ║ l.44: if [0, 0] is P(a, Bit, b) then "values: " + a + ", " + b //│ ╙── ^ -//│ = "values: 0, 0" +//│ ═══[RUNTIME ERROR] ReferenceError: a is not defined pattern S(a, pattern Q, b) = [Q as a, Q as b] @@ -57,11 +58,12 @@ f([0, 0]) // Order actually matters. fun f(x) = if x is S(Bit, a, b) then [a, b] //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'a' -//│ ║ l.58: fun f(x) = if x is S(Bit, a, b) then [a, b] +//│ ║ l.59: fun f(x) = if x is S(Bit, a, b) then [a, b] //│ ╙── ^ +:re f([0, 0]) -//│ = [0, 0] +//│ ═══[RUNTIME ERROR] ReferenceError: a is not defined // The last thing to note is that if there is only one pattern parameter, it // will not be put into a tuple. @@ -84,6 +86,6 @@ if 0 is Q'(Bit) as y then y // Similarly, it cannot be written like this: if 0 is Q'(Bit, y) then y //│ ╔══[ERROR] Expected zero extraction arguments, but found one argument. -//│ ║ l.85: if 0 is Q'(Bit, y) then y +//│ ║ l.87: if 0 is Q'(Bit, y) then y //│ ╙── ^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error From 30c1ae9a9cf7e26c341b6fa5ca8ecd82277b6576 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Mon, 24 Nov 2025 01:31:18 +0800 Subject: [PATCH 23/72] hopefully complete `Term.definedSyms` --- .../main/scala/hkmc2/semantics/Split.scala | 2 +- .../src/main/scala/hkmc2/semantics/Term.scala | 88 +++++++++++++++++-- .../src/test/mlscript/codegen/Scoped.mls | 32 +++++++ 3 files changed, 112 insertions(+), 10 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 8746d65a24..b16e22bc04 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -32,7 +32,7 @@ enum Split extends AutoLocated with ProductWithTail: locally: pattern match case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) - case FlatPattern.Tuple(size, inf) => Set.empty // TODO: + case FlatPattern.Tuple(size, inf) => Set.empty // TODO: seems to be ok to leave this as empty? case FlatPattern.Record(entries) => entries.unzip._2 case FlatPattern.Lit(_) => Set.empty case Let(sym, term, tail) => term.definedSyms ++ tail.definedSyms + sym diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 26bf577dc4..049a60902f 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -401,14 +401,48 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) lazy val definedSyms: Set[Symbol] = this match - case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty - case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) - case LetDecl(sym, annotations) => Set(sym) - case termdef: TermDefinition => Set(termdef.sym) - case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) - // case imp: Import => Set(imp.sym) + // case Error => + // case UnitVal() => + // case Missing => + // case Lit(lit) => + // case Ref(tree, refNum, typ) => + // case Import(sym, str, file) => + case Resolved(typ, _) => typ.definedSyms + case App(l, r) => l.definedSyms ++ r.definedSyms + case TyApp(l, targs) => targs.foldLeft(l.definedSyms)(_ ++ _.definedSyms) + case Sel(s, _) => s.definedSyms + case SynthSel(s, _) => s.definedSyms + case SelProj(s, c, _) => s.definedSyms ++ c.definedSyms + case DynSel(prefix, fld, _) => prefix.definedSyms ++ fld.definedSyms + case Tup(tree) => tree + .flatMap: + case Fld(flags, term, asc) => term.definedSyms ++ asc.fold(Set.empty)(_.definedSyms) + case Spd(eager, term) => term.definedSyms + .toSet + case Mut(underlying) => underlying.definedSyms + case CtxTup(tree) => tree + .flatMap: + case Fld(flags, term, asc) => term.definedSyms ++ asc.fold(Set.empty)(_.definedSyms) + case Spd(eager, term) => term.definedSyms + .toSet // IfLikes get their own scope - case _: (IfLike | SynthIf) => Set.empty + case IfLike(kw, split) => Set.empty + case SynthIf(split) => Set.empty + case Lam(params, body) => Set.empty + case FunTy(lhs, rhs, eff) => lhs.definedSyms ++ rhs.definedSyms ++ eff.fold(Set.empty)(_.definedSyms) + case Forall(tvs, outer, body) => body.definedSyms + case WildcardTy(in, out) => in.fold(Set.empty)(_.definedSyms ++ out.fold(Set.empty)(_.definedSyms)) + case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + case Rcd(mut, stats) => stats.foldLeft(Set.empty)(_ ++ _.definedSyms) + case Quoted(body) => body.definedSyms + case Unquoted(body) => body.definedSyms + case New(typ, args, _) => args.foldLeft(typ.definedSyms)(_ ++ _.definedSyms) + case DynNew(cls, args) => args.foldLeft(cls.definedSyms)(_ ++ _.definedSyms) + case Asc(term, ty) => term.definedSyms ++ ty.definedSyms + case CompType(lhs, rhs, pol) => lhs.definedSyms ++ rhs.definedSyms + case Neg(rhs) => rhs.definedSyms + case Region(name, body) => body.definedSyms + case RegRef(reg, value) => reg.definedSyms ++ value.definedSyms // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. // And including the sym here may cause error for delayed init in a function: // ``` @@ -419,9 +453,45 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: // the definedSyms of the rhs of these two cases should be included // because those needs to be included in the // same current `Scoped` anyway - case DefineVar(_, rhs) => rhs.definedSyms case Assgn(_, rhs) => rhs.definedSyms - case _ => Set.empty // TODO: add other cases + case DefineVar(_, rhs) => rhs.definedSyms + case Drop(trm) => trm.definedSyms + case Deref(ref) => ref.definedSyms + case SetRef(ref, value) => ref.definedSyms ++ value.definedSyms + case Ret(result) => result.definedSyms + case Throw(result) => result.definedSyms + case Try(body, finallyDo) => body.definedSyms ++ finallyDo.definedSyms + case Annotated(annot, target) => target.definedSyms + case Handle(lhs, rhs, args, derivedClsSym, defs, body) => + Set.empty // TODO: + case LetDecl(sym, annotations) => Set(sym) + case RcdField(field, rhs) => field.definedSyms ++ rhs.definedSyms + case RcdSpread(rcd) => rcd.definedSyms + case termdef: TermDefinition => Set(termdef.sym) + case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) + case _ => Set.empty + // this match + // case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty + // case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) + // case LetDecl(sym, annotations) => Set(sym) + // case termdef: TermDefinition => Set(termdef.sym) + // case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) + // // case imp: Import => Set(imp.sym) + // // IfLikes get their own scope + // case _: (IfLike | SynthIf) => Set.empty + // // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. + // // And including the sym here may cause error for delayed init in a function: + // // ``` + // // let x + // // fun f() = + // // x = 2 + // // ``` + // // the definedSyms of the rhs of these two cases should be included + // // because those needs to be included in the + // // same current `Scoped` anyway + // case DefineVar(_, rhs) => rhs.definedSyms + // case Assgn(_, rhs) => rhs.definedSyms + // case _ => Set.empty def describe: Str = val desc = this match diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 0e0843588a..4da3172954 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -243,3 +243,35 @@ fun f() = //│ tmp3 = tmp2 !== runtime.LoopEnd; //│ if (tmp3 === true) { return tmp2 } else { return tmp1 } //│ }; + + +:sjs +fun f(x, y) = + while x && y do + f(0, 0) + 4 +//│ JS (unsanitized): +//│ let f5; /** scoped **/ +//│ f5 = function f(x1, y1) { +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ while1 = (undefined, function () { +//│ let scrut, lambda, tmp4; /** scoped **/ +//│ lambda = (undefined, function () { +//│ return y1 +//│ }); +//│ scrut = runtime.short_and(x1, lambda); +//│ if (scrut === true) { +//│ tmp4 = f5(0, 0); +//│ tmp1 = tmp4 + 4; +//│ return while1() +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ }; + + + From 46dd487c0cadde8f0ce89b0cfbcc55fe474efe51 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 25 Nov 2025 01:02:36 +0800 Subject: [PATCH 24/72] wip: make all tests pass --- .../src/main/scala/hkmc2/codegen/Block.scala | 6 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 8 +- .../scala/hkmc2/codegen/llir/Builder.scala | 1 + .../src/main/scala/hkmc2/semantics/Term.scala | 2 +- .../hkmc2/semantics/ucs/Normalization.scala | 2 +- .../src/test/mlscript-compile/Runtime.mjs | 2 +- .../OverloadedModulesInSignatures.mls | 10 +- .../src/test/mlscript/backlog/ToTriage.mls | 64 +++++----- .../test/mlscript/basics/BadModuleUses.mls | 4 +- .../basics/CompanionModules_Classes.mls | 2 +- .../basics/CompanionModules_Functions.mls | 4 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../mlscript/basics/DynamicInstantiation.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 4 +- hkmc2/shared/src/test/mlscript/basics/New.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../test/mlscript/basics/ObjectExtensions.mls | 4 +- .../src/test/mlscript/basics/Overloading.mls | 2 +- .../src/test/mlscript/basics/Records.mls | 16 +-- .../src/test/mlscript/basics/Underscores.mls | 28 ++--- .../src/test/mlscript/bbml/bbCodeGen.mls | 8 +- .../src/test/mlscript/codegen/BadOpen.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 42 +++---- .../src/test/mlscript/codegen/CaseOfCase.mls | 12 +- .../test/mlscript/codegen/CaseShorthand.mls | 4 +- .../test/mlscript/codegen/ClassInClass.mls | 27 ++-- .../test/mlscript/codegen/ClassMatching.mls | 40 +++--- .../src/test/mlscript/codegen/ConsoleLog.mls | 2 +- .../test/mlscript/codegen/FieldSymbols.mls | 87 +++++++------ .../src/test/mlscript/codegen/FunInClass.mls | 11 +- .../test/mlscript/codegen/FunctionsThis.mls | 2 +- .../src/test/mlscript/codegen/FunnyOpen.mls | 2 +- .../src/test/mlscript/codegen/Getters.mls | 2 +- .../src/test/mlscript/codegen/Hygiene.mls | 8 +- .../src/test/mlscript/codegen/ImportedOps.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 75 ++++++------ .../test/mlscript/codegen/ModuleMethods.mls | 6 +- .../src/test/mlscript/codegen/Modules.mls | 12 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 8 +- .../test/mlscript/codegen/ParamClasses.mls | 28 ++--- .../test/mlscript/codegen/PlainClasses.mls | 18 +-- .../shared/src/test/mlscript/codegen/Repl.mls | 4 +- .../test/mlscript/codegen/SanityChecks.mls | 40 +++--- .../src/test/mlscript/codegen/Scoped.mls | 43 ++++--- .../test/mlscript/codegen/SelfReferences.mls | 2 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/Throw.mls | 2 +- .../src/test/mlscript/codegen/TraceLog.mls | 20 +-- .../src/test/mlscript/codegen/While.mls | 84 ++++++------- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../test/mlscript/ctx/TrickyResolution.mls | 8 +- .../src/test/mlscript/handlers/Effects.mls | 20 +-- .../mlscript/handlers/EffectsInClasses.mls | 3 +- .../mlscript/handlers/RecursiveHandlers.mls | 115 +++++++++--------- .../src/test/mlscript/interop/CtorBypass.mls | 2 +- .../src/test/mlscript/interop/Objects.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 41 ++++--- .../test/mlscript/lifter/CompanionsInFun.mls | 1 + .../src/test/mlscript/lifter/FunInFun.mls | 4 + .../shared/src/test/mlscript/lifter/Loops.mls | 1 + .../test/mlscript/lifter/ModulesObjects.mls | 16 +-- .../src/test/mlscript/lifter/Mutation.mls | 1 + .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 24 ++-- .../ucs/general/LogicalConnectives.mls | 8 +- .../ucs/normalization/Deduplication.mls | 14 +-- .../ucs/normalization/SimplePairMatches.mls | 16 +-- .../ucs/patterns/ConjunctionPattern.mls | 2 +- .../src/test/mlscript/ups/MatchResult.mls | 2 +- .../src/test/mlscript/ups/SimpleTransform.mls | 2 +- .../test/mlscript/ups/regex/Separation.mls | 2 +- .../mlscript/ups/syntax/MixedParameters.mls | 12 +- 74 files changed, 529 insertions(+), 547 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 552ed41cb0..58cc197287 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,7 +49,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - case Scoped(syms, body) => body.definedVars -- syms + case Scoped(syms, body) => body.definedVars// -- syms lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -103,7 +103,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body) => body.freeVars -- syms + case Scoped(syms, body) => body.freeVars// -- syms case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -124,7 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body) => body.freeVarsLLIR -- syms + case Scoped(syms, body) => body.freeVarsLLIR// -- syms case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index d699b94f32..24fda06ce3 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -512,8 +512,8 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case Scoped(syms, body) => val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => if scope.lookup(l).isDefined then - raise: - WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) + // raise: + // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) None else Some(l -> scope.allocateName(l)) @@ -611,7 +611,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = // println(s"$t :::::::: ${t.definedVars}") - blockPreamble(t.definedVars) :: returningTerm(t, endSemi) + val pre = blockPreamble(t.definedVars) + val rest = returningTerm(t, endSemi) + pre :: rest def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn: block(t, endSemi) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala index 0e35ae270b..a7c1621a29 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala @@ -567,6 +567,7 @@ final class LlirBuilder(using Elaborator.State)(tl: TraceLogger, uid: FreshInt): case AssignField(_, _, _, rest) => applyBlock(rest) case AssignDynField(lhs, fld, arrayIdx, rhs, rest) => applyBlock(rest) case Define(defn, rest) => applyDefn(defn); applyBlock(rest) + case Scoped(_, body) => applyBlock(body) case HandleBlock(lhs, res, par, args, cls, handlers, body, rest) => applyBlock(rest) case End(msg) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 049a60902f..62c95229e3 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -468,7 +468,7 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdField(field, rhs) => field.definedSyms ++ rhs.definedSyms case RcdSpread(rcd) => rcd.definedSyms case termdef: TermDefinition => Set(termdef.sym) - case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) + case tpeLikeDef: TypeLikeDef if tpeLikeDef.hasDeclareModifier.isEmpty => Set(tpeLikeDef.bsym) case _ => Set.empty // this match // case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index a7c628df92..058ff505a7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -407,7 +407,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e val loopEnd: Path = Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) val blk = blockBuilder - // .assign(l, Value.Lit(Tree.UnitLit(false))) + .assign(l, Value.Lit(Tree.UnitLit(false))) .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) if summon[LoweringCtx].mayRet then diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index d5cc8a4258..7cb9792f43 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -428,9 +428,9 @@ globalThis.Object.freeze(class Runtime { } static checkArgs(functionName, expected, isUB, got) { let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let lambda1; /** scoped **/ tmp = got < expected; lambda = (undefined, function () { - let lambda1; lambda1 = (undefined, function () { return got > expected }); diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index 69abb2d790..da40af046e 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,18 +81,18 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; f5 = function f() { let tmp4; tmp4 = TrmMod1(); return tmp4 }; +//│ let f5; f5 = function f() { let tmp4; tmp4 = TrmMod(); return tmp4 }; :fixme :expect 42 f -//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod1 cannot be invoked without 'new' //│ ═══[RUNTIME ERROR] Expected: '42', got: 'undefined' :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; f6 = function f() { return TrmMod1.class }; +//│ let f6; f6 = function f() { return TrmMod.class }; fun assertModule(module m: ClsMod): module ClsMod = m @@ -104,7 +104,7 @@ assertModule(TypMod).whoami :fixme assertModule(TrmMod).whoami -//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod1 cannot be invoked without 'new' fun assertNonModule(m) = m @@ -124,7 +124,7 @@ assertNonModule(TypMod).whoami :breakme :fixme assertNonModule(TrmMod).whoami -//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod1 cannot be invoked without 'new' type Foo[A] = Int diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index cd5015b385..71e5c6cda1 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -140,7 +140,7 @@ let c = new Cls(123) //│ ║ l.138: let c = new Cls(123) //│ ║ ^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ═══[RUNTIME ERROR] TypeError: Cls1 is not a constructor +//│ ═══[RUNTIME ERROR] TypeError: Cls is not a constructor //│ c = undefined c.x @@ -150,8 +150,8 @@ c.x class Foo' with class Bar' -//│ > let Foo$_1;try { globalThis.Object.freeze(class Foo$_ { static { Foo$_1 = this } constructor() { const this$Foo$_ = this; globalThis.Object.freeze(class Bar$_ { static { this$Foo$_.Bar' = this } constructor() {} toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Bar'"]; }); } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Foo'"]; }); block$res20 = undefined; } catch (e) { console.log('\u200B' + e + '\u200B'); } -//│ > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +//│ > let Foo$_;try { globalThis.Object.freeze(class Foo$_1 { static { Foo$_ = this } constructor() { const this$Foo$_ = this; globalThis.Object.freeze(class Bar$_ { static { this$Foo$_.Bar' = this } constructor() {} toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Bar'"]; }); } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Foo'"]; }); block$res20 = undefined; } catch (e) { console.log('\u200B' + e + '\u200B'); } +//│ > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Unexpected string // ——— ——— ——— @@ -179,11 +179,7 @@ let xs = new Oopsie //│ ╙── ^^^^^^ //│ ╔══[ERROR] Expected a statically known class; found ‹error›. //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'xs' -//│ ╟── which references the symbol introduced here -//│ ║ l.176: let xs = new Oopsie -//│ ╙── ^^ -//│ ═══[RUNTIME ERROR] ReferenceError: xs is not defined +//│ xs = undefined // ——— ——— ——— @@ -202,7 +198,7 @@ class C :sjs class D extends id(C) //│ ╔══[ERROR] Expected a statically known class; found selection. -//│ ║ l.203: class D extends id(C) +//│ ║ l.199: class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -212,7 +208,7 @@ class D extends id(C) //│ D1 = this //│ } //│ constructor() { -//│ super(C1); +//│ super(C); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "D"]; @@ -230,13 +226,13 @@ let x = 0 :fixme set x += 1; () //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.231: set x += 1; () +//│ ║ l.227: set x += 1; () //│ ╙── ^^^^^^^^^^ :e set (x += 1; ()) //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.237: set (x += 1; ()) +//│ ║ l.233: set (x += 1; ()) //│ ╙── ^^^^^^^^^^^^ (set x += 1); () @@ -244,7 +240,7 @@ set (x += 1; ()) :fixme [x, set x += 1; x] //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.245: [x, set x += 1; x] +//│ ║ l.241: [x, set x += 1; x] //│ ╙── ^^^^^^^^^ //│ = [1] @@ -266,13 +262,13 @@ import "../../mlscript-compile/Stack.mls" // The parser rejects this, but it should be allowed. open Stack { ::, Nil } //│ ╔══[ERROR] Illegal 'open' statement shape. -//│ ║ l.267: open Stack { ::, Nil } +//│ ║ l.263: open Stack { ::, Nil } //│ ╙── ^^^^^^^^^^^^^^^ // Instead, if we parenthesize the operator, it is rejected by the elaborator. open Stack { (::), Nil } //│ ╔══[ERROR] Illegal 'open' statement element. -//│ ║ l.273: open Stack { (::), Nil } +//│ ║ l.269: open Stack { (::), Nil } //│ ╙── ^^^^ open Stack { Nil, :: } @@ -280,7 +276,7 @@ open Stack { Nil, :: } :sjs 1 :: Nil //│ ╔══[ERROR] Module 'Stack' does not contain member '::' -//│ ║ l.281: 1 :: Nil +//│ ║ l.277: 1 :: Nil //│ ╙── ^^ //│ JS (unsanitized): //│ Stack["::"](1, Stack.Nil) @@ -291,9 +287,9 @@ open Stack { Nil, :: } mkStr of ... // hello "oops" //│ ╔══[PARSE ERROR] Expected start of expression in this position; found new line instead -//│ ║ l.291: mkStr of ... // hello +//│ ║ l.287: mkStr of ... // hello //│ ║ ^ -//│ ║ l.292: "oops" +//│ ║ l.288: "oops" //│ ╙── //│ = "oops" @@ -316,25 +312,25 @@ Foo(1, 2, 3).args :todo if Foo(1, 2, 3) is Foo(...args) then args //│ ╔══[ERROR] Unrecognized pattern (spread). -//│ ║ l.317: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^^^^ //│ ╔══[ERROR] Name not found: args -//│ ║ l.317: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^ //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.317: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╔══[ERROR] The constructor does not take any arguments but found three arguments. -//│ ║ l.329: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] +//│ ║ l.325: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(arg) then arg //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.335: if Foo(1, 2, 3) is Foo(arg) then arg +//│ ║ l.331: if Foo(1, 2, 3) is Foo(arg) then arg //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error @@ -343,7 +339,7 @@ if Foo(1, 2, 3) is Foo(arg) then arg module M with val name = "Student" -//│ ═══[RUNTIME ERROR] TypeError: Cannot assign to read only property 'name' of function 'class M { static { M1 = this } constructor() { runtime.Unit; } static { this.name = "Stud...... }' +//│ ═══[RUNTIME ERROR] TypeError: Cannot assign to read only property 'name' of function 'class M1 { static { M = this } constructor() { runtime.Unit; } static { this.name = "Stud...... }' // ——— ——— ——— @@ -368,10 +364,10 @@ fun foo() = let bar(x: Str): Str = x + x bar("test") //│ ╔══[ERROR] Unsupported let binding shape -//│ ║ l.368: let bar(x: Str): Str = x + x +//│ ║ l.364: let bar(x: Str): Str = x + x //│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Expected 0 arguments, got 1 -//│ ║ l.369: bar("test") +//│ ║ l.365: bar("test") //│ ╙── ^^^^^^^^ // ——— ——— ——— @@ -381,7 +377,7 @@ fun foo() = module Foo(x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.381: module Foo(x) +//│ ║ l.377: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): //│ let Foo5; @@ -405,7 +401,7 @@ module Foo(x) module Foo(val x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.405: module Foo(val x) +//│ ║ l.401: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): //│ let Foo7; @@ -428,7 +424,7 @@ module Foo(val x) // TODO support syntax? data class Foo(mut x) //│ ╔══[ERROR] Expected a valid parameter, found 'mut'-modified identifier -//│ ║ l.429: data class Foo(mut x) +//│ ║ l.425: data class Foo(mut x) //│ ╙── ^ // ——— ——— ——— @@ -445,10 +441,10 @@ set this.pc = 0 set (this).pc = 0 //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.444: set this.pc = 0 +//│ ║ l.440: set this.pc = 0 //│ ╙── ^^^^ //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.446: (this).pc = 0 +//│ ║ l.442: (this).pc = 0 //│ ╙── ^^^^ // But this doesn't... @@ -457,10 +453,10 @@ set set this.pc = 0 //│ ╔══[PARSE ERROR] Unexpected static selector here -//│ ║ l.458: this.pc = 0 +//│ ║ l.454: this.pc = 0 //│ ╙── ^^^ //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.458: this.pc = 0 +//│ ║ l.454: this.pc = 0 //│ ╙── ^^^^ // ——— ——— ——— @@ -472,7 +468,7 @@ pattern A(pattern B) = B fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ║ l.473: fun foo(x) = if x is @annotations.compile A(0) as y then y +//│ ║ l.469: fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╙── ^ // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls index 5b0eae7a0a..c4c7814570 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls @@ -47,14 +47,14 @@ Example.foo() + 1 //│ ╔══[ERROR] Unexpected moduleful application of type M. //│ ║ l.46: Example.foo() + 1 //│ ╙── ^^^^^^^^^^^^^ -//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M + 1 //│ ╔══[ERROR] Unexpected moduleful reference of type M. //│ ║ l.53: M + 1 //│ ╙── ^ -//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M |> id diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index ced354b8b2..b2a425d898 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -96,6 +96,7 @@ module Foo with val res = Foo().foo //│ JS (unsanitized): //│ let Foo1, tmp1; +//│ let tmp2; /** scoped **/ //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { @@ -107,7 +108,6 @@ module Foo with //│ } //│ constructor() {} //│ static { -//│ let tmp2; //│ tmp2 = Foo1(); //│ this.res = tmp2.foo; //│ } diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls index 7a7a7d299d..c045045957 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls @@ -14,10 +14,10 @@ module foo with //│ ║ ^^^^^^^^^^^^^^^ //│ ║ l.8: val zero = foo(0) //│ ╙── ^^^^^^^^^^^^^^^^^^^ -//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo1 cannot be invoked without 'new' :fixme foo(123) -//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo1 cannot be invoked without 'new' diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index d0425820bb..df691692ad 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ +//│ let foo; /** scoped **/ :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls b/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls index 2353a93c51..8001ff0155 100644 --- a/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls +++ b/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls @@ -6,7 +6,7 @@ class C :sjs new! C //│ JS (unsanitized): -//│ globalThis.Object.freeze(new C1()) +//│ globalThis.Object.freeze(new C()) //│ = C let c = C @@ -38,7 +38,7 @@ new! id(C) //│ rhs = Tup of Ls of //│ Ident of "C" //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Predef.id(C1)) +//│ globalThis.Object.freeze(new Predef.id(C)) //│ ═══[RUNTIME ERROR] TypeError: Predef.id is not a constructor // * Ok (as in JS) diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index db082741cb..38009e7283 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -52,15 +52,15 @@ data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): //│ let Baz1; +//│ let tmp; /** scoped **/ //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; -//│ globalThis.Object.freeze(class Baz extends Bar3.class { +//│ globalThis.Object.freeze(class Baz extends Bar2.class { //│ static { //│ Baz1.class = this //│ } //│ constructor(z) { -//│ let tmp; //│ tmp = z * 1; //│ super(tmp); //│ this.z = z; diff --git a/hkmc2/shared/src/test/mlscript/basics/New.mls b/hkmc2/shared/src/test/mlscript/basics/New.mls index 2bd38e7674..f774bb4d4d 100644 --- a/hkmc2/shared/src/test/mlscript/basics/New.mls +++ b/hkmc2/shared/src/test/mlscript/basics/New.mls @@ -36,7 +36,7 @@ new //│ ║ l.34: Foo() //│ ║ ^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo1 cannot be invoked without 'new' :e new diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index b24d4380ee..660664e019 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; f = globalThis.Object.freeze(new Foo1()); +//│ let f; f = globalThis.Object.freeze(new Foo()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; f1 = new Foo1(); +//│ let f1; f1 = new Foo(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls b/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls index 303968c6b4..aa7506e715 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls @@ -9,11 +9,11 @@ new Foo //│ ║ l.7: new Foo //│ ║ ^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ═══[RUNTIME ERROR] TypeError: Foo1 is not a constructor +//│ ═══[RUNTIME ERROR] TypeError: Foo is not a constructor :re new! Foo -//│ ═══[RUNTIME ERROR] TypeError: Foo1 is not a constructor +//│ ═══[RUNTIME ERROR] TypeError: Foo is not a constructor :fixme // Support? new! Foo.class diff --git a/hkmc2/shared/src/test/mlscript/basics/Overloading.mls b/hkmc2/shared/src/test/mlscript/basics/Overloading.mls index ba9d7b1d7d..93f7395114 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Overloading.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Overloading.mls @@ -19,6 +19,6 @@ Foo :todo Foo() -//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo1 cannot be invoked without 'new' diff --git a/hkmc2/shared/src/test/mlscript/basics/Records.mls b/hkmc2/shared/src/test/mlscript/basics/Records.mls index c4d757b8aa..a60b1f3527 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Records.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Records.mls @@ -78,11 +78,7 @@ let rcd = //│ ╔══[ERROR] Name not found: bar //│ ║ l.73: bar = //│ ╙── ^^^ -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'rcd' -//│ ╟── which references the symbol introduced here -//│ ║ l.71: let rcd = -//│ ╙── ^^^ -//│ rcd = {foo: 1, bar: "..."} +//│ rcd = undefined // * How about this syntax? @@ -93,10 +89,10 @@ let rcd = new with bar = "..." //│ ╔══[ERROR] Name not found: foo -//│ ║ l.92: foo = 1 +//│ ║ l.88: foo = 1 //│ ╙── ^^^ //│ ╔══[ERROR] Name not found: bar -//│ ║ l.93: bar = +//│ ║ l.89: bar = //│ ╙── ^^^ //│ rcd = $anon @@ -138,7 +134,7 @@ m0.get(41) :e {get(k): _} //│ ╔══[ERROR] Unexpected record key shape. -//│ ║ l.139: {get(k): _} +//│ ║ l.135: {get(k): _} //│ ╙── ^^^^^^ //│ = fun @@ -193,14 +189,14 @@ display(x: 88, y: 99, i: 0) :todo t("group") //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.194: t("group") +//│ ║ l.190: t("group") //│ ╙── ^^^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function 't' expected 2 arguments but got 1 :todo t("group") //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.201: t("group") +//│ ║ l.197: t("group") //│ ╙── ^^^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function 't' expected 2 arguments but got 1 diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index 593a13bc38..1d443c419c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -11,17 +11,13 @@ _ //│ = fun :e -:ge -:re +// :ge +// :re y: _ //│ ╔══[ERROR] Illegal position for '_' placeholder. //│ ║ l.16: y: _ //│ ╙── ^ -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ╟── which references the symbol introduced here -//│ ║ l.16: y: _ -//│ ╙── ^ -//│ ═══[RUNTIME ERROR] ReferenceError: y is not defined +//│ y = undefined (y: _) //│ = fun @@ -135,23 +131,15 @@ mkObj(1, 2) // * Note: this is parsed as `{let mkObj = x: _}, y: _, z: 3` // * TODO: improve or at least raise a warning :e -:ge -:re +// :ge +// :re let mkObj = x: _, y: _, z: 3 //│ ╔══[ERROR] Illegal position for '_' placeholder. -//│ ║ l.140: let mkObj = x: _, y: _, z: 3 +//│ ║ l.136: let mkObj = x: _, y: _, z: 3 //│ ╙── ^ //│ mkObj = fun mkObj -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ╟── which references the symbol introduced here -//│ ║ l.140: let mkObj = x: _, y: _, z: 3 -//│ ╙── ^ -//│ ═══[RUNTIME ERROR] ReferenceError: y is not defined -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'z' -//│ ╟── which references the symbol introduced here -//│ ║ l.140: let mkObj = x: _, y: _, z: 3 -//│ ╙── ^ -//│ ═══[RUNTIME ERROR] ReferenceError: z is not defined +//│ y = undefined +//│ z = undefined // mkObj(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 19e96e6493..0d8f01f0f5 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -81,7 +81,7 @@ data class Foo(x: Int) :sjs new Foo(42) //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Foo1.class(42)) +//│ globalThis.Object.freeze(new Foo.class(42)) //│ = Foo(42) //│ Type: Foo @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; foo = globalThis.Object.freeze(new Foo1.class(42)); foo.x +//│ let foo; foo = globalThis.Object.freeze(new Foo.class(42)); foo.x //│ = 42 //│ Type: Int @@ -135,8 +135,8 @@ let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): //│ let foo1; -//│ foo1 = globalThis.Object.freeze(new Foo3.class()); -//│ if (foo1 instanceof Foo3.class) { 1 } else { 0 } +//│ foo1 = globalThis.Object.freeze(new Foo2.class()); +//│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } //│ = 1 //│ foo = Foo() //│ Type: Int diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls b/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls index e2a1c73393..93a395f5a0 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls @@ -32,7 +32,7 @@ open Foo { y } :sjs y //│ JS (unsanitized): -//│ Foo1.y +//│ Foo.y val Oops = "oops" diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index a2ed080be2..7bf650836b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -24,11 +24,8 @@ //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Assign: -//│ lhs = $block$res -//│ rhs = Lit of IntLit of 2 -//│ rest = Return: \ -//│ res = Lit of UnitLit of false +//│ main = Return: +//│ res = Lit of IntLit of 2 //│ implct = true //│ = 2 @@ -46,24 +43,23 @@ print("Hi") //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Assign: -//│ lhs = $tmp -//│ rhs = Call: -//│ fun = Select{sym=term:module:Predef.print}: -//│ qual = Ref{disamb=module:Predef}: -//│ l = member:Predef -//│ disamb = S of module:Predef -//│ name = Ident of "print" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "Hi" -//│ rest = Assign: \ -//│ lhs = $block$res -//│ rhs = Lit of IntLit of 2 -//│ rest = Return: \ -//│ res = Lit of UnitLit of false -//│ implct = true +//│ main = Scoped: +//│ syms = HashSet($tmp) +//│ body = Assign: +//│ lhs = $tmp +//│ rhs = Call: +//│ fun = Select{sym=term:module:Predef.print}: +//│ qual = Ref{disamb=module:Predef}: +//│ l = member:Predef +//│ disamb = S of module:Predef +//│ name = Ident of "print" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "Hi" +//│ rest = Return: \ +//│ res = Lit of IntLit of 2 +//│ implct = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 22009ea6bb..6bc047ff4f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -18,22 +18,22 @@ fun test(x) = //│ let test; //│ test = function test(x) { //│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; -//│ if (x instanceof Some1.class) { +//│ if (x instanceof Some.class) { //│ argument0$1 = x.value; //│ v = argument0$1; //│ tmp = v + 1; -//│ tmp1 = Some1(tmp); -//│ } else if (x instanceof None1.class) { -//│ tmp1 = None1; +//│ tmp1 = Some(tmp); +//│ } else if (x instanceof None.class) { +//│ tmp1 = None; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ scrut = tmp1; -//│ if (scrut instanceof Some1.class) { +//│ if (scrut instanceof Some.class) { //│ argument0$ = scrut.value; //│ v1 = argument0$; //│ return Predef.print(v1) -//│ } else if (scrut instanceof None1.class) { +//│ } else if (scrut instanceof None.class) { //│ return Predef.print("none") //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 510dbe3eeb..87451b3e49 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -81,9 +81,9 @@ val isDefined = case //│ JS (unsanitized): //│ let isDefined, lambda12; //│ lambda12 = (undefined, function (caseScrut) { -//│ if (caseScrut instanceof Some1.class) { +//│ if (caseScrut instanceof Some.class) { //│ return true -//│ } else if (caseScrut instanceof None1.class) { +//│ } else if (caseScrut instanceof None.class) { //│ return false //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index 195b55a49a..e29724b8ef 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -15,6 +15,7 @@ data class Outer(a, b) with print(i.i1(1)) //│ JS (unsanitized): //│ let Outer1; +//│ let tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -23,7 +24,6 @@ data class Outer(a, b) with //│ Outer1.class = this //│ } //│ constructor(a, b) { -//│ let tmp, tmp1, tmp2; //│ this.a = a; //│ this.b = b; //│ const this$Outer = this; @@ -35,12 +35,11 @@ data class Outer(a, b) with //│ this$Outer.Inner.class = this //│ } //│ constructor(c) { -//│ let tmp3, tmp4, tmp5; //│ this.c = c; -//│ tmp3 = Predef.print(this$Outer.a); -//│ tmp4 = Predef.print(this.c); -//│ tmp5 = this.i1(this$Outer.a); -//│ Predef.print(tmp5); +//│ tmp = Predef.print(this$Outer.a); +//│ tmp1 = Predef.print(this.c); +//│ tmp2 = this.i1(this$Outer.a); +//│ Predef.print(tmp2); //│ } //│ i1(d) { //│ return globalThis.Object.freeze([ @@ -52,19 +51,19 @@ data class Outer(a, b) with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Inner", ["c"]]; //│ }); -//│ tmp = this.Inner(this.a); -//│ this.i = tmp; -//│ tmp1 = Predef.print(this.i.c); -//│ tmp2 = runtime.safeCall(this.i.i1(1)); -//│ Predef.print(tmp2); +//│ tmp3 = this.Inner(this.a); +//│ this.i = tmp3; +//│ tmp4 = Predef.print(this.i.c); +//│ tmp5 = runtime.safeCall(this.i.i1(1)); +//│ Predef.print(tmp5); //│ } //│ o1(c) { //│ return this.Inner(c) //│ } //│ o2(c, d) { -//│ let tmp; -//│ tmp = this.Inner(c); -//│ return tmp.i1(d) +//│ let tmp6; +//│ tmp6 = this.Inner(c); +//│ return tmp6.i1(d) //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Outer", ["a", "b"]]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 55c80c83c3..0e8998d346 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -10,8 +10,8 @@ object None if Some(0) is Some(x) then x //│ JS (unsanitized): //│ let scrut, x, argument0$; -//│ scrut = Some1(0); -//│ if (scrut instanceof Some1.class) { +//│ scrut = Some(0); +//│ if (scrut instanceof Some.class) { //│ argument0$ = scrut.value; //│ x = argument0$; //│ x @@ -27,7 +27,7 @@ if s is Some(x) then x //│ JS (unsanitized): //│ let x1, argument0$1; -//│ if (s instanceof Some1.class) { +//│ if (s instanceof Some.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; //│ x1 @@ -56,7 +56,7 @@ x => if x is Some(x) then x //│ let lambda; //│ lambda = (undefined, function (x3) { //│ let x4, argument0$4; -//│ if (x3 instanceof Some1.class) { +//│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; //│ return x4 @@ -120,27 +120,27 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f4; //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp5; +//│ let x4, scrut1, argument0$4, tmp6; //│ split_root$1: { //│ split_1$: { -//│ if (x3 instanceof Some1.class) { +//│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; //│ scrut1 = x4 > 0; //│ if (scrut1 === true) { -//│ tmp5 = 42; +//│ tmp6 = 42; //│ break split_root$1 //│ } else { //│ break split_1$ //│ } -//│ } else if (x3 instanceof None1.class) { -//│ tmp5 = "ok"; +//│ } else if (x3 instanceof None.class) { +//│ tmp6 = "ok"; //│ break split_root$1 //│ } else { break split_1$ } //│ } -//│ tmp5 = Predef.print("oops"); +//│ tmp6 = Predef.print("oops"); //│ } -//│ return tmp5 +//│ return tmp6 //│ }; f(Some(0)) @@ -167,11 +167,11 @@ fun f(x) = if x is //│ let f5; //│ f5 = function f(x3) { //│ let u, a, b, argument0$4, argument1$; -//│ if (x3 instanceof Some1.class) { +//│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; //│ return u -//│ } else if (x3 instanceof Pair1.class) { +//│ } else if (x3 instanceof Pair.class) { //│ argument0$4 = x3.fst; //│ argument1$ = x3.snd; //│ b = argument1$; @@ -196,25 +196,25 @@ fun f(x) = print of if x is //│ JS (unsanitized): //│ let f6; //│ f6 = function f(x3) { -//│ let argument0$4, tmp9; +//│ let argument0$4, tmp10; //│ split_root$1: { //│ split_1$: { -//│ if (x3 instanceof Some1.class) { +//│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ if (argument0$4 === 0) { -//│ tmp9 = "0"; +//│ tmp10 = "0"; //│ break split_root$1 //│ } else { //│ break split_1$ //│ } -//│ } else if (x3 instanceof None1.class) { -//│ tmp9 = "ok"; +//│ } else if (x3 instanceof None.class) { +//│ tmp10 = "ok"; //│ break split_root$1 //│ } else { break split_1$ } //│ } -//│ tmp9 = "oops"; +//│ tmp10 = "oops"; //│ } -//│ return Predef.print(tmp9) +//│ return Predef.print(tmp10) //│ }; f(Some(0)) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index be5f1aee43..70836f427c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,7 +7,7 @@ :silent declare val console //│ JS (unsanitized): -//│ +//│ let console; /** scoped **/ console.log("a") diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 6466aeb028..5f13db5813 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -99,53 +99,52 @@ case //│ sign = N //│ modulefulness = Modulefulness of N //│ restParam = N -//│ body = Match: -//│ scrut = Ref: -//│ l = caseScrut -//│ disamb = N -//│ arms = Ls of -//│ Tuple2: -//│ _1 = Cls: -//│ cls = class:Foo -//│ path = Ref{disamb=class:Foo}: -//│ l = member:Foo -//│ disamb = S of class:Foo -//│ _2 = Assign: -//│ lhs = $argument0$ -//│ rhs = Select{sym=term:class:Foo.x}: -//│ qual = Ref: -//│ l = caseScrut +//│ body = Scoped: +//│ syms = HashSet(a, $argument0$) +//│ body = Match: +//│ scrut = Ref: +//│ l = caseScrut +//│ disamb = N +//│ arms = Ls of +//│ Tuple2: +//│ _1 = Cls: +//│ cls = class:Foo +//│ path = Ref{disamb=class:Foo}: +//│ l = member:Foo +//│ disamb = S of class:Foo +//│ _2 = Assign: +//│ lhs = $argument0$ +//│ rhs = Select{sym=term:class:Foo.x}: +//│ qual = Ref: +//│ l = caseScrut +//│ disamb = N +//│ name = Ident of "x" +//│ rest = Assign: \ +//│ lhs = a +//│ rhs = Ref: +//│ l = $argument0$ //│ disamb = N -//│ name = Ident of "x" -//│ rest = Assign: \ -//│ lhs = a -//│ rhs = Ref: -//│ l = $argument0$ -//│ disamb = N -//│ rest = Return: \ -//│ res = Ref: -//│ l = a -//│ disamb = N -//│ implct = false -//│ dflt = S of Throw of Instantiate: -//│ mut = false -//│ cls = Select: -//│ qual = Ref{disamb=globalThis:globalThis}: -//│ l = globalThis:globalThis -//│ disamb = S of globalThis:globalThis -//│ name = Ident of "Error" -//│ args = Ls of -//│ Arg: -//│ spread = N -//│ value = Lit of StrLit of "match error" -//│ rest = End of "" -//│ rest = Assign: \ -//│ lhs = $block$res -//│ rhs = Ref: +//│ rest = Return: \ +//│ res = Ref: +//│ l = a +//│ disamb = N +//│ implct = false +//│ dflt = S of Throw of Instantiate: +//│ mut = false +//│ cls = Select: +//│ qual = Ref{disamb=globalThis:globalThis}: +//│ l = globalThis:globalThis +//│ disamb = S of globalThis:globalThis +//│ name = Ident of "Error" +//│ args = Ls of +//│ Arg: +//│ spread = N +//│ value = Lit of StrLit of "match error" +//│ rest = End of "" +//│ rest = Return: \ +//│ res = Ref: //│ l = member:lambda //│ disamb = N -//│ rest = Return: \ -//│ res = Lit of UnitLit of false //│ implct = true //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 6fef9160a3..8da41c9dda 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -22,6 +22,7 @@ fun test(a) = //│ let test1; //│ test1 = function test(a) { //│ let Inner1; +//│ let tmp; /** scoped **/ //│ Inner1 = function Inner(b) { //│ return globalThis.Object.freeze(new Inner.class(b)); //│ }; @@ -30,7 +31,6 @@ fun test(a) = //│ Inner1.class = this //│ } //│ constructor(b) { -//│ let tmp; //│ this.b = b; //│ tmp = Predef.print(a); //│ } @@ -82,6 +82,7 @@ fun test(a) = //│ let test2; //│ test2 = function test(a) { //│ let C11, C21, tmp, tmp1; +//│ let tmp2, tmp3; /** scoped **/ //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -90,7 +91,6 @@ fun test(a) = //│ C11.class = this //│ } //│ constructor(b) { -//│ let tmp2; //│ this.b = b; //│ tmp2 = globalThis.Object.freeze([ //│ a, @@ -109,13 +109,12 @@ fun test(a) = //│ C21.class = this //│ } //│ constructor(b) { -//│ let tmp2; //│ this.b = b; -//│ tmp2 = globalThis.Object.freeze([ +//│ tmp3 = globalThis.Object.freeze([ //│ a, //│ this.b //│ ]); -//│ Predef.print(tmp2); +//│ Predef.print(tmp3); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "C2", ["b"]]; @@ -181,6 +180,7 @@ data class Bar(x) with Bar(1) //│ JS (unsanitized): //│ let Bar1; +//│ let tmp; /** scoped **/ //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -189,7 +189,6 @@ Bar(1) //│ Bar1.class = this //│ } //│ constructor(x) { -//│ let tmp; //│ this.x = x; //│ tmp = this.foo(); //│ runtime.safeCall(tmp()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index 540121fc08..e371cd8530 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -14,12 +14,12 @@ class Test with print(foo()) //│ JS (unsanitized): //│ let Test1; +//│ let tmp; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this //│ } //│ constructor() { -//│ let tmp; //│ tmp = foo(); //│ Predef.print(tmp); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls b/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls index d213c06158..9429c32a49 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls @@ -67,6 +67,6 @@ open Mod { printt, |>> } //│ ╔══[ERROR] Module 'Mod' does not contain member '|>>' //│ ║ l.66: 12 |>> printt //│ ╙── ^^^ -//│ ═══[RUNTIME ERROR] TypeError: Mod1.|>> is not a function +//│ ═══[RUNTIME ERROR] TypeError: Mod.|>> is not a function diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 6a73053cbf..10cff6a0ed 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -126,7 +126,7 @@ module M with :sjs M.t //│ JS (unsanitized): -//│ M1.t +//│ M.t //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index d46187c0ac..4a386a6b9a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -39,7 +39,7 @@ print(Test) :sjs Test.foo() //│ JS (unsanitized): -//│ Test1.foo() +//│ Test.foo() //│ > Test { x: 12, class: object Test } //│ = 12 @@ -78,9 +78,9 @@ module Test with //│ ║ l.72: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: -//│ globalThis.Object.freeze(class Test3 { +//│ globalThis.Object.freeze(class Test4 { //│ static { -//│ Test4 = this +//│ Test3 = this //│ } //│ constructor() { //│ runtime.Unit; @@ -88,7 +88,7 @@ module Test with //│ static #x; //│ static { //│ this.x = 1; -//│ Test3.#x = 2; +//│ Test4.#x = 2; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Test"]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index f328a019d5..9ade6db65e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -12,7 +12,7 @@ fun foo() = foo() //│ JS (unsanitized): //│ let foo; -//│ foo = function foo() { let tmp; tmp = M1.concat("a", "b"); return M1.concat(tmp, "c") }; +//│ foo = function foo() { let tmp; tmp = M.concat("a", "b"); return M.concat(tmp, "c") }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index bc312d91f9..411551d944 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -19,13 +19,13 @@ if a is C then 3 D then 4 //│ JS (unsanitized): -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ 1 -//│ } else if (a instanceof B1.class) { +//│ } else if (a instanceof B.class) { //│ 2 -//│ } else if (a instanceof C1.class) { +//│ } else if (a instanceof C.class) { //│ 3 -//│ } else if (a instanceof D1.class) { +//│ } else if (a instanceof D.class) { //│ 4 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -40,11 +40,11 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ 1 -//│ } else if (a instanceof B1.class) { +//│ } else if (a instanceof B.class) { //│ 2 -//│ } else if (a instanceof C1.class) { +//│ } else if (a instanceof C.class) { //│ 3 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -62,18 +62,18 @@ if a is //│ split_root$: { //│ split_default$: { //│ split_1$: { -//│ if (a instanceof A1.class) { -//│ if (b instanceof A1.class) { +//│ if (a instanceof A.class) { +//│ if (b instanceof A.class) { //│ tmp = 1; //│ break split_root$ -//│ } else if (b instanceof B1.class) { +//│ } else if (b instanceof B.class) { //│ break split_1$ //│ } else { //│ break split_default$ //│ } -//│ } else if (a instanceof B1.class) { +//│ } else if (a instanceof B.class) { //│ break split_1$ -//│ } else if (a instanceof C1.class) { +//│ } else if (a instanceof C.class) { //│ tmp = 3; //│ break split_root$ //│ } else { @@ -108,28 +108,29 @@ let x = if a is print(x) //│ JS (unsanitized): //│ let x, tmp1, tmp2, tmp3, tmp4; -//│ tmp1 = 3; -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ tmp2 = 1; -//│ } else if (a instanceof B1.class) { +//│ } else if (a instanceof B.class) { //│ tmp2 = 2; -//│ } else if (a instanceof C1.class) { +//│ } else if (a instanceof C.class) { //│ tmp2 = 3; -//│ } else if (a instanceof D1.class) { -//│ if (a instanceof A1.class) { -//│ tmp3 = 1 + tmp1; -//│ } else if (a instanceof B1.class) { -//│ tmp3 = 2 + tmp1; +//│ } else { +//│ tmp1 = 3; +//│ if (a instanceof D.class) { +//│ if (a instanceof A.class) { +//│ tmp3 = 1 + tmp1; +//│ } else if (a instanceof B.class) { +//│ tmp3 = 2 + tmp1; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ tmp4 = tmp3; +//│ } else if (a instanceof E.class) { +//│ tmp4 = 5; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp4 = tmp3; //│ tmp2 = Predef.print("done"); -//│ } else if (a instanceof E1.class) { -//│ tmp4 = 5; -//│ tmp2 = Predef.print("done"); -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ x = tmp2; //│ Predef.print(x) @@ -147,9 +148,9 @@ if a is //│ JS (unsanitized): //│ let tmp5; //│ tmp5 = 2; -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ 1 -//│ } else if (a instanceof B1.class) { +//│ } else if (a instanceof B.class) { //│ 2 + tmp5 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -168,11 +169,11 @@ if a is B then 2 + tmp //│ JS (unsanitized): //│ let tmp6; -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ 1 //│ } else { //│ tmp6 = printAndId(3); -//│ if (a instanceof B1.class) { +//│ if (a instanceof B.class) { //│ 2 + tmp6 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ } @@ -191,13 +192,13 @@ if a is print(x) //│ JS (unsanitized): //│ let x1, tmp7; -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ 1 -//│ } else if (a instanceof B1.class) { +//│ } else if (a instanceof B.class) { //│ tmp7 = 2; //│ x1 = tmp7; //│ Predef.print(x1) -//│ } else if (a instanceof C1.class) { +//│ } else if (a instanceof C.class) { //│ tmp7 = 3; //│ x1 = tmp7; //│ Predef.print(x1) @@ -220,12 +221,12 @@ if a is print(x + 2) //│ JS (unsanitized): //│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; -//│ if (a instanceof B1.class) { +//│ if (a instanceof B.class) { //│ 1 //│ } else { -//│ if (a instanceof A1.class) { +//│ if (a instanceof A.class) { //│ tmp8 = 2; -//│ } else if (a instanceof C1.class) { +//│ } else if (a instanceof C.class) { //│ tmp8 = 3; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index 0d43252465..efb15ce8eb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -8,13 +8,13 @@ module Example with :ssjs Example.f(123) //│ JS: -//│ block$res2 = runtime.checkCall(Example1.f(123)); undefined +//│ block$res2 = runtime.checkCall(Example.f(123)); undefined //│ = [123, 456] :ssjs let s = Example.f //│ JS: -//│ selRes = Example1.f; +//│ selRes = Example.f; //│ if (selRes === undefined) { //│ throw globalThis.Object.freeze(new globalThis.Error("Access to required field 'f' yielded 'undefined'")) //│ } @@ -42,6 +42,7 @@ module Test with fun bar() = foo() //│ JS (unsanitized): //│ let Test1; +//│ let tmp1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -51,7 +52,6 @@ module Test with //│ } //│ static #s; //│ static { -//│ let tmp1; //│ Test.#s = 1; //│ tmp1 = Predef.print(Test.#s); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index 11e0e37234..4e1ab0c899 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -19,15 +19,15 @@ module None :sjs None //│ JS (unsanitized): -//│ None1 +//│ None //│ = class None :sjs :re None() //│ JS (unsanitized): -//│ runtime.safeCall(None1()) -//│ ═══[RUNTIME ERROR] TypeError: Class constructor None cannot be invoked without 'new' +//│ runtime.safeCall(None()) +//│ ═══[RUNTIME ERROR] TypeError: Class constructor None1 cannot be invoked without 'new' :e new None @@ -44,7 +44,7 @@ new! None //│ ║ l.42: new! None //│ ╙── ^^^^ //│ JS (unsanitized): -//│ globalThis.Object.freeze(new None1()) +//│ globalThis.Object.freeze(new None()) //│ = None @@ -56,6 +56,7 @@ module M with val y = x + 1 //│ JS (unsanitized): //│ let M1; +//│ let tmp; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -64,7 +65,6 @@ module M with //│ runtime.Unit; //│ } //│ static { -//│ let tmp; //│ globalThis.Object.freeze(class C { //│ static { //│ M.C = this @@ -141,7 +141,7 @@ module M with :sjs M.m //│ JS (unsanitized): -//│ M3.m +//│ M2.m //│ = class M { m: ref'1 } as ref'1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 59ec70080c..1318302565 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -78,7 +78,7 @@ Some :sjs None //│ JS (unsanitized): -//│ Option3.None +//│ Option2.None //│ = 123 diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 1cbc9194fb..f2e726848b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -12,9 +12,9 @@ fun isDefined(x) = if x is //│ JS (unsanitized): //│ let isDefined; //│ isDefined = function isDefined(x) { -//│ if (x instanceof Some1.class) { +//│ if (x instanceof Some.class) { //│ return true -//│ } else if (x instanceof None1.class) { +//│ } else if (x instanceof None.class) { //│ return false //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; @@ -34,10 +34,10 @@ val isDefined = case //│ let isDefined1, lambda; //│ lambda = (undefined, function (caseScrut) { //│ let argument0$; -//│ if (caseScrut instanceof Some1.class) { +//│ if (caseScrut instanceof Some.class) { //│ argument0$ = caseScrut.value; //│ return true -//│ } else if (caseScrut instanceof None1.class) { +//│ } else if (caseScrut instanceof None.class) { //│ return false //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 30be3cf8a8..1d24077798 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -21,17 +21,17 @@ data class Foo() Foo //│ JS (unsanitized): -//│ Foo1 +//│ Foo //│ = fun Foo { class: class Foo } Foo() //│ JS (unsanitized): -//│ Foo1() +//│ Foo() //│ = Foo() Foo.class //│ JS (unsanitized): -//│ Foo1.class +//│ Foo.class //│ = class Foo @@ -54,23 +54,23 @@ data class Foo(a) Foo //│ JS (unsanitized): -//│ Foo3 +//│ Foo2 //│ = fun Foo { class: class Foo } Foo(1) //│ JS (unsanitized): -//│ Foo3(1) +//│ Foo2(1) //│ = Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; tmp = Foo3(1); tmp.a +//│ let tmp; tmp = Foo2(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; foo = function foo(y) { return Foo3(y) }; foo(27) +//│ let foo; foo = function foo(y) { return Foo2(y) }; foo(27) //│ = Foo(27) @@ -94,7 +94,7 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; foo1 = Foo5; +//│ let foo1; foo1 = Foo4; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; f2 = Foo5(1, 2); +//│ let f2; f2 = Foo4(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo5(tmp1, tmp2) +//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; i = globalThis.Object.freeze(new Inner1.class(100)); +//│ let i; i = globalThis.Object.freeze(new Inner.class(100)); //│ > 100 //│ i = Inner(100) @@ -186,6 +186,7 @@ class Foo(x, val y, z, val z, z) with print("this.z = " + this.z) //│ JS (unsanitized): //│ let Foo7; +//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -194,7 +195,6 @@ class Foo(x, val y, z, val z, z) with //│ Foo7.class = this //│ } //│ constructor(x, y, z, z1, z2) { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ this.#x = x; //│ this.y = y; //│ this.#z = z; @@ -224,7 +224,7 @@ class Foo(x, val y, z, val z, z) with Foo(10, 20, 30, 40, 50) //│ JS (unsanitized): -//│ Foo7(10, 20, 30, 40, 50) +//│ Foo6(10, 20, 30, 40, 50) //│ > x = 1 //│ > y = 21 //│ > this.y = 20 @@ -258,7 +258,7 @@ class Foo(val z, val z) Foo(1, 2) //│ JS (unsanitized): -//│ Foo9(1, 2) +//│ Foo8(1, 2) //│ = Foo(undefined, undefined) diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index acf7a35fca..f18bf34f8e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -18,36 +18,36 @@ class Foo Foo is Foo //│ JS (unsanitized): -//│ if (Foo1 instanceof Foo1) { true } else { false } +//│ if (Foo instanceof Foo) { true } else { false } //│ = false (new Foo) is Foo //│ JS (unsanitized): //│ let scrut; -//│ scrut = globalThis.Object.freeze(new Foo1()); -//│ if (scrut instanceof Foo1) { true } else { false } +//│ scrut = globalThis.Object.freeze(new Foo()); +//│ if (scrut instanceof Foo) { true } else { false } //│ = true new Foo //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Foo1()) +//│ globalThis.Object.freeze(new Foo()) //│ = Foo new Foo() //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Foo1()) +//│ globalThis.Object.freeze(new Foo()) //│ = Foo Foo //│ JS (unsanitized): -//│ Foo1 +//│ Foo //│ = class Foo :re Foo() //│ JS (unsanitized): -//│ runtime.safeCall(Foo1()) -//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo cannot be invoked without 'new' +//│ runtime.safeCall(Foo()) +//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo1 cannot be invoked without 'new' data class Foo with { print("hi") } @@ -218,7 +218,7 @@ print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): //│ let a, tmp, tmp1, tmp2, tmp3; -//│ a = globalThis.Object.freeze(new Foo10()); +//│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); //│ tmp2 = Predef.print(tmp1); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Repl.mls b/hkmc2/shared/src/test/mlscript/codegen/Repl.mls index 241505d047..9fad84082a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Repl.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Repl.mls @@ -10,7 +10,7 @@ fun res() = 1 //│ REPL> Sending: block$res3 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let res;try { res = function res(...args) { runtime.checkArgs("res", 0, true, args.length); return 1 }; block$res3 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let res2;try { res2 = function res(...args) { runtime.checkArgs("res", 0, true, args.length); return 1 }; block$res3 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: @@ -47,7 +47,7 @@ res //│ REPL> Sending: block$res5 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: try { block$res5 = res; undefined } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: try { block$res5 = res2; undefined } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index d5ef1fbb25..2e8314b296 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -97,12 +97,12 @@ data class Cls(x, y) with fun f(z, p) = x + y + z + p id(Cls(1, 2)).f(3) //│ JS: -//│ Cls1 = function Cls(x, y) { +//│ Cls = function Cls(x, y) { //│ return globalThis.Object.freeze(new Cls.class(x, y)); //│ }; -//│ globalThis.Object.freeze(class Cls { +//│ globalThis.Object.freeze(class Cls1 { //│ static { -//│ Cls1.class = this +//│ Cls.class = this //│ } //│ constructor(x, y) { //│ this.x = x; @@ -117,7 +117,7 @@ id(Cls(1, 2)).f(3) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; //│ }); -//│ tmp4 = Cls1(1, 2); +//│ tmp4 = Cls(1, 2); //│ tmp5 = Predef.id(tmp4); //│ block$res7 = runtime.safeCall(tmp5.f(3)); //│ undefined @@ -129,12 +129,12 @@ data class Cls(x, y) with fun f(z, p) = x + y + z + p id(Cls(1, 2)).f(3) //│ JS: -//│ Cls3 = function Cls(...args) { +//│ Cls2 = function Cls(...args) { //│ return globalThis.Object.freeze(new Cls.class(...args)); //│ }; -//│ globalThis.Object.freeze(class Cls2 { +//│ globalThis.Object.freeze(class Cls3 { //│ static { -//│ Cls3.class = this +//│ Cls2.class = this //│ } //│ constructor(x, y) { //│ this.x = x; @@ -153,7 +153,7 @@ id(Cls(1, 2)).f(3) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; //│ }); -//│ tmp6 = runtime.checkCall(Cls3(1, 2)); +//│ tmp6 = runtime.checkCall(Cls2(1, 2)); //│ tmp7 = runtime.checkCall(Predef.id(tmp6)); //│ block$res8 = runtime.safeCall(tmp7.f(3)); //│ undefined @@ -166,12 +166,12 @@ data class Cls(x, y) with fun f(z, p)(q, s) = x + y + z + p + q + s id(Cls(1, 2)).f(3, 4)(5) //│ JS: -//│ Cls5 = function Cls(...args) { +//│ Cls4 = function Cls(...args) { //│ return globalThis.Object.freeze(new Cls.class(...args)); //│ }; -//│ globalThis.Object.freeze(class Cls4 { +//│ globalThis.Object.freeze(class Cls5 { //│ static { -//│ Cls5.class = this +//│ Cls4.class = this //│ } //│ constructor(x, y) { //│ this.x = x; @@ -197,7 +197,7 @@ id(Cls(1, 2)).f(3, 4)(5) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; //│ }); -//│ tmp8 = runtime.checkCall(Cls5(1, 2)); +//│ tmp8 = runtime.checkCall(Cls4(1, 2)); //│ tmp9 = runtime.checkCall(Predef.id(tmp8)); //│ tmp10 = tmp9.f(3, 4); //│ block$res9 = runtime.safeCall(tmp10(5)); @@ -250,9 +250,9 @@ if M.A(1).y is x and x == 1 then x else 0 //│ JS: -//│ globalThis.Object.freeze(class M { +//│ globalThis.Object.freeze(class M1 { //│ static { -//│ M1 = this +//│ M = this //│ } //│ constructor() { //│ runtime.Unit; @@ -263,7 +263,7 @@ if M.A(1).y is //│ }; //│ globalThis.Object.freeze(class A { //│ static { -//│ M.A.class = this +//│ M1.A.class = this //│ } //│ constructor(x1) { //│ this.x = x1; @@ -281,7 +281,7 @@ if M.A(1).y is //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp11 = runtime.checkCall(M1.A(1)); +//│ tmp11 = runtime.checkCall(M.A(1)); //│ selRes2 = tmp11.y; //│ discarded2 = tmp11.y$__checkNotMethod; //│ if (selRes2 === undefined) { @@ -305,7 +305,7 @@ M.A(1).y //│ if (selRes3 === undefined) { //│ throw globalThis.Object.freeze(new globalThis.Error("Access to required field 'console' yielded 'undefined'")) //│ } -//│ tmp12 = runtime.checkCall(M1.A(1)); +//│ tmp12 = runtime.checkCall(M.A(1)); //│ selRes4 = tmp12.y; //│ discarded4 = tmp12.y$__checkNotMethod; //│ if (selRes4 === undefined) { @@ -322,7 +322,7 @@ M.A(1).y M.A(1).y console.log() //│ JS: -//│ tmp13 = M1.A(1); block$res15 = runtime.safeCall(globalThis.console.log(tmp13.y)); undefined +//│ tmp13 = M.A(1); block$res15 = runtime.safeCall(globalThis.console.log(tmp13.y)); undefined //│ > undefined :noSanityCheck @@ -338,7 +338,7 @@ M.A(2).y > 1 :ssjs M.A(1).g(0) //│ JS: -//│ tmp16 = runtime.checkCall(M1.A(1)); block$res18 = runtime.safeCall(tmp16.g(0)); undefined +//│ tmp16 = runtime.checkCall(M.A(1)); block$res18 = runtime.safeCall(tmp16.g(0)); undefined //│ ═══[RUNTIME ERROR] TypeError: tmp16.g is not a function @@ -346,7 +346,7 @@ M.A(1).g(0) :ssjs M.A(1).f(0) //│ JS: -//│ tmp17 = runtime.checkCall(M1.A(1)); block$res19 = runtime.checkCall(tmp17.f(0)); undefined +//│ tmp17 = runtime.checkCall(M.A(1)); block$res19 = runtime.checkCall(tmp17.f(0)); undefined //│ = 1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 4da3172954..82211a48b5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,9 +19,9 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f() { -//│ let scrut, a, b; /** scoped **/ +//│ let scrut, a, b; //│ scrut = true; //│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -40,12 +40,10 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; /** scoped **/ +//│ let g; //│ g = function g(x1, y1, z) { -//│ let lambda; -//│ let tmp1, tmp2; /** scoped **/ +//│ let a, tmp1, tmp2, lambda; //│ split_root$: { -//│ let a; /** scoped **/ //│ split_1$: { //│ if (x1 === true) { //│ break split_1$ @@ -85,11 +83,10 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ f1 = function f() { -//│ let tmp1; /** scoped **/ +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; //│ split_root$: { -//│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ if (x instanceof A.class) { //│ argument0$ = x.a; @@ -124,11 +121,10 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ f2 = function f() { -//│ let tmp1; /** scoped **/ +//│ let scrut, argument0$, argument1$, tmp1; //│ split_root$: { -//│ let scrut, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ scrut = A(1, Nil); //│ if (scrut instanceof A.class) { @@ -164,11 +160,12 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(x1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, while1, tmp2, tmp3; +//│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, a1, argument0$, argument1$; /** scoped **/ +//│ let a, a1, argument0$, argument1$; //│ if (x1 instanceof A.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; @@ -203,13 +200,14 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; /** scoped **/ +//│ let f4; //│ f4 = function f() { -//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let y1, tmp1, while1, tmp2, tmp3; //│ y1 = x + 1; +//│ tmp1 = undefined; //│ while1 = (undefined, function () { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; //│ split_root$: { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_1$: { //│ if (y1 instanceof A.class) { //│ argument0$ = y1.a; @@ -250,11 +248,12 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; /** scoped **/ +//│ let f5; //│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, while1, tmp2, tmp3; +//│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; /** scoped **/ +//│ let scrut, lambda, tmp4; //│ lambda = (undefined, function () { //│ return y1 //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 7682c56896..76e0d53b89 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -62,12 +62,12 @@ object Foo with val self = id(Foo) //│ JS (unsanitized): //│ let Foo11; +//│ let tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) //│ } //│ constructor() { -//│ let tmp; //│ tmp = Predef.id(Foo11); //│ this.self = tmp; //│ Object.defineProperty(this, "class", { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 99166a71c4..44fb9381b8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; tmp = call(Example1, oops); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = call(Example, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example1, tmp22) +//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 4ef0cd898a..acf2884167 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { throw globalThis.Error("e") }; f2(1) +//│ let f2; f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 1b3593632c..0336784324 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -75,18 +75,18 @@ fun toInt(x) = if x is //│ > | CALL toString() //│ > | | CALL toString() //│ > | | => 'N: 0' -//│ > | | CALL succ(N {}) -//│ > | | => S { x: N {} } -//│ > | | CALL toInt(S { x: N {} }) -//│ > | | | CALL toInt(N {}) +//│ > | | CALL succ(N1 {}) +//│ > | | => S1 { x: N1 {} } +//│ > | | CALL toInt(S1 { x: N1 {} }) +//│ > | | | CALL toInt(N1 {}) //│ > | | | => 0 //│ > | | => 1 //│ > | => 'S(N: 0): 1' -//│ > | CALL succ(S { x: N {} }) -//│ > | => S { x: S { x: N {} } } -//│ > | CALL toInt(S { x: S { x: N {} } }) -//│ > | | CALL toInt(S { x: N {} }) -//│ > | | | CALL toInt(N {}) +//│ > | CALL succ(S1 { x: N1 {} }) +//│ > | => S1 { x: S1 { x: N1 {} } } +//│ > | CALL toInt(S1 { x: S1 { x: N1 {} } }) +//│ > | | CALL toInt(S1 { x: N1 {} }) +//│ > | | | CALL toInt(N1 {}) //│ > | | | => 0 //│ > | | => 1 //│ > | => 2 @@ -105,7 +105,7 @@ id(2) //│ > CALL id(2) //│ > | CALL id(1) //│ > | | CALL id(0) -//│ > | | => Cls { +//│ > | | => Cls1 { //│ > a: 'aaaa', //│ > b: 'bbbb', //│ > c: 'cccc', diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 407c7639f2..5753956740 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -6,7 +6,7 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function () { -//│ let while1, tmp, tmp1, tmp2; +//│ let tmp, while1, tmp1, tmp2; //│ tmp = undefined; //│ while1 = (undefined, function () { //│ let scrut; @@ -57,20 +57,21 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let while3, tmp4, tmp5; -//│ tmp4 = undefined; +//│ let tmp6, while3, tmp7; +//│ let tmp8; /** scoped **/ +//│ tmp6 = undefined; //│ while3 = (undefined, function () { -//│ let tmp6; +//│ let tmp9; //│ if (x2 === true) { -//│ tmp6 = Predef.print("Hello World"); +//│ tmp9 = Predef.print("Hello World"); //│ x2 = false; -//│ tmp4 = runtime.Unit; +//│ tmp6 = runtime.Unit; //│ return while3() -//│ } else { tmp4 = 42; } +//│ } else { tmp6 = 42; } //│ return runtime.LoopEnd //│ }); -//│ tmp5 = while3(); -//│ tmp4 +//│ tmp7 = while3(); +//│ tmp6 //│ > Hello World //│ = 42 @@ -112,25 +113,25 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let while7, tmp12, tmp13, tmp14; -//│ tmp12 = undefined; +//│ let tmp18, while7, tmp19, tmp20; +//│ tmp18 = undefined; //│ while7 = (undefined, function () { -//│ let i2, scrut, tmp15; +//│ let i2, scrut, tmp21; //│ i2 = 0; //│ scrut = i2 < 10; //│ if (scrut === true) { -//│ tmp15 = i2 + 1; -//│ i2 = tmp15; -//│ tmp12 = runtime.Unit; +//│ tmp21 = i2 + 1; +//│ i2 = tmp21; +//│ tmp18 = runtime.Unit; //│ return while7() //│ } else { -//│ tmp12 = runtime.Unit; +//│ tmp18 = runtime.Unit; //│ } //│ return runtime.LoopEnd //│ }); -//│ tmp13 = while7(); -//│ tmp14 = tmp13 !== runtime.LoopEnd; -//│ if (tmp14 === true) { return tmp13 } else { return tmp12 } +//│ tmp19 = while7(); +//│ tmp20 = tmp19 !== runtime.LoopEnd; +//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } //│ }); //│ lambda2 //│ = fun @@ -210,26 +211,26 @@ fun f(ls) = //│ JS (unsanitized): //│ let f; //│ f = function f(ls) { -//│ let while10, tmp18, tmp19, tmp20; -//│ tmp18 = undefined; +//│ let tmp27, while10, tmp28, tmp29; +//│ tmp27 = undefined; //│ while10 = (undefined, function () { //│ let tl, h, argument0$, argument1$; -//│ if (ls instanceof Cons1.class) { +//│ if (ls instanceof Cons.class) { //│ argument0$ = ls.hd; //│ argument1$ = ls.tl; //│ tl = argument1$; //│ h = argument0$; //│ ls = tl; -//│ tmp18 = Predef.print(h); +//│ tmp27 = Predef.print(h); //│ return while10() //│ } else { -//│ tmp18 = Predef.print("Done!"); +//│ tmp27 = Predef.print("Done!"); //│ } //│ return runtime.LoopEnd //│ }); -//│ tmp19 = while10(); -//│ tmp20 = tmp19 !== runtime.LoopEnd; -//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } +//│ tmp28 = while10(); +//│ tmp29 = tmp28 !== runtime.LoopEnd; +//│ if (tmp29 === true) { return tmp28 } else { return tmp27 } //│ }; f(0) @@ -267,8 +268,9 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let while11, tmp27, tmp28; -//│ tmp27 = undefined; +//│ let tmp37, tmp38, while11; +//│ let tmp39; /** scoped **/ +//│ tmp37 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { //│ split_1$: { @@ -278,12 +280,12 @@ while x is {} do() //│ break split_1$ //│ } //│ } -//│ tmp27 = runtime.Unit; +//│ tmp37 = runtime.Unit; //│ } //│ return runtime.LoopEnd //│ }); -//│ tmp28 = while11(); -//│ tmp27 +//│ tmp38 = while11(); +//│ tmp37 // ——— FIXME: ——— @@ -297,10 +299,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.297: then 0(0) +//│ ║ l.299: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.296: while print("Hello World"); false +//│ ║ l.298: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -310,12 +312,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.309: while { print("Hello World"), false } +//│ ║ l.311: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.310: then 0(0) +//│ ║ l.312: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.311: else 1 +//│ ║ l.313: else 1 //│ ╙── ^^^^ :fixme @@ -325,14 +327,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.323: print("Hello World") +//│ ║ l.325: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.324: false +//│ ║ l.326: false //│ ║ ^^^^^^^^^ -//│ ║ l.325: then 0(0) +//│ ║ l.327: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.326: else 1 +//│ ║ l.328: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 6fe4528dde..885a6dd1ed 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -111,7 +111,7 @@ class Foo(using T) with //│ } //│ constructor(tmp5) { //│ this.#tmp = tmp5; -//│ Predef.print(T1); +//│ Predef.print(T); //│ } //│ #tmp; //│ toString() { return runtime.render(this); } diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index a58b71139f..482d1d6532 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) +//│ let tmp2; tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/ctx/TrickyResolution.mls b/hkmc2/shared/src/test/mlscript/ctx/TrickyResolution.mls index 0236beeda4..d9a8abb01a 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/TrickyResolution.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/TrickyResolution.mls @@ -21,12 +21,6 @@ using Bar[Str] = new Bar // * and thus picks the Bar[Int] instance :fixme foo -//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'foo' -//│ ║ l.23: foo -//│ ║ ^^^ -//│ ╟── which references the symbol introduced here -//│ ║ l.11: fun foo[A](using f: Foo[A], b: Bar[A]): A -//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ═══[RUNTIME ERROR] ReferenceError: foo is not defined +//│ ═══[RUNTIME ERROR] TypeError: foo1 is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 41f5579d45..3c23c2b146 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,16 +156,16 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let tmp11, handleBlock$11; +//│ let tmp20, handleBlock$11; //│ handleBlock$11 = function handleBlock$() { //│ let f, h, scrut, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; -//│ globalThis.Object.freeze(class Handler$h$11 extends Effect1 { +//│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { //│ Handler$h$12 = this //│ } //│ constructor() { -//│ let tmp12; -//│ tmp12 = super(); +//│ let tmp21; +//│ tmp21 = super(); //│ } //│ perform(arg) { //│ let hdlrFun; @@ -183,8 +183,8 @@ if true do //│ Cont$handleBlock$h$11 = this //│ } //│ constructor(pc) { -//│ let tmp12; -//│ tmp12 = super(null); +//│ let tmp21; +//│ tmp21 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { @@ -219,11 +219,11 @@ if true do //│ return runtime.Unit //│ } //│ }; -//│ tmp11 = handleBlock$11(); -//│ if (tmp11 instanceof runtime.EffectSig.class) { -//│ tmp11 = runtime.topLevelEffect(tmp11, false); +//│ tmp20 = handleBlock$11(); +//│ if (tmp20 instanceof runtime.EffectSig.class) { +//│ tmp20 = runtime.topLevelEffect(tmp20, false); //│ } -//│ tmp11 +//│ tmp20 //│ = 3 module A with diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index fbef803763..7f9e99ade4 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -11,6 +11,7 @@ data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): //│ let Lol1; +//│ let tmp; /** scoped **/ //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; @@ -19,7 +20,7 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { -//│ let tmp, res, Cont$ctor$Lol$1, doUnwind; +//│ let res, Cont$ctor$Lol$1, doUnwind; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 74770fcc40..b15707c0ed 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,41 +155,42 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let str, scrut, tmp9, tmp10, handleBlock$7; +//│ let str, scrut, tmp24, tmp25, handleBlock$7; //│ str = ""; +//│ let tmp26, tmp27; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ handleBlock$7 = function handleBlock$() { -//│ let h1, tmp11, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; -//│ globalThis.Object.freeze(class Handler$h1$1 extends Effect1 { +//│ let h1, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; +//│ globalThis.Object.freeze(class Handler$h1$1 extends Effect { //│ static { //│ Handler$h1$2 = this //│ } //│ constructor() { -//│ let tmp12; -//│ tmp12 = super(); +//│ let tmp28; +//│ tmp28 = super(); //│ } //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp12, tmp13, tmp14, Cont$handler$h1$perform$2, doUnwind1; +//│ let tmp28, tmp29, tmp30, Cont$handler$h1$perform$2, doUnwind1; //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h1$perform$2 = this //│ } //│ constructor(pc) { -//│ let tmp15; -//│ tmp15 = super(null); +//│ let tmp31; +//│ tmp31 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 7) { -//│ tmp13 = value$; +//│ tmp29 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 7) { -//│ tmp14 = str + "A"; -//│ str = tmp14; +//│ tmp30 = str + "A"; +//│ str = tmp30; //│ return runtime.Unit //│ } //│ break; @@ -203,14 +204,14 @@ str //│ res7.contTrace.last = res7.contTrace.last.next; //│ return res7 //│ }; -//│ tmp12 = str + "A"; -//│ str = tmp12; -//│ tmp13 = runtime.safeCall(k(arg)); -//│ if (tmp13 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp13, 7) +//│ tmp28 = str + "A"; +//│ str = tmp28; +//│ tmp29 = runtime.safeCall(k(arg)); +//│ if (tmp29 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp29, 7) //│ } -//│ tmp14 = str + "A"; -//│ str = tmp14; +//│ tmp30 = str + "A"; +//│ str = tmp30; //│ return runtime.Unit //│ }; //│ return runtime.mkEffect(this, hdlrFun) @@ -224,17 +225,17 @@ str //│ Cont$handleBlock$h1$2 = this //│ } //│ constructor(pc) { -//│ let tmp12; -//│ tmp12 = super(null); +//│ let tmp28; +//│ tmp28 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 6) { -//│ tmp11 = value$; +//│ tmp26 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 6) { -//│ return tmp11 +//│ return tmp26 //│ } //│ break; //│ } @@ -247,37 +248,37 @@ str //│ return runtime.handleBlockImpl(res7, h1) //│ }; //│ handleBlock$8 = function handleBlock$() { -//│ let h2, tmp12, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; -//│ globalThis.Object.freeze(class Handler$h2$1 extends Effect1 { +//│ let h2, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; +//│ globalThis.Object.freeze(class Handler$h2$1 extends Effect { //│ static { //│ Handler$h2$2 = this //│ } //│ constructor() { -//│ let tmp13; -//│ tmp13 = super(); +//│ let tmp28; +//│ tmp28 = super(); //│ } //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp13, tmp14, tmp15, tmp16, tmp17, Cont$handler$h2$perform$2, doUnwind2; +//│ let tmp28, tmp29, tmp30, tmp31, tmp32, Cont$handler$h2$perform$2, doUnwind2; //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h2$perform$2 = this //│ } //│ constructor(pc) { -//│ let tmp18; -//│ tmp18 = super(null); +//│ let tmp33; +//│ tmp33 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 3) { -//│ tmp15 = value$; +//│ tmp30 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 3) { -//│ tmp16 = str + "B"; -//│ tmp17 = str + tmp16; -//│ str = tmp17; +//│ tmp31 = str + "B"; +//│ tmp32 = str + tmp31; +//│ str = tmp32; //│ return runtime.Unit //│ } //│ break; @@ -291,16 +292,16 @@ str //│ res8.contTrace.last = res8.contTrace.last.next; //│ return res8 //│ }; -//│ tmp13 = str + "B"; -//│ tmp14 = str + tmp13; -//│ str = tmp14; -//│ tmp15 = runtime.safeCall(k(arg)); -//│ if (tmp15 instanceof runtime.EffectSig.class) { -//│ return doUnwind2(tmp15, 3) +//│ tmp28 = str + "B"; +//│ tmp29 = str + tmp28; +//│ str = tmp29; +//│ tmp30 = runtime.safeCall(k(arg)); +//│ if (tmp30 instanceof runtime.EffectSig.class) { +//│ return doUnwind2(tmp30, 3) //│ } -//│ tmp16 = str + "B"; -//│ tmp17 = str + tmp16; -//│ str = tmp17; +//│ tmp31 = str + "B"; +//│ tmp32 = str + tmp31; +//│ str = tmp32; //│ return runtime.Unit //│ }; //│ return runtime.mkEffect(this, hdlrFun) @@ -314,13 +315,13 @@ str //│ Cont$handleBlock$h2$2 = this //│ } //│ constructor(pc) { -//│ let tmp13; -//│ tmp13 = super(null); +//│ let tmp28; +//│ tmp28 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 1) { -//│ tmp12 = value$; +//│ tmp27 = value$; //│ } else if (this.pc === 2) { //│ res7 = value$; //│ } @@ -345,9 +346,9 @@ str //│ res8.contTrace.last.next = new Cont$handleBlock$h2$2(pc); //│ return runtime.handleBlockImpl(res8, h2) //│ }; -//│ tmp12 = runtime.safeCall(h2.perform(runtime.Unit)); -//│ if (tmp12 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp12, 1) +//│ tmp27 = runtime.safeCall(h2.perform(runtime.Unit)); +//│ if (tmp27 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp27, 1) //│ } //│ res7 = runtime.safeCall(h1.perform(runtime.Unit)); //│ if (res7 instanceof runtime.EffectSig.class) { @@ -355,18 +356,18 @@ str //│ } //│ return res7 //│ }; -//│ tmp11 = handleBlock$8(); -//│ if (tmp11 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp11, 6) +//│ tmp26 = handleBlock$8(); +//│ if (tmp26 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp26, 6) //│ } -//│ return tmp11 +//│ return tmp26 //│ }; -//│ tmp9 = handleBlock$7(); -//│ if (tmp9 instanceof runtime.EffectSig.class) { -//│ tmp9 = runtime.topLevelEffect(tmp9, false); +//│ tmp24 = handleBlock$7(); +//│ if (tmp24 instanceof runtime.EffectSig.class) { +//│ tmp24 = runtime.topLevelEffect(tmp24, false); //│ } -//│ tmp10 = tmp9; -//│ } else { tmp10 = runtime.Unit; } +//│ tmp25 = tmp24; +//│ } else { tmp25 = runtime.Unit; } //│ str //│ = "BABABA" //│ str = "BABABA" diff --git a/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls b/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls index 58a58516ee..c6aa169f29 100644 --- a/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls +++ b/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls @@ -23,7 +23,7 @@ let b = Object.create(A.prototype) :sjs b is A //│ JS (unsanitized): -//│ if (b instanceof A1) { true } else { false } +//│ if (b instanceof A) { true } else { false } //│ = true :re diff --git a/hkmc2/shared/src/test/mlscript/interop/Objects.mls b/hkmc2/shared/src/test/mlscript/interop/Objects.mls index 5fd07cbc93..96898b9208 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Objects.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Objects.mls @@ -34,14 +34,14 @@ Foo.name //│ = "Foo" Foo.class.name -//│ = "Foo" +//│ = "Foo1" foo.constructor //│ = class Foo // ! will be changed by minification foo.constructor.name -//│ = "Foo" +//│ = "Foo1" typeof(foo) //│ = "object" diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index ec726f6ed4..9eb9276288 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,16 +88,16 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let Bad1, Good1, f5, tmp5, Bad$, Good$, f$capture3; +//│ let Bad1, Good1, f5, tmp9, Bad$, Good$, f$capture3; //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { -//│ let tmp6, tmp7; +//│ let tmp10, tmp11; //│ if (isMut === true) { -//│ tmp6 = new Good1.class(); +//│ tmp10 = new Good1.class(); //│ } else { -//│ tmp6 = globalThis.Object.freeze(new Good1.class()); +//│ tmp10 = globalThis.Object.freeze(new Good1.class()); //│ } -//│ tmp7 = tmp6(x, y, z, f$capture4); -//│ return tmp7 +//│ tmp11 = tmp10(x, y, z, f$capture4); +//│ return tmp11 //│ }; //│ Good1 = function Good() { //│ return (x, y, z, f$capture4) => { @@ -130,24 +130,24 @@ f().foo() //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ foo() { -//│ let tmp6, tmp7; +//│ let tmp10, tmp11; //│ this.z = 100; -//│ tmp6 = this.x + this.y; -//│ tmp7 = tmp6 + this.z; -//│ return tmp7 + this.f$capture.w$capture$0 +//│ tmp10 = this.x + this.y; +//│ tmp11 = tmp10 + this.z; +//│ return tmp11 + this.f$capture.w$capture$0 //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Good", []]; //│ }); //│ Bad$ = function Bad$(isMut, f$capture4) { -//│ let tmp6, tmp7; +//│ let tmp10, tmp11; //│ if (isMut === true) { -//│ tmp6 = new Bad1.class(); +//│ tmp10 = new Bad1.class(); //│ } else { -//│ tmp6 = globalThis.Object.freeze(new Bad1.class()); +//│ tmp10 = globalThis.Object.freeze(new Bad1.class()); //│ } -//│ tmp7 = tmp6(f$capture4); -//│ return tmp7 +//│ tmp11 = tmp10(f$capture4); +//│ return tmp11 //│ }; //│ Bad1 = function Bad() { //│ return (f$capture4) => { @@ -185,18 +185,19 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let x, y, z, tmp6, tmp7, capture; +//│ let x, y, z, tmp10, tmp11, capture; //│ capture = new f$capture3(null); +//│ let w; /** scoped **/ //│ x = 1; //│ y = 10; //│ z = 10; //│ capture.w$capture$0 = 1000; -//│ tmp6 = Bad$(false, capture); -//│ tmp7 = tmp6.foo(); +//│ tmp10 = Bad$(false, capture); +//│ tmp11 = tmp10.foo(); //│ return Good$(false, x, y, z, capture) //│ }; -//│ tmp5 = f5(); -//│ runtime.safeCall(tmp5.foo()) +//│ tmp9 = f5(); +//│ runtime.safeCall(tmp9.foo()) //│ = 10111 :expect 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index 211460c6f5..df95d95b1e 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -17,6 +17,7 @@ fun f(x) = //│ }; //│ f = function f(x) { //│ let A1; +//│ let g; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index dfd2bc3676..86ea2da67f 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -88,6 +88,7 @@ f(1,2,3,4,5,6) //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { //│ let tmp; +//│ let g2; /** scoped **/ //│ tmp = g$4(a1, a2, a3, a4, a5, a6); //│ return tmp //│ }; @@ -183,6 +184,7 @@ f(1, 2, 1000) //│ f7 = function f(unused, immutable, mutated) { //│ let a1, tmp7, tmp8, capture; //│ capture = new f$capture5(mutated); +//│ let g3, h2; /** scoped **/ //│ a1 = g$6(immutable, capture); //│ tmp7 = h$2(capture); //│ tmp8 = a1 + tmp7; @@ -259,6 +261,7 @@ g()(1) //│ }; //│ f$1 = function f$(g$capture2, x1) { //│ let k; +//│ let h3; /** scoped **/ //│ k = 4; //│ g$capture2.y$capture$0 = 2; //│ return x1 @@ -281,6 +284,7 @@ g()(1) //│ g6 = function g() { //│ let capture, f$here; //│ capture = new g$capture1(null); +//│ let y1; /** scoped **/ //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index f3202dd07c..2b34cb6860 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -81,6 +81,7 @@ fun foo() = //│ foo2 = function foo() { //│ let tmp2, tmp3, capture; //│ capture = new foo$capture5(null, null); +//│ let x, tmp4, while1; /** scoped **/ //│ capture.x$capture$0 = 1; //│ capture.tmp$capture$1 = undefined; //│ tmp2 = while$1(capture); diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index 1ab37b7a93..c03f6dbe44 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -147,6 +147,7 @@ fun foo(x, y) = //│ foo4 = function foo(x, y) { //│ let tmp, M$1, capture; //│ capture = new foo$capture3(y); +//│ let foo31; /** scoped **/ //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); //│ tmp = foo3$(M$1, x, capture); //│ return tmp @@ -211,7 +212,8 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp3; +//│ let M17, tmp5; +//│ let tmp6; /** scoped **/ //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -220,7 +222,7 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut, tmp4; +//│ let scrut; //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); //│ }; @@ -237,17 +239,17 @@ M.A().get //│ }); //│ scrut = M16.A(); //│ if (scrut instanceof M16.A.class) { -//│ tmp4 = 2; +//│ tmp6 = 2; //│ } else { -//│ tmp4 = 3; +//│ tmp6 = 3; //│ } -//│ this.x = tmp4; +//│ this.x = tmp6; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp3 = M17.A(); -//│ tmp3.get +//│ tmp5 = M17.A(); +//│ tmp5.get //│ = 4 module M with diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 85b58b0d92..dbba90138c 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -34,6 +34,7 @@ fun foo() = //│ foo = function foo() { //│ let tmp, capture, bar$here; //│ capture = new foo$capture1(null); +//│ let x; /** scoped **/ //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); //│ tmp = runtime.safeCall(xs.push(bar$here)); diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 3883902479..25ac7189a4 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons1(tmp, 2) +//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; tmp1 = Cons1(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 789353450b..27429ef9e1 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -62,17 +62,17 @@ fun crazy(v) = //│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; //│ split_root$: { //│ split_1$: { -//│ if (v instanceof S1.class) { +//│ if (v instanceof S.class) { //│ argument0$ = v.value; -//│ if (argument0$ instanceof S1.class) { +//│ if (argument0$ instanceof S.class) { //│ argument0$1 = argument0$.value; -//│ if (argument0$1 instanceof S1.class) { +//│ if (argument0$1 instanceof S.class) { //│ argument0$2 = argument0$1.value; -//│ if (argument0$2 instanceof S1.class) { +//│ if (argument0$2 instanceof S.class) { //│ argument0$3 = argument0$2.value; -//│ if (argument0$3 instanceof S1.class) { +//│ if (argument0$3 instanceof S.class) { //│ argument0$4 = argument0$3.value; -//│ if (argument0$4 instanceof S1.class) { +//│ if (argument0$4 instanceof S.class) { //│ argument0$5 = argument0$4.value; //│ if (argument0$5 === 0) { //│ tmp = "bruh!"; @@ -99,12 +99,12 @@ fun crazy(v) = //│ break split_1$ //│ } //│ } -//│ tmp1 = S1(0); -//│ tmp2 = S1(tmp1); -//│ tmp3 = S1(tmp2); -//│ tmp4 = S1(tmp3); -//│ tmp5 = S1(tmp4); -//│ tmp = S1(tmp5); +//│ tmp1 = S(0); +//│ tmp2 = S(tmp1); +//│ tmp3 = S(tmp2); +//│ tmp4 = S(tmp3); +//│ tmp5 = S(tmp4); +//│ tmp = S(tmp5); //│ } //│ return tmp //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index 1c74e8bfff..aef9d23d6a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,21 +25,21 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, scrut5, tmp4; +//│ let scrut4, scrut5, tmp5; //│ split_root$2: { //│ split_1$2: { //│ scrut4 = true; //│ if (scrut4 === true) { //│ scrut5 = test(42); //│ if (scrut5 === true) { -//│ tmp4 = true; +//│ tmp5 = true; //│ break split_root$2 //│ } else { break split_1$2 } //│ } else { break split_1$2 } //│ } -//│ tmp4 = false; +//│ tmp5 = false; //│ } -//│ tmp4 +//│ tmp5 //│ > 42 //│ = false diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 4bba48a888..0ebb0ea24b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -379,7 +379,7 @@ fun foo(x, y, z) = //│ JS (unsanitized): //│ let foo3; //│ foo3 = function foo(x1, y1, z1) { -//│ if (x1 instanceof A1.class) { return "Hello" } else { return "Goodbye" } +//│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } //│ }; :sjs @@ -403,9 +403,9 @@ fun foo(x, y, z) = //│ let tmp6; //│ split_root$4: { //│ split_1$4: { -//│ if (x1 instanceof A1.class) { -//│ if (y1 instanceof B1.class) { -//│ if (z1 instanceof C1.class) { +//│ if (x1 instanceof A.class) { +//│ if (y1 instanceof B.class) { +//│ if (z1 instanceof C.class) { //│ tmp6 = "Hello"; //│ break split_root$4 //│ } else { @@ -429,9 +429,9 @@ fun foo(x, y, z) = //│ let tmp6; //│ split_root$4: { //│ split_default$: { -//│ if (x1 instanceof A1.class) { -//│ if (y1 instanceof B1.class) { -//│ if (z1 instanceof C1.class) { +//│ if (x1 instanceof A.class) { +//│ if (y1 instanceof B.class) { +//│ if (z1 instanceof C.class) { //│ tmp6 = "Hello"; //│ break split_root$4 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index 7a70cb4e1f..feb39394f2 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -14,11 +14,11 @@ x => if x is Pair(A, B) then 1 //│ let argument0$, argument1$, tmp; //│ split_root$: { //│ split_default$: { -//│ if (x instanceof Pair1.class) { +//│ if (x instanceof Pair.class) { //│ argument0$ = x.a; //│ argument1$ = x.b; -//│ if (argument0$ instanceof A1) { -//│ if (argument1$ instanceof B1) { +//│ if (argument0$ instanceof A) { +//│ if (argument1$ instanceof B) { //│ tmp = 1; //│ break split_root$ //│ } else { @@ -50,18 +50,18 @@ fun f(x) = if x is //│ let argument0$, argument1$, tmp; //│ split_root$: { //│ split_default$: { -//│ if (x instanceof Pair1.class) { +//│ if (x instanceof Pair.class) { //│ argument0$ = x.a; //│ argument1$ = x.b; -//│ if (argument0$ instanceof A1) { -//│ if (argument1$ instanceof A1) { +//│ if (argument0$ instanceof A) { +//│ if (argument1$ instanceof A) { //│ tmp = 1; //│ break split_root$ //│ } else { //│ break split_default$ //│ } -//│ } else if (argument0$ instanceof B1) { -//│ if (argument1$ instanceof B1) { +//│ } else if (argument0$ instanceof B) { +//│ if (argument1$ instanceof B) { //│ tmp = 2; //│ break split_root$ //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index ae076f1267..970b684cf9 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -41,7 +41,7 @@ fun foo(v) = //│ let tmp2; //│ split_root$2: { //│ split_1$2: { -//│ if (v instanceof A1) { break split_1$2 } else { break split_1$2 } +//│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } //│ } //│ tmp2 = 0; //│ } diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index f385992ea7..b203efa621 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -36,7 +36,7 @@ fun foo(x) = x is Cross //│ let foo; //│ foo = function foo(x2) { //│ let unapplyResult, output, bindings; -//│ unapplyResult = runtime.safeCall(Cross1.unapply(x2)); +//│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; //│ bindings = unapplyResult.bindings; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index 4a86244e76..e3c34d49df 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -23,7 +23,7 @@ pattern SumPair = Pair(a, b) => a + b //│ return a + b //│ }); //│ transform = lambda; -//│ if (input instanceof Pair1.class) { +//│ if (input instanceof Pair.class) { //│ argument0$ = input.first; //│ argument1$ = input.second; //│ transformResult = runtime.safeCall(transform(argument0$, argument1$)); diff --git a/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls b/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls index 188d640c44..9e5f309f9f 100644 --- a/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls +++ b/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls @@ -49,4 +49,4 @@ parseIntegers of "123\n456\n789" //│ ╔══[ERROR] Illegal juxtaposition right-hand side (identifier). //│ ║ l.48: "123\n456\n789" through Lines(Integer) // ==> [123, 456, 789] //│ ╙── ^^^^^^^ -//│ ═══[RUNTIME ERROR] TypeError: Lines1 is not a function +//│ ═══[RUNTIME ERROR] TypeError: Lines is not a function diff --git a/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls b/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls index 45139d43b0..1b18c8c894 100644 --- a/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls +++ b/hkmc2/shared/src/test/mlscript/ups/syntax/MixedParameters.mls @@ -38,14 +38,13 @@ if [0, 0] is P'(Bit, a, b) then "values: " + a + ", " + b //│ ═══[RUNTIME ERROR] Error: match error :fixme -:re // Order actually matters. But it seems that I didn't implement the error // reporting correctly. if [0, 0] is P(a, Bit, b) then "values: " + a + ", " + b //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'a' -//│ ║ l.44: if [0, 0] is P(a, Bit, b) then "values: " + a + ", " + b +//│ ║ l.43: if [0, 0] is P(a, Bit, b) then "values: " + a + ", " + b //│ ╙── ^ -//│ ═══[RUNTIME ERROR] ReferenceError: a is not defined +//│ = "values: 0, 0" pattern S(a, pattern Q, b) = [Q as a, Q as b] @@ -58,12 +57,11 @@ f([0, 0]) // Order actually matters. fun f(x) = if x is S(Bit, a, b) then [a, b] //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'a' -//│ ║ l.59: fun f(x) = if x is S(Bit, a, b) then [a, b] +//│ ║ l.58: fun f(x) = if x is S(Bit, a, b) then [a, b] //│ ╙── ^ -:re f([0, 0]) -//│ ═══[RUNTIME ERROR] ReferenceError: a is not defined +//│ = [0, 0] // The last thing to note is that if there is only one pattern parameter, it // will not be put into a tuple. @@ -86,6 +84,6 @@ if 0 is Q'(Bit) as y then y // Similarly, it cannot be written like this: if 0 is Q'(Bit, y) then y //│ ╔══[ERROR] Expected zero extraction arguments, but found one argument. -//│ ║ l.87: if 0 is Q'(Bit, y) then y +//│ ║ l.85: if 0 is Q'(Bit, y) then y //│ ╙── ^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error From 046827abc2e00eda326ae47ad7f25938ef4dd6f0 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 25 Nov 2025 01:41:19 +0800 Subject: [PATCH 25/72] wip: meaningful use scoped var a bit... now tests related to handler fail --- .../src/main/scala/hkmc2/codegen/Block.scala | 21 ++++++++++ .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../src/test/mlscript/codegen/Scoped.mls | 40 ++++++++++--------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 58cc197287..3dfe913d5d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -29,6 +29,27 @@ sealed abstract class Block extends Product: case _: End => true case _ => false + // FIXME: clean up this along with `definedVars` below... + lazy val definedVarsNoScoped: Set[Local] = this match + case _: Return | _: Throw => Set.empty + case Begin(sub, rst) => sub.definedVarsNoScoped ++ rst.definedVarsNoScoped + case Assign(l: TermSymbol, r, rst) => rst.definedVarsNoScoped + case Assign(l, r, rst) => rst.definedVarsNoScoped + l + case AssignField(l, n, r, rst) => rst.definedVarsNoScoped + case AssignDynField(l, n, ai, r, rst) => rst.definedVarsNoScoped + case Match(scrut, arms, dflt, rst) => + arms.flatMap(_._2.definedVarsNoScoped).toSet ++ dflt.toList.flatMap(_.definedVarsNoScoped) ++ rst.definedVarsNoScoped + case End(_) => Set.empty + case Break(_) => Set.empty + case Continue(_) => Set.empty + case Define(defn, rst) => + val rest = rst.definedVarsNoScoped + if defn.isOwned then rest else rest + defn.sym + // Note that the handler's LHS and body are not part of the current block, so we do not consider them here. + case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVarsNoScoped + res + case TryBlock(sub, fin, rst) => sub.definedVarsNoScoped ++ fin.definedVarsNoScoped ++ rst.definedVarsNoScoped + case Label(lbl, _, bod, rst) => bod.definedVarsNoScoped ++ rst.definedVarsNoScoped + case Scoped(syms, body) => body.definedVarsNoScoped -- syms lazy val definedVars: Set[Local] = this match case _: Return | _: Throw => Set.empty diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 24fda06ce3..c381f20519 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -611,7 +611,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = // println(s"$t :::::::: ${t.definedVars}") - val pre = blockPreamble(t.definedVars) + val pre = blockPreamble(t.definedVarsNoScoped) val rest = returningTerm(t, endSemi) pre :: rest diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 82211a48b5..11e6c1d868 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,9 +19,9 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f() { -//│ let scrut, a, b; +//│ let scrut, a, b; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -40,10 +40,12 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; +//│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { -//│ let a, tmp1, tmp2, lambda; +//│ let lambda; +//│ let tmp1, tmp2; /** scoped **/ //│ split_root$: { +//│ let a; /** scoped **/ //│ split_1$: { //│ if (x1 === true) { //│ break split_1$ @@ -83,10 +85,11 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f() { -//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; +//│ let tmp1; /** scoped **/ //│ split_root$: { +//│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ if (x instanceof A.class) { //│ argument0$ = x.a; @@ -121,10 +124,11 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ f2 = function f() { -//│ let scrut, argument0$, argument1$, tmp1; +//│ let tmp1; /** scoped **/ //│ split_root$: { +//│ let scrut, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ scrut = A(1, Nil); //│ if (scrut instanceof A.class) { @@ -160,12 +164,12 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(x1) { -//│ let tmp1, while1, tmp2, tmp3; +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, a1, argument0$, argument1$; +//│ let a, a1, argument0$, argument1$; /** scoped **/ //│ if (x1 instanceof A.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; @@ -200,14 +204,14 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ f4 = function f() { -//│ let y1, tmp1, while1, tmp2, tmp3; +//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ //│ y1 = x + 1; //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; //│ split_root$: { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_1$: { //│ if (y1 instanceof A.class) { //│ argument0$ = y1.a; @@ -248,12 +252,12 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; +//│ let f5; /** scoped **/ //│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; +//│ let scrut, lambda, tmp4; /** scoped **/ //│ lambda = (undefined, function () { //│ return y1 //│ }); From 12ec16e951518dc57563cf50af182e5800102549 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Wed, 26 Nov 2025 13:18:19 +0800 Subject: [PATCH 26/72] wip: nested Scoped blocks for pattern matching bodies with bugs --- .../main/scala/hkmc2/codegen/Lowering.scala | 4 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 25 ++--- .../main/scala/hkmc2/semantics/Split.scala | 19 ++-- .../hkmc2/semantics/ucs/Normalization.scala | 54 +++++++---- .../src/test/mlscript/codegen/Scoped.mls | 93 +++++++++++++++++-- 5 files changed, 143 insertions(+), 52 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 16d264cf85..cb3656c930 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -1020,9 +1020,9 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - possiblyScoped(scopedSyms, body) + possiblyScoped(scopedSyms)(body) - inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = + inline def possiblyScoped(syms: collection.Set[Symbol])(body: Block) = if syms.isEmpty then body else Scoped(syms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index c381f20519..aa0658d8fc 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -510,18 +510,19 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => - if scope.lookup(l).isDefined then - // raise: - // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) - None - else - Some(l -> scope.allocateName(l)) - (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc"; /** scoped **/") :: returningTerm(body, endSemi) + scope.nest.givenIn: + val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => + if scope.lookup(l).isDefined then + // raise: + // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) + None + else + Some(l -> scope.allocateName(l)) + (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc"; /** scoped **/") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index b16e22bc04..7935bea9cd 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -26,15 +26,16 @@ enum Split extends AutoLocated with ProductWithTail: def definedSyms: Set[Symbol] = this match case Cons(Branch(scrutinee, pattern, continuation), tail) => - tail.definedSyms ++ - locally: - continuation.definedSyms ++ - locally: - pattern match - case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) - case FlatPattern.Tuple(size, inf) => Set.empty // TODO: seems to be ok to leave this as empty? - case FlatPattern.Record(entries) => entries.unzip._2 - case FlatPattern.Lit(_) => Set.empty + Set.empty + // tail.definedSyms ++ + // locally: + // continuation.definedSyms ++ + // locally: + // pattern match + // case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) + // case FlatPattern.Tuple(size, inf) => Set.empty // TODO: seems to be ok to leave this as empty? + // case FlatPattern.Record(entries) => entries.unzip._2 + // case FlatPattern.Lit(_) => Set.empty case Let(sym, term, tail) => term.definedSyms ++ tail.definedSyms + sym case Else(default) => default.definedSyms case End => Set.empty diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 058ff505a7..895b8ba973 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -247,7 +247,9 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e End() ) pat match - case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) + case FlatPattern.Lit(lit) => mkMatch( + Case.Lit(lit) -> + lowering.inScopedBlock(tail.definedSyms)(lowerSplit(tail, cont, topLevel = false))) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = @@ -255,13 +257,17 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // Normalization should reject cases where the user provides // more sub-patterns than there are actual class parameters. assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) - def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using LoweringCtx): Case -> Block = args match - case Nil => - Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) - case (param, arg) :: args => - val (cse, blk) = mkArgs(args) - (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) - mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) + val cse -> blk = + LoweringCtx.nestScoped.givenIn: + def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol]): Case -> Block = args match + case Nil => + Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) + case (param, arg) :: args => + val (cse, blk) = mkArgs(args) + (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + val cse -> toBeScoped = mkArgs(clsParams.iterator.zip(args).toList) + cse -> lowering.possiblyScoped(tail.definedSyms ++ LoweringCtx.subst.getCollectedSym ++ args)(toBeScoped) + mkMatch(cse -> blk) symbol match case cls: ClassSymbol if ctx.builtins.virtualClasses contains cls => // [invariant:0] Some classes (e.g., `Int`) from `Prelude` do @@ -275,21 +281,28 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctor)(k(cls, cls.tree.clsParams)) case mod: ModuleOrObjectSymbol => subTerm_nonTail(ctor)(k(mod, Nil)) - case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) + case FlatPattern.Tuple(len, inf) => mkMatch( + Case.Tup(len, inf) -> + lowering.inScopedBlock(tail.definedSyms)(lowerSplit(tail, cont, topLevel = false))) case FlatPattern.Record(entries) => val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - entries.foldRight(lowerSplit(tail, cont, topLevel = false)): - case ((fieldName, fieldSymbol), blk) => - mkMatch( - Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - Assign(fieldSymbol, Select(sr, fieldName)(N), blk) - ) + LoweringCtx.nestScoped.givenIn: + val inner = lowerSplit(tail, cont, topLevel = false) + lowering.possiblyScoped(tail.definedSyms ++ LoweringCtx.subst.getCollectedSym ++ entries.unzip._2): + entries.foldRight(inner): + case ((fieldName, fieldSymbol), blk) => + mkMatch( + Case.Field(fieldName, safe = true), // we know we have an object, no need to check again + Assign(fieldSymbol, Select(sr, fieldName)(N), blk) + ) ) case Split.Else(els) => labels.get(els) match case S(label) => Break(label) - case N => term_nonTail(els)(cont.fold(identity, _(topLevel))) + case N => + // lowering.inScopedBlock(els.definedSyms): + term_nonTail(els)(cont.fold(identity, _(topLevel))) case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) /** @@ -315,7 +328,8 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e def apply(split: Split)(k: Result => Block)(using Config, LoweringCtx): Block = this(split, `if`, N, k) - private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = LoweringCtx.nestScoped.givenIn: + private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = + // LoweringCtx.nestScoped.givenIn: var usesResTmp = false // The symbol of the temporary variable for the result of the `if`-like term. // It will be created in one of the following situations. @@ -392,9 +406,9 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // `Label` so that `Break`s in the shared consequents can jump to the end. val body = val possiblyScoped = - lowering.possiblyScoped( - LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - mainBlock) + // lowering.possiblyScoped( + // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + mainBlock if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 11e6c1d868..21eb487cca 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -21,9 +21,9 @@ fun f() = //│ JS (unsanitized): //│ let f; /** scoped **/ //│ f = function f() { -//│ let scrut, a, b; /** scoped **/ +//│ let scrut, b; //│ scrut = true; -//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ if (scrut === true) { let a; /** scoped **/ a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -45,7 +45,6 @@ fun g(x, y, z) = //│ let lambda; //│ let tmp1, tmp2; /** scoped **/ //│ split_root$: { -//│ let a; /** scoped **/ //│ split_1$: { //│ if (x1 === true) { //│ break split_1$ @@ -54,6 +53,7 @@ fun g(x, y, z) = //│ break split_1$ //│ } else { //│ if (z === true) { +//│ let a; /** scoped **/ //│ a = 1; //│ lambda = (undefined, function (_0) { //│ return _0 + a @@ -89,9 +89,9 @@ fun f() = if x is //│ f1 = function f() { //│ let tmp1; /** scoped **/ //│ split_root$: { -//│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ if (x instanceof A.class) { +//│ let a, b, scrut, argument0$, argument1$; /** scoped **/ //│ argument0$ = x.a; //│ argument1$ = x.b; //│ b = argument1$; @@ -104,6 +104,7 @@ fun f() = if x is //│ break split_default$ //│ } //│ } else if (x instanceof B.class) { +//│ let c, d; /** scoped **/ //│ argument0$ = x.a; //│ argument1$ = x.b; //│ d = argument1$; @@ -126,12 +127,13 @@ fun f() = if A(1, Nil) is //│ JS (unsanitized): //│ let f2; /** scoped **/ //│ f2 = function f() { +//│ let scrut; //│ let tmp1; /** scoped **/ //│ split_root$: { -//│ let scrut, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ scrut = A(1, Nil); //│ if (scrut instanceof A.class) { +//│ let argument0$, argument1$; /** scoped **/ //│ argument0$ = scrut.a; //│ argument1$ = scrut.b; //│ if (argument0$ === 1) { @@ -169,14 +171,15 @@ fun f(x) = //│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, a1, argument0$, argument1$; /** scoped **/ //│ if (x1 instanceof A.class) { +//│ let a, argument0$, argument1$; /** scoped **/ //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a = argument0$; //│ tmp1 = runtime.safeCall(a()); //│ return while1() //│ } else if (x1 instanceof B.class) { +//│ let a1; /** scoped **/ //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a1 = argument0$; @@ -211,17 +214,19 @@ fun f() = //│ tmp1 = undefined; //│ while1 = (undefined, function () { //│ split_root$: { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_1$: { //│ if (y1 instanceof A.class) { +//│ let a, z, scrut, argument0$, argument1$; /** scoped **/ //│ argument0$ = y1.a; //│ argument1$ = y1.b; //│ a = argument1$; //│ z = argument0$; //│ scrut = false; //│ if (scrut === true) { +//│ let scrut1; /** scoped **/ //│ scrut1 = true; //│ if (scrut1 === true) { +//│ let tmp4; /** scoped **/ //│ tmp4 = z + 1; //│ y1 = tmp4; //│ tmp1 = runtime.Unit; @@ -254,15 +259,16 @@ fun f(x, y) = //│ JS (unsanitized): //│ let f5; /** scoped **/ //│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let lambda, tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; /** scoped **/ +//│ let scrut; //│ lambda = (undefined, function () { //│ return y1 //│ }); //│ scrut = runtime.short_and(x1, lambda); //│ if (scrut === true) { +//│ let tmp4; /** scoped **/ //│ tmp4 = f5(0, 0); //│ tmp1 = tmp4 + 4; //│ return while1() @@ -276,5 +282,74 @@ fun f(x, y) = //│ if (tmp3 === true) { return tmp2 } else { return tmp1 } //│ }; +data class X(x) +data class XX(x) + +:sjs +fun f(x) = if x is + X(t) and + false and true then 1 + else t + XX(t) then t +//│ JS (unsanitized): +//│ let f6; /** scoped **/ +//│ f6 = function f(x1) { +//│ let tmp1; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (x1 instanceof X.class) { +//│ let t, scrut, argument0$; /** scoped **/ +//│ argument0$ = x1.x; +//│ t = argument0$; +//│ scrut = false; +//│ if (scrut === true) { +//│ let scrut1; /** scoped **/ +//│ scrut1 = true; +//│ if (scrut1 === true) { +//│ tmp1 = 1; +//│ break split_root$ +//│ } else { +//│ break split_1$ +//│ } +//│ } else { +//│ break split_1$ +//│ } +//│ } else if (x1 instanceof XX.class) { +//│ let t1; /** scoped **/ +//│ argument0$ = x1.x; +//│ t1 = argument0$; +//│ tmp1 = t1; +//│ break split_root$ +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ } +//│ tmp1 = t; +//│ } +//│ return tmp1 +//│ }; + +// f(XX(3)) + + +:sjs +module M with + fun f() = 4 +//│ JS (unsanitized): +//│ let M3; /** scoped **/ +//│ globalThis.Object.freeze(class M2 { +//│ static { +//│ M3 = this +//│ } +//│ constructor() { +//│ runtime.Unit; +//│ } +//│ static f() { +//│ return 4 +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "M"]; +//│ }); + From a11aa2d9d02e98ab3978109bc3ff073239d7cf52 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Thu, 27 Nov 2025 02:02:20 +0800 Subject: [PATCH 27/72] Revert "wip: nested Scoped blocks for pattern matching bodies with bugs" This reverts commit 12ec16e951518dc57563cf50af182e5800102549. --- .../main/scala/hkmc2/codegen/Lowering.scala | 4 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 25 +++-- .../main/scala/hkmc2/semantics/Split.scala | 19 ++-- .../hkmc2/semantics/ucs/Normalization.scala | 54 ++++------- .../src/test/mlscript/codegen/Scoped.mls | 93 ++----------------- 5 files changed, 52 insertions(+), 143 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index cb3656c930..16d264cf85 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -1020,9 +1020,9 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - possiblyScoped(scopedSyms)(body) + possiblyScoped(scopedSyms, body) - inline def possiblyScoped(syms: collection.Set[Symbol])(body: Block) = + inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = if syms.isEmpty then body else Scoped(syms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index aa0658d8fc..c381f20519 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -510,19 +510,18 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - scope.nest.givenIn: - val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => - if scope.lookup(l).isDefined then - // raise: - // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) - None - else - Some(l -> scope.allocateName(l)) - (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc"; /** scoped **/") :: returningTerm(body, endSemi) + val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => + if scope.lookup(l).isDefined then + // raise: + // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) + None + else + Some(l -> scope.allocateName(l)) + (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc"; /** scoped **/") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 7935bea9cd..b16e22bc04 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -26,16 +26,15 @@ enum Split extends AutoLocated with ProductWithTail: def definedSyms: Set[Symbol] = this match case Cons(Branch(scrutinee, pattern, continuation), tail) => - Set.empty - // tail.definedSyms ++ - // locally: - // continuation.definedSyms ++ - // locally: - // pattern match - // case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) - // case FlatPattern.Tuple(size, inf) => Set.empty // TODO: seems to be ok to leave this as empty? - // case FlatPattern.Record(entries) => entries.unzip._2 - // case FlatPattern.Lit(_) => Set.empty + tail.definedSyms ++ + locally: + continuation.definedSyms ++ + locally: + pattern match + case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) + case FlatPattern.Tuple(size, inf) => Set.empty // TODO: seems to be ok to leave this as empty? + case FlatPattern.Record(entries) => entries.unzip._2 + case FlatPattern.Lit(_) => Set.empty case Let(sym, term, tail) => term.definedSyms ++ tail.definedSyms + sym case Else(default) => default.definedSyms case End => Set.empty diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 895b8ba973..058ff505a7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -247,9 +247,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e End() ) pat match - case FlatPattern.Lit(lit) => mkMatch( - Case.Lit(lit) -> - lowering.inScopedBlock(tail.definedSyms)(lowerSplit(tail, cont, topLevel = false))) + case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = @@ -257,17 +255,13 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // Normalization should reject cases where the user provides // more sub-patterns than there are actual class parameters. assert(argsOpt.isEmpty || args.length <= clsParams.length, (argsOpt, clsParams)) - val cse -> blk = - LoweringCtx.nestScoped.givenIn: - def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol]): Case -> Block = args match - case Nil => - Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) - case (param, arg) :: args => - val (cse, blk) = mkArgs(args) - (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) - val cse -> toBeScoped = mkArgs(clsParams.iterator.zip(args).toList) - cse -> lowering.possiblyScoped(tail.definedSyms ++ LoweringCtx.subst.getCollectedSym ++ args)(toBeScoped) - mkMatch(cse -> blk) + def mkArgs(args: Ls[TermSymbol -> BlockLocalSymbol])(using LoweringCtx): Case -> Block = args match + case Nil => + Case.Cls(ctorSym, st) -> lowerSplit(tail, cont, topLevel = false) + case (param, arg) :: args => + val (cse, blk) = mkArgs(args) + (cse, Assign(arg, Select(sr, new Tree.Ident(param.id.name).withLocOf(arg))(S(param)), blk)) + mkMatch(mkArgs(clsParams.iterator.zip(args).toList)) symbol match case cls: ClassSymbol if ctx.builtins.virtualClasses contains cls => // [invariant:0] Some classes (e.g., `Int`) from `Prelude` do @@ -281,28 +275,21 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctor)(k(cls, cls.tree.clsParams)) case mod: ModuleOrObjectSymbol => subTerm_nonTail(ctor)(k(mod, Nil)) - case FlatPattern.Tuple(len, inf) => mkMatch( - Case.Tup(len, inf) -> - lowering.inScopedBlock(tail.definedSyms)(lowerSplit(tail, cont, topLevel = false))) + case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.Record(entries) => val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), - LoweringCtx.nestScoped.givenIn: - val inner = lowerSplit(tail, cont, topLevel = false) - lowering.possiblyScoped(tail.definedSyms ++ LoweringCtx.subst.getCollectedSym ++ entries.unzip._2): - entries.foldRight(inner): - case ((fieldName, fieldSymbol), blk) => - mkMatch( - Case.Field(fieldName, safe = true), // we know we have an object, no need to check again - Assign(fieldSymbol, Select(sr, fieldName)(N), blk) - ) + entries.foldRight(lowerSplit(tail, cont, topLevel = false)): + case ((fieldName, fieldSymbol), blk) => + mkMatch( + Case.Field(fieldName, safe = true), // we know we have an object, no need to check again + Assign(fieldSymbol, Select(sr, fieldName)(N), blk) + ) ) case Split.Else(els) => labels.get(els) match case S(label) => Break(label) - case N => - // lowering.inScopedBlock(els.definedSyms): - term_nonTail(els)(cont.fold(identity, _(topLevel))) + case N => term_nonTail(els)(cont.fold(identity, _(topLevel))) case Split.End => labels.default.fold(throwMatchErrorBlock)(Break(_)) /** @@ -328,8 +315,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e def apply(split: Split)(k: Result => Block)(using Config, LoweringCtx): Block = this(split, `if`, N, k) - private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = - // LoweringCtx.nestScoped.givenIn: + private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = LoweringCtx.nestScoped.givenIn: var usesResTmp = false // The symbol of the temporary variable for the result of the `if`-like term. // It will be created in one of the following situations. @@ -406,9 +392,9 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // `Label` so that `Break`s in the shared consequents can jump to the end. val body = val possiblyScoped = - // lowering.possiblyScoped( - // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - mainBlock + lowering.possiblyScoped( + LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + mainBlock) if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 21eb487cca..11e6c1d868 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -21,9 +21,9 @@ fun f() = //│ JS (unsanitized): //│ let f; /** scoped **/ //│ f = function f() { -//│ let scrut, b; +//│ let scrut, a, b; /** scoped **/ //│ scrut = true; -//│ if (scrut === true) { let a; /** scoped **/ a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -45,6 +45,7 @@ fun g(x, y, z) = //│ let lambda; //│ let tmp1, tmp2; /** scoped **/ //│ split_root$: { +//│ let a; /** scoped **/ //│ split_1$: { //│ if (x1 === true) { //│ break split_1$ @@ -53,7 +54,6 @@ fun g(x, y, z) = //│ break split_1$ //│ } else { //│ if (z === true) { -//│ let a; /** scoped **/ //│ a = 1; //│ lambda = (undefined, function (_0) { //│ return _0 + a @@ -89,9 +89,9 @@ fun f() = if x is //│ f1 = function f() { //│ let tmp1; /** scoped **/ //│ split_root$: { +//│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ if (x instanceof A.class) { -//│ let a, b, scrut, argument0$, argument1$; /** scoped **/ //│ argument0$ = x.a; //│ argument1$ = x.b; //│ b = argument1$; @@ -104,7 +104,6 @@ fun f() = if x is //│ break split_default$ //│ } //│ } else if (x instanceof B.class) { -//│ let c, d; /** scoped **/ //│ argument0$ = x.a; //│ argument1$ = x.b; //│ d = argument1$; @@ -127,13 +126,12 @@ fun f() = if A(1, Nil) is //│ JS (unsanitized): //│ let f2; /** scoped **/ //│ f2 = function f() { -//│ let scrut; //│ let tmp1; /** scoped **/ //│ split_root$: { +//│ let scrut, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ scrut = A(1, Nil); //│ if (scrut instanceof A.class) { -//│ let argument0$, argument1$; /** scoped **/ //│ argument0$ = scrut.a; //│ argument1$ = scrut.b; //│ if (argument0$ === 1) { @@ -171,15 +169,14 @@ fun f(x) = //│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { +//│ let a, a1, argument0$, argument1$; /** scoped **/ //│ if (x1 instanceof A.class) { -//│ let a, argument0$, argument1$; /** scoped **/ //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a = argument0$; //│ tmp1 = runtime.safeCall(a()); //│ return while1() //│ } else if (x1 instanceof B.class) { -//│ let a1; /** scoped **/ //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a1 = argument0$; @@ -214,19 +211,17 @@ fun f() = //│ tmp1 = undefined; //│ while1 = (undefined, function () { //│ split_root$: { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_1$: { //│ if (y1 instanceof A.class) { -//│ let a, z, scrut, argument0$, argument1$; /** scoped **/ //│ argument0$ = y1.a; //│ argument1$ = y1.b; //│ a = argument1$; //│ z = argument0$; //│ scrut = false; //│ if (scrut === true) { -//│ let scrut1; /** scoped **/ //│ scrut1 = true; //│ if (scrut1 === true) { -//│ let tmp4; /** scoped **/ //│ tmp4 = z + 1; //│ y1 = tmp4; //│ tmp1 = runtime.Unit; @@ -259,16 +254,15 @@ fun f(x, y) = //│ JS (unsanitized): //│ let f5; /** scoped **/ //│ f5 = function f(x1, y1) { -//│ let lambda, tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let scrut; +//│ let scrut, lambda, tmp4; /** scoped **/ //│ lambda = (undefined, function () { //│ return y1 //│ }); //│ scrut = runtime.short_and(x1, lambda); //│ if (scrut === true) { -//│ let tmp4; /** scoped **/ //│ tmp4 = f5(0, 0); //│ tmp1 = tmp4 + 4; //│ return while1() @@ -282,74 +276,5 @@ fun f(x, y) = //│ if (tmp3 === true) { return tmp2 } else { return tmp1 } //│ }; -data class X(x) -data class XX(x) - -:sjs -fun f(x) = if x is - X(t) and - false and true then 1 - else t - XX(t) then t -//│ JS (unsanitized): -//│ let f6; /** scoped **/ -//│ f6 = function f(x1) { -//│ let tmp1; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (x1 instanceof X.class) { -//│ let t, scrut, argument0$; /** scoped **/ -//│ argument0$ = x1.x; -//│ t = argument0$; -//│ scrut = false; -//│ if (scrut === true) { -//│ let scrut1; /** scoped **/ -//│ scrut1 = true; -//│ if (scrut1 === true) { -//│ tmp1 = 1; -//│ break split_root$ -//│ } else { -//│ break split_1$ -//│ } -//│ } else { -//│ break split_1$ -//│ } -//│ } else if (x1 instanceof XX.class) { -//│ let t1; /** scoped **/ -//│ argument0$ = x1.x; -//│ t1 = argument0$; -//│ tmp1 = t1; -//│ break split_root$ -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ } -//│ tmp1 = t; -//│ } -//│ return tmp1 -//│ }; - -// f(XX(3)) - - -:sjs -module M with - fun f() = 4 -//│ JS (unsanitized): -//│ let M3; /** scoped **/ -//│ globalThis.Object.freeze(class M2 { -//│ static { -//│ M3 = this -//│ } -//│ constructor() { -//│ runtime.Unit; -//│ } -//│ static f() { -//│ return 4 -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "M"]; -//│ }); - From 50253aed7650f1ec6705ffa15f28f2ae31860963 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Thu, 27 Nov 2025 22:50:40 +0800 Subject: [PATCH 28/72] wip: try to fix errors in handler tests --- .../scala/hkmc2/codegen/HandlerLowering.scala | 41 ++++++++++++++----- .../main/scala/hkmc2/codegen/Lowering.scala | 8 ++-- .../hkmc2/semantics/ucs/Normalization.scala | 2 +- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index 97a13183b4..cb0ef27098 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -559,18 +559,32 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, doUnwindBlk )(false) val doUnwindLazy = Lazy(doUnwindSym.asPath) - val rst = genNormalBody(b, cls.sym, S(doUnwindLazy)) + val rst0 = genNormalBody(b, cls.sym, S(doUnwindLazy)) + val scopedSyms -> rst = rst0 match + case Scoped(syms, body) => syms -> body + case _ => Set.empty[Symbol] -> rst0 - if doUnwindLazy.isEmpty && opt.stackSafety.isEmpty then - blockBuilder - .define(cls) - .rest(rst) - else - blockBuilder - .define(cls) - .define(doUnwindDef) - .rest(rst) + // println(scopedSyms) + // println(rst) + Lowering.possiblyScoped( + scopedSyms, + locally: + val res = + if doUnwindLazy.isEmpty && opt.stackSafety.isEmpty then + blockBuilder + .define(cls) + .rest(rst) + else + blockBuilder + .define(cls) + .define(doUnwindDef) + .rest(rst) + // println(res) + res + ) + if opt.debug then + // println(getLocalsFn) Define(getLocalsFn, ret) else ret @@ -580,7 +594,12 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, // to ensure the fun and class references in the continuation class are properly scoped, // we move all function defns to the top level of the handler block val (blk, defns) = b.floatOutDefns() - defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) + blk match + case Scoped(s, blk) => + val res = Scoped(s, defns.foldLeft(blk)((acc, defn) => Define(defn, acc))) + // println(s">>>>>>> $res") + res + case blk => defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) private def locToStr(l: Loc): Str = Scope.replaceInvalidCharacters(l.origin.fileName.last + "_L" + l.origin.startLineNum + "_" + l.spanStart + "_" + l.spanEnd) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 16d264cf85..648ce49af2 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -65,6 +65,9 @@ end LoweringCtx import LoweringCtx.subst +object Lowering: + inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = + if syms.isEmpty then body else Scoped(syms, body) class Lowering()(using Config, TL, Raise, State, Ctx): @@ -1020,10 +1023,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - possiblyScoped(scopedSyms, body) - - inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = - if syms.isEmpty then body else Scoped(syms, body) + Lowering.possiblyScoped(scopedSyms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 058ff505a7..8d4c906711 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -392,7 +392,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // `Label` so that `Break`s in the shared consequents can jump to the end. val body = val possiblyScoped = - lowering.possiblyScoped( + Lowering.possiblyScoped( LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, mainBlock) if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) From 1871ea29882314306dc95c4cf4ad6023ff131034 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 28 Nov 2025 01:00:27 +0800 Subject: [PATCH 29/72] wip: smart constructors..? --- .../src/main/scala/hkmc2/codegen/Block.scala | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 3dfe913d5d..244eda5cb0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -320,6 +320,47 @@ case class AssignDynField(lhs: Path, fld: Path, arrayIdx: Bool, rhs: Result, res case class Define(defn: Defn, rest: Block) extends Block with ProductWithTail +object Match: + def apply(scrut: Path, arms: Ls[Case -> Block], dflt: Opt[Block], rest: Block): Block = rest match + case Scoped(syms, body) => Scoped(syms, Match(scrut, arms, dflt, body)) + case _ => new Match(scrut, arms, dflt, rest) +object Label: + def apply(label: Local, loop: Bool, body: Block, rest: Block): Block = rest match + case Scoped(syms, rest) => Scoped(syms, Label(label, loop, body, rest)) + case _ => new Label(label, loop, body, rest) +object Scoped: + def apply(syms: collection.Set[Local], body: Block): Block = body match + case Scoped(syms2, body) => + if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body) else Scoped(syms ++ syms2, body) + case _ => + if syms.isEmpty then body else new Scoped(syms, body) +object Begin: + def apply(sub: Block, rest: Block): Block = (sub, rest) match + case (Scoped(symsSub, bodySub), Scoped(symsRest, bodyRest)) => + Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest)) + case (Scoped(symsSub, bodySub), _) => Scoped(symsSub, Begin(bodySub, rest)) + case (_, Scoped(symsRest, bodyRest)) => Scoped(symsRest, Begin(sub, bodyRest)) + case _ => new Begin(sub, rest) +object TryBlock: + def apply(sub: Block, finallyDo: Block, rest: Block): Block = rest match + case Scoped(syms, body) => Scoped(syms, TryBlock(sub, finallyDo, body)) + case _ => new TryBlock(sub, finallyDo, rest) +object Assign: + def apply(lhs: Local, rhs: Result, rest: Block): Block = rest match + case Scoped(syms, body) => Scoped(syms, Assign(lhs, rhs, body)) + case _ => new Assign(lhs, rhs, rest) +object AssignField: + def apply(lhs: Path, nme: Tree.Ident, rhs: Result, rest: Block)(symbol: Opt[MemberSymbol]): Block = rest match + case Scoped(syms, body) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol)) + case _ => new AssignField(lhs, nme, rhs, rest)(symbol) +object AssignDynField: + def apply(lhs: Path, fld: Path, arrayIdx: Bool, rhs: Result, rest: Block): Block = rest match + case Scoped(syms, body) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body)) + case _ => new AssignDynField(lhs, fld, arrayIdx, rhs, rest) +object Define: + def apply(defn: Defn, rest: Block): Block = rest match + case Scoped(syms, body) => Scoped(syms, Define(defn, body)) + case _ => new Define(defn, rest) case class HandleBlock( lhs: Local, @@ -333,6 +374,41 @@ case class HandleBlock( ) extends Block with ProductWithTail +object HandleBlock: + def apply( + lhs: Local, + res: Local, + par: Path, + args: Ls[Path], + cls: ClassSymbol, + handlers: Ls[Handler], + body: Block, + rest: Block + ) = rest match + case Scoped(syms, rest) => + Scoped( + syms, + new HandleBlock( + lhs, + res, + par, + args, + cls, + handlers, + body, + rest + )) + case _ => new HandleBlock( + lhs, + res, + par, + args, + cls, + handlers, + body, + rest) + + sealed abstract class Defn: val innerSym: Opt[MemberSymbol] val sym: BlockMemberSymbol From dde2adf43898e2cf45c10e0e7586a8ab55a05036 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 28 Nov 2025 01:09:04 +0800 Subject: [PATCH 30/72] Revert "wip: try to fix errors in handler tests" This reverts commit 50253aed7650f1ec6705ffa15f28f2ae31860963. --- .../scala/hkmc2/codegen/HandlerLowering.scala | 41 +++++-------------- .../main/scala/hkmc2/codegen/Lowering.scala | 8 ++-- .../hkmc2/semantics/ucs/Normalization.scala | 2 +- 3 files changed, 16 insertions(+), 35 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index cb0ef27098..97a13183b4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -559,32 +559,18 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, doUnwindBlk )(false) val doUnwindLazy = Lazy(doUnwindSym.asPath) - val rst0 = genNormalBody(b, cls.sym, S(doUnwindLazy)) - val scopedSyms -> rst = rst0 match - case Scoped(syms, body) => syms -> body - case _ => Set.empty[Symbol] -> rst0 + val rst = genNormalBody(b, cls.sym, S(doUnwindLazy)) - // println(scopedSyms) - // println(rst) - Lowering.possiblyScoped( - scopedSyms, - locally: - val res = - if doUnwindLazy.isEmpty && opt.stackSafety.isEmpty then - blockBuilder - .define(cls) - .rest(rst) - else - blockBuilder - .define(cls) - .define(doUnwindDef) - .rest(rst) - // println(res) - res - ) - + if doUnwindLazy.isEmpty && opt.stackSafety.isEmpty then + blockBuilder + .define(cls) + .rest(rst) + else + blockBuilder + .define(cls) + .define(doUnwindDef) + .rest(rst) if opt.debug then - // println(getLocalsFn) Define(getLocalsFn, ret) else ret @@ -594,12 +580,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, // to ensure the fun and class references in the continuation class are properly scoped, // we move all function defns to the top level of the handler block val (blk, defns) = b.floatOutDefns() - blk match - case Scoped(s, blk) => - val res = Scoped(s, defns.foldLeft(blk)((acc, defn) => Define(defn, acc))) - // println(s">>>>>>> $res") - res - case blk => defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) + defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) private def locToStr(l: Loc): Str = Scope.replaceInvalidCharacters(l.origin.fileName.last + "_L" + l.origin.startLineNum + "_" + l.spanStart + "_" + l.spanEnd) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 648ce49af2..16d264cf85 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -65,9 +65,6 @@ end LoweringCtx import LoweringCtx.subst -object Lowering: - inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = - if syms.isEmpty then body else Scoped(syms, body) class Lowering()(using Config, TL, Raise, State, Ctx): @@ -1023,7 +1020,10 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - Lowering.possiblyScoped(scopedSyms, body) + possiblyScoped(scopedSyms, body) + + inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = + if syms.isEmpty then body else Scoped(syms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 8d4c906711..058ff505a7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -392,7 +392,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // `Label` so that `Break`s in the shared consequents can jump to the end. val body = val possiblyScoped = - Lowering.possiblyScoped( + lowering.possiblyScoped( LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, mainBlock) if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) From 112f4199da7f28839b9550a51c87a6ce2ff8f518 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 28 Nov 2025 02:33:56 +0800 Subject: [PATCH 31/72] wip: all tests can pass by not putting scoped blocks for UCS under the root label... this was causing problems because some definitions inside branches are floated out to the top level --- .../scala/hkmc2/semantics/ucs/Normalization.scala | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 058ff505a7..b0d5618c75 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -392,10 +392,15 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // `Label` so that `Break`s in the shared consequents can jump to the end. val body = val possiblyScoped = - lowering.possiblyScoped( - LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - mainBlock) - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + // lowering.possiblyScoped( + // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + // mainBlock) + mainBlock + // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + Scoped( + LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + ) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = From 4aa8c35c745eb9144a99c6c1da8e2727eb1584b2 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 28 Nov 2025 02:36:10 +0800 Subject: [PATCH 32/72] update tests --- .../src/test/mlscript-compile/Predef.mjs | 39 +- .../src/test/mlscript-compile/Runtime.mjs | 362 ++++++++++-------- .../OverloadedModulesInSignatures.mls | 4 +- .../backlog/NonReturningStatements.mls | 2 +- .../src/test/mlscript/backlog/ToTriage.mls | 60 +-- .../src/test/mlscript/basics/BadDefs.mls | 4 +- .../basics/CompanionModules_Classes.mls | 3 +- .../shared/src/test/mlscript/basics/Drop.mls | 9 +- .../test/mlscript/basics/FunnyRecordKeys.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 9 +- .../src/test/mlscript/basics/LazySpreads.mls | 8 +- .../mlscript/basics/MemberProjections.mls | 4 +- .../test/mlscript/basics/MiscArrayTests.mls | 2 +- .../mlscript/basics/MultiParamListClasses.mls | 9 +- .../test/mlscript/basics/MultiParamLists.mls | 22 +- .../mlscript/basics/MultilineExpressions.mls | 2 +- .../src/test/mlscript/basics/MutArr.mls | 4 +- .../src/test/mlscript/basics/MutRcd.mls | 2 +- .../src/test/mlscript/basics/MutVal.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../src/test/mlscript/basics/PartialApps.mls | 9 +- .../mlscript/basics/PureTermStatements.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 12 +- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/basics/Underscores.mls | 9 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 32 +- .../src/test/mlscript/bbml/bbGetters.mls | 17 +- .../src/test/mlscript/codegen/Arrays.mls | 4 +- .../src/test/mlscript/codegen/BadInit.mls | 4 +- .../src/test/mlscript/codegen/BadNew.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 2 +- .../src/test/mlscript/codegen/BuiltinOps.mls | 4 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 4 +- .../test/mlscript/codegen/CaseShorthand.mls | 14 +- .../test/mlscript/codegen/ClassInClass.mls | 5 +- .../src/test/mlscript/codegen/ClassInFun.mls | 8 +- .../test/mlscript/codegen/ClassMatching.mls | 18 +- .../src/test/mlscript/codegen/Classes.mls | 8 +- .../src/test/mlscript/codegen/Comma.mls | 20 +- .../src/test/mlscript/codegen/ConsoleLog.mls | 6 +- .../test/mlscript/codegen/DelayedLetInit.mls | 18 +- .../src/test/mlscript/codegen/EarlyReturn.mls | 4 +- .../src/test/mlscript/codegen/Formatting.mls | 21 +- .../src/test/mlscript/codegen/FunInClass.mls | 35 +- .../test/mlscript/codegen/FunctionsThis.mls | 5 +- .../src/test/mlscript/codegen/Getters.mls | 48 +-- .../src/test/mlscript/codegen/GlobalThis.mls | 6 +- .../src/test/mlscript/codegen/Hygiene.mls | 24 +- .../src/test/mlscript/codegen/IfThenElse.mls | 10 +- .../src/test/mlscript/codegen/ImportMLs.mls | 10 +- .../src/test/mlscript/codegen/ImportedOps.mls | 8 +- .../test/mlscript/codegen/InlineLambdas.mls | 12 +- .../src/test/mlscript/codegen/Lambdas.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 21 +- .../test/mlscript/codegen/ModuleMethods.mls | 3 +- .../src/test/mlscript/codegen/Modules.mls | 11 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 6 +- .../test/mlscript/codegen/ParamClasses.mls | 33 +- .../src/test/mlscript/codegen/PartialApps.mls | 27 +- .../test/mlscript/codegen/PlainClasses.mls | 28 +- .../src/test/mlscript/codegen/Primes.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/Quasiquotes.mls | 9 +- .../src/test/mlscript/codegen/RandomStuff.mls | 26 +- .../test/mlscript/codegen/SanityChecks.mls | 8 +- .../src/test/mlscript/codegen/Scoped.mls | 11 +- .../test/mlscript/codegen/SelfReferences.mls | 5 +- .../src/test/mlscript/codegen/SetIn.mls | 30 +- .../src/test/mlscript/codegen/Spreads.mls | 2 +- .../shared/src/test/mlscript/codegen/This.mls | 7 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/ThisCalls.mls | 2 +- .../src/test/mlscript/codegen/Throw.mls | 6 +- .../src/test/mlscript/codegen/TraceLog.mls | 4 +- .../src/test/mlscript/codegen/UnitValue.mls | 10 +- .../src/test/mlscript/codegen/While.mls | 40 +- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/ctx/EtaExpansion.mls | 35 +- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../src/test/mlscript/handlers/Debugging.mls | 16 +- .../src/test/mlscript/handlers/Effects.mls | 6 +- .../test/mlscript/handlers/EffectsHygiene.mls | 4 +- .../mlscript/handlers/EffectsInClasses.mls | 3 +- .../mlscript/handlers/RecursiveHandlers.mls | 34 +- .../test/mlscript/handlers/SetInHandlers.mls | 2 +- .../test/mlscript/handlers/StackSafety.mls | 18 +- .../src/test/mlscript/interop/Arrays.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 9 +- .../test/mlscript/lifter/CompanionsInFun.mls | 6 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 8 +- .../src/test/mlscript/lifter/FunInFun.mls | 43 ++- .../src/test/mlscript/lifter/Imports.mls | 2 +- .../shared/src/test/mlscript/lifter/Loops.mls | 16 +- .../test/mlscript/lifter/ModulesObjects.mls | 27 +- .../src/test/mlscript/lifter/Mutation.mls | 16 +- .../test/mlscript/lifter/StackSafetyLift.mls | 18 +- .../src/test/mlscript/objbuf/Mutation.mls | 7 +- .../src/test/mlscript/parser/PrefixOps.mls | 4 +- .../test/mlscript/std/FingerTreeListTest.mls | 6 +- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 6 +- .../ucs/general/LogicalConnectives.mls | 2 +- .../ucs/normalization/Deduplication.mls | 41 +- .../normalization/ExcessiveDeduplication.mls | 2 +- .../ucs/normalization/SimplePairMatches.mls | 6 +- .../ucs/patterns/ConjunctionPattern.mls | 4 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 12 +- .../src/test/mlscript/ups/MatchResult.mls | 4 +- .../src/test/mlscript/ups/SimpleTransform.mls | 5 +- 110 files changed, 868 insertions(+), 745 deletions(-) diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index a4ce900a78..e7f4e8a4fa 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -5,7 +5,7 @@ import Term from "./Term.mjs"; import RuntimeJS from "./RuntimeJS.mjs"; import Runtime from "./Runtime.mjs"; import Rendering from "./Rendering.mjs"; -let Predef1; +let Predef1; /** scoped **/ globalThis.Object.freeze(class Predef { static { Predef1 = this @@ -57,12 +57,12 @@ globalThis.Object.freeze(class Predef { return runtime.safeCall(f(x)) } static tap(x, f) { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(f(x)); return (tmp , x) } static pat(f, x) { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(f(x)); return (tmp , x) } @@ -71,14 +71,14 @@ globalThis.Object.freeze(class Predef { } static andThen(f, g) { return (x) => { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(f(x)); return runtime.safeCall(g(tmp)) } } static compose(f, g) { return (x) => { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(g(x)); return runtime.safeCall(f(tmp)) } @@ -99,7 +99,7 @@ globalThis.Object.freeze(class Predef { } } static print(...xs) { - let tmp, tmp1; + let tmp, tmp1; /** scoped **/ tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); tmp1 = runtime.safeCall(tmp(...xs)); return runtime.safeCall(globalThis.console.log(...tmp1)) @@ -112,7 +112,7 @@ globalThis.Object.freeze(class Predef { } } static notImplemented(msg) { - let tmp; + let tmp; /** scoped **/ tmp = "Not implemented: " + msg; throw globalThis.Error(tmp) } @@ -124,7 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; + let len, scrut, i, init, tmp; /** scoped **/ len = rest.length; scrut = len == 0; if (scrut === true) { @@ -132,18 +132,19 @@ globalThis.Object.freeze(class Predef { } else { i = len - 1; init = runtime.safeCall(rest.at(i)); - tmp4: while (true) { + tmp1: while (true) { + let scrut1, tmp2, tmp3, tmp4; /** scoped **/ scrut1 = i > 0; if (scrut1 === true) { - tmp = i - 1; - i = tmp; - tmp1 = runtime.safeCall(rest.at(i)); - tmp2 = runtime.safeCall(f(tmp1, init)); - init = tmp2; - tmp3 = runtime.Unit; - continue tmp4 + tmp2 = i - 1; + i = tmp2; + tmp3 = runtime.safeCall(rest.at(i)); + tmp4 = runtime.safeCall(f(tmp3, init)); + init = tmp4; + tmp = runtime.Unit; + continue tmp1 } else { - tmp3 = runtime.Unit; + tmp = runtime.Unit; } break; } @@ -152,9 +153,9 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda, tmp; + let lambda, tmp; /** scoped **/ lambda = (undefined, function (acc, x) { - let tmp1, tmp2, tmp3; + let tmp1, tmp2, tmp3; /** scoped **/ if (typeof x === 'string') { tmp1 = true; } else { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 7cb9792f43..a2cb048716 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -6,7 +6,7 @@ import RuntimeJS from "./RuntimeJS.mjs"; import Rendering from "./Rendering.mjs"; import LazyArray from "./LazyArray.mjs"; import Iter from "./Iter.mjs"; -let Runtime1; +let Runtime1; /** scoped **/ globalThis.Object.freeze(class Runtime { static { Runtime1 = this @@ -74,10 +74,10 @@ globalThis.Object.freeze(class Runtime { } #_reified; resumeWith(value) { - let lambda; + let lambda; /** scoped **/ const this$EffectHandle = this; lambda = (undefined, function () { - let tmp; + let tmp; /** scoped **/ tmp = Runtime.resume(this$EffectHandle.reified.contTrace); return runtime.safeCall(tmp(value)) }); @@ -127,12 +127,12 @@ globalThis.Object.freeze(class Runtime { this.split = LazyArray.__split; } static slice(xs, i, j) { - let tmp; + let tmp; /** scoped **/ tmp = xs.length - j; return xs.slice(i, tmp) } static lazySlice(xs, i, j) { - let tmp; + let tmp; /** scoped **/ tmp = LazyArray.dropLeftRight(i, j); return runtime.safeCall(tmp(xs)) } @@ -140,7 +140,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(LazyArray.__concat(...args)) } static get(xs, i) { - let scrut, scrut1, tmp, tmp1, tmp2; + let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ scrut = i >= xs.length; if (scrut === true) { throw globalThis.RangeError("Tuple.get: index out of bounds") @@ -173,7 +173,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(string.startsWith(prefix)) } static get(string, i) { - let scrut; + let scrut; /** scoped **/ scrut = i >= string.length; if (scrut === true) { throw globalThis.RangeError("Str.get: index out of bounds") @@ -209,7 +209,7 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let scrut, prev, tmp; + let scrut, prev, tmp; /** scoped **/ scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -221,7 +221,7 @@ globalThis.Object.freeze(class Runtime { } } static resetIndent(n) { - let scrut; + let scrut; /** scoped **/ scrut = TraceLogger.enabled; if (scrut === true) { TraceLogger.indentLvl = n; @@ -231,7 +231,7 @@ globalThis.Object.freeze(class Runtime { } } static log(msg) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4; + let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ scrut = TraceLogger.enabled; if (scrut === true) { tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); @@ -387,7 +387,7 @@ globalThis.Object.freeze(class Runtime { }) } delay() { - let lambda; + let lambda; /** scoped **/ lambda = (undefined, function (k) { Runtime.stackResume = k; return runtime.Unit @@ -409,13 +409,13 @@ globalThis.Object.freeze(class Runtime { } #v; zext() { - let tmp, tmp1; + let tmp, tmp1; /** scoped **/ tmp = Runtime.shl(1, 31); tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); return Runtime.bitand(this.#v, tmp1) } sext() { - let tmp; + let tmp; /** scoped **/ tmp = Runtime.shl(1, 31); return Runtime.bitor(this.#v, tmp) } @@ -427,8 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; - let lambda1; /** scoped **/ + let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -438,34 +437,35 @@ globalThis.Object.freeze(class Runtime { }); scrut = runtime.short_or(tmp, lambda); if (scrut === true) { + let scrut1, scrut2, tmp12; /** scoped **/ scrut1 = functionName.length > 0; if (scrut1 === true) { - tmp1 = " '" + functionName; - tmp2 = tmp1 + "'"; + tmp12 = " '" + functionName; + tmp1 = tmp12 + "'"; } else { - tmp2 = ""; + tmp1 = ""; } - name = tmp2; - tmp3 = "Function" + name; - tmp4 = tmp3 + " expected "; + name = tmp1; + tmp2 = "Function" + name; + tmp3 = tmp2 + " expected "; if (isUB === true) { - tmp5 = ""; + tmp4 = ""; } else { - tmp5 = "at least "; + tmp4 = "at least "; } - tmp6 = tmp4 + tmp5; - tmp7 = tmp6 + expected; - tmp8 = tmp7 + " argument"; + tmp5 = tmp3 + tmp4; + tmp6 = tmp5 + expected; + tmp7 = tmp6 + " argument"; scrut2 = expected === 1; if (scrut2 === true) { - tmp9 = ""; + tmp8 = ""; } else { - tmp9 = "s"; + tmp8 = "s"; } - tmp10 = tmp8 + tmp9; - tmp11 = tmp10 + " but got "; - tmp12 = tmp11 + got; - throw globalThis.Error(tmp12) + tmp9 = tmp7 + tmp8; + tmp10 = tmp9 + " but got "; + tmp11 = tmp10 + got; + throw globalThis.Error(tmp11) } else { return runtime.Unit } @@ -485,7 +485,7 @@ globalThis.Object.freeze(class Runtime { } } static deboundMethod(mtdName, clsName) { - let tmp, tmp1, tmp2, tmp3; + let tmp, tmp1, tmp2, tmp3; /** scoped **/ tmp = "[debinding error] Method '" + mtdName; tmp1 = tmp + "' of class '"; tmp2 = tmp1 + clsName; @@ -493,7 +493,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error(tmp3) } static try(f) { - let res; + let res; /** scoped **/ res = runtime.safeCall(f()); if (res instanceof Runtime.EffectSig.class) { return Runtime.EffectHandle(res) @@ -502,7 +502,7 @@ globalThis.Object.freeze(class Runtime { } } static printRaw(x) { - let rcd, tmp; + let rcd, tmp; /** scoped **/ rcd = globalThis.Object.freeze({ indent: 2, breakLength: 76 @@ -514,66 +514,72 @@ globalThis.Object.freeze(class Runtime { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } static topLevelEffect(tr, debug) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; - tmp6: while (true) { + let tmp, tmp1; /** scoped **/ + tmp2: while (true) { + let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ scrut = tr.handler === Runtime.PrintStackEffect; if (scrut === true) { - tmp = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); - tmp1 = runtime.safeCall(globalThis.console.log(tmp)); - tmp2 = Runtime.resume(tr.contTrace); - tmp3 = runtime.safeCall(tmp2(runtime.Unit)); - tr = tmp3; - tmp4 = runtime.Unit; - continue tmp6 + tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); + tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); + tmp5 = Runtime.resume(tr.contTrace); + tmp6 = runtime.safeCall(tmp5(runtime.Unit)); + tr = tmp6; + tmp = runtime.Unit; + continue tmp2 } else { - tmp4 = runtime.Unit; + tmp = runtime.Unit; } break; } if (tr instanceof Runtime.EffectSig.class) { - tmp5 = "Error: Unhandled effect " + tr.handler.constructor.name; - throw Runtime.showStackTrace(tmp5, tr, debug, false) + tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; + throw Runtime.showStackTrace(tmp1, tr, debug, false) } else { return tr } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; + let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { - tmp19: while (true) { + let tmp3; /** scoped **/ + tmp4: while (true) { + let scrut, cur, tmp5, tmp6; /** scoped **/ scrut = curHandler !== null; if (scrut === true) { + let scrut1, tmp7, tmp8; /** scoped **/ cur = curHandler.next; - tmp20: while (true) { - scrut1 = cur !== null; - if (scrut1 === true) { + tmp9: while (true) { + let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + scrut2 = cur !== null; + if (scrut2 === true) { + let scrut3, lambda, tmp19, tmp20; /** scoped **/ locals = cur.getLocals; - tmp = locals.length - 1; - curLocals = runtime.safeCall(locals.at(tmp)); + tmp10 = locals.length - 1; + curLocals = runtime.safeCall(locals.at(tmp10)); loc = cur.getLoc; if (loc === null) { - tmp1 = "pc=" + cur.pc; + tmp11 = "pc=" + cur.pc; } else { - tmp1 = loc; + tmp11 = loc; } - loc1 = tmp1; + loc1 = tmp11; split_root$: { split_1$: { if (showLocals === true) { - scrut2 = curLocals.locals.length > 0; - if (scrut2 === true) { + scrut3 = curLocals.locals.length > 0; + if (scrut3 === true) { lambda = (undefined, function (l) { - let tmp21, tmp22; + let tmp21, tmp22; /** scoped **/ tmp21 = l.localName + "="; tmp22 = Rendering.render(l.value); return tmp21 + tmp22 }); - tmp2 = runtime.safeCall(curLocals.locals.map(lambda)); - tmp3 = runtime.safeCall(tmp2.join(", ")); - tmp4 = " with locals: " + tmp3; + tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); + tmp20 = runtime.safeCall(tmp19.join(", ")); + tmp12 = " with locals: " + tmp20; break split_root$ } else { break split_1$ @@ -582,66 +588,67 @@ globalThis.Object.freeze(class Runtime { break split_1$ } } - tmp4 = ""; + tmp12 = ""; } - localsMsg = tmp4; - tmp5 = "\n\tat " + curLocals.fnName; - tmp6 = tmp5 + " ("; - tmp7 = tmp6 + loc1; - tmp8 = tmp7 + ")"; - tmp9 = msg + tmp8; - msg = tmp9; - tmp10 = msg + localsMsg; - msg = tmp10; + localsMsg = tmp12; + tmp13 = "\n\tat " + curLocals.fnName; + tmp14 = tmp13 + " ("; + tmp15 = tmp14 + loc1; + tmp16 = tmp15 + ")"; + tmp17 = msg + tmp16; + msg = tmp17; + tmp18 = msg + localsMsg; + msg = tmp18; cur = cur.next; atTail = false; - tmp11 = runtime.Unit; - continue tmp20 + tmp5 = runtime.Unit; + continue tmp9 } else { - tmp11 = runtime.Unit; + tmp5 = runtime.Unit; } break; } curHandler = curHandler.nextHandler; - scrut3 = curHandler !== null; - if (scrut3 === true) { - tmp12 = "\n\twith handler " + curHandler.handler.constructor.name; - tmp13 = msg + tmp12; - msg = tmp13; + scrut1 = curHandler !== null; + if (scrut1 === true) { + tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; + tmp8 = msg + tmp7; + msg = tmp8; atTail = false; - tmp14 = runtime.Unit; + tmp6 = runtime.Unit; } else { - tmp14 = runtime.Unit; + tmp6 = runtime.Unit; } - tmp15 = tmp14; - continue tmp19 + tmp = tmp6; + continue tmp4 } else { - tmp15 = runtime.Unit; + tmp = runtime.Unit; } break; } if (atTail === true) { - tmp16 = msg + "\n\tat tail position"; - msg = tmp16; - tmp17 = runtime.Unit; + tmp3 = msg + "\n\tat tail position"; + msg = tmp3; + tmp1 = runtime.Unit; } else { - tmp17 = runtime.Unit; + tmp1 = runtime.Unit; } - tmp18 = tmp17; + tmp2 = tmp1; } else { - tmp18 = runtime.Unit; + tmp2 = runtime.Unit; } return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; + let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { + let scrut, tmp5, tmp6, tmp7; /** scoped **/ tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut3, tmp8, tmp9; - scrut3 = runtime.safeCall(m.has(cont)); - if (scrut3 === true) { + let scrut1, tmp8, tmp9; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { tmp8 = ", " + marker; tmp9 = result + tmp8; result = tmp9; @@ -653,24 +660,26 @@ globalThis.Object.freeze(class Runtime { tmp1 = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - tmp2 = reps + 1; - reps = tmp2; + let scrut1; /** scoped **/ + tmp5 = reps + 1; + reps = tmp5; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp3 = runtime.Unit; + tmp6 = runtime.Unit; } - tmp4 = result + ", REPEAT"; - result = tmp4; - tmp5 = runtime.Unit; + tmp7 = result + ", REPEAT"; + result = tmp7; + tmp2 = runtime.Unit; } else { - tmp5 = runtime.safeCall(vis.add(cont)); + tmp2 = runtime.safeCall(vis.add(cont)); } - tmp6 = result + ") -> "; - tmp7 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp6 + tmp7 + tmp3 = result + ") -> "; + tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp3 + tmp4 } else { + let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -680,13 +689,14 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ if (cont instanceof Runtime.HandlerContFrame.class) { + let scrut, tmp4, tmp5, tmp6; /** scoped **/ result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut3, tmp7, tmp8; - scrut3 = runtime.safeCall(m.has(cont)); - if (scrut3 === true) { + let scrut1, tmp7, tmp8; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { tmp7 = ", " + marker; tmp8 = result + tmp7; result = tmp8; @@ -698,24 +708,26 @@ globalThis.Object.freeze(class Runtime { tmp = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - tmp1 = reps + 1; - reps = tmp1; + let scrut1; /** scoped **/ + tmp4 = reps + 1; + reps = tmp4; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp2 = runtime.Unit; + tmp5 = runtime.Unit; } - tmp3 = result + ", REPEAT"; - result = tmp3; - tmp4 = runtime.Unit; + tmp6 = result + ", REPEAT"; + result = tmp6; + tmp1 = runtime.Unit; } else { - tmp4 = runtime.safeCall(vis.add(cont)); + tmp1 = runtime.safeCall(vis.add(cont)); } - tmp5 = result + " -> "; - tmp6 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp5 + tmp6 + tmp2 = result + " -> "; + tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp2 + tmp3 } else { + let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -725,22 +737,23 @@ globalThis.Object.freeze(class Runtime { } } static debugCont(cont) { - let tmp, tmp1, tmp2; + let tmp, tmp1, tmp2; /** scoped **/ tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugHandler(cont) { - let tmp, tmp1, tmp2; + let tmp, tmp1, tmp2; /** scoped **/ tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; + let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ if (contTrace instanceof Runtime.ContTrace.class) { + let scrut, scrut1; /** scoped **/ tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; if (scrut === true) { @@ -769,27 +782,28 @@ globalThis.Object.freeze(class Runtime { tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); cur = contTrace.nextHandler; - tmp15: while (true) { + tmp13: while (true) { + let scrut2, tmp14, tmp15; /** scoped **/ scrut2 = cur !== null; if (scrut2 === true) { - tmp11 = Runtime.showHandlerContChain(cur, hl, vis, 0); - tmp12 = runtime.safeCall(globalThis.console.log(tmp11)); + tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); + tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); cur = cur.nextHandler; - tmp13 = runtime.Unit; - continue tmp15 + tmp11 = runtime.Unit; + continue tmp13 } else { - tmp13 = runtime.Unit; + tmp11 = runtime.Unit; } break; } return runtime.safeCall(globalThis.console.log()) } else { - tmp14 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); + tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); return runtime.safeCall(globalThis.console.log(contTrace)) } } static debugEff(eff) { - let tmp, tmp1, tmp2, tmp3; + let tmp, tmp1, tmp2, tmp3; /** scoped **/ if (eff instanceof Runtime.EffectSig.class) { tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); @@ -801,7 +815,7 @@ globalThis.Object.freeze(class Runtime { } } static mkEffect(handler, handlerFun) { - let res, tmp; + let res, tmp; /** scoped **/ tmp = new Runtime.ContTrace.class(null, null, null, null, false); res = new Runtime.EffectSig.class(tmp, handler, handlerFun); res.contTrace.last = res.contTrace; @@ -809,7 +823,7 @@ globalThis.Object.freeze(class Runtime { return res } static handleBlockImpl(cur, handler) { - let handlerFrame; + let handlerFrame; /** scoped **/ handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); cur.contTrace.lastHandler.nextHandler = handlerFrame; cur.contTrace.lastHandler = handlerFrame; @@ -817,7 +831,7 @@ globalThis.Object.freeze(class Runtime { return Runtime.handleEffects(cur) } static enterHandleBlock(handler, body) { - let cur; + let cur; /** scoped **/ cur = runtime.safeCall(body()); if (cur instanceof Runtime.EffectSig.class) { return Runtime.handleBlockImpl(cur, handler) @@ -826,36 +840,39 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let nxt, scrut, tmp, tmp1; - tmp2: while (true) { + let tmp; /** scoped **/ + tmp1: while (true) { + let nxt, tmp2; /** scoped **/ if (cur instanceof Runtime.EffectSig.class) { + let scrut; /** scoped **/ nxt = Runtime.handleEffect(cur); scrut = cur === nxt; if (scrut === true) { return cur } else { cur = nxt; - tmp = runtime.Unit; + tmp2 = runtime.Unit; } - tmp1 = tmp; - continue tmp2 + tmp = tmp2; + continue tmp1 } else { return cur } break; } - return tmp1 + return tmp } static handleEffect(cur) { - let prevHandlerFrame, scrut, scrut1, scrut2, handlerFrame, saved, scrut3, scrut4, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; + let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ prevHandlerFrame = cur.contTrace; tmp6: while (true) { + let scrut1, scrut2; /** scoped **/ split_root$: { split_1$: { - scrut = prevHandlerFrame.nextHandler !== null; - if (scrut === true) { - scrut1 = prevHandlerFrame.nextHandler.handler !== cur.handler; - if (scrut1 === true) { + scrut1 = prevHandlerFrame.nextHandler !== null; + if (scrut1 === true) { + scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; + if (scrut2 === true) { prevHandlerFrame = prevHandlerFrame.nextHandler; tmp = runtime.Unit; continue tmp6 @@ -870,8 +887,8 @@ globalThis.Object.freeze(class Runtime { } break; } - scrut2 = prevHandlerFrame.nextHandler === null; - if (scrut2 === true) { + scrut = prevHandlerFrame.nextHandler === null; + if (scrut === true) { return cur } else { tmp1 = runtime.Unit; @@ -886,6 +903,7 @@ globalThis.Object.freeze(class Runtime { tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); cur = tmp3; if (cur instanceof Runtime.EffectSig.class) { + let scrut3, scrut4; /** scoped **/ scrut3 = saved.next !== null; if (scrut3 === true) { cur.contTrace.last.next = saved.next; @@ -909,7 +927,7 @@ globalThis.Object.freeze(class Runtime { } static resume(contTrace) { return (value) => { - let scrut, tmp, tmp1; + let scrut, tmp, tmp1; /** scoped **/ scrut = contTrace.resumed; if (scrut === true) { throw globalThis.Error("Multiple resumption") @@ -922,55 +940,58 @@ globalThis.Object.freeze(class Runtime { } } static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, scrut, scrut1, tmp, tmp1, tmp2, tmp3, tmp4; + let cont, handlerCont, curDepth, tmp; /** scoped **/ cont = contTrace.next; handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; - tmp5: while (true) { + tmp1: while (true) { + let tmp2, tmp3; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - tmp = runtime.safeCall(cont.resume(value)); - value = tmp; + let tmp4, tmp5; /** scoped **/ + tmp2 = runtime.safeCall(cont.resume(value)); + value = tmp2; Runtime.stackDepth = curDepth; if (value instanceof Runtime.EffectSig.class) { + let scrut, scrut1; /** scoped **/ value.contTrace.last.next = cont.next; value.contTrace.lastHandler.nextHandler = handlerCont; scrut = contTrace.last !== cont; if (scrut === true) { value.contTrace.last = contTrace.last; - tmp1 = runtime.Unit; + tmp4 = runtime.Unit; } else { - tmp1 = runtime.Unit; + tmp4 = runtime.Unit; } scrut1 = handlerCont !== null; if (scrut1 === true) { value.contTrace.lastHandler = contTrace.lastHandler; - tmp2 = runtime.Unit; + tmp5 = runtime.Unit; } else { - tmp2 = runtime.Unit; + tmp5 = runtime.Unit; } return value } else { cont = cont.next; tmp3 = runtime.Unit; } - tmp4 = tmp3; - continue tmp5 + tmp = tmp3; + continue tmp1 } else { if (handlerCont instanceof Runtime.HandlerContFrame.class) { cont = handlerCont.next; handlerCont = handlerCont.nextHandler; - tmp4 = runtime.Unit; - continue tmp5 + tmp = runtime.Unit; + continue tmp1 } else { return value } } break; } - return tmp4 + return tmp } static checkDepth() { - let scrut, tmp, lambda; + let scrut, tmp, lambda; /** scoped **/ tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -983,24 +1004,25 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, scrut, saved, tmp, tmp1; + let result, tmp; /** scoped **/ Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); Runtime.stackDepth = 1; - tmp2: while (true) { + tmp1: while (true) { + let scrut, saved, tmp2; /** scoped **/ scrut = Runtime.stackResume !== null; if (scrut === true) { saved = Runtime.stackResume; Runtime.stackResume = null; - tmp = runtime.safeCall(saved()); - result = tmp; + tmp2 = runtime.safeCall(saved()); + result = tmp2; Runtime.stackDepth = 1; - tmp1 = runtime.Unit; - continue tmp2 + tmp = runtime.Unit; + continue tmp1 } else { - tmp1 = runtime.Unit; + tmp = runtime.Unit; } break; } @@ -1010,7 +1032,7 @@ globalThis.Object.freeze(class Runtime { return result } static plus_impl(lhs, rhs) { - let tmp; + let tmp; /** scoped **/ split_root$: { split_1$: { if (lhs instanceof Runtime.Int31.class) { diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index da40af046e..07725a0283 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,7 +81,7 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; f5 = function f() { let tmp4; tmp4 = TrmMod(); return tmp4 }; +//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 }; :fixme :expect 42 @@ -92,7 +92,7 @@ f :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; f6 = function f() { return TrmMod.class }; +//│ let f6; /** scoped **/ f6 = function f() { return TrmMod.class }; fun assertModule(module m: ClsMod): module ClsMod = m diff --git a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls index 630a7d1107..18ba41070d 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls @@ -31,7 +31,7 @@ fun foo = :sjs foo //│ JS (unsanitized): -//│ let tmp2; tmp2 = foo2(); tmp2 +//│ let tmp2; /** scoped **/ tmp2 = foo2(); tmp2 //│ > 1 //│ > ... diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 71e5c6cda1..9ac7a2e8e9 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -64,7 +64,7 @@ Infinity :sjs val Infinity = 1 //│ JS (unsanitized): -//│ let Infinity; Infinity = 1; +//│ let Infinity; /** scoped **/ Infinity = 1; //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Identifier 'Infinity' has already been declared //│ Infinity = Infinity @@ -105,7 +105,7 @@ fun main() = object Cls(val x) with fun huh = x //│ JS (unsanitized): -//│ let Cls1; +//│ let Cls1; /** scoped **/ //│ globalThis.Object.freeze(class Cls { //│ static { //│ Cls1.class = globalThis.Object.freeze(new this) @@ -202,7 +202,7 @@ class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let D1; +//│ let D1; /** scoped **/ //│ globalThis.Object.freeze(class D extends Predef.id { //│ static { //│ D1 = this @@ -251,9 +251,11 @@ set (x += 1; ()) fun baz = 1 fun bar() = baz //│ JS (unsanitized): -//│ let bar, baz; -//│ baz = function baz() { return 1 }; -//│ bar = function bar() { let tmp4; tmp4 = baz(); return tmp4 }; +//│ let bar, baz; /** scoped **/ +//│ baz = function baz() { +//│ return 1 +//│ }; +//│ bar = function bar() { let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 }; // ——— ——— ——— @@ -262,13 +264,13 @@ import "../../mlscript-compile/Stack.mls" // The parser rejects this, but it should be allowed. open Stack { ::, Nil } //│ ╔══[ERROR] Illegal 'open' statement shape. -//│ ║ l.263: open Stack { ::, Nil } +//│ ║ l.265: open Stack { ::, Nil } //│ ╙── ^^^^^^^^^^^^^^^ // Instead, if we parenthesize the operator, it is rejected by the elaborator. open Stack { (::), Nil } //│ ╔══[ERROR] Illegal 'open' statement element. -//│ ║ l.269: open Stack { (::), Nil } +//│ ║ l.271: open Stack { (::), Nil } //│ ╙── ^^^^ open Stack { Nil, :: } @@ -276,7 +278,7 @@ open Stack { Nil, :: } :sjs 1 :: Nil //│ ╔══[ERROR] Module 'Stack' does not contain member '::' -//│ ║ l.277: 1 :: Nil +//│ ║ l.279: 1 :: Nil //│ ╙── ^^ //│ JS (unsanitized): //│ Stack["::"](1, Stack.Nil) @@ -287,9 +289,9 @@ open Stack { Nil, :: } mkStr of ... // hello "oops" //│ ╔══[PARSE ERROR] Expected start of expression in this position; found new line instead -//│ ║ l.287: mkStr of ... // hello +//│ ║ l.289: mkStr of ... // hello //│ ║ ^ -//│ ║ l.288: "oops" +//│ ║ l.290: "oops" //│ ╙── //│ = "oops" @@ -312,25 +314,25 @@ Foo(1, 2, 3).args :todo if Foo(1, 2, 3) is Foo(...args) then args //│ ╔══[ERROR] Unrecognized pattern (spread). -//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^^^^ //│ ╔══[ERROR] Name not found: args -//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^ //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╔══[ERROR] The constructor does not take any arguments but found three arguments. -//│ ║ l.325: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] +//│ ║ l.327: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(arg) then arg //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.331: if Foo(1, 2, 3) is Foo(arg) then arg +//│ ║ l.333: if Foo(1, 2, 3) is Foo(arg) then arg //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error @@ -352,7 +354,7 @@ fun w(txt) = fs.writeFileSync(outFilePath, txt) // () //│ JS (unsanitized): -//│ let w; w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; +//│ let w; /** scoped **/ w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; w("whoops") //│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value. @@ -364,10 +366,10 @@ fun foo() = let bar(x: Str): Str = x + x bar("test") //│ ╔══[ERROR] Unsupported let binding shape -//│ ║ l.364: let bar(x: Str): Str = x + x +//│ ║ l.366: let bar(x: Str): Str = x + x //│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Expected 0 arguments, got 1 -//│ ║ l.365: bar("test") +//│ ║ l.367: bar("test") //│ ╙── ^^^^^^^^ // ——— ——— ——— @@ -377,10 +379,10 @@ fun foo() = module Foo(x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.377: module Foo(x) +//│ ║ l.379: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -401,10 +403,10 @@ module Foo(x) module Foo(val x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.401: module Foo(val x) +//│ ║ l.403: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7; /** scoped **/ //│ globalThis.Object.freeze(class Foo6 { //│ static { //│ Foo7 = this @@ -424,7 +426,7 @@ module Foo(val x) // TODO support syntax? data class Foo(mut x) //│ ╔══[ERROR] Expected a valid parameter, found 'mut'-modified identifier -//│ ║ l.425: data class Foo(mut x) +//│ ║ l.427: data class Foo(mut x) //│ ╙── ^ // ——— ——— ——— @@ -441,10 +443,10 @@ set this.pc = 0 set (this).pc = 0 //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.440: set this.pc = 0 +//│ ║ l.442: set this.pc = 0 //│ ╙── ^^^^ //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.442: (this).pc = 0 +//│ ║ l.444: (this).pc = 0 //│ ╙── ^^^^ // But this doesn't... @@ -453,10 +455,10 @@ set set this.pc = 0 //│ ╔══[PARSE ERROR] Unexpected static selector here -//│ ║ l.454: this.pc = 0 +//│ ║ l.456: this.pc = 0 //│ ╙── ^^^ //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.454: this.pc = 0 +//│ ║ l.456: this.pc = 0 //│ ╙── ^^^^ // ——— ——— ——— @@ -468,7 +470,7 @@ pattern A(pattern B) = B fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ║ l.469: fun foo(x) = if x is @annotations.compile A(0) as y then y +//│ ║ l.471: fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╙── ^ // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls index 037ca2f59d..8c99b7c926 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls @@ -15,7 +15,7 @@ x :sjs val ++ = 0 //│ JS (unsanitized): -//│ let $_$_; $_$_ = 0; +//│ let $_$_; /** scoped **/ $_$_ = 0; //│ ++ = 0 :sjs @@ -51,7 +51,7 @@ fun ++ z = 0 :sjs ++ //│ JS (unsanitized): -//│ let tmp; tmp = $_$_2(); tmp +//│ let tmp; /** scoped **/ tmp = $_$_2(); tmp //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index b2a425d898..1dc6b6f2b7 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,8 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1, tmp1; -//│ let tmp2; /** scoped **/ +//│ let Foo1, tmp1, tmp2; /** scoped **/ //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index 9d957653bc..ce93f69fb6 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp, tmp1; /** scoped **/ tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; +//│ let a, b, tmp2, tmp3; /** scoped **/ //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a @@ -22,7 +22,10 @@ drop { a: 0, b: 1 } :sjs let discard = drop _ //│ JS (unsanitized): -//│ let discard, discard1; discard1 = function discard(_0) { return runtime.Unit }; discard = discard1; +//│ let discard; +//│ let discard1; /** scoped **/ +//│ discard = function discard(_0) { return runtime.Unit }; +//│ discard1 = discard; //│ discard = fun discard discard of { a: 0, b: 1 } diff --git a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls index d91b3fba16..708cfa4897 100644 --- a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls +++ b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls @@ -6,7 +6,7 @@ { a: 1 } //│ JS (unsanitized): -//│ let a; a = 1; globalThis.Object.freeze({ a: a }) +//│ let a; /** scoped **/ a = 1; globalThis.Object.freeze({ a: a }) //│ = {a: 1} { "a": 1 } @@ -35,7 +35,7 @@ :sjs { (id(0)): 1 } //│ JS (unsanitized): -//│ let tmp; tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) +//│ let tmp; /** scoped **/ tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) //│ = {"0": 1} diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index 38009e7283..7f9f887e0c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,8 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let Baz1; -//│ let tmp; /** scoped **/ +//│ let Baz1, tmp; /** scoped **/ //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; @@ -100,8 +99,8 @@ data class Barr(x: Int)(y: Int) with data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with fun g = z //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.100: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with -//│ ╙── ^^^^^^^^^^^^^^^^^^^ +//│ ║ l.99: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with +//│ ╙── ^^^^^^^^^^^^^^^^^^^ :todo Bazz(42).f @@ -111,7 +110,7 @@ Bazz(42).f new Barr(40)(2) with fun f = 43 //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.111: new Barr(40)(2) with +//│ ║ l.110: new Barr(40)(2) with //│ ╙── ^^^^^^^^^^^ //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index 26fb4b1450..dd9ce4d58f 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -17,11 +17,11 @@ fun buildPalindrome = case 0 then [] n then [n, ..buildPalindrome(n - 1), n] //│ JS (unsanitized): -//│ let buildPalindrome; +//│ let buildPalindrome; /** scoped **/ //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; +//│ let n, tmp3, tmp4, tmp5; /** scoped **/ //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -48,11 +48,11 @@ fun sum2 = case [] then 0 [x, ..xs, y] then x + y + sum2(xs) //│ JS (unsanitized): -//│ let sum2; +//│ let sum2; /** scoped **/ //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls index c22fcf695c..1464825716 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls @@ -46,7 +46,7 @@ M.Foo:: n(foo, 2) :sjs let m = M.Foo::m //│ JS (unsanitized): -//│ let m, m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; +//│ let m; let m1; /** scoped **/ m = function m(self, ...args) { return self.m(...args) }; m1 = m; //│ m = fun m m(foo) @@ -132,7 +132,7 @@ Foo::n(foo, 2) //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ═══[ERROR] Expected a statically known class; found ‹error›. //│ JS (unsanitized): -//│ let lambda9; +//│ let lambda9; /** scoped **/ //│ lambda9 = (undefined, function (self, ...args) { //│ return runtime.safeCall(self.n(...args)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls index 2c3bbe2920..4cffa34572 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls @@ -54,7 +54,7 @@ xs \ //│ ║ l.52: map(x => x * 2) //│ ╙── ^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let lambda5, tmp4; +//│ let lambda5, tmp4; /** scoped **/ //│ lambda5 = (undefined, function (x) { return x * 2 }); //│ tmp4 = map(lambda5); //│ Predef.passTo(xs1, tmp4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index 2c3b5349e7..dae4535fa9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -77,13 +77,14 @@ Foo(1, 2) :sjs let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): -//│ let f, f1; -//│ f1 = function f(x, y) { -//│ let tmp4; +//│ let f; +//│ let f1; /** scoped **/ +//│ f = function f(x, y) { +//│ let tmp4; /** scoped **/ //│ tmp4 = Foo7(x, y); //│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) //│ }; -//│ f = f1; +//│ f1 = f; //│ f = fun f // Eta-expansion happens here. See EtaExpansion.mls. diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 178203f9e4..9c2202fc04 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -6,7 +6,7 @@ fun f(n1: Int): Int = n1 //│ JS (unsanitized): -//│ let f; f = function f(n1) { return n1 }; +//│ let f; /** scoped **/ f = function f(n1) { return n1 }; f(42) //│ JS (unsanitized): @@ -19,23 +19,24 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): -//│ let f1; f1 = function f(n1) { return (n2) => { let tmp; tmp = 10 * n1; return tmp + n2 } }; +//│ let f1; /** scoped **/ +//│ f1 = function f(n1) { return (n2) => { let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } }; // TODO compile this to // this.f$(4, 2) f(4)(2) //│ JS (unsanitized): -//│ let tmp; tmp = f1(4); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = f1(4); runtime.safeCall(tmp(2)) //│ = 42 fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ let tmp1, tmp2, tmp3; +//│ let tmp1, tmp2, tmp3; /** scoped **/ //│ tmp1 = 10 * n1; //│ tmp2 = tmp1 + n2; //│ tmp3 = 10 * tmp2; @@ -46,17 +47,20 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) +//│ let tmp1, tmp2; /** scoped **/ +//│ tmp1 = f2(4); +//│ tmp2 = runtime.safeCall(tmp1(2)); +//│ runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { //│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ //│ tmp3 = 10 * n1; //│ tmp4 = tmp3 + n2; //│ tmp5 = 10 * tmp4; @@ -70,7 +74,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; +//│ let tmp3, tmp4, tmp5; /** scoped **/ //│ tmp3 = f3(3); //│ tmp4 = runtime.safeCall(tmp3(0)); //│ tmp5 = runtime.safeCall(tmp4(3)); diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index 5025320ce4..aa73bdb0d9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,7 +80,7 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; +//│ let scrut, tmp17; /** scoped **/ //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls index 58a398ea87..d95bc500e7 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls @@ -4,7 +4,7 @@ :sjs let t = [0, 1, 2] //│ JS (unsanitized): -//│ let t; t = globalThis.Object.freeze([ 0, 1, 2 ]); +//│ let t; /** scoped **/ t = globalThis.Object.freeze([ 0, 1, 2 ]); //│ t = [0, 1, 2] :re @@ -21,7 +21,7 @@ t :sjs let t = mut [0, 1, 2] //│ JS (unsanitized): -//│ let t1; t1 = [ 0, 1, 2 ]; +//│ let t1; /** scoped **/ t1 = [ 0, 1, 2 ]; //│ t = [0, 1, 2] t.push(4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls index 0c8a48f3a4..7efae81a91 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls @@ -46,7 +46,7 @@ r.foo :sjs let r = {} //│ JS (unsanitized): -//│ let r3; r3 = runtime.Unit; +//│ let r3; /** scoped **/ r3 = runtime.Unit; //│ r = () :re diff --git a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls index 408d7db105..655c4d0a45 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls @@ -33,7 +33,7 @@ O.gy :sjs class Foo(x, val y, mut val z) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x, y, z) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index 660664e019..790005a3c5 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; f = globalThis.Object.freeze(new Foo()); +//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; f1 = new Foo(); +//│ let f1; /** scoped **/ f1 = new Foo(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index 99fe9b0fdb..e1e6e3a5f0 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -109,16 +109,19 @@ passTo(1, add(., 1) @ _ + _)(2) :sjs let f = add(_, 1) //│ JS (unsanitized): -//│ let f, f1; f1 = function f(_0) { let tmp15; tmp15 = add(); return tmp15(_0, 1) }; f = f1; +//│ let f; +//│ let f1; /** scoped **/ +//│ f = function f(_0) { let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) }; +//│ f1 = f; //│ f = fun f :fixme let f = add(., 1) //│ ╔══[PARSE ERROR] Expected an expression; found period instead -//│ ║ l.116: let f = add(., 1) +//│ ║ l.119: let f = add(., 1) //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected period here -//│ ║ l.116: let f = add(., 1) +//│ ║ l.119: let f = add(., 1) //│ ╙── ^ //│ ═══[RUNTIME ERROR] Error: Function expected 2 arguments but got 0 //│ f = undefined diff --git a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls index 36ad46feac..c9d079ed1c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls @@ -56,7 +56,7 @@ fun foo() = :sjs 1; id(2) //│ JS (unsanitized): -//│ let tmp; tmp = Predef.id(2); (1 , tmp) +//│ let tmp; /** scoped **/ tmp = Predef.id(2); (1 , tmp) //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index df1eb55c0e..02207fb129 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -4,7 +4,9 @@ :sjs 1 && 2 //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function () { return 2 }); runtime.short_and(1, lambda) +//│ let lambda; /** scoped **/ +//│ lambda = (undefined, function () { return 2 }); +//│ runtime.short_and(1, lambda) //│ = 2 1 || 2 @@ -18,7 +20,9 @@ fun test(x) = :sjs 123 || test(42) //│ JS (unsanitized): -//│ let lambda2; lambda2 = (undefined, function () { return test(42) }); runtime.short_or(123, lambda2) +//│ let lambda2; /** scoped **/ +//│ lambda2 = (undefined, function () { return test(42) }); +//│ runtime.short_or(123, lambda2) //│ = 123 0 || test(42) @@ -32,9 +36,9 @@ fun test(x) = :sjs fold(||)(0, false, 42, 123) //│ JS (unsanitized): -//│ let lambda5, tmp; +//│ let lambda5, tmp; /** scoped **/ //│ lambda5 = (undefined, function (arg1, arg2) { -//│ let lambda6; +//│ let lambda6; /** scoped **/ //│ lambda6 = (undefined, function () { //│ return arg2 //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index 666d7e6f4c..b9d1163e58 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ let scrut; scrut = "a"; if (typeof scrut === 'string') { true } else { false } +//│ let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index 1d443c419c..dcb0d0ba4b 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -61,9 +61,10 @@ print(_) :sjs let test = _.f(0, _, 2) //│ JS (unsanitized): -//│ let test, test1; -//│ test1 = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; -//│ test = test1; +//│ let test; +//│ let test1; /** scoped **/ +//│ test = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; +//│ test1 = test; //│ test = fun test :re @@ -135,7 +136,7 @@ mkObj(1, 2) // :re let mkObj = x: _, y: _, z: 3 //│ ╔══[ERROR] Illegal position for '_' placeholder. -//│ ║ l.136: let mkObj = x: _, y: _, z: 3 +//│ ║ l.137: let mkObj = x: _, y: _, z: 3 //│ ╙── ^ //│ mkObj = fun mkObj //│ y = undefined diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 0d8f01f0f5..01726b0d7d 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -31,7 +31,7 @@ :sjs let x = 1 in x + 1 //│ JS (unsanitized): -//│ let x; x = 1; x + 1 +//│ let x; /** scoped **/ x = 1; x + 1 //│ = 2 //│ Type: Int @@ -61,7 +61,7 @@ false :sjs data class Foo(x: Int) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x1) { //│ return globalThis.Object.freeze(new Foo.class(x1)); //│ }; @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; foo = globalThis.Object.freeze(new Foo.class(42)); foo.x +//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo.class(42)); foo.x //│ = 42 //│ Type: Int @@ -97,7 +97,7 @@ let foo = new Foo(42) in foo.Foo#x :sjs fun inc(x) = x + 1 //│ JS (unsanitized): -//│ let inc; inc = function inc(x1) { return x1 + 1 }; +//│ let inc; /** scoped **/ inc = function inc(x1) { return x1 + 1 }; //│ Type: ⊤ @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ let scrut; scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } +//│ let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ let scrut1; scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } +//│ let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } //│ = 1 //│ Type: Int @@ -134,7 +134,7 @@ data class Foo() let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ foo1 = globalThis.Object.freeze(new Foo2.class()); //│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } //│ = 1 @@ -147,11 +147,11 @@ fun pow(x) = case 0 then 1 n then x * pow(x)(n-1) //│ JS (unsanitized): -//│ let pow; +//│ let pow; /** scoped **/ //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; +//│ let n, tmp, tmp1, tmp2; /** scoped **/ //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -172,7 +172,7 @@ fun nott = case true then false false then true //│ JS (unsanitized): -//│ let nott; +//│ let nott; /** scoped **/ //│ nott = function nott() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { @@ -193,7 +193,7 @@ fun nott = case :sjs nott of false //│ JS (unsanitized): -//│ let tmp; tmp = nott(); tmp(false) +//│ let tmp; /** scoped **/ tmp = nott(); tmp(false) //│ = true //│ Type: Bool @@ -203,11 +203,11 @@ fun fact = case 0 then 1 n then n * fact(n - 1) //│ JS (unsanitized): -//│ let fact; +//│ let fact; /** scoped **/ //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; +//│ let n, tmp1, tmp2, tmp3; /** scoped **/ //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -258,7 +258,8 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let x4, y; +//│ let x4; +//│ let y; /** scoped **/ //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); //│ y.value @@ -269,7 +270,8 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let x5, y1; +//│ let x5; +//│ let y1; /** scoped **/ //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); //│ y1.value = 0; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index d72caafeeb..87c687f79b 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -87,27 +87,28 @@ fun test2() = //│ ║ l.83: case 0 then 0 //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let test22; +//│ let test22; /** scoped **/ //│ test22 = function test2() { -//│ let funny, tmp1; +//│ let funny, tmp1; /** scoped **/ //│ funny = function funny() { -//│ let lambda, lambda1; -//│ lambda = (undefined, function (caseScrut) { +//│ let lambda; +//│ let lambda1; /** scoped **/ +//│ lambda1 = (undefined, function (caseScrut) { //│ if (caseScrut === 0) { //│ return 0 //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ }); -//│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp2, tmp3, tmp4; +//│ lambda = (undefined, function (caseScrut) { +//│ let n, tmp2, tmp3, tmp4; /** scoped **/ //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; //│ tmp4 = tmp2(tmp3); //│ return tmp4 + 1 //│ }); -//│ return lambda1 +//│ return lambda //│ }; //│ tmp1 = funny(); //│ return tmp1 @@ -130,7 +131,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.131: print("Hi") +//│ ║ l.132: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index 9cb5c5210c..d7ff1e8b8d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -11,7 +11,7 @@ empty.0 :sjs let single = [1] //│ JS (unsanitized): -//│ let single; single = globalThis.Object.freeze([ 1 ]); +//│ let single; /** scoped **/ single = globalThis.Object.freeze([ 1 ]); //│ single = [1] single.0 @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls index ce774c920f..0d443fdd05 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls @@ -9,7 +9,7 @@ object Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar1; +//│ let Bar1; /** scoped **/ //│ globalThis.Object.freeze(class Bar { //│ static { //│ Bar1 = globalThis.Object.freeze(new this) @@ -49,7 +49,7 @@ module Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar3; +//│ let Bar3; /** scoped **/ //│ globalThis.Object.freeze(class Bar2 { //│ static { //│ Bar3 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls index ed25930a28..454e29826f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls @@ -29,7 +29,7 @@ new 2 + 2 :re new! 2 + 2 //│ JS (unsanitized): -//│ let tmp1; tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 +//│ let tmp1; /** scoped **/ tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 //│ ═══[RUNTIME ERROR] TypeError: 2 is not a constructor :e diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index 7bf650836b..db1624542a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -39,7 +39,7 @@ print("Hi") print("Hi") 2 //│ JS (unsanitized): -//│ let tmp; tmp = Predef.print("Hi"); 2 +//│ let tmp; /** scoped **/ tmp = Predef.print("Hi"); 2 //│ Lowered: //│ Program: //│ imports = Nil diff --git a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls index 51390245ed..70dc34b630 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls @@ -74,7 +74,7 @@ id(+)(1, 2) :re id(+)(1) //│ JS (unsanitized): -//│ let lambda4, tmp1; +//│ let lambda4, tmp1; /** scoped **/ //│ lambda4 = (undefined, function (arg1, arg2) { //│ return arg1 + arg2 //│ }); @@ -95,7 +95,7 @@ fun (+) lol(a, b) = [a, b] :sjs id(~)(2) //│ JS (unsanitized): -//│ let lambda5, tmp2; +//│ let lambda5, tmp2; /** scoped **/ //│ lambda5 = (undefined, function (arg) { //│ return ~ arg //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 6bc047ff4f..8be8e2459a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -15,9 +15,9 @@ fun test(x) = Some(v) then print(v) None then print("none") //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ //│ if (x instanceof Some.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 87451b3e49..2c5e5cee00 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -7,7 +7,13 @@ case x then x :sjs case { x then x } //│ JS (unsanitized): -//│ let lambda1; lambda1 = (undefined, function (caseScrut) { let x; x = caseScrut; return x }); lambda1 +//│ let lambda1; +//│ lambda1 = (undefined, function (caseScrut) { +//│ let x; /** scoped **/ +//│ x = caseScrut; +//│ return x +//│ }); +//│ lambda1 //│ = fun :sjs @@ -53,10 +59,10 @@ case 0 then true :todo // TODO: support this braceless syntax? case [x] then x, [] then 0 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.54: case [x] then x, [] then 0 +//│ ║ l.60: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^ //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.54: case [x] then x, [] then 0 +//│ ║ l.60: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^^^^^^^ :sjs @@ -79,7 +85,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let isDefined, lambda12; +//│ let isDefined, lambda12; /** scoped **/ //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index e29724b8ef..67640a68bf 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,8 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let Outer1; -//│ let tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ let Outer1, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -61,7 +60,7 @@ data class Outer(a, b) with //│ return this.Inner(c) //│ } //│ o2(c, d) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ tmp6 = this.Inner(c); //│ return tmp6.i1(d) //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index b5c1b26b34..696a1e22bd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -6,9 +6,9 @@ fun test(a) = class C with { val x = a } new C //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test(a) { -//│ let C1; +//│ let C1; /** scoped **/ //│ globalThis.Object.freeze(class C { //│ static { //│ C1 = this @@ -37,9 +37,9 @@ fun test(x) = data class Foo(a, b) Foo(x, x + 1) //│ JS (unsanitized): -//│ let test2; +//│ let test2; /** scoped **/ //│ test2 = function test(x) { -//│ let Foo2, tmp; +//│ let Foo2, tmp; /** scoped **/ //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 0e8998d346..b4df883b66 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,7 +9,7 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; +//│ let scrut, x, argument0$; /** scoped **/ //│ scrut = Some(0); //│ if (scrut instanceof Some.class) { //│ argument0$ = scrut.value; @@ -26,7 +26,7 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ let x1, argument0$1; +//│ let x1, argument0$1; /** scoped **/ //│ if (s instanceof Some.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; @@ -55,7 +55,7 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; +//│ let x4, argument0$4; /** scoped **/ //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -118,9 +118,9 @@ fun f(x) = if x is None then "ok" else print("oops") //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp6; +//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { @@ -164,9 +164,9 @@ fun f(x) = if x is Some(u) then u Pair(a, b) then a + b //│ JS (unsanitized): -//│ let f5; +//│ let f5; /** scoped **/ //│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; +//│ let u, a, b, argument0$4, argument1$; /** scoped **/ //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; @@ -194,9 +194,9 @@ fun f(x) = print of if x is None then "ok" else "oops" //│ JS (unsanitized): -//│ let f6; +//│ let f6; /** scoped **/ //│ f6 = function f(x3) { -//│ let argument0$4, tmp10; +//│ let argument0$4, tmp10; /** scoped **/ //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls index 1c596f3fc1..fc79ff4035 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls @@ -6,7 +6,7 @@ data class Foo(arguments) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(arguments1) { //│ return globalThis.Object.freeze(new Foo.class(arguments1)); //│ }; @@ -24,7 +24,7 @@ data class Foo(arguments) data class Foo(eval) //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ Foo3 = function Foo(eval1) { //│ return globalThis.Object.freeze(new Foo.class(eval1)); //│ }; @@ -42,7 +42,7 @@ data class Foo(eval) data class Foo(static) //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ Foo5 = function Foo(static1) { //│ return globalThis.Object.freeze(new Foo.class(static1)); //│ }; @@ -60,7 +60,7 @@ data class Foo(static) data class Foo(v') //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7; /** scoped **/ //│ Foo7 = function Foo(v$_) { //│ return globalThis.Object.freeze(new Foo.class(v$_)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index 7ba95074d3..af16010767 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -7,17 +7,27 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f; -//│ f = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; +//│ let f; /** scoped **/ +//│ f = function f() { +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 +//│ }; fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): -//│ let f1; -//│ f1 = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; +//│ let f1; /** scoped **/ +//│ f1 = function f() { +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 +//│ }; fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f2; f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; 42 +//│ let f2; /** scoped **/ +//│ f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; +//│ 42 //│ = 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index 70836f427c..1770b21e64 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -14,7 +14,7 @@ console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.console.log("a")); //│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 @@ -25,7 +25,7 @@ console.log("b") let l = console.log l(123) //│ JS (unsanitized): -//│ let l; l = globalThis.console.log; runtime.safeCall(l(123)) +//│ let l; /** scoped **/ l = globalThis.console.log; runtime.safeCall(l(123)) //│ > 123 //│ l = fun log @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3, tmp4; +//│ let x, y, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index a4fddd8d26..421a23afac 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; x = undefined; +//│ let x; /** scoped **/ x = undefined; //│ x = undefined x = 1 @@ -31,7 +31,7 @@ x let y = 1 //│ JS (unsanitized): -//│ let y; y = 1; +//│ let y; /** scoped **/ y = 1; //│ y = 1 :e @@ -45,7 +45,7 @@ z = 1 fun f() = 1 //│ JS (unsanitized): -//│ let f; f = function f() { return 1 }; +//│ let f; /** scoped **/ f = function f() { return 1 }; f //│ JS (unsanitized): @@ -56,7 +56,7 @@ f let f f(x) = x + 1 //│ JS (unsanitized): -//│ let f1, f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; +//│ let f1; let f2; /** scoped **/ f1 = function f(x1) { return x1 + 1 }; f2 = f1; //│ f = fun f f(1) @@ -68,7 +68,7 @@ f(1) let foo foo = 0 //│ JS (unsanitized): -//│ let foo; foo = 0; +//│ let foo; /** scoped **/ foo = 0; //│ foo = 0 :fixme @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; +//│ let foo1, scrut; /** scoped **/ //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; +//│ let foo2, scrut1; /** scoped **/ //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -105,7 +105,7 @@ else fun f() = foo = 42 //│ JS (unsanitized): -//│ let f3; f3 = function f() { foo2 = 42; return runtime.Unit }; +//│ let f3; /** scoped **/ f3 = function f() { foo2 = 42; return runtime.Unit }; f() //│ JS (unsanitized): @@ -123,6 +123,6 @@ fun f() = foo = 0 //│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let f4; f4 = function f() { return foo2 }; +//│ let f4; /** scoped **/ f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 797b5c0c47..1c439570a2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -18,9 +18,9 @@ fun f(x) = return 0 x + 1 //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f(x) { -//│ let scrut, tmp, tmp1; +//│ let scrut, tmp, tmp1; /** scoped **/ //│ scrut = x < 0; //│ if (scrut === true) { //│ tmp = Predef.print("whoops"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index 85ea77ef62..e353c2a33e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -7,7 +7,10 @@ let discard = drop _ //│ JS (unsanitized): -//│ let discard, discard1; discard1 = function discard(_0) { return runtime.Unit }; discard = discard1; +//│ let discard; +//│ let discard1; /** scoped **/ +//│ discard = function discard(_0) { return runtime.Unit }; +//│ discard1 = discard; //│ discard = fun discard @@ -15,7 +18,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; +//│ let a, b, tmp; /** scoped **/ //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -23,7 +26,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; +//│ let a1, b1, tmp1; /** scoped **/ //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -32,14 +35,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; +//│ let aaaa, tmp2; /** scoped **/ //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let aaaaaaaaa, bbbb, tmp3; +//│ let aaaaaaaaa, bbbb, tmp3; /** scoped **/ //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -50,7 +53,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; +//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; /** scoped **/ //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -66,7 +69,7 @@ discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ tmp5 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb" @@ -75,7 +78,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ] //│ JS (unsanitized): -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ tmp6 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", @@ -86,7 +89,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; +//│ let tmp7, tmp8; /** scoped **/ //│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 8da41c9dda..1078c18fd7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -19,10 +19,9 @@ fun test(a) = h(d) Inner(42) //│ JS (unsanitized): -//│ let test1; +//│ let test1; /** scoped **/ //│ test1 = function test(a) { -//│ let Inner1; -//│ let tmp; /** scoped **/ +//│ let Inner1, tmp; /** scoped **/ //│ Inner1 = function Inner(b) { //│ return globalThis.Object.freeze(new Inner.class(b)); //│ }; @@ -42,7 +41,7 @@ fun test(a) = //│ ]) //│ } //│ g(d) { -//│ let h; +//│ let h; /** scoped **/ //│ const this$Inner = this; //│ h = function h(e) { //│ return globalThis.Object.freeze([ @@ -79,10 +78,9 @@ fun test(a) = print of [a, b] [C1(1), C2(2)] //│ JS (unsanitized): -//│ let test2; +//│ let test2; /** scoped **/ //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1; -//│ let tmp2, tmp3; /** scoped **/ +//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -92,11 +90,11 @@ fun test(a) = //│ } //│ constructor(b) { //│ this.b = b; -//│ tmp2 = globalThis.Object.freeze([ +//│ tmp = globalThis.Object.freeze([ //│ a, //│ this.b //│ ]); -//│ Predef.print(tmp2); +//│ Predef.print(tmp); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "C1", ["b"]]; @@ -110,18 +108,18 @@ fun test(a) = //│ } //│ constructor(b) { //│ this.b = b; -//│ tmp3 = globalThis.Object.freeze([ +//│ tmp1 = globalThis.Object.freeze([ //│ a, //│ this.b //│ ]); -//│ Predef.print(tmp3); +//│ Predef.print(tmp1); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "C2", ["b"]]; //│ }); -//│ tmp = C11(1); -//│ tmp1 = C21(2); -//│ return globalThis.Object.freeze([ tmp, tmp1 ]) +//│ tmp2 = C11(1); +//│ tmp3 = C21(2); +//│ return globalThis.Object.freeze([ tmp2, tmp3 ]) //│ }; test(123) @@ -140,7 +138,7 @@ data class Foo(a) with foo() Foo(123) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -153,7 +151,7 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz, tmp; +//│ let bar, baz, tmp; /** scoped **/ //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a @@ -179,8 +177,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1; -//│ let tmp; /** scoped **/ +//│ let Bar1, tmp; /** scoped **/ //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -195,7 +192,7 @@ Bar(1) //│ } //│ foo() { //│ return () => { -//│ let bar; +//│ let bar; /** scoped **/ //│ const this$Bar = this; //│ bar = function bar() { //│ return this$Bar.x diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index e371cd8530..e3460c4b18 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -6,15 +6,14 @@ val x = 2 fun foo() = x + 1 //│ JS (unsanitized): -//│ let foo, x; foo = function foo() { return x + 1 }; x = 2; +//│ let foo, x; /** scoped **/ foo = function foo() { return x + 1 }; x = 2; //│ x = 2 :sjs class Test with print(foo()) //│ JS (unsanitized): -//│ let Test1; -//│ let tmp; /** scoped **/ +//│ let Test1, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 10cff6a0ed..23e4c2f5d8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -4,14 +4,14 @@ :sjs fun t = 42 //│ JS (unsanitized): -//│ let t; t = function t() { return 42 }; +//│ let t; /** scoped **/ t = function t() { return 42 }; :expect 42 :sjs t //│ JS (unsanitized): -//│ let tmp; tmp = t(); tmp +//│ let tmp; /** scoped **/ tmp = t(); tmp //│ = 42 @@ -37,11 +37,11 @@ fun test() = 42 whoops + whoops //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test() { -//│ let whoops, tmp1, tmp2; +//│ let whoops, tmp1, tmp2; /** scoped **/ //│ whoops = function whoops() { -//│ let tmp3; +//│ let tmp3; /** scoped **/ //│ tmp3 = Predef.print("ok"); //│ return 42 //│ }; @@ -65,7 +65,7 @@ module T with val c = p val d = this.p //│ JS (unsanitized): -//│ let T1; +//│ let T1; /** scoped **/ //│ globalThis.Object.freeze(class T { //│ static { //│ T1 = this @@ -107,7 +107,7 @@ T.d module M with fun t = 0 //│ JS (unsanitized): -//│ let M1; +//│ let M1; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -135,9 +135,9 @@ fun test() = fun whoops = 42 whoops //│ JS (unsanitized): -//│ let test1; +//│ let test1; /** scoped **/ //│ test1 = function test() { -//│ let whoops, tmp1; +//│ let whoops, tmp1; /** scoped **/ //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -163,7 +163,8 @@ fun bar() = fun baz() = 42 baz //│ JS (unsanitized): -//│ let bar; bar = function bar() { let baz; baz = function baz() { return 42 }; return baz }; +//│ let bar; /** scoped **/ +//│ bar = function bar() { let baz; /** scoped **/ baz = function baz() { return 42 }; return baz }; :sjs @@ -172,9 +173,10 @@ fun baz() = fun z = 2 (x, y) => x + y + w + z //│ JS (unsanitized): -//│ let baz; +//│ let baz; /** scoped **/ //│ baz = function baz() { -//│ let w, z, lambda; +//│ let lambda; +//│ let w, z; /** scoped **/ //│ w = function w() { //│ return 1 //│ }; @@ -182,7 +184,7 @@ fun baz() = //│ return 2 //│ }; //│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3, tmp4; +//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp1 = x + y; //│ tmp2 = w(); //│ tmp3 = tmp1 + tmp2; @@ -205,14 +207,14 @@ fun a() = b + d c //│ JS (unsanitized): -//│ let a; +//│ let a; /** scoped **/ //│ a = function a() { -//│ let b, c; +//│ let b, c; /** scoped **/ //│ b = function b() { //│ return 1 //│ }; //│ c = function c() { -//│ let d, tmp2, tmp3; +//│ let d, tmp2, tmp3; /** scoped **/ //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -234,14 +236,14 @@ fun b() = c d //│ JS (unsanitized): -//│ let b; +//│ let b; /** scoped **/ //│ b = function b() { -//│ let c, d; +//│ let c, d; /** scoped **/ //│ c = function c() { //│ return 1 //│ }; //│ d = function d() { -//│ let c1, tmp3; +//│ let c1, tmp3; /** scoped **/ //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -262,14 +264,14 @@ fun c() = e + f d //│ JS (unsanitized): -//│ let c; +//│ let c; /** scoped **/ //│ c = function c() { -//│ let d, f, tmp4; +//│ let d, f, tmp4; /** scoped **/ //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e, tmp5, tmp6; +//│ let e, tmp5, tmp6; /** scoped **/ //│ e = function e() { //│ return 1 //│ }; @@ -290,7 +292,7 @@ c() data class Foo(x) with fun oops = x //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index 883fdd4d3e..7c40714263 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,7 +21,7 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = false; //│ if (scrut === true) { //│ 0 @@ -37,9 +37,9 @@ fun foo() = if false then 0 foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() { -//│ let scrut1; +//│ let scrut1; /** scoped **/ //│ scrut1 = false; //│ if (scrut1 === true) { //│ return 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index 4a386a6b9a..10433bdb39 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -10,7 +10,7 @@ object Test with print(Test) Test.x //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = globalThis.Object.freeze(new this) @@ -22,7 +22,7 @@ object Test with //│ }) //│ } //│ foo() { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = Predef.print(Test1); //│ return Test1.x //│ } @@ -46,7 +46,7 @@ Test.foo() :sjs val Test = "oops" //│ JS (unsanitized): -//│ let Test2; Test2 = "oops"; +//│ let Test2; /** scoped **/ Test2 = "oops"; //│ Test = "oops" :re @@ -60,7 +60,13 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let x, f, x1, f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) +//│ let f; +//│ let x, f1, x1; /** scoped **/ +//│ x = 1; +//│ f = function f() { return x }; +//│ f1 = f; +//│ x1 = 2; +//│ runtime.safeCall(f1()) //│ = 1 //│ f = fun f //│ x = 2 @@ -72,10 +78,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.73: let x = 2 +//│ ║ l.79: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.72: val x = 1 +//│ ║ l.78: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test4 { @@ -115,7 +121,7 @@ data class Cls(x) with fun bar = x print(this.x, x) //│ JS (unsanitized): -//│ let Cls1; +//│ let Cls1; /** scoped **/ //│ Cls1 = function Cls(x2) { //│ return globalThis.Object.freeze(new Cls.class(x2)); //│ }; @@ -179,7 +185,7 @@ module Whoops with val w: module Whoops = this fun g() = f() //│ JS (unsanitized): -//│ let Whoops2; +//│ let Whoops2; /** scoped **/ //│ globalThis.Object.freeze(class Whoops { //│ static { //│ Whoops2 = this @@ -226,7 +232,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.227: Runtime +//│ ║ l.233: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index a440378432..e09edf1374 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let f, lambda; +//│ let f, lambda; /** scoped **/ //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,9 +26,9 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f1, lambda1; +//│ let f1, lambda1; /** scoped **/ //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } @@ -41,9 +41,9 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f2, lambda2; +//│ let f2, lambda2; /** scoped **/ //│ lambda2 = (undefined, function (x) { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls index 07b13f5dfb..e2425e33b6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls @@ -39,7 +39,9 @@ Some(1) :sjs (new Some(1)) isDefined() //│ JS (unsanitized): -//│ let tmp3; tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); Option.isDefined(tmp3) +//│ let tmp3; /** scoped **/ +//│ tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); +//│ Option.isDefined(tmp3) //│ = true new Some(1) isDefined() @@ -66,14 +68,14 @@ None == Option.None :re Option.oops //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.67: Option.oops +//│ ║ l.69: Option.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' :e open Option { oops } //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.74: open Option { oops } +//│ ║ l.76: open Option { oops } //│ ╙── ^^^^ oops @@ -82,7 +84,7 @@ oops :re Option.None.oops //│ ╔══[ERROR] Object 'None' does not contain member 'oops' -//│ ║ l.83: Option.None.oops +//│ ║ l.85: Option.None.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index 9ade6db65e..79d3e59992 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -11,8 +11,12 @@ fun foo() = "a" ~ "b" ~ "c" foo() //│ JS (unsanitized): -//│ let foo; -//│ foo = function foo() { let tmp; tmp = M.concat("a", "b"); return M.concat(tmp, "c") }; +//│ let foo; /** scoped **/ +//│ foo = function foo() { +//│ let tmp; /** scoped **/ +//│ tmp = M.concat("a", "b"); +//│ return M.concat(tmp, "c") +//│ }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index 37ffed6216..eb040590d5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -3,9 +3,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda; +//│ let lambda; /** scoped **/ //│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -18,9 +18,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda1; +//│ let lambda1; /** scoped **/ //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3, tmp4; +//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -34,13 +34,13 @@ :sjs (x => x) + 1 //│ JS (unsanitized): -//│ let lambda2; lambda2 = (undefined, function (x) { return x }); lambda2 + 1 +//│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 //│ = "function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }1" :sjs 1 + (x => x) //│ JS (unsanitized): -//│ let lambda3; lambda3 = (undefined, function (x) { return x }); 1 + lambda3 +//│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 //│ = "1function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls index cf8b2bf680..86a50c8394 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls @@ -8,7 +8,7 @@ x => let y = x y //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function (x) { let y; y = x; return y }); lambda +//│ let lambda; lambda = (undefined, function (x) { let y; /** scoped **/ y = x; return y }); lambda //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 411551d944..d7b8eb9113 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -58,7 +58,7 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ let tmp; +//│ let tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ split_1$: { @@ -107,7 +107,7 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3, tmp4; +//│ let x, tmp1, tmp2, tmp3; /** scoped **/ //│ if (a instanceof A.class) { //│ tmp2 = 1; //│ } else if (a instanceof B.class) { @@ -115,18 +115,19 @@ print(x) //│ } else if (a instanceof C.class) { //│ tmp2 = 3; //│ } else { +//│ let tmp4; /** scoped **/ //│ tmp1 = 3; //│ if (a instanceof D.class) { //│ if (a instanceof A.class) { -//│ tmp3 = 1 + tmp1; +//│ tmp4 = 1 + tmp1; //│ } else if (a instanceof B.class) { -//│ tmp3 = 2 + tmp1; +//│ tmp4 = 2 + tmp1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp4 = tmp3; +//│ tmp3 = tmp4; //│ } else if (a instanceof E.class) { -//│ tmp4 = 5; +//│ tmp3 = 5; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } @@ -146,7 +147,7 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ tmp5 = 2; //│ if (a instanceof A.class) { //│ 1 @@ -168,7 +169,7 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ if (a instanceof A.class) { //│ 1 //│ } else { @@ -191,7 +192,7 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ let x1, tmp7; +//│ let x1, tmp7; /** scoped **/ //│ if (a instanceof A.class) { //│ 1 //│ } else if (a instanceof B.class) { @@ -220,7 +221,7 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ //│ if (a instanceof B.class) { //│ 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index efb15ce8eb..19b5d0c1e1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,8 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let Test1; -//│ let tmp1; /** scoped **/ +//│ let Test1, tmp1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index 4e1ab0c899..dc8f7d5442 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -4,7 +4,7 @@ :sjs module None //│ JS (unsanitized): -//│ let None1; +//│ let None1; /** scoped **/ //│ globalThis.Object.freeze(class None { //│ static { //│ None1 = this @@ -55,8 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1; -//│ let tmp; /** scoped **/ +//│ let M1, tmp; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -111,7 +110,7 @@ M.y :re M.oops //│ ╔══[ERROR] Module 'M' does not contain member 'oops' -//│ ║ l.112: M.oops +//│ ║ l.111: M.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' @@ -123,7 +122,7 @@ M.oops module M with val m: module M = M //│ JS (unsanitized): -//│ let M3; +//│ let M3; /** scoped **/ //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -156,7 +155,7 @@ module AA with val y = 2 [x, y] //│ JS (unsanitized): -//│ let AA1; +//│ let AA1; /** scoped **/ //│ globalThis.Object.freeze(class AA { //│ static { //│ AA1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 1318302565..9f2d151267 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -52,7 +52,7 @@ none() :sjs val Option = "Oops" //│ JS (unsanitized): -//│ let Option1; Option1 = "Oops"; +//│ let Option1; /** scoped **/ Option1 = "Oops"; //│ Option = "Oops" :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index f2e726848b..2416214a74 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -10,7 +10,7 @@ fun isDefined(x) = if x is Some then true None then false //│ JS (unsanitized): -//│ let isDefined; +//│ let isDefined; /** scoped **/ //│ isDefined = function isDefined(x) { //│ if (x instanceof Some.class) { //│ return true @@ -31,9 +31,9 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; +//│ let isDefined1, lambda; /** scoped **/ //│ lambda = (undefined, function (caseScrut) { -//│ let argument0$; +//│ let argument0$; /** scoped **/ //│ if (caseScrut instanceof Some.class) { //│ argument0$ = caseScrut.value; //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 1d24077798..04625446e1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -6,7 +6,7 @@ data class Foo() //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo() { //│ return globalThis.Object.freeze(new Foo.class()); //│ }; @@ -37,7 +37,7 @@ Foo.class data class Foo(a) //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ Foo3 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -64,19 +64,19 @@ Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; tmp = Foo2(1); tmp.a +//│ let tmp; /** scoped **/ tmp = Foo2(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; foo = function foo(y) { return Foo2(y) }; foo(27) +//│ let foo; /** scoped **/ foo = function foo(y) { return Foo2(y) }; foo(27) //│ = Foo(27) data class Foo(a, b) //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ Foo5 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -94,17 +94,17 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; foo1 = Foo4; +//│ let foo1; /** scoped **/ foo1 = Foo4; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) //│ JS (unsanitized): -//│ let f; f = runtime.safeCall(foo1(1, 2)); +//│ let f; /** scoped **/ f = runtime.safeCall(foo1(1, 2)); //│ f = Foo(1, 2) let f = new! foo(1, 2) //│ JS (unsanitized): -//│ let f1; f1 = globalThis.Object.freeze(new foo1(1, 2)); +//│ let f1; /** scoped **/ f1 = globalThis.Object.freeze(new foo1(1, 2)); //│ f = Foo(1, 2) f.a @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; f2 = Foo4(1, 2); +//│ let f2; /** scoped **/ f2 = Foo4(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) +//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -144,7 +144,7 @@ data class Inner(c) with fun i1(d) = c + d print(c) //│ JS (unsanitized): -//│ let Inner1; +//│ let Inner1; /** scoped **/ //│ Inner1 = function Inner(c) { //│ return globalThis.Object.freeze(new Inner.class(c)); //│ }; @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; i = globalThis.Object.freeze(new Inner.class(100)); +//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner.class(100)); //│ > 100 //│ i = Inner(100) @@ -185,8 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7; -//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -237,13 +236,13 @@ Foo(10, 20, 30, 40, 50) class Foo(val z, val z) //│ ╔══[ERROR] Duplicate definition of member named 'z'. //│ ╟── Defined at: -//│ ║ l.237: class Foo(val z, val z) +//│ ║ l.236: class Foo(val z, val z) //│ ║ ^ //│ ╟── Defined at: -//│ ║ l.237: class Foo(val z, val z) +//│ ║ l.236: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo9; +//│ let Foo9; /** scoped **/ //│ Foo9 = function Foo(z, z1) { //│ return globalThis.Object.freeze(new Foo.class(z, z1)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index ec8c349b28..9b85fd7887 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -12,7 +12,7 @@ f(2) :sjs let f = foo(1, _, _) //│ JS (unsanitized): -//│ let f2, f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; +//│ let f2; let f3; /** scoped **/ f2 = function f(_0, _1) { return foo(1, _0, _1) }; f3 = f2; //│ f = fun f f(2, 3) @@ -79,7 +79,10 @@ h(1) :sjs let i = _(0, 1, _) //│ JS (unsanitized): -//│ let i, i1; i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; i = i1; +//│ let i; +//│ let i1; /** scoped **/ +//│ i = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; +//│ i1 = i; //│ i = fun i i(print, 2) @@ -199,7 +202,7 @@ _ - _ of 1, 2 :pe |> 1 //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.200: |> 1 +//│ ║ l.203: |> 1 //│ ╙── ^ //│ = fun pipeInto @@ -207,7 +210,7 @@ _ - _ of 1, 2 :re |> (1) //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.208: |> (1) +//│ ║ l.211: |> (1) //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] TypeError: f is not a function @@ -225,13 +228,15 @@ _ - _ of 1, 2 :sjs 1 \ (_ - 2) //│ JS (unsanitized): -//│ let lambda38; lambda38 = (undefined, function (_0) { return _0 - 2 }); Predef.passTo(1, lambda38) +//│ let lambda38; /** scoped **/ +//│ lambda38 = (undefined, function (_0) { return _0 - 2 }); +//│ Predef.passTo(1, lambda38) //│ = fun :sjs 1 \ (_ - 2)() //│ JS (unsanitized): -//│ let lambda39, tmp19; +//│ let lambda39, tmp19; /** scoped **/ //│ lambda39 = (undefined, function (_0) { //│ return _0 - 2 //│ }); @@ -248,13 +253,13 @@ _ - _ of 1, 2 :w let f = if _ then 1 else 0 //│ ╔══[WARNING] This catch-all clause makes the following branches unreachable. -//│ ║ l.249: let f = if _ then 1 else 0 +//│ ║ l.254: let f = if _ then 1 else 0 //│ ║ ^^^^^^ //│ ╟── This branch is unreachable. -//│ ║ l.249: let f = if _ then 1 else 0 +//│ ║ l.254: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; /** scoped **/ tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs @@ -263,9 +268,9 @@ fun f(x) = 0 then 1 _ then 2 //│ JS (unsanitized): -//│ let f16; +//│ let f16; /** scoped **/ //│ f16 = function f(x3) { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = x3 == 0; //│ if (scrut === true) { return 1 } else { return 2 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index f18bf34f8e..19591829d2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -6,7 +6,7 @@ class Foo //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -23,7 +23,7 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = globalThis.Object.freeze(new Foo()); //│ if (scrut instanceof Foo) { true } else { false } //│ = true @@ -53,7 +53,7 @@ Foo() data class Foo with { print("hi") } print("ok") //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ globalThis.Object.freeze(class Foo2 { //│ static { //│ Foo3 = this @@ -72,9 +72,9 @@ fun test() = print("ok") Foo //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test() { -//│ let Foo5, tmp; +//│ let Foo5, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -91,7 +91,7 @@ fun test() = let t = test() //│ JS (unsanitized): -//│ let t; t = test(); +//│ let t; /** scoped **/ t = test(); //│ > ok //│ t = class Foo @@ -135,7 +135,7 @@ class Foo with let y = x + 1 fun z() = y + x //│ JS (unsanitized): -//│ let Foo6; +//│ let Foo6; /** scoped **/ //│ globalThis.Object.freeze(class Foo5 { //│ static { //│ Foo6 = this @@ -162,7 +162,7 @@ class Foo with fun z2() = 6 print("hello") //│ JS (unsanitized): -//│ let Foo8; +//│ let Foo8; /** scoped **/ //│ globalThis.Object.freeze(class Foo7 { //│ static { //│ Foo8 = this @@ -192,7 +192,7 @@ class Foo with fun foo(y) = x + y fun bar(z) = foo(z) + 1 //│ JS (unsanitized): -//│ let Foo10; +//│ let Foo10; /** scoped **/ //│ globalThis.Object.freeze(class Foo9 { //│ static { //│ Foo10 = this @@ -204,7 +204,7 @@ class Foo with //│ return this.x + y //│ } //│ bar(z) { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = this.foo(z); //│ return tmp + 1 //│ } @@ -217,7 +217,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let a, tmp, tmp1, tmp2, tmp3; +//│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); @@ -243,7 +243,7 @@ class Foo with //│ ║ l.237: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo12; +//│ let Foo12; /** scoped **/ //│ globalThis.Object.freeze(class Foo11 { //│ static { //│ Foo12 = this @@ -267,7 +267,7 @@ class Foo with //│ ║ l.261: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo14; +//│ let Foo14; /** scoped **/ //│ globalThis.Object.freeze(class Foo13 { //│ static { //│ Foo14 = this @@ -294,7 +294,7 @@ class Foo with val x = 1 //│ ║ l.292: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo16; +//│ let Foo16; /** scoped **/ //│ globalThis.Object.freeze(class Foo15 { //│ static { //│ Foo16 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls index 8a571ad88e..ea8a6fed34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls @@ -7,7 +7,7 @@ let x = 1 :sjs let x' = 2 //│ JS (unsanitized): -//│ let x$_; x$_ = 2; +//│ let x$_; /** scoped **/ x$_ = 2; //│ x' = 2 x' diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 4defe108c8..3cd21a98c2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; +//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index 3e823a55c8..3ae4eea381 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,15 +76,16 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let x1, tmp6, tmp7, tmp8, arr2; +//│ let x1, tmp6, tmp7, arr2; +//│ let tmp8; /** scoped **/ //│ tmp6 = globalThis.Object.freeze(new Term.Symbol("x")); //│ x1 = globalThis.Object.freeze(new Term.Ref(tmp6)); -//│ tmp7 = Predef.print(x1); +//│ tmp8 = Predef.print(x1); //│ arr2 = globalThis.Object.freeze([ //│ tmp6 //│ ]); -//│ tmp8 = x1; -//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp8)) +//│ tmp7 = x1; +//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp7)) //│ > Ref(Symbol("x")) //│ = Lam([Symbol("x")], Ref(Symbol("x"))) diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index 10940ea078..a252994119 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -4,9 +4,9 @@ :sjs fun foo() = if false do foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = false; //│ if (scrut === true) { return foo() } else { return runtime.Unit } //│ }; @@ -14,7 +14,7 @@ fun foo() = if false do foo() :sjs fun foo() = foo() //│ JS (unsanitized): -//│ let foo1; foo1 = function foo() { return foo1() }; +//│ let foo1; /** scoped **/ foo1 = function foo() { return foo1() }; :sjs @@ -22,7 +22,7 @@ data class Foo(x) with class Bar with val y = x //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; @@ -54,7 +54,12 @@ fun foo() = fun bar() = bar() bar() //│ JS (unsanitized): -//│ let foo2; foo2 = function foo() { let bar; bar = function bar() { return bar() }; return bar() }; +//│ let foo2; /** scoped **/ +//│ foo2 = function foo() { +//│ let bar; /** scoped **/ +//│ bar = function bar() { return bar() }; +//│ return bar() +//│ }; :sjs @@ -63,7 +68,10 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; f1 = 1; f = function f() { let tmp; tmp = f(); return tmp }; runtime.Unit +//│ let f, f1; /** scoped **/ +//│ f1 = 1; +//│ f = function f() { let tmp; /** scoped **/ tmp = f(); return tmp }; +//│ runtime.Unit //│ f = 1 :sjs @@ -71,13 +79,13 @@ do let foo = 1 fun foo(x) = foo //│ ╔══[ERROR] Name 'foo' is already used -//│ ║ l.71: let foo = 1 +//│ ║ l.79: let foo = 1 //│ ║ ^^^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.72: fun foo(x) = foo +//│ ║ l.80: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let foo3, foo4; foo3 = function foo(x) { return foo4 }; foo4 = 1; +//│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; //│ foo = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 2e8314b296..53686fe526 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -52,7 +52,7 @@ id(f)(3)(4) //│ runtime.checkArgs("", 2, true, args1.length); //│ let y = args1[0]; //│ let z = args1[1]; -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ tmp4 = x + y; //│ return tmp4 + z //│ } @@ -109,7 +109,7 @@ id(Cls(1, 2)).f(3) //│ this.y = y; //│ } //│ f(z, p) { -//│ let tmp6, tmp7; +//│ let tmp6, tmp7; /** scoped **/ //│ tmp6 = this.x + this.y; //│ tmp7 = tmp6 + z; //│ return tmp7 + p @@ -145,7 +145,7 @@ id(Cls(1, 2)).f(3) //│ runtime.checkArgs("f", 2, true, args.length); //│ let z = args[0]; //│ let p = args[1]; -//│ let tmp8, tmp9; +//│ let tmp8, tmp9; /** scoped **/ //│ tmp8 = this.x + this.y; //│ tmp9 = tmp8 + z; //│ return tmp9 + p @@ -186,7 +186,7 @@ id(Cls(1, 2)).f(3, 4)(5) //│ runtime.checkArgs("", 2, true, args1.length); //│ let q = args1[0]; //│ let s = args1[1]; -//│ let tmp11, tmp12, tmp13, tmp14; +//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ //│ tmp11 = this.x + this.y; //│ tmp12 = tmp11 + z; //│ tmp13 = tmp12 + p; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 11e6c1d868..6c1ed8a013 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -43,9 +43,8 @@ fun g(x, y, z) = //│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { //│ let lambda; -//│ let tmp1, tmp2; /** scoped **/ +//│ let a, tmp1, tmp2; /** scoped **/ //│ split_root$: { -//│ let a; /** scoped **/ //│ split_1$: { //│ if (x1 === true) { //│ break split_1$ @@ -87,9 +86,8 @@ fun f() = if x is //│ JS (unsanitized): //│ let f1; /** scoped **/ //│ f1 = function f() { -//│ let tmp1; /** scoped **/ +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { -//│ let a, b, scrut, c, d, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ if (x instanceof A.class) { //│ argument0$ = x.a; @@ -126,9 +124,8 @@ fun f() = if A(1, Nil) is //│ JS (unsanitized): //│ let f2; /** scoped **/ //│ f2 = function f() { -//│ let tmp1; /** scoped **/ +//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { -//│ let scrut, argument0$, argument1$; /** scoped **/ //│ split_default$: { //│ scrut = A(1, Nil); //│ if (scrut instanceof A.class) { @@ -210,8 +207,8 @@ fun f() = //│ y1 = x + 1; //│ tmp1 = undefined; //│ while1 = (undefined, function () { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_root$: { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_1$: { //│ if (y1 instanceof A.class) { //│ argument0$ = y1.a; diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 76e0d53b89..56d63f1ecd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -5,7 +5,7 @@ module Foo with val self: module Foo = Foo //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -61,8 +61,7 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11; -//│ let tmp; /** scoped **/ +//│ let Foo11, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 0f46270d22..0eed770a5e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -7,7 +7,7 @@ let x = 0 :sjs let x += 1 //│ JS (unsanitized): -//│ let x1; x1 = x + 1; +//│ let x1; /** scoped **/ x1 = x + 1; //│ x = 1 x @@ -21,7 +21,7 @@ set x = 0 :sjs set x += 1 //│ JS (unsanitized): -//│ let tmp; tmp = x1 + 1; x1 = tmp; runtime.Unit +//│ let tmp; /** scoped **/ tmp = x1 + 1; x1 = tmp; runtime.Unit x //│ = 1 @@ -30,7 +30,7 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let old, tmp1, tmp2, tmp3; +//│ let old, tmp1, tmp2, tmp3; /** scoped **/ //│ old = x1; //│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } //│ tmp1 @@ -69,20 +69,21 @@ fun example() = print(get_x()) example() //│ JS (unsanitized): -//│ let example2; +//│ let example2; /** scoped **/ //│ example2 = function example() { -//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, get_x1; +//│ let get_x; +//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ x2 = 0; -//│ get_x1 = function get_x() { +//│ get_x = function get_x() { //│ return x2 //│ }; -//│ get_x = get_x1; +//│ get_x1 = get_x; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; //│ x2 = tmp5; //│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x()); +//│ tmp7 = runtime.safeCall(get_x1()); //│ tmp8 = Predef.print(tmp7); //│ tmp9 = (tmp6 , tmp8); //│ tmp4 = tmp9; @@ -90,7 +91,7 @@ example() //│ x2 = old1; //│ } //│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x()); +//│ tmp11 = runtime.safeCall(get_x1()); //│ return Predef.print(tmp11) //│ }; //│ example2() @@ -110,14 +111,15 @@ fun example() = y example() //│ JS (unsanitized): -//│ let example3; +//│ let example3; /** scoped **/ //│ example3 = function example() { -//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, get_x1; +//│ let get_x; +//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ //│ x2 = 0; -//│ get_x1 = function get_x() { +//│ get_x = function get_x() { //│ return x2 //│ }; -//│ get_x = get_x1; +//│ get_x1 = get_x; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; @@ -130,7 +132,7 @@ example() //│ } //│ y = tmp4; //│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x()); +//│ tmp9 = runtime.safeCall(get_x1()); //│ tmp10 = Predef.print(tmp9); //│ return y //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls index 9be3a3a5b7..228d0613df 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls @@ -19,7 +19,7 @@ foo(0, ...a) :sjs foo(1, ...[2, 3], 4) //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) //│ = [1, 2, 3, 4] :re diff --git a/hkmc2/shared/src/test/mlscript/codegen/This.mls b/hkmc2/shared/src/test/mlscript/codegen/This.mls index 64418595dd..9d809eb9e6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/This.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/This.mls @@ -10,13 +10,14 @@ fun test(x) = //│ ║ l.8: [this.a, x] //│ ╙── ^^^^ //│ JS (unsanitized): -//│ let test; test = function test(x) { return globalThis.Object.freeze([]) }; +//│ let test; /** scoped **/ test = function test(x) { return globalThis.Object.freeze([]) }; :sjs fun test(x) = [globalThis.a, x] //│ JS (unsanitized): -//│ let test1; test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; +//│ let test1; /** scoped **/ +//│ test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; :re test(123) @@ -42,7 +43,7 @@ module Test with fun test2(x) = [this.a, x] //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 44fb9381b8..3bb6a4a271 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; tmp = call(Example, oops); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = call(Example, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) +//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls index cad43e858a..b2171fa4c0 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls @@ -19,7 +19,7 @@ s(123) :sjs ex |>. s(123) //│ JS (unsanitized): -//│ let tmp1; tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) +//│ let tmp1; /** scoped **/ tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) //│ = [123, 456] diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index acf2884167..fa1b38cc34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,9 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; f2(1) +//│ let f2; /** scoped **/ +//│ f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; +//│ f2(1) //│ ═══[RUNTIME ERROR] Error: e @@ -41,7 +43,7 @@ fun f(x) = throw (if x then Error("x") else Error("y")) f(false) //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(x) { //│ if (x === true) { throw globalThis.Error("x") } else { throw globalThis.Error("y") } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 0336784324..993741a4b1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -7,9 +7,9 @@ fun fib(a) = if a <= 1 then a else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): -//│ let fib; +//│ let fib; /** scoped **/ //│ fib = function fib(a) { -//│ let scrut, tmp, tmp1, tmp2, tmp3; +//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ scrut = a <= 1; //│ if (scrut === true) { //│ return a diff --git a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls index c0ec266ced..72170fa838 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls @@ -30,7 +30,9 @@ print of foo() :sjs print of globalThis.console.log("Hello, world!") //│ JS (unsanitized): -//│ let tmp4; tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); Predef.print(tmp4) +//│ let tmp4; /** scoped **/ +//│ tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); +//│ Predef.print(tmp4) //│ > Hello, world! //│ > () @@ -54,7 +56,7 @@ print of Box(foo()).value :sjs fun foo() = {} //│ JS (unsanitized): -//│ let foo5; foo5 = function foo() { return runtime.Unit }; +//│ let foo5; /** scoped **/ foo5 = function foo() { return runtime.Unit }; print of Box(foo()).value //│ > () @@ -64,7 +66,7 @@ print of Box(foo()).value fun foo(x) = set x = 1 //│ JS (unsanitized): -//│ let foo6; foo6 = function foo(x) { x = 1; return runtime.Unit }; +//│ let foo6; /** scoped **/ foo6 = function foo(x) { x = 1; return runtime.Unit }; print of Box(foo(1)).value //│ > () @@ -73,7 +75,7 @@ print of Box(foo(1)).value :e fun f = let x = 1 //│ ╔══[ERROR] Expected a body for let bindings in expression position -//│ ║ l.74: fun f = let x = 1 +//│ ║ l.76: fun f = let x = 1 //│ ╙── ^^^^^ print of f diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 5753956740..eb618c68fb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -6,10 +6,10 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function () { -//│ let tmp, while1, tmp1, tmp2; +//│ let tmp, while1, tmp1, tmp2; /** scoped **/ //│ tmp = undefined; //│ while1 = (undefined, function () { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ tmp = 0; @@ -57,11 +57,10 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let tmp6, while3, tmp7; -//│ let tmp8; /** scoped **/ +//│ let tmp6, while3, tmp7, tmp8; /** scoped **/ //│ tmp6 = undefined; //│ while3 = (undefined, function () { -//│ let tmp9; +//│ let tmp9; /** scoped **/ //│ if (x2 === true) { //│ tmp9 = Predef.print("Hello World"); //│ x2 = false; @@ -113,10 +112,10 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let tmp18, while7, tmp19, tmp20; +//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ //│ tmp18 = undefined; //│ while7 = (undefined, function () { -//│ let i2, scrut, tmp21; +//│ let i2, scrut, tmp21; /** scoped **/ //│ i2 = 0; //│ scrut = i2 < 10; //│ if (scrut === true) { @@ -209,12 +208,12 @@ fun f(ls) = print(h) else print("Done!") //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f(ls) { -//│ let tmp27, while10, tmp28, tmp29; +//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ //│ tmp27 = undefined; //│ while10 = (undefined, function () { -//│ let tl, h, argument0$, argument1$; +//│ let tl, h, argument0$, argument1$; /** scoped **/ //│ if (ls instanceof Cons.class) { //│ argument0$ = ls.hd; //│ argument1$ = ls.tl; @@ -268,8 +267,7 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, while11; -//│ let tmp39; /** scoped **/ +//│ let tmp37, tmp38, tmp39, while11; /** scoped **/ //│ tmp37 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { @@ -299,10 +297,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.299: then 0(0) +//│ ║ l.297: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.298: while print("Hello World"); false +//│ ║ l.296: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -312,12 +310,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.311: while { print("Hello World"), false } +//│ ║ l.309: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.312: then 0(0) +//│ ║ l.310: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.313: else 1 +//│ ║ l.311: else 1 //│ ╙── ^^^^ :fixme @@ -327,14 +325,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.325: print("Hello World") +//│ ║ l.323: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.326: false +//│ ║ l.324: false //│ ║ ^^^^^^^^^ -//│ ║ l.327: then 0(0) +//│ ║ l.325: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.328: else 1 +//│ ║ l.326: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 885a6dd1ed..9452424ea9 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -101,7 +101,7 @@ class T class Foo(using T) with print(T) //│ JS (unsanitized): -//│ let Foo9; +//│ let Foo9; /** scoped **/ //│ Foo9 = function Foo(tmp5) { //│ return globalThis.Object.freeze(new Foo.class(tmp5)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index b7e9ea0e36..23ed0cba02 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -31,13 +31,14 @@ using Num = 0.42 :sjs let f1 = foo //│ JS (unsanitized): -//│ let f1, f11; -//│ f11 = function f1() { -//│ let tmp; +//│ let f1; +//│ let f11; /** scoped **/ +//│ f1 = function f1() { +//│ let tmp; /** scoped **/ //│ tmp = foo(); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; -//│ f1 = f11; +//│ f11 = f1; //│ f1 = fun f1 :expect [42] @@ -52,13 +53,14 @@ f1() :sjs let f2 = bar //│ JS (unsanitized): -//│ let f2, f21; -//│ f21 = function f2(x) { -//│ let tmp; +//│ let f2; +//│ let f21; /** scoped **/ +//│ f2 = function f2(x) { +//│ let tmp; /** scoped **/ //│ tmp = bar(x); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; -//│ f2 = f21; +//│ f21 = f2; //│ f2 = fun f2 :expect [0.42, 42] @@ -73,11 +75,12 @@ f2(0.42) :sjs let f3 = baz //│ JS (unsanitized): -//│ let f3, f31; -//│ f31 = function f3(x) { +//│ let f3; +//│ let f31; /** scoped **/ +//│ f3 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1, tmp2; +//│ let tmp, tmp1, tmp2; /** scoped **/ //│ tmp = baz(x); //│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); //│ tmp2 = runtime.safeCall(tmp1(y)); @@ -85,7 +88,7 @@ let f3 = baz //│ }); //│ return lambda1 //│ }; -//│ f3 = f31; +//│ f31 = f3; //│ f3 = fun f3 :expect [43, 42, 44, 0.42] @@ -96,14 +99,14 @@ f3(43)(44) :sjs foo() //│ JS (unsanitized): -//│ let tmp1; tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) +//│ let tmp1; /** scoped **/ tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) //│ = [42] // This should not expand :sjs bar(0.42) //│ JS (unsanitized): -//│ let tmp2; tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) +//│ let tmp2; /** scoped **/ tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) //│ = [0.42, 42] @@ -112,7 +115,7 @@ module Test with fun test(j)(using Int) = 0 fun main(using Int) = test //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -128,7 +131,7 @@ module Test with //│ static main(tmp3) { //│ let lambda1; //│ lambda1 = (undefined, function (j) { -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ tmp4 = Test.test(j); //│ return runtime.safeCall(tmp4(tmp3)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index 482d1d6532..a58b71139f 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; tmp2 = test(); tmp2(1, 1) +//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index de274fb30f..4cd471afe4 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -17,7 +17,8 @@ fun f() = print of raiseUnhandledEffect() j / i //│ JS (unsanitized): -//│ let f, getLocals2; +//│ let getLocals2; +//│ let f; /** scoped **/ //│ getLocals2 = function getLocals() { //│ let prev, thisInfo, arr, tmp; //│ prev = []; @@ -27,7 +28,8 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let i, j, k, scrut, tmp, tmp1, getLocals3, Cont$func$f$1, doUnwind; +//│ let getLocals3, Cont$func$f$1, doUnwind; +//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ //│ getLocals3 = function getLocals() { //│ let i1, j1, k1, prev, thisInfo, arr, tmp2; //│ prev = getLocals2(); @@ -126,8 +128,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:126:3) -//│ at lambda_test (Debugging.mls:123:3) +//│ at lambda (Debugging.mls:128:3) +//│ at lambda_test (Debugging.mls:125:3) import "../../mlscript-compile/Runtime.mls" @@ -206,9 +208,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:199:3) with locals: j=200 +//│ > at f (Debugging.mls:201:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:201:3) +//│ > at f (Debugging.mls:203:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:202:3) with locals: j=300 +//│ > at f (Debugging.mls:204:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 3c23c2b146..4b3fa31cce 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,9 +156,11 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let tmp20, handleBlock$11; +//│ let handleBlock$11; +//│ let tmp20; /** scoped **/ //│ handleBlock$11 = function handleBlock$() { -//│ let f, h, scrut, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; +//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; +//│ let f, scrut; /** scoped **/ //│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { //│ Handler$h$12 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index ee16b6ecbd..3b0ece8c6e 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -13,9 +13,9 @@ fun foo(h): module M = module A A //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(h) { -//│ let A2, A3, scrut; +//│ let A2, A3, scrut; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A2 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 7f9e99ade4..72893cfea0 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,8 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1; -//│ let tmp; /** scoped **/ +//│ let Lol1, tmp; /** scoped **/ //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index b15707c0ed..be8ffedbb6 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,9 +155,9 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let str, scrut, tmp24, tmp25, handleBlock$7; +//│ let handleBlock$7; +//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; /** scoped **/ //│ str = ""; -//│ let tmp26, tmp27; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ handleBlock$7 = function handleBlock$() { @@ -173,7 +173,8 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, Cont$handler$h1$perform$2, doUnwind1; +//│ let Cont$handler$h1$perform$2, doUnwind1; +//│ let tmp28, tmp29, tmp30; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h1$perform$2 = this @@ -231,11 +232,11 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 6) { -//│ tmp26 = value$; +//│ tmp25 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 6) { -//│ return tmp26 +//│ return tmp25 //│ } //│ break; //│ } @@ -260,7 +261,8 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, tmp31, tmp32, Cont$handler$h2$perform$2, doUnwind2; +//│ let Cont$handler$h2$perform$2, doUnwind2; +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h2$perform$2 = this @@ -321,7 +323,7 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 1) { -//│ tmp27 = value$; +//│ tmp26 = value$; //│ } else if (this.pc === 2) { //│ res7 = value$; //│ } @@ -346,9 +348,9 @@ str //│ res8.contTrace.last.next = new Cont$handleBlock$h2$2(pc); //│ return runtime.handleBlockImpl(res8, h2) //│ }; -//│ tmp27 = runtime.safeCall(h2.perform(runtime.Unit)); -//│ if (tmp27 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp27, 1) +//│ tmp26 = runtime.safeCall(h2.perform(runtime.Unit)); +//│ if (tmp26 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp26, 1) //│ } //│ res7 = runtime.safeCall(h1.perform(runtime.Unit)); //│ if (res7 instanceof runtime.EffectSig.class) { @@ -356,18 +358,18 @@ str //│ } //│ return res7 //│ }; -//│ tmp26 = handleBlock$8(); -//│ if (tmp26 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp26, 6) +//│ tmp25 = handleBlock$8(); +//│ if (tmp25 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp25, 6) //│ } -//│ return tmp26 +//│ return tmp25 //│ }; //│ tmp24 = handleBlock$7(); //│ if (tmp24 instanceof runtime.EffectSig.class) { //│ tmp24 = runtime.topLevelEffect(tmp24, false); //│ } -//│ tmp25 = tmp24; -//│ } else { tmp25 = runtime.Unit; } +//│ tmp27 = tmp24; +//│ } else { tmp27 = runtime.Unit; } //│ str //│ = "BABABA" //│ str = "BABABA" diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 722499592a..6dd7795543 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,7 +8,7 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let x, old, tmp, tmp1, tmp2; +//│ let x, old, tmp, tmp1, tmp2; /** scoped **/ //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 86684cf7ea..688ae8e6d2 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -20,9 +20,11 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi, res, $_stack$_safe$_body$_; +//│ let res, $_stack$_safe$_body$_; +//│ let hi; /** scoped **/ //│ hi = function hi(n) { -//│ let scrut, tmp, Cont$func$hi$1, doUnwind, stackDelayRes; +//│ let Cont$func$hi$1, doUnwind, stackDelayRes; +//│ let scrut, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$hi$1 = this @@ -74,9 +76,11 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1, res1, $_stack$_safe$_body$_1; +//│ let res1, $_stack$_safe$_body$_1; +//│ let sum1; /** scoped **/ //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1, Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; +//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1; /** scoped **/ //│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$sum$1 = this @@ -256,9 +260,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; +//│ let max; /** scoped **/ //│ max = function max(a, b) { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -270,7 +274,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls index 76a8e46f8f..a1596055ef 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls @@ -13,7 +13,7 @@ arr.map(_ === false) :sjs arr.map((e, ...) => e === false) //│ JS (unsanitized): -//│ let lambda1; +//│ let lambda1; /** scoped **/ //│ lambda1 = (undefined, function (e, ..._) { return e === false }); //│ runtime.safeCall(arr.map(lambda1)) //│ = [false, true] @@ -21,7 +21,7 @@ arr.map((e, ...) => e === false) :sjs arr.map((_, ...) => 1) //│ JS (unsanitized): -//│ let lambda2; +//│ let lambda2; /** scoped **/ //│ lambda2 = (undefined, function (_, ..._1) { return 1 }); //│ runtime.safeCall(arr.map(lambda2)) //│ = [1, 1] diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 9eb9276288..21b5a1233e 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,7 +88,8 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let Bad1, Good1, f5, tmp9, Bad$, Good$, f$capture3; +//│ let Bad1, Good1, Bad$, Good$, f$capture3; +//│ let f5, tmp9; /** scoped **/ //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; //│ if (isMut === true) { @@ -130,7 +131,7 @@ f().foo() //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ foo() { -//│ let tmp10, tmp11; +//│ let tmp10, tmp11; /** scoped **/ //│ this.z = 100; //│ tmp10 = this.x + this.y; //│ tmp11 = tmp10 + this.z; @@ -185,9 +186,9 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let x, y, z, tmp10, tmp11, capture; +//│ let capture; +//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ //│ capture = new f$capture3(null); -//│ let w; /** scoped **/ //│ x = 1; //│ y = 10; //│ z = 10; diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index df95d95b1e..9be85d298a 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -11,13 +11,13 @@ fun f(x) = fun g = new A //│ ═══[WARNING] Modules are not yet lifted. //│ JS (unsanitized): -//│ let f, g$; +//│ let g$; +//│ let f; /** scoped **/ //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; //│ f = function f(x) { -//│ let A1; -//│ let g; /** scoped **/ +//│ let A1, g; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index d5b4fad2c4..2bce7ec269 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -7,7 +7,8 @@ data class A(x) with fun getB() = x + y fun getA() = B(2).getB() //│ JS (unsanitized): -//│ let B1, A1, B$; +//│ let B1, B$; +//│ let A1; /** scoped **/ //│ B$ = function B$(isMut, A$instance, y) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -54,7 +55,7 @@ data class A(x) with //│ this.x = x; //│ } //│ getA() { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = B$(false, this, 2); //│ return tmp.getB() //│ } @@ -73,7 +74,8 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let g, A3, tmp1, g$; +//│ let g, g$; +//│ let A3, tmp1; /** scoped **/ //│ g$ = function g$(A$instance) { //│ return 2 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index 86ea2da67f..3c83263869 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -47,9 +47,10 @@ fun f(used1, unused1) = foo(used2) + unused2 f(1, 2) //│ JS (unsanitized): -//│ let g1, f3, g$3; +//│ let g1, g$3; +//│ let f3; /** scoped **/ //│ g$3 = function g$(used1, used2, g_arg) { -//│ let used3; +//│ let used3; /** scoped **/ //│ used3 = 2; //│ return used1 + used2 //│ }; @@ -59,7 +60,8 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let used2, unused2, foo, tmp, g$here; +//│ let g$here; +//│ let used2, unused2, foo, tmp; /** scoped **/ //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); @@ -77,9 +79,10 @@ fun f(a1, a2, a3, a4, a5, a6) = g f(1,2,3,4,5,6) //│ JS (unsanitized): -//│ let f4, g$4; +//│ let g$4; +//│ let f4; /** scoped **/ //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ tmp = a1 + a2; //│ tmp1 = tmp + a3; //│ tmp2 = tmp1 + a4; @@ -87,8 +90,7 @@ f(1,2,3,4,5,6) //│ return tmp3 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { -//│ let tmp; -//│ let g2; /** scoped **/ +//│ let g2, tmp; /** scoped **/ //│ tmp = g$4(a1, a2, a3, a4, a5, a6); //│ return tmp //│ }; @@ -163,7 +165,8 @@ fun f(unused, immutable, mutated) = a + h() + unused f(1, 2, 1000) //│ JS (unsanitized): -//│ let f7, h$2, g$6, f$capture5; +//│ let h$2, g$6, f$capture5; +//│ let f7; /** scoped **/ //│ g$6 = function g$(immutable, f$capture6) { //│ f$capture6.mutated$capture$0 = 2; //│ return immutable + f$capture6.mutated$capture$0 @@ -182,9 +185,9 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let a1, tmp7, tmp8, capture; +//│ let capture; +//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ //│ capture = new f$capture5(mutated); -//│ let g3, h2; /** scoped **/ //│ a1 = g$6(immutable, capture); //│ tmp7 = h$2(capture); //│ tmp8 = a1 + tmp7; @@ -253,15 +256,15 @@ fun g() = f g()(1) //│ JS (unsanitized): -//│ let f14, g6, tmp7, f$1, h$4, g$capture1; +//│ let f14, f$1, h$4, g$capture1; +//│ let g6, tmp7; /** scoped **/ //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; //│ x1 = 4; //│ return x1 + g$capture2.y$capture$0 //│ }; //│ f$1 = function f$(g$capture2, x1) { -//│ let k; -//│ let h3; /** scoped **/ +//│ let h3, k; /** scoped **/ //│ k = 4; //│ g$capture2.y$capture$0 = 2; //│ return x1 @@ -283,8 +286,8 @@ g()(1) //│ }); //│ g6 = function g() { //│ let capture, f$here; -//│ capture = new g$capture1(null); //│ let y1; /** scoped **/ +//│ capture = new g$capture1(null); //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here @@ -374,7 +377,8 @@ fun f(x) = set y = 2 [g, g] //│ JS (unsanitized): -//│ let g12, f23, g$14; +//│ let g12, g$14; +//│ let f23; /** scoped **/ //│ g$14 = function g$(y1) { //│ return y1 //│ }; @@ -384,7 +388,8 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let y1, scrut, g$here; +//│ let g$here; +//│ let y1, scrut; /** scoped **/ //│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { @@ -408,7 +413,8 @@ fun f(x) = set x += 1 [a, g] //│ JS (unsanitized): -//│ let g13, f24, g$15, f$capture17; +//│ let g13, g$15, f$capture17; +//│ let f24; /** scoped **/ //│ g$15 = function g$(f$capture18) { //│ return f$capture18.x$capture$0 //│ }; @@ -428,7 +434,8 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let a1, tmp10, capture, g$here; +//│ let capture, g$here; +//│ let a1, tmp10; /** scoped **/ //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); //│ a1 = g$here; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls index d5bc28e321..581a224319 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls @@ -7,7 +7,7 @@ import "../../mlscript-compile/Option.mls" module A with fun f(x) = x is Option.Some //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 2b34cb6860..4931b1249b 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -45,7 +45,8 @@ fun foo() = set x += 1 return () => x //│ JS (unsanitized): -//│ let foo2, lambda2, while$1, lambda$2, foo$capture5; +//│ let lambda2, while$1, lambda$2, foo$capture5; +//│ let foo2; /** scoped **/ //│ lambda$2 = function lambda$(foo$capture6) { //│ return foo$capture6.x$capture$0 //│ }; @@ -55,7 +56,8 @@ fun foo() = //│ } //│ }); //│ while$1 = function while$(foo$capture6) { -//│ let scrut, tmp2, lambda$here; +//│ let lambda$here; +//│ let scrut, tmp2; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ tmp2 = foo$capture6.x$capture$0 + 1; @@ -79,14 +81,14 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo2 = function foo() { -//│ let tmp2, tmp3, capture; +//│ let capture; +//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ //│ capture = new foo$capture5(null, null); -//│ let x, tmp4, while1; /** scoped **/ //│ capture.x$capture$0 = 1; //│ capture.tmp$capture$1 = undefined; -//│ tmp2 = while$1(capture); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return capture.tmp$capture$1 } +//│ tmp3 = while$1(capture); +//│ tmp4 = tmp3 !== runtime.LoopEnd; +//│ if (tmp4 === true) { return tmp3 } else { return capture.tmp$capture$1 } //│ }; :expect 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index c03f6dbe44..c09ff52620 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -27,7 +27,8 @@ fun foo(y) = (new M).foo() foo(10) //│ JS (unsanitized): -//│ let M3, foo1, M$; +//│ let M3, M$; +//│ let foo1; /** scoped **/ //│ M$ = function M$(isMut, y) { //│ let tmp; //│ if (isMut === true) { @@ -54,7 +55,7 @@ foo(10) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ foo1 = function foo(y) { let tmp; tmp = M$(false, y); return tmp.foo() }; +//│ foo1 = function foo(y) { let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() }; //│ foo1(10) @@ -109,7 +110,8 @@ fun foo(x, y) = fun foo3 = M.foo2() foo3 //│ JS (unsanitized): -//│ let M5, foo4, foo3$, foo$capture3; +//│ let M5, foo3$, foo$capture3; +//│ let foo4; /** scoped **/ //│ foo3$ = function foo3$(M6, x, foo$capture4) { //│ return M6.foo2() //│ }; @@ -145,9 +147,9 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let tmp, M$1, capture; +//│ let M$1, capture; +//│ let foo31, tmp; /** scoped **/ //│ capture = new foo$capture3(y); -//│ let foo31; /** scoped **/ //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); //│ tmp = foo3$(M$1, x, capture); //│ return tmp @@ -212,8 +214,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp5; -//│ let tmp6; /** scoped **/ +//│ let M17, tmp5, tmp6; /** scoped **/ //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -222,7 +223,7 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); //│ }; @@ -239,17 +240,17 @@ M.A().get //│ }); //│ scrut = M16.A(); //│ if (scrut instanceof M16.A.class) { -//│ tmp6 = 2; +//│ tmp5 = 2; //│ } else { -//│ tmp6 = 3; +//│ tmp5 = 3; //│ } -//│ this.x = tmp6; +//│ this.x = tmp5; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp5 = M17.A(); -//│ tmp5.get +//│ tmp6 = M17.A(); +//│ tmp6.get //│ = 4 module M with diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index dbba90138c..fae5e2cb30 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -12,7 +12,8 @@ fun foo() = xs.push(bar) set x = 2 //│ JS (unsanitized): -//│ let bar, foo, bar$, foo$capture1; +//│ let bar, bar$, foo$capture1; +//│ let foo; /** scoped **/ //│ bar$ = function bar$(foo$capture2) { //│ return foo$capture2.x$capture$0 //│ }; @@ -32,9 +33,9 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let tmp, capture, bar$here; +//│ let capture, bar$here; +//│ let x, tmp; /** scoped **/ //│ capture = new foo$capture1(null); -//│ let x; /** scoped **/ //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); //│ tmp = runtime.safeCall(xs.push(bar$here)); @@ -85,8 +86,13 @@ fun foo() = x bar //│ JS (unsanitized): -//│ let bar3, foo3; -//│ bar3 = function bar() { let x; x = undefined; return x }; +//│ let bar3; +//│ let foo3; /** scoped **/ +//│ bar3 = function bar() { +//│ let x; /** scoped **/ +//│ x = undefined; +//│ return x +//│ }; //│ foo3 = function foo() { return bar3 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 8b772c4748..964c518180 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -21,7 +21,8 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi, Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; +//│ let Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; +//│ let hi; /** scoped **/ //│ Cont$func$hi$$ = function Cont$func$hi$$(isMut, n, pc) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -60,7 +61,8 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let scrut, tmp, stackDelayRes; +//│ let stackDelayRes; +//│ let scrut, tmp; /** scoped **/ //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); //│ if (stackDelayRes instanceof runtime.EffectSig.class) { @@ -92,7 +94,8 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1, Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; +//│ let Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; +//│ let sum1; /** scoped **/ //│ Cont$func$sum$$ = function Cont$func$sum$$(isMut, n, tmp, pc) { //│ let tmp1, tmp2; //│ if (isMut === true) { @@ -148,7 +151,8 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1, curDepth, stackDelayRes; +//│ let curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1; /** scoped **/ //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); @@ -272,9 +276,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; +//│ let max; /** scoped **/ //│ max = function max(a, b) { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -286,7 +290,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index bb16dac916..ada3f10bb0 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -14,7 +14,7 @@ class A(x) with set z += 2 x //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ A1 = function A(x) { //│ return globalThis.Object.freeze(new A.class(x)); //│ }; @@ -42,7 +42,8 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let tmp, tmp1, idx1, idx2, idx3, idx4, idx5; +//│ let idx1, idx2, idx3, idx4, idx5; +//│ let tmp, tmp1; /** scoped **/ //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; //│ idx5 = idx + 0; @@ -60,7 +61,7 @@ class A(x) with //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ f(y) { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ tmp = this.#x + 1; //│ this.#x = tmp; //│ tmp1 = this.z + 2; diff --git a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls index 41df93f70a..226fd3ed63 100644 --- a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls +++ b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls @@ -20,7 +20,7 @@ //│ ║ l.16: 1 //│ ╙── ^ //│ JS (unsanitized): -//│ let tmp2; tmp2 = + 2; + 3 +//│ let tmp2; /** scoped **/ tmp2 = + 2; + 3 //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.16: 1 //│ ╙── ^ @@ -31,7 +31,7 @@ + 2 + 3 //│ JS (unsanitized): -//│ let tmp3; tmp3 = 1 + 2; tmp3 + 3 +//│ let tmp3; /** scoped **/ tmp3 = 1 + 2; tmp3 + 3 //│ = 6 1 diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 01db6e89df..87eb91c7eb 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let ls, a, middleElements, element0$; +//│ let ls, a, middleElements, element0$; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); @@ -133,9 +133,9 @@ fun popByIndex(start, end, acc, lft) = if start >= end then acc else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): -//│ let popByIndex; +//│ let popByIndex; /** scoped **/ //│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let scrut, tmp34, tmp35, tmp36; +//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ //│ scrut = start >= end; //│ if (scrut === true) { //│ return acc diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 25ac7189a4..783e6d19df 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; /** scoped **/ tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 27429ef9e1..72036ed9d4 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -20,7 +20,7 @@ x => if x is [[0]] then 1 //│ JS (unsanitized): //│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let element0$, element0$1, tmp; +//│ let element0$, element0$1, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { @@ -57,9 +57,9 @@ fun crazy(v) = S(S(S(S(S(S(0)))))) then "bruh!" _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): -//│ let crazy; +//│ let crazy; /** scoped **/ //│ crazy = function crazy(v) { -//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; +//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (v instanceof S.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index aef9d23d6a..de445d712f 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,7 +25,7 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, scrut5, tmp5; +//│ let scrut4, scrut5, tmp5; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ scrut4 = true; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 0ebb0ea24b..ca253fe2be 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,19 +10,22 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut; scrut = x === 0; if (scrut === true) { 1 } else { 2 } +//│ let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } //│ = 1 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; +//│ let a, scrut1, tmp; /** scoped **/ +//│ scrut1 = x === 0; +//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } +//│ a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; +//│ let scrut2, tmp1; /** scoped **/ //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -38,7 +41,7 @@ if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp2; +//│ let tmp2; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x === 0) { @@ -78,7 +81,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; +//│ let qqq, tmp3; /** scoped **/ //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -118,7 +121,7 @@ print of if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ if (x === 0) { @@ -159,9 +162,9 @@ fun foo(x, y, z) = 1 then "1" else "" //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(x1, y1, z1) { -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ split_root$3: { //│ split_1$3: { //│ if (x1 === 0) { @@ -205,7 +208,7 @@ print of if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ split_root$3: { //│ split_1$3: { //│ split_2$: { @@ -267,9 +270,9 @@ fun foo(x, y, z) = if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ foo1 = function foo(x1, y1, z1) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { //│ split_2$1: { @@ -334,9 +337,9 @@ fun foo(x, y, z) = if x is let value = "hello" expensive_call(value) //│ JS (unsanitized): -//│ let foo2; +//│ let foo2; /** scoped **/ //│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; +//│ let value, tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { @@ -377,7 +380,7 @@ fun foo(x, y, z) = if x is A then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo3; +//│ let foo3; /** scoped **/ //│ foo3 = function foo(x1, y1, z1) { //│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } //│ }; @@ -398,9 +401,9 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo4; +//│ let foo4; /** scoped **/ //│ foo4 = function foo(x1, y1, z1) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { //│ if (x1 instanceof A.class) { @@ -424,9 +427,9 @@ fun foo(x, y, z) = fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): -//│ let foo5; +//│ let foo5; /** scoped **/ //│ foo5 = function foo(x1, y1, z1) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_default$: { //│ if (x1 instanceof A.class) { @@ -464,7 +467,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; +//│ let y1, scrut3, tmp6; /** scoped **/ //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls index 18fb0d1700..db15ad49ed 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls @@ -17,7 +17,7 @@ if x then 0 y then 0 //│ JS (unsanitized): -//│ let tmp; +//│ let tmp; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index feb39394f2..04f680fc0b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -11,7 +11,7 @@ x => if x is Pair(A, B) then 1 //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x) { -//│ let argument0$, argument1$, tmp; +//│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair.class) { @@ -45,9 +45,9 @@ fun f(x) = if x is Pair(A, A) then 1 Pair(B, B) then 2 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f(x) { -//│ let argument0$, argument1$, tmp; +//│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 970b684cf9..697105bd95 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -36,9 +36,9 @@ fun foo(v) = A & B then 1 else 0 //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(v) { -//│ let tmp2; +//│ let tmp2; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 33339b6e3a..408479468b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -6,9 +6,9 @@ fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ JS (unsanitized): -//│ let nonsense; +//│ let nonsense; /** scoped **/ //│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; +//│ let ys, middleElements; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -27,9 +27,9 @@ fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ JS (unsanitized): -//│ let lead_and_last; +//│ let lead_and_last; /** scoped **/ //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; +//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -61,9 +61,9 @@ fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 //│ JS (unsanitized): -//│ let nested_tuple_patterns; +//│ let nested_tuple_patterns; /** scoped **/ //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index b203efa621..daa8dac993 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -33,9 +33,9 @@ Cross.unapply("0") :sjs fun foo(x) = x is Cross //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(x2) { -//│ let unapplyResult, output, bindings; +//│ let unapplyResult, output, bindings; /** scoped **/ //│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index e3c34d49df..b5e544bd4f 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -11,14 +11,15 @@ data class Pair[A, B](first: A, second: B) :sjs pattern SumPair = Pair(a, b) => a + b //│ JS (unsanitized): -//│ let SumPair1; +//│ let SumPair1; /** scoped **/ //│ globalThis.Object.freeze(class SumPair { //│ static { //│ SumPair1 = globalThis.Object.freeze(new this) //│ } //│ constructor() {} //│ unapply(input) { -//│ let transform, argument0$, argument1$, transformResult, tmp, lambda; +//│ let lambda; +//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ //│ lambda = (undefined, function (a, b) { //│ return a + b //│ }); From a9cdfd191c7a8860f43c958d56157e5111f3b0f1 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 28 Nov 2025 03:12:53 +0800 Subject: [PATCH 33/72] jsbuilder: scope.nest.givenIn --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 39 +- .../src/test/mlscript-compile/Predef.mjs | 140 +- .../src/test/mlscript-compile/Runtime.mjs | 1256 +++++++++-------- .../OverloadedModulesInSignatures.mls | 4 +- .../backlog/NonReturningStatements.mls | 2 +- .../src/test/mlscript/backlog/ToTriage.mls | 16 +- .../src/test/mlscript/basics/BadDefs.mls | 4 +- .../basics/CompanionModules_Classes.mls | 3 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../shared/src/test/mlscript/basics/Drop.mls | 9 +- .../test/mlscript/basics/FunnyRecordKeys.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 9 +- .../src/test/mlscript/basics/LazySpreads.mls | 64 +- .../mlscript/basics/MemberProjections.mls | 4 +- .../test/mlscript/basics/MiscArrayTests.mls | 2 +- .../mlscript/basics/MultiParamListClasses.mls | 14 +- .../test/mlscript/basics/MultiParamLists.mls | 51 +- .../mlscript/basics/MultilineExpressions.mls | 2 +- .../src/test/mlscript/basics/MutArr.mls | 4 +- .../src/test/mlscript/basics/MutRcd.mls | 2 +- .../src/test/mlscript/basics/MutVal.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../src/test/mlscript/basics/PartialApps.mls | 11 +- .../mlscript/basics/PureTermStatements.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 23 +- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/basics/Underscores.mls | 9 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 70 +- .../src/test/mlscript/bbml/bbGetters.mls | 53 +- .../src/test/mlscript/codegen/Arrays.mls | 4 +- .../src/test/mlscript/codegen/BadInit.mls | 4 +- .../src/test/mlscript/codegen/BadNew.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 2 +- .../src/test/mlscript/codegen/BuiltinOps.mls | 4 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 41 +- .../test/mlscript/codegen/CaseShorthand.mls | 15 +- .../test/mlscript/codegen/ClassInClass.mls | 31 +- .../src/test/mlscript/codegen/ClassInFun.mls | 70 +- .../test/mlscript/codegen/ClassMatching.mls | 126 +- .../src/test/mlscript/codegen/Classes.mls | 8 +- .../src/test/mlscript/codegen/Comma.mls | 26 +- .../src/test/mlscript/codegen/ConsoleLog.mls | 8 +- .../test/mlscript/codegen/DelayedLetInit.mls | 18 +- .../src/test/mlscript/codegen/EarlyReturn.mls | 19 +- .../src/test/mlscript/codegen/Formatting.mls | 21 +- .../src/test/mlscript/codegen/FunInClass.mls | 200 +-- .../test/mlscript/codegen/FunctionsThis.mls | 5 +- .../src/test/mlscript/codegen/Getters.mls | 179 +-- .../src/test/mlscript/codegen/GlobalThis.mls | 17 +- .../src/test/mlscript/codegen/Hygiene.mls | 31 +- .../src/test/mlscript/codegen/IfThenElse.mls | 36 +- .../src/test/mlscript/codegen/ImportMLs.mls | 10 +- .../src/test/mlscript/codegen/ImportedOps.mls | 11 +- .../test/mlscript/codegen/InlineLambdas.mls | 40 +- .../src/test/mlscript/codegen/Lambdas.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 41 +- .../test/mlscript/codegen/ModuleMethods.mls | 3 +- .../src/test/mlscript/codegen/Modules.mls | 11 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 23 +- .../test/mlscript/codegen/ParamClasses.mls | 33 +- .../src/test/mlscript/codegen/PartialApps.mls | 34 +- .../test/mlscript/codegen/PlainClasses.mls | 78 +- .../src/test/mlscript/codegen/Primes.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/Quasiquotes.mls | 9 +- .../src/test/mlscript/codegen/RandomStuff.mls | 36 +- .../test/mlscript/codegen/SanityChecks.mls | 46 +- .../src/test/mlscript/codegen/Scoped.mls | 306 ++-- .../test/mlscript/codegen/SelfReferences.mls | 5 +- .../src/test/mlscript/codegen/SetIn.mls | 96 +- .../src/test/mlscript/codegen/Spreads.mls | 2 +- .../shared/src/test/mlscript/codegen/This.mls | 7 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/ThisCalls.mls | 2 +- .../src/test/mlscript/codegen/Throw.mls | 6 +- .../src/test/mlscript/codegen/TraceLog.mls | 15 +- .../src/test/mlscript/codegen/UnitValue.mls | 10 +- .../src/test/mlscript/codegen/While.mls | 165 +-- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/ctx/EtaExpansion.mls | 63 +- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../src/test/mlscript/handlers/Debugging.mls | 162 +-- .../src/test/mlscript/handlers/Effects.mls | 114 +- .../test/mlscript/handlers/EffectsHygiene.mls | 51 +- .../mlscript/handlers/EffectsInClasses.mls | 4 +- .../mlscript/handlers/RecursiveHandlers.mls | 187 +-- .../test/mlscript/handlers/SetInHandlers.mls | 2 +- .../test/mlscript/handlers/StackSafety.mls | 177 +-- .../src/test/mlscript/interop/Arrays.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 37 +- .../test/mlscript/lifter/CompanionsInFun.mls | 32 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 15 +- .../src/test/mlscript/lifter/FunInFun.mls | 157 ++- .../src/test/mlscript/lifter/Imports.mls | 2 +- .../shared/src/test/mlscript/lifter/Loops.mls | 43 +- .../test/mlscript/lifter/ModulesObjects.mls | 69 +- .../src/test/mlscript/lifter/Mutation.mls | 34 +- .../test/mlscript/lifter/StackSafetyLift.mls | 83 +- .../src/test/mlscript/objbuf/Mutation.mls | 42 +- .../src/test/mlscript/parser/PrefixOps.mls | 4 +- .../test/mlscript/std/FingerTreeListTest.mls | 31 +- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 90 +- .../ucs/general/LogicalConnectives.mls | 2 +- .../ucs/normalization/Deduplication.mls | 246 ++-- .../normalization/ExcessiveDeduplication.mls | 2 +- .../ucs/normalization/SimplePairMatches.mls | 78 +- .../ucs/patterns/ConjunctionPattern.mls | 19 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 99 +- .../src/test/mlscript/ups/MatchResult.mls | 19 +- .../src/test/mlscript/ups/SimpleTransform.mls | 31 +- 112 files changed, 2873 insertions(+), 2709 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index c381f20519..c11f4ed307 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -510,18 +510,26 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: returningTerm(rst, endSemi).stripBreaks}" case Scoped(syms, body) => - val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => - if scope.lookup(l).isDefined then - // raise: - // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) - None - else - Some(l -> scope.allocateName(l)) - (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc"; /** scoped **/") :: returningTerm(body, endSemi) + scope.nest.givenIn: + val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => + if scope.lookup(l).isDefined then + // NOTE: this warning is turned off because the lifter is not + // yet updated to maintian the Scoped blocks, so when + // something is lifted out, its symbol may be already declared in an outer level, + // but the inner Scoped block still contains the same symbol + // raise: + // WarningReport(msg"var ${l.toString()} in scoped is already allocated" -> N :: Nil) + // Some(l -> s"${scope.lookup_!(l, N)}_again") + None + else + Some(l -> scope.allocateName(l)) + // NOTE: currently this does not generate pretty JS codes... + braced: + (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc"; /** scoped **/") :: returningTerm(body, endSemi) // case _ => ??? @@ -586,7 +594,12 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: then "./" + os.Path(path).relativeTo(wd).toString else path doc"""import ${getVar(i._1, N)} from "${relPath}";""" - imps.mkDocument(doc" # ") :/: block(p.main, endSemi = false).stripBreaks :: ( + // NOTE: this is to make sure that we are NOT generating the top level + // block in a nested scope, because for exported symbols they are looked up in the outer scope + val unscopedMain = p.main match + case Scoped(syms, body) /* if exprt.isDefined */ => body + case _ => p.main + imps.mkDocument(doc" # ") :/: block(unscopedMain, endSemi = false).stripBreaks :: ( exprt match case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" case N => doc"" diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index e7f4e8a4fa..c1955ffcc3 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -5,7 +5,7 @@ import Term from "./Term.mjs"; import RuntimeJS from "./RuntimeJS.mjs"; import Runtime from "./Runtime.mjs"; import Rendering from "./Rendering.mjs"; -let Predef1; /** scoped **/ +let Predef1; globalThis.Object.freeze(class Predef { static { Predef1 = this @@ -56,31 +56,35 @@ globalThis.Object.freeze(class Predef { static pipeFromHi(f, x) { return runtime.safeCall(f(x)) } - static tap(x, f) { - let tmp; /** scoped **/ - tmp = runtime.safeCall(f(x)); - return (tmp , x) + static tap(x, f) {{ + let tmp; /** scoped **/ + tmp = runtime.safeCall(f(x)); + return (tmp , x) + } } - static pat(f, x) { - let tmp; /** scoped **/ - tmp = runtime.safeCall(f(x)); - return (tmp , x) + static pat(f, x) {{ + let tmp; /** scoped **/ + tmp = runtime.safeCall(f(x)); + return (tmp , x) + } } static alsoDo(x, eff) { return x } static andThen(f, g) { - return (x) => { - let tmp; /** scoped **/ - tmp = runtime.safeCall(f(x)); - return runtime.safeCall(g(tmp)) + return (x) => {{ + let tmp; /** scoped **/ + tmp = runtime.safeCall(f(x)); + return runtime.safeCall(g(tmp)) + } } } static compose(f, g) { - return (x) => { - let tmp; /** scoped **/ - tmp = runtime.safeCall(g(x)); - return runtime.safeCall(f(tmp)) + return (x) => {{ + let tmp; /** scoped **/ + tmp = runtime.safeCall(g(x)); + return runtime.safeCall(f(tmp)) + } } } static passTo(receiver, f) { @@ -98,11 +102,12 @@ globalThis.Object.freeze(class Predef { return f.call(receiver, ...args) } } - static print(...xs) { - let tmp, tmp1; /** scoped **/ - tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); - tmp1 = runtime.safeCall(tmp(...xs)); - return runtime.safeCall(globalThis.console.log(...tmp1)) + static print(...xs) {{ + let tmp, tmp1; /** scoped **/ + tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); + tmp1 = runtime.safeCall(tmp(...xs)); + return runtime.safeCall(globalThis.console.log(...tmp1)) + } } static renderAsStr(arg) { if (typeof arg === 'string') { @@ -111,10 +116,11 @@ globalThis.Object.freeze(class Predef { return runtime.safeCall(Predef.render(arg)) } } - static notImplemented(msg) { - let tmp; /** scoped **/ - tmp = "Not implemented: " + msg; - throw globalThis.Error(tmp) + static notImplemented(msg) {{ + let tmp; /** scoped **/ + tmp = "Not implemented: " + msg; + throw globalThis.Error(tmp) + } } static get notImplementedError() { throw globalThis.Error("Not implemented"); @@ -123,50 +129,54 @@ globalThis.Object.freeze(class Predef { return xs } static foldr(f) { - return (first, ...rest) => { - let len, scrut, i, init, tmp; /** scoped **/ - len = rest.length; - scrut = len == 0; - if (scrut === true) { - return first - } else { - i = len - 1; - init = runtime.safeCall(rest.at(i)); - tmp1: while (true) { - let scrut1, tmp2, tmp3, tmp4; /** scoped **/ - scrut1 = i > 0; - if (scrut1 === true) { - tmp2 = i - 1; - i = tmp2; - tmp3 = runtime.safeCall(rest.at(i)); - tmp4 = runtime.safeCall(f(tmp3, init)); - init = tmp4; - tmp = runtime.Unit; - continue tmp1 - } else { - tmp = runtime.Unit; + return (first, ...rest) => {{ + let len, scrut, i, init, tmp; /** scoped **/ + len = rest.length; + scrut = len == 0; + if (scrut === true) { + return first + } else { + i = len - 1; + init = runtime.safeCall(rest.at(i)); + tmp1: while (true) {{ + let scrut1, tmp2, tmp3, tmp4; /** scoped **/ + scrut1 = i > 0; + if (scrut1 === true) { + tmp2 = i - 1; + i = tmp2; + tmp3 = runtime.safeCall(rest.at(i)); + tmp4 = runtime.safeCall(f(tmp3, init)); + init = tmp4; + tmp = runtime.Unit; + continue tmp1 + } else { + tmp = runtime.Unit; + } + } + break; } - break; + return runtime.safeCall(f(first, init)) } - return runtime.safeCall(f(first, init)) } } } - static mkStr(...xs) { - let lambda, tmp; /** scoped **/ - lambda = (undefined, function (acc, x) { - let tmp1, tmp2, tmp3; /** scoped **/ - if (typeof x === 'string') { - tmp1 = true; - } else { - tmp1 = false; - } - tmp2 = runtime.safeCall(Predef.assert(tmp1)); - tmp3 = acc + x; - return (tmp2 , tmp3) - }); - tmp = runtime.safeCall(Predef.fold(lambda)); - return runtime.safeCall(tmp(...xs)) + static mkStr(...xs) {{ + let lambda, tmp; /** scoped **/ + lambda = (undefined, function (acc, x) {{ + let tmp1, tmp2, tmp3; /** scoped **/ + if (typeof x === 'string') { + tmp1 = true; + } else { + tmp1 = false; + } + tmp2 = runtime.safeCall(Predef.assert(tmp1)); + tmp3 = acc + x; + return (tmp2 , tmp3) + } + }); + tmp = runtime.safeCall(Predef.fold(lambda)); + return runtime.safeCall(tmp(...xs)) + } } static use(instance) { return instance diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index a2cb048716..cd89dbd87a 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -6,7 +6,7 @@ import RuntimeJS from "./RuntimeJS.mjs"; import Rendering from "./Rendering.mjs"; import LazyArray from "./LazyArray.mjs"; import Iter from "./Iter.mjs"; -let Runtime1; /** scoped **/ +let Runtime1; globalThis.Object.freeze(class Runtime { static { Runtime1 = this @@ -73,15 +73,17 @@ globalThis.Object.freeze(class Runtime { this.reified = this.#_reified; } #_reified; - resumeWith(value) { - let lambda; /** scoped **/ - const this$EffectHandle = this; - lambda = (undefined, function () { - let tmp; /** scoped **/ - tmp = Runtime.resume(this$EffectHandle.reified.contTrace); - return runtime.safeCall(tmp(value)) - }); - return Runtime1.try(lambda) + resumeWith(value) {{ + let lambda; /** scoped **/ + const this$EffectHandle = this; + lambda = (undefined, function () {{ + let tmp; /** scoped **/ + tmp = Runtime.resume(this$EffectHandle.reified.contTrace); + return runtime.safeCall(tmp(value)) + } + }); + return Runtime1.try(lambda) + } } raise() { return Runtime.topLevelEffect(this.reified, false) @@ -126,35 +128,38 @@ globalThis.Object.freeze(class Runtime { static { this.split = LazyArray.__split; } - static slice(xs, i, j) { - let tmp; /** scoped **/ - tmp = xs.length - j; - return xs.slice(i, tmp) + static slice(xs, i, j) {{ + let tmp; /** scoped **/ + tmp = xs.length - j; + return xs.slice(i, tmp) + } } - static lazySlice(xs, i, j) { - let tmp; /** scoped **/ - tmp = LazyArray.dropLeftRight(i, j); - return runtime.safeCall(tmp(xs)) + static lazySlice(xs, i, j) {{ + let tmp; /** scoped **/ + tmp = LazyArray.dropLeftRight(i, j); + return runtime.safeCall(tmp(xs)) + } } static lazyConcat(...args) { return runtime.safeCall(LazyArray.__concat(...args)) } - static get(xs, i) { - let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ - scrut = i >= xs.length; - if (scrut === true) { - throw globalThis.RangeError("Tuple.get: index out of bounds") - } else { - tmp = runtime.Unit; - } - tmp1 = - xs.length; - scrut1 = i < tmp1; - if (scrut1 === true) { - throw globalThis.RangeError("Tuple.get: negative index out of bounds") - } else { - tmp2 = runtime.Unit; + static get(xs, i) {{ + let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ + scrut = i >= xs.length; + if (scrut === true) { + throw globalThis.RangeError("Tuple.get: index out of bounds") + } else { + tmp = runtime.Unit; + } + tmp1 = - xs.length; + scrut1 = i < tmp1; + if (scrut1 === true) { + throw globalThis.RangeError("Tuple.get: negative index out of bounds") + } else { + tmp2 = runtime.Unit; + } + return xs.at(i) } - return xs.at(i) } static isArrayLike(xs) { return runtime.safeCall(Iter.isArrayLike(xs)) @@ -172,13 +177,14 @@ globalThis.Object.freeze(class Runtime { static startsWith(string, prefix) { return runtime.safeCall(string.startsWith(prefix)) } - static get(string, i) { - let scrut; /** scoped **/ - scrut = i >= string.length; - if (scrut === true) { - throw globalThis.RangeError("Str.get: index out of bounds") - } else { - return runtime.safeCall(string.at(i)) + static get(string, i) {{ + let scrut; /** scoped **/ + scrut = i >= string.length; + if (scrut === true) { + throw globalThis.RangeError("Str.get: index out of bounds") + } else { + return runtime.safeCall(string.at(i)) + } } } static take(string, n) { @@ -208,40 +214,43 @@ globalThis.Object.freeze(class Runtime { this.enabled = false; this.indentLvl = 0; } - static indent() { - let scrut, prev, tmp; /** scoped **/ - scrut = TraceLogger.enabled; - if (scrut === true) { - prev = TraceLogger.indentLvl; - tmp = prev + 1; - TraceLogger.indentLvl = tmp; - return prev - } else { - return runtime.Unit + static indent() {{ + let scrut, prev, tmp; /** scoped **/ + scrut = TraceLogger.enabled; + if (scrut === true) { + prev = TraceLogger.indentLvl; + tmp = prev + 1; + TraceLogger.indentLvl = tmp; + return prev + } else { + return runtime.Unit + } } } - static resetIndent(n) { - let scrut; /** scoped **/ - scrut = TraceLogger.enabled; - if (scrut === true) { - TraceLogger.indentLvl = n; - return runtime.Unit - } else { - return runtime.Unit + static resetIndent(n) {{ + let scrut; /** scoped **/ + scrut = TraceLogger.enabled; + if (scrut === true) { + TraceLogger.indentLvl = n; + return runtime.Unit + } else { + return runtime.Unit + } } } - static log(msg) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ - scrut = TraceLogger.enabled; - if (scrut === true) { - tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); - tmp1 = runtime.safeCall(" ".repeat(TraceLogger.indentLvl)); - tmp2 = "\n" + tmp1; - tmp3 = msg.replaceAll("\n", tmp2); - tmp4 = tmp + tmp3; - return runtime.safeCall(globalThis.console.log(tmp4)) - } else { - return runtime.Unit + static log(msg) {{ + let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + scrut = TraceLogger.enabled; + if (scrut === true) { + tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); + tmp1 = runtime.safeCall(" ".repeat(TraceLogger.indentLvl)); + tmp2 = "\n" + tmp1; + tmp3 = msg.replaceAll("\n", tmp2); + tmp4 = tmp + tmp3; + return runtime.safeCall(globalThis.console.log(tmp4)) + } else { + return runtime.Unit + } } } toString() { return runtime.render(this); } @@ -386,13 +395,14 @@ globalThis.Object.freeze(class Runtime { value: StackDelayHandler }) } - delay() { - let lambda; /** scoped **/ - lambda = (undefined, function (k) { - Runtime.stackResume = k; - return runtime.Unit - }); - return Runtime.mkEffect(this, lambda) + delay() {{ + let lambda; /** scoped **/ + lambda = (undefined, function (k) { + Runtime.stackResume = k; + return runtime.Unit + }); + return Runtime.mkEffect(this, lambda) + } } toString() { return runtime.render(this); } static [definitionMetadata] = ["object", "StackDelayHandler"]; @@ -408,16 +418,18 @@ globalThis.Object.freeze(class Runtime { this.#v = v; } #v; - zext() { - let tmp, tmp1; /** scoped **/ - tmp = Runtime.shl(1, 31); - tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); - return Runtime.bitand(this.#v, tmp1) + zext() {{ + let tmp, tmp1; /** scoped **/ + tmp = Runtime.shl(1, 31); + tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); + return Runtime.bitand(this.#v, tmp1) + } } - sext() { - let tmp; /** scoped **/ - tmp = Runtime.shl(1, 31); - return Runtime.bitor(this.#v, tmp) + sext() {{ + let tmp; /** scoped **/ + tmp = Runtime.shl(1, 31); + return Runtime.bitor(this.#v, tmp) + } } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Int31", [null]]; @@ -426,48 +438,50 @@ globalThis.Object.freeze(class Runtime { static get unreachable() { throw globalThis.Error("unreachable"); } - static checkArgs(functionName, expected, isUB, got) { - let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ - tmp = got < expected; - lambda = (undefined, function () { - lambda1 = (undefined, function () { - return got > expected + static checkArgs(functionName, expected, isUB, got) {{ + let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ + tmp = got < expected; + lambda = (undefined, function () { + lambda1 = (undefined, function () { + return got > expected + }); + return runtime.short_and(isUB, lambda1) }); - return runtime.short_and(isUB, lambda1) - }); - scrut = runtime.short_or(tmp, lambda); - if (scrut === true) { - let scrut1, scrut2, tmp12; /** scoped **/ - scrut1 = functionName.length > 0; - if (scrut1 === true) { - tmp12 = " '" + functionName; - tmp1 = tmp12 + "'"; - } else { - tmp1 = ""; - } - name = tmp1; - tmp2 = "Function" + name; - tmp3 = tmp2 + " expected "; - if (isUB === true) { - tmp4 = ""; - } else { - tmp4 = "at least "; - } - tmp5 = tmp3 + tmp4; - tmp6 = tmp5 + expected; - tmp7 = tmp6 + " argument"; - scrut2 = expected === 1; - if (scrut2 === true) { - tmp8 = ""; + scrut = runtime.short_or(tmp, lambda); + if (scrut === true) {{ + let scrut1, scrut2, tmp12; /** scoped **/ + scrut1 = functionName.length > 0; + if (scrut1 === true) { + tmp12 = " '" + functionName; + tmp1 = tmp12 + "'"; + } else { + tmp1 = ""; + } + name = tmp1; + tmp2 = "Function" + name; + tmp3 = tmp2 + " expected "; + if (isUB === true) { + tmp4 = ""; + } else { + tmp4 = "at least "; + } + tmp5 = tmp3 + tmp4; + tmp6 = tmp5 + expected; + tmp7 = tmp6 + " argument"; + scrut2 = expected === 1; + if (scrut2 === true) { + tmp8 = ""; + } else { + tmp8 = "s"; + } + tmp9 = tmp7 + tmp8; + tmp10 = tmp9 + " but got "; + tmp11 = tmp10 + got; + throw globalThis.Error(tmp11) + } } else { - tmp8 = "s"; + return runtime.Unit } - tmp9 = tmp7 + tmp8; - tmp10 = tmp9 + " but got "; - tmp11 = tmp10 + got; - throw globalThis.Error(tmp11) - } else { - return runtime.Unit } } static safeCall(x) { @@ -484,571 +498,617 @@ globalThis.Object.freeze(class Runtime { return x } } - static deboundMethod(mtdName, clsName) { - let tmp, tmp1, tmp2, tmp3; /** scoped **/ - tmp = "[debinding error] Method '" + mtdName; - tmp1 = tmp + "' of class '"; - tmp2 = tmp1 + clsName; - tmp3 = tmp2 + "' was accessed without being called."; - throw globalThis.Error(tmp3) + static deboundMethod(mtdName, clsName) {{ + let tmp, tmp1, tmp2, tmp3; /** scoped **/ + tmp = "[debinding error] Method '" + mtdName; + tmp1 = tmp + "' of class '"; + tmp2 = tmp1 + clsName; + tmp3 = tmp2 + "' was accessed without being called."; + throw globalThis.Error(tmp3) + } } - static try(f) { - let res; /** scoped **/ - res = runtime.safeCall(f()); - if (res instanceof Runtime.EffectSig.class) { - return Runtime.EffectHandle(res) - } else { - return res + static try(f) {{ + let res; /** scoped **/ + res = runtime.safeCall(f()); + if (res instanceof Runtime.EffectSig.class) { + return Runtime.EffectHandle(res) + } else { + return res + } } } - static printRaw(x) { - let rcd, tmp; /** scoped **/ - rcd = globalThis.Object.freeze({ - indent: 2, - breakLength: 76 - }); - tmp = Runtime.render(x, rcd); - return runtime.safeCall(globalThis.console.log(tmp)) + static printRaw(x) {{ + let rcd, tmp; /** scoped **/ + rcd = globalThis.Object.freeze({ + indent: 2, + breakLength: 76 + }); + tmp = Runtime.render(x, rcd); + return runtime.safeCall(globalThis.console.log(tmp)) + } } static raisePrintStackEffect(showLocals) { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } - static topLevelEffect(tr, debug) { - let tmp, tmp1; /** scoped **/ - tmp2: while (true) { - let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ - scrut = tr.handler === Runtime.PrintStackEffect; - if (scrut === true) { - tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); - tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); - tmp5 = Runtime.resume(tr.contTrace); - tmp6 = runtime.safeCall(tmp5(runtime.Unit)); - tr = tmp6; - tmp = runtime.Unit; - continue tmp2 + static topLevelEffect(tr, debug) {{ + let tmp, tmp1; /** scoped **/ + tmp2: while (true) {{ + let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + scrut = tr.handler === Runtime.PrintStackEffect; + if (scrut === true) { + tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); + tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); + tmp5 = Runtime.resume(tr.contTrace); + tmp6 = runtime.safeCall(tmp5(runtime.Unit)); + tr = tmp6; + tmp = runtime.Unit; + continue tmp2 + } else { + tmp = runtime.Unit; + } + } + break; + } + if (tr instanceof Runtime.EffectSig.class) { + tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; + throw Runtime.showStackTrace(tmp1, tr, debug, false) } else { - tmp = runtime.Unit; + return tr } - break; - } - if (tr instanceof Runtime.EffectSig.class) { - tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; - throw Runtime.showStackTrace(tmp1, tr, debug, false) - } else { - return tr } } - static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ - msg = header; - curHandler = tr.contTrace; - atTail = true; - if (debug === true) { - let tmp3; /** scoped **/ - tmp4: while (true) { - let scrut, cur, tmp5, tmp6; /** scoped **/ - scrut = curHandler !== null; - if (scrut === true) { - let scrut1, tmp7, tmp8; /** scoped **/ - cur = curHandler.next; - tmp9: while (true) { - let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ - scrut2 = cur !== null; - if (scrut2 === true) { - let scrut3, lambda, tmp19, tmp20; /** scoped **/ - locals = cur.getLocals; - tmp10 = locals.length - 1; - curLocals = runtime.safeCall(locals.at(tmp10)); - loc = cur.getLoc; - if (loc === null) { - tmp11 = "pc=" + cur.pc; - } else { - tmp11 = loc; - } - loc1 = tmp11; - split_root$: { - split_1$: { - if (showLocals === true) { - scrut3 = curLocals.locals.length > 0; - if (scrut3 === true) { - lambda = (undefined, function (l) { - let tmp21, tmp22; /** scoped **/ - tmp21 = l.localName + "="; - tmp22 = Rendering.render(l.value); - return tmp21 + tmp22 - }); - tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); - tmp20 = runtime.safeCall(tmp19.join(", ")); - tmp12 = " with locals: " + tmp20; - break split_root$ - } else { - break split_1$ + static showStackTrace(header, tr, debug, showLocals) {{ + let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ + msg = header; + curHandler = tr.contTrace; + atTail = true; + if (debug === true) {{ + let tmp3; /** scoped **/ + tmp4: while (true) {{ + let scrut, cur, tmp5, tmp6; /** scoped **/ + scrut = curHandler !== null; + if (scrut === true) {{ + let scrut1, tmp7, tmp8; /** scoped **/ + cur = curHandler.next; + tmp9: while (true) {{ + let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + scrut2 = cur !== null; + if (scrut2 === true) {{ + let scrut3, lambda, tmp19, tmp20; /** scoped **/ + locals = cur.getLocals; + tmp10 = locals.length - 1; + curLocals = runtime.safeCall(locals.at(tmp10)); + loc = cur.getLoc; + if (loc === null) { + tmp11 = "pc=" + cur.pc; + } else { + tmp11 = loc; + } + loc1 = tmp11; + split_root$: { + split_1$: { + if (showLocals === true) { + scrut3 = curLocals.locals.length > 0; + if (scrut3 === true) { + lambda = (undefined, function (l) {{ + let tmp21, tmp22; /** scoped **/ + tmp21 = l.localName + "="; + tmp22 = Rendering.render(l.value); + return tmp21 + tmp22 + } + }); + tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); + tmp20 = runtime.safeCall(tmp19.join(", ")); + tmp12 = " with locals: " + tmp20; + break split_root$ + } else { + break split_1$ + } + } else { + break split_1$ + } + } + tmp12 = ""; + } + localsMsg = tmp12; + tmp13 = "\n\tat " + curLocals.fnName; + tmp14 = tmp13 + " ("; + tmp15 = tmp14 + loc1; + tmp16 = tmp15 + ")"; + tmp17 = msg + tmp16; + msg = tmp17; + tmp18 = msg + localsMsg; + msg = tmp18; + cur = cur.next; + atTail = false; + tmp5 = runtime.Unit; + continue tmp9 + } + } else { + tmp5 = runtime.Unit; + } } + break; + } + curHandler = curHandler.nextHandler; + scrut1 = curHandler !== null; + if (scrut1 === true) { + tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; + tmp8 = msg + tmp7; + msg = tmp8; + atTail = false; + tmp6 = runtime.Unit; } else { - break split_1$ + tmp6 = runtime.Unit; } + tmp = tmp6; + continue tmp4 } - tmp12 = ""; + } else { + tmp = runtime.Unit; } - localsMsg = tmp12; - tmp13 = "\n\tat " + curLocals.fnName; - tmp14 = tmp13 + " ("; - tmp15 = tmp14 + loc1; - tmp16 = tmp15 + ")"; - tmp17 = msg + tmp16; - msg = tmp17; - tmp18 = msg + localsMsg; - msg = tmp18; - cur = cur.next; - atTail = false; - tmp5 = runtime.Unit; - continue tmp9 - } else { - tmp5 = runtime.Unit; } break; } - curHandler = curHandler.nextHandler; - scrut1 = curHandler !== null; - if (scrut1 === true) { - tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; - tmp8 = msg + tmp7; - msg = tmp8; - atTail = false; - tmp6 = runtime.Unit; + if (atTail === true) { + tmp3 = msg + "\n\tat tail position"; + msg = tmp3; + tmp1 = runtime.Unit; } else { - tmp6 = runtime.Unit; + tmp1 = runtime.Unit; } - tmp = tmp6; - continue tmp4 - } else { - tmp = runtime.Unit; + tmp2 = tmp1; } - break; - } - if (atTail === true) { - tmp3 = msg + "\n\tat tail position"; - msg = tmp3; - tmp1 = runtime.Unit; } else { - tmp1 = runtime.Unit; + tmp2 = runtime.Unit; } - tmp2 = tmp1; - } else { - tmp2 = runtime.Unit; + return msg } - return msg } - static showFunctionContChain(cont, hl, vis, reps) { - let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ - if (cont instanceof Runtime.FunctionContFrame.class) { - let scrut, tmp5, tmp6, tmp7; /** scoped **/ - tmp = cont.constructor.name + "(pc="; - result = tmp + cont.pc; - lambda = (undefined, function (m, marker) { - let scrut1, tmp8, tmp9; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { - tmp8 = ", " + marker; - tmp9 = result + tmp8; - result = tmp9; - return runtime.Unit - } else { - return runtime.Unit + static showFunctionContChain(cont, hl, vis, reps) {{ + let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + if (cont instanceof Runtime.FunctionContFrame.class) {{ + let scrut, tmp5, tmp6, tmp7; /** scoped **/ + tmp = cont.constructor.name + "(pc="; + result = tmp + cont.pc; + lambda = (undefined, function (m, marker) {{ + let scrut1, tmp8, tmp9; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { + tmp8 = ", " + marker; + tmp9 = result + tmp8; + result = tmp9; + return runtime.Unit + } else { + return runtime.Unit + } + } + }); + tmp1 = runtime.safeCall(hl.forEach(lambda)); + scrut = runtime.safeCall(vis.has(cont)); + if (scrut === true) {{ + let scrut1; /** scoped **/ + tmp5 = reps + 1; + reps = tmp5; + scrut1 = reps > 10; + if (scrut1 === true) { + throw globalThis.Error("10 repeated continuation frame (loop?)") + } else { + tmp6 = runtime.Unit; + } + tmp7 = result + ", REPEAT"; + result = tmp7; + tmp2 = runtime.Unit; + } + } else { + tmp2 = runtime.safeCall(vis.add(cont)); + } + tmp3 = result + ") -> "; + tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp3 + tmp4 } - }); - tmp1 = runtime.safeCall(hl.forEach(lambda)); - scrut = runtime.safeCall(vis.has(cont)); - if (scrut === true) { - let scrut1; /** scoped **/ - tmp5 = reps + 1; - reps = tmp5; - scrut1 = reps > 10; - if (scrut1 === true) { - throw globalThis.Error("10 repeated continuation frame (loop?)") - } else { - tmp6 = runtime.Unit; + } else {{ + let scrut; /** scoped **/ + scrut = cont === null; + if (scrut === true) { + return "(null)" + } else { + return "(NOT CONT)" + } } - tmp7 = result + ", REPEAT"; - result = tmp7; - tmp2 = runtime.Unit; - } else { - tmp2 = runtime.safeCall(vis.add(cont)); - } - tmp3 = result + ") -> "; - tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp3 + tmp4 - } else { - let scrut2; /** scoped **/ - scrut2 = cont === null; - if (scrut2 === true) { - return "(null)" - } else { - return "(NOT CONT)" } } } - static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ - if (cont instanceof Runtime.HandlerContFrame.class) { - let scrut, tmp4, tmp5, tmp6; /** scoped **/ - result = cont.handler.constructor.name; - lambda = (undefined, function (m, marker) { - let scrut1, tmp7, tmp8; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { - tmp7 = ", " + marker; - tmp8 = result + tmp7; - result = tmp8; - return runtime.Unit - } else { - return runtime.Unit + static showHandlerContChain(cont, hl, vis, reps) {{ + let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ + if (cont instanceof Runtime.HandlerContFrame.class) {{ + let scrut, tmp4, tmp5, tmp6; /** scoped **/ + result = cont.handler.constructor.name; + lambda = (undefined, function (m, marker) {{ + let scrut1, tmp7, tmp8; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { + tmp7 = ", " + marker; + tmp8 = result + tmp7; + result = tmp8; + return runtime.Unit + } else { + return runtime.Unit + } + } + }); + tmp = runtime.safeCall(hl.forEach(lambda)); + scrut = runtime.safeCall(vis.has(cont)); + if (scrut === true) {{ + let scrut1; /** scoped **/ + tmp4 = reps + 1; + reps = tmp4; + scrut1 = reps > 10; + if (scrut1 === true) { + throw globalThis.Error("10 repeated continuation frame (loop?)") + } else { + tmp5 = runtime.Unit; + } + tmp6 = result + ", REPEAT"; + result = tmp6; + tmp1 = runtime.Unit; + } + } else { + tmp1 = runtime.safeCall(vis.add(cont)); + } + tmp2 = result + " -> "; + tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp2 + tmp3 } - }); - tmp = runtime.safeCall(hl.forEach(lambda)); - scrut = runtime.safeCall(vis.has(cont)); - if (scrut === true) { - let scrut1; /** scoped **/ - tmp4 = reps + 1; - reps = tmp4; - scrut1 = reps > 10; - if (scrut1 === true) { - throw globalThis.Error("10 repeated continuation frame (loop?)") - } else { - tmp5 = runtime.Unit; + } else {{ + let scrut; /** scoped **/ + scrut = cont === null; + if (scrut === true) { + return "(null)" + } else { + return "(NOT HANDLER CONT)" + } } - tmp6 = result + ", REPEAT"; - result = tmp6; - tmp1 = runtime.Unit; - } else { - tmp1 = runtime.safeCall(vis.add(cont)); - } - tmp2 = result + " -> "; - tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp2 + tmp3 - } else { - let scrut2; /** scoped **/ - scrut2 = cont === null; - if (scrut2 === true) { - return "(null)" - } else { - return "(NOT HANDLER CONT)" } } } - static debugCont(cont) { - let tmp, tmp1, tmp2; /** scoped **/ - tmp = globalThis.Object.freeze(new globalThis.Map()); - tmp1 = globalThis.Object.freeze(new globalThis.Set()); - tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); - return runtime.safeCall(globalThis.console.log(tmp2)) + static debugCont(cont) {{ + let tmp, tmp1, tmp2; /** scoped **/ + tmp = globalThis.Object.freeze(new globalThis.Map()); + tmp1 = globalThis.Object.freeze(new globalThis.Set()); + tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); + return runtime.safeCall(globalThis.console.log(tmp2)) + } } - static debugHandler(cont) { - let tmp, tmp1, tmp2; /** scoped **/ - tmp = globalThis.Object.freeze(new globalThis.Map()); - tmp1 = globalThis.Object.freeze(new globalThis.Set()); - tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); - return runtime.safeCall(globalThis.console.log(tmp2)) + static debugHandler(cont) {{ + let tmp, tmp1, tmp2; /** scoped **/ + tmp = globalThis.Object.freeze(new globalThis.Map()); + tmp1 = globalThis.Object.freeze(new globalThis.Set()); + tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); + return runtime.safeCall(globalThis.console.log(tmp2)) + } } - static debugContTrace(contTrace) { - let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ - if (contTrace instanceof Runtime.ContTrace.class) { - let scrut, scrut1; /** scoped **/ - tmp = globalThis.console.log("resumed: ", contTrace.resumed); - scrut = contTrace.last === contTrace; - if (scrut === true) { - tmp1 = runtime.safeCall(globalThis.console.log("")); + static debugContTrace(contTrace) {{ + let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + if (contTrace instanceof Runtime.ContTrace.class) {{ + let scrut, scrut1; /** scoped **/ + tmp = globalThis.console.log("resumed: ", contTrace.resumed); + scrut = contTrace.last === contTrace; + if (scrut === true) { + tmp1 = runtime.safeCall(globalThis.console.log("")); + } else { + tmp1 = runtime.Unit; + } + scrut1 = contTrace.lastHandler === contTrace; + if (scrut1 === true) { + tmp2 = runtime.safeCall(globalThis.console.log("")); + } else { + tmp2 = runtime.Unit; + } + vis = globalThis.Object.freeze(new globalThis.Set()); + hl = globalThis.Object.freeze(new globalThis.Map()); + tmp3 = globalThis.Object.freeze([ + contTrace.last + ]); + tmp4 = globalThis.Object.freeze(new globalThis.Set(tmp3)); + tmp5 = hl.set("last", tmp4); + tmp6 = globalThis.Object.freeze([ + contTrace.lastHandler + ]); + tmp7 = globalThis.Object.freeze(new globalThis.Set(tmp6)); + tmp8 = hl.set("last-handler", tmp7); + tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); + tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); + cur = contTrace.nextHandler; + tmp13: while (true) {{ + let scrut2, tmp14, tmp15; /** scoped **/ + scrut2 = cur !== null; + if (scrut2 === true) { + tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); + tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); + cur = cur.nextHandler; + tmp11 = runtime.Unit; + continue tmp13 + } else { + tmp11 = runtime.Unit; + } + } + break; + } + return runtime.safeCall(globalThis.console.log()) + } } else { - tmp1 = runtime.Unit; + tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); + return runtime.safeCall(globalThis.console.log(contTrace)) } - scrut1 = contTrace.lastHandler === contTrace; - if (scrut1 === true) { - tmp2 = runtime.safeCall(globalThis.console.log("")); + } + } + static debugEff(eff) {{ + let tmp, tmp1, tmp2, tmp3; /** scoped **/ + if (eff instanceof Runtime.EffectSig.class) { + tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); + tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); + tmp2 = globalThis.console.log("handlerFun: ", eff.handlerFun); + return Runtime.debugContTrace(eff.contTrace) } else { - tmp2 = runtime.Unit; + tmp3 = runtime.safeCall(globalThis.console.log("Not an effect:")); + return runtime.safeCall(globalThis.console.log(eff)) } - vis = globalThis.Object.freeze(new globalThis.Set()); - hl = globalThis.Object.freeze(new globalThis.Map()); - tmp3 = globalThis.Object.freeze([ - contTrace.last - ]); - tmp4 = globalThis.Object.freeze(new globalThis.Set(tmp3)); - tmp5 = hl.set("last", tmp4); - tmp6 = globalThis.Object.freeze([ - contTrace.lastHandler - ]); - tmp7 = globalThis.Object.freeze(new globalThis.Set(tmp6)); - tmp8 = hl.set("last-handler", tmp7); - tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); - tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); - cur = contTrace.nextHandler; - tmp13: while (true) { - let scrut2, tmp14, tmp15; /** scoped **/ - scrut2 = cur !== null; - if (scrut2 === true) { - tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); - tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); - cur = cur.nextHandler; - tmp11 = runtime.Unit; - continue tmp13 - } else { - tmp11 = runtime.Unit; - } - break; - } - return runtime.safeCall(globalThis.console.log()) - } else { - tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); - return runtime.safeCall(globalThis.console.log(contTrace)) } } - static debugEff(eff) { - let tmp, tmp1, tmp2, tmp3; /** scoped **/ - if (eff instanceof Runtime.EffectSig.class) { - tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); - tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); - tmp2 = globalThis.console.log("handlerFun: ", eff.handlerFun); - return Runtime.debugContTrace(eff.contTrace) - } else { - tmp3 = runtime.safeCall(globalThis.console.log("Not an effect:")); - return runtime.safeCall(globalThis.console.log(eff)) + static mkEffect(handler, handlerFun) {{ + let res, tmp; /** scoped **/ + tmp = new Runtime.ContTrace.class(null, null, null, null, false); + res = new Runtime.EffectSig.class(tmp, handler, handlerFun); + res.contTrace.last = res.contTrace; + res.contTrace.lastHandler = res.contTrace; + return res } } - static mkEffect(handler, handlerFun) { - let res, tmp; /** scoped **/ - tmp = new Runtime.ContTrace.class(null, null, null, null, false); - res = new Runtime.EffectSig.class(tmp, handler, handlerFun); - res.contTrace.last = res.contTrace; - res.contTrace.lastHandler = res.contTrace; - return res - } - static handleBlockImpl(cur, handler) { - let handlerFrame; /** scoped **/ - handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); - cur.contTrace.lastHandler.nextHandler = handlerFrame; - cur.contTrace.lastHandler = handlerFrame; - cur.contTrace.last = handlerFrame; - return Runtime.handleEffects(cur) - } - static enterHandleBlock(handler, body) { - let cur; /** scoped **/ - cur = runtime.safeCall(body()); - if (cur instanceof Runtime.EffectSig.class) { - return Runtime.handleBlockImpl(cur, handler) - } else { - return cur + static handleBlockImpl(cur, handler) {{ + let handlerFrame; /** scoped **/ + handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); + cur.contTrace.lastHandler.nextHandler = handlerFrame; + cur.contTrace.lastHandler = handlerFrame; + cur.contTrace.last = handlerFrame; + return Runtime.handleEffects(cur) } } - static handleEffects(cur) { - let tmp; /** scoped **/ - tmp1: while (true) { - let nxt, tmp2; /** scoped **/ + static enterHandleBlock(handler, body) {{ + let cur; /** scoped **/ + cur = runtime.safeCall(body()); if (cur instanceof Runtime.EffectSig.class) { - let scrut; /** scoped **/ - nxt = Runtime.handleEffect(cur); - scrut = cur === nxt; - if (scrut === true) { - return cur - } else { - cur = nxt; - tmp2 = runtime.Unit; - } - tmp = tmp2; - continue tmp1 + return Runtime.handleBlockImpl(cur, handler) } else { return cur } - break; } - return tmp } - static handleEffect(cur) { - let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ - prevHandlerFrame = cur.contTrace; - tmp6: while (true) { - let scrut1, scrut2; /** scoped **/ - split_root$: { - split_1$: { - scrut1 = prevHandlerFrame.nextHandler !== null; - if (scrut1 === true) { - scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; - if (scrut2 === true) { - prevHandlerFrame = prevHandlerFrame.nextHandler; - tmp = runtime.Unit; - continue tmp6 - } else { - break split_1$ + static handleEffects(cur) {{ + let tmp; /** scoped **/ + tmp1: while (true) {{ + let nxt, tmp2; /** scoped **/ + if (cur instanceof Runtime.EffectSig.class) {{ + let scrut; /** scoped **/ + nxt = Runtime.handleEffect(cur); + scrut = cur === nxt; + if (scrut === true) { + return cur + } else { + cur = nxt; + tmp2 = runtime.Unit; + } + tmp = tmp2; + continue tmp1 } } else { - break split_1$ + return cur } } - tmp = runtime.Unit; - } - break; - } - scrut = prevHandlerFrame.nextHandler === null; - if (scrut === true) { - return cur - } else { - tmp1 = runtime.Unit; - } - handlerFrame = prevHandlerFrame.nextHandler; - saved = new Runtime.ContTrace.class(handlerFrame.next, cur.contTrace.last, handlerFrame.nextHandler, cur.contTrace.lastHandler, false); - cur.contTrace.last = handlerFrame; - cur.contTrace.lastHandler = handlerFrame; - handlerFrame.next = null; - handlerFrame.nextHandler = null; - tmp2 = Runtime.resume(cur.contTrace); - tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); - cur = tmp3; - if (cur instanceof Runtime.EffectSig.class) { - let scrut3, scrut4; /** scoped **/ - scrut3 = saved.next !== null; - if (scrut3 === true) { - cur.contTrace.last.next = saved.next; - cur.contTrace.last = saved.last; - tmp4 = runtime.Unit; - } else { - tmp4 = runtime.Unit; - } - scrut4 = saved.nextHandler !== null; - if (scrut4 === true) { - cur.contTrace.lastHandler.nextHandler = saved.nextHandler; - cur.contTrace.lastHandler = saved.lastHandler; - tmp5 = runtime.Unit; - } else { - tmp5 = runtime.Unit; + break; } - return cur - } else { - return Runtime.resumeContTrace(saved, cur) + return tmp } } - static resume(contTrace) { - return (value) => { - let scrut, tmp, tmp1; /** scoped **/ - scrut = contTrace.resumed; + static handleEffect(cur) {{ + let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + prevHandlerFrame = cur.contTrace; + tmp6: while (true) {{ + let scrut1, scrut2; /** scoped **/ + split_root$: { + split_1$: { + scrut1 = prevHandlerFrame.nextHandler !== null; + if (scrut1 === true) { + scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; + if (scrut2 === true) { + prevHandlerFrame = prevHandlerFrame.nextHandler; + tmp = runtime.Unit; + continue tmp6 + } else { + break split_1$ + } + } else { + break split_1$ + } + } + tmp = runtime.Unit; + } + } + break; + } + scrut = prevHandlerFrame.nextHandler === null; if (scrut === true) { - throw globalThis.Error("Multiple resumption") + return cur } else { - tmp = runtime.Unit; + tmp1 = runtime.Unit; } - contTrace.resumed = true; - tmp1 = Runtime.resumeContTrace(contTrace, value); - return Runtime.handleEffects(tmp1) - } - } - static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, tmp; /** scoped **/ - cont = contTrace.next; - handlerCont = contTrace.nextHandler; - curDepth = Runtime.stackDepth; - tmp1: while (true) { - let tmp2, tmp3; /** scoped **/ - if (cont instanceof Runtime.FunctionContFrame.class) { - let tmp4, tmp5; /** scoped **/ - tmp2 = runtime.safeCall(cont.resume(value)); - value = tmp2; - Runtime.stackDepth = curDepth; - if (value instanceof Runtime.EffectSig.class) { - let scrut, scrut1; /** scoped **/ - value.contTrace.last.next = cont.next; - value.contTrace.lastHandler.nextHandler = handlerCont; - scrut = contTrace.last !== cont; - if (scrut === true) { - value.contTrace.last = contTrace.last; + handlerFrame = prevHandlerFrame.nextHandler; + saved = new Runtime.ContTrace.class(handlerFrame.next, cur.contTrace.last, handlerFrame.nextHandler, cur.contTrace.lastHandler, false); + cur.contTrace.last = handlerFrame; + cur.contTrace.lastHandler = handlerFrame; + handlerFrame.next = null; + handlerFrame.nextHandler = null; + tmp2 = Runtime.resume(cur.contTrace); + tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); + cur = tmp3; + if (cur instanceof Runtime.EffectSig.class) {{ + let scrut1, scrut2; /** scoped **/ + scrut1 = saved.next !== null; + if (scrut1 === true) { + cur.contTrace.last.next = saved.next; + cur.contTrace.last = saved.last; tmp4 = runtime.Unit; } else { tmp4 = runtime.Unit; } - scrut1 = handlerCont !== null; - if (scrut1 === true) { - value.contTrace.lastHandler = contTrace.lastHandler; + scrut2 = saved.nextHandler !== null; + if (scrut2 === true) { + cur.contTrace.lastHandler.nextHandler = saved.nextHandler; + cur.contTrace.lastHandler = saved.lastHandler; tmp5 = runtime.Unit; } else { tmp5 = runtime.Unit; } - return value - } else { - cont = cont.next; - tmp3 = runtime.Unit; + return cur } - tmp = tmp3; - continue tmp1 } else { - if (handlerCont instanceof Runtime.HandlerContFrame.class) { - cont = handlerCont.next; - handlerCont = handlerCont.nextHandler; - tmp = runtime.Unit; - continue tmp1 + return Runtime.resumeContTrace(saved, cur) + } + } + } + static resume(contTrace) { + return (value) => {{ + let scrut, tmp, tmp1; /** scoped **/ + scrut = contTrace.resumed; + if (scrut === true) { + throw globalThis.Error("Multiple resumption") } else { - return value + tmp = runtime.Unit; } + contTrace.resumed = true; + tmp1 = Runtime.resumeContTrace(contTrace, value); + return Runtime.handleEffects(tmp1) } - break; } - return tmp } - static checkDepth() { - let scrut, tmp, lambda; /** scoped **/ - tmp = Runtime.stackDepth >= Runtime.stackLimit; - lambda = (undefined, function () { - return Runtime.stackHandler !== null - }); - scrut = runtime.short_and(tmp, lambda); - if (scrut === true) { - return runtime.safeCall(Runtime.stackHandler.delay()) - } else { - return runtime.Unit + static resumeContTrace(contTrace, value) {{ + let cont, handlerCont, curDepth, tmp; /** scoped **/ + cont = contTrace.next; + handlerCont = contTrace.nextHandler; + curDepth = Runtime.stackDepth; + tmp1: while (true) {{ + let tmp2, tmp3; /** scoped **/ + if (cont instanceof Runtime.FunctionContFrame.class) {{ + let tmp4, tmp5; /** scoped **/ + tmp2 = runtime.safeCall(cont.resume(value)); + value = tmp2; + Runtime.stackDepth = curDepth; + if (value instanceof Runtime.EffectSig.class) {{ + let scrut, scrut1; /** scoped **/ + value.contTrace.last.next = cont.next; + value.contTrace.lastHandler.nextHandler = handlerCont; + scrut = contTrace.last !== cont; + if (scrut === true) { + value.contTrace.last = contTrace.last; + tmp4 = runtime.Unit; + } else { + tmp4 = runtime.Unit; + } + scrut1 = handlerCont !== null; + if (scrut1 === true) { + value.contTrace.lastHandler = contTrace.lastHandler; + tmp5 = runtime.Unit; + } else { + tmp5 = runtime.Unit; + } + return value + } + } else { + cont = cont.next; + tmp3 = runtime.Unit; + } + tmp = tmp3; + continue tmp1 + } + } else { + if (handlerCont instanceof Runtime.HandlerContFrame.class) { + cont = handlerCont.next; + handlerCont = handlerCont.nextHandler; + tmp = runtime.Unit; + continue tmp1 + } else { + return value + } + } + } + break; + } + return tmp } } - static runStackSafe(limit, f) { - let result, tmp; /** scoped **/ - Runtime.stackLimit = limit; - Runtime.stackDepth = 1; - Runtime.stackHandler = Runtime.StackDelayHandler; - result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); - Runtime.stackDepth = 1; - tmp1: while (true) { - let scrut, saved, tmp2; /** scoped **/ - scrut = Runtime.stackResume !== null; + static checkDepth() {{ + let scrut, tmp, lambda; /** scoped **/ + tmp = Runtime.stackDepth >= Runtime.stackLimit; + lambda = (undefined, function () { + return Runtime.stackHandler !== null + }); + scrut = runtime.short_and(tmp, lambda); if (scrut === true) { - saved = Runtime.stackResume; - Runtime.stackResume = null; - tmp2 = runtime.safeCall(saved()); - result = tmp2; - Runtime.stackDepth = 1; - tmp = runtime.Unit; - continue tmp1 + return runtime.safeCall(Runtime.stackHandler.delay()) } else { - tmp = runtime.Unit; + return runtime.Unit } - break; } - Runtime.stackLimit = 0; - Runtime.stackDepth = 0; - Runtime.stackHandler = null; - return result } - static plus_impl(lhs, rhs) { - let tmp; /** scoped **/ - split_root$: { - split_1$: { - if (lhs instanceof Runtime.Int31.class) { - if (rhs instanceof Runtime.Int31.class) { - tmp = lhs + rhs; - break split_root$ + static runStackSafe(limit, f) {{ + let result, tmp; /** scoped **/ + Runtime.stackLimit = limit; + Runtime.stackDepth = 1; + Runtime.stackHandler = Runtime.StackDelayHandler; + result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); + Runtime.stackDepth = 1; + tmp1: while (true) {{ + let scrut, saved, tmp2; /** scoped **/ + scrut = Runtime.stackResume !== null; + if (scrut === true) { + saved = Runtime.stackResume; + Runtime.stackResume = null; + tmp2 = runtime.safeCall(saved()); + result = tmp2; + Runtime.stackDepth = 1; + tmp = runtime.Unit; + continue tmp1 + } else { + tmp = runtime.Unit; + } + } + break; + } + Runtime.stackLimit = 0; + Runtime.stackDepth = 0; + Runtime.stackHandler = null; + return result + } + } + static plus_impl(lhs, rhs) {{ + let tmp; /** scoped **/ + split_root$: { + split_1$: { + if (lhs instanceof Runtime.Int31.class) { + if (rhs instanceof Runtime.Int31.class) { + tmp = lhs + rhs; + break split_root$ + } else { + break split_1$ + } } else { break split_1$ } - } else { - break split_1$ } + tmp = Runtime.unreachable(); } - tmp = Runtime.unreachable(); + return tmp } - return tmp } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Runtime"]; diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index 07725a0283..cc5d9072a5 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,7 +81,7 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 }; +//│ let f5; f5 = function f() {{ let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 } }; :fixme :expect 42 @@ -92,7 +92,7 @@ f :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; /** scoped **/ f6 = function f() { return TrmMod.class }; +//│ let f6; f6 = function f() { return TrmMod.class }; fun assertModule(module m: ClsMod): module ClsMod = m diff --git a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls index 18ba41070d..630a7d1107 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls @@ -31,7 +31,7 @@ fun foo = :sjs foo //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = foo2(); tmp2 +//│ let tmp2; tmp2 = foo2(); tmp2 //│ > 1 //│ > ... diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 9ac7a2e8e9..e6e31984ba 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -64,7 +64,7 @@ Infinity :sjs val Infinity = 1 //│ JS (unsanitized): -//│ let Infinity; /** scoped **/ Infinity = 1; +//│ let Infinity; Infinity = 1; //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Identifier 'Infinity' has already been declared //│ Infinity = Infinity @@ -105,7 +105,7 @@ fun main() = object Cls(val x) with fun huh = x //│ JS (unsanitized): -//│ let Cls1; /** scoped **/ +//│ let Cls1; //│ globalThis.Object.freeze(class Cls { //│ static { //│ Cls1.class = globalThis.Object.freeze(new this) @@ -202,7 +202,7 @@ class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let D1; /** scoped **/ +//│ let D1; //│ globalThis.Object.freeze(class D extends Predef.id { //│ static { //│ D1 = this @@ -251,11 +251,11 @@ set (x += 1; ()) fun baz = 1 fun bar() = baz //│ JS (unsanitized): -//│ let bar, baz; /** scoped **/ +//│ let bar, baz; //│ baz = function baz() { //│ return 1 //│ }; -//│ bar = function bar() { let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 }; +//│ bar = function bar() {{ let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 } }; // ——— ——— ——— @@ -354,7 +354,7 @@ fun w(txt) = fs.writeFileSync(outFilePath, txt) // () //│ JS (unsanitized): -//│ let w; /** scoped **/ w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; +//│ let w; w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; w("whoops") //│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value. @@ -382,7 +382,7 @@ module Foo(x) //│ ║ l.379: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -406,7 +406,7 @@ module Foo(val x) //│ ║ l.403: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ globalThis.Object.freeze(class Foo6 { //│ static { //│ Foo7 = this diff --git a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls index 8c99b7c926..037ca2f59d 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls @@ -15,7 +15,7 @@ x :sjs val ++ = 0 //│ JS (unsanitized): -//│ let $_$_; /** scoped **/ $_$_ = 0; +//│ let $_$_; $_$_ = 0; //│ ++ = 0 :sjs @@ -51,7 +51,7 @@ fun ++ z = 0 :sjs ++ //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = $_$_2(); tmp +//│ let tmp; tmp = $_$_2(); tmp //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 1dc6b6f2b7..ced354b8b2 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1, tmp1, tmp2; /** scoped **/ +//│ let Foo1, tmp1; //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { @@ -107,6 +107,7 @@ module Foo with //│ } //│ constructor() {} //│ static { +//│ let tmp2; //│ tmp2 = Foo1(); //│ this.res = tmp2.foo; //│ } diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index df691692ad..d0425820bb 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index ce93f69fb6..9d957653bc 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; /** scoped **/ tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; /** scoped **/ +//│ let a, b, tmp2, tmp3; //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a @@ -22,10 +22,7 @@ drop { a: 0, b: 1 } :sjs let discard = drop _ //│ JS (unsanitized): -//│ let discard; -//│ let discard1; /** scoped **/ -//│ discard = function discard(_0) { return runtime.Unit }; -//│ discard1 = discard; +//│ let discard, discard1; discard1 = function discard(_0) { return runtime.Unit }; discard = discard1; //│ discard = fun discard discard of { a: 0, b: 1 } diff --git a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls index 708cfa4897..d91b3fba16 100644 --- a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls +++ b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls @@ -6,7 +6,7 @@ { a: 1 } //│ JS (unsanitized): -//│ let a; /** scoped **/ a = 1; globalThis.Object.freeze({ a: a }) +//│ let a; a = 1; globalThis.Object.freeze({ a: a }) //│ = {a: 1} { "a": 1 } @@ -35,7 +35,7 @@ :sjs { (id(0)): 1 } //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) +//│ let tmp; tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) //│ = {"0": 1} diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index 7f9f887e0c..e50e75b2e8 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,7 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let Baz1, tmp; /** scoped **/ +//│ let Baz1; //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; @@ -60,6 +60,7 @@ data class Baz(z) extends Bar(z * 1) with //│ Baz1.class = this //│ } //│ constructor(z) { +//│ let tmp; //│ tmp = z * 1; //│ super(tmp); //│ this.z = z; @@ -99,8 +100,8 @@ data class Barr(x: Int)(y: Int) with data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with fun g = z //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.99: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with -//│ ╙── ^^^^^^^^^^^^^^^^^^^ +//│ ║ l.100: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with +//│ ╙── ^^^^^^^^^^^^^^^^^^^ :todo Bazz(42).f @@ -110,7 +111,7 @@ Bazz(42).f new Barr(40)(2) with fun f = 43 //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.110: new Barr(40)(2) with +//│ ║ l.111: new Barr(40)(2) with //│ ╙── ^^^^^^^^^^^ //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index dd9ce4d58f..aa50ea39b9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -17,19 +17,20 @@ fun buildPalindrome = case 0 then [] n then [n, ..buildPalindrome(n - 1), n] //│ JS (unsanitized): -//│ let buildPalindrome; /** scoped **/ +//│ let buildPalindrome; //│ buildPalindrome = function buildPalindrome() { //│ let lambda; -//│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; /** scoped **/ -//│ if (caseScrut === 0) { -//│ return globalThis.Object.freeze([]) -//│ } else { -//│ n = caseScrut; -//│ tmp3 = buildPalindrome(); -//│ tmp4 = n - 1; -//│ tmp5 = tmp3(tmp4); -//│ return globalThis.Object.freeze(runtime.Tuple.lazyConcat(n, runtime.Tuple.split, tmp5, n)) +//│ lambda = (undefined, function (caseScrut) {{ +//│ let n, tmp3, tmp4, tmp5; /** scoped **/ +//│ if (caseScrut === 0) { +//│ return globalThis.Object.freeze([]) +//│ } else { +//│ n = caseScrut; +//│ tmp3 = buildPalindrome(); +//│ tmp4 = n - 1; +//│ tmp5 = tmp3(tmp4); +//│ return globalThis.Object.freeze(runtime.Tuple.lazyConcat(n, runtime.Tuple.split, tmp5, n)) +//│ } //│ } //│ }); //│ return lambda @@ -48,26 +49,27 @@ fun sum2 = case [] then 0 [x, ..xs, y] then x + y + sum2(xs) //│ JS (unsanitized): -//│ let sum2; /** scoped **/ +//│ let sum2; //│ sum2 = function sum2() { //│ let lambda; -//│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { -//│ return 0 -//│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { -//│ element0$ = runtime.Tuple.get(caseScrut, 0); -//│ middleElements3 = runtime.safeCall(runtime.Tuple.slice(caseScrut, 1, 1)); -//│ lastElement0$3 = runtime.Tuple.get(caseScrut, -1); -//│ y = lastElement0$3; -//│ xs = middleElements3; -//│ x = element0$; -//│ tmp5 = x + y; -//│ tmp6 = sum2(); -//│ tmp7 = tmp6(xs); -//│ return tmp5 + tmp7 -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ lambda = (undefined, function (caseScrut) {{ +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { +//│ return 0 +//│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { +//│ element0$ = runtime.Tuple.get(caseScrut, 0); +//│ middleElements3 = runtime.safeCall(runtime.Tuple.slice(caseScrut, 1, 1)); +//│ lastElement0$3 = runtime.Tuple.get(caseScrut, -1); +//│ y = lastElement0$3; +//│ xs = middleElements3; +//│ x = element0$; +//│ tmp5 = x + y; +//│ tmp6 = sum2(); +//│ tmp7 = tmp6(xs); +//│ return tmp5 + tmp7 +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } //│ } //│ }); //│ return lambda @@ -80,13 +82,13 @@ sum2(arr) :e fun f(..xs) = xs //│ ╔══[ERROR] Lazy spread parameters not allowed. -//│ ║ l.81: fun f(..xs) = xs +//│ ║ l.83: fun f(..xs) = xs //│ ╙── ^^^^ :ge sum2(..arr) //│ ╔══[COMPILATION ERROR] Lazy spreads are not supported in call arguments -//│ ║ l.87: sum2(..arr) +//│ ║ l.89: sum2(..arr) //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function expected 1 argument but got 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls index 1464825716..c22fcf695c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls @@ -46,7 +46,7 @@ M.Foo:: n(foo, 2) :sjs let m = M.Foo::m //│ JS (unsanitized): -//│ let m; let m1; /** scoped **/ m = function m(self, ...args) { return self.m(...args) }; m1 = m; +//│ let m, m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; //│ m = fun m m(foo) @@ -132,7 +132,7 @@ Foo::n(foo, 2) //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ═══[ERROR] Expected a statically known class; found ‹error›. //│ JS (unsanitized): -//│ let lambda9; /** scoped **/ +//│ let lambda9; //│ lambda9 = (undefined, function (self, ...args) { //│ return runtime.safeCall(self.n(...args)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls index 4cffa34572..2c3bbe2920 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls @@ -54,7 +54,7 @@ xs \ //│ ║ l.52: map(x => x * 2) //│ ╙── ^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let lambda5, tmp4; /** scoped **/ +//│ let lambda5, tmp4; //│ lambda5 = (undefined, function (x) { return x * 2 }); //│ tmp4 = map(lambda5); //│ Predef.passTo(xs1, tmp4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index dae4535fa9..69ed3c99b7 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -77,14 +77,14 @@ Foo(1, 2) :sjs let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): -//│ let f; -//│ let f1; /** scoped **/ -//│ f = function f(x, y) { -//│ let tmp4; /** scoped **/ -//│ tmp4 = Foo7(x, y); -//│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) +//│ let f, f1; +//│ f1 = function f(x, y) {{ +//│ let tmp4; /** scoped **/ +//│ tmp4 = Foo7(x, y); +//│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) +//│ } //│ }; -//│ f1 = f; +//│ f = f1; //│ f = fun f // Eta-expansion happens here. See EtaExpansion.mls. diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 9c2202fc04..01bf238555 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -6,7 +6,7 @@ fun f(n1: Int): Int = n1 //│ JS (unsanitized): -//│ let f; /** scoped **/ f = function f(n1) { return n1 }; +//│ let f; f = function f(n1) { return n1 }; f(42) //│ JS (unsanitized): @@ -19,54 +19,55 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f(n1) { return (n2) => { let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } }; +//│ let f1; +//│ f1 = function f(n1) { +//│ return (n2) => {{ let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } } +//│ }; // TODO compile this to // this.f$(4, 2) f(4)(2) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = f1(4); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = f1(4); runtime.safeCall(tmp(2)) //│ = 42 fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ f2 = function f(n1) { //│ return (n2) => { -//│ return (n3) => { -//│ let tmp1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = 10 * n1; -//│ tmp2 = tmp1 + n2; -//│ tmp3 = 10 * tmp2; -//│ return tmp3 + n3 +//│ return (n3) => {{ +//│ let tmp1, tmp2, tmp3; /** scoped **/ +//│ tmp1 = 10 * n1; +//│ tmp2 = tmp1 + n2; +//│ tmp3 = 10 * tmp2; +//│ return tmp3 + n3 +//│ } //│ } //│ } //│ }; f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ -//│ tmp1 = f2(4); -//│ tmp2 = runtime.safeCall(tmp1(2)); -//│ runtime.safeCall(tmp2(0)) +//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ -//│ tmp3 = 10 * n1; -//│ tmp4 = tmp3 + n2; -//│ tmp5 = 10 * tmp4; -//│ tmp6 = tmp5 + n3; -//│ tmp7 = 10 * tmp6; -//│ return tmp7 + n4 +//│ return (n4) => {{ +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ +//│ tmp3 = 10 * n1; +//│ tmp4 = tmp3 + n2; +//│ tmp5 = 10 * tmp4; +//│ tmp6 = tmp5 + n3; +//│ tmp7 = 10 * tmp6; +//│ return tmp7 + n4 +//│ } //│ } //│ } //│ } @@ -74,7 +75,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; /** scoped **/ +//│ let tmp3, tmp4, tmp5; //│ tmp3 = f3(3); //│ tmp4 = runtime.safeCall(tmp3(0)); //│ tmp5 = runtime.safeCall(tmp4(3)); diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index aa73bdb0d9..5025320ce4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,7 +80,7 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; /** scoped **/ +//│ let scrut, tmp17; //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls index d95bc500e7..58a398ea87 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls @@ -4,7 +4,7 @@ :sjs let t = [0, 1, 2] //│ JS (unsanitized): -//│ let t; /** scoped **/ t = globalThis.Object.freeze([ 0, 1, 2 ]); +//│ let t; t = globalThis.Object.freeze([ 0, 1, 2 ]); //│ t = [0, 1, 2] :re @@ -21,7 +21,7 @@ t :sjs let t = mut [0, 1, 2] //│ JS (unsanitized): -//│ let t1; /** scoped **/ t1 = [ 0, 1, 2 ]; +//│ let t1; t1 = [ 0, 1, 2 ]; //│ t = [0, 1, 2] t.push(4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls index 7efae81a91..0c8a48f3a4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls @@ -46,7 +46,7 @@ r.foo :sjs let r = {} //│ JS (unsanitized): -//│ let r3; /** scoped **/ r3 = runtime.Unit; +//│ let r3; r3 = runtime.Unit; //│ r = () :re diff --git a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls index 655c4d0a45..408d7db105 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls @@ -33,7 +33,7 @@ O.gy :sjs class Foo(x, val y, mut val z) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x, y, z) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index 790005a3c5..660664e019 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo()); +//│ let f; f = globalThis.Object.freeze(new Foo()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = new Foo(); +//│ let f1; f1 = new Foo(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index e1e6e3a5f0..cae9d03b91 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -109,19 +109,18 @@ passTo(1, add(., 1) @ _ + _)(2) :sjs let f = add(_, 1) //│ JS (unsanitized): -//│ let f; -//│ let f1; /** scoped **/ -//│ f = function f(_0) { let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) }; -//│ f1 = f; +//│ let f, f1; +//│ f1 = function f(_0) {{ let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) } }; +//│ f = f1; //│ f = fun f :fixme let f = add(., 1) //│ ╔══[PARSE ERROR] Expected an expression; found period instead -//│ ║ l.119: let f = add(., 1) +//│ ║ l.118: let f = add(., 1) //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected period here -//│ ║ l.119: let f = add(., 1) +//│ ║ l.118: let f = add(., 1) //│ ╙── ^ //│ ═══[RUNTIME ERROR] Error: Function expected 2 arguments but got 0 //│ f = undefined diff --git a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls index c9d079ed1c..36ad46feac 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls @@ -56,7 +56,7 @@ fun foo() = :sjs 1; id(2) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.id(2); (1 , tmp) +//│ let tmp; tmp = Predef.id(2); (1 , tmp) //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index 02207fb129..d1e18036da 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -4,9 +4,7 @@ :sjs 1 && 2 //│ JS (unsanitized): -//│ let lambda; /** scoped **/ -//│ lambda = (undefined, function () { return 2 }); -//│ runtime.short_and(1, lambda) +//│ let lambda; lambda = (undefined, function () { return 2 }); runtime.short_and(1, lambda) //│ = 2 1 || 2 @@ -20,9 +18,7 @@ fun test(x) = :sjs 123 || test(42) //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ -//│ lambda2 = (undefined, function () { return test(42) }); -//│ runtime.short_or(123, lambda2) +//│ let lambda2; lambda2 = (undefined, function () { return test(42) }); runtime.short_or(123, lambda2) //│ = 123 0 || test(42) @@ -36,13 +32,14 @@ fun test(x) = :sjs fold(||)(0, false, 42, 123) //│ JS (unsanitized): -//│ let lambda5, tmp; /** scoped **/ -//│ lambda5 = (undefined, function (arg1, arg2) { -//│ let lambda6; /** scoped **/ -//│ lambda6 = (undefined, function () { -//│ return arg2 -//│ }); -//│ return runtime.short_or(arg1, lambda6) +//│ let lambda5, tmp; +//│ lambda5 = (undefined, function (arg1, arg2) {{ +//│ let lambda6; /** scoped **/ +//│ lambda6 = (undefined, function () { +//│ return arg2 +//│ }); +//│ return runtime.short_or(arg1, lambda6) +//│ } //│ }); //│ tmp = runtime.safeCall(Predef.fold(lambda5)); //│ runtime.safeCall(tmp(0, false, 42, 123)) diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index b9d1163e58..666d7e6f4c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } +//│ let scrut; scrut = "a"; if (typeof scrut === 'string') { true } else { false } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index dcb0d0ba4b..1d443c419c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -61,10 +61,9 @@ print(_) :sjs let test = _.f(0, _, 2) //│ JS (unsanitized): -//│ let test; -//│ let test1; /** scoped **/ -//│ test = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; -//│ test1 = test; +//│ let test, test1; +//│ test1 = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; +//│ test = test1; //│ test = fun test :re @@ -136,7 +135,7 @@ mkObj(1, 2) // :re let mkObj = x: _, y: _, z: 3 //│ ╔══[ERROR] Illegal position for '_' placeholder. -//│ ║ l.137: let mkObj = x: _, y: _, z: 3 +//│ ║ l.136: let mkObj = x: _, y: _, z: 3 //│ ╙── ^ //│ mkObj = fun mkObj //│ y = undefined diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 01726b0d7d..3bcd4220f5 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -31,7 +31,7 @@ :sjs let x = 1 in x + 1 //│ JS (unsanitized): -//│ let x; /** scoped **/ x = 1; x + 1 +//│ let x; x = 1; x + 1 //│ = 2 //│ Type: Int @@ -61,7 +61,7 @@ false :sjs data class Foo(x: Int) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x1) { //│ return globalThis.Object.freeze(new Foo.class(x1)); //│ }; @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo.class(42)); foo.x +//│ let foo; foo = globalThis.Object.freeze(new Foo.class(42)); foo.x //│ = 42 //│ Type: Int @@ -97,7 +97,7 @@ let foo = new Foo(42) in foo.Foo#x :sjs fun inc(x) = x + 1 //│ JS (unsanitized): -//│ let inc; /** scoped **/ inc = function inc(x1) { return x1 + 1 }; +//│ let inc; inc = function inc(x1) { return x1 + 1 }; //│ Type: ⊤ @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } +//│ let scrut; scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } +//│ let scrut1; scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } //│ = 1 //│ Type: Int @@ -134,7 +134,7 @@ data class Foo() let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ foo1 = globalThis.Object.freeze(new Foo2.class()); //│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } //│ = 1 @@ -147,19 +147,20 @@ fun pow(x) = case 0 then 1 n then x * pow(x)(n-1) //│ JS (unsanitized): -//│ let pow; /** scoped **/ +//│ let pow; //│ pow = function pow(x1) { //│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; /** scoped **/ -//│ if (caseScrut === 0) { -//│ return 1 -//│ } else { -//│ n = caseScrut; -//│ tmp = pow(x1); -//│ tmp1 = n - 1; -//│ tmp2 = runtime.safeCall(tmp(tmp1)); -//│ return x1 * tmp2 +//│ lambda1 = (undefined, function (caseScrut) {{ +//│ let n, tmp, tmp1, tmp2; /** scoped **/ +//│ if (caseScrut === 0) { +//│ return 1 +//│ } else { +//│ n = caseScrut; +//│ tmp = pow(x1); +//│ tmp1 = n - 1; +//│ tmp2 = runtime.safeCall(tmp(tmp1)); +//│ return x1 * tmp2 +//│ } //│ } //│ }); //│ return lambda1 @@ -172,7 +173,7 @@ fun nott = case true then false false then true //│ JS (unsanitized): -//│ let nott; /** scoped **/ +//│ let nott; //│ nott = function nott() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { @@ -193,7 +194,7 @@ fun nott = case :sjs nott of false //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = nott(); tmp(false) +//│ let tmp; tmp = nott(); tmp(false) //│ = true //│ Type: Bool @@ -203,19 +204,20 @@ fun fact = case 0 then 1 n then n * fact(n - 1) //│ JS (unsanitized): -//│ let fact; /** scoped **/ +//│ let fact; //│ fact = function fact() { //│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; /** scoped **/ -//│ if (caseScrut === 0) { -//│ return 1 -//│ } else { -//│ n = caseScrut; -//│ tmp1 = fact(); -//│ tmp2 = n - 1; -//│ tmp3 = tmp1(tmp2); -//│ return n * tmp3 +//│ lambda1 = (undefined, function (caseScrut) {{ +//│ let n, tmp1, tmp2, tmp3; /** scoped **/ +//│ if (caseScrut === 0) { +//│ return 1 +//│ } else { +//│ n = caseScrut; +//│ tmp1 = fact(); +//│ tmp2 = n - 1; +//│ tmp3 = tmp1(tmp2); +//│ return n * tmp3 +//│ } //│ } //│ }); //│ return lambda1 @@ -258,8 +260,7 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let x4; -//│ let y; /** scoped **/ +//│ let x4, y; //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); //│ y.value @@ -270,8 +271,7 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let x5; -//│ let y1; /** scoped **/ +//│ let x5, y1; //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); //│ y1.value = 0; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 87c687f79b..44ca37d219 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -87,31 +87,34 @@ fun test2() = //│ ║ l.83: case 0 then 0 //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let test22; /** scoped **/ -//│ test22 = function test2() { -//│ let funny, tmp1; /** scoped **/ -//│ funny = function funny() { -//│ let lambda; -//│ let lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (caseScrut) { -//│ if (caseScrut === 0) { -//│ return 0 -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ let test22; +//│ test22 = function test2() {{ +//│ let funny, tmp1; /** scoped **/ +//│ funny = function funny() { +//│ let lambda;{ +//│ let lambda1; /** scoped **/ +//│ lambda1 = (undefined, function (caseScrut) { +//│ if (caseScrut === 0) { +//│ return 0 +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ }); +//│ lambda = (undefined, function (caseScrut) {{ +//│ let n, tmp2, tmp3, tmp4; /** scoped **/ +//│ n = caseScrut; +//│ tmp2 = funny(); +//│ tmp3 = n - 1; +//│ tmp4 = tmp2(tmp3); +//│ return tmp4 + 1 +//│ } +//│ }); +//│ return lambda //│ } -//│ }); -//│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp2, tmp3, tmp4; /** scoped **/ -//│ n = caseScrut; -//│ tmp2 = funny(); -//│ tmp3 = n - 1; -//│ tmp4 = tmp2(tmp3); -//│ return tmp4 + 1 -//│ }); -//│ return lambda -//│ }; -//│ tmp1 = funny(); -//│ return tmp1 +//│ }; +//│ tmp1 = funny(); +//│ return tmp1 +//│ } //│ }; //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.83: case 0 then 0 @@ -131,7 +134,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.132: print("Hi") +//│ ║ l.135: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index d7ff1e8b8d..9cb5c5210c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -11,7 +11,7 @@ empty.0 :sjs let single = [1] //│ JS (unsanitized): -//│ let single; /** scoped **/ single = globalThis.Object.freeze([ 1 ]); +//│ let single; single = globalThis.Object.freeze([ 1 ]); //│ single = [1] single.0 @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls index 0d443fdd05..ce774c920f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls @@ -9,7 +9,7 @@ object Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar1; /** scoped **/ +//│ let Bar1; //│ globalThis.Object.freeze(class Bar { //│ static { //│ Bar1 = globalThis.Object.freeze(new this) @@ -49,7 +49,7 @@ module Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar3; /** scoped **/ +//│ let Bar3; //│ globalThis.Object.freeze(class Bar2 { //│ static { //│ Bar3 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls index 454e29826f..ed25930a28 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls @@ -29,7 +29,7 @@ new 2 + 2 :re new! 2 + 2 //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 +//│ let tmp1; tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 //│ ═══[RUNTIME ERROR] TypeError: 2 is not a constructor :e diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index db1624542a..7bf650836b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -39,7 +39,7 @@ print("Hi") print("Hi") 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.print("Hi"); 2 +//│ let tmp; tmp = Predef.print("Hi"); 2 //│ Lowered: //│ Program: //│ imports = Nil diff --git a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls index 70dc34b630..51390245ed 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls @@ -74,7 +74,7 @@ id(+)(1, 2) :re id(+)(1) //│ JS (unsanitized): -//│ let lambda4, tmp1; /** scoped **/ +//│ let lambda4, tmp1; //│ lambda4 = (undefined, function (arg1, arg2) { //│ return arg1 + arg2 //│ }); @@ -95,7 +95,7 @@ fun (+) lol(a, b) = [a, b] :sjs id(~)(2) //│ JS (unsanitized): -//│ let lambda5, tmp2; /** scoped **/ +//│ let lambda5, tmp2; //│ lambda5 = (undefined, function (arg) { //│ return ~ arg //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 8be8e2459a..443e468c07 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -15,27 +15,28 @@ fun test(x) = Some(v) then print(v) None then print("none") //│ JS (unsanitized): -//│ let test; /** scoped **/ -//│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ -//│ if (x instanceof Some.class) { -//│ argument0$1 = x.value; -//│ v = argument0$1; -//│ tmp = v + 1; -//│ tmp1 = Some(tmp); -//│ } else if (x instanceof None.class) { -//│ tmp1 = None; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ let test; +//│ test = function test(x) {{ +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ +//│ if (x instanceof Some.class) { +//│ argument0$1 = x.value; +//│ v = argument0$1; +//│ tmp = v + 1; +//│ tmp1 = Some(tmp); +//│ } else if (x instanceof None.class) { +//│ tmp1 = None; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ scrut = tmp1; +//│ if (scrut instanceof Some.class) { +//│ argument0$ = scrut.value; +//│ v1 = argument0$; +//│ return Predef.print(v1) +//│ } else if (scrut instanceof None.class) { +//│ return Predef.print("none") +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ } -//│ scrut = tmp1; -//│ if (scrut instanceof Some.class) { -//│ argument0$ = scrut.value; -//│ v1 = argument0$; -//│ return Predef.print(v1) -//│ } else if (scrut instanceof None.class) { -//│ return Predef.print("none") -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 2c5e5cee00..993b31c6dd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -8,10 +8,11 @@ case x then x case { x then x } //│ JS (unsanitized): //│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) { -//│ let x; /** scoped **/ -//│ x = caseScrut; -//│ return x +//│ lambda1 = (undefined, function (caseScrut) {{ +//│ let x; /** scoped **/ +//│ x = caseScrut; +//│ return x +//│ } //│ }); //│ lambda1 //│ = fun @@ -59,10 +60,10 @@ case 0 then true :todo // TODO: support this braceless syntax? case [x] then x, [] then 0 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.60: case [x] then x, [] then 0 +//│ ║ l.61: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^ //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.60: case [x] then x, [] then 0 +//│ ║ l.61: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^^^^^^^ :sjs @@ -85,7 +86,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let isDefined, lambda12; /** scoped **/ +//│ let isDefined, lambda12; //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index 67640a68bf..ffdefba167 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,7 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let Outer1, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ let Outer1; //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -23,6 +23,7 @@ data class Outer(a, b) with //│ Outer1.class = this //│ } //│ constructor(a, b) { +//│ let tmp, tmp1, tmp2; //│ this.a = a; //│ this.b = b; //│ const this$Outer = this; @@ -34,11 +35,12 @@ data class Outer(a, b) with //│ this$Outer.Inner.class = this //│ } //│ constructor(c) { +//│ let tmp3, tmp4, tmp5; //│ this.c = c; -//│ tmp = Predef.print(this$Outer.a); -//│ tmp1 = Predef.print(this.c); -//│ tmp2 = this.i1(this$Outer.a); -//│ Predef.print(tmp2); +//│ tmp3 = Predef.print(this$Outer.a); +//│ tmp4 = Predef.print(this.c); +//│ tmp5 = this.i1(this$Outer.a); +//│ Predef.print(tmp5); //│ } //│ i1(d) { //│ return globalThis.Object.freeze([ @@ -50,19 +52,20 @@ data class Outer(a, b) with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Inner", ["c"]]; //│ }); -//│ tmp3 = this.Inner(this.a); -//│ this.i = tmp3; -//│ tmp4 = Predef.print(this.i.c); -//│ tmp5 = runtime.safeCall(this.i.i1(1)); -//│ Predef.print(tmp5); +//│ tmp = this.Inner(this.a); +//│ this.i = tmp; +//│ tmp1 = Predef.print(this.i.c); +//│ tmp2 = runtime.safeCall(this.i.i1(1)); +//│ Predef.print(tmp2); //│ } //│ o1(c) { //│ return this.Inner(c) //│ } -//│ o2(c, d) { -//│ let tmp6; /** scoped **/ -//│ tmp6 = this.Inner(c); -//│ return tmp6.i1(d) +//│ o2(c, d) {{ +//│ let tmp; /** scoped **/ +//│ tmp = this.Inner(c); +//│ return tmp.i1(d) +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Outer", ["a", "b"]]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index 696a1e22bd..cb9f81e5ce 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -6,20 +6,21 @@ fun test(a) = class C with { val x = a } new C //│ JS (unsanitized): -//│ let test; /** scoped **/ -//│ test = function test(a) { -//│ let C1; /** scoped **/ -//│ globalThis.Object.freeze(class C { -//│ static { -//│ C1 = this -//│ } -//│ constructor() { -//│ this.x = a; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "C"]; -//│ }); -//│ return globalThis.Object.freeze(new C1()) +//│ let test; +//│ test = function test(a) {{ +//│ let C1; /** scoped **/ +//│ globalThis.Object.freeze(class C { +//│ static { +//│ C1 = this +//│ } +//│ constructor() { +//│ this.x = a; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "C"]; +//│ }); +//│ return globalThis.Object.freeze(new C1()) +//│ } //│ }; test(12) @@ -37,25 +38,26 @@ fun test(x) = data class Foo(a, b) Foo(x, x + 1) //│ JS (unsanitized): -//│ let test2; /** scoped **/ -//│ test2 = function test(x) { -//│ let Foo2, tmp; /** scoped **/ -//│ Foo2 = function Foo(a, b) { -//│ return globalThis.Object.freeze(new Foo.class(a, b)); -//│ }; -//│ globalThis.Object.freeze(class Foo1 { -//│ static { -//│ Foo2.class = this -//│ } -//│ constructor(a, b) { -//│ this.a = a; -//│ this.b = b; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Foo", ["a", "b"]]; -//│ }); -//│ tmp = x + 1; -//│ return Foo2(x, tmp) +//│ let test2; +//│ test2 = function test(x) {{ +//│ let Foo2, tmp; /** scoped **/ +//│ Foo2 = function Foo(a, b) { +//│ return globalThis.Object.freeze(new Foo.class(a, b)); +//│ }; +//│ globalThis.Object.freeze(class Foo1 { +//│ static { +//│ Foo2.class = this +//│ } +//│ constructor(a, b) { +//│ this.a = a; +//│ this.b = b; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Foo", ["a", "b"]]; +//│ }); +//│ tmp = x + 1; +//│ return Foo2(x, tmp) +//│ } //│ }; @@ -68,7 +70,7 @@ test(123) :re test() //│ ╔══[ERROR] Expected 1 arguments, got 0 -//│ ║ l.69: test() +//│ ║ l.71: test() //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Function 'test' expected 1 argument but got 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index b4df883b66..ddc9b4cba6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,7 +9,7 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; /** scoped **/ +//│ let scrut, x, argument0$; //│ scrut = Some(0); //│ if (scrut instanceof Some.class) { //│ argument0$ = scrut.value; @@ -26,7 +26,7 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ let x1, argument0$1; /** scoped **/ +//│ let x1, argument0$1; //│ if (s instanceof Some.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; @@ -54,13 +54,14 @@ if s is x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; -//│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; /** scoped **/ -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ x4 = argument0$4; -//│ return x4 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ lambda = (undefined, function (x3) {{ +//│ let x4, argument0$4; /** scoped **/ +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ x4 = argument0$4; +//│ return x4 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ }); //│ lambda //│ = fun @@ -118,29 +119,30 @@ fun f(x) = if x is None then "ok" else print("oops") //│ JS (unsanitized): -//│ let f4; /** scoped **/ -//│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ -//│ split_root$1: { -//│ split_1$: { -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ x4 = argument0$4; -//│ scrut1 = x4 > 0; -//│ if (scrut1 === true) { -//│ tmp6 = 42; +//│ let f4; +//│ f4 = function f(x3) {{ +//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ +//│ split_root$1: { +//│ split_1$: { +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ x4 = argument0$4; +//│ scrut1 = x4 > 0; +//│ if (scrut1 === true) { +//│ tmp6 = 42; +//│ break split_root$1 +//│ } else { +//│ break split_1$ +//│ } +//│ } else if (x3 instanceof None.class) { +//│ tmp6 = "ok"; //│ break split_root$1 -//│ } else { -//│ break split_1$ -//│ } -//│ } else if (x3 instanceof None.class) { -//│ tmp6 = "ok"; -//│ break split_root$1 -//│ } else { break split_1$ } +//│ } else { break split_1$ } +//│ } +//│ tmp6 = Predef.print("oops"); //│ } -//│ tmp6 = Predef.print("oops"); +//│ return tmp6 //│ } -//│ return tmp6 //│ }; f(Some(0)) @@ -164,20 +166,21 @@ fun f(x) = if x is Some(u) then u Pair(a, b) then a + b //│ JS (unsanitized): -//│ let f5; /** scoped **/ -//│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; /** scoped **/ -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ u = argument0$4; -//│ return u -//│ } else if (x3 instanceof Pair.class) { -//│ argument0$4 = x3.fst; -//│ argument1$ = x3.snd; -//│ b = argument1$; -//│ a = argument0$4; -//│ return a + b -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ let f5; +//│ f5 = function f(x3) {{ +//│ let u, a, b, argument0$4, argument1$; /** scoped **/ +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ u = argument0$4; +//│ return u +//│ } else if (x3 instanceof Pair.class) { +//│ argument0$4 = x3.fst; +//│ argument1$ = x3.snd; +//│ b = argument1$; +//│ a = argument0$4; +//│ return a + b +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ }; f(Some(123)) @@ -194,27 +197,28 @@ fun f(x) = print of if x is None then "ok" else "oops" //│ JS (unsanitized): -//│ let f6; /** scoped **/ -//│ f6 = function f(x3) { -//│ let argument0$4, tmp10; /** scoped **/ -//│ split_root$1: { -//│ split_1$: { -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ if (argument0$4 === 0) { -//│ tmp10 = "0"; +//│ let f6; +//│ f6 = function f(x3) {{ +//│ let argument0$4, tmp10; /** scoped **/ +//│ split_root$1: { +//│ split_1$: { +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ if (argument0$4 === 0) { +//│ tmp10 = "0"; +//│ break split_root$1 +//│ } else { +//│ break split_1$ +//│ } +//│ } else if (x3 instanceof None.class) { +//│ tmp10 = "ok"; //│ break split_root$1 -//│ } else { -//│ break split_1$ -//│ } -//│ } else if (x3 instanceof None.class) { -//│ tmp10 = "ok"; -//│ break split_root$1 -//│ } else { break split_1$ } +//│ } else { break split_1$ } +//│ } +//│ tmp10 = "oops"; //│ } -//│ tmp10 = "oops"; +//│ return Predef.print(tmp10) //│ } -//│ return Predef.print(tmp10) //│ }; f(Some(0)) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls index fc79ff4035..1c596f3fc1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls @@ -6,7 +6,7 @@ data class Foo(arguments) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(arguments1) { //│ return globalThis.Object.freeze(new Foo.class(arguments1)); //│ }; @@ -24,7 +24,7 @@ data class Foo(arguments) data class Foo(eval) //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ Foo3 = function Foo(eval1) { //│ return globalThis.Object.freeze(new Foo.class(eval1)); //│ }; @@ -42,7 +42,7 @@ data class Foo(eval) data class Foo(static) //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ Foo5 = function Foo(static1) { //│ return globalThis.Object.freeze(new Foo.class(static1)); //│ }; @@ -60,7 +60,7 @@ data class Foo(static) data class Foo(v') //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ Foo7 = function Foo(v$_) { //│ return globalThis.Object.freeze(new Foo.class(v$_)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index af16010767..8d18e010f2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -7,27 +7,27 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ f = function f() { -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 +//│ let f; +//│ f = function f() {{ +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 +//│ } //│ }; fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f() { -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 +//│ let f1; +//│ f1 = function f() {{ +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 +//│ } //│ }; fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; -//│ 42 +//│ let f2; f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; 42 //│ = 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index 1770b21e64..be5f1aee43 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,14 +7,14 @@ :silent declare val console //│ JS (unsanitized): -//│ let console; /** scoped **/ +//│ console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ tmp = runtime.safeCall(globalThis.console.log("a")); //│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 @@ -25,7 +25,7 @@ console.log("b") let l = console.log l(123) //│ JS (unsanitized): -//│ let l; /** scoped **/ l = globalThis.console.log; runtime.safeCall(l(123)) +//│ let l; l = globalThis.console.log; runtime.safeCall(l(123)) //│ > 123 //│ l = fun log @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3, tmp4; /** scoped **/ +//│ let x, y, tmp2, tmp3, tmp4; //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index 421a23afac..a4fddd8d26 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; /** scoped **/ x = undefined; +//│ let x; x = undefined; //│ x = undefined x = 1 @@ -31,7 +31,7 @@ x let y = 1 //│ JS (unsanitized): -//│ let y; /** scoped **/ y = 1; +//│ let y; y = 1; //│ y = 1 :e @@ -45,7 +45,7 @@ z = 1 fun f() = 1 //│ JS (unsanitized): -//│ let f; /** scoped **/ f = function f() { return 1 }; +//│ let f; f = function f() { return 1 }; f //│ JS (unsanitized): @@ -56,7 +56,7 @@ f let f f(x) = x + 1 //│ JS (unsanitized): -//│ let f1; let f2; /** scoped **/ f1 = function f(x1) { return x1 + 1 }; f2 = f1; +//│ let f1, f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; //│ f = fun f f(1) @@ -68,7 +68,7 @@ f(1) let foo foo = 0 //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = 0; +//│ let foo; foo = 0; //│ foo = 0 :fixme @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; /** scoped **/ +//│ let foo1, scrut; //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; /** scoped **/ +//│ let foo2, scrut1; //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -105,7 +105,7 @@ else fun f() = foo = 42 //│ JS (unsanitized): -//│ let f3; /** scoped **/ f3 = function f() { foo2 = 42; return runtime.Unit }; +//│ let f3; f3 = function f() { foo2 = 42; return runtime.Unit }; f() //│ JS (unsanitized): @@ -123,6 +123,6 @@ fun f() = foo = 0 //│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let f4; /** scoped **/ f4 = function f() { return foo2 }; +//│ let f4; f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 1c439570a2..5c8b93ff4b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -18,15 +18,16 @@ fun f(x) = return 0 x + 1 //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f(x) { -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ scrut = x < 0; -//│ if (scrut === true) { -//│ tmp = Predef.print("whoops"); -//│ return 0 -//│ } else { tmp1 = runtime.Unit; } -//│ return x + 1 +//│ let f1; +//│ f1 = function f(x) {{ +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ scrut = x < 0; +//│ if (scrut === true) { +//│ tmp = Predef.print("whoops"); +//│ return 0 +//│ } else { tmp1 = runtime.Unit; } +//│ return x + 1 +//│ } //│ }; f(1) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index e353c2a33e..85ea77ef62 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -7,10 +7,7 @@ let discard = drop _ //│ JS (unsanitized): -//│ let discard; -//│ let discard1; /** scoped **/ -//│ discard = function discard(_0) { return runtime.Unit }; -//│ discard1 = discard; +//│ let discard, discard1; discard1 = function discard(_0) { return runtime.Unit }; discard = discard1; //│ discard = fun discard @@ -18,7 +15,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; /** scoped **/ +//│ let a, b, tmp; //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -26,7 +23,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; /** scoped **/ +//│ let a1, b1, tmp1; //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -35,14 +32,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; /** scoped **/ +//│ let aaaa, tmp2; //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let aaaaaaaaa, bbbb, tmp3; /** scoped **/ +//│ let aaaaaaaaa, bbbb, tmp3; //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -53,7 +50,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; /** scoped **/ +//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -69,7 +66,7 @@ discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ tmp5 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb" @@ -78,7 +75,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ] //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ tmp6 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", @@ -89,7 +86,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; /** scoped **/ +//│ let tmp7, tmp8; //│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 1078c18fd7..f56965a619 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -19,44 +19,46 @@ fun test(a) = h(d) Inner(42) //│ JS (unsanitized): -//│ let test1; /** scoped **/ -//│ test1 = function test(a) { -//│ let Inner1, tmp; /** scoped **/ -//│ Inner1 = function Inner(b) { -//│ return globalThis.Object.freeze(new Inner.class(b)); -//│ }; -//│ globalThis.Object.freeze(class Inner { -//│ static { -//│ Inner1.class = this -//│ } -//│ constructor(b) { -//│ this.b = b; -//│ tmp = Predef.print(a); -//│ } -//│ f(c) { -//│ return globalThis.Object.freeze([ -//│ a, -//│ this.b, -//│ c -//│ ]) -//│ } -//│ g(d) { -//│ let h; /** scoped **/ -//│ const this$Inner = this; -//│ h = function h(e) { +//│ let test1; +//│ test1 = function test(a) {{ +//│ let Inner1, tmp; /** scoped **/ +//│ Inner1 = function Inner(b) { +//│ return globalThis.Object.freeze(new Inner.class(b)); +//│ }; +//│ globalThis.Object.freeze(class Inner { +//│ static { +//│ Inner1.class = this +//│ } +//│ constructor(b) { +//│ this.b = b; +//│ tmp = Predef.print(a); +//│ } +//│ f(c) { //│ return globalThis.Object.freeze([ //│ a, -//│ this$Inner.b, -//│ d, -//│ e +//│ this.b, +//│ c //│ ]) -//│ }; -//│ return h(d) -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Inner", ["b"]]; -//│ }); -//│ return Inner1(42) +//│ } +//│ g(d) {{ +//│ let h; /** scoped **/ +//│ const this$Inner = this; +//│ h = function h(e) { +//│ return globalThis.Object.freeze([ +//│ a, +//│ this$Inner.b, +//│ d, +//│ e +//│ ]) +//│ }; +//│ return h(d) +//│ } +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Inner", ["b"]]; +//│ }); +//│ return Inner1(42) +//│ } //│ }; let i = test(100) @@ -78,48 +80,49 @@ fun test(a) = print of [a, b] [C1(1), C2(2)] //│ JS (unsanitized): -//│ let test2; /** scoped **/ -//│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ C11 = function C1(b) { -//│ return globalThis.Object.freeze(new C1.class(b)); -//│ }; -//│ globalThis.Object.freeze(class C1 { -//│ static { -//│ C11.class = this -//│ } -//│ constructor(b) { -//│ this.b = b; -//│ tmp = globalThis.Object.freeze([ -//│ a, -//│ this.b -//│ ]); -//│ Predef.print(tmp); -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "C1", ["b"]]; -//│ }); -//│ C21 = function C2(b) { -//│ return globalThis.Object.freeze(new C2.class(b)); -//│ }; -//│ globalThis.Object.freeze(class C2 { -//│ static { -//│ C21.class = this -//│ } -//│ constructor(b) { -//│ this.b = b; -//│ tmp1 = globalThis.Object.freeze([ -//│ a, -//│ this.b -//│ ]); -//│ Predef.print(tmp1); -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "C2", ["b"]]; -//│ }); -//│ tmp2 = C11(1); -//│ tmp3 = C21(2); -//│ return globalThis.Object.freeze([ tmp2, tmp3 ]) +//│ let test2; +//│ test2 = function test(a) {{ +//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ C11 = function C1(b) { +//│ return globalThis.Object.freeze(new C1.class(b)); +//│ }; +//│ globalThis.Object.freeze(class C1 { +//│ static { +//│ C11.class = this +//│ } +//│ constructor(b) { +//│ this.b = b; +//│ tmp = globalThis.Object.freeze([ +//│ a, +//│ this.b +//│ ]); +//│ Predef.print(tmp); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "C1", ["b"]]; +//│ }); +//│ C21 = function C2(b) { +//│ return globalThis.Object.freeze(new C2.class(b)); +//│ }; +//│ globalThis.Object.freeze(class C2 { +//│ static { +//│ C21.class = this +//│ } +//│ constructor(b) { +//│ this.b = b; +//│ tmp1 = globalThis.Object.freeze([ +//│ a, +//│ this.b +//│ ]); +//│ Predef.print(tmp1); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "C2", ["b"]]; +//│ }); +//│ tmp2 = C11(1); +//│ tmp3 = C21(2); +//│ return globalThis.Object.freeze([ tmp2, tmp3 ]) +//│ } //│ }; test(123) @@ -138,7 +141,7 @@ data class Foo(a) with foo() Foo(123) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -150,17 +153,18 @@ Foo(123) //│ this.a = a; //│ this.foo(); //│ } -//│ foo() { -//│ let bar, baz, tmp; /** scoped **/ -//│ const this$Foo = this; -//│ bar = function bar() { -//│ return this$Foo.a -//│ }; -//│ baz = function baz() { -//│ return this$Foo.a -//│ }; -//│ tmp = bar(); -//│ return baz() +//│ foo() {{ +//│ let bar, baz, tmp; /** scoped **/ +//│ const this$Foo = this; +//│ bar = function bar() { +//│ return this$Foo.a +//│ }; +//│ baz = function baz() { +//│ return this$Foo.a +//│ }; +//│ tmp = bar(); +//│ return baz() +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Foo", ["a"]]; @@ -177,7 +181,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1, tmp; /** scoped **/ +//│ let Bar1; //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -186,18 +190,20 @@ Bar(1) //│ Bar1.class = this //│ } //│ constructor(x) { +//│ let tmp; //│ this.x = x; //│ tmp = this.foo(); //│ runtime.safeCall(tmp()); //│ } //│ foo() { -//│ return () => { -//│ let bar; /** scoped **/ -//│ const this$Bar = this; -//│ bar = function bar() { -//│ return this$Bar.x -//│ }; -//│ return bar() +//│ return () => {{ +//│ let bar; /** scoped **/ +//│ const this$Bar = this; +//│ bar = function bar() { +//│ return this$Bar.x +//│ }; +//│ return bar() +//│ } //│ } //│ } //│ toString() { return runtime.render(this); } diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index e3460c4b18..540121fc08 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -6,19 +6,20 @@ val x = 2 fun foo() = x + 1 //│ JS (unsanitized): -//│ let foo, x; /** scoped **/ foo = function foo() { return x + 1 }; x = 2; +//│ let foo, x; foo = function foo() { return x + 1 }; x = 2; //│ x = 2 :sjs class Test with print(foo()) //│ JS (unsanitized): -//│ let Test1, tmp; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this //│ } //│ constructor() { +//│ let tmp; //│ tmp = foo(); //│ Predef.print(tmp); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 23e4c2f5d8..9439cac263 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -4,14 +4,14 @@ :sjs fun t = 42 //│ JS (unsanitized): -//│ let t; /** scoped **/ t = function t() { return 42 }; +//│ let t; t = function t() { return 42 }; :expect 42 :sjs t //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = t(); tmp +//│ let tmp; tmp = t(); tmp //│ = 42 @@ -37,17 +37,19 @@ fun test() = 42 whoops + whoops //│ JS (unsanitized): -//│ let test; /** scoped **/ -//│ test = function test() { -//│ let whoops, tmp1, tmp2; /** scoped **/ -//│ whoops = function whoops() { -//│ let tmp3; /** scoped **/ -//│ tmp3 = Predef.print("ok"); -//│ return 42 -//│ }; -//│ tmp1 = whoops(); -//│ tmp2 = whoops(); -//│ return tmp1 + tmp2 +//│ let test; +//│ test = function test() {{ +//│ let whoops, tmp1, tmp2; /** scoped **/ +//│ whoops = function whoops() {{ +//│ let tmp3; /** scoped **/ +//│ tmp3 = Predef.print("ok"); +//│ return 42 +//│ } +//│ }; +//│ tmp1 = whoops(); +//│ tmp2 = whoops(); +//│ return tmp1 + tmp2 +//│ } //│ }; test() @@ -65,7 +67,7 @@ module T with val c = p val d = this.p //│ JS (unsanitized): -//│ let T1; /** scoped **/ +//│ let T1; //│ globalThis.Object.freeze(class T { //│ static { //│ T1 = this @@ -107,7 +109,7 @@ T.d module M with fun t = 0 //│ JS (unsanitized): -//│ let M1; /** scoped **/ +//│ let M1; //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -135,12 +137,13 @@ fun test() = fun whoops = 42 whoops //│ JS (unsanitized): -//│ let test1; /** scoped **/ -//│ test1 = function test() { -//│ let whoops, tmp1; /** scoped **/ -//│ whoops = function whoops() { return 42 }; -//│ tmp1 = whoops(); -//│ return tmp1 +//│ let test1; +//│ test1 = function test() {{ +//│ let whoops, tmp1; /** scoped **/ +//│ whoops = function whoops() { return 42 }; +//│ tmp1 = whoops(); +//│ return tmp1 +//│ } //│ }; @@ -163,8 +166,8 @@ fun bar() = fun baz() = 42 baz //│ JS (unsanitized): -//│ let bar; /** scoped **/ -//│ bar = function bar() { let baz; /** scoped **/ baz = function baz() { return 42 }; return baz }; +//│ let bar; +//│ bar = function bar() {{ let baz; /** scoped **/ baz = function baz() { return 42 }; return baz } }; :sjs @@ -173,25 +176,27 @@ fun baz() = fun z = 2 (x, y) => x + y + w + z //│ JS (unsanitized): -//│ let baz; /** scoped **/ +//│ let baz; //│ baz = function baz() { -//│ let lambda; -//│ let w, z; /** scoped **/ -//│ w = function w() { -//│ return 1 -//│ }; -//│ z = function z() { -//│ return 2 -//│ }; -//│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ -//│ tmp1 = x + y; -//│ tmp2 = w(); -//│ tmp3 = tmp1 + tmp2; -//│ tmp4 = z(); -//│ return tmp3 + tmp4 -//│ }); -//│ return lambda +//│ let lambda;{ +//│ let w, z; /** scoped **/ +//│ w = function w() { +//│ return 1 +//│ }; +//│ z = function z() { +//│ return 2 +//│ }; +//│ lambda = (undefined, function (x, y) {{ +//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ tmp1 = x + y; +//│ tmp2 = w(); +//│ tmp3 = tmp1 + tmp2; +//│ tmp4 = z(); +//│ return tmp3 + tmp4 +//│ } +//│ }); +//│ return lambda +//│ } //│ }; @@ -207,20 +212,22 @@ fun a() = b + d c //│ JS (unsanitized): -//│ let a; /** scoped **/ -//│ a = function a() { -//│ let b, c; /** scoped **/ -//│ b = function b() { -//│ return 1 -//│ }; -//│ c = function c() { -//│ let d, tmp2, tmp3; /** scoped **/ -//│ d = function d() { return 2 }; -//│ tmp2 = b(); -//│ tmp3 = d(); -//│ return tmp2 + tmp3 -//│ }; -//│ return c +//│ let a; +//│ a = function a() {{ +//│ let b, c; /** scoped **/ +//│ b = function b() { +//│ return 1 +//│ }; +//│ c = function c() {{ +//│ let d, tmp2, tmp3; /** scoped **/ +//│ d = function d() { return 2 }; +//│ tmp2 = b(); +//│ tmp3 = d(); +//│ return tmp2 + tmp3 +//│ } +//│ }; +//│ return c +//│ } //│ }; @@ -236,19 +243,21 @@ fun b() = c d //│ JS (unsanitized): -//│ let b; /** scoped **/ -//│ b = function b() { -//│ let c, d; /** scoped **/ -//│ c = function c() { -//│ return 1 -//│ }; -//│ d = function d() { -//│ let c1, tmp3; /** scoped **/ -//│ c1 = function c() { return 2 }; -//│ tmp3 = c1(); -//│ return tmp3 -//│ }; -//│ return d +//│ let b; +//│ b = function b() {{ +//│ let c, d; /** scoped **/ +//│ c = function c() { +//│ return 1 +//│ }; +//│ d = function d() {{ +//│ let c1, tmp3; /** scoped **/ +//│ c1 = function c() { return 2 }; +//│ tmp3 = c1(); +//│ return tmp3 +//│ } +//│ }; +//│ return d +//│ } //│ }; @@ -264,23 +273,25 @@ fun c() = e + f d //│ JS (unsanitized): -//│ let c; /** scoped **/ -//│ c = function c() { -//│ let d, f, tmp4; /** scoped **/ -//│ f = function f() { -//│ return 1 -//│ }; -//│ d = function d() { -//│ let e, tmp5, tmp6; /** scoped **/ -//│ e = function e() { +//│ let c; +//│ c = function c() {{ +//│ let d, f, tmp4; /** scoped **/ +//│ f = function f() { //│ return 1 //│ }; -//│ tmp5 = e(); -//│ tmp6 = f(); -//│ return tmp5 + tmp6 -//│ }; -//│ tmp4 = d(); -//│ return tmp4 +//│ d = function d() {{ +//│ let e, tmp5, tmp6; /** scoped **/ +//│ e = function e() { +//│ return 1 +//│ }; +//│ tmp5 = e(); +//│ tmp6 = f(); +//│ return tmp5 + tmp6 +//│ } +//│ }; +//│ tmp4 = d(); +//│ return tmp4 +//│ } //│ }; @@ -292,7 +303,7 @@ c() data class Foo(x) with fun oops = x //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index 7c40714263..e93f631cf8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,7 +21,7 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = false; //│ if (scrut === true) { //│ 0 @@ -37,13 +37,14 @@ fun foo() = if false then 0 foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo() { -//│ let scrut1; /** scoped **/ -//│ scrut1 = false; -//│ if (scrut1 === true) { -//│ return 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ let foo; +//│ foo = function foo() {{ +//│ let scrut1; /** scoped **/ +//│ scrut1 = false; +//│ if (scrut1 === true) { +//│ return 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ }; //│ foo() //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'freeze') diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index 10433bdb39..84ccc36c3c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -10,7 +10,7 @@ object Test with print(Test) Test.x //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = globalThis.Object.freeze(new this) @@ -21,10 +21,11 @@ object Test with //│ value: Test //│ }) //│ } -//│ foo() { -//│ let tmp; /** scoped **/ -//│ tmp = Predef.print(Test1); -//│ return Test1.x +//│ foo() {{ +//│ let tmp; /** scoped **/ +//│ tmp = Predef.print(Test1); +//│ return Test1.x +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["object", "Test"]; @@ -46,7 +47,7 @@ Test.foo() :sjs val Test = "oops" //│ JS (unsanitized): -//│ let Test2; /** scoped **/ Test2 = "oops"; +//│ let Test2; Test2 = "oops"; //│ Test = "oops" :re @@ -60,13 +61,7 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let f; -//│ let x, f1, x1; /** scoped **/ -//│ x = 1; -//│ f = function f() { return x }; -//│ f1 = f; -//│ x1 = 2; -//│ runtime.safeCall(f1()) +//│ let x, f, x1, f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) //│ = 1 //│ f = fun f //│ x = 2 @@ -78,10 +73,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.79: let x = 2 +//│ ║ l.74: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.78: val x = 1 +//│ ║ l.73: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test4 { @@ -121,7 +116,7 @@ data class Cls(x) with fun bar = x print(this.x, x) //│ JS (unsanitized): -//│ let Cls1; /** scoped **/ +//│ let Cls1; //│ Cls1 = function Cls(x2) { //│ return globalThis.Object.freeze(new Cls.class(x2)); //│ }; @@ -185,7 +180,7 @@ module Whoops with val w: module Whoops = this fun g() = f() //│ JS (unsanitized): -//│ let Whoops2; /** scoped **/ +//│ let Whoops2; //│ globalThis.Object.freeze(class Whoops { //│ static { //│ Whoops2 = this @@ -232,7 +227,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.233: Runtime +//│ ║ l.228: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index e09edf1374..c048b26350 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let f, lambda; /** scoped **/ +//│ let f, lambda; //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,14 +26,15 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f1, lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1; /** scoped **/ -//│ if (x === true) { -//│ tmp = "ok"; -//│ } else { tmp = "ko"; } -//│ tmp1 = tmp + "!"; -//│ return Predef.print(tmp1) +//│ let f1, lambda1; +//│ lambda1 = (undefined, function (x) {{ +//│ let tmp, tmp1; /** scoped **/ +//│ if (x === true) { +//│ tmp = "ok"; +//│ } else { tmp = "ko"; } +//│ tmp1 = tmp + "!"; +//│ return Predef.print(tmp1) +//│ } //│ }); //│ f1 = lambda1; //│ f = fun @@ -41,14 +42,15 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f2, lambda2; /** scoped **/ -//│ lambda2 = (undefined, function (x) { -//│ let tmp, tmp1; /** scoped **/ -//│ if (x === true) { -//│ tmp = "ok"; -//│ } else { tmp = "ko"; } -//│ tmp1 = tmp + "!"; -//│ return Predef.print(tmp1) +//│ let f2, lambda2; +//│ lambda2 = (undefined, function (x) {{ +//│ let tmp, tmp1; /** scoped **/ +//│ if (x === true) { +//│ tmp = "ok"; +//│ } else { tmp = "ko"; } +//│ tmp1 = tmp + "!"; +//│ return Predef.print(tmp1) +//│ } //│ }); //│ f2 = lambda2; //│ f = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls index e2425e33b6..07b13f5dfb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls @@ -39,9 +39,7 @@ Some(1) :sjs (new Some(1)) isDefined() //│ JS (unsanitized): -//│ let tmp3; /** scoped **/ -//│ tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); -//│ Option.isDefined(tmp3) +//│ let tmp3; tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); Option.isDefined(tmp3) //│ = true new Some(1) isDefined() @@ -68,14 +66,14 @@ None == Option.None :re Option.oops //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.69: Option.oops +//│ ║ l.67: Option.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' :e open Option { oops } //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.76: open Option { oops } +//│ ║ l.74: open Option { oops } //│ ╙── ^^^^ oops @@ -84,7 +82,7 @@ oops :re Option.None.oops //│ ╔══[ERROR] Object 'None' does not contain member 'oops' -//│ ║ l.85: Option.None.oops +//│ ║ l.83: Option.None.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index 79d3e59992..279a112807 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -11,11 +11,12 @@ fun foo() = "a" ~ "b" ~ "c" foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo() { -//│ let tmp; /** scoped **/ -//│ tmp = M.concat("a", "b"); -//│ return M.concat(tmp, "c") +//│ let foo; +//│ foo = function foo() {{ +//│ let tmp; /** scoped **/ +//│ tmp = M.concat("a", "b"); +//│ return M.concat(tmp, "c") +//│ } //│ }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index eb040590d5..e688b7c912 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -3,14 +3,15 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda; /** scoped **/ -//│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ tmp = x + 1; -//│ tmp1 = tmp + 1; -//│ tmp2 = tmp1 + 1; -//│ tmp3 = tmp2 + 1; -//│ return tmp3 + 1 +//│ let lambda; +//│ lambda = (undefined, function (x) {{ +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ tmp = x + 1; +//│ tmp1 = tmp + 1; +//│ tmp2 = tmp1 + 1; +//│ tmp3 = tmp2 + 1; +//│ return tmp3 + 1 +//│ } //│ }); //│ lambda(1) //│ = 6 @@ -18,15 +19,16 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ -//│ tmp = x + 1; -//│ tmp1 = tmp + 1; -//│ tmp2 = tmp1 + 1; -//│ tmp3 = tmp2 + 1; -//│ tmp4 = tmp3 + 1; -//│ return tmp4 + 1 +//│ let lambda1; +//│ lambda1 = (undefined, function (x) {{ +//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ tmp = x + 1; +//│ tmp1 = tmp + 1; +//│ tmp2 = tmp1 + 1; +//│ tmp3 = tmp2 + 1; +//│ tmp4 = tmp3 + 1; +//│ return tmp4 + 1 +//│ } //│ }); //│ lambda1(1) //│ = 7 @@ -34,13 +36,13 @@ :sjs (x => x) + 1 //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 +//│ let lambda2; lambda2 = (undefined, function (x) { return x }); lambda2 + 1 //│ = "function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }1" :sjs 1 + (x => x) //│ JS (unsanitized): -//│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 +//│ let lambda3; lambda3 = (undefined, function (x) { return x }); 1 + lambda3 //│ = "1function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls index 86a50c8394..82aa43bbfd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls @@ -8,7 +8,7 @@ x => let y = x y //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function (x) { let y; /** scoped **/ y = x; return y }); lambda +//│ let lambda; lambda = (undefined, function (x) {{ let y; /** scoped **/ y = x; return y } }); lambda //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index d7b8eb9113..66b4dc8e2c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -58,7 +58,7 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ +//│ let tmp; //│ split_root$: { //│ split_default$: { //│ split_1$: { @@ -107,31 +107,32 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3; /** scoped **/ +//│ let x, tmp1, tmp2, tmp3; //│ if (a instanceof A.class) { //│ tmp2 = 1; //│ } else if (a instanceof B.class) { //│ tmp2 = 2; //│ } else if (a instanceof C.class) { //│ tmp2 = 3; -//│ } else { -//│ let tmp4; /** scoped **/ -//│ tmp1 = 3; -//│ if (a instanceof D.class) { -//│ if (a instanceof A.class) { -//│ tmp4 = 1 + tmp1; -//│ } else if (a instanceof B.class) { -//│ tmp4 = 2 + tmp1; +//│ } else {{ +//│ let tmp4; /** scoped **/ +//│ tmp1 = 3; +//│ if (a instanceof D.class) { +//│ if (a instanceof A.class) { +//│ tmp4 = 1 + tmp1; +//│ } else if (a instanceof B.class) { +//│ tmp4 = 2 + tmp1; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ tmp3 = tmp4; +//│ } else if (a instanceof E.class) { +//│ tmp3 = 5; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp3 = tmp4; -//│ } else if (a instanceof E.class) { -//│ tmp3 = 5; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ tmp2 = Predef.print("done"); //│ } -//│ tmp2 = Predef.print("done"); //│ } //│ x = tmp2; //│ Predef.print(x) @@ -147,7 +148,7 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ tmp5 = 2; //│ if (a instanceof A.class) { //│ 1 @@ -169,7 +170,7 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ if (a instanceof A.class) { //│ 1 //│ } else { @@ -192,7 +193,7 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ let x1, tmp7; /** scoped **/ +//│ let x1, tmp7; //│ if (a instanceof A.class) { //│ 1 //│ } else if (a instanceof B.class) { @@ -221,7 +222,7 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; //│ if (a instanceof B.class) { //│ 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index 19b5d0c1e1..f82067f6d7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,7 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let Test1, tmp1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -51,6 +51,7 @@ module Test with //│ } //│ static #s; //│ static { +//│ let tmp1; //│ Test.#s = 1; //│ tmp1 = Predef.print(Test.#s); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index dc8f7d5442..ba7417ebd2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -4,7 +4,7 @@ :sjs module None //│ JS (unsanitized): -//│ let None1; /** scoped **/ +//│ let None1; //│ globalThis.Object.freeze(class None { //│ static { //│ None1 = this @@ -55,7 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1, tmp; /** scoped **/ +//│ let M1; //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -64,6 +64,7 @@ module M with //│ runtime.Unit; //│ } //│ static { +//│ let tmp; //│ globalThis.Object.freeze(class C { //│ static { //│ M.C = this @@ -110,7 +111,7 @@ M.y :re M.oops //│ ╔══[ERROR] Module 'M' does not contain member 'oops' -//│ ║ l.111: M.oops +//│ ║ l.112: M.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' @@ -122,7 +123,7 @@ M.oops module M with val m: module M = M //│ JS (unsanitized): -//│ let M3; /** scoped **/ +//│ let M3; //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -155,7 +156,7 @@ module AA with val y = 2 [x, y] //│ JS (unsanitized): -//│ let AA1; /** scoped **/ +//│ let AA1; //│ globalThis.Object.freeze(class AA { //│ static { //│ AA1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 9f2d151267..1318302565 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -52,7 +52,7 @@ none() :sjs val Option = "Oops" //│ JS (unsanitized): -//│ let Option1; /** scoped **/ Option1 = "Oops"; +//│ let Option1; Option1 = "Oops"; //│ Option = "Oops" :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 2416214a74..079a3249d3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -10,7 +10,7 @@ fun isDefined(x) = if x is Some then true None then false //│ JS (unsanitized): -//│ let isDefined; /** scoped **/ +//│ let isDefined; //│ isDefined = function isDefined(x) { //│ if (x instanceof Some.class) { //│ return true @@ -31,16 +31,17 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; /** scoped **/ -//│ lambda = (undefined, function (caseScrut) { -//│ let argument0$; /** scoped **/ -//│ if (caseScrut instanceof Some.class) { -//│ argument0$ = caseScrut.value; -//│ return true -//│ } else if (caseScrut instanceof None.class) { -//│ return false -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ let isDefined1, lambda; +//│ lambda = (undefined, function (caseScrut) {{ +//│ let argument0$; /** scoped **/ +//│ if (caseScrut instanceof Some.class) { +//│ argument0$ = caseScrut.value; +//│ return true +//│ } else if (caseScrut instanceof None.class) { +//│ return false +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } //│ } //│ }); //│ isDefined1 = lambda; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 04625446e1..71bbc57a17 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -6,7 +6,7 @@ data class Foo() //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo() { //│ return globalThis.Object.freeze(new Foo.class()); //│ }; @@ -37,7 +37,7 @@ Foo.class data class Foo(a) //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ Foo3 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -64,19 +64,19 @@ Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Foo2(1); tmp.a +//│ let tmp; tmp = Foo2(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = function foo(y) { return Foo2(y) }; foo(27) +//│ let foo; foo = function foo(y) { return Foo2(y) }; foo(27) //│ = Foo(27) data class Foo(a, b) //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ Foo5 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -94,17 +94,17 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = Foo4; +//│ let foo1; foo1 = Foo4; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) //│ JS (unsanitized): -//│ let f; /** scoped **/ f = runtime.safeCall(foo1(1, 2)); +//│ let f; f = runtime.safeCall(foo1(1, 2)); //│ f = Foo(1, 2) let f = new! foo(1, 2) //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = globalThis.Object.freeze(new foo1(1, 2)); +//│ let f1; f1 = globalThis.Object.freeze(new foo1(1, 2)); //│ f = Foo(1, 2) f.a @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; /** scoped **/ f2 = Foo4(1, 2); +//│ let f2; f2 = Foo4(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) +//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -144,7 +144,7 @@ data class Inner(c) with fun i1(d) = c + d print(c) //│ JS (unsanitized): -//│ let Inner1; /** scoped **/ +//│ let Inner1; //│ Inner1 = function Inner(c) { //│ return globalThis.Object.freeze(new Inner.class(c)); //│ }; @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner.class(100)); +//│ let i; i = globalThis.Object.freeze(new Inner.class(100)); //│ > 100 //│ i = Inner(100) @@ -185,7 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let Foo7; //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -194,6 +194,7 @@ class Foo(x, val y, z, val z, z) with //│ Foo7.class = this //│ } //│ constructor(x, y, z, z1, z2) { +//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ this.#x = x; //│ this.y = y; //│ this.#z = z; @@ -236,13 +237,13 @@ Foo(10, 20, 30, 40, 50) class Foo(val z, val z) //│ ╔══[ERROR] Duplicate definition of member named 'z'. //│ ╟── Defined at: -//│ ║ l.236: class Foo(val z, val z) +//│ ║ l.237: class Foo(val z, val z) //│ ║ ^ //│ ╟── Defined at: -//│ ║ l.236: class Foo(val z, val z) +//│ ║ l.237: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo9; /** scoped **/ +//│ let Foo9; //│ Foo9 = function Foo(z, z1) { //│ return globalThis.Object.freeze(new Foo.class(z, z1)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index 9b85fd7887..dd66cf1dbf 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -12,7 +12,7 @@ f(2) :sjs let f = foo(1, _, _) //│ JS (unsanitized): -//│ let f2; let f3; /** scoped **/ f2 = function f(_0, _1) { return foo(1, _0, _1) }; f3 = f2; +//│ let f2, f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; //│ f = fun f f(2, 3) @@ -79,10 +79,7 @@ h(1) :sjs let i = _(0, 1, _) //│ JS (unsanitized): -//│ let i; -//│ let i1; /** scoped **/ -//│ i = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; -//│ i1 = i; +//│ let i, i1; i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; i = i1; //│ i = fun i i(print, 2) @@ -202,7 +199,7 @@ _ - _ of 1, 2 :pe |> 1 //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.203: |> 1 +//│ ║ l.200: |> 1 //│ ╙── ^ //│ = fun pipeInto @@ -210,7 +207,7 @@ _ - _ of 1, 2 :re |> (1) //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.211: |> (1) +//│ ║ l.208: |> (1) //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] TypeError: f is not a function @@ -228,15 +225,13 @@ _ - _ of 1, 2 :sjs 1 \ (_ - 2) //│ JS (unsanitized): -//│ let lambda38; /** scoped **/ -//│ lambda38 = (undefined, function (_0) { return _0 - 2 }); -//│ Predef.passTo(1, lambda38) +//│ let lambda38; lambda38 = (undefined, function (_0) { return _0 - 2 }); Predef.passTo(1, lambda38) //│ = fun :sjs 1 \ (_ - 2)() //│ JS (unsanitized): -//│ let lambda39, tmp19; /** scoped **/ +//│ let lambda39, tmp19; //│ lambda39 = (undefined, function (_0) { //│ return _0 - 2 //│ }); @@ -253,13 +248,13 @@ _ - _ of 1, 2 :w let f = if _ then 1 else 0 //│ ╔══[WARNING] This catch-all clause makes the following branches unreachable. -//│ ║ l.254: let f = if _ then 1 else 0 +//│ ║ l.249: let f = if _ then 1 else 0 //│ ║ ^^^^^^ //│ ╟── This branch is unreachable. -//│ ║ l.254: let f = if _ then 1 else 0 +//│ ║ l.249: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; /** scoped **/ tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs @@ -268,10 +263,11 @@ fun f(x) = 0 then 1 _ then 2 //│ JS (unsanitized): -//│ let f16; /** scoped **/ -//│ f16 = function f(x3) { -//│ let scrut; /** scoped **/ -//│ scrut = x3 == 0; -//│ if (scrut === true) { return 1 } else { return 2 } +//│ let f16; +//│ f16 = function f(x3) {{ +//│ let scrut; /** scoped **/ +//│ scrut = x3 == 0; +//│ if (scrut === true) { return 1 } else { return 2 } +//│ } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 19591829d2..e5395f1812 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -6,7 +6,7 @@ class Foo //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -23,7 +23,7 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = globalThis.Object.freeze(new Foo()); //│ if (scrut instanceof Foo) { true } else { false } //│ = true @@ -53,7 +53,7 @@ Foo() data class Foo with { print("hi") } print("ok") //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ globalThis.Object.freeze(class Foo2 { //│ static { //│ Foo3 = this @@ -72,34 +72,35 @@ fun test() = print("ok") Foo //│ JS (unsanitized): -//│ let test; /** scoped **/ -//│ test = function test() { -//│ let Foo5, tmp; /** scoped **/ -//│ globalThis.Object.freeze(class Foo4 { -//│ static { -//│ Foo5 = this -//│ } -//│ constructor() { -//│ Predef.print("hi"); -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Foo"]; -//│ }); -//│ tmp = Predef.print("ok"); -//│ return Foo5 +//│ let test; +//│ test = function test() {{ +//│ let Foo5, tmp; /** scoped **/ +//│ globalThis.Object.freeze(class Foo4 { +//│ static { +//│ Foo5 = this +//│ } +//│ constructor() { +//│ Predef.print("hi"); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Foo"]; +//│ }); +//│ tmp = Predef.print("ok"); +//│ return Foo5 +//│ } //│ }; let t = test() //│ JS (unsanitized): -//│ let t; /** scoped **/ t = test(); +//│ let t; t = test(); //│ > ok //│ t = class Foo :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.99: new t -//│ ║ ^ +//│ ║ l.100: new t +//│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): //│ globalThis.Object.freeze(new t()) @@ -115,7 +116,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.116: new t() +//│ ║ l.117: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -135,7 +136,7 @@ class Foo with let y = x + 1 fun z() = y + x //│ JS (unsanitized): -//│ let Foo6; /** scoped **/ +//│ let Foo6; //│ globalThis.Object.freeze(class Foo5 { //│ static { //│ Foo6 = this @@ -162,7 +163,7 @@ class Foo with fun z2() = 6 print("hello") //│ JS (unsanitized): -//│ let Foo8; /** scoped **/ +//│ let Foo8; //│ globalThis.Object.freeze(class Foo7 { //│ static { //│ Foo8 = this @@ -192,7 +193,7 @@ class Foo with fun foo(y) = x + y fun bar(z) = foo(z) + 1 //│ JS (unsanitized): -//│ let Foo10; /** scoped **/ +//│ let Foo10; //│ globalThis.Object.freeze(class Foo9 { //│ static { //│ Foo10 = this @@ -203,10 +204,11 @@ class Foo with //│ foo(y) { //│ return this.x + y //│ } -//│ bar(z) { -//│ let tmp; /** scoped **/ -//│ tmp = this.foo(z); -//│ return tmp + 1 +//│ bar(z) {{ +//│ let tmp; /** scoped **/ +//│ tmp = this.foo(z); +//│ return tmp + 1 +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Foo"]; @@ -217,7 +219,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let a, tmp, tmp1, tmp2, tmp3; //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); @@ -237,13 +239,13 @@ class Foo with val x = 2 //│ ╔══[ERROR] Multiple definitions of symbol 'x' //│ ╟── defined here -//│ ║ l.236: val x = 1 +//│ ║ l.238: val x = 1 //│ ║ ^^^^^^^^^ //│ ╟── defined here -//│ ║ l.237: val x = 2 +//│ ║ l.239: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo12; /** scoped **/ +//│ let Foo12; //│ globalThis.Object.freeze(class Foo11 { //│ static { //│ Foo12 = this @@ -261,13 +263,13 @@ class Foo with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.262: let x = 2 +//│ ║ l.264: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.261: val x = 1 +//│ ║ l.263: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo14; /** scoped **/ +//│ let Foo14; //│ globalThis.Object.freeze(class Foo13 { //│ static { //│ Foo14 = this @@ -291,10 +293,10 @@ class Foo with :e class Foo with val x = 1 //│ ╔══[ERROR] Illegal body of class definition (should be a block; found term definition). -//│ ║ l.292: class Foo with val x = 1 +//│ ║ l.294: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo16; /** scoped **/ +//│ let Foo16; //│ globalThis.Object.freeze(class Foo15 { //│ static { //│ Foo16 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls index ea8a6fed34..8a571ad88e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls @@ -7,7 +7,7 @@ let x = 1 :sjs let x' = 2 //│ JS (unsanitized): -//│ let x$_; /** scoped **/ x$_ = 2; +//│ let x$_; x$_ = 2; //│ x' = 2 x' diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 3cd21a98c2..4defe108c8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; /** scoped **/ +//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index 3ae4eea381..3e823a55c8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,16 +76,15 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let x1, tmp6, tmp7, arr2; -//│ let tmp8; /** scoped **/ +//│ let x1, tmp6, tmp7, tmp8, arr2; //│ tmp6 = globalThis.Object.freeze(new Term.Symbol("x")); //│ x1 = globalThis.Object.freeze(new Term.Ref(tmp6)); -//│ tmp8 = Predef.print(x1); +//│ tmp7 = Predef.print(x1); //│ arr2 = globalThis.Object.freeze([ //│ tmp6 //│ ]); -//│ tmp7 = x1; -//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp7)) +//│ tmp8 = x1; +//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp8)) //│ > Ref(Symbol("x")) //│ = Lam([Symbol("x")], Ref(Symbol("x"))) diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index a252994119..dae7d44373 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -4,17 +4,18 @@ :sjs fun foo() = if false do foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo() { -//│ let scrut; /** scoped **/ -//│ scrut = false; -//│ if (scrut === true) { return foo() } else { return runtime.Unit } +//│ let foo; +//│ foo = function foo() {{ +//│ let scrut; /** scoped **/ +//│ scrut = false; +//│ if (scrut === true) { return foo() } else { return runtime.Unit } +//│ } //│ }; :sjs fun foo() = foo() //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = function foo() { return foo1() }; +//│ let foo1; foo1 = function foo() { return foo1() }; :sjs @@ -22,7 +23,7 @@ data class Foo(x) with class Bar with val y = x //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; @@ -54,11 +55,12 @@ fun foo() = fun bar() = bar() bar() //│ JS (unsanitized): -//│ let foo2; /** scoped **/ -//│ foo2 = function foo() { -//│ let bar; /** scoped **/ -//│ bar = function bar() { return bar() }; -//│ return bar() +//│ let foo2; +//│ foo2 = function foo() {{ +//│ let bar; /** scoped **/ +//│ bar = function bar() { return bar() }; +//│ return bar() +//│ } //│ }; @@ -68,9 +70,9 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; /** scoped **/ +//│ let f, f1; //│ f1 = 1; -//│ f = function f() { let tmp; /** scoped **/ tmp = f(); return tmp }; +//│ f = function f() {{ let tmp; /** scoped **/ tmp = f(); return tmp } }; //│ runtime.Unit //│ f = 1 @@ -79,13 +81,13 @@ do let foo = 1 fun foo(x) = foo //│ ╔══[ERROR] Name 'foo' is already used -//│ ║ l.79: let foo = 1 +//│ ║ l.81: let foo = 1 //│ ║ ^^^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.80: fun foo(x) = foo +//│ ║ l.82: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; +//│ let foo3, foo4; foo3 = function foo(x) { return foo4 }; foo4 = 1; //│ foo = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 53686fe526..e09a382fc7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -51,10 +51,11 @@ id(f)(3)(4) //│ return (...args1) => { //│ runtime.checkArgs("", 2, true, args1.length); //│ let y = args1[0]; -//│ let z = args1[1]; -//│ let tmp4; /** scoped **/ -//│ tmp4 = x + y; -//│ return tmp4 + z +//│ let z = args1[1];{ +//│ let tmp4; /** scoped **/ +//│ tmp4 = x + y; +//│ return tmp4 + z +//│ } //│ } //│ }; //│ tmp2 = runtime.checkCall(Predef.id(f1)); @@ -108,11 +109,12 @@ id(Cls(1, 2)).f(3) //│ this.x = x; //│ this.y = y; //│ } -//│ f(z, p) { -//│ let tmp6, tmp7; /** scoped **/ -//│ tmp6 = this.x + this.y; -//│ tmp7 = tmp6 + z; -//│ return tmp7 + p +//│ f(z, p) {{ +//│ let tmp6, tmp7; /** scoped **/ +//│ tmp6 = this.x + this.y; +//│ tmp7 = tmp6 + z; +//│ return tmp7 + p +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; @@ -144,11 +146,12 @@ id(Cls(1, 2)).f(3) //│ f(...args) { //│ runtime.checkArgs("f", 2, true, args.length); //│ let z = args[0]; -//│ let p = args[1]; -//│ let tmp8, tmp9; /** scoped **/ -//│ tmp8 = this.x + this.y; -//│ tmp9 = tmp8 + z; -//│ return tmp9 + p +//│ let p = args[1];{ +//│ let tmp8, tmp9; /** scoped **/ +//│ tmp8 = this.x + this.y; +//│ tmp9 = tmp8 + z; +//│ return tmp9 + p +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; @@ -185,13 +188,14 @@ id(Cls(1, 2)).f(3, 4)(5) //│ return (...args1) => { //│ runtime.checkArgs("", 2, true, args1.length); //│ let q = args1[0]; -//│ let s = args1[1]; -//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ -//│ tmp11 = this.x + this.y; -//│ tmp12 = tmp11 + z; -//│ tmp13 = tmp12 + p; -//│ tmp14 = tmp13 + q; -//│ return tmp14 + s +//│ let s = args1[1];{ +//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ +//│ tmp11 = this.x + this.y; +//│ tmp12 = tmp11 + z; +//│ tmp13 = tmp12 + p; +//│ tmp14 = tmp13 + q; +//│ return tmp14 + s +//│ } //│ } //│ } //│ toString() { return runtime.render(this); } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 6c1ed8a013..dce6ad997c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,11 +19,12 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ f = function f() { -//│ let scrut, a, b; /** scoped **/ -//│ scrut = true; -//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ let f; +//│ f = function f() {{ +//│ let scrut, a, b; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ } //│ }; @@ -40,35 +41,36 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; /** scoped **/ +//│ let g; //│ g = function g(x1, y1, z) { -//│ let lambda; -//│ let a, tmp1, tmp2; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (x1 === true) { -//│ break split_1$ -//│ } else { -//│ if (y1 === true) { +//│ let lambda;{ +//│ let a, tmp1, tmp2; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (x1 === true) { //│ break split_1$ //│ } else { -//│ if (z === true) { -//│ a = 1; -//│ lambda = (undefined, function (_0) { -//│ return _0 + a -//│ }); -//│ tmp1 = lambda; -//│ break split_root$ +//│ if (y1 === true) { +//│ break split_1$ //│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ if (z === true) { +//│ a = 1; +//│ lambda = (undefined, function (_0) { +//│ return _0 + a +//│ }); +//│ tmp1 = lambda; +//│ break split_root$ +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } //│ } //│ } //│ } +//│ tmp1 = 0; //│ } -//│ tmp1 = 0; +//│ tmp2 = 4 * tmp1; +//│ return 3 + tmp2 //│ } -//│ tmp2 = 4 * tmp1; -//│ return 3 + tmp2 //│ }; @@ -84,37 +86,38 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f() { -//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (x instanceof A.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ b = argument1$; -//│ a = argument0$; -//│ scrut = a > b; -//│ if (scrut === true) { -//│ tmp1 = true; +//│ let f1; +//│ f1 = function f() {{ +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (x instanceof A.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ b = argument1$; +//│ a = argument0$; +//│ scrut = a > b; +//│ if (scrut === true) { +//│ tmp1 = true; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else if (x instanceof B.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ d = argument1$; +//│ c = argument0$; +//│ tmp1 = false; //│ break split_root$ //│ } else { //│ break split_default$ //│ } -//│ } else if (x instanceof B.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ d = argument1$; -//│ c = argument0$; -//│ tmp1 = false; -//│ break split_root$ -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp1 //│ } -//│ return tmp1 //│ }; @@ -122,32 +125,33 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f() { -//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ scrut = A(1, Nil); -//│ if (scrut instanceof A.class) { -//│ argument0$ = scrut.a; -//│ argument1$ = scrut.b; -//│ if (argument0$ === 1) { -//│ if (argument1$ instanceof Nil.class) { -//│ tmp1 = 3; -//│ break split_root$ +//│ let f2; +//│ f2 = function f() {{ +//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ scrut = A(1, Nil); +//│ if (scrut instanceof A.class) { +//│ argument0$ = scrut.a; +//│ argument1$ = scrut.b; +//│ if (argument0$ === 1) { +//│ if (argument1$ instanceof Nil.class) { +//│ tmp1 = 3; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp1 //│ } -//│ return tmp1 //│ }; @@ -161,32 +165,34 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; /** scoped **/ -//│ f3 = function f(x1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = undefined; -//│ while1 = (undefined, function () { -//│ let a, a1, argument0$, argument1$; /** scoped **/ -//│ if (x1 instanceof A.class) { -//│ argument0$ = x1.a; -//│ argument1$ = x1.b; -//│ a = argument0$; -//│ tmp1 = runtime.safeCall(a()); -//│ return while1() -//│ } else if (x1 instanceof B.class) { -//│ argument0$ = x1.a; -//│ argument1$ = x1.b; -//│ a1 = argument0$; -//│ tmp1 = runtime.safeCall(a1()); -//│ return while1() -//│ } else { -//│ tmp1 = runtime.safeCall(x1()); -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { x1 = tmp1; return x1 } +//│ let f3; +//│ f3 = function f(x1) {{ +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ tmp1 = undefined; +//│ while1 = (undefined, function () {{ +//│ let a, a1, argument0$, argument1$; /** scoped **/ +//│ if (x1 instanceof A.class) { +//│ argument0$ = x1.a; +//│ argument1$ = x1.b; +//│ a = argument0$; +//│ tmp1 = runtime.safeCall(a()); +//│ return while1() +//│ } else if (x1 instanceof B.class) { +//│ argument0$ = x1.a; +//│ argument1$ = x1.b; +//│ a1 = argument0$; +//│ tmp1 = runtime.safeCall(a1()); +//│ return while1() +//│ } else { +//│ tmp1 = runtime.safeCall(x1()); +//│ } +//│ return runtime.LoopEnd +//│ } +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { x1 = tmp1; return x1 } +//│ } //│ }; @@ -201,46 +207,48 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; /** scoped **/ -//│ f4 = function f() { -//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ y1 = x + 1; -//│ tmp1 = undefined; -//│ while1 = (undefined, function () { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (y1 instanceof A.class) { -//│ argument0$ = y1.a; -//│ argument1$ = y1.b; -//│ a = argument1$; -//│ z = argument0$; -//│ scrut = false; -//│ if (scrut === true) { -//│ scrut1 = true; -//│ if (scrut1 === true) { -//│ tmp4 = z + 1; -//│ y1 = tmp4; -//│ tmp1 = runtime.Unit; -//│ return while1() +//│ let f4; +//│ f4 = function f() {{ +//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ y1 = x + 1; +//│ tmp1 = undefined; +//│ while1 = (undefined, function () {{ +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (y1 instanceof A.class) { +//│ argument0$ = y1.a; +//│ argument1$ = y1.b; +//│ a = argument1$; +//│ z = argument0$; +//│ scrut = false; +//│ if (scrut === true) { +//│ scrut1 = true; +//│ if (scrut1 === true) { +//│ tmp4 = z + 1; +//│ y1 = tmp4; +//│ tmp1 = runtime.Unit; +//│ return while1() +//│ } else { +//│ break split_1$ +//│ } +//│ } else { +//│ break split_1$ +//│ } //│ } else { -//│ break split_1$ +//│ tmp1 = runtime.Unit; //│ } -//│ } else { -//│ break split_1$ //│ } -//│ } else { +//│ y1 = a; //│ tmp1 = runtime.Unit; //│ } +//│ return runtime.LoopEnd //│ } -//│ y1 = a; -//│ tmp1 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ } //│ }; @@ -249,28 +257,30 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; /** scoped **/ -//│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = undefined; -//│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; /** scoped **/ -//│ lambda = (undefined, function () { -//│ return y1 +//│ let f5; +//│ f5 = function f(x1, y1) {{ +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ tmp1 = undefined; +//│ while1 = (undefined, function () {{ +//│ let scrut, lambda, tmp4; /** scoped **/ +//│ lambda = (undefined, function () { +//│ return y1 +//│ }); +//│ scrut = runtime.short_and(x1, lambda); +//│ if (scrut === true) { +//│ tmp4 = f5(0, 0); +//│ tmp1 = tmp4 + 4; +//│ return while1() +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ } //│ }); -//│ scrut = runtime.short_and(x1, lambda); -//│ if (scrut === true) { -//│ tmp4 = f5(0, 0); -//│ tmp1 = tmp4 + 4; -//│ return while1() -//│ } else { -//│ tmp1 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 56d63f1ecd..7682c56896 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -5,7 +5,7 @@ module Foo with val self: module Foo = Foo //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -61,12 +61,13 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11, tmp; /** scoped **/ +//│ let Foo11; //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) //│ } //│ constructor() { +//│ let tmp; //│ tmp = Predef.id(Foo11); //│ this.self = tmp; //│ Object.defineProperty(this, "class", { diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 0eed770a5e..523db5a30a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -7,7 +7,7 @@ let x = 0 :sjs let x += 1 //│ JS (unsanitized): -//│ let x1; /** scoped **/ x1 = x + 1; +//│ let x1; x1 = x + 1; //│ x = 1 x @@ -21,7 +21,7 @@ set x = 0 :sjs set x += 1 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = x1 + 1; x1 = tmp; runtime.Unit +//│ let tmp; tmp = x1 + 1; x1 = tmp; runtime.Unit x //│ = 1 @@ -30,7 +30,7 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let old, tmp1, tmp2, tmp3; /** scoped **/ +//│ let old, tmp1, tmp2, tmp3; //│ old = x1; //│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } //│ tmp1 @@ -69,30 +69,31 @@ fun example() = print(get_x()) example() //│ JS (unsanitized): -//│ let example2; /** scoped **/ +//│ let example2; //│ example2 = function example() { -//│ let get_x; -//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ -//│ x2 = 0; -//│ get_x = function get_x() { -//│ return x2 -//│ }; -//│ get_x1 = get_x; -//│ old1 = x2; -//│ try { -//│ tmp5 = x2 + 1; -//│ x2 = tmp5; -//│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x1()); -//│ tmp8 = Predef.print(tmp7); -//│ tmp9 = (tmp6 , tmp8); -//│ tmp4 = tmp9; -//│ } finally { -//│ x2 = old1; +//│ let get_x;{ +//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ x2 = 0; +//│ get_x = function get_x() { +//│ return x2 +//│ }; +//│ get_x1 = get_x; +//│ old1 = x2; +//│ try { +//│ tmp5 = x2 + 1; +//│ x2 = tmp5; +//│ tmp6 = Predef.print(x2); +//│ tmp7 = runtime.safeCall(get_x1()); +//│ tmp8 = Predef.print(tmp7); +//│ tmp9 = (tmp6 , tmp8); +//│ tmp4 = tmp9; +//│ } finally { +//│ x2 = old1; +//│ } +//│ tmp10 = Predef.print(x2); +//│ tmp11 = runtime.safeCall(get_x1()); +//│ return Predef.print(tmp11) //│ } -//│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x1()); -//│ return Predef.print(tmp11) //│ }; //│ example2() //│ > 1 @@ -111,30 +112,31 @@ fun example() = y example() //│ JS (unsanitized): -//│ let example3; /** scoped **/ +//│ let example3; //│ example3 = function example() { -//│ let get_x; -//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ -//│ x2 = 0; -//│ get_x = function get_x() { -//│ return x2 -//│ }; -//│ get_x1 = get_x; -//│ old1 = x2; -//│ try { -//│ tmp5 = x2 + 1; -//│ x2 = tmp5; -//│ tmp6 = Predef.print(x2); -//│ tmp7 = (tmp6 , x2); -//│ tmp4 = tmp7; -//│ } finally { -//│ x2 = old1; +//│ let get_x;{ +//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ +//│ x2 = 0; +//│ get_x = function get_x() { +//│ return x2 +//│ }; +//│ get_x1 = get_x; +//│ old1 = x2; +//│ try { +//│ tmp5 = x2 + 1; +//│ x2 = tmp5; +//│ tmp6 = Predef.print(x2); +//│ tmp7 = (tmp6 , x2); +//│ tmp4 = tmp7; +//│ } finally { +//│ x2 = old1; +//│ } +//│ y = tmp4; +//│ tmp8 = Predef.print(x2); +//│ tmp9 = runtime.safeCall(get_x1()); +//│ tmp10 = Predef.print(tmp9); +//│ return y //│ } -//│ y = tmp4; -//│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x1()); -//│ tmp10 = Predef.print(tmp9); -//│ return y //│ }; //│ example3() //│ > 1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls index 228d0613df..9be3a3a5b7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls @@ -19,7 +19,7 @@ foo(0, ...a) :sjs foo(1, ...[2, 3], 4) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) +//│ let tmp; tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) //│ = [1, 2, 3, 4] :re diff --git a/hkmc2/shared/src/test/mlscript/codegen/This.mls b/hkmc2/shared/src/test/mlscript/codegen/This.mls index 9d809eb9e6..64418595dd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/This.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/This.mls @@ -10,14 +10,13 @@ fun test(x) = //│ ║ l.8: [this.a, x] //│ ╙── ^^^^ //│ JS (unsanitized): -//│ let test; /** scoped **/ test = function test(x) { return globalThis.Object.freeze([]) }; +//│ let test; test = function test(x) { return globalThis.Object.freeze([]) }; :sjs fun test(x) = [globalThis.a, x] //│ JS (unsanitized): -//│ let test1; /** scoped **/ -//│ test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; +//│ let test1; test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; :re test(123) @@ -43,7 +42,7 @@ module Test with fun test2(x) = [this.a, x] //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 3bb6a4a271..44fb9381b8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = call(Example, oops); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = call(Example, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) +//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls index b2171fa4c0..cad43e858a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls @@ -19,7 +19,7 @@ s(123) :sjs ex |>. s(123) //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) +//│ let tmp1; tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) //│ = [123, 456] diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index fa1b38cc34..b2041f799f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,9 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; -//│ f2(1) +//│ let f2; f2 = function f(x) {{ let y; /** scoped **/ throw globalThis.Error("e") } }; f2(1) //│ ═══[RUNTIME ERROR] Error: e @@ -43,7 +41,7 @@ fun f(x) = throw (if x then Error("x") else Error("y")) f(false) //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(x) { //│ if (x === true) { throw globalThis.Error("x") } else { throw globalThis.Error("y") } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 993741a4b1..bb1550c0d6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -7,13 +7,14 @@ fun fib(a) = if a <= 1 then a else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): -//│ let fib; /** scoped **/ -//│ fib = function fib(a) { -//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ scrut = a <= 1; -//│ if (scrut === true) { -//│ return a -//│ } else { tmp = a - 1; tmp1 = fib(tmp); tmp2 = a - 2; tmp3 = fib(tmp2); return tmp1 + tmp3 } +//│ let fib; +//│ fib = function fib(a) {{ +//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ scrut = a <= 1; +//│ if (scrut === true) { +//│ return a +//│ } else { tmp = a - 1; tmp1 = fib(tmp); tmp2 = a - 2; tmp3 = fib(tmp2); return tmp1 + tmp3 } +//│ } //│ }; fun f(x) = g(x) diff --git a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls index 72170fa838..c0ec266ced 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls @@ -30,9 +30,7 @@ print of foo() :sjs print of globalThis.console.log("Hello, world!") //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ -//│ tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); -//│ Predef.print(tmp4) +//│ let tmp4; tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); Predef.print(tmp4) //│ > Hello, world! //│ > () @@ -56,7 +54,7 @@ print of Box(foo()).value :sjs fun foo() = {} //│ JS (unsanitized): -//│ let foo5; /** scoped **/ foo5 = function foo() { return runtime.Unit }; +//│ let foo5; foo5 = function foo() { return runtime.Unit }; print of Box(foo()).value //│ > () @@ -66,7 +64,7 @@ print of Box(foo()).value fun foo(x) = set x = 1 //│ JS (unsanitized): -//│ let foo6; /** scoped **/ foo6 = function foo(x) { x = 1; return runtime.Unit }; +//│ let foo6; foo6 = function foo(x) { x = 1; return runtime.Unit }; print of Box(foo(1)).value //│ > () @@ -75,7 +73,7 @@ print of Box(foo(1)).value :e fun f = let x = 1 //│ ╔══[ERROR] Expected a body for let bindings in expression position -//│ ║ l.76: fun f = let x = 1 +//│ ║ l.74: fun f = let x = 1 //│ ╙── ^^^^^ print of f diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index eb618c68fb..884c8818ee 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -5,23 +5,25 @@ () => (while true then 0) //│ JS (unsanitized): //│ let lambda; -//│ lambda = (undefined, function () { -//│ let tmp, while1, tmp1, tmp2; /** scoped **/ -//│ tmp = undefined; -//│ while1 = (undefined, function () { -//│ let scrut; /** scoped **/ -//│ scrut = true; -//│ if (scrut === true) { -//│ tmp = 0; -//│ return while1() -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp1 = while1(); -//│ tmp2 = tmp1 !== runtime.LoopEnd; -//│ if (tmp2 === true) { return tmp1 } else { return tmp } +//│ lambda = (undefined, function () {{ +//│ let tmp, while1, tmp1, tmp2; /** scoped **/ +//│ tmp = undefined; +//│ while1 = (undefined, function () {{ +//│ let scrut; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { +//│ tmp = 0; +//│ return while1() +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ return runtime.LoopEnd +//│ } +//│ }); +//│ tmp1 = while1(); +//│ tmp2 = tmp1 !== runtime.LoopEnd; +//│ if (tmp2 === true) { return tmp1 } else { return tmp } +//│ } //│ }); //│ lambda //│ = fun @@ -57,17 +59,18 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let tmp6, while3, tmp7, tmp8; /** scoped **/ +//│ let tmp6, while3, tmp7; //│ tmp6 = undefined; -//│ while3 = (undefined, function () { -//│ let tmp9; /** scoped **/ -//│ if (x2 === true) { -//│ tmp9 = Predef.print("Hello World"); -//│ x2 = false; -//│ tmp6 = runtime.Unit; -//│ return while3() -//│ } else { tmp6 = 42; } -//│ return runtime.LoopEnd +//│ while3 = (undefined, function () {{ +//│ let tmp8; /** scoped **/ +//│ if (x2 === true) { +//│ tmp8 = Predef.print("Hello World"); +//│ x2 = false; +//│ tmp6 = runtime.Unit; +//│ return while3() +//│ } else { tmp6 = 42; } +//│ return runtime.LoopEnd +//│ } //│ }); //│ tmp7 = while3(); //│ tmp6 @@ -111,26 +114,28 @@ while i < 10 do set i += 1 //│ JS (unsanitized): //│ let lambda2; -//│ lambda2 = (undefined, function () { -//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ -//│ tmp18 = undefined; -//│ while7 = (undefined, function () { -//│ let i2, scrut, tmp21; /** scoped **/ -//│ i2 = 0; -//│ scrut = i2 < 10; -//│ if (scrut === true) { -//│ tmp21 = i2 + 1; -//│ i2 = tmp21; -//│ tmp18 = runtime.Unit; -//│ return while7() -//│ } else { -//│ tmp18 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp19 = while7(); -//│ tmp20 = tmp19 !== runtime.LoopEnd; -//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } +//│ lambda2 = (undefined, function () {{ +//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ +//│ tmp18 = undefined; +//│ while7 = (undefined, function () {{ +//│ let i2, scrut, tmp21; /** scoped **/ +//│ i2 = 0; +//│ scrut = i2 < 10; +//│ if (scrut === true) { +//│ tmp21 = i2 + 1; +//│ i2 = tmp21; +//│ tmp18 = runtime.Unit; +//│ return while7() +//│ } else { +//│ tmp18 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ } +//│ }); +//│ tmp19 = while7(); +//│ tmp20 = tmp19 !== runtime.LoopEnd; +//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } +//│ } //│ }); //│ lambda2 //│ = fun @@ -208,28 +213,30 @@ fun f(ls) = print(h) else print("Done!") //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ f = function f(ls) { -//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ -//│ tmp27 = undefined; -//│ while10 = (undefined, function () { -//│ let tl, h, argument0$, argument1$; /** scoped **/ -//│ if (ls instanceof Cons.class) { -//│ argument0$ = ls.hd; -//│ argument1$ = ls.tl; -//│ tl = argument1$; -//│ h = argument0$; -//│ ls = tl; -//│ tmp27 = Predef.print(h); -//│ return while10() -//│ } else { -//│ tmp27 = Predef.print("Done!"); -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp28 = while10(); -//│ tmp29 = tmp28 !== runtime.LoopEnd; -//│ if (tmp29 === true) { return tmp28 } else { return tmp27 } +//│ let f; +//│ f = function f(ls) {{ +//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ +//│ tmp27 = undefined; +//│ while10 = (undefined, function () {{ +//│ let tl, h, argument0$, argument1$; /** scoped **/ +//│ if (ls instanceof Cons.class) { +//│ argument0$ = ls.hd; +//│ argument1$ = ls.tl; +//│ tl = argument1$; +//│ h = argument0$; +//│ ls = tl; +//│ tmp27 = Predef.print(h); +//│ return while10() +//│ } else { +//│ tmp27 = Predef.print("Done!"); +//│ } +//│ return runtime.LoopEnd +//│ } +//│ }); +//│ tmp28 = while10(); +//│ tmp29 = tmp28 !== runtime.LoopEnd; +//│ if (tmp29 === true) { return tmp28 } else { return tmp27 } +//│ } //│ }; f(0) @@ -267,7 +274,7 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, tmp39, while11; /** scoped **/ +//│ let tmp37, tmp38, while11; //│ tmp37 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { @@ -297,10 +304,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.297: then 0(0) +//│ ║ l.304: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.296: while print("Hello World"); false +//│ ║ l.303: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -310,12 +317,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.309: while { print("Hello World"), false } +//│ ║ l.316: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.310: then 0(0) +//│ ║ l.317: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.311: else 1 +//│ ║ l.318: else 1 //│ ╙── ^^^^ :fixme @@ -325,14 +332,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.323: print("Hello World") +//│ ║ l.330: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.324: false +//│ ║ l.331: false //│ ║ ^^^^^^^^^ -//│ ║ l.325: then 0(0) +//│ ║ l.332: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.326: else 1 +//│ ║ l.333: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 9452424ea9..885a6dd1ed 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -101,7 +101,7 @@ class T class Foo(using T) with print(T) //│ JS (unsanitized): -//│ let Foo9; /** scoped **/ +//│ let Foo9; //│ Foo9 = function Foo(tmp5) { //│ return globalThis.Object.freeze(new Foo.class(tmp5)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index 23ed0cba02..131b3e3f4c 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -31,14 +31,14 @@ using Num = 0.42 :sjs let f1 = foo //│ JS (unsanitized): -//│ let f1; -//│ let f11; /** scoped **/ -//│ f1 = function f1() { -//│ let tmp; /** scoped **/ -//│ tmp = foo(); -//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) +//│ let f1, f11; +//│ f11 = function f1() {{ +//│ let tmp; /** scoped **/ +//│ tmp = foo(); +//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) +//│ } //│ }; -//│ f11 = f1; +//│ f1 = f11; //│ f1 = fun f1 :expect [42] @@ -53,14 +53,14 @@ f1() :sjs let f2 = bar //│ JS (unsanitized): -//│ let f2; -//│ let f21; /** scoped **/ -//│ f2 = function f2(x) { -//│ let tmp; /** scoped **/ -//│ tmp = bar(x); -//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) +//│ let f2, f21; +//│ f21 = function f2(x) {{ +//│ let tmp; /** scoped **/ +//│ tmp = bar(x); +//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) +//│ } //│ }; -//│ f21 = f2; +//│ f2 = f21; //│ f2 = fun f2 :expect [0.42, 42] @@ -75,20 +75,20 @@ f2(0.42) :sjs let f3 = baz //│ JS (unsanitized): -//│ let f3; -//│ let f31; /** scoped **/ -//│ f3 = function f3(x) { +//│ let f3, f31; +//│ f31 = function f3(x) { //│ let lambda1; -//│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1, tmp2; /** scoped **/ -//│ tmp = baz(x); -//│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); -//│ tmp2 = runtime.safeCall(tmp1(y)); -//│ return runtime.safeCall(tmp2(instance$Ident$_Num$_)) +//│ lambda1 = (undefined, function (y) {{ +//│ let tmp, tmp1, tmp2; /** scoped **/ +//│ tmp = baz(x); +//│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); +//│ tmp2 = runtime.safeCall(tmp1(y)); +//│ return runtime.safeCall(tmp2(instance$Ident$_Num$_)) +//│ } //│ }); //│ return lambda1 //│ }; -//│ f31 = f3; +//│ f3 = f31; //│ f3 = fun f3 :expect [43, 42, 44, 0.42] @@ -99,14 +99,14 @@ f3(43)(44) :sjs foo() //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) +//│ let tmp1; tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) //│ = [42] // This should not expand :sjs bar(0.42) //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) +//│ let tmp2; tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) //│ = [0.42, 42] @@ -115,7 +115,7 @@ module Test with fun test(j)(using Int) = 0 fun main(using Int) = test //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -130,10 +130,11 @@ module Test with //│ } //│ static main(tmp3) { //│ let lambda1; -//│ lambda1 = (undefined, function (j) { -//│ let tmp4; /** scoped **/ -//│ tmp4 = Test.test(j); -//│ return runtime.safeCall(tmp4(tmp3)) +//│ lambda1 = (undefined, function (j) {{ +//│ let tmp4; /** scoped **/ +//│ tmp4 = Test.test(j); +//│ return runtime.safeCall(tmp4(tmp3)) +//│ } //│ }); //│ return lambda1 //│ } diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index a58b71139f..482d1d6532 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) +//│ let tmp2; tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index 4cd471afe4..a51de360c7 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -17,8 +17,7 @@ fun f() = print of raiseUnhandledEffect() j / i //│ JS (unsanitized): -//│ let getLocals2; -//│ let f; /** scoped **/ +//│ let f, getLocals2; //│ getLocals2 = function getLocals() { //│ let prev, thisInfo, arr, tmp; //│ prev = []; @@ -28,91 +27,92 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let getLocals3, Cont$func$f$1, doUnwind; -//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ -//│ getLocals3 = function getLocals() { -//│ let i1, j1, k1, prev, thisInfo, arr, tmp2; -//│ prev = getLocals2(); -//│ i1 = new runtime.LocalVarInfo.class("i", i); -//│ j1 = new runtime.LocalVarInfo.class("j", j); -//│ k1 = new runtime.LocalVarInfo.class("k", k); -//│ arr = globalThis.Object.freeze([ -//│ i1, -//│ j1, -//│ k1 -//│ ]); -//│ thisInfo = new runtime.FnLocalsInfo.class("f", arr); -//│ tmp2 = runtime.safeCall(prev.push(thisInfo)); -//│ return prev -//│ }; -//│ globalThis.Object.freeze(class Cont$func$f$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$f$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp2; -//│ tmp2 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 1) { -//│ tmp = value$; -//│ } else if (this.pc === 2) { -//│ tmp1 = value$; +//│ let getLocals3, Cont$func$f$1, doUnwind;{ +//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ +//│ getLocals3 = function getLocals() { +//│ let i1, j1, k1, prev, thisInfo, arr, tmp2; +//│ prev = getLocals2(); +//│ i1 = new runtime.LocalVarInfo.class("i", i); +//│ j1 = new runtime.LocalVarInfo.class("j", j); +//│ k1 = new runtime.LocalVarInfo.class("k", k); +//│ arr = globalThis.Object.freeze([ +//│ i1, +//│ j1, +//│ k1 +//│ ]); +//│ thisInfo = new runtime.FnLocalsInfo.class("f", arr); +//│ tmp2 = runtime.safeCall(prev.push(thisInfo)); +//│ return prev +//│ }; +//│ globalThis.Object.freeze(class Cont$func$f$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$f$1 = this //│ } -//│ contLoop: while (true) { -//│ if (this.pc === 3) { -//│ return j / i -//│ } else if (this.pc === 4) { -//│ tmp1 = Predef.print(tmp); -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return this.doUnwind(tmp1, 2) +//│ constructor(pc) { +//│ let tmp2; +//│ tmp2 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 1) { +//│ tmp = value$; +//│ } else if (this.pc === 2) { +//│ tmp1 = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 3) { +//│ return j / i +//│ } else if (this.pc === 4) { +//│ tmp1 = Predef.print(tmp); +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return this.doUnwind(tmp1, 2) +//│ } +//│ this.pc = 2; +//│ continue contLoop +//│ } else if (this.pc === 1) { +//│ this.pc = 4; +//│ continue contLoop +//│ } else if (this.pc === 2) { +//│ this.pc = 3; +//│ continue contLoop //│ } -//│ this.pc = 2; -//│ continue contLoop -//│ } else if (this.pc === 1) { -//│ this.pc = 4; -//│ continue contLoop +//│ break; +//│ } +//│ } +//│ get getLocals() { +//│ return getLocals3(); +//│ } +//│ get getLoc() { +//│ if (this.pc === 1) { +//│ return "Debugging.mls:17:14" //│ } else if (this.pc === 2) { -//│ this.pc = 3; -//│ continue contLoop +//│ return "Debugging.mls:17:5" //│ } -//│ break; //│ } -//│ } -//│ get getLocals() { -//│ return getLocals3(); -//│ } -//│ get getLoc() { -//│ if (this.pc === 1) { -//│ return "Debugging.mls:17:14" -//│ } else if (this.pc === 2) { -//│ return "Debugging.mls:17:5" +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$f$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$func$f$1(pc); +//│ res1.contTrace.last = res1.contTrace.last.next; +//│ return res1 +//│ }; +//│ i = 0; +//│ j = 100; +//│ k = 2000; +//│ scrut = i == 0; +//│ if (scrut === true) { +//│ tmp = Predef.raiseUnhandledEffect(); +//│ if (tmp instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp, 1) +//│ } +//│ tmp1 = Predef.print(tmp); +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp1, 2) //│ } -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$f$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$func$f$1(pc); -//│ res1.contTrace.last = res1.contTrace.last.next; -//│ return res1 -//│ }; -//│ i = 0; -//│ j = 100; -//│ k = 2000; -//│ scrut = i == 0; -//│ if (scrut === true) { -//│ tmp = Predef.raiseUnhandledEffect(); -//│ if (tmp instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp, 1) -//│ } -//│ tmp1 = Predef.print(tmp); -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp1, 2) -//│ } -//│ } else { tmp1 = runtime.Unit; } -//│ return j / i +//│ } else { tmp1 = runtime.Unit; } +//│ return j / i +//│ } //│ }; :re diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 4b3fa31cce..f70c9ad163 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,69 +156,69 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let handleBlock$11; -//│ let tmp20; /** scoped **/ +//│ let tmp20, handleBlock$11; //│ handleBlock$11 = function handleBlock$() { -//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; -//│ let f, scrut; /** scoped **/ -//│ globalThis.Object.freeze(class Handler$h$11 extends Effect { -//│ static { -//│ Handler$h$12 = this -//│ } -//│ constructor() { -//│ let tmp21; -//│ tmp21 = super(); -//│ } -//│ perform(arg) { -//│ let hdlrFun; -//│ hdlrFun = function hdlrFun(k) { -//│ return arg -//│ }; -//│ return runtime.mkEffect(this, hdlrFun) -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Handler$h$"]; -//│ }); -//│ h = new Handler$h$12(); -//│ globalThis.Object.freeze(class Cont$handleBlock$h$10 extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handleBlock$h$11 = this -//│ } -//│ constructor(pc) { -//│ let tmp21; -//│ tmp21 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 1) { -//│ res = value$; +//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12;{ +//│ let f, scrut; /** scoped **/ +//│ globalThis.Object.freeze(class Handler$h$11 extends Effect { +//│ static { +//│ Handler$h$12 = this +//│ } +//│ constructor() { +//│ let tmp21; +//│ tmp21 = super(); //│ } -//│ contLoop: while (true) { +//│ perform(arg) { +//│ let hdlrFun; +//│ hdlrFun = function hdlrFun(k) { +//│ return arg +//│ }; +//│ return runtime.mkEffect(this, hdlrFun) +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Handler$h$"]; +//│ }); +//│ h = new Handler$h$12(); +//│ globalThis.Object.freeze(class Cont$handleBlock$h$10 extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handleBlock$h$11 = this +//│ } +//│ constructor(pc) { +//│ let tmp21; +//│ tmp21 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { //│ if (this.pc === 1) { -//│ return res +//│ res = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 1) { +//│ return res +//│ } +//│ break; //│ } -//│ break; //│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handleBlock$h$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$handleBlock$h$11(pc); +//│ return runtime.handleBlockImpl(res1, h) +//│ }; +//│ f = function f() { +//│ return 3 +//│ }; +//│ scrut = true; +//│ if (scrut === true) { +//│ res = f(); +//│ if (res instanceof runtime.EffectSig.class) { +//│ return doUnwind(res, 1) +//│ } +//│ return res +//│ } else { +//│ return runtime.Unit //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handleBlock$h$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$handleBlock$h$11(pc); -//│ return runtime.handleBlockImpl(res1, h) -//│ }; -//│ f = function f() { -//│ return 3 -//│ }; -//│ scrut = true; -//│ if (scrut === true) { -//│ res = f(); -//│ if (res instanceof runtime.EffectSig.class) { -//│ return doUnwind(res, 1) -//│ } -//│ return res -//│ } else { -//│ return runtime.Unit //│ } //│ }; //│ tmp20 = handleBlock$11(); diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index 3b0ece8c6e..32c8b1fed1 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -13,31 +13,32 @@ fun foo(h): module M = module A A //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo(h) { -//│ let A2, A3, scrut; /** scoped **/ -//│ globalThis.Object.freeze(class A { -//│ static { -//│ A2 = this -//│ } -//│ constructor() { -//│ runtime.Unit; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A"]; -//│ }); -//│ globalThis.Object.freeze(class A1 { -//│ static { -//│ A3 = this -//│ } -//│ constructor() { -//│ runtime.Unit; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A"]; -//│ }); -//│ scrut = false; -//│ if (scrut === true) { return A2 } else { return A3 } +//│ let foo; +//│ foo = function foo(h) {{ +//│ let A2, A3, scrut; /** scoped **/ +//│ globalThis.Object.freeze(class A { +//│ static { +//│ A2 = this +//│ } +//│ constructor() { +//│ runtime.Unit; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); +//│ globalThis.Object.freeze(class A1 { +//│ static { +//│ A3 = this +//│ } +//│ constructor() { +//│ runtime.Unit; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); +//│ scrut = false; +//│ if (scrut === true) { return A2 } else { return A3 } +//│ } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 72893cfea0..fbef803763 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,7 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1, tmp; /** scoped **/ +//│ let Lol1; //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; @@ -19,7 +19,7 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { -//│ let res, Cont$ctor$Lol$1, doUnwind; +//│ let tmp, res, Cont$ctor$Lol$1, doUnwind; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index be8ffedbb6..7f1b7dcb5d 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,65 +155,65 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let handleBlock$7; -//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; /** scoped **/ +//│ let str, scrut, tmp24, tmp25, handleBlock$7; //│ str = ""; //│ scrut = true; //│ if (scrut === true) { //│ handleBlock$7 = function handleBlock$() { -//│ let h1, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; +//│ let h1, tmp26, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; //│ globalThis.Object.freeze(class Handler$h1$1 extends Effect { //│ static { //│ Handler$h1$2 = this //│ } //│ constructor() { -//│ let tmp28; -//│ tmp28 = super(); +//│ let tmp27; +//│ tmp27 = super(); //│ } //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let Cont$handler$h1$perform$2, doUnwind1; -//│ let tmp28, tmp29, tmp30; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handler$h1$perform$2 = this -//│ } -//│ constructor(pc) { -//│ let tmp31; -//│ tmp31 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 7) { -//│ tmp29 = value$; +//│ let Cont$handler$h1$perform$2, doUnwind1;{ +//│ let tmp27, tmp28, tmp29; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handler$h1$perform$2 = this //│ } -//│ contLoop: while (true) { +//│ constructor(pc) { +//│ let tmp30; +//│ tmp30 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { //│ if (this.pc === 7) { -//│ tmp30 = str + "A"; -//│ str = tmp30; -//│ return runtime.Unit +//│ tmp28 = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 7) { +//│ tmp29 = str + "A"; +//│ str = tmp29; +//│ return runtime.Unit +//│ } +//│ break; //│ } -//│ break; //│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handler$h1$perform$"]; +//│ }); +//│ doUnwind1 = function doUnwind(res7, pc) { +//│ res7.contTrace.last.next = new Cont$handler$h1$perform$2(pc); +//│ res7.contTrace.last = res7.contTrace.last.next; +//│ return res7 +//│ }; +//│ tmp27 = str + "A"; +//│ str = tmp27; +//│ tmp28 = runtime.safeCall(k(arg)); +//│ if (tmp28 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp28, 7) //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handler$h1$perform$"]; -//│ }); -//│ doUnwind1 = function doUnwind(res7, pc) { -//│ res7.contTrace.last.next = new Cont$handler$h1$perform$2(pc); -//│ res7.contTrace.last = res7.contTrace.last.next; -//│ return res7 -//│ }; -//│ tmp28 = str + "A"; -//│ str = tmp28; -//│ tmp29 = runtime.safeCall(k(arg)); -//│ if (tmp29 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp29, 7) +//│ tmp29 = str + "A"; +//│ str = tmp29; +//│ return runtime.Unit //│ } -//│ tmp30 = str + "A"; -//│ str = tmp30; -//│ return runtime.Unit //│ }; //│ return runtime.mkEffect(this, hdlrFun) //│ } @@ -226,17 +226,17 @@ str //│ Cont$handleBlock$h1$2 = this //│ } //│ constructor(pc) { -//│ let tmp28; -//│ tmp28 = super(null); +//│ let tmp27; +//│ tmp27 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 6) { -//│ tmp25 = value$; +//│ tmp26 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 6) { -//│ return tmp25 +//│ return tmp26 //│ } //│ break; //│ } @@ -249,7 +249,7 @@ str //│ return runtime.handleBlockImpl(res7, h1) //│ }; //│ handleBlock$8 = function handleBlock$() { -//│ let h2, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; +//│ let h2, tmp27, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; //│ globalThis.Object.freeze(class Handler$h2$1 extends Effect { //│ static { //│ Handler$h2$2 = this @@ -261,50 +261,51 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let Cont$handler$h2$perform$2, doUnwind2; -//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handler$h2$perform$2 = this -//│ } -//│ constructor(pc) { -//│ let tmp33; -//│ tmp33 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 3) { -//│ tmp30 = value$; +//│ let Cont$handler$h2$perform$2, doUnwind2;{ +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handler$h2$perform$2 = this //│ } -//│ contLoop: while (true) { +//│ constructor(pc) { +//│ let tmp33; +//│ tmp33 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { //│ if (this.pc === 3) { -//│ tmp31 = str + "B"; -//│ tmp32 = str + tmp31; -//│ str = tmp32; -//│ return runtime.Unit +//│ tmp30 = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 3) { +//│ tmp31 = str + "B"; +//│ tmp32 = str + tmp31; +//│ str = tmp32; +//│ return runtime.Unit +//│ } +//│ break; //│ } -//│ break; //│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handler$h2$perform$"]; +//│ }); +//│ doUnwind2 = function doUnwind(res8, pc) { +//│ res8.contTrace.last.next = new Cont$handler$h2$perform$2(pc); +//│ res8.contTrace.last = res8.contTrace.last.next; +//│ return res8 +//│ }; +//│ tmp28 = str + "B"; +//│ tmp29 = str + tmp28; +//│ str = tmp29; +//│ tmp30 = runtime.safeCall(k(arg)); +//│ if (tmp30 instanceof runtime.EffectSig.class) { +//│ return doUnwind2(tmp30, 3) //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handler$h2$perform$"]; -//│ }); -//│ doUnwind2 = function doUnwind(res8, pc) { -//│ res8.contTrace.last.next = new Cont$handler$h2$perform$2(pc); -//│ res8.contTrace.last = res8.contTrace.last.next; -//│ return res8 -//│ }; -//│ tmp28 = str + "B"; -//│ tmp29 = str + tmp28; -//│ str = tmp29; -//│ tmp30 = runtime.safeCall(k(arg)); -//│ if (tmp30 instanceof runtime.EffectSig.class) { -//│ return doUnwind2(tmp30, 3) +//│ tmp31 = str + "B"; +//│ tmp32 = str + tmp31; +//│ str = tmp32; +//│ return runtime.Unit //│ } -//│ tmp31 = str + "B"; -//│ tmp32 = str + tmp31; -//│ str = tmp32; -//│ return runtime.Unit //│ }; //│ return runtime.mkEffect(this, hdlrFun) //│ } @@ -323,7 +324,7 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 1) { -//│ tmp26 = value$; +//│ tmp27 = value$; //│ } else if (this.pc === 2) { //│ res7 = value$; //│ } @@ -348,9 +349,9 @@ str //│ res8.contTrace.last.next = new Cont$handleBlock$h2$2(pc); //│ return runtime.handleBlockImpl(res8, h2) //│ }; -//│ tmp26 = runtime.safeCall(h2.perform(runtime.Unit)); -//│ if (tmp26 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp26, 1) +//│ tmp27 = runtime.safeCall(h2.perform(runtime.Unit)); +//│ if (tmp27 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp27, 1) //│ } //│ res7 = runtime.safeCall(h1.perform(runtime.Unit)); //│ if (res7 instanceof runtime.EffectSig.class) { @@ -358,18 +359,18 @@ str //│ } //│ return res7 //│ }; -//│ tmp25 = handleBlock$8(); -//│ if (tmp25 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp25, 6) +//│ tmp26 = handleBlock$8(); +//│ if (tmp26 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp26, 6) //│ } -//│ return tmp25 +//│ return tmp26 //│ }; //│ tmp24 = handleBlock$7(); //│ if (tmp24 instanceof runtime.EffectSig.class) { //│ tmp24 = runtime.topLevelEffect(tmp24, false); //│ } -//│ tmp27 = tmp24; -//│ } else { tmp27 = runtime.Unit; } +//│ tmp25 = tmp24; +//│ } else { tmp25 = runtime.Unit; } //│ str //│ = "BABABA" //│ str = "BABABA" diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 6dd7795543..722499592a 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,7 +8,7 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let x, old, tmp, tmp1, tmp2; /** scoped **/ +//│ let x, old, tmp, tmp1, tmp2; //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 688ae8e6d2..24ec00ee32 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -20,42 +20,42 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let res, $_stack$_safe$_body$_; -//│ let hi; /** scoped **/ +//│ let hi, res, $_stack$_safe$_body$_; //│ hi = function hi(n) { -//│ let Cont$func$hi$1, doUnwind, stackDelayRes; -//│ let scrut, tmp; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$hi$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp1; -//│ tmp1 = super(null); -//│ this.pc = pc; +//│ let Cont$func$hi$1, doUnwind, stackDelayRes;{ +//│ let scrut, tmp; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$hi$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp1; +//│ tmp1 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ return hi(n) +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$hi$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$func$hi$1(pc); +//│ res1.contTrace.last = res1.contTrace.last.next; +//│ return res1 +//│ }; +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind(stackDelayRes, 0) //│ } -//│ resume(value$) { -//│ return hi(n) +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ return hi(tmp) //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$hi$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$func$hi$1(pc); -//│ res1.contTrace.last = res1.contTrace.last.next; -//│ return res1 -//│ }; -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind(stackDelayRes, 0) -//│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ return hi(tmp) //│ } //│ }; //│ $_stack$_safe$_body$_ = (undefined, function () { @@ -76,61 +76,61 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let res1, $_stack$_safe$_body$_1; -//│ let sum1; /** scoped **/ +//│ let sum1, res1, $_stack$_safe$_body$_1; //│ sum1 = function sum(n) { -//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$sum$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp2; -//│ tmp2 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ let curDepth1; -//│ curDepth1 = runtime.stackDepth; -//│ if (this.pc === 1) { -//│ tmp1 = value$; +//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes;{ +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$sum$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp2; +//│ tmp2 = super(null); +//│ this.pc = pc; //│ } -//│ contLoop: while (true) { -//│ runtime.stackDepth = curDepth1; -//│ if (this.pc === 0) { -//│ return sum1(n) -//│ } else if (this.pc === 1) { -//│ return n + tmp1 +//│ resume(value$) { +//│ let curDepth1; +//│ curDepth1 = runtime.stackDepth; +//│ if (this.pc === 1) { +//│ tmp1 = value$; +//│ } +//│ contLoop: while (true) { +//│ runtime.stackDepth = curDepth1; +//│ if (this.pc === 0) { +//│ return sum1(n) +//│ } else if (this.pc === 1) { +//│ return n + tmp1 +//│ } +//│ break; //│ } -//│ break; //│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$sum$"]; +//│ }); +//│ doUnwind = function doUnwind(res2, pc) { +//│ res2.contTrace.last.next = new Cont$func$sum$1(pc); +//│ res2.contTrace.last = res2.contTrace.last.next; +//│ return res2 +//│ }; +//│ curDepth = runtime.stackDepth; +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind(stackDelayRes, 0) //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$sum$"]; -//│ }); -//│ doUnwind = function doUnwind(res2, pc) { -//│ res2.contTrace.last.next = new Cont$func$sum$1(pc); -//│ res2.contTrace.last = res2.contTrace.last.next; -//│ return res2 -//│ }; -//│ curDepth = runtime.stackDepth; -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind(stackDelayRes, 0) -//│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ tmp1 = sum1(tmp); -//│ runtime.stackDepth = curDepth; -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp1, 1) +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ tmp1 = sum1(tmp); +//│ runtime.stackDepth = curDepth; +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp1, 1) +//│ } +//│ return n + tmp1 //│ } -//│ return n + tmp1 //│ } //│ }; //│ $_stack$_safe$_body$_1 = (undefined, function () { @@ -260,11 +260,12 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; /** scoped **/ -//│ max = function max(a, b) { -//│ let scrut; /** scoped **/ -//│ scrut = a < b; -//│ if (scrut === true) { return b } else { return a } +//│ let max; +//│ max = function max(a, b) {{ +//│ let scrut; /** scoped **/ +//│ scrut = a < b; +//│ if (scrut === true) { return b } else { return a } +//│ } //│ }; @@ -274,7 +275,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls index a1596055ef..76a8e46f8f 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls @@ -13,7 +13,7 @@ arr.map(_ === false) :sjs arr.map((e, ...) => e === false) //│ JS (unsanitized): -//│ let lambda1; /** scoped **/ +//│ let lambda1; //│ lambda1 = (undefined, function (e, ..._) { return e === false }); //│ runtime.safeCall(arr.map(lambda1)) //│ = [false, true] @@ -21,7 +21,7 @@ arr.map((e, ...) => e === false) :sjs arr.map((_, ...) => 1) //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ +//│ let lambda2; //│ lambda2 = (undefined, function (_, ..._1) { return 1 }); //│ runtime.safeCall(arr.map(lambda2)) //│ = [1, 1] diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 21b5a1233e..adf92d3828 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,8 +88,7 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let Bad1, Good1, Bad$, Good$, f$capture3; -//│ let f5, tmp9; /** scoped **/ +//│ let Bad1, Good1, f5, tmp9, Bad$, Good$, f$capture3; //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; //│ if (isMut === true) { @@ -130,12 +129,13 @@ f().foo() //│ set y(value) { this.#y = value; } //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } -//│ foo() { -//│ let tmp10, tmp11; /** scoped **/ -//│ this.z = 100; -//│ tmp10 = this.x + this.y; -//│ tmp11 = tmp10 + this.z; -//│ return tmp11 + this.f$capture.w$capture$0 +//│ foo() {{ +//│ let tmp10, tmp11; /** scoped **/ +//│ this.z = 100; +//│ tmp10 = this.x + this.y; +//│ tmp11 = tmp10 + this.z; +//│ return tmp11 + this.f$capture.w$capture$0 +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Good", []]; @@ -186,16 +186,17 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let capture; -//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ -//│ capture = new f$capture3(null); -//│ x = 1; -//│ y = 10; -//│ z = 10; -//│ capture.w$capture$0 = 1000; -//│ tmp10 = Bad$(false, capture); -//│ tmp11 = tmp10.foo(); -//│ return Good$(false, x, y, z, capture) +//│ let capture;{ +//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ +//│ capture = new f$capture3(null); +//│ x = 1; +//│ y = 10; +//│ z = 10; +//│ capture.w$capture$0 = 1000; +//│ tmp10 = Bad$(false, capture); +//│ tmp11 = tmp10.foo(); +//│ return Good$(false, x, y, z, capture) +//│ } //│ }; //│ tmp9 = f5(); //│ runtime.safeCall(tmp9.foo()) diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index 9be85d298a..cf0ee14bd2 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -11,25 +11,25 @@ fun f(x) = fun g = new A //│ ═══[WARNING] Modules are not yet lifted. //│ JS (unsanitized): -//│ let g$; -//│ let f; /** scoped **/ +//│ let f, g$; //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; -//│ f = function f(x) { -//│ let A1, g; /** scoped **/ -//│ globalThis.Object.freeze(class A { -//│ static { -//│ A1 = this -//│ } -//│ constructor() {} -//│ get get() { -//│ return x; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A"]; -//│ }); -//│ return runtime.Unit +//│ f = function f(x) {{ +//│ let A1, g; /** scoped **/ +//│ globalThis.Object.freeze(class A { +//│ static { +//│ A1 = this +//│ } +//│ constructor() {} +//│ get get() { +//│ return x; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); +//│ return runtime.Unit +//│ } //│ }; //│ ═══[WARNING] Modules are not yet lifted. diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 2bce7ec269..8b6880737c 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -7,8 +7,7 @@ data class A(x) with fun getB() = x + y fun getA() = B(2).getB() //│ JS (unsanitized): -//│ let B1, B$; -//│ let A1; /** scoped **/ +//│ let B1, A1, B$; //│ B$ = function B$(isMut, A$instance, y) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -54,10 +53,11 @@ data class A(x) with //│ constructor(x) { //│ this.x = x; //│ } -//│ getA() { -//│ let tmp; /** scoped **/ -//│ tmp = B$(false, this, 2); -//│ return tmp.getB() +//│ getA() {{ +//│ let tmp; /** scoped **/ +//│ tmp = B$(false, this, 2); +//│ return tmp.getB() +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A", ["x"]]; @@ -74,8 +74,7 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let g, g$; -//│ let A3, tmp1; /** scoped **/ +//│ let g, A3, tmp1, g$; //│ g$ = function g$(A$instance) { //│ return 2 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index 3c83263869..c5e0081c81 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -47,12 +47,12 @@ fun f(used1, unused1) = foo(used2) + unused2 f(1, 2) //│ JS (unsanitized): -//│ let g1, g$3; -//│ let f3; /** scoped **/ -//│ g$3 = function g$(used1, used2, g_arg) { -//│ let used3; /** scoped **/ -//│ used3 = 2; -//│ return used1 + used2 +//│ let g1, f3, g$3; +//│ g$3 = function g$(used1, used2, g_arg) {{ +//│ let used3; /** scoped **/ +//│ used3 = 2; +//│ return used1 + used2 +//│ } //│ }; //│ g1 = function g(used1, used2) { //│ return (g_arg) => { @@ -60,14 +60,15 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let g$here; -//│ let used2, unused2, foo, tmp; /** scoped **/ -//│ used2 = unused1; -//│ unused2 = 2; -//│ g$here = runtime.safeCall(g1(used1, used2)); -//│ foo = g$here; -//│ tmp = runtime.safeCall(foo(used2)); -//│ return tmp + unused2 +//│ let g$here;{ +//│ let used2, unused2, foo, tmp; /** scoped **/ +//│ used2 = unused1; +//│ unused2 = 2; +//│ g$here = runtime.safeCall(g1(used1, used2)); +//│ foo = g$here; +//│ tmp = runtime.safeCall(foo(used2)); +//│ return tmp + unused2 +//│ } //│ }; //│ f3(1, 2) //│ = 5 @@ -79,20 +80,21 @@ fun f(a1, a2, a3, a4, a5, a6) = g f(1,2,3,4,5,6) //│ JS (unsanitized): -//│ let g$4; -//│ let f4; /** scoped **/ -//│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ tmp = a1 + a2; -//│ tmp1 = tmp + a3; -//│ tmp2 = tmp1 + a4; -//│ tmp3 = tmp2 + a5; -//│ return tmp3 + a6 +//│ let f4, g$4; +//│ g$4 = function g$(a1, a2, a3, a4, a5, a6) {{ +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ tmp = a1 + a2; +//│ tmp1 = tmp + a3; +//│ tmp2 = tmp1 + a4; +//│ tmp3 = tmp2 + a5; +//│ return tmp3 + a6 +//│ } //│ }; -//│ f4 = function f(a1, a2, a3, a4, a5, a6) { -//│ let g2, tmp; /** scoped **/ -//│ tmp = g$4(a1, a2, a3, a4, a5, a6); -//│ return tmp +//│ f4 = function f(a1, a2, a3, a4, a5, a6) {{ +//│ let g2, tmp; /** scoped **/ +//│ tmp = g$4(a1, a2, a3, a4, a5, a6); +//│ return tmp +//│ } //│ }; //│ f4(1, 2, 3, 4, 5, 6) //│ = 21 @@ -165,8 +167,7 @@ fun f(unused, immutable, mutated) = a + h() + unused f(1, 2, 1000) //│ JS (unsanitized): -//│ let h$2, g$6, f$capture5; -//│ let f7; /** scoped **/ +//│ let f7, h$2, g$6, f$capture5; //│ g$6 = function g$(immutable, f$capture6) { //│ f$capture6.mutated$capture$0 = 2; //│ return immutable + f$capture6.mutated$capture$0 @@ -185,13 +186,14 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let capture; -//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ -//│ capture = new f$capture5(mutated); -//│ a1 = g$6(immutable, capture); -//│ tmp7 = h$2(capture); -//│ tmp8 = a1 + tmp7; -//│ return tmp8 + unused +//│ let capture;{ +//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ +//│ capture = new f$capture5(mutated); +//│ a1 = g$6(immutable, capture); +//│ tmp7 = h$2(capture); +//│ tmp8 = a1 + tmp7; +//│ return tmp8 + unused +//│ } //│ }; //│ f7(1, 2, 1000) //│ = 7 @@ -256,18 +258,18 @@ fun g() = f g()(1) //│ JS (unsanitized): -//│ let f14, f$1, h$4, g$capture1; -//│ let g6, tmp7; /** scoped **/ +//│ let f14, g6, tmp7, f$1, h$4, g$capture1; //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; //│ x1 = 4; //│ return x1 + g$capture2.y$capture$0 //│ }; -//│ f$1 = function f$(g$capture2, x1) { -//│ let h3, k; /** scoped **/ -//│ k = 4; -//│ g$capture2.y$capture$0 = 2; -//│ return x1 +//│ f$1 = function f$(g$capture2, x1) {{ +//│ let h3, k; /** scoped **/ +//│ k = 4; +//│ g$capture2.y$capture$0 = 2; +//│ return x1 +//│ } //│ }; //│ f14 = function f(g$capture2) { //│ return (x1) => { @@ -285,12 +287,13 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { -//│ let capture, f$here; -//│ let y1; /** scoped **/ -//│ capture = new g$capture1(null); -//│ capture.y$capture$0 = 0; -//│ f$here = runtime.safeCall(f14(capture)); -//│ return f$here +//│ let capture, f$here;{ +//│ let y1; /** scoped **/ +//│ capture = new g$capture1(null); +//│ capture.y$capture$0 = 0; +//│ f$here = runtime.safeCall(f14(capture)); +//│ return f$here +//│ } //│ }; //│ tmp7 = g6(); //│ runtime.safeCall(tmp7(1)) @@ -377,8 +380,7 @@ fun f(x) = set y = 2 [g, g] //│ JS (unsanitized): -//│ let g12, g$14; -//│ let f23; /** scoped **/ +//│ let g12, f23, g$14; //│ g$14 = function g$(y1) { //│ return y1 //│ }; @@ -388,21 +390,22 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let g$here; -//│ let y1, scrut; /** scoped **/ -//│ y1 = undefined; -//│ scrut = x1 < 0; -//│ if (scrut === true) { -//│ y1 = 1; -//│ g$here = runtime.safeCall(g12(y1)); -//│ return globalThis.Object.freeze([ -//│ g$here, -//│ g$here -//│ ]) -//│ } else { -//│ y1 = 2; -//│ g$here = runtime.safeCall(g12(y1)); -//│ return globalThis.Object.freeze([ g$here, g$here ]) +//│ let g$here;{ +//│ let y1, scrut; /** scoped **/ +//│ y1 = undefined; +//│ scrut = x1 < 0; +//│ if (scrut === true) { +//│ y1 = 1; +//│ g$here = runtime.safeCall(g12(y1)); +//│ return globalThis.Object.freeze([ +//│ g$here, +//│ g$here +//│ ]) +//│ } else { +//│ y1 = 2; +//│ g$here = runtime.safeCall(g12(y1)); +//│ return globalThis.Object.freeze([ g$here, g$here ]) +//│ } //│ } //│ }; @@ -413,8 +416,7 @@ fun f(x) = set x += 1 [a, g] //│ JS (unsanitized): -//│ let g13, g$15, f$capture17; -//│ let f24; /** scoped **/ +//│ let g13, f24, g$15, f$capture17; //│ g$15 = function g$(f$capture18) { //│ return f$capture18.x$capture$0 //│ }; @@ -434,15 +436,16 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let capture, g$here; -//│ let a1, tmp10; /** scoped **/ -//│ capture = new f$capture17(x1); -//│ g$here = runtime.safeCall(g13(capture)); -//│ a1 = g$here; -//│ tmp10 = capture.x$capture$0 + 1; -//│ capture.x$capture$0 = tmp10; -//│ g$here = runtime.safeCall(g13(capture)); -//│ return globalThis.Object.freeze([ a1, g$here ]) +//│ let capture, g$here;{ +//│ let a1, tmp10; /** scoped **/ +//│ capture = new f$capture17(x1); +//│ g$here = runtime.safeCall(g13(capture)); +//│ a1 = g$here; +//│ tmp10 = capture.x$capture$0 + 1; +//│ capture.x$capture$0 = tmp10; +//│ g$here = runtime.safeCall(g13(capture)); +//│ return globalThis.Object.freeze([ a1, g$here ]) +//│ } //│ }; // Can be avoided in `h` by passing the initialized closure, but this is more complicated diff --git a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls index 581a224319..d5bc28e321 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls @@ -7,7 +7,7 @@ import "../../mlscript-compile/Option.mls" module A with fun f(x) = x is Option.Some //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 4931b1249b..74979d2b2d 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -45,8 +45,7 @@ fun foo() = set x += 1 return () => x //│ JS (unsanitized): -//│ let lambda2, while$1, lambda$2, foo$capture5; -//│ let foo2; /** scoped **/ +//│ let foo2, lambda2, while$1, lambda$2, foo$capture5; //│ lambda$2 = function lambda$(foo$capture6) { //│ return foo$capture6.x$capture$0 //│ }; @@ -56,18 +55,19 @@ fun foo() = //│ } //│ }); //│ while$1 = function while$(foo$capture6) { -//│ let lambda$here; -//│ let scrut, tmp2; /** scoped **/ -//│ scrut = true; -//│ if (scrut === true) { -//│ tmp2 = foo$capture6.x$capture$0 + 1; -//│ foo$capture6.x$capture$0 = tmp2; -//│ lambda$here = runtime.safeCall(lambda2(foo$capture6)); -//│ return lambda$here -//│ } else { -//│ foo$capture6.tmp$capture$1 = runtime.Unit; +//│ let lambda$here;{ +//│ let scrut, tmp2; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { +//│ tmp2 = foo$capture6.x$capture$0 + 1; +//│ foo$capture6.x$capture$0 = tmp2; +//│ lambda$here = runtime.safeCall(lambda2(foo$capture6)); +//│ return lambda$here +//│ } else { +//│ foo$capture6.tmp$capture$1 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd //│ } -//│ return runtime.LoopEnd //│ }; //│ globalThis.Object.freeze(class foo$capture4 { //│ static { @@ -81,14 +81,15 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo2 = function foo() { -//│ let capture; -//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ -//│ capture = new foo$capture5(null, null); -//│ capture.x$capture$0 = 1; -//│ capture.tmp$capture$1 = undefined; -//│ tmp3 = while$1(capture); -//│ tmp4 = tmp3 !== runtime.LoopEnd; -//│ if (tmp4 === true) { return tmp3 } else { return capture.tmp$capture$1 } +//│ let capture;{ +//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ +//│ capture = new foo$capture5(null, null); +//│ capture.x$capture$0 = 1; +//│ capture.tmp$capture$1 = undefined; +//│ tmp3 = while$1(capture); +//│ tmp4 = tmp3 !== runtime.LoopEnd; +//│ if (tmp4 === true) { return tmp3 } else { return capture.tmp$capture$1 } +//│ } //│ }; :expect 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index c09ff52620..50f1da26aa 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -27,8 +27,7 @@ fun foo(y) = (new M).foo() foo(10) //│ JS (unsanitized): -//│ let M3, M$; -//│ let foo1; /** scoped **/ +//│ let M3, foo1, M$; //│ M$ = function M$(isMut, y) { //│ let tmp; //│ if (isMut === true) { @@ -55,7 +54,7 @@ foo(10) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ foo1 = function foo(y) { let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() }; +//│ foo1 = function foo(y) {{ let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() } }; //│ foo1(10) @@ -110,8 +109,7 @@ fun foo(x, y) = fun foo3 = M.foo2() foo3 //│ JS (unsanitized): -//│ let M5, foo3$, foo$capture3; -//│ let foo4; /** scoped **/ +//│ let M5, foo4, foo3$, foo$capture3; //│ foo3$ = function foo3$(M6, x, foo$capture4) { //│ return M6.foo2() //│ }; @@ -147,12 +145,13 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let M$1, capture; -//│ let foo31, tmp; /** scoped **/ -//│ capture = new foo$capture3(y); -//│ M$1 = globalThis.Object.freeze(new M5(x, capture)); -//│ tmp = foo3$(M$1, x, capture); -//│ return tmp +//│ let M$1, capture;{ +//│ let foo31, tmp; /** scoped **/ +//│ capture = new foo$capture3(y); +//│ M$1 = globalThis.Object.freeze(new M5(x, capture)); +//│ tmp = foo3$(M$1, x, capture); +//│ return tmp +//│ } //│ }; :expect 12 @@ -214,7 +213,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp5, tmp6; /** scoped **/ +//│ let M17, tmp5; //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -223,34 +222,36 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut; /** scoped **/ -//│ this.A = function A() { -//│ return globalThis.Object.freeze(new A.class()); -//│ }; -//│ globalThis.Object.freeze(class A5 { -//│ static { -//│ M16.A.class = this -//│ } -//│ constructor() {} -//│ get get() { -//│ return M17.x + M16.x; +//│ let tmp6;{ +//│ let scrut; /** scoped **/ +//│ this.A = function A() { +//│ return globalThis.Object.freeze(new A.class()); +//│ }; +//│ globalThis.Object.freeze(class A5 { +//│ static { +//│ M16.A.class = this +//│ } +//│ constructor() {} +//│ get get() { +//│ return M17.x + M16.x; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A", []]; +//│ }); +//│ scrut = M16.A(); +//│ if (scrut instanceof M16.A.class) { +//│ tmp6 = 2; +//│ } else { +//│ tmp6 = 3; //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A", []]; -//│ }); -//│ scrut = M16.A(); -//│ if (scrut instanceof M16.A.class) { -//│ tmp5 = 2; -//│ } else { -//│ tmp5 = 3; +//│ this.x = tmp6; //│ } -//│ this.x = tmp5; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp6 = M17.A(); -//│ tmp6.get +//│ tmp5 = M17.A(); +//│ tmp5.get //│ = 4 module M with diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index fae5e2cb30..8179e3081c 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -12,8 +12,7 @@ fun foo() = xs.push(bar) set x = 2 //│ JS (unsanitized): -//│ let bar, bar$, foo$capture1; -//│ let foo; /** scoped **/ +//│ let bar, foo, bar$, foo$capture1; //│ bar$ = function bar$(foo$capture2) { //│ return foo$capture2.x$capture$0 //│ }; @@ -33,14 +32,15 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let capture, bar$here; -//│ let x, tmp; /** scoped **/ -//│ capture = new foo$capture1(null); -//│ capture.x$capture$0 = 1; -//│ bar$here = runtime.safeCall(bar(capture)); -//│ tmp = runtime.safeCall(xs.push(bar$here)); -//│ capture.x$capture$0 = 2; -//│ return runtime.Unit +//│ let capture, bar$here;{ +//│ let x, tmp; /** scoped **/ +//│ capture = new foo$capture1(null); +//│ capture.x$capture$0 = 1; +//│ bar$here = runtime.safeCall(bar(capture)); +//│ tmp = runtime.safeCall(xs.push(bar$here)); +//│ capture.x$capture$0 = 2; +//│ return runtime.Unit +//│ } //│ }; :expect 2 @@ -86,13 +86,13 @@ fun foo() = x bar //│ JS (unsanitized): -//│ let bar3; -//│ let foo3; /** scoped **/ -//│ bar3 = function bar() { -//│ let x; /** scoped **/ -//│ x = undefined; -//│ return x +//│ let bar3, foo3; +//│ bar3 = function bar() {{ +//│ let x; /** scoped **/ +//│ x = undefined; +//│ return x +//│ } //│ }; -//│ foo3 = function foo() { return bar3 }; +//│ foo3 = function foo() {{ return bar3 } }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 964c518180..24e145213d 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -21,8 +21,7 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; -//│ let hi; /** scoped **/ +//│ let hi, Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; //│ Cont$func$hi$$ = function Cont$func$hi$$(isMut, n, pc) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -61,19 +60,20 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let stackDelayRes; -//│ let scrut, tmp; /** scoped **/ -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind$(n, stackDelayRes, 0) -//│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ return hi(tmp) +//│ let stackDelayRes;{ +//│ let scrut, tmp; /** scoped **/ +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind$(n, stackDelayRes, 0) +//│ } +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ return hi(tmp) +//│ } //│ } //│ }; //│ $_stack$_safe$_body$_ = (undefined, function () { @@ -94,8 +94,7 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; -//│ let sum1; /** scoped **/ +//│ let sum1, Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; //│ Cont$func$sum$$ = function Cont$func$sum$$(isMut, n, tmp, pc) { //│ let tmp1, tmp2; //│ if (isMut === true) { @@ -151,25 +150,26 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let curDepth, stackDelayRes; -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ curDepth = runtime.stackDepth; -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind$1(n, tmp1, stackDelayRes, 0) -//│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ tmp1 = sum1(tmp); -//│ runtime.stackDepth = curDepth; -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind$1(n, tmp1, tmp1, 1) +//│ let curDepth, stackDelayRes;{ +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ curDepth = runtime.stackDepth; +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind$1(n, tmp1, stackDelayRes, 0) +//│ } +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ tmp1 = sum1(tmp); +//│ runtime.stackDepth = curDepth; +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind$1(n, tmp1, tmp1, 1) +//│ } +//│ return n + tmp1 //│ } -//│ return n + tmp1 //│ } //│ }; //│ $_stack$_safe$_body$_1 = (undefined, function () { @@ -276,11 +276,12 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; /** scoped **/ -//│ max = function max(a, b) { -//│ let scrut; /** scoped **/ -//│ scrut = a < b; -//│ if (scrut === true) { return b } else { return a } +//│ let max; +//│ max = function max(a, b) {{ +//│ let scrut; /** scoped **/ +//│ scrut = a < b; +//│ if (scrut === true) { return b } else { return a } +//│ } //│ }; @@ -290,7 +291,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index ada3f10bb0..e6dbdc99c7 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -14,7 +14,7 @@ class A(x) with set z += 2 x //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ A1 = function A(x) { //│ return globalThis.Object.freeze(new A.class(x)); //│ }; @@ -42,31 +42,33 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let idx1, idx2, idx3, idx4, idx5; -//│ let tmp, tmp1; /** scoped **/ -//│ idx1 = idx + 0; -//│ tmp = buf.buf.at(idx1) + 1; -//│ idx5 = idx + 0; -//│ buf.buf[idx5] = tmp; -//│ idx2 = idx + 1; -//│ tmp1 = buf.buf.at(idx2) + 2; -//│ idx4 = idx + 1; -//│ buf.buf[idx4] = tmp1; -//│ idx3 = idx + 0; -//│ return buf.buf.at(idx3) +//│ let idx1, idx2, idx3, idx4, idx5;{ +//│ let tmp, tmp1; /** scoped **/ +//│ idx1 = idx + 0; +//│ tmp = buf.buf.at(idx1) + 1; +//│ idx5 = idx + 0; +//│ buf.buf[idx5] = tmp; +//│ idx2 = idx + 1; +//│ tmp1 = buf.buf.at(idx2) + 2; +//│ idx4 = idx + 1; +//│ buf.buf[idx4] = tmp1; +//│ idx3 = idx + 0; +//│ return buf.buf.at(idx3) +//│ } //│ } //│ } //│ #x; //│ #z; //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } -//│ f(y) { -//│ let tmp, tmp1; /** scoped **/ -//│ tmp = this.#x + 1; -//│ this.#x = tmp; -//│ tmp1 = this.z + 2; -//│ this.z = tmp1; -//│ return this.#x +//│ f(y) {{ +//│ let tmp, tmp1; /** scoped **/ +//│ tmp = this.#x + 1; +//│ this.#x = tmp; +//│ tmp1 = this.z + 2; +//│ this.z = tmp1; +//│ return this.#x +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A", [null]]; diff --git a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls index 226fd3ed63..41df93f70a 100644 --- a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls +++ b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls @@ -20,7 +20,7 @@ //│ ║ l.16: 1 //│ ╙── ^ //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = + 2; + 3 +//│ let tmp2; tmp2 = + 2; + 3 //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.16: 1 //│ ╙── ^ @@ -31,7 +31,7 @@ + 2 + 3 //│ JS (unsanitized): -//│ let tmp3; /** scoped **/ tmp3 = 1 + 2; tmp3 + 3 +//│ let tmp3; tmp3 = 1 + 2; tmp3 + 3 //│ = 6 1 diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 87eb91c7eb..b726512101 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let ls, a, middleElements, element0$; /** scoped **/ +//│ let ls, a, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); @@ -133,20 +133,21 @@ fun popByIndex(start, end, acc, lft) = if start >= end then acc else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): -//│ let popByIndex; /** scoped **/ -//│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ -//│ scrut = start >= end; -//│ if (scrut === true) { -//│ return acc -//│ } else { -//│ tmp34 = start + 1; -//│ tmp35 = runtime.safeCall(lft.at(start)); -//│ tmp36 = globalThis.Object.freeze([ -//│ ...acc, -//│ tmp35 -//│ ]); -//│ return popByIndex(tmp34, end, tmp36, lft) +//│ let popByIndex; +//│ popByIndex = function popByIndex(start, end, acc, lft) {{ +//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ +//│ scrut = start >= end; +//│ if (scrut === true) { +//│ return acc +//│ } else { +//│ tmp34 = start + 1; +//│ tmp35 = runtime.safeCall(lft.at(start)); +//│ tmp36 = globalThis.Object.freeze([ +//│ ...acc, +//│ tmp35 +//│ ]); +//│ return popByIndex(tmp34, end, tmp36, lft) +//│ } //│ } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 783e6d19df..25ac7189a4 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) +//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 72036ed9d4..d38baf3139 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -19,30 +19,31 @@ x => if x is 0 then 1 x => if x is [[0]] then 1 //│ JS (unsanitized): //│ let lambda1; -//│ lambda1 = (undefined, function (x) { -//│ let element0$, element0$1, tmp; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { -//│ element0$ = runtime.Tuple.get(x, 0); -//│ if (runtime.Tuple.isArrayLike(element0$) && element0$.length === 1) { -//│ element0$1 = runtime.Tuple.get(element0$, 0); -//│ if (element0$1 === 0) { -//│ tmp = 1; -//│ break split_root$ +//│ lambda1 = (undefined, function (x) {{ +//│ let element0$, element0$1, tmp; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { +//│ element0$ = runtime.Tuple.get(x, 0); +//│ if (runtime.Tuple.isArrayLike(element0$) && element0$.length === 1) { +//│ element0$1 = runtime.Tuple.get(element0$, 0); +//│ if (element0$1 === 0) { +//│ tmp = 1; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp //│ } -//│ return tmp //│ }); //│ lambda1 //│ = fun @@ -57,26 +58,29 @@ fun crazy(v) = S(S(S(S(S(S(0)))))) then "bruh!" _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): -//│ let crazy; /** scoped **/ -//│ crazy = function crazy(v) { -//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (v instanceof S.class) { -//│ argument0$ = v.value; -//│ if (argument0$ instanceof S.class) { -//│ argument0$1 = argument0$.value; -//│ if (argument0$1 instanceof S.class) { -//│ argument0$2 = argument0$1.value; -//│ if (argument0$2 instanceof S.class) { -//│ argument0$3 = argument0$2.value; -//│ if (argument0$3 instanceof S.class) { -//│ argument0$4 = argument0$3.value; -//│ if (argument0$4 instanceof S.class) { -//│ argument0$5 = argument0$4.value; -//│ if (argument0$5 === 0) { -//│ tmp = "bruh!"; -//│ break split_root$ +//│ let crazy; +//│ crazy = function crazy(v) {{ +//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (v instanceof S.class) { +//│ argument0$ = v.value; +//│ if (argument0$ instanceof S.class) { +//│ argument0$1 = argument0$.value; +//│ if (argument0$1 instanceof S.class) { +//│ argument0$2 = argument0$1.value; +//│ if (argument0$2 instanceof S.class) { +//│ argument0$3 = argument0$2.value; +//│ if (argument0$3 instanceof S.class) { +//│ argument0$4 = argument0$3.value; +//│ if (argument0$4 instanceof S.class) { +//│ argument0$5 = argument0$4.value; +//│ if (argument0$5 === 0) { +//│ tmp = "bruh!"; +//│ break split_root$ +//│ } else { +//│ break split_1$ +//│ } //│ } else { //│ break split_1$ //│ } @@ -95,18 +99,16 @@ fun crazy(v) = //│ } else { //│ break split_1$ //│ } -//│ } else { -//│ break split_1$ //│ } +//│ tmp1 = S(0); +//│ tmp2 = S(tmp1); +//│ tmp3 = S(tmp2); +//│ tmp4 = S(tmp3); +//│ tmp5 = S(tmp4); +//│ tmp = S(tmp5); //│ } -//│ tmp1 = S(0); -//│ tmp2 = S(tmp1); -//│ tmp3 = S(tmp2); -//│ tmp4 = S(tmp3); -//│ tmp5 = S(tmp4); -//│ tmp = S(tmp5); +//│ return tmp //│ } -//│ return tmp //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index de445d712f..aef9d23d6a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,7 +25,7 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, scrut5, tmp5; /** scoped **/ +//│ let scrut4, scrut5, tmp5; //│ split_root$2: { //│ split_1$2: { //│ scrut4 = true; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index ca253fe2be..d8df0da7d6 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,22 +10,19 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } +//│ let scrut; scrut = x === 0; if (scrut === true) { 1 } else { 2 } //│ = 1 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; /** scoped **/ -//│ scrut1 = x === 0; -//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } -//│ a = tmp; +//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; /** scoped **/ +//│ let scrut2, tmp1; //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -41,7 +38,7 @@ if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ split_root$: { //│ split_1$: { //│ if (x === 0) { @@ -81,7 +78,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; /** scoped **/ +//│ let qqq, tmp3; //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -121,7 +118,7 @@ print of if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ split_root$2: { //│ split_1$2: { //│ if (x === 0) { @@ -162,36 +159,37 @@ fun foo(x, y, z) = 1 then "1" else "" //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo(x1, y1, z1) { -//│ let tmp5; /** scoped **/ -//│ split_root$3: { -//│ split_1$3: { -//│ if (x1 === 0) { -//│ if (y1 === 0) { -//│ if (z1 === 0) { -//│ tmp5 = "000"; -//│ break split_root$3 -//│ } else if (z1 === 1) { -//│ tmp5 = "001"; +//│ let foo; +//│ foo = function foo(x1, y1, z1) {{ +//│ let tmp5; /** scoped **/ +//│ split_root$3: { +//│ split_1$3: { +//│ if (x1 === 0) { +//│ if (y1 === 0) { +//│ if (z1 === 0) { +//│ tmp5 = "000"; +//│ break split_root$3 +//│ } else if (z1 === 1) { +//│ tmp5 = "001"; +//│ break split_root$3 +//│ } else { +//│ break split_1$3 +//│ } +//│ } else if (y1 === 1) { +//│ tmp5 = "01"; //│ break split_root$3 //│ } else { //│ break split_1$3 //│ } -//│ } else if (y1 === 1) { -//│ tmp5 = "01"; +//│ } else if (x1 === 1) { +//│ tmp5 = "1"; //│ break split_root$3 -//│ } else { -//│ break split_1$3 -//│ } -//│ } else if (x1 === 1) { -//│ tmp5 = "1"; -//│ break split_root$3 -//│ } else { break split_1$3 } +//│ } else { break split_1$3 } +//│ } +//│ tmp5 = ""; //│ } -//│ tmp5 = ""; +//│ return tmp5 //│ } -//│ return tmp5 //│ }; foo(0, 0, 0) @@ -208,7 +206,7 @@ print of if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ split_root$3: { //│ split_1$3: { //│ split_2$: { @@ -270,57 +268,58 @@ fun foo(x, y, z) = if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let foo1; /** scoped **/ -//│ foo1 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_1$4: { -//│ split_2$1: { -//│ split_3$1: { -//│ if (x1 === 0) { -//│ if (y1 === 0) { -//│ if (z1 === 0) { -//│ tmp6 = "000"; +//│ let foo1; +//│ foo1 = function foo(x1, y1, z1) {{ +//│ let tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_1$4: { +//│ split_2$1: { +//│ split_3$1: { +//│ if (x1 === 0) { +//│ if (y1 === 0) { +//│ if (z1 === 0) { +//│ tmp6 = "000"; +//│ break split_root$4 +//│ } else if (z1 === 1) { +//│ break split_1$4 +//│ } else { +//│ break split_2$1 +//│ } +//│ } else if (y1 === 1) { +//│ tmp6 = "01_"; //│ break split_root$4 -//│ } else if (z1 === 1) { -//│ break split_1$4 //│ } else { -//│ break split_2$1 +//│ if (z1 === 1) { +//│ break split_1$4 +//│ } else { +//│ if (y1 === 2) { +//│ break split_3$1 +//│ } else { +//│ break split_2$1 +//│ } +//│ } //│ } -//│ } else if (y1 === 1) { -//│ tmp6 = "01_"; +//│ } else if (x1 === 1) { +//│ tmp6 = "1__"; //│ break split_root$4 //│ } else { -//│ if (z1 === 1) { -//│ break split_1$4 +//│ if (y1 === 2) { +//│ break split_3$1 //│ } else { -//│ if (y1 === 2) { -//│ break split_3$1 -//│ } else { -//│ break split_2$1 -//│ } +//│ break split_2$1 //│ } //│ } -//│ } else if (x1 === 1) { -//│ tmp6 = "1__"; -//│ break split_root$4 -//│ } else { -//│ if (y1 === 2) { -//│ break split_3$1 -//│ } else { -//│ break split_2$1 -//│ } //│ } +//│ tmp6 = "_2_"; +//│ break split_root$4; //│ } -//│ tmp6 = "_2_"; +//│ tmp6 = "___"; //│ break split_root$4; //│ } -//│ tmp6 = "___"; -//│ break split_root$4; +//│ tmp6 = "0_1"; //│ } -//│ tmp6 = "0_1"; +//│ return tmp6 //│ } -//│ return tmp6 //│ }; fun expensive_call(str) = Math.pow(2, str.length) @@ -337,37 +336,40 @@ fun foo(x, y, z) = if x is let value = "hello" expensive_call(value) //│ JS (unsanitized): -//│ let foo2; /** scoped **/ -//│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_1$4: { -//│ if (x1 === 0) { -//│ if (y1 === 0) { -//│ if (z1 === 0) { -//│ tmp6 = "000"; -//│ break split_root$4 -//│ } else if (z1 === 1) { -//│ tmp6 = "001"; +//│ let foo2; +//│ foo2 = function foo(x1, y1, z1) {{ +//│ let value, tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_1$4: { +//│ if (x1 === 0) { +//│ if (y1 === 0) { +//│ if (z1 === 0) { +//│ tmp6 = "000"; +//│ break split_root$4 +//│ } else if (z1 === 1) { +//│ tmp6 = "001"; +//│ break split_root$4 +//│ } else { +//│ break split_1$4 +//│ } +//│ } else if (y1 === 1) { +//│ tmp6 = "01"; //│ break split_root$4 //│ } else { //│ break split_1$4 //│ } -//│ } else if (y1 === 1) { -//│ tmp6 = "01"; +//│ } else if (x1 === 1) { +//│ tmp6 = "1"; //│ break split_root$4 //│ } else { //│ break split_1$4 //│ } -//│ } else if (x1 === 1) { -//│ tmp6 = "1"; -//│ break split_root$4 -//│ } else { break split_1$4 } +//│ } +//│ value = "hello"; +//│ tmp6 = expensive_call(value); //│ } -//│ value = "hello"; -//│ tmp6 = expensive_call(value); +//│ return tmp6 //│ } -//│ return tmp6 //│ }; object A @@ -380,7 +382,7 @@ fun foo(x, y, z) = if x is A then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo3; /** scoped **/ +//│ let foo3; //│ foo3 = function foo(x1, y1, z1) { //│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } //│ }; @@ -401,25 +403,28 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo4; /** scoped **/ -//│ foo4 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_1$4: { -//│ if (x1 instanceof A.class) { -//│ if (y1 instanceof B.class) { -//│ if (z1 instanceof C.class) { -//│ tmp6 = "Hello"; -//│ break split_root$4 +//│ let foo4; +//│ foo4 = function foo(x1, y1, z1) {{ +//│ let tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_1$4: { +//│ if (x1 instanceof A.class) { +//│ if (y1 instanceof B.class) { +//│ if (z1 instanceof C.class) { +//│ tmp6 = "Hello"; +//│ break split_root$4 +//│ } else { +//│ break split_1$4 +//│ } //│ } else { //│ break split_1$4 //│ } //│ } else { break split_1$4 } -//│ } else { break split_1$4 } +//│ } +//│ tmp6 = "Goodbye"; //│ } -//│ tmp6 = "Goodbye"; +//│ return tmp6 //│ } -//│ return tmp6 //│ }; :sjs @@ -427,29 +432,30 @@ fun foo(x, y, z) = fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): -//│ let foo5; /** scoped **/ -//│ foo5 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_default$: { -//│ if (x1 instanceof A.class) { -//│ if (y1 instanceof B.class) { -//│ if (z1 instanceof C.class) { -//│ tmp6 = "Hello"; -//│ break split_root$4 +//│ let foo5; +//│ foo5 = function foo(x1, y1, z1) {{ +//│ let tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_default$: { +//│ if (x1 instanceof A.class) { +//│ if (y1 instanceof B.class) { +//│ if (z1 instanceof C.class) { +//│ tmp6 = "Hello"; +//│ break split_root$4 +//│ } else { +//│ break split_default$ +//│ } //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp6 //│ } -//│ return tmp6 //│ }; :expect "Hello" @@ -467,7 +473,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; /** scoped **/ +//│ let y1, scrut3, tmp6; //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls index db15ad49ed..18fb0d1700 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls @@ -17,7 +17,7 @@ if x then 0 y then 0 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ +//│ let tmp; //│ split_root$: { //│ split_1$: { //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index 04f680fc0b..96f735e094 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -10,30 +10,31 @@ class B x => if x is Pair(A, B) then 1 //│ JS (unsanitized): //│ let lambda; -//│ lambda = (undefined, function (x) { -//│ let argument0$, argument1$, tmp; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (x instanceof Pair.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ if (argument0$ instanceof A) { -//│ if (argument1$ instanceof B) { -//│ tmp = 1; -//│ break split_root$ +//│ lambda = (undefined, function (x) {{ +//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (x instanceof Pair.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ if (argument0$ instanceof A) { +//│ if (argument1$ instanceof B) { +//│ tmp = 1; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp //│ } -//│ return tmp //│ }); //│ lambda //│ = fun @@ -45,38 +46,39 @@ fun f(x) = if x is Pair(A, A) then 1 Pair(B, B) then 2 //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ f = function f(x) { -//│ let argument0$, argument1$, tmp; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (x instanceof Pair.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ if (argument0$ instanceof A) { -//│ if (argument1$ instanceof A) { -//│ tmp = 1; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } -//│ } else if (argument0$ instanceof B) { -//│ if (argument1$ instanceof B) { -//│ tmp = 2; -//│ break split_root$ +//│ let f; +//│ f = function f(x) {{ +//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (x instanceof Pair.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ if (argument0$ instanceof A) { +//│ if (argument1$ instanceof A) { +//│ tmp = 1; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else if (argument0$ instanceof B) { +//│ if (argument1$ instanceof B) { +//│ tmp = 2; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp //│ } -//│ return tmp //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 697105bd95..ddc5a8376c 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -36,16 +36,19 @@ fun foo(v) = A & B then 1 else 0 //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo(v) { -//│ let tmp2; /** scoped **/ -//│ split_root$2: { -//│ split_1$2: { -//│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } +//│ let foo; +//│ foo = function foo(v) {{ +//│ let tmp2; /** scoped **/ +//│ split_root$2: { +//│ split_1$2: { +//│ if (v instanceof A) { +//│ break split_1$2 +//│ } else { break split_1$2 } +//│ } +//│ tmp2 = 0; //│ } -//│ tmp2 = 0; +//│ return tmp2 //│ } -//│ return tmp2 //│ }; fun range(i, j) = diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 408479468b..09dd6ce1b7 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -6,14 +6,15 @@ fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ JS (unsanitized): -//│ let nonsense; /** scoped **/ -//│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); -//│ ys = middleElements; -//│ return ys -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ let nonsense; +//│ nonsense = function nonsense(xs) {{ +//│ let ys, middleElements; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); +//│ ys = middleElements; +//│ return ys +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ }; nonsense([]) @@ -27,20 +28,21 @@ fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ JS (unsanitized): -//│ let lead_and_last; /** scoped **/ -//│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { -//│ element0$ = runtime.Tuple.get(xs, 0); -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); -//│ lastElement0$ = runtime.Tuple.get(xs, -1); -//│ y = lastElement0$; -//│ ys = middleElements; -//│ x = element0$; -//│ return x + y -//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { -//│ return 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ let lead_and_last; +//│ lead_and_last = function lead_and_last(xs) {{ +//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { +//│ element0$ = runtime.Tuple.get(xs, 0); +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); +//│ lastElement0$ = runtime.Tuple.get(xs, -1); +//│ y = lastElement0$; +//│ ys = middleElements; +//│ x = element0$; +//│ return x + y +//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { +//│ return 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ }; lead_and_last(["foo", "bar"]) @@ -61,39 +63,40 @@ fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 //│ JS (unsanitized): -//│ let nested_tuple_patterns; /** scoped **/ -//│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { -//│ element0$ = runtime.Tuple.get(xs, 0); -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); -//│ lastElement0$ = runtime.Tuple.get(xs, -1); -//│ if (runtime.Tuple.isArrayLike(middleElements) && middleElements.length === 2) { -//│ element0$1 = runtime.Tuple.get(middleElements, 0); -//│ element1$ = runtime.Tuple.get(middleElements, 1); -//│ w = lastElement0$; -//│ z = element1$; -//│ y = element0$1; -//│ x = element0$; -//│ tmp6 = x + y; -//│ tmp7 = tmp6 + z; -//│ tmp8 = tmp7 + w; +//│ let nested_tuple_patterns; +//│ nested_tuple_patterns = function nested_tuple_patterns(xs) {{ +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { +//│ element0$ = runtime.Tuple.get(xs, 0); +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); +//│ lastElement0$ = runtime.Tuple.get(xs, -1); +//│ if (runtime.Tuple.isArrayLike(middleElements) && middleElements.length === 2) { +//│ element0$1 = runtime.Tuple.get(middleElements, 0); +//│ element1$ = runtime.Tuple.get(middleElements, 1); +//│ w = lastElement0$; +//│ z = element1$; +//│ y = element0$1; +//│ x = element0$; +//│ tmp6 = x + y; +//│ tmp7 = tmp6 + z; +//│ tmp8 = tmp7 + w; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { +//│ tmp8 = 0; //│ break split_root$ //│ } else { //│ break split_default$ //│ } -//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { -//│ tmp8 = 0; -//│ break split_root$ -//│ } else { -//│ break split_default$ //│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); +//│ return tmp8 //│ } -//│ return tmp8 //│ }; nested_tuple_patterns of [1, 2, 3, 4] diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index daa8dac993..4e3ef3a9f6 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -33,13 +33,14 @@ Cross.unapply("0") :sjs fun foo(x) = x is Cross //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo(x2) { -//│ let unapplyResult, output, bindings; /** scoped **/ -//│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); -//│ if (unapplyResult instanceof runtime.MatchSuccess.class) { -//│ output = unapplyResult.output; -//│ bindings = unapplyResult.bindings; -//│ return true -//│ } else { return false } +//│ let foo; +//│ foo = function foo(x2) {{ +//│ let unapplyResult, output, bindings; /** scoped **/ +//│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); +//│ if (unapplyResult instanceof runtime.MatchSuccess.class) { +//│ output = unapplyResult.output; +//│ bindings = unapplyResult.bindings; +//│ return true +//│ } else { return false } +//│ } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index b5e544bd4f..5ca7d2fa86 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -11,27 +11,28 @@ data class Pair[A, B](first: A, second: B) :sjs pattern SumPair = Pair(a, b) => a + b //│ JS (unsanitized): -//│ let SumPair1; /** scoped **/ +//│ let SumPair1; //│ globalThis.Object.freeze(class SumPair { //│ static { //│ SumPair1 = globalThis.Object.freeze(new this) //│ } //│ constructor() {} //│ unapply(input) { -//│ let lambda; -//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ -//│ lambda = (undefined, function (a, b) { -//│ return a + b -//│ }); -//│ transform = lambda; -//│ if (input instanceof Pair.class) { -//│ argument0$ = input.first; -//│ argument1$ = input.second; -//│ transformResult = runtime.safeCall(transform(argument0$, argument1$)); -//│ tmp = globalThis.Object.freeze({}); -//│ return globalThis.Object.freeze(new runtime.MatchSuccess.class(transformResult, tmp)) -//│ } else { -//│ return globalThis.Object.freeze(new runtime.MatchFailure.class(null)) +//│ let lambda;{ +//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ +//│ lambda = (undefined, function (a, b) { +//│ return a + b +//│ }); +//│ transform = lambda; +//│ if (input instanceof Pair.class) { +//│ argument0$ = input.first; +//│ argument1$ = input.second; +//│ transformResult = runtime.safeCall(transform(argument0$, argument1$)); +//│ tmp = globalThis.Object.freeze({}); +//│ return globalThis.Object.freeze(new runtime.MatchSuccess.class(transformResult, tmp)) +//│ } else { +//│ return globalThis.Object.freeze(new runtime.MatchFailure.class(null)) +//│ } //│ } //│ } //│ unapplyStringPrefix(input) { From 2b0590e9241c55bdccb0547865db3c286da8ebdc Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 28 Nov 2025 15:51:04 +0800 Subject: [PATCH 34/72] WIP: Fix top-level scope & add nest --- .../src/main/scala/hkmc2/codegen/Block.scala | 52 +++++++++---------- .../hkmc2/codegen/BlockTransformer.scala | 4 +- .../scala/hkmc2/codegen/BlockTraverser.scala | 2 +- .../scala/hkmc2/codegen/HandlerLowering.scala | 2 +- .../main/scala/hkmc2/codegen/Lowering.scala | 14 ++--- .../main/scala/hkmc2/codegen/Printer.scala | 2 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 12 +++-- .../scala/hkmc2/codegen/llir/Builder.scala | 4 +- .../hkmc2/codegen/wasm/text/WatBuilder.scala | 2 +- .../hkmc2/semantics/ucs/Normalization.scala | 3 +- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 15 +++--- 11 files changed, 57 insertions(+), 55 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 244eda5cb0..17be1b40d9 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,7 +49,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVarsNoScoped + res case TryBlock(sub, fin, rst) => sub.definedVarsNoScoped ++ fin.definedVarsNoScoped ++ rst.definedVarsNoScoped case Label(lbl, _, bod, rst) => bod.definedVarsNoScoped ++ rst.definedVarsNoScoped - case Scoped(syms, body) => body.definedVarsNoScoped -- syms + case Scoped(syms, body, _) => body.definedVarsNoScoped -- syms lazy val definedVars: Set[Local] = this match case _: Return | _: Throw => Set.empty @@ -70,7 +70,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - case Scoped(syms, body) => body.definedVars// -- syms + case Scoped(syms, body, _) => body.definedVars// -- syms lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -84,7 +84,7 @@ sealed abstract class Block extends Product: case TryBlock(sub, fin, rst) => 1 + sub.size + fin.size + rst.size case Label(_, _, bod, rst) => 1 + bod.size + rst.size case HandleBlock(lhs, res, par, args, cls, handlers, bdy, rst) => 1 + handlers.map(_.body.size).sum + bdy.size + rst.size - case Scoped(_, body) => body.size + case Scoped(_, body, _) => body.size // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match @@ -124,7 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body) => body.freeVars// -- syms + case Scoped(syms, body, _) => body.freeVars// -- syms case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -145,7 +145,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body) => body.freeVarsLLIR// -- syms + case Scoped(syms, body, _) => body.freeVarsLLIR// -- syms case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match @@ -158,7 +158,7 @@ sealed abstract class Block extends Product: case Define(d, rest) => d.subBlocks ::: rest :: Nil case HandleBlock(_, _, par, args, _, handlers, body, rest) => par.subBlocks ++ args.flatMap(_.subBlocks) ++ handlers.map(_.body) :+ body :+ rest case Label(_, _, body, rest) => body :: rest :: Nil - case Scoped(_, body) => body :: Nil + case Scoped(_, body, _) => body :: Nil // TODO rm Lam from values and thus the need for these cases case Return(r, _) => r.subBlocks @@ -273,11 +273,11 @@ sealed abstract class Block extends Product: then this else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) - case Scoped(syms, body) => + case Scoped(syms, body, topLevel) => val newBody = body.flatten(k) if newBody is body then this - else Scoped(syms, newBody) + else Scoped(syms, newBody, topLevel) case e: End => k(e) case t: BlockTail => this @@ -304,7 +304,7 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail +case class Scoped(syms: collection.Set[Local], body: Block, topLevel: Bool) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail @@ -322,44 +322,44 @@ case class Define(defn: Defn, rest: Block) extends Block with ProductWithTail object Match: def apply(scrut: Path, arms: Ls[Case -> Block], dflt: Opt[Block], rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, Match(scrut, arms, dflt, body)) + case Scoped(syms, body, tlvl) => Scoped(syms, Match(scrut, arms, dflt, body), tlvl) case _ => new Match(scrut, arms, dflt, rest) object Label: def apply(label: Local, loop: Bool, body: Block, rest: Block): Block = rest match - case Scoped(syms, rest) => Scoped(syms, Label(label, loop, body, rest)) + case Scoped(syms, rest, tlvl) => Scoped(syms, Label(label, loop, body, rest), tlvl) case _ => new Label(label, loop, body, rest) object Scoped: - def apply(syms: collection.Set[Local], body: Block): Block = body match - case Scoped(syms2, body) => - if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body) else Scoped(syms ++ syms2, body) + def apply(syms: collection.Set[Local], body: Block, tlvl: Bool): Block = body match + case Scoped(syms2, body, _) => + if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body, tlvl) else Scoped(syms ++ syms2, body, tlvl) case _ => - if syms.isEmpty then body else new Scoped(syms, body) + if syms.isEmpty then body else new Scoped(syms, body, tlvl) object Begin: def apply(sub: Block, rest: Block): Block = (sub, rest) match - case (Scoped(symsSub, bodySub), Scoped(symsRest, bodyRest)) => - Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest)) - case (Scoped(symsSub, bodySub), _) => Scoped(symsSub, Begin(bodySub, rest)) - case (_, Scoped(symsRest, bodyRest)) => Scoped(symsRest, Begin(sub, bodyRest)) + case (Scoped(symsSub, bodySub, tlvl1), Scoped(symsRest, bodyRest, tlvl2)) => + Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest), tlvl1 || tlvl2) + case (Scoped(symsSub, bodySub, tlvl), _) => Scoped(symsSub, Begin(bodySub, rest), tlvl) + case (_, Scoped(symsRest, bodyRest, tlvl)) => Scoped(symsRest, Begin(sub, bodyRest), tlvl) case _ => new Begin(sub, rest) object TryBlock: def apply(sub: Block, finallyDo: Block, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, TryBlock(sub, finallyDo, body)) + case Scoped(syms, body, tlvl) => Scoped(syms, TryBlock(sub, finallyDo, body), tlvl) case _ => new TryBlock(sub, finallyDo, rest) object Assign: def apply(lhs: Local, rhs: Result, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, Assign(lhs, rhs, body)) + case Scoped(syms, body, tlvl) => Scoped(syms, Assign(lhs, rhs, body), tlvl) case _ => new Assign(lhs, rhs, rest) object AssignField: def apply(lhs: Path, nme: Tree.Ident, rhs: Result, rest: Block)(symbol: Opt[MemberSymbol]): Block = rest match - case Scoped(syms, body) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol)) + case Scoped(syms, body, tlvl) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol), tlvl) case _ => new AssignField(lhs, nme, rhs, rest)(symbol) object AssignDynField: def apply(lhs: Path, fld: Path, arrayIdx: Bool, rhs: Result, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body)) + case Scoped(syms, body, tlvl) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body), tlvl) case _ => new AssignDynField(lhs, fld, arrayIdx, rhs, rest) object Define: def apply(defn: Defn, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, Define(defn, body)) + case Scoped(syms, body, tlvl) => Scoped(syms, Define(defn, body), tlvl) case _ => new Define(defn, rest) case class HandleBlock( @@ -385,7 +385,7 @@ object HandleBlock: body: Block, rest: Block ) = rest match - case Scoped(syms, rest) => + case Scoped(syms, rest, tlvl) => Scoped( syms, new HandleBlock( @@ -397,7 +397,7 @@ object HandleBlock: handlers, body, rest - )) + ), tlvl) case _ => new HandleBlock( lhs, res, diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala index c3dc63c211..6575afda49 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala @@ -95,9 +95,9 @@ class BlockTransformer(subst: SymbolSubst): if (lhs2 is lhs) && (fld2 is fld) && (rhs2 is rhs) && (rest2 is rest) then b else AssignDynField(lhs2, fld2, arrayIdx, rhs2, rest2) - case Scoped(s, bd) => + case Scoped(s, bd, topLevel) => val nb = applySubBlock(bd) - if nb is bd then b else Scoped(s, nb) + if nb is bd then b else Scoped(s, nb, topLevel) def applyRcdArg(rcdArg: RcdArg)(k: RcdArg => Block): Block = val RcdArg(idx, p) = rcdArg diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala index f8f41dd373..68201e292f 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala @@ -51,7 +51,7 @@ class BlockTraverser: applyResult(rhs) applyPath(fld) applySubBlock(rest) - case Scoped(_, body) => applySubBlock(body) + case Scoped(_, body, _) => applySubBlock(body) def applyResult(r: Result): Unit = r match case r @ Call(fun, args) => applyPath(fun); args.foreach(applyArg) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index 97a13183b4..cc6cda7293 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -420,7 +420,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, // ignored cases case TryBlock(sub, finallyDo, rest) => ??? // ignore case Throw(_) => PartRet(blk, Nil) - case Scoped(_, body) => go(body) + case Scoped(_, body, _) => go(body) case _: HandleBlock => lastWords("unexpected handleBlock") // already translated at this point val PartRet(head, states) = go(blk)(using labelIds, N) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 16d264cf85..87d87e13e0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -971,7 +971,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) val blk = - inScopedBlock(main.stats.foldLeft(main.res.definedSyms)(_ ++ _.definedSyms))(using LoweringCtx.empty): + inScopedBlock(main.stats.foldLeft(main.res.definedSyms)(_ ++ _.definedSyms), true)(using LoweringCtx.empty): block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -1016,18 +1016,18 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case ps => ps setupFunctionDef(physicalParams, bodyTerm, name) - def inScopedBlock(definedSymsInElaborated: Set[Symbol])(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = + def inScopedBlock(definedSymsInElaborated: Set[Symbol], topLevel: Bool)(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - possiblyScoped(scopedSyms, body) + possiblyScoped(scopedSyms, body, topLevel) - inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = - if syms.isEmpty then body else Scoped(syms, body) + inline def possiblyScoped(syms: collection.Set[Symbol], body: Block, topLevel: Bool) = + if syms.isEmpty then body else Scoped(syms, body, topLevel) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = - val scopedBody = inScopedBlock(bodyTerm.definedSyms)(returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(bodyTerm.definedSyms, false)(returnedTerm(bodyTerm)) (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = @@ -1140,7 +1140,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) go(paramLists.reverse, bod) def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - inScopedBlock(bod.definedSyms): + inScopedBlock(bod.definedSyms, false): val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") val resSym = TempSymbol(N, dbgNme = "traceLogRes") diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala index 00dc4e6160..c5c9df6290 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala @@ -55,7 +55,7 @@ object Printer: doc"set ${mkDocument(lhs)}.${nme.name} = ${mkDocument(rhs)} in # ${mkDocument(rest)}" case Define(defn, rest) => doc"define ${mkDocument(defn)} in # ${mkDocument(rest)}" - case Scoped(_, body) => mkDocument(body) + case Scoped(_, body, _) => mkDocument(body) case End("") => doc"end" case End(msg) => doc"end ${msg}" case _ => TODO(blk) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index c11f4ed307..5767ace624 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -479,7 +479,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: t :: e :: returningTerm(rest, endSemi) case Begin(sub, thn) => - doc"${returningTerm(sub, endSemi = true)}${returningTerm(thn, endSemi)}" + doc"${returningTerm(sub, endSemi = true)}${returningTerm(thn, endSemi)}" case End("") => doc"" case End(msg) => @@ -509,8 +509,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: } # ${ returningTerm(rst, endSemi).stripBreaks}" - case Scoped(syms, body) => - scope.nest.givenIn: + case Scoped(syms, body, topLevel) => + val nextScope = if topLevel then scope else scope.nest + nextScope.givenIn: val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => if scope.lookup(l).isDefined then // NOTE: this warning is turned off because the lifter is not @@ -525,11 +526,12 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: Some(l -> scope.allocateName(l)) // NOTE: currently this does not generate pretty JS codes... braced: - (if vars.isEmpty then doc"" else + val res = (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") :: doc"; /** scoped **/") :: returningTerm(body, endSemi) + if !topLevel then doc""" # { ${res} # }""" else res // TODO: pp // case _ => ??? @@ -597,7 +599,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: // NOTE: this is to make sure that we are NOT generating the top level // block in a nested scope, because for exported symbols they are looked up in the outer scope val unscopedMain = p.main match - case Scoped(syms, body) /* if exprt.isDefined */ => body + case Scoped(syms, body, _) /* if exprt.isDefined */ => body case _ => p.main imps.mkDocument(doc" # ") :/: block(unscopedMain, endSemi = false).stripBreaks :: ( exprt match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala index a7c1621a29..4048b8ecb5 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala @@ -523,7 +523,7 @@ final class LlirBuilder(using Elaborator.State)(tl: TraceLogger, uid: FreshInt): bBlock(rest)(k)(ct) case Define(_: ClsLikeDefn, rest) => bBlock(rest)(k)(ct) case End(msg) => k(Expr.Literal(Tree.UnitLit(false))) - case Scoped(_, body) => bBlock(body)(k)(ct) + case Scoped(_, body, _) => bBlock(body)(k)(ct) case _: Block => val docBlock = blk.showAsTree bErrStop(msg"Unsupported block: $docBlock") @@ -567,7 +567,7 @@ final class LlirBuilder(using Elaborator.State)(tl: TraceLogger, uid: FreshInt): case AssignField(_, _, _, rest) => applyBlock(rest) case AssignDynField(lhs, fld, arrayIdx, rhs, rest) => applyBlock(rest) case Define(defn, rest) => applyDefn(defn); applyBlock(rest) - case Scoped(_, body) => applyBlock(body) + case Scoped(_, body, _) => applyBlock(body) case HandleBlock(lhs, res, par, args, cls, handlers, body, rest) => applyBlock(rest) case End(msg) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala index 00cdae4b46..a7f8350505 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala @@ -600,7 +600,7 @@ class WatBuilder(using TraceLogger, State) extends CodeBuilder: `return`(S(resWat)) - case Scoped(_, body) => returningTerm(body) + case Scoped(_, body, _) => returningTerm(body) case Match(scrut, arms, dflt, rst) => val matchLabelSym = TempSymbol(N, "match") val matchLabel = scope.allocateName(matchLabelSym) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index b0d5618c75..986ac61a29 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -399,7 +399,8 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) Scoped( LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()), + false ) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 08c772d47d..273c3bfbb1 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -111,21 +111,20 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: // TODO: remove mutation var toplvlDefinedVars = collection.Set.empty[Symbol] - def assignResSym(b: Block, toplvl: Boolean): Block = + def assignResSym(b: Block): Block = b.mapTail: case e: End => Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) case Return(res, implct) => assert(implct) Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case s@Scoped(xs, body) => - if toplvl then - toplvlDefinedVars = xs - assignResSym(body, false) - else - Scoped(xs, assignResSym(body, false)) + case Scoped(xs, body, true) => + toplvlDefinedVars = xs + assignResSym(body) + case Scoped(xs, body, false) => + Scoped(xs, assignResSym(body), false) case tl: (Throw | Break | Continue) => tl - val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + val le = lowered0.copy(main = assignResSym(lowered0.main)) if showLoweredTree.isSet then output(s"Lowered:") output(lowered0.showAsTree) From 64385a29c6236b5452a5ddedc37724ada2b34141 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Sat, 29 Nov 2025 11:14:54 +0800 Subject: [PATCH 35/72] WIP: Try to remove redundant braces (not clean up yet) --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 50 +++++---- .../src/test/mlscript-compile/Predef.mjs | 2 +- .../src/test/mlscript-compile/Runtime.mjs | 2 +- .../OverloadedModulesInSignatures.mls | 13 +-- .../backlog/NonReturningStatements.mls | 2 +- .../src/test/mlscript/backlog/ToTriage.mls | 14 +-- .../src/test/mlscript/basics/BadDefs.mls | 4 +- .../mlscript/basics/BadMemberProjections.mls | 19 ++-- .../test/mlscript/basics/BadModuleUses.mls | 4 +- .../basics/CompanionModules_Classes.mls | 3 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../shared/src/test/mlscript/basics/Drop.mls | 9 +- .../test/mlscript/basics/FunnyRecordKeys.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 9 +- .../src/test/mlscript/basics/LazySpreads.mls | 4 +- .../mlscript/basics/MemberProjections.mls | 4 +- .../test/mlscript/basics/MiscArrayTests.mls | 2 +- .../mlscript/basics/MultiParamListClasses.mls | 7 +- .../test/mlscript/basics/MultiParamLists.mls | 17 +-- .../mlscript/basics/MultilineExpressions.mls | 28 ++--- .../src/test/mlscript/basics/MutArr.mls | 4 +- .../src/test/mlscript/basics/MutRcd.mls | 2 +- .../src/test/mlscript/basics/MutVal.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../src/test/mlscript/basics/PartialApps.mls | 11 +- .../mlscript/basics/PureTermStatements.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 10 +- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/basics/Underscores.mls | 9 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 28 ++--- .../src/test/mlscript/bbml/bbGetters.mls | 2 +- .../src/test/mlscript/codegen/Arrays.mls | 4 +- .../src/test/mlscript/codegen/BadInit.mls | 4 +- .../src/test/mlscript/codegen/BadNew.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 3 +- .../src/test/mlscript/codegen/BuiltinOps.mls | 4 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 2 +- .../test/mlscript/codegen/CaseShorthand.mls | 2 +- .../test/mlscript/codegen/ClassInClass.mls | 28 +++-- .../src/test/mlscript/codegen/ClassInFun.mls | 4 +- .../test/mlscript/codegen/ClassMatching.mls | 36 ++++--- .../src/test/mlscript/codegen/Classes.mls | 8 +- .../src/test/mlscript/codegen/Comma.mls | 8 +- .../src/test/mlscript/codegen/ConsoleLog.mls | 8 +- .../test/mlscript/codegen/DelayedLetInit.mls | 18 ++-- .../src/test/mlscript/codegen/EarlyReturn.mls | 2 +- .../test/mlscript/codegen/FieldSymbols.mls | 1 + .../src/test/mlscript/codegen/Formatting.mls | 21 ++-- .../src/test/mlscript/codegen/FunInClass.mls | 9 +- .../src/test/mlscript/codegen/Functions.mls | 2 +- .../test/mlscript/codegen/FunctionsThis.mls | 5 +- .../src/test/mlscript/codegen/Getters.mls | 24 ++--- .../src/test/mlscript/codegen/GlobalThis.mls | 14 +-- .../src/test/mlscript/codegen/Hygiene.mls | 22 ++-- .../src/test/mlscript/codegen/IfThenElse.mls | 6 +- .../src/test/mlscript/codegen/ImportMLs.mls | 10 +- .../src/test/mlscript/codegen/ImportedOps.mls | 2 +- .../test/mlscript/codegen/InlineLambdas.mls | 12 +-- .../test/mlscript/codegen/MergeMatchArms.mls | 96 +++++++++-------- .../test/mlscript/codegen/ModuleMethods.mls | 3 +- .../src/test/mlscript/codegen/Modules.mls | 11 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 4 +- .../test/mlscript/codegen/ParamClasses.mls | 33 +++--- .../src/test/mlscript/codegen/PartialApps.mls | 25 +++-- .../test/mlscript/codegen/PlainClasses.mls | 44 ++++---- .../src/test/mlscript/codegen/Primes.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/Quasiquotes.mls | 9 +- .../src/test/mlscript/codegen/RandomStuff.mls | 12 +-- .../shared/src/test/mlscript/codegen/Repl.mls | 2 +- .../test/mlscript/codegen/SanityChecks.mls | 100 ++++++++++-------- .../src/test/mlscript/codegen/Scoped.mls | 16 +-- .../test/mlscript/codegen/SelfReferences.mls | 5 +- .../src/test/mlscript/codegen/SetIn.mls | 10 +- .../src/test/mlscript/codegen/Spreads.mls | 2 +- .../shared/src/test/mlscript/codegen/This.mls | 7 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/ThisCalls.mls | 2 +- .../src/test/mlscript/codegen/Throw.mls | 6 +- .../src/test/mlscript/codegen/TraceLog.mls | 2 +- .../src/test/mlscript/codegen/UnitValue.mls | 10 +- .../src/test/mlscript/codegen/While.mls | 10 +- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/ctx/EtaExpansion.mls | 27 ++--- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../src/test/mlscript/handlers/Debugging.mls | 13 +-- .../src/test/mlscript/handlers/Effects.mls | 3 +- .../test/mlscript/handlers/EffectsHygiene.mls | 2 +- .../mlscript/handlers/EffectsInClasses.mls | 4 +- .../mlscript/handlers/RecursiveHandlers.mls | 65 ++++++------ .../test/mlscript/handlers/SetInHandlers.mls | 2 +- .../test/mlscript/handlers/StackSafety.mls | 10 +- .../src/test/mlscript/interop/Arrays.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 3 +- .../test/mlscript/lifter/CompanionsInFun.mls | 3 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 6 +- .../src/test/mlscript/lifter/FunInFun.mls | 18 ++-- .../src/test/mlscript/lifter/Imports.mls | 2 +- .../shared/src/test/mlscript/lifter/Loops.mls | 3 +- .../test/mlscript/lifter/ModulesObjects.mls | 23 ++-- .../src/test/mlscript/lifter/Mutation.mls | 8 +- .../test/mlscript/lifter/StackSafetyLift.mls | 10 +- .../src/test/mlscript/objbuf/Mutation.mls | 2 +- .../src/test/mlscript/parser/PrefixOps.mls | 4 +- .../test/mlscript/std/FingerTreeListTest.mls | 26 ++--- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 2 +- .../ucs/general/LogicalConnectives.mls | 2 +- .../ucs/normalization/Deduplication.mls | 31 +++--- .../normalization/ExcessiveDeduplication.mls | 2 +- .../ucs/normalization/SimplePairMatches.mls | 2 +- .../ucs/patterns/ConjunctionPattern.mls | 2 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 6 +- .../src/test/mlscript/ups/MatchResult.mls | 2 +- .../src/test/mlscript/ups/SimpleTransform.mls | 2 +- 116 files changed, 672 insertions(+), 580 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 5767ace624..bea581ab7e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -103,6 +103,11 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else s.toIntOption match case S(index) => doc"[$index]" case N => doc"[${JSBuilder.makeStringLiteral(s)}]" + + def tryBraced(original: Block, res: Document)(using Scope) = original match + case Scoped(syms, body, _) => // FIXME: remove the hack `!(body.definedVarsNoScoped -- syms).isEmpty` after handling the later passes correctly + if (syms.filter(l => scope.lookup(l).isDefined)).isEmpty || !(body.definedVarsNoScoped -- syms).isEmpty then braced(res) else res + case _ => braced(res) def result(r: Result)(using Raise, Scope): Document = r match case Value.This(sym) => scope.findThis_!(sym) @@ -148,7 +153,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else doc"$runtimeVar.safeCall(${base}(${argsDoc}))" case Lambda(ps, bod) => scope.nest givenIn: val (params, bodyDoc) = setupFunction(none, ps, bod) - doc"($params) => ${ braced(bodyDoc) }" + doc"($params) => ${ tryBraced(bod, bodyDoc) }" case s @ Select(qual, id) => val dotClass = s.symbol match case S(ds) if ds.shouldBeLifted => doc".class" @@ -237,10 +242,10 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: if sym.nameIsMeaningful then // If the name is not valid JavaScript identifiers, do not use it in the generated function. val nme = if isValidIdentifier(sym.nme) then sym.nme else "" - doc"${getVar(sym, sym.toLoc)} = function $nme($params) ${ braced(bodyDoc) };" + doc"${getVar(sym, sym.toLoc)} = function $nme($params) ${ tryBraced(result, bodyDoc) };" else // in JS, let name = (0, function (args) => {} ) prevents function's name from being bound to `name` - doc"${getVar(sym, sym.toLoc)} = (undefined, function ($params) ${ braced(bodyDoc) });" + doc"${getVar(sym, sym.toLoc)} = (undefined, function ($params) ${ tryBraced(result, bodyDoc) });" case ClsLikeDefn(ownr, isym, sym, kind, paramsOpt, auxParams, par, mtds, privFlds, pubFlds, preCtor, ctor, modo, bufferable) @@ -257,9 +262,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: Return(Lambda(ps, block), false) val (params, bodyDoc) = scope.nest.givenIn: setupFunction(S(td.sym.nme), ps, result) - doc" # $mtdPrefix${td.sym.nme}($params) ${ braced(bodyDoc) }" + doc" # $mtdPrefix${td.sym.nme}($params) ${ tryBraced(result, bodyDoc) }" case td @ FunDefn(params = Nil, body = bod) => - doc" # ${mtdPrefix}get ${td.sym.nme}() ${ braced(body(bod, endSemi = true)) }" + doc" # ${mtdPrefix}get ${td.sym.nme}() ${ tryBraced(bod, body(bod, endSemi = true)) }" .mkDocument(" ") def mkPrivs(pubFlds: Ls[BlockMemberSymbol -> TermSymbol], privFlds: Ls[TermSymbol], @@ -469,17 +474,17 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc"""typeof $sd === "object" && $sd !== null && "${n.name}" in $sd""" case Case.Field(name = n, safe = true) => doc""""${n.name}" in $sd""" - val h = doc" # if (${ cond(hd._1) }) ${ braced(returningTerm(hd._2, endSemi = false)) }" + val h = doc" # if (${ cond(hd._1) }) ${ tryBraced(hd._2, returningTerm(hd._2, endSemi = false)) }" val t = tl.foldLeft(h)((acc, arm) => - acc :: doc" else if (${ cond(arm._1) }) ${ braced(returningTerm(arm._2, endSemi = false)) }") + acc :: doc" else if (${ cond(arm._1) }) ${ tryBraced(arm._2, returningTerm(arm._2, endSemi = false)) }") val e = els match case S(el) => - doc" else ${ braced(returningTerm(el, endSemi = false)) }" + doc" else ${ tryBraced(el, returningTerm(el, endSemi = false)) }" case N => doc"" t :: e :: returningTerm(rest, endSemi) case Begin(sub, thn) => - doc"${returningTerm(sub, endSemi = true)}${returningTerm(thn, endSemi)}" + doc"${returningTerm(sub, endSemi = true)}${returningTerm(thn, endSemi)}" case End("") => doc"" case End(msg) => @@ -504,8 +509,8 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: } :: returningTerm(rst, endSemi) case TryBlock(sub, fin, rst) => - doc" # try ${ braced(returningTerm(sub, endSemi = false)) } finally ${ - braced(returningTerm(fin, endSemi = false)) + doc" # try ${ tryBraced(sub, returningTerm(sub, endSemi = false)) } finally ${ + tryBraced(fin, returningTerm(fin, endSemi = false)) } # ${ returningTerm(rst, endSemi).stripBreaks}" @@ -525,13 +530,12 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else Some(l -> scope.allocateName(l)) // NOTE: currently this does not generate pretty JS codes... - braced: - val res = (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc"; /** scoped **/") :: returningTerm(body, endSemi) - if !topLevel then doc""" # { ${res} # }""" else res // TODO: pp + val res = (if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: doc"; /** scoped **/") :: returningTerm(body, endSemi) + if !topLevel then braced(res) else res // case _ => ??? @@ -598,10 +602,10 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc"""import ${getVar(i._1, N)} from "${relPath}";""" // NOTE: this is to make sure that we are NOT generating the top level // block in a nested scope, because for exported symbols they are looked up in the outer scope - val unscopedMain = p.main match - case Scoped(syms, body, _) /* if exprt.isDefined */ => body - case _ => p.main - imps.mkDocument(doc" # ") :/: block(unscopedMain, endSemi = false).stripBreaks :: ( + // val unscopedMain = p.main match + // case Scoped(syms, body, _) /* if exprt.isDefined */ => body + // case _ => p.main + imps.mkDocument(doc" # ") :/: block(p.main, endSemi = false).stripBreaks :: ( exprt match case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" case N => doc"" @@ -776,7 +780,7 @@ trait JSBuilderArgNumSanityChecks(using Config, Elaborator.State) val restAssign = paramRest match case N => doc"" case S(p) => doc"\nlet $p = $runtimeVar.Tuple.slice($paramsStr, ${params.paramCountLB}, 0);" - (doc"...$paramsStr", doc"$checkArgsNum$paramsAssign$restAssign${this.body(body, endSemi = false)}") + (doc"...$paramsStr", braced(doc"$checkArgsNum$paramsAssign$restAssign${this.body(body, endSemi = false)}")) else super.setupFunction(name, params, body) diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index c1955ffcc3..8a2a1e88d9 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -5,7 +5,7 @@ import Term from "./Term.mjs"; import RuntimeJS from "./RuntimeJS.mjs"; import Runtime from "./Runtime.mjs"; import Rendering from "./Rendering.mjs"; -let Predef1; +let Predef1; /** scoped **/ globalThis.Object.freeze(class Predef { static { Predef1 = this diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index cd89dbd87a..e426a546ad 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -6,7 +6,7 @@ import RuntimeJS from "./RuntimeJS.mjs"; import Rendering from "./Rendering.mjs"; import LazyArray from "./LazyArray.mjs"; import Iter from "./Iter.mjs"; -let Runtime1; +let Runtime1; /** scoped **/ globalThis.Object.freeze(class Runtime { static { Runtime1 = this diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index cc5d9072a5..7480913d4d 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,7 +81,8 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; f5 = function f() {{ let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 } }; +//│ let f5; /** scoped **/ +//│ f5 = function f() {{ let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 } }; :fixme :expect 42 @@ -92,7 +93,7 @@ f :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; f6 = function f() { return TrmMod.class }; +//│ let f6; /** scoped **/ f6 = function f() { return TrmMod.class }; fun assertModule(module m: ClsMod): module ClsMod = m @@ -133,10 +134,10 @@ module Foo :e fun f(x: Foo): Foo = x //│ ╔══[ERROR] Expected 1 type arguments, got none -//│ ║ l.134: fun f(x: Foo): Foo = x +//│ ║ l.135: fun f(x: Foo): Foo = x //│ ╙── ^^^ //│ ╔══[ERROR] Expected 1 type arguments, got none -//│ ║ l.134: fun f(x: Foo): Foo = x +//│ ║ l.135: fun f(x: Foo): Foo = x //│ ╙── ^^^ fun f(x: Foo[Int]): Foo[Int] = x @@ -146,10 +147,10 @@ fun f(module x: Foo): module Foo = x :e fun f(module x: Foo[Int]): module Foo[Int] = x //│ ╔══[ERROR] Expected no type arguments, got 1 -//│ ║ l.147: fun f(module x: Foo[Int]): module Foo[Int] = x +//│ ║ l.148: fun f(module x: Foo[Int]): module Foo[Int] = x //│ ╙── ^^^^^^^^ //│ ╔══[ERROR] Expected no type arguments, got 1 -//│ ║ l.147: fun f(module x: Foo[Int]): module Foo[Int] = x +//│ ║ l.148: fun f(module x: Foo[Int]): module Foo[Int] = x //│ ╙── ^^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls index 630a7d1107..18ba41070d 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls @@ -31,7 +31,7 @@ fun foo = :sjs foo //│ JS (unsanitized): -//│ let tmp2; tmp2 = foo2(); tmp2 +//│ let tmp2; /** scoped **/ tmp2 = foo2(); tmp2 //│ > 1 //│ > ... diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index e6e31984ba..51186bf223 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -64,7 +64,7 @@ Infinity :sjs val Infinity = 1 //│ JS (unsanitized): -//│ let Infinity; Infinity = 1; +//│ let Infinity; /** scoped **/ Infinity = 1; //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Identifier 'Infinity' has already been declared //│ Infinity = Infinity @@ -105,7 +105,7 @@ fun main() = object Cls(val x) with fun huh = x //│ JS (unsanitized): -//│ let Cls1; +//│ let Cls1; /** scoped **/ //│ globalThis.Object.freeze(class Cls { //│ static { //│ Cls1.class = globalThis.Object.freeze(new this) @@ -202,7 +202,7 @@ class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let D1; +//│ let D1; /** scoped **/ //│ globalThis.Object.freeze(class D extends Predef.id { //│ static { //│ D1 = this @@ -251,7 +251,7 @@ set (x += 1; ()) fun baz = 1 fun bar() = baz //│ JS (unsanitized): -//│ let bar, baz; +//│ let bar, baz; /** scoped **/ //│ baz = function baz() { //│ return 1 //│ }; @@ -354,7 +354,7 @@ fun w(txt) = fs.writeFileSync(outFilePath, txt) // () //│ JS (unsanitized): -//│ let w; w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; +//│ let w; /** scoped **/ w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; w("whoops") //│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value. @@ -382,7 +382,7 @@ module Foo(x) //│ ║ l.379: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -406,7 +406,7 @@ module Foo(val x) //│ ║ l.403: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7; /** scoped **/ //│ globalThis.Object.freeze(class Foo6 { //│ static { //│ Foo7 = this diff --git a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls index 037ca2f59d..8c99b7c926 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls @@ -15,7 +15,7 @@ x :sjs val ++ = 0 //│ JS (unsanitized): -//│ let $_$_; $_$_ = 0; +//│ let $_$_; /** scoped **/ $_$_ = 0; //│ ++ = 0 :sjs @@ -51,7 +51,7 @@ fun ++ z = 0 :sjs ++ //│ JS (unsanitized): -//│ let tmp; tmp = $_$_2(); tmp +//│ let tmp; /** scoped **/ tmp = $_$_2(); tmp //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls index 3873c91b9a..8619f92a40 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls @@ -31,11 +31,12 @@ //│ ║ l.24: 1::x() //│ ╙── ^ //│ JS: -//│ lambda1 = (undefined, function (...args1) { -//│ runtime.checkArgs("", 1, false, args1.length); -//│ let self = args1[0]; -//│ let args = runtime.Tuple.slice(args1, 1, 0); -//│ return runtime.safeCall(self.x(...args)) +//│ lambda1 = (undefined, function (...args1) {{ +//│ runtime.checkArgs("", 1, false, args1.length); +//│ let self = args1[0]; +//│ let args = runtime.Tuple.slice(args1, 1, 0); +//│ return runtime.safeCall(self.x(...args)) +//│ } //│ }); //│ block$res2 = runtime.checkCall(lambda1()); //│ undefined @@ -56,24 +57,24 @@ let x = 1 :e "A"::x //│ ╔══[ERROR] String literal is not a known class. -//│ ║ l.57: "A"::x +//│ ║ l.58: "A"::x //│ ║ ^^^ //│ ╟── Note: any expression of the form `‹expression›::‹identifier›` is a member projection; //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ╔══[ERROR] Expected a statically known class; found string literal. -//│ ║ l.57: "A"::x +//│ ║ l.58: "A"::x //│ ╙── ^^^ //│ = fun :e "A" ::x //│ ╔══[ERROR] String literal is not a known class. -//│ ║ l.69: "A" ::x +//│ ║ l.70: "A" ::x //│ ║ ^^^ //│ ╟── Note: any expression of the form `‹expression›::‹identifier›` is a member projection; //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ╔══[ERROR] Expected a statically known class; found string literal. -//│ ║ l.69: "A" ::x +//│ ║ l.70: "A" ::x //│ ╙── ^^^ //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls index c4c7814570..417c7de2ad 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls @@ -47,14 +47,14 @@ Example.foo() + 1 //│ ╔══[ERROR] Unexpected moduleful application of type M. //│ ║ l.46: Example.foo() + 1 //│ ╙── ^^^^^^^^^^^^^ -//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M + 1 //│ ╔══[ERROR] Unexpected moduleful reference of type M. //│ ║ l.53: M + 1 //│ ╙── ^ -//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M |> id diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index ced354b8b2..1dc6b6f2b7 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1, tmp1; +//│ let Foo1, tmp1, tmp2; /** scoped **/ //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { @@ -107,7 +107,6 @@ module Foo with //│ } //│ constructor() {} //│ static { -//│ let tmp2; //│ tmp2 = Foo1(); //│ this.res = tmp2.foo; //│ } diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index d0425820bb..df691692ad 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ +//│ let foo; /** scoped **/ :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index 9d957653bc..ce93f69fb6 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp, tmp1; /** scoped **/ tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; +//│ let a, b, tmp2, tmp3; /** scoped **/ //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a @@ -22,7 +22,10 @@ drop { a: 0, b: 1 } :sjs let discard = drop _ //│ JS (unsanitized): -//│ let discard, discard1; discard1 = function discard(_0) { return runtime.Unit }; discard = discard1; +//│ let discard; +//│ let discard1; /** scoped **/ +//│ discard = function discard(_0) { return runtime.Unit }; +//│ discard1 = discard; //│ discard = fun discard discard of { a: 0, b: 1 } diff --git a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls index d91b3fba16..708cfa4897 100644 --- a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls +++ b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls @@ -6,7 +6,7 @@ { a: 1 } //│ JS (unsanitized): -//│ let a; a = 1; globalThis.Object.freeze({ a: a }) +//│ let a; /** scoped **/ a = 1; globalThis.Object.freeze({ a: a }) //│ = {a: 1} { "a": 1 } @@ -35,7 +35,7 @@ :sjs { (id(0)): 1 } //│ JS (unsanitized): -//│ let tmp; tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) +//│ let tmp; /** scoped **/ tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) //│ = {"0": 1} diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index e50e75b2e8..7f9f887e0c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,7 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let Baz1; +//│ let Baz1, tmp; /** scoped **/ //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; @@ -60,7 +60,6 @@ data class Baz(z) extends Bar(z * 1) with //│ Baz1.class = this //│ } //│ constructor(z) { -//│ let tmp; //│ tmp = z * 1; //│ super(tmp); //│ this.z = z; @@ -100,8 +99,8 @@ data class Barr(x: Int)(y: Int) with data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with fun g = z //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.100: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with -//│ ╙── ^^^^^^^^^^^^^^^^^^^ +//│ ║ l.99: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with +//│ ╙── ^^^^^^^^^^^^^^^^^^^ :todo Bazz(42).f @@ -111,7 +110,7 @@ Bazz(42).f new Barr(40)(2) with fun f = 43 //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.111: new Barr(40)(2) with +//│ ║ l.110: new Barr(40)(2) with //│ ╙── ^^^^^^^^^^^ //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index aa50ea39b9..8153dc1d64 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -17,7 +17,7 @@ fun buildPalindrome = case 0 then [] n then [n, ..buildPalindrome(n - 1), n] //│ JS (unsanitized): -//│ let buildPalindrome; +//│ let buildPalindrome; /** scoped **/ //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) {{ @@ -49,7 +49,7 @@ fun sum2 = case [] then 0 [x, ..xs, y] then x + y + sum2(xs) //│ JS (unsanitized): -//│ let sum2; +//│ let sum2; /** scoped **/ //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) {{ diff --git a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls index c22fcf695c..1464825716 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls @@ -46,7 +46,7 @@ M.Foo:: n(foo, 2) :sjs let m = M.Foo::m //│ JS (unsanitized): -//│ let m, m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; +//│ let m; let m1; /** scoped **/ m = function m(self, ...args) { return self.m(...args) }; m1 = m; //│ m = fun m m(foo) @@ -132,7 +132,7 @@ Foo::n(foo, 2) //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ═══[ERROR] Expected a statically known class; found ‹error›. //│ JS (unsanitized): -//│ let lambda9; +//│ let lambda9; /** scoped **/ //│ lambda9 = (undefined, function (self, ...args) { //│ return runtime.safeCall(self.n(...args)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls index 2c3bbe2920..4cffa34572 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls @@ -54,7 +54,7 @@ xs \ //│ ║ l.52: map(x => x * 2) //│ ╙── ^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let lambda5, tmp4; +//│ let lambda5, tmp4; /** scoped **/ //│ lambda5 = (undefined, function (x) { return x * 2 }); //│ tmp4 = map(lambda5); //│ Predef.passTo(xs1, tmp4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index 69ed3c99b7..13c14110a3 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -77,14 +77,15 @@ Foo(1, 2) :sjs let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): -//│ let f, f1; -//│ f1 = function f(x, y) {{ +//│ let f; +//│ let f1; /** scoped **/ +//│ f = function f(x, y) {{ //│ let tmp4; /** scoped **/ //│ tmp4 = Foo7(x, y); //│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) //│ } //│ }; -//│ f = f1; +//│ f1 = f; //│ f = fun f // Eta-expansion happens here. See EtaExpansion.mls. diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 01bf238555..f7a8de0017 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -6,7 +6,7 @@ fun f(n1: Int): Int = n1 //│ JS (unsanitized): -//│ let f; f = function f(n1) { return n1 }; +//│ let f; /** scoped **/ f = function f(n1) { return n1 }; f(42) //│ JS (unsanitized): @@ -19,7 +19,7 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f(n1) { //│ return (n2) => {{ let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } } //│ }; @@ -29,12 +29,12 @@ fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) f(4)(2) //│ JS (unsanitized): -//│ let tmp; tmp = f1(4); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = f1(4); runtime.safeCall(tmp(2)) //│ = 42 fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => {{ @@ -50,12 +50,15 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) +//│ let tmp1, tmp2; /** scoped **/ +//│ tmp1 = f2(4); +//│ tmp2 = runtime.safeCall(tmp1(2)); +//│ runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { @@ -75,7 +78,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; +//│ let tmp3, tmp4, tmp5; /** scoped **/ //│ tmp3 = f3(3); //│ tmp4 = runtime.safeCall(tmp3(0)); //│ tmp5 = runtime.safeCall(tmp4(3)); diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index 5025320ce4..868d71282e 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,12 +80,14 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; -//│ tmp17 = 2 * 3; -//│ scrut = 1 + tmp17; -//│ if (scrut === true) { -//│ 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let scrut, tmp17; /** scoped **/ +//│ tmp17 = 2 * 3; +//│ scrut = 1 + tmp17; +//│ if (scrut === true) { +//│ 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ ═══[RUNTIME ERROR] Error: match error :pt @@ -116,23 +118,23 @@ if 1 + 2 * 3 then 0 + 4 then 1 //│ ╔══[PARSE ERROR] Operator cannot be used inside this operator split -//│ ║ l.117: + 4 then 1 +//│ ║ l.119: + 4 then 1 //│ ║ ^ //│ ╟── as it has lower precedence than the splitting operator here -//│ ║ l.116: * 3 then 0 +//│ ║ l.118: * 3 then 0 //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected 'then' keyword in this operator split inner position -//│ ║ l.117: + 4 then 1 +//│ ║ l.119: + 4 then 1 //│ ║ ^^^^ //│ ╟── Note: the operator split starts here -//│ ║ l.116: * 3 then 0 +//│ ║ l.118: * 3 then 0 //│ ╙── ^ //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.115: if 1 + 2 +//│ ║ l.117: if 1 + 2 //│ ║ ^ -//│ ║ l.116: * 3 then 0 +//│ ║ l.118: * 3 then 0 //│ ║ ^^^^^^^^^^^^ -//│ ║ l.117: + 4 then 1 +//│ ║ l.119: + 4 then 1 //│ ╙── ^^^^^ :pt diff --git a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls index 58a398ea87..d95bc500e7 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls @@ -4,7 +4,7 @@ :sjs let t = [0, 1, 2] //│ JS (unsanitized): -//│ let t; t = globalThis.Object.freeze([ 0, 1, 2 ]); +//│ let t; /** scoped **/ t = globalThis.Object.freeze([ 0, 1, 2 ]); //│ t = [0, 1, 2] :re @@ -21,7 +21,7 @@ t :sjs let t = mut [0, 1, 2] //│ JS (unsanitized): -//│ let t1; t1 = [ 0, 1, 2 ]; +//│ let t1; /** scoped **/ t1 = [ 0, 1, 2 ]; //│ t = [0, 1, 2] t.push(4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls index 0c8a48f3a4..7efae81a91 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls @@ -46,7 +46,7 @@ r.foo :sjs let r = {} //│ JS (unsanitized): -//│ let r3; r3 = runtime.Unit; +//│ let r3; /** scoped **/ r3 = runtime.Unit; //│ r = () :re diff --git a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls index 408d7db105..655c4d0a45 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls @@ -33,7 +33,7 @@ O.gy :sjs class Foo(x, val y, mut val z) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x, y, z) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index 660664e019..790005a3c5 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; f = globalThis.Object.freeze(new Foo()); +//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; f1 = new Foo(); +//│ let f1; /** scoped **/ f1 = new Foo(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index cae9d03b91..567f89950c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -109,18 +109,19 @@ passTo(1, add(., 1) @ _ + _)(2) :sjs let f = add(_, 1) //│ JS (unsanitized): -//│ let f, f1; -//│ f1 = function f(_0) {{ let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) } }; -//│ f = f1; +//│ let f; +//│ let f1; /** scoped **/ +//│ f = function f(_0) {{ let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) } }; +//│ f1 = f; //│ f = fun f :fixme let f = add(., 1) //│ ╔══[PARSE ERROR] Expected an expression; found period instead -//│ ║ l.118: let f = add(., 1) +//│ ║ l.119: let f = add(., 1) //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected period here -//│ ║ l.118: let f = add(., 1) +//│ ║ l.119: let f = add(., 1) //│ ╙── ^ //│ ═══[RUNTIME ERROR] Error: Function expected 2 arguments but got 0 //│ f = undefined diff --git a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls index 36ad46feac..c9d079ed1c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls @@ -56,7 +56,7 @@ fun foo() = :sjs 1; id(2) //│ JS (unsanitized): -//│ let tmp; tmp = Predef.id(2); (1 , tmp) +//│ let tmp; /** scoped **/ tmp = Predef.id(2); (1 , tmp) //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index d1e18036da..3ce3d11b78 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -4,7 +4,9 @@ :sjs 1 && 2 //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function () { return 2 }); runtime.short_and(1, lambda) +//│ let lambda; /** scoped **/ +//│ lambda = (undefined, function () { return 2 }); +//│ runtime.short_and(1, lambda) //│ = 2 1 || 2 @@ -18,7 +20,9 @@ fun test(x) = :sjs 123 || test(42) //│ JS (unsanitized): -//│ let lambda2; lambda2 = (undefined, function () { return test(42) }); runtime.short_or(123, lambda2) +//│ let lambda2; /** scoped **/ +//│ lambda2 = (undefined, function () { return test(42) }); +//│ runtime.short_or(123, lambda2) //│ = 123 0 || test(42) @@ -32,7 +36,7 @@ fun test(x) = :sjs fold(||)(0, false, 42, 123) //│ JS (unsanitized): -//│ let lambda5, tmp; +//│ let lambda5, tmp; /** scoped **/ //│ lambda5 = (undefined, function (arg1, arg2) {{ //│ let lambda6; /** scoped **/ //│ lambda6 = (undefined, function () { diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index 666d7e6f4c..c92e767325 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ let scrut; scrut = "a"; if (typeof scrut === 'string') { true } else { false } +//│ { let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index 1d443c419c..dcb0d0ba4b 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -61,9 +61,10 @@ print(_) :sjs let test = _.f(0, _, 2) //│ JS (unsanitized): -//│ let test, test1; -//│ test1 = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; -//│ test = test1; +//│ let test; +//│ let test1; /** scoped **/ +//│ test = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; +//│ test1 = test; //│ test = fun test :re @@ -135,7 +136,7 @@ mkObj(1, 2) // :re let mkObj = x: _, y: _, z: 3 //│ ╔══[ERROR] Illegal position for '_' placeholder. -//│ ║ l.136: let mkObj = x: _, y: _, z: 3 +//│ ║ l.137: let mkObj = x: _, y: _, z: 3 //│ ╙── ^ //│ mkObj = fun mkObj //│ y = undefined diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 3bcd4220f5..25738567f0 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -31,7 +31,7 @@ :sjs let x = 1 in x + 1 //│ JS (unsanitized): -//│ let x; x = 1; x + 1 +//│ let x; /** scoped **/ x = 1; x + 1 //│ = 2 //│ Type: Int @@ -61,7 +61,7 @@ false :sjs data class Foo(x: Int) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x1) { //│ return globalThis.Object.freeze(new Foo.class(x1)); //│ }; @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; foo = globalThis.Object.freeze(new Foo.class(42)); foo.x +//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo.class(42)); foo.x //│ = 42 //│ Type: Int @@ -97,7 +97,7 @@ let foo = new Foo(42) in foo.Foo#x :sjs fun inc(x) = x + 1 //│ JS (unsanitized): -//│ let inc; inc = function inc(x1) { return x1 + 1 }; +//│ let inc; /** scoped **/ inc = function inc(x1) { return x1 + 1 }; //│ Type: ⊤ @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ let scrut; scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } +//│ { let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ let scrut1; scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } +//│ { let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } } //│ = 1 //│ Type: Int @@ -134,7 +134,7 @@ data class Foo() let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ foo1 = globalThis.Object.freeze(new Foo2.class()); //│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } //│ = 1 @@ -147,7 +147,7 @@ fun pow(x) = case 0 then 1 n then x * pow(x)(n-1) //│ JS (unsanitized): -//│ let pow; +//│ let pow; /** scoped **/ //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) {{ @@ -173,7 +173,7 @@ fun nott = case true then false false then true //│ JS (unsanitized): -//│ let nott; +//│ let nott; /** scoped **/ //│ nott = function nott() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { @@ -194,7 +194,7 @@ fun nott = case :sjs nott of false //│ JS (unsanitized): -//│ let tmp; tmp = nott(); tmp(false) +//│ let tmp; /** scoped **/ tmp = nott(); tmp(false) //│ = true //│ Type: Bool @@ -204,7 +204,7 @@ fun fact = case 0 then 1 n then n * fact(n - 1) //│ JS (unsanitized): -//│ let fact; +//│ let fact; /** scoped **/ //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) {{ @@ -260,7 +260,8 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let x4, y; +//│ let x4; +//│ let y; /** scoped **/ //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); //│ y.value @@ -271,7 +272,8 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let x5, y1; +//│ let x5; +//│ let y1; /** scoped **/ //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); //│ y1.value = 0; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 44ca37d219..aaa6637bd8 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -87,7 +87,7 @@ fun test2() = //│ ║ l.83: case 0 then 0 //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let test22; +//│ let test22; /** scoped **/ //│ test22 = function test2() {{ //│ let funny, tmp1; /** scoped **/ //│ funny = function funny() { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index 9cb5c5210c..d7ff1e8b8d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -11,7 +11,7 @@ empty.0 :sjs let single = [1] //│ JS (unsanitized): -//│ let single; single = globalThis.Object.freeze([ 1 ]); +//│ let single; /** scoped **/ single = globalThis.Object.freeze([ 1 ]); //│ single = [1] single.0 @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls index ce774c920f..0d443fdd05 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls @@ -9,7 +9,7 @@ object Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar1; +//│ let Bar1; /** scoped **/ //│ globalThis.Object.freeze(class Bar { //│ static { //│ Bar1 = globalThis.Object.freeze(new this) @@ -49,7 +49,7 @@ module Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar3; +//│ let Bar3; /** scoped **/ //│ globalThis.Object.freeze(class Bar2 { //│ static { //│ Bar3 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls index ed25930a28..454e29826f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls @@ -29,7 +29,7 @@ new 2 + 2 :re new! 2 + 2 //│ JS (unsanitized): -//│ let tmp1; tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 +//│ let tmp1; /** scoped **/ tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 //│ ═══[RUNTIME ERROR] TypeError: 2 is not a constructor :e diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index 7bf650836b..eb9457bb23 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -39,7 +39,7 @@ print("Hi") print("Hi") 2 //│ JS (unsanitized): -//│ let tmp; tmp = Predef.print("Hi"); 2 +//│ let tmp; /** scoped **/ tmp = Predef.print("Hi"); 2 //│ Lowered: //│ Program: //│ imports = Nil @@ -60,6 +60,7 @@ print("Hi") //│ rest = Return: \ //│ res = Lit of IntLit of 2 //│ implct = true +//│ topLevel = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls index 51390245ed..70dc34b630 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls @@ -74,7 +74,7 @@ id(+)(1, 2) :re id(+)(1) //│ JS (unsanitized): -//│ let lambda4, tmp1; +//│ let lambda4, tmp1; /** scoped **/ //│ lambda4 = (undefined, function (arg1, arg2) { //│ return arg1 + arg2 //│ }); @@ -95,7 +95,7 @@ fun (+) lol(a, b) = [a, b] :sjs id(~)(2) //│ JS (unsanitized): -//│ let lambda5, tmp2; +//│ let lambda5, tmp2; /** scoped **/ //│ lambda5 = (undefined, function (arg) { //│ return ~ arg //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 443e468c07..e7081a77fd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -15,7 +15,7 @@ fun test(x) = Some(v) then print(v) None then print("none") //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test(x) {{ //│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ //│ if (x instanceof Some.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 993b31c6dd..a0de0717d3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -86,7 +86,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let isDefined, lambda12; +//│ let isDefined, lambda12; /** scoped **/ //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index ffdefba167..e145bfd6cb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,7 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let Outer1; +//│ let Outer1, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -23,7 +23,6 @@ data class Outer(a, b) with //│ Outer1.class = this //│ } //│ constructor(a, b) { -//│ let tmp, tmp1, tmp2; //│ this.a = a; //│ this.b = b; //│ const this$Outer = this; @@ -35,12 +34,11 @@ data class Outer(a, b) with //│ this$Outer.Inner.class = this //│ } //│ constructor(c) { -//│ let tmp3, tmp4, tmp5; //│ this.c = c; -//│ tmp3 = Predef.print(this$Outer.a); -//│ tmp4 = Predef.print(this.c); -//│ tmp5 = this.i1(this$Outer.a); -//│ Predef.print(tmp5); +//│ tmp = Predef.print(this$Outer.a); +//│ tmp1 = Predef.print(this.c); +//│ tmp2 = this.i1(this$Outer.a); +//│ Predef.print(tmp2); //│ } //│ i1(d) { //│ return globalThis.Object.freeze([ @@ -52,19 +50,19 @@ data class Outer(a, b) with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Inner", ["c"]]; //│ }); -//│ tmp = this.Inner(this.a); -//│ this.i = tmp; -//│ tmp1 = Predef.print(this.i.c); -//│ tmp2 = runtime.safeCall(this.i.i1(1)); -//│ Predef.print(tmp2); +//│ tmp3 = this.Inner(this.a); +//│ this.i = tmp3; +//│ tmp4 = Predef.print(this.i.c); +//│ tmp5 = runtime.safeCall(this.i.i1(1)); +//│ Predef.print(tmp5); //│ } //│ o1(c) { //│ return this.Inner(c) //│ } //│ o2(c, d) {{ -//│ let tmp; /** scoped **/ -//│ tmp = this.Inner(c); -//│ return tmp.i1(d) +//│ let tmp6; /** scoped **/ +//│ tmp6 = this.Inner(c); +//│ return tmp6.i1(d) //│ } //│ } //│ toString() { return runtime.render(this); } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index cb9f81e5ce..2689aaa7f2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -6,7 +6,7 @@ fun test(a) = class C with { val x = a } new C //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test(a) {{ //│ let C1; /** scoped **/ //│ globalThis.Object.freeze(class C { @@ -38,7 +38,7 @@ fun test(x) = data class Foo(a, b) Foo(x, x + 1) //│ JS (unsanitized): -//│ let test2; +//│ let test2; /** scoped **/ //│ test2 = function test(x) {{ //│ let Foo2, tmp; /** scoped **/ //│ Foo2 = function Foo(a, b) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index ddc9b4cba6..9c37025e6a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,13 +9,15 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; -//│ scrut = Some(0); -//│ if (scrut instanceof Some.class) { -//│ argument0$ = scrut.value; -//│ x = argument0$; -//│ x -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let scrut, x, argument0$; /** scoped **/ +//│ scrut = Some(0); +//│ if (scrut instanceof Some.class) { +//│ argument0$ = scrut.value; +//│ x = argument0$; +//│ x +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ = 0 @@ -26,12 +28,14 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ let x1, argument0$1; -//│ if (s instanceof Some.class) { -//│ argument0$1 = s.value; -//│ x1 = argument0$1; -//│ x1 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let x1, argument0$1; /** scoped **/ +//│ if (s instanceof Some.class) { +//│ argument0$1 = s.value; +//│ x1 = argument0$1; +//│ x1 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ = 0 if s is @@ -119,7 +123,7 @@ fun f(x) = if x is None then "ok" else print("oops") //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ f4 = function f(x3) {{ //│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ //│ split_root$1: { @@ -166,7 +170,7 @@ fun f(x) = if x is Some(u) then u Pair(a, b) then a + b //│ JS (unsanitized): -//│ let f5; +//│ let f5; /** scoped **/ //│ f5 = function f(x3) {{ //│ let u, a, b, argument0$4, argument1$; /** scoped **/ //│ if (x3 instanceof Some.class) { @@ -197,7 +201,7 @@ fun f(x) = print of if x is None then "ok" else "oops" //│ JS (unsanitized): -//│ let f6; +//│ let f6; /** scoped **/ //│ f6 = function f(x3) {{ //│ let argument0$4, tmp10; /** scoped **/ //│ split_root$1: { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls index 1c596f3fc1..fc79ff4035 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls @@ -6,7 +6,7 @@ data class Foo(arguments) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(arguments1) { //│ return globalThis.Object.freeze(new Foo.class(arguments1)); //│ }; @@ -24,7 +24,7 @@ data class Foo(arguments) data class Foo(eval) //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ Foo3 = function Foo(eval1) { //│ return globalThis.Object.freeze(new Foo.class(eval1)); //│ }; @@ -42,7 +42,7 @@ data class Foo(eval) data class Foo(static) //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ Foo5 = function Foo(static1) { //│ return globalThis.Object.freeze(new Foo.class(static1)); //│ }; @@ -60,7 +60,7 @@ data class Foo(static) data class Foo(v') //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7; /** scoped **/ //│ Foo7 = function Foo(v$_) { //│ return globalThis.Object.freeze(new Foo.class(v$_)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index 8d18e010f2..c697cc38ad 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -7,7 +7,7 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f() {{ //│ let tmp; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.console.log("ok")); @@ -17,7 +17,7 @@ fun f() = fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f() {{ //│ let tmp; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.console.log("ok")); @@ -27,7 +27,9 @@ fun f() = { console.log("ok"), 42 } fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f2; f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; 42 +//│ let f2; /** scoped **/ +//│ f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; +//│ 42 //│ = 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index be5f1aee43..1770b21e64 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,14 +7,14 @@ :silent declare val console //│ JS (unsanitized): -//│ +//│ let console; /** scoped **/ console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.console.log("a")); //│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 @@ -25,7 +25,7 @@ console.log("b") let l = console.log l(123) //│ JS (unsanitized): -//│ let l; l = globalThis.console.log; runtime.safeCall(l(123)) +//│ let l; /** scoped **/ l = globalThis.console.log; runtime.safeCall(l(123)) //│ > 123 //│ l = fun log @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3, tmp4; +//│ let x, y, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index a4fddd8d26..421a23afac 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; x = undefined; +//│ let x; /** scoped **/ x = undefined; //│ x = undefined x = 1 @@ -31,7 +31,7 @@ x let y = 1 //│ JS (unsanitized): -//│ let y; y = 1; +//│ let y; /** scoped **/ y = 1; //│ y = 1 :e @@ -45,7 +45,7 @@ z = 1 fun f() = 1 //│ JS (unsanitized): -//│ let f; f = function f() { return 1 }; +//│ let f; /** scoped **/ f = function f() { return 1 }; f //│ JS (unsanitized): @@ -56,7 +56,7 @@ f let f f(x) = x + 1 //│ JS (unsanitized): -//│ let f1, f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; +//│ let f1; let f2; /** scoped **/ f1 = function f(x1) { return x1 + 1 }; f2 = f1; //│ f = fun f f(1) @@ -68,7 +68,7 @@ f(1) let foo foo = 0 //│ JS (unsanitized): -//│ let foo; foo = 0; +//│ let foo; /** scoped **/ foo = 0; //│ foo = 0 :fixme @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; +//│ let foo1, scrut; /** scoped **/ //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; +//│ let foo2, scrut1; /** scoped **/ //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -105,7 +105,7 @@ else fun f() = foo = 42 //│ JS (unsanitized): -//│ let f3; f3 = function f() { foo2 = 42; return runtime.Unit }; +//│ let f3; /** scoped **/ f3 = function f() { foo2 = 42; return runtime.Unit }; f() //│ JS (unsanitized): @@ -123,6 +123,6 @@ fun f() = foo = 0 //│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let f4; f4 = function f() { return foo2 }; +//│ let f4; /** scoped **/ f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 5c8b93ff4b..64e60adbba 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -18,7 +18,7 @@ fun f(x) = return 0 x + 1 //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f(x) {{ //│ let scrut, tmp, tmp1; /** scoped **/ //│ scrut = x < 0; diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 5f13db5813..548e76af38 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -141,6 +141,7 @@ case //│ spread = N //│ value = Lit of StrLit of "match error" //│ rest = End of "" +//│ topLevel = false //│ rest = Return: \ //│ res = Ref: //│ l = member:lambda diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index 85ea77ef62..e353c2a33e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -7,7 +7,10 @@ let discard = drop _ //│ JS (unsanitized): -//│ let discard, discard1; discard1 = function discard(_0) { return runtime.Unit }; discard = discard1; +//│ let discard; +//│ let discard1; /** scoped **/ +//│ discard = function discard(_0) { return runtime.Unit }; +//│ discard1 = discard; //│ discard = fun discard @@ -15,7 +18,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; +//│ let a, b, tmp; /** scoped **/ //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -23,7 +26,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; +//│ let a1, b1, tmp1; /** scoped **/ //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -32,14 +35,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; +//│ let aaaa, tmp2; /** scoped **/ //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let aaaaaaaaa, bbbb, tmp3; +//│ let aaaaaaaaa, bbbb, tmp3; /** scoped **/ //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -50,7 +53,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; +//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; /** scoped **/ //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -66,7 +69,7 @@ discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ tmp5 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb" @@ -75,7 +78,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ] //│ JS (unsanitized): -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ tmp6 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", @@ -86,7 +89,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; +//│ let tmp7, tmp8; /** scoped **/ //│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index f56965a619..7fb778805c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -19,7 +19,7 @@ fun test(a) = h(d) Inner(42) //│ JS (unsanitized): -//│ let test1; +//│ let test1; /** scoped **/ //│ test1 = function test(a) {{ //│ let Inner1, tmp; /** scoped **/ //│ Inner1 = function Inner(b) { @@ -80,7 +80,7 @@ fun test(a) = print of [a, b] [C1(1), C2(2)] //│ JS (unsanitized): -//│ let test2; +//│ let test2; /** scoped **/ //│ test2 = function test(a) {{ //│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ C11 = function C1(b) { @@ -141,7 +141,7 @@ data class Foo(a) with foo() Foo(123) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -181,7 +181,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1; +//│ let Bar1, tmp; /** scoped **/ //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -190,7 +190,6 @@ Bar(1) //│ Bar1.class = this //│ } //│ constructor(x) { -//│ let tmp; //│ this.x = x; //│ tmp = this.foo(); //│ runtime.safeCall(tmp()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Functions.mls b/hkmc2/shared/src/test/mlscript/codegen/Functions.mls index 67d3f0e56c..4b3dd3c22f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Functions.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Functions.mls @@ -89,7 +89,7 @@ fun test2(y) = y + 1 //│ REPL> Sending: block$res18 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let test2, test1;try { test1 = function test1(...args) { runtime.checkArgs("test1", 1, true, args.length); let x = args[0]; return runtime.checkCall(test2(x)) }; test2 = function test2(...args) { runtime.checkArgs("test2", 1, true, args.length); let y = args[0]; return y + 1 }; block$res18 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let test2, test1;try { test1 = function test1(...args) {{ runtime.checkArgs("test1", 1, true, args.length); let x = args[0]; return runtime.checkCall(test2(x)) } }; test2 = function test2(...args) {{ runtime.checkArgs("test2", 1, true, args.length); let y = args[0]; return y + 1 } }; block$res18 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index 540121fc08..e3460c4b18 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -6,20 +6,19 @@ val x = 2 fun foo() = x + 1 //│ JS (unsanitized): -//│ let foo, x; foo = function foo() { return x + 1 }; x = 2; +//│ let foo, x; /** scoped **/ foo = function foo() { return x + 1 }; x = 2; //│ x = 2 :sjs class Test with print(foo()) //│ JS (unsanitized): -//│ let Test1; +//│ let Test1, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this //│ } //│ constructor() { -//│ let tmp; //│ tmp = foo(); //│ Predef.print(tmp); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 9439cac263..3ff90f7008 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -4,14 +4,14 @@ :sjs fun t = 42 //│ JS (unsanitized): -//│ let t; t = function t() { return 42 }; +//│ let t; /** scoped **/ t = function t() { return 42 }; :expect 42 :sjs t //│ JS (unsanitized): -//│ let tmp; tmp = t(); tmp +//│ let tmp; /** scoped **/ tmp = t(); tmp //│ = 42 @@ -37,7 +37,7 @@ fun test() = 42 whoops + whoops //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test() {{ //│ let whoops, tmp1, tmp2; /** scoped **/ //│ whoops = function whoops() {{ @@ -67,7 +67,7 @@ module T with val c = p val d = this.p //│ JS (unsanitized): -//│ let T1; +//│ let T1; /** scoped **/ //│ globalThis.Object.freeze(class T { //│ static { //│ T1 = this @@ -109,7 +109,7 @@ T.d module M with fun t = 0 //│ JS (unsanitized): -//│ let M1; +//│ let M1; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -137,7 +137,7 @@ fun test() = fun whoops = 42 whoops //│ JS (unsanitized): -//│ let test1; +//│ let test1; /** scoped **/ //│ test1 = function test() {{ //│ let whoops, tmp1; /** scoped **/ //│ whoops = function whoops() { return 42 }; @@ -166,7 +166,7 @@ fun bar() = fun baz() = 42 baz //│ JS (unsanitized): -//│ let bar; +//│ let bar; /** scoped **/ //│ bar = function bar() {{ let baz; /** scoped **/ baz = function baz() { return 42 }; return baz } }; @@ -176,7 +176,7 @@ fun baz() = fun z = 2 (x, y) => x + y + w + z //│ JS (unsanitized): -//│ let baz; +//│ let baz; /** scoped **/ //│ baz = function baz() { //│ let lambda;{ //│ let w, z; /** scoped **/ @@ -212,7 +212,7 @@ fun a() = b + d c //│ JS (unsanitized): -//│ let a; +//│ let a; /** scoped **/ //│ a = function a() {{ //│ let b, c; /** scoped **/ //│ b = function b() { @@ -243,7 +243,7 @@ fun b() = c d //│ JS (unsanitized): -//│ let b; +//│ let b; /** scoped **/ //│ b = function b() {{ //│ let c, d; /** scoped **/ //│ c = function c() { @@ -273,7 +273,7 @@ fun c() = e + f d //│ JS (unsanitized): -//│ let c; +//│ let c; /** scoped **/ //│ c = function c() {{ //│ let d, f, tmp4; /** scoped **/ //│ f = function f() { @@ -303,7 +303,7 @@ c() data class Foo(x) with fun oops = x //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index e93f631cf8..3f3a143f2a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,11 +21,13 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ let scrut; -//│ scrut = false; -//│ if (scrut === true) { -//│ 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let scrut; /** scoped **/ +//│ scrut = false; +//│ if (scrut === true) { +//│ 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'freeze') //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'freeze') @@ -37,7 +39,7 @@ fun foo() = if false then 0 foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() {{ //│ let scrut1; /** scoped **/ //│ scrut1 = false; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index 84ccc36c3c..e77ced4ebc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -10,7 +10,7 @@ object Test with print(Test) Test.x //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = globalThis.Object.freeze(new this) @@ -47,7 +47,7 @@ Test.foo() :sjs val Test = "oops" //│ JS (unsanitized): -//│ let Test2; Test2 = "oops"; +//│ let Test2; /** scoped **/ Test2 = "oops"; //│ Test = "oops" :re @@ -61,7 +61,13 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let x, f, x1, f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) +//│ let f; +//│ let x, f1, x1; /** scoped **/ +//│ x = 1; +//│ f = function f() { return x }; +//│ f1 = f; +//│ x1 = 2; +//│ runtime.safeCall(f1()) //│ = 1 //│ f = fun f //│ x = 2 @@ -73,10 +79,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.74: let x = 2 +//│ ║ l.80: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.73: val x = 1 +//│ ║ l.79: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test4 { @@ -116,7 +122,7 @@ data class Cls(x) with fun bar = x print(this.x, x) //│ JS (unsanitized): -//│ let Cls1; +//│ let Cls1; /** scoped **/ //│ Cls1 = function Cls(x2) { //│ return globalThis.Object.freeze(new Cls.class(x2)); //│ }; @@ -180,7 +186,7 @@ module Whoops with val w: module Whoops = this fun g() = f() //│ JS (unsanitized): -//│ let Whoops2; +//│ let Whoops2; /** scoped **/ //│ globalThis.Object.freeze(class Whoops { //│ static { //│ Whoops2 = this @@ -227,7 +233,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.228: Runtime +//│ ║ l.234: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index c048b26350..4a569315b8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let f, lambda; +//│ let f, lambda; /** scoped **/ //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,7 +26,7 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f1, lambda1; +//│ let f1, lambda1; /** scoped **/ //│ lambda1 = (undefined, function (x) {{ //│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { @@ -42,7 +42,7 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f2, lambda2; +//│ let f2, lambda2; /** scoped **/ //│ lambda2 = (undefined, function (x) {{ //│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls index 07b13f5dfb..e2425e33b6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls @@ -39,7 +39,9 @@ Some(1) :sjs (new Some(1)) isDefined() //│ JS (unsanitized): -//│ let tmp3; tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); Option.isDefined(tmp3) +//│ let tmp3; /** scoped **/ +//│ tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); +//│ Option.isDefined(tmp3) //│ = true new Some(1) isDefined() @@ -66,14 +68,14 @@ None == Option.None :re Option.oops //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.67: Option.oops +//│ ║ l.69: Option.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' :e open Option { oops } //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.74: open Option { oops } +//│ ║ l.76: open Option { oops } //│ ╙── ^^^^ oops @@ -82,7 +84,7 @@ oops :re Option.None.oops //│ ╔══[ERROR] Object 'None' does not contain member 'oops' -//│ ║ l.83: Option.None.oops +//│ ║ l.85: Option.None.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index 279a112807..a99d4baea0 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -11,7 +11,7 @@ fun foo() = "a" ~ "b" ~ "c" foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() {{ //│ let tmp; /** scoped **/ //│ tmp = M.concat("a", "b"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index e688b7c912..eb6fc23793 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -3,7 +3,7 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda; +//│ let lambda; /** scoped **/ //│ lambda = (undefined, function (x) {{ //│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ tmp = x + 1; @@ -19,7 +19,7 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda1; +//│ let lambda1; /** scoped **/ //│ lambda1 = (undefined, function (x) {{ //│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp = x + 1; @@ -36,13 +36,13 @@ :sjs (x => x) + 1 //│ JS (unsanitized): -//│ let lambda2; lambda2 = (undefined, function (x) { return x }); lambda2 + 1 -//│ = "function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }1" +//│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 +//│ = "function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }1" :sjs 1 + (x => x) //│ JS (unsanitized): -//│ let lambda3; lambda3 = (undefined, function (x) { return x }); 1 + lambda3 -//│ = "1function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }" +//│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 +//│ = "1function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 66b4dc8e2c..04984a7233 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -58,7 +58,7 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ let tmp; +//│ let tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ split_1$: { @@ -107,7 +107,7 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3; +//│ let x, tmp1, tmp2, tmp3; /** scoped **/ //│ if (a instanceof A.class) { //│ tmp2 = 1; //│ } else if (a instanceof B.class) { @@ -148,13 +148,15 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ let tmp5; -//│ tmp5 = 2; -//│ if (a instanceof A.class) { -//│ 1 -//│ } else if (a instanceof B.class) { -//│ 2 + tmp5 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let tmp5; /** scoped **/ +//│ tmp5 = 2; +//│ if (a instanceof A.class) { +//│ 1 +//│ } else if (a instanceof B.class) { +//│ 2 + tmp5 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ = 1 @@ -170,14 +172,16 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ let tmp6; -//│ if (a instanceof A.class) { -//│ 1 -//│ } else { -//│ tmp6 = printAndId(3); -//│ if (a instanceof B.class) { -//│ 2 + tmp6 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let tmp6; /** scoped **/ +//│ if (a instanceof A.class) { +//│ 1 +//│ } else { +//│ tmp6 = printAndId(3); +//│ if (a instanceof B.class) { +//│ 2 + tmp6 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ } //│ = 1 @@ -193,18 +197,20 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ let x1, tmp7; -//│ if (a instanceof A.class) { -//│ 1 -//│ } else if (a instanceof B.class) { -//│ tmp7 = 2; -//│ x1 = tmp7; -//│ Predef.print(x1) -//│ } else if (a instanceof C.class) { -//│ tmp7 = 3; -//│ x1 = tmp7; -//│ Predef.print(x1) -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let x1, tmp7; /** scoped **/ +//│ if (a instanceof A.class) { +//│ 1 +//│ } else if (a instanceof B.class) { +//│ tmp7 = 2; +//│ x1 = tmp7; +//│ Predef.print(x1) +//│ } else if (a instanceof C.class) { +//│ tmp7 = 3; +//│ x1 = tmp7; +//│ Predef.print(x1) +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ = 1 @@ -222,23 +228,25 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; -//│ if (a instanceof B.class) { -//│ 1 -//│ } else { -//│ if (a instanceof A.class) { -//│ tmp8 = 2; -//│ } else if (a instanceof C.class) { -//│ tmp8 = 3; +//│ { +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ if (a instanceof B.class) { +//│ 1 //│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ if (a instanceof A.class) { +//│ tmp8 = 2; +//│ } else if (a instanceof C.class) { +//│ tmp8 = 3; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ x2 = tmp8; +//│ tmp9 = Predef.print(x2); +//│ tmp10 = x2 + 1; +//│ tmp11 = Predef.print(tmp10); +//│ tmp12 = x2 + 2; +//│ Predef.print(tmp12) //│ } -//│ x2 = tmp8; -//│ tmp9 = Predef.print(x2); -//│ tmp10 = x2 + 1; -//│ tmp11 = Predef.print(tmp10); -//│ tmp12 = x2 + 2; -//│ Predef.print(tmp12) //│ } //│ > 2 //│ > 3 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index f82067f6d7..19b5d0c1e1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,7 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let Test1; +//│ let Test1, tmp1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -51,7 +51,6 @@ module Test with //│ } //│ static #s; //│ static { -//│ let tmp1; //│ Test.#s = 1; //│ tmp1 = Predef.print(Test.#s); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index ba7417ebd2..dc8f7d5442 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -4,7 +4,7 @@ :sjs module None //│ JS (unsanitized): -//│ let None1; +//│ let None1; /** scoped **/ //│ globalThis.Object.freeze(class None { //│ static { //│ None1 = this @@ -55,7 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1; +//│ let M1, tmp; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -64,7 +64,6 @@ module M with //│ runtime.Unit; //│ } //│ static { -//│ let tmp; //│ globalThis.Object.freeze(class C { //│ static { //│ M.C = this @@ -111,7 +110,7 @@ M.y :re M.oops //│ ╔══[ERROR] Module 'M' does not contain member 'oops' -//│ ║ l.112: M.oops +//│ ║ l.111: M.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' @@ -123,7 +122,7 @@ M.oops module M with val m: module M = M //│ JS (unsanitized): -//│ let M3; +//│ let M3; /** scoped **/ //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -156,7 +155,7 @@ module AA with val y = 2 [x, y] //│ JS (unsanitized): -//│ let AA1; +//│ let AA1; /** scoped **/ //│ globalThis.Object.freeze(class AA { //│ static { //│ AA1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 1318302565..9f2d151267 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -52,7 +52,7 @@ none() :sjs val Option = "Oops" //│ JS (unsanitized): -//│ let Option1; Option1 = "Oops"; +//│ let Option1; /** scoped **/ Option1 = "Oops"; //│ Option = "Oops" :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 079a3249d3..4bc8484693 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -10,7 +10,7 @@ fun isDefined(x) = if x is Some then true None then false //│ JS (unsanitized): -//│ let isDefined; +//│ let isDefined; /** scoped **/ //│ isDefined = function isDefined(x) { //│ if (x instanceof Some.class) { //│ return true @@ -31,7 +31,7 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; +//│ let isDefined1, lambda; /** scoped **/ //│ lambda = (undefined, function (caseScrut) {{ //│ let argument0$; /** scoped **/ //│ if (caseScrut instanceof Some.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 71bbc57a17..04625446e1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -6,7 +6,7 @@ data class Foo() //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo() { //│ return globalThis.Object.freeze(new Foo.class()); //│ }; @@ -37,7 +37,7 @@ Foo.class data class Foo(a) //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ Foo3 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -64,19 +64,19 @@ Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; tmp = Foo2(1); tmp.a +//│ let tmp; /** scoped **/ tmp = Foo2(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; foo = function foo(y) { return Foo2(y) }; foo(27) +//│ let foo; /** scoped **/ foo = function foo(y) { return Foo2(y) }; foo(27) //│ = Foo(27) data class Foo(a, b) //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ Foo5 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -94,17 +94,17 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; foo1 = Foo4; +//│ let foo1; /** scoped **/ foo1 = Foo4; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) //│ JS (unsanitized): -//│ let f; f = runtime.safeCall(foo1(1, 2)); +//│ let f; /** scoped **/ f = runtime.safeCall(foo1(1, 2)); //│ f = Foo(1, 2) let f = new! foo(1, 2) //│ JS (unsanitized): -//│ let f1; f1 = globalThis.Object.freeze(new foo1(1, 2)); +//│ let f1; /** scoped **/ f1 = globalThis.Object.freeze(new foo1(1, 2)); //│ f = Foo(1, 2) f.a @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; f2 = Foo4(1, 2); +//│ let f2; /** scoped **/ f2 = Foo4(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) +//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -144,7 +144,7 @@ data class Inner(c) with fun i1(d) = c + d print(c) //│ JS (unsanitized): -//│ let Inner1; +//│ let Inner1; /** scoped **/ //│ Inner1 = function Inner(c) { //│ return globalThis.Object.freeze(new Inner.class(c)); //│ }; @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; i = globalThis.Object.freeze(new Inner.class(100)); +//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner.class(100)); //│ > 100 //│ i = Inner(100) @@ -185,7 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -194,7 +194,6 @@ class Foo(x, val y, z, val z, z) with //│ Foo7.class = this //│ } //│ constructor(x, y, z, z1, z2) { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ this.#x = x; //│ this.y = y; //│ this.#z = z; @@ -237,13 +236,13 @@ Foo(10, 20, 30, 40, 50) class Foo(val z, val z) //│ ╔══[ERROR] Duplicate definition of member named 'z'. //│ ╟── Defined at: -//│ ║ l.237: class Foo(val z, val z) +//│ ║ l.236: class Foo(val z, val z) //│ ║ ^ //│ ╟── Defined at: -//│ ║ l.237: class Foo(val z, val z) +//│ ║ l.236: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo9; +//│ let Foo9; /** scoped **/ //│ Foo9 = function Foo(z, z1) { //│ return globalThis.Object.freeze(new Foo.class(z, z1)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index dd66cf1dbf..06e8647e58 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -12,7 +12,7 @@ f(2) :sjs let f = foo(1, _, _) //│ JS (unsanitized): -//│ let f2, f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; +//│ let f2; let f3; /** scoped **/ f2 = function f(_0, _1) { return foo(1, _0, _1) }; f3 = f2; //│ f = fun f f(2, 3) @@ -79,7 +79,10 @@ h(1) :sjs let i = _(0, 1, _) //│ JS (unsanitized): -//│ let i, i1; i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; i = i1; +//│ let i; +//│ let i1; /** scoped **/ +//│ i = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; +//│ i1 = i; //│ i = fun i i(print, 2) @@ -199,7 +202,7 @@ _ - _ of 1, 2 :pe |> 1 //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.200: |> 1 +//│ ║ l.203: |> 1 //│ ╙── ^ //│ = fun pipeInto @@ -207,7 +210,7 @@ _ - _ of 1, 2 :re |> (1) //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.208: |> (1) +//│ ║ l.211: |> (1) //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] TypeError: f is not a function @@ -225,13 +228,15 @@ _ - _ of 1, 2 :sjs 1 \ (_ - 2) //│ JS (unsanitized): -//│ let lambda38; lambda38 = (undefined, function (_0) { return _0 - 2 }); Predef.passTo(1, lambda38) +//│ let lambda38; /** scoped **/ +//│ lambda38 = (undefined, function (_0) { return _0 - 2 }); +//│ Predef.passTo(1, lambda38) //│ = fun :sjs 1 \ (_ - 2)() //│ JS (unsanitized): -//│ let lambda39, tmp19; +//│ let lambda39, tmp19; /** scoped **/ //│ lambda39 = (undefined, function (_0) { //│ return _0 - 2 //│ }); @@ -248,13 +253,13 @@ _ - _ of 1, 2 :w let f = if _ then 1 else 0 //│ ╔══[WARNING] This catch-all clause makes the following branches unreachable. -//│ ║ l.249: let f = if _ then 1 else 0 +//│ ║ l.254: let f = if _ then 1 else 0 //│ ║ ^^^^^^ //│ ╟── This branch is unreachable. -//│ ║ l.249: let f = if _ then 1 else 0 +//│ ║ l.254: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; /** scoped **/ tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs @@ -263,7 +268,7 @@ fun f(x) = 0 then 1 _ then 2 //│ JS (unsanitized): -//│ let f16; +//│ let f16; /** scoped **/ //│ f16 = function f(x3) {{ //│ let scrut; /** scoped **/ //│ scrut = x3 == 0; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index e5395f1812..6c559ab5cc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -6,7 +6,7 @@ class Foo //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -23,9 +23,11 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ let scrut; -//│ scrut = globalThis.Object.freeze(new Foo()); -//│ if (scrut instanceof Foo) { true } else { false } +//│ { +//│ let scrut; /** scoped **/ +//│ scrut = globalThis.Object.freeze(new Foo()); +//│ if (scrut instanceof Foo) { true } else { false } +//│ } //│ = true new Foo @@ -53,7 +55,7 @@ Foo() data class Foo with { print("hi") } print("ok") //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ globalThis.Object.freeze(class Foo2 { //│ static { //│ Foo3 = this @@ -72,7 +74,7 @@ fun test() = print("ok") Foo //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test() {{ //│ let Foo5, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo4 { @@ -92,14 +94,14 @@ fun test() = let t = test() //│ JS (unsanitized): -//│ let t; t = test(); +//│ let t; /** scoped **/ t = test(); //│ > ok //│ t = class Foo :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.100: new t +//│ ║ l.102: new t //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -116,7 +118,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.117: new t() +//│ ║ l.119: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -136,7 +138,7 @@ class Foo with let y = x + 1 fun z() = y + x //│ JS (unsanitized): -//│ let Foo6; +//│ let Foo6; /** scoped **/ //│ globalThis.Object.freeze(class Foo5 { //│ static { //│ Foo6 = this @@ -163,7 +165,7 @@ class Foo with fun z2() = 6 print("hello") //│ JS (unsanitized): -//│ let Foo8; +//│ let Foo8; /** scoped **/ //│ globalThis.Object.freeze(class Foo7 { //│ static { //│ Foo8 = this @@ -193,7 +195,7 @@ class Foo with fun foo(y) = x + y fun bar(z) = foo(z) + 1 //│ JS (unsanitized): -//│ let Foo10; +//│ let Foo10; /** scoped **/ //│ globalThis.Object.freeze(class Foo9 { //│ static { //│ Foo10 = this @@ -219,7 +221,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let a, tmp, tmp1, tmp2, tmp3; +//│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); @@ -239,13 +241,13 @@ class Foo with val x = 2 //│ ╔══[ERROR] Multiple definitions of symbol 'x' //│ ╟── defined here -//│ ║ l.238: val x = 1 +//│ ║ l.240: val x = 1 //│ ║ ^^^^^^^^^ //│ ╟── defined here -//│ ║ l.239: val x = 2 +//│ ║ l.241: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo12; +//│ let Foo12; /** scoped **/ //│ globalThis.Object.freeze(class Foo11 { //│ static { //│ Foo12 = this @@ -263,13 +265,13 @@ class Foo with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.264: let x = 2 +//│ ║ l.266: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.263: val x = 1 +//│ ║ l.265: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo14; +//│ let Foo14; /** scoped **/ //│ globalThis.Object.freeze(class Foo13 { //│ static { //│ Foo14 = this @@ -293,10 +295,10 @@ class Foo with :e class Foo with val x = 1 //│ ╔══[ERROR] Illegal body of class definition (should be a block; found term definition). -//│ ║ l.294: class Foo with val x = 1 +//│ ║ l.296: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo16; +//│ let Foo16; /** scoped **/ //│ globalThis.Object.freeze(class Foo15 { //│ static { //│ Foo16 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls index 8a571ad88e..ea8a6fed34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls @@ -7,7 +7,7 @@ let x = 1 :sjs let x' = 2 //│ JS (unsanitized): -//│ let x$_; x$_ = 2; +//│ let x$_; /** scoped **/ x$_ = 2; //│ x' = 2 x' diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 4defe108c8..3cd21a98c2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; +//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index 3e823a55c8..3ae4eea381 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,15 +76,16 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let x1, tmp6, tmp7, tmp8, arr2; +//│ let x1, tmp6, tmp7, arr2; +//│ let tmp8; /** scoped **/ //│ tmp6 = globalThis.Object.freeze(new Term.Symbol("x")); //│ x1 = globalThis.Object.freeze(new Term.Ref(tmp6)); -//│ tmp7 = Predef.print(x1); +//│ tmp8 = Predef.print(x1); //│ arr2 = globalThis.Object.freeze([ //│ tmp6 //│ ]); -//│ tmp8 = x1; -//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp8)) +//│ tmp7 = x1; +//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp7)) //│ > Ref(Symbol("x")) //│ = Lam([Symbol("x")], Ref(Symbol("x"))) diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index dae7d44373..d85915ee33 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -4,7 +4,7 @@ :sjs fun foo() = if false do foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() {{ //│ let scrut; /** scoped **/ //│ scrut = false; @@ -15,7 +15,7 @@ fun foo() = if false do foo() :sjs fun foo() = foo() //│ JS (unsanitized): -//│ let foo1; foo1 = function foo() { return foo1() }; +//│ let foo1; /** scoped **/ foo1 = function foo() { return foo1() }; :sjs @@ -23,7 +23,7 @@ data class Foo(x) with class Bar with val y = x //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; @@ -55,7 +55,7 @@ fun foo() = fun bar() = bar() bar() //│ JS (unsanitized): -//│ let foo2; +//│ let foo2; /** scoped **/ //│ foo2 = function foo() {{ //│ let bar; /** scoped **/ //│ bar = function bar() { return bar() }; @@ -70,7 +70,7 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; +//│ let f, f1; /** scoped **/ //│ f1 = 1; //│ f = function f() {{ let tmp; /** scoped **/ tmp = f(); return tmp } }; //│ runtime.Unit @@ -87,7 +87,7 @@ fun foo(x) = foo //│ ║ l.82: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let foo3, foo4; foo3 = function foo(x) { return foo4 }; foo4 = 1; +//│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; //│ foo = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/Repl.mls b/hkmc2/shared/src/test/mlscript/codegen/Repl.mls index 9fad84082a..a4c490fccb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Repl.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Repl.mls @@ -10,7 +10,7 @@ fun res() = 1 //│ REPL> Sending: block$res3 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let res2;try { res2 = function res(...args) { runtime.checkArgs("res", 0, true, args.length); return 1 }; block$res3 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let res2;try { res2 = function res(...args) {{ runtime.checkArgs("res", 0, true, args.length); return 1 } }; block$res3 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index e09a382fc7..5c9aba15e5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -7,11 +7,12 @@ fun f(x, y) = x + y f(2, 3) //│ JS: -//│ f = function f(...args) { -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let x = args[0]; -//│ let y = args[1]; -//│ return x + y +//│ f = function f(...args) {{ +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let x = args[0]; +//│ let y = args[1]; +//│ return x + y +//│ } //│ }; //│ block$res1 = runtime.checkCall(f(2, 3)); //│ undefined @@ -45,16 +46,18 @@ id(f)(2) fun f(x)(y, z) = x + y + z id(f)(3)(4) //│ JS: -//│ f1 = function f(...args) { -//│ runtime.checkArgs("f", 1, true, args.length); -//│ let x = args[0]; -//│ return (...args1) => { -//│ runtime.checkArgs("", 2, true, args1.length); -//│ let y = args1[0]; -//│ let z = args1[1];{ -//│ let tmp4; /** scoped **/ -//│ tmp4 = x + y; -//│ return tmp4 + z +//│ f1 = function f(...args) {{ +//│ runtime.checkArgs("f", 1, true, args.length); +//│ let x = args[0]; +//│ return (...args1) => {{ +//│ runtime.checkArgs("", 2, true, args1.length); +//│ let y = args1[0]; +//│ let z = args1[1];{ +//│ let tmp4; /** scoped **/ +//│ tmp4 = x + y; +//│ return tmp4 + z +//│ } +//│ } //│ } //│ } //│ }; @@ -78,11 +81,12 @@ let f = (x, y) => x + y in f(2) let f = (x, y) => x + y f(2) //│ JS: -//│ f6 = function f(...args) { -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let x = args[0]; -//│ let y = args[1]; -//│ return x + y +//│ f6 = function f(...args) {{ +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let x = args[0]; +//│ let y = args[1]; +//│ return x + y +//│ } //│ }; //│ f5 = f6; //│ block$res6 = runtime.safeCall(f5(2)); @@ -143,14 +147,15 @@ id(Cls(1, 2)).f(3) //│ this.y = y; //│ } //│ get f$__checkNotMethod() { runtime.deboundMethod("f", "Cls"); } -//│ f(...args) { -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let z = args[0]; -//│ let p = args[1];{ -//│ let tmp8, tmp9; /** scoped **/ -//│ tmp8 = this.x + this.y; -//│ tmp9 = tmp8 + z; -//│ return tmp9 + p +//│ f(...args) {{ +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let z = args[0]; +//│ let p = args[1];{ +//│ let tmp8, tmp9; /** scoped **/ +//│ tmp8 = this.x + this.y; +//│ tmp9 = tmp8 + z; +//│ return tmp9 + p +//│ } //│ } //│ } //│ toString() { return runtime.render(this); } @@ -181,20 +186,22 @@ id(Cls(1, 2)).f(3, 4)(5) //│ this.y = y; //│ } //│ get f$__checkNotMethod() { runtime.deboundMethod("f", "Cls"); } -//│ f(...args) { -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let z = args[0]; -//│ let p = args[1]; -//│ return (...args1) => { -//│ runtime.checkArgs("", 2, true, args1.length); -//│ let q = args1[0]; -//│ let s = args1[1];{ -//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ -//│ tmp11 = this.x + this.y; -//│ tmp12 = tmp11 + z; -//│ tmp13 = tmp12 + p; -//│ tmp14 = tmp13 + q; -//│ return tmp14 + s +//│ f(...args) {{ +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let z = args[0]; +//│ let p = args[1]; +//│ return (...args1) => {{ +//│ runtime.checkArgs("", 2, true, args1.length); +//│ let q = args1[0]; +//│ let s = args1[1];{ +//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ +//│ tmp11 = this.x + this.y; +//│ tmp12 = tmp11 + z; +//│ tmp13 = tmp12 + p; +//│ tmp14 = tmp13 + q; +//│ return tmp14 + s +//│ } +//│ } //│ } //│ } //│ } @@ -273,10 +280,11 @@ if M.A(1).y is //│ this.x = x1; //│ } //│ get f$__checkNotMethod() { runtime.deboundMethod("f", "A"); } -//│ f(...args) { -//│ runtime.checkArgs("f", 1, true, args.length); -//│ let y = args[0]; -//│ return this.x + y +//│ f(...args) {{ +//│ runtime.checkArgs("f", 1, true, args.length); +//│ let y = args[0]; +//│ return this.x + y +//│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A", ["x"]]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index dce6ad997c..09ecbcae09 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,7 +19,7 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f() {{ //│ let scrut, a, b; /** scoped **/ //│ scrut = true; @@ -41,7 +41,7 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; +//│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { //│ let lambda;{ //│ let a, tmp1, tmp2; /** scoped **/ @@ -86,7 +86,7 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f() {{ //│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { @@ -125,7 +125,7 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ f2 = function f() {{ //│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { @@ -165,7 +165,7 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(x1) {{ //│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; @@ -207,7 +207,7 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ f4 = function f() {{ //│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ //│ y1 = x + 1; @@ -257,7 +257,7 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; +//│ let f5; /** scoped **/ //│ f5 = function f(x1, y1) {{ //│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 7682c56896..56d63f1ecd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -5,7 +5,7 @@ module Foo with val self: module Foo = Foo //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -61,13 +61,12 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11; +//│ let Foo11, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) //│ } //│ constructor() { -//│ let tmp; //│ tmp = Predef.id(Foo11); //│ this.self = tmp; //│ Object.defineProperty(this, "class", { diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 523db5a30a..4d2bef3a02 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -7,7 +7,7 @@ let x = 0 :sjs let x += 1 //│ JS (unsanitized): -//│ let x1; x1 = x + 1; +//│ let x1; /** scoped **/ x1 = x + 1; //│ x = 1 x @@ -21,7 +21,7 @@ set x = 0 :sjs set x += 1 //│ JS (unsanitized): -//│ let tmp; tmp = x1 + 1; x1 = tmp; runtime.Unit +//│ let tmp; /** scoped **/ tmp = x1 + 1; x1 = tmp; runtime.Unit x //│ = 1 @@ -30,7 +30,7 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let old, tmp1, tmp2, tmp3; +//│ let old, tmp1, tmp2, tmp3; /** scoped **/ //│ old = x1; //│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } //│ tmp1 @@ -69,7 +69,7 @@ fun example() = print(get_x()) example() //│ JS (unsanitized): -//│ let example2; +//│ let example2; /** scoped **/ //│ example2 = function example() { //│ let get_x;{ //│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ @@ -112,7 +112,7 @@ fun example() = y example() //│ JS (unsanitized): -//│ let example3; +//│ let example3; /** scoped **/ //│ example3 = function example() { //│ let get_x;{ //│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ diff --git a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls index 9be3a3a5b7..228d0613df 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls @@ -19,7 +19,7 @@ foo(0, ...a) :sjs foo(1, ...[2, 3], 4) //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) //│ = [1, 2, 3, 4] :re diff --git a/hkmc2/shared/src/test/mlscript/codegen/This.mls b/hkmc2/shared/src/test/mlscript/codegen/This.mls index 64418595dd..9d809eb9e6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/This.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/This.mls @@ -10,13 +10,14 @@ fun test(x) = //│ ║ l.8: [this.a, x] //│ ╙── ^^^^ //│ JS (unsanitized): -//│ let test; test = function test(x) { return globalThis.Object.freeze([]) }; +//│ let test; /** scoped **/ test = function test(x) { return globalThis.Object.freeze([]) }; :sjs fun test(x) = [globalThis.a, x] //│ JS (unsanitized): -//│ let test1; test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; +//│ let test1; /** scoped **/ +//│ test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; :re test(123) @@ -42,7 +43,7 @@ module Test with fun test2(x) = [this.a, x] //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 44fb9381b8..3bb6a4a271 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; tmp = call(Example, oops); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = call(Example, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) +//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls index cad43e858a..b2171fa4c0 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls @@ -19,7 +19,7 @@ s(123) :sjs ex |>. s(123) //│ JS (unsanitized): -//│ let tmp1; tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) +//│ let tmp1; /** scoped **/ tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) //│ = [123, 456] diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index b2041f799f..760c5f6b53 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,9 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) {{ let y; /** scoped **/ throw globalThis.Error("e") } }; f2(1) +//│ let f2; /** scoped **/ +//│ f2 = function f(x) {{ let y; /** scoped **/ throw globalThis.Error("e") } }; +//│ f2(1) //│ ═══[RUNTIME ERROR] Error: e @@ -41,7 +43,7 @@ fun f(x) = throw (if x then Error("x") else Error("y")) f(false) //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(x) { //│ if (x === true) { throw globalThis.Error("x") } else { throw globalThis.Error("y") } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index bb1550c0d6..09873ebd4a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -7,7 +7,7 @@ fun fib(a) = if a <= 1 then a else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): -//│ let fib; +//│ let fib; /** scoped **/ //│ fib = function fib(a) {{ //│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ scrut = a <= 1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls index c0ec266ced..72170fa838 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls @@ -30,7 +30,9 @@ print of foo() :sjs print of globalThis.console.log("Hello, world!") //│ JS (unsanitized): -//│ let tmp4; tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); Predef.print(tmp4) +//│ let tmp4; /** scoped **/ +//│ tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); +//│ Predef.print(tmp4) //│ > Hello, world! //│ > () @@ -54,7 +56,7 @@ print of Box(foo()).value :sjs fun foo() = {} //│ JS (unsanitized): -//│ let foo5; foo5 = function foo() { return runtime.Unit }; +//│ let foo5; /** scoped **/ foo5 = function foo() { return runtime.Unit }; print of Box(foo()).value //│ > () @@ -64,7 +66,7 @@ print of Box(foo()).value fun foo(x) = set x = 1 //│ JS (unsanitized): -//│ let foo6; foo6 = function foo(x) { x = 1; return runtime.Unit }; +//│ let foo6; /** scoped **/ foo6 = function foo(x) { x = 1; return runtime.Unit }; print of Box(foo(1)).value //│ > () @@ -73,7 +75,7 @@ print of Box(foo(1)).value :e fun f = let x = 1 //│ ╔══[ERROR] Expected a body for let bindings in expression position -//│ ║ l.74: fun f = let x = 1 +//│ ║ l.76: fun f = let x = 1 //│ ╙── ^^^^^ print of f diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 884c8818ee..19c7ede879 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -59,12 +59,12 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let tmp6, while3, tmp7; +//│ let tmp6, while3, tmp7, tmp8; /** scoped **/ //│ tmp6 = undefined; //│ while3 = (undefined, function () {{ -//│ let tmp8; /** scoped **/ +//│ let tmp9; /** scoped **/ //│ if (x2 === true) { -//│ tmp8 = Predef.print("Hello World"); +//│ tmp9 = Predef.print("Hello World"); //│ x2 = false; //│ tmp6 = runtime.Unit; //│ return while3() @@ -213,7 +213,7 @@ fun f(ls) = print(h) else print("Done!") //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f(ls) {{ //│ let tmp27, while10, tmp28, tmp29; /** scoped **/ //│ tmp27 = undefined; @@ -274,7 +274,7 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, while11; +//│ let tmp37, tmp38, tmp39, while11; /** scoped **/ //│ tmp37 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 885a6dd1ed..9452424ea9 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -101,7 +101,7 @@ class T class Foo(using T) with print(T) //│ JS (unsanitized): -//│ let Foo9; +//│ let Foo9; /** scoped **/ //│ Foo9 = function Foo(tmp5) { //│ return globalThis.Object.freeze(new Foo.class(tmp5)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index 131b3e3f4c..be5a583ee1 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -31,14 +31,15 @@ using Num = 0.42 :sjs let f1 = foo //│ JS (unsanitized): -//│ let f1, f11; -//│ f11 = function f1() {{ +//│ let f1; +//│ let f11; /** scoped **/ +//│ f1 = function f1() {{ //│ let tmp; /** scoped **/ //│ tmp = foo(); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ } //│ }; -//│ f1 = f11; +//│ f11 = f1; //│ f1 = fun f1 :expect [42] @@ -53,14 +54,15 @@ f1() :sjs let f2 = bar //│ JS (unsanitized): -//│ let f2, f21; -//│ f21 = function f2(x) {{ +//│ let f2; +//│ let f21; /** scoped **/ +//│ f2 = function f2(x) {{ //│ let tmp; /** scoped **/ //│ tmp = bar(x); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ } //│ }; -//│ f2 = f21; +//│ f21 = f2; //│ f2 = fun f2 :expect [0.42, 42] @@ -75,8 +77,9 @@ f2(0.42) :sjs let f3 = baz //│ JS (unsanitized): -//│ let f3, f31; -//│ f31 = function f3(x) { +//│ let f3; +//│ let f31; /** scoped **/ +//│ f3 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) {{ //│ let tmp, tmp1, tmp2; /** scoped **/ @@ -88,7 +91,7 @@ let f3 = baz //│ }); //│ return lambda1 //│ }; -//│ f3 = f31; +//│ f31 = f3; //│ f3 = fun f3 :expect [43, 42, 44, 0.42] @@ -99,14 +102,14 @@ f3(43)(44) :sjs foo() //│ JS (unsanitized): -//│ let tmp1; tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) +//│ let tmp1; /** scoped **/ tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) //│ = [42] // This should not expand :sjs bar(0.42) //│ JS (unsanitized): -//│ let tmp2; tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) +//│ let tmp2; /** scoped **/ tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) //│ = [0.42, 42] @@ -115,7 +118,7 @@ module Test with fun test(j)(using Int) = 0 fun main(using Int) = test //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index 482d1d6532..a58b71139f 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; tmp2 = test(); tmp2(1, 1) +//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index a51de360c7..d7b6079082 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -17,7 +17,8 @@ fun f() = print of raiseUnhandledEffect() j / i //│ JS (unsanitized): -//│ let f, getLocals2; +//│ let getLocals2; +//│ let f; /** scoped **/ //│ getLocals2 = function getLocals() { //│ let prev, thisInfo, arr, tmp; //│ prev = []; @@ -128,8 +129,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:128:3) -//│ at lambda_test (Debugging.mls:125:3) +//│ at lambda (Debugging.mls:129:3) +//│ at lambda_test (Debugging.mls:126:3) import "../../mlscript-compile/Runtime.mls" @@ -208,9 +209,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:201:3) with locals: j=200 +//│ > at f (Debugging.mls:202:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:203:3) +//│ > at f (Debugging.mls:204:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:204:3) with locals: j=300 +//│ > at f (Debugging.mls:205:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index f70c9ad163..5c1f2971d8 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,7 +156,8 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let tmp20, handleBlock$11; +//│ let handleBlock$11; +//│ let tmp20; /** scoped **/ //│ handleBlock$11 = function handleBlock$() { //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12;{ //│ let f, scrut; /** scoped **/ diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index 32c8b1fed1..2f00f0a268 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -13,7 +13,7 @@ fun foo(h): module M = module A A //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(h) {{ //│ let A2, A3, scrut; /** scoped **/ //│ globalThis.Object.freeze(class A { diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index fbef803763..72893cfea0 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,7 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1; +//│ let Lol1, tmp; /** scoped **/ //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; @@ -19,7 +19,7 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { -//│ let tmp, res, Cont$ctor$Lol$1, doUnwind; +//│ let res, Cont$ctor$Lol$1, doUnwind; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 7f1b7dcb5d..cc4a69f256 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,42 +155,43 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let str, scrut, tmp24, tmp25, handleBlock$7; +//│ let handleBlock$7; +//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; /** scoped **/ //│ str = ""; //│ scrut = true; //│ if (scrut === true) { //│ handleBlock$7 = function handleBlock$() { -//│ let h1, tmp26, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; +//│ let h1, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; //│ globalThis.Object.freeze(class Handler$h1$1 extends Effect { //│ static { //│ Handler$h1$2 = this //│ } //│ constructor() { -//│ let tmp27; -//│ tmp27 = super(); +//│ let tmp28; +//│ tmp28 = super(); //│ } //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { //│ let Cont$handler$h1$perform$2, doUnwind1;{ -//│ let tmp27, tmp28, tmp29; /** scoped **/ +//│ let tmp28, tmp29, tmp30; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h1$perform$2 = this //│ } //│ constructor(pc) { -//│ let tmp30; -//│ tmp30 = super(null); +//│ let tmp31; +//│ tmp31 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 7) { -//│ tmp28 = value$; +//│ tmp29 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 7) { -//│ tmp29 = str + "A"; -//│ str = tmp29; +//│ tmp30 = str + "A"; +//│ str = tmp30; //│ return runtime.Unit //│ } //│ break; @@ -204,14 +205,14 @@ str //│ res7.contTrace.last = res7.contTrace.last.next; //│ return res7 //│ }; -//│ tmp27 = str + "A"; -//│ str = tmp27; -//│ tmp28 = runtime.safeCall(k(arg)); -//│ if (tmp28 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp28, 7) +//│ tmp28 = str + "A"; +//│ str = tmp28; +//│ tmp29 = runtime.safeCall(k(arg)); +//│ if (tmp29 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp29, 7) //│ } -//│ tmp29 = str + "A"; -//│ str = tmp29; +//│ tmp30 = str + "A"; +//│ str = tmp30; //│ return runtime.Unit //│ } //│ }; @@ -226,17 +227,17 @@ str //│ Cont$handleBlock$h1$2 = this //│ } //│ constructor(pc) { -//│ let tmp27; -//│ tmp27 = super(null); +//│ let tmp28; +//│ tmp28 = super(null); //│ this.pc = pc; //│ } //│ resume(value$) { //│ if (this.pc === 6) { -//│ tmp26 = value$; +//│ tmp25 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 6) { -//│ return tmp26 +//│ return tmp25 //│ } //│ break; //│ } @@ -249,7 +250,7 @@ str //│ return runtime.handleBlockImpl(res7, h1) //│ }; //│ handleBlock$8 = function handleBlock$() { -//│ let h2, tmp27, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; +//│ let h2, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; //│ globalThis.Object.freeze(class Handler$h2$1 extends Effect { //│ static { //│ Handler$h2$2 = this @@ -324,7 +325,7 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 1) { -//│ tmp27 = value$; +//│ tmp26 = value$; //│ } else if (this.pc === 2) { //│ res7 = value$; //│ } @@ -349,9 +350,9 @@ str //│ res8.contTrace.last.next = new Cont$handleBlock$h2$2(pc); //│ return runtime.handleBlockImpl(res8, h2) //│ }; -//│ tmp27 = runtime.safeCall(h2.perform(runtime.Unit)); -//│ if (tmp27 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp27, 1) +//│ tmp26 = runtime.safeCall(h2.perform(runtime.Unit)); +//│ if (tmp26 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp26, 1) //│ } //│ res7 = runtime.safeCall(h1.perform(runtime.Unit)); //│ if (res7 instanceof runtime.EffectSig.class) { @@ -359,18 +360,18 @@ str //│ } //│ return res7 //│ }; -//│ tmp26 = handleBlock$8(); -//│ if (tmp26 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp26, 6) +//│ tmp25 = handleBlock$8(); +//│ if (tmp25 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp25, 6) //│ } -//│ return tmp26 +//│ return tmp25 //│ }; //│ tmp24 = handleBlock$7(); //│ if (tmp24 instanceof runtime.EffectSig.class) { //│ tmp24 = runtime.topLevelEffect(tmp24, false); //│ } -//│ tmp25 = tmp24; -//│ } else { tmp25 = runtime.Unit; } +//│ tmp27 = tmp24; +//│ } else { tmp27 = runtime.Unit; } //│ str //│ = "BABABA" //│ str = "BABABA" diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 722499592a..6dd7795543 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,7 +8,7 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let x, old, tmp, tmp1, tmp2; +//│ let x, old, tmp, tmp1, tmp2; /** scoped **/ //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 24ec00ee32..b506610f6d 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -20,7 +20,8 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi, res, $_stack$_safe$_body$_; +//│ let res, $_stack$_safe$_body$_; +//│ let hi; /** scoped **/ //│ hi = function hi(n) { //│ let Cont$func$hi$1, doUnwind, stackDelayRes;{ //│ let scrut, tmp; /** scoped **/ @@ -76,7 +77,8 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1, res1, $_stack$_safe$_body$_1; +//│ let res1, $_stack$_safe$_body$_1; +//│ let sum1; /** scoped **/ //│ sum1 = function sum(n) { //│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes;{ //│ let scrut, tmp, tmp1; /** scoped **/ @@ -260,7 +262,7 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; +//│ let max; /** scoped **/ //│ max = function max(a, b) {{ //│ let scrut; /** scoped **/ //│ scrut = a < b; @@ -275,7 +277,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls index 76a8e46f8f..a1596055ef 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls @@ -13,7 +13,7 @@ arr.map(_ === false) :sjs arr.map((e, ...) => e === false) //│ JS (unsanitized): -//│ let lambda1; +//│ let lambda1; /** scoped **/ //│ lambda1 = (undefined, function (e, ..._) { return e === false }); //│ runtime.safeCall(arr.map(lambda1)) //│ = [false, true] @@ -21,7 +21,7 @@ arr.map((e, ...) => e === false) :sjs arr.map((_, ...) => 1) //│ JS (unsanitized): -//│ let lambda2; +//│ let lambda2; /** scoped **/ //│ lambda2 = (undefined, function (_, ..._1) { return 1 }); //│ runtime.safeCall(arr.map(lambda2)) //│ = [1, 1] diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index adf92d3828..39bd2e8e55 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,7 +88,8 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let Bad1, Good1, f5, tmp9, Bad$, Good$, f$capture3; +//│ let Bad1, Good1, Bad$, Good$, f$capture3; +//│ let f5, tmp9; /** scoped **/ //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; //│ if (isMut === true) { diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index cf0ee14bd2..99fb3ba51d 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -11,7 +11,8 @@ fun f(x) = fun g = new A //│ ═══[WARNING] Modules are not yet lifted. //│ JS (unsanitized): -//│ let f, g$; +//│ let g$; +//│ let f; /** scoped **/ //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 8b6880737c..ac1a943970 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -7,7 +7,8 @@ data class A(x) with fun getB() = x + y fun getA() = B(2).getB() //│ JS (unsanitized): -//│ let B1, A1, B$; +//│ let B1, B$; +//│ let A1; /** scoped **/ //│ B$ = function B$(isMut, A$instance, y) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -74,7 +75,8 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let g, A3, tmp1, g$; +//│ let g, g$; +//│ let A3, tmp1; /** scoped **/ //│ g$ = function g$(A$instance) { //│ return 2 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index c5e0081c81..75303e1a13 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -47,7 +47,8 @@ fun f(used1, unused1) = foo(used2) + unused2 f(1, 2) //│ JS (unsanitized): -//│ let g1, f3, g$3; +//│ let g1, g$3; +//│ let f3; /** scoped **/ //│ g$3 = function g$(used1, used2, g_arg) {{ //│ let used3; /** scoped **/ //│ used3 = 2; @@ -80,7 +81,8 @@ fun f(a1, a2, a3, a4, a5, a6) = g f(1,2,3,4,5,6) //│ JS (unsanitized): -//│ let f4, g$4; +//│ let g$4; +//│ let f4; /** scoped **/ //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) {{ //│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ tmp = a1 + a2; @@ -167,7 +169,8 @@ fun f(unused, immutable, mutated) = a + h() + unused f(1, 2, 1000) //│ JS (unsanitized): -//│ let f7, h$2, g$6, f$capture5; +//│ let h$2, g$6, f$capture5; +//│ let f7; /** scoped **/ //│ g$6 = function g$(immutable, f$capture6) { //│ f$capture6.mutated$capture$0 = 2; //│ return immutable + f$capture6.mutated$capture$0 @@ -258,7 +261,8 @@ fun g() = f g()(1) //│ JS (unsanitized): -//│ let f14, g6, tmp7, f$1, h$4, g$capture1; +//│ let f14, f$1, h$4, g$capture1; +//│ let g6, tmp7; /** scoped **/ //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; //│ x1 = 4; @@ -380,7 +384,8 @@ fun f(x) = set y = 2 [g, g] //│ JS (unsanitized): -//│ let g12, f23, g$14; +//│ let g12, g$14; +//│ let f23; /** scoped **/ //│ g$14 = function g$(y1) { //│ return y1 //│ }; @@ -416,7 +421,8 @@ fun f(x) = set x += 1 [a, g] //│ JS (unsanitized): -//│ let g13, f24, g$15, f$capture17; +//│ let g13, g$15, f$capture17; +//│ let f24; /** scoped **/ //│ g$15 = function g$(f$capture18) { //│ return f$capture18.x$capture$0 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls index d5bc28e321..581a224319 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls @@ -7,7 +7,7 @@ import "../../mlscript-compile/Option.mls" module A with fun f(x) = x is Option.Some //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 74979d2b2d..9d90f95a3d 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -45,7 +45,8 @@ fun foo() = set x += 1 return () => x //│ JS (unsanitized): -//│ let foo2, lambda2, while$1, lambda$2, foo$capture5; +//│ let lambda2, while$1, lambda$2, foo$capture5; +//│ let foo2; /** scoped **/ //│ lambda$2 = function lambda$(foo$capture6) { //│ return foo$capture6.x$capture$0 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index 50f1da26aa..7d1e56a774 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -27,7 +27,8 @@ fun foo(y) = (new M).foo() foo(10) //│ JS (unsanitized): -//│ let M3, foo1, M$; +//│ let M3, M$; +//│ let foo1; /** scoped **/ //│ M$ = function M$(isMut, y) { //│ let tmp; //│ if (isMut === true) { @@ -54,7 +55,7 @@ foo(10) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ foo1 = function foo(y) {{ let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() } }; +//│ foo1 = function foo(y) { let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() }; //│ foo1(10) @@ -109,7 +110,8 @@ fun foo(x, y) = fun foo3 = M.foo2() foo3 //│ JS (unsanitized): -//│ let M5, foo4, foo3$, foo$capture3; +//│ let M5, foo3$, foo$capture3; +//│ let foo4; /** scoped **/ //│ foo3$ = function foo3$(M6, x, foo$capture4) { //│ return M6.foo2() //│ }; @@ -213,7 +215,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp5; +//│ let M17, tmp5, tmp6; /** scoped **/ //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -221,8 +223,7 @@ M.A().get //│ constructor() { //│ runtime.Unit; //│ } -//│ static { -//│ let tmp6;{ +//│ static {{ //│ let scrut; /** scoped **/ //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); @@ -240,18 +241,18 @@ M.A().get //│ }); //│ scrut = M16.A(); //│ if (scrut instanceof M16.A.class) { -//│ tmp6 = 2; +//│ tmp5 = 2; //│ } else { -//│ tmp6 = 3; +//│ tmp5 = 3; //│ } -//│ this.x = tmp6; +//│ this.x = tmp5; //│ } //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp5 = M17.A(); -//│ tmp5.get +//│ tmp6 = M17.A(); +//│ tmp6.get //│ = 4 module M with diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 8179e3081c..a3a89f256b 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -12,7 +12,8 @@ fun foo() = xs.push(bar) set x = 2 //│ JS (unsanitized): -//│ let bar, foo, bar$, foo$capture1; +//│ let bar, bar$, foo$capture1; +//│ let foo; /** scoped **/ //│ bar$ = function bar$(foo$capture2) { //│ return foo$capture2.x$capture$0 //│ }; @@ -86,13 +87,14 @@ fun foo() = x bar //│ JS (unsanitized): -//│ let bar3, foo3; +//│ let bar3; +//│ let foo3; /** scoped **/ //│ bar3 = function bar() {{ //│ let x; /** scoped **/ //│ x = undefined; //│ return x //│ } //│ }; -//│ foo3 = function foo() {{ return bar3 } }; +//│ foo3 = function foo() { return bar3 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 24e145213d..540b7068ff 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -21,7 +21,8 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi, Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; +//│ let Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; +//│ let hi; /** scoped **/ //│ Cont$func$hi$$ = function Cont$func$hi$$(isMut, n, pc) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -94,7 +95,8 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1, Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; +//│ let Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; +//│ let sum1; /** scoped **/ //│ Cont$func$sum$$ = function Cont$func$sum$$(isMut, n, tmp, pc) { //│ let tmp1, tmp2; //│ if (isMut === true) { @@ -276,7 +278,7 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; +//│ let max; /** scoped **/ //│ max = function max(a, b) {{ //│ let scrut; /** scoped **/ //│ scrut = a < b; @@ -291,7 +293,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index e6dbdc99c7..a18bd747f6 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -14,7 +14,7 @@ class A(x) with set z += 2 x //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ A1 = function A(x) { //│ return globalThis.Object.freeze(new A.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls index 41df93f70a..226fd3ed63 100644 --- a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls +++ b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls @@ -20,7 +20,7 @@ //│ ║ l.16: 1 //│ ╙── ^ //│ JS (unsanitized): -//│ let tmp2; tmp2 = + 2; + 3 +//│ let tmp2; /** scoped **/ tmp2 = + 2; + 3 //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.16: 1 //│ ╙── ^ @@ -31,7 +31,7 @@ + 2 + 3 //│ JS (unsanitized): -//│ let tmp3; tmp3 = 1 + 2; tmp3 + 3 +//│ let tmp3; /** scoped **/ tmp3 = 1 + 2; tmp3 + 3 //│ = 6 1 diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index b726512101..63bbdc68d8 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,17 +99,19 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let ls, a, middleElements, element0$; -//│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { -//│ element0$ = runtime.Tuple.get(xs1, 0); -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); -//│ ls = middleElements; -//│ a = element0$; -//│ globalThis.Object.freeze([ -//│ a, -//│ ls -//│ ]) -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ { +//│ let ls, a, middleElements, element0$; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { +//│ element0$ = runtime.Tuple.get(xs1, 0); +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); +//│ ls = middleElements; +//│ a = element0$; +//│ globalThis.Object.freeze([ +//│ a, +//│ ls +//│ ]) +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ } //│ = [1, [2, 3]] fun addUntilN(xs, n) = @@ -133,7 +135,7 @@ fun popByIndex(start, end, acc, lft) = if start >= end then acc else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): -//│ let popByIndex; +//│ let popByIndex; /** scoped **/ //│ popByIndex = function popByIndex(start, end, acc, lft) {{ //│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ //│ scrut = start >= end; diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 25ac7189a4..783e6d19df 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; /** scoped **/ tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index d38baf3139..69e093afa5 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -58,7 +58,7 @@ fun crazy(v) = S(S(S(S(S(S(0)))))) then "bruh!" _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): -//│ let crazy; +//│ let crazy; /** scoped **/ //│ crazy = function crazy(v) {{ //│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ split_root$: { diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index aef9d23d6a..de445d712f 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,7 +25,7 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, scrut5, tmp5; +//│ let scrut4, scrut5, tmp5; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ scrut4 = true; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index d8df0da7d6..7d9a4fdf27 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,19 +10,22 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut; scrut = x === 0; if (scrut === true) { 1 } else { 2 } +//│ { let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } } //│ = 1 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; +//│ let a, scrut1, tmp; /** scoped **/ +//│ scrut1 = x === 0; +//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } +//│ a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; +//│ let scrut2, tmp1; /** scoped **/ //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -38,7 +41,7 @@ if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp2; +//│ let tmp2; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x === 0) { @@ -78,7 +81,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; +//│ let qqq, tmp3; /** scoped **/ //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -118,7 +121,7 @@ print of if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ if (x === 0) { @@ -159,7 +162,7 @@ fun foo(x, y, z) = 1 then "1" else "" //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(x1, y1, z1) {{ //│ let tmp5; /** scoped **/ //│ split_root$3: { @@ -206,7 +209,7 @@ print of if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ split_root$3: { //│ split_1$3: { //│ split_2$: { @@ -268,7 +271,7 @@ fun foo(x, y, z) = if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ foo1 = function foo(x1, y1, z1) {{ //│ let tmp6; /** scoped **/ //│ split_root$4: { @@ -336,7 +339,7 @@ fun foo(x, y, z) = if x is let value = "hello" expensive_call(value) //│ JS (unsanitized): -//│ let foo2; +//│ let foo2; /** scoped **/ //│ foo2 = function foo(x1, y1, z1) {{ //│ let value, tmp6; /** scoped **/ //│ split_root$4: { @@ -382,7 +385,7 @@ fun foo(x, y, z) = if x is A then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo3; +//│ let foo3; /** scoped **/ //│ foo3 = function foo(x1, y1, z1) { //│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } //│ }; @@ -403,7 +406,7 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo4; +//│ let foo4; /** scoped **/ //│ foo4 = function foo(x1, y1, z1) {{ //│ let tmp6; /** scoped **/ //│ split_root$4: { @@ -432,7 +435,7 @@ fun foo(x, y, z) = fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): -//│ let foo5; +//│ let foo5; /** scoped **/ //│ foo5 = function foo(x1, y1, z1) {{ //│ let tmp6; /** scoped **/ //│ split_root$4: { @@ -473,7 +476,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; +//│ let y1, scrut3, tmp6; /** scoped **/ //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls index 18fb0d1700..db15ad49ed 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls @@ -17,7 +17,7 @@ if x then 0 y then 0 //│ JS (unsanitized): -//│ let tmp; +//│ let tmp; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index 96f735e094..2907b42354 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -46,7 +46,7 @@ fun f(x) = if x is Pair(A, A) then 1 Pair(B, B) then 2 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f(x) {{ //│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index ddc5a8376c..54381dba85 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -36,7 +36,7 @@ fun foo(v) = A & B then 1 else 0 //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(v) {{ //│ let tmp2; /** scoped **/ //│ split_root$2: { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 09dd6ce1b7..bc193b99b3 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -6,7 +6,7 @@ fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ JS (unsanitized): -//│ let nonsense; +//│ let nonsense; /** scoped **/ //│ nonsense = function nonsense(xs) {{ //│ let ys, middleElements; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { @@ -28,7 +28,7 @@ fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ JS (unsanitized): -//│ let lead_and_last; +//│ let lead_and_last; /** scoped **/ //│ lead_and_last = function lead_and_last(xs) {{ //│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { @@ -63,7 +63,7 @@ fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 //│ JS (unsanitized): -//│ let nested_tuple_patterns; +//│ let nested_tuple_patterns; /** scoped **/ //│ nested_tuple_patterns = function nested_tuple_patterns(xs) {{ //│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ //│ split_root$: { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index 4e3ef3a9f6..34bdc937e6 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -33,7 +33,7 @@ Cross.unapply("0") :sjs fun foo(x) = x is Cross //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(x2) {{ //│ let unapplyResult, output, bindings; /** scoped **/ //│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index 5ca7d2fa86..f162b5ee1a 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -11,7 +11,7 @@ data class Pair[A, B](first: A, second: B) :sjs pattern SumPair = Pair(a, b) => a + b //│ JS (unsanitized): -//│ let SumPair1; +//│ let SumPair1; /** scoped **/ //│ globalThis.Object.freeze(class SumPair { //│ static { //│ SumPair1 = globalThis.Object.freeze(new this) From 36f95ce1f3bd5c86d27daa016f1fcde95a9b10e7 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Sat, 29 Nov 2025 14:05:41 +0800 Subject: [PATCH 36/72] WIP: Remove redundant braces (not clean up yet) --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 30 +- .../src/test/mlscript-compile/Predef.mjs | 138 +- .../src/test/mlscript-compile/Runtime.mjs | 1254 ++++++++--------- .../OverloadedModulesInSignatures.mls | 11 +- .../src/test/mlscript/backlog/ToTriage.mls | 2 +- .../src/test/mlscript/basics/LazySpreads.mls | 60 +- .../mlscript/basics/MultiParamListClasses.mls | 9 +- .../test/mlscript/basics/MultiParamLists.mls | 34 +- .../mlscript/basics/MultilineExpressions.mls | 28 +- .../src/test/mlscript/basics/PartialApps.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 13 +- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 46 +- .../src/test/mlscript/bbml/bbGetters.mls | 51 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 39 +- .../test/mlscript/codegen/CaseShorthand.mls | 13 +- .../test/mlscript/codegen/ClassInClass.mls | 9 +- .../src/test/mlscript/codegen/ClassInFun.mls | 66 +- .../test/mlscript/codegen/ClassMatching.mls | 146 +- .../src/test/mlscript/codegen/Comma.mls | 18 +- .../src/test/mlscript/codegen/EarlyReturn.mls | 17 +- .../src/test/mlscript/codegen/FunInClass.mls | 191 ++- .../src/test/mlscript/codegen/Getters.mls | 155 +- .../src/test/mlscript/codegen/GlobalThis.mls | 25 +- .../src/test/mlscript/codegen/Hygiene.mls | 15 +- .../src/test/mlscript/codegen/IfThenElse.mls | 30 +- .../src/test/mlscript/codegen/ImportedOps.mls | 9 +- .../test/mlscript/codegen/InlineLambdas.mls | 32 +- .../src/test/mlscript/codegen/Lambdas.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 121 +- .../src/test/mlscript/codegen/OptMatch.mls | 19 +- .../src/test/mlscript/codegen/PartialApps.mls | 9 +- .../test/mlscript/codegen/PlainClasses.mls | 62 +- .../src/test/mlscript/codegen/RandomStuff.mls | 24 +- .../test/mlscript/codegen/SanityChecks.mls | 46 +- .../src/test/mlscript/codegen/Scoped.mls | 290 ++-- .../src/test/mlscript/codegen/SetIn.mls | 86 +- .../src/test/mlscript/codegen/Throw.mls | 2 +- .../src/test/mlscript/codegen/TraceLog.mls | 13 +- .../src/test/mlscript/codegen/While.mls | 159 +-- .../src/test/mlscript/ctx/EtaExpansion.mls | 40 +- .../src/test/mlscript/handlers/Debugging.mls | 169 ++- .../src/test/mlscript/handlers/Effects.mls | 111 +- .../test/mlscript/handlers/EffectsHygiene.mls | 49 +- .../mlscript/handlers/RecursiveHandlers.mls | 148 +- .../test/mlscript/handlers/StackSafety.mls | 167 ++- .../src/test/mlscript/lifter/ClassInFun.mls | 34 +- .../test/mlscript/lifter/CompanionsInFun.mls | 29 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 9 +- .../src/test/mlscript/lifter/FunInFun.mls | 139 +- .../shared/src/test/mlscript/lifter/Loops.mls | 40 +- .../test/mlscript/lifter/ModulesObjects.mls | 56 +- .../src/test/mlscript/lifter/Mutation.mls | 26 +- .../test/mlscript/lifter/StackSafetyLift.mls | 73 +- .../src/test/mlscript/objbuf/Mutation.mls | 40 +- .../test/mlscript/std/FingerTreeListTest.mls | 51 +- .../test/mlscript/ucs/general/JoinPoints.mls | 88 +- .../ucs/normalization/Deduplication.mls | 217 ++- .../ucs/normalization/SimplePairMatches.mls | 76 +- .../ucs/patterns/ConjunctionPattern.mls | 17 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 93 +- .../src/test/mlscript/ups/MatchResult.mls | 17 +- .../src/test/mlscript/ups/SimpleTransform.mls | 29 +- 63 files changed, 2381 insertions(+), 2615 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index bea581ab7e..250c56c569 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -103,11 +103,6 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else s.toIntOption match case S(index) => doc"[$index]" case N => doc"[${JSBuilder.makeStringLiteral(s)}]" - - def tryBraced(original: Block, res: Document)(using Scope) = original match - case Scoped(syms, body, _) => // FIXME: remove the hack `!(body.definedVarsNoScoped -- syms).isEmpty` after handling the later passes correctly - if (syms.filter(l => scope.lookup(l).isDefined)).isEmpty || !(body.definedVarsNoScoped -- syms).isEmpty then braced(res) else res - case _ => braced(res) def result(r: Result)(using Raise, Scope): Document = r match case Value.This(sym) => scope.findThis_!(sym) @@ -153,7 +148,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else doc"$runtimeVar.safeCall(${base}(${argsDoc}))" case Lambda(ps, bod) => scope.nest givenIn: val (params, bodyDoc) = setupFunction(none, ps, bod) - doc"($params) => ${ tryBraced(bod, bodyDoc) }" + doc"($params) => ${ braced(bodyDoc) }" case s @ Select(qual, id) => val dotClass = s.symbol match case S(ds) if ds.shouldBeLifted => doc".class" @@ -242,10 +237,10 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: if sym.nameIsMeaningful then // If the name is not valid JavaScript identifiers, do not use it in the generated function. val nme = if isValidIdentifier(sym.nme) then sym.nme else "" - doc"${getVar(sym, sym.toLoc)} = function $nme($params) ${ tryBraced(result, bodyDoc) };" + doc"${getVar(sym, sym.toLoc)} = function $nme($params) ${ braced(bodyDoc) };" else // in JS, let name = (0, function (args) => {} ) prevents function's name from being bound to `name` - doc"${getVar(sym, sym.toLoc)} = (undefined, function ($params) ${ tryBraced(result, bodyDoc) });" + doc"${getVar(sym, sym.toLoc)} = (undefined, function ($params) ${ braced(bodyDoc) });" case ClsLikeDefn(ownr, isym, sym, kind, paramsOpt, auxParams, par, mtds, privFlds, pubFlds, preCtor, ctor, modo, bufferable) @@ -262,9 +257,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: Return(Lambda(ps, block), false) val (params, bodyDoc) = scope.nest.givenIn: setupFunction(S(td.sym.nme), ps, result) - doc" # $mtdPrefix${td.sym.nme}($params) ${ tryBraced(result, bodyDoc) }" + doc" # $mtdPrefix${td.sym.nme}($params) ${ braced(bodyDoc) }" case td @ FunDefn(params = Nil, body = bod) => - doc" # ${mtdPrefix}get ${td.sym.nme}() ${ tryBraced(bod, body(bod, endSemi = true)) }" + doc" # ${mtdPrefix}get ${td.sym.nme}() ${ braced(body(bod, endSemi = true)) }" .mkDocument(" ") def mkPrivs(pubFlds: Ls[BlockMemberSymbol -> TermSymbol], privFlds: Ls[TermSymbol], @@ -474,12 +469,12 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc"""typeof $sd === "object" && $sd !== null && "${n.name}" in $sd""" case Case.Field(name = n, safe = true) => doc""""${n.name}" in $sd""" - val h = doc" # if (${ cond(hd._1) }) ${ tryBraced(hd._2, returningTerm(hd._2, endSemi = false)) }" + val h = doc" # if (${ cond(hd._1) }) ${ braced(returningTerm(hd._2, endSemi = false)) }" val t = tl.foldLeft(h)((acc, arm) => - acc :: doc" else if (${ cond(arm._1) }) ${ tryBraced(arm._2, returningTerm(arm._2, endSemi = false)) }") + acc :: doc" else if (${ cond(arm._1) }) ${ braced(returningTerm(arm._2, endSemi = false)) }") val e = els match case S(el) => - doc" else ${ tryBraced(el, returningTerm(el, endSemi = false)) }" + doc" else ${ braced(returningTerm(el, endSemi = false)) }" case N => doc"" t :: e :: returningTerm(rest, endSemi) @@ -509,8 +504,8 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: } :: returningTerm(rst, endSemi) case TryBlock(sub, fin, rst) => - doc" # try ${ tryBraced(sub, returningTerm(sub, endSemi = false)) } finally ${ - tryBraced(fin, returningTerm(fin, endSemi = false)) + doc" # try ${ braced(returningTerm(sub, endSemi = false)) } finally ${ + braced(returningTerm(fin, endSemi = false)) } # ${ returningTerm(rst, endSemi).stripBreaks}" @@ -530,12 +525,11 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else Some(l -> scope.allocateName(l)) // NOTE: currently this does not generate pretty JS codes... - val res = (if vars.isEmpty then doc"" else + (if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") - :: doc"; /** scoped **/") :: returningTerm(body, endSemi) - if !topLevel then braced(res) else res + :: doc"; /** scoped **/") :: returningTerm(body, endSemi) // case _ => ??? diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index 8a2a1e88d9..e7f4e8a4fa 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -56,35 +56,31 @@ globalThis.Object.freeze(class Predef { static pipeFromHi(f, x) { return runtime.safeCall(f(x)) } - static tap(x, f) {{ - let tmp; /** scoped **/ - tmp = runtime.safeCall(f(x)); - return (tmp , x) - } + static tap(x, f) { + let tmp; /** scoped **/ + tmp = runtime.safeCall(f(x)); + return (tmp , x) } - static pat(f, x) {{ - let tmp; /** scoped **/ - tmp = runtime.safeCall(f(x)); - return (tmp , x) - } + static pat(f, x) { + let tmp; /** scoped **/ + tmp = runtime.safeCall(f(x)); + return (tmp , x) } static alsoDo(x, eff) { return x } static andThen(f, g) { - return (x) => {{ - let tmp; /** scoped **/ - tmp = runtime.safeCall(f(x)); - return runtime.safeCall(g(tmp)) - } + return (x) => { + let tmp; /** scoped **/ + tmp = runtime.safeCall(f(x)); + return runtime.safeCall(g(tmp)) } } static compose(f, g) { - return (x) => {{ - let tmp; /** scoped **/ - tmp = runtime.safeCall(g(x)); - return runtime.safeCall(f(tmp)) - } + return (x) => { + let tmp; /** scoped **/ + tmp = runtime.safeCall(g(x)); + return runtime.safeCall(f(tmp)) } } static passTo(receiver, f) { @@ -102,12 +98,11 @@ globalThis.Object.freeze(class Predef { return f.call(receiver, ...args) } } - static print(...xs) {{ - let tmp, tmp1; /** scoped **/ - tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); - tmp1 = runtime.safeCall(tmp(...xs)); - return runtime.safeCall(globalThis.console.log(...tmp1)) - } + static print(...xs) { + let tmp, tmp1; /** scoped **/ + tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); + tmp1 = runtime.safeCall(tmp(...xs)); + return runtime.safeCall(globalThis.console.log(...tmp1)) } static renderAsStr(arg) { if (typeof arg === 'string') { @@ -116,11 +111,10 @@ globalThis.Object.freeze(class Predef { return runtime.safeCall(Predef.render(arg)) } } - static notImplemented(msg) {{ - let tmp; /** scoped **/ - tmp = "Not implemented: " + msg; - throw globalThis.Error(tmp) - } + static notImplemented(msg) { + let tmp; /** scoped **/ + tmp = "Not implemented: " + msg; + throw globalThis.Error(tmp) } static get notImplementedError() { throw globalThis.Error("Not implemented"); @@ -129,54 +123,50 @@ globalThis.Object.freeze(class Predef { return xs } static foldr(f) { - return (first, ...rest) => {{ - let len, scrut, i, init, tmp; /** scoped **/ - len = rest.length; - scrut = len == 0; - if (scrut === true) { - return first - } else { - i = len - 1; - init = runtime.safeCall(rest.at(i)); - tmp1: while (true) {{ - let scrut1, tmp2, tmp3, tmp4; /** scoped **/ - scrut1 = i > 0; - if (scrut1 === true) { - tmp2 = i - 1; - i = tmp2; - tmp3 = runtime.safeCall(rest.at(i)); - tmp4 = runtime.safeCall(f(tmp3, init)); - init = tmp4; - tmp = runtime.Unit; - continue tmp1 - } else { - tmp = runtime.Unit; - } - } - break; + return (first, ...rest) => { + let len, scrut, i, init, tmp; /** scoped **/ + len = rest.length; + scrut = len == 0; + if (scrut === true) { + return first + } else { + i = len - 1; + init = runtime.safeCall(rest.at(i)); + tmp1: while (true) { + let scrut1, tmp2, tmp3, tmp4; /** scoped **/ + scrut1 = i > 0; + if (scrut1 === true) { + tmp2 = i - 1; + i = tmp2; + tmp3 = runtime.safeCall(rest.at(i)); + tmp4 = runtime.safeCall(f(tmp3, init)); + init = tmp4; + tmp = runtime.Unit; + continue tmp1 + } else { + tmp = runtime.Unit; } - return runtime.safeCall(f(first, init)) + break; } + return runtime.safeCall(f(first, init)) } } } - static mkStr(...xs) {{ - let lambda, tmp; /** scoped **/ - lambda = (undefined, function (acc, x) {{ - let tmp1, tmp2, tmp3; /** scoped **/ - if (typeof x === 'string') { - tmp1 = true; - } else { - tmp1 = false; - } - tmp2 = runtime.safeCall(Predef.assert(tmp1)); - tmp3 = acc + x; - return (tmp2 , tmp3) - } - }); - tmp = runtime.safeCall(Predef.fold(lambda)); - return runtime.safeCall(tmp(...xs)) - } + static mkStr(...xs) { + let lambda, tmp; /** scoped **/ + lambda = (undefined, function (acc, x) { + let tmp1, tmp2, tmp3; /** scoped **/ + if (typeof x === 'string') { + tmp1 = true; + } else { + tmp1 = false; + } + tmp2 = runtime.safeCall(Predef.assert(tmp1)); + tmp3 = acc + x; + return (tmp2 , tmp3) + }); + tmp = runtime.safeCall(Predef.fold(lambda)); + return runtime.safeCall(tmp(...xs)) } static use(instance) { return instance diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index e426a546ad..42a7977412 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -73,17 +73,15 @@ globalThis.Object.freeze(class Runtime { this.reified = this.#_reified; } #_reified; - resumeWith(value) {{ - let lambda; /** scoped **/ - const this$EffectHandle = this; - lambda = (undefined, function () {{ - let tmp; /** scoped **/ - tmp = Runtime.resume(this$EffectHandle.reified.contTrace); - return runtime.safeCall(tmp(value)) - } - }); - return Runtime1.try(lambda) - } + resumeWith(value) { + let lambda; /** scoped **/ + const this$EffectHandle = this; + lambda = (undefined, function () { + let tmp; /** scoped **/ + tmp = Runtime.resume(this$EffectHandle.reified.contTrace); + return runtime.safeCall(tmp(value)) + }); + return Runtime1.try(lambda) } raise() { return Runtime.topLevelEffect(this.reified, false) @@ -128,38 +126,35 @@ globalThis.Object.freeze(class Runtime { static { this.split = LazyArray.__split; } - static slice(xs, i, j) {{ - let tmp; /** scoped **/ - tmp = xs.length - j; - return xs.slice(i, tmp) - } + static slice(xs, i, j) { + let tmp; /** scoped **/ + tmp = xs.length - j; + return xs.slice(i, tmp) } - static lazySlice(xs, i, j) {{ - let tmp; /** scoped **/ - tmp = LazyArray.dropLeftRight(i, j); - return runtime.safeCall(tmp(xs)) - } + static lazySlice(xs, i, j) { + let tmp; /** scoped **/ + tmp = LazyArray.dropLeftRight(i, j); + return runtime.safeCall(tmp(xs)) } static lazyConcat(...args) { return runtime.safeCall(LazyArray.__concat(...args)) } - static get(xs, i) {{ - let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ - scrut = i >= xs.length; - if (scrut === true) { - throw globalThis.RangeError("Tuple.get: index out of bounds") - } else { - tmp = runtime.Unit; - } - tmp1 = - xs.length; - scrut1 = i < tmp1; - if (scrut1 === true) { - throw globalThis.RangeError("Tuple.get: negative index out of bounds") - } else { - tmp2 = runtime.Unit; - } - return xs.at(i) + static get(xs, i) { + let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ + scrut = i >= xs.length; + if (scrut === true) { + throw globalThis.RangeError("Tuple.get: index out of bounds") + } else { + tmp = runtime.Unit; + } + tmp1 = - xs.length; + scrut1 = i < tmp1; + if (scrut1 === true) { + throw globalThis.RangeError("Tuple.get: negative index out of bounds") + } else { + tmp2 = runtime.Unit; } + return xs.at(i) } static isArrayLike(xs) { return runtime.safeCall(Iter.isArrayLike(xs)) @@ -177,14 +172,13 @@ globalThis.Object.freeze(class Runtime { static startsWith(string, prefix) { return runtime.safeCall(string.startsWith(prefix)) } - static get(string, i) {{ - let scrut; /** scoped **/ - scrut = i >= string.length; - if (scrut === true) { - throw globalThis.RangeError("Str.get: index out of bounds") - } else { - return runtime.safeCall(string.at(i)) - } + static get(string, i) { + let scrut; /** scoped **/ + scrut = i >= string.length; + if (scrut === true) { + throw globalThis.RangeError("Str.get: index out of bounds") + } else { + return runtime.safeCall(string.at(i)) } } static take(string, n) { @@ -214,43 +208,40 @@ globalThis.Object.freeze(class Runtime { this.enabled = false; this.indentLvl = 0; } - static indent() {{ - let scrut, prev, tmp; /** scoped **/ - scrut = TraceLogger.enabled; - if (scrut === true) { - prev = TraceLogger.indentLvl; - tmp = prev + 1; - TraceLogger.indentLvl = tmp; - return prev - } else { - return runtime.Unit - } + static indent() { + let scrut, prev, tmp; /** scoped **/ + scrut = TraceLogger.enabled; + if (scrut === true) { + prev = TraceLogger.indentLvl; + tmp = prev + 1; + TraceLogger.indentLvl = tmp; + return prev + } else { + return runtime.Unit } } - static resetIndent(n) {{ - let scrut; /** scoped **/ - scrut = TraceLogger.enabled; - if (scrut === true) { - TraceLogger.indentLvl = n; - return runtime.Unit - } else { - return runtime.Unit - } + static resetIndent(n) { + let scrut; /** scoped **/ + scrut = TraceLogger.enabled; + if (scrut === true) { + TraceLogger.indentLvl = n; + return runtime.Unit + } else { + return runtime.Unit } } - static log(msg) {{ - let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ - scrut = TraceLogger.enabled; - if (scrut === true) { - tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); - tmp1 = runtime.safeCall(" ".repeat(TraceLogger.indentLvl)); - tmp2 = "\n" + tmp1; - tmp3 = msg.replaceAll("\n", tmp2); - tmp4 = tmp + tmp3; - return runtime.safeCall(globalThis.console.log(tmp4)) - } else { - return runtime.Unit - } + static log(msg) { + let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + scrut = TraceLogger.enabled; + if (scrut === true) { + tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); + tmp1 = runtime.safeCall(" ".repeat(TraceLogger.indentLvl)); + tmp2 = "\n" + tmp1; + tmp3 = msg.replaceAll("\n", tmp2); + tmp4 = tmp + tmp3; + return runtime.safeCall(globalThis.console.log(tmp4)) + } else { + return runtime.Unit } } toString() { return runtime.render(this); } @@ -395,14 +386,13 @@ globalThis.Object.freeze(class Runtime { value: StackDelayHandler }) } - delay() {{ - let lambda; /** scoped **/ - lambda = (undefined, function (k) { - Runtime.stackResume = k; - return runtime.Unit - }); - return Runtime.mkEffect(this, lambda) - } + delay() { + let lambda; /** scoped **/ + lambda = (undefined, function (k) { + Runtime.stackResume = k; + return runtime.Unit + }); + return Runtime.mkEffect(this, lambda) } toString() { return runtime.render(this); } static [definitionMetadata] = ["object", "StackDelayHandler"]; @@ -418,18 +408,16 @@ globalThis.Object.freeze(class Runtime { this.#v = v; } #v; - zext() {{ - let tmp, tmp1; /** scoped **/ - tmp = Runtime.shl(1, 31); - tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); - return Runtime.bitand(this.#v, tmp1) - } + zext() { + let tmp, tmp1; /** scoped **/ + tmp = Runtime.shl(1, 31); + tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); + return Runtime.bitand(this.#v, tmp1) } - sext() {{ - let tmp; /** scoped **/ - tmp = Runtime.shl(1, 31); - return Runtime.bitor(this.#v, tmp) - } + sext() { + let tmp; /** scoped **/ + tmp = Runtime.shl(1, 31); + return Runtime.bitor(this.#v, tmp) } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Int31", [null]]; @@ -438,50 +426,48 @@ globalThis.Object.freeze(class Runtime { static get unreachable() { throw globalThis.Error("unreachable"); } - static checkArgs(functionName, expected, isUB, got) {{ - let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ - tmp = got < expected; - lambda = (undefined, function () { - lambda1 = (undefined, function () { - return got > expected - }); - return runtime.short_and(isUB, lambda1) + static checkArgs(functionName, expected, isUB, got) { + let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ + tmp = got < expected; + lambda = (undefined, function () { + lambda1 = (undefined, function () { + return got > expected }); - scrut = runtime.short_or(tmp, lambda); - if (scrut === true) {{ - let scrut1, scrut2, tmp12; /** scoped **/ - scrut1 = functionName.length > 0; - if (scrut1 === true) { - tmp12 = " '" + functionName; - tmp1 = tmp12 + "'"; - } else { - tmp1 = ""; - } - name = tmp1; - tmp2 = "Function" + name; - tmp3 = tmp2 + " expected "; - if (isUB === true) { - tmp4 = ""; - } else { - tmp4 = "at least "; - } - tmp5 = tmp3 + tmp4; - tmp6 = tmp5 + expected; - tmp7 = tmp6 + " argument"; - scrut2 = expected === 1; - if (scrut2 === true) { - tmp8 = ""; - } else { - tmp8 = "s"; - } - tmp9 = tmp7 + tmp8; - tmp10 = tmp9 + " but got "; - tmp11 = tmp10 + got; - throw globalThis.Error(tmp11) - } + return runtime.short_and(isUB, lambda1) + }); + scrut = runtime.short_or(tmp, lambda); + if (scrut === true) { + let scrut1, scrut2, tmp12; /** scoped **/ + scrut1 = functionName.length > 0; + if (scrut1 === true) { + tmp12 = " '" + functionName; + tmp1 = tmp12 + "'"; } else { - return runtime.Unit + tmp1 = ""; } + name = tmp1; + tmp2 = "Function" + name; + tmp3 = tmp2 + " expected "; + if (isUB === true) { + tmp4 = ""; + } else { + tmp4 = "at least "; + } + tmp5 = tmp3 + tmp4; + tmp6 = tmp5 + expected; + tmp7 = tmp6 + " argument"; + scrut2 = expected === 1; + if (scrut2 === true) { + tmp8 = ""; + } else { + tmp8 = "s"; + } + tmp9 = tmp7 + tmp8; + tmp10 = tmp9 + " but got "; + tmp11 = tmp10 + got; + throw globalThis.Error(tmp11) + } else { + return runtime.Unit } } static safeCall(x) { @@ -498,617 +484,571 @@ globalThis.Object.freeze(class Runtime { return x } } - static deboundMethod(mtdName, clsName) {{ - let tmp, tmp1, tmp2, tmp3; /** scoped **/ - tmp = "[debinding error] Method '" + mtdName; - tmp1 = tmp + "' of class '"; - tmp2 = tmp1 + clsName; - tmp3 = tmp2 + "' was accessed without being called."; - throw globalThis.Error(tmp3) - } + static deboundMethod(mtdName, clsName) { + let tmp, tmp1, tmp2, tmp3; /** scoped **/ + tmp = "[debinding error] Method '" + mtdName; + tmp1 = tmp + "' of class '"; + tmp2 = tmp1 + clsName; + tmp3 = tmp2 + "' was accessed without being called."; + throw globalThis.Error(tmp3) } - static try(f) {{ - let res; /** scoped **/ - res = runtime.safeCall(f()); - if (res instanceof Runtime.EffectSig.class) { - return Runtime.EffectHandle(res) - } else { - return res - } + static try(f) { + let res; /** scoped **/ + res = runtime.safeCall(f()); + if (res instanceof Runtime.EffectSig.class) { + return Runtime.EffectHandle(res) + } else { + return res } } - static printRaw(x) {{ - let rcd, tmp; /** scoped **/ - rcd = globalThis.Object.freeze({ - indent: 2, - breakLength: 76 - }); - tmp = Runtime.render(x, rcd); - return runtime.safeCall(globalThis.console.log(tmp)) - } + static printRaw(x) { + let rcd, tmp; /** scoped **/ + rcd = globalThis.Object.freeze({ + indent: 2, + breakLength: 76 + }); + tmp = Runtime.render(x, rcd); + return runtime.safeCall(globalThis.console.log(tmp)) } static raisePrintStackEffect(showLocals) { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } - static topLevelEffect(tr, debug) {{ - let tmp, tmp1; /** scoped **/ - tmp2: while (true) {{ - let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ - scrut = tr.handler === Runtime.PrintStackEffect; - if (scrut === true) { - tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); - tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); - tmp5 = Runtime.resume(tr.contTrace); - tmp6 = runtime.safeCall(tmp5(runtime.Unit)); - tr = tmp6; - tmp = runtime.Unit; - continue tmp2 - } else { - tmp = runtime.Unit; - } - } - break; - } - if (tr instanceof Runtime.EffectSig.class) { - tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; - throw Runtime.showStackTrace(tmp1, tr, debug, false) + static topLevelEffect(tr, debug) { + let tmp, tmp1; /** scoped **/ + tmp2: while (true) { + let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + scrut = tr.handler === Runtime.PrintStackEffect; + if (scrut === true) { + tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); + tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); + tmp5 = Runtime.resume(tr.contTrace); + tmp6 = runtime.safeCall(tmp5(runtime.Unit)); + tr = tmp6; + tmp = runtime.Unit; + continue tmp2 } else { - return tr + tmp = runtime.Unit; } + break; + } + if (tr instanceof Runtime.EffectSig.class) { + tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; + throw Runtime.showStackTrace(tmp1, tr, debug, false) + } else { + return tr } } - static showStackTrace(header, tr, debug, showLocals) {{ - let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ - msg = header; - curHandler = tr.contTrace; - atTail = true; - if (debug === true) {{ - let tmp3; /** scoped **/ - tmp4: while (true) {{ - let scrut, cur, tmp5, tmp6; /** scoped **/ - scrut = curHandler !== null; - if (scrut === true) {{ - let scrut1, tmp7, tmp8; /** scoped **/ - cur = curHandler.next; - tmp9: while (true) {{ - let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ - scrut2 = cur !== null; - if (scrut2 === true) {{ - let scrut3, lambda, tmp19, tmp20; /** scoped **/ - locals = cur.getLocals; - tmp10 = locals.length - 1; - curLocals = runtime.safeCall(locals.at(tmp10)); - loc = cur.getLoc; - if (loc === null) { - tmp11 = "pc=" + cur.pc; - } else { - tmp11 = loc; - } - loc1 = tmp11; - split_root$: { - split_1$: { - if (showLocals === true) { - scrut3 = curLocals.locals.length > 0; - if (scrut3 === true) { - lambda = (undefined, function (l) {{ - let tmp21, tmp22; /** scoped **/ - tmp21 = l.localName + "="; - tmp22 = Rendering.render(l.value); - return tmp21 + tmp22 - } - }); - tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); - tmp20 = runtime.safeCall(tmp19.join(", ")); - tmp12 = " with locals: " + tmp20; - break split_root$ - } else { - break split_1$ - } - } else { - break split_1$ - } - } - tmp12 = ""; - } - localsMsg = tmp12; - tmp13 = "\n\tat " + curLocals.fnName; - tmp14 = tmp13 + " ("; - tmp15 = tmp14 + loc1; - tmp16 = tmp15 + ")"; - tmp17 = msg + tmp16; - msg = tmp17; - tmp18 = msg + localsMsg; - msg = tmp18; - cur = cur.next; - atTail = false; - tmp5 = runtime.Unit; - continue tmp9 - } - } else { - tmp5 = runtime.Unit; - } + static showStackTrace(header, tr, debug, showLocals) { + let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ + msg = header; + curHandler = tr.contTrace; + atTail = true; + if (debug === true) { + let tmp3; /** scoped **/ + tmp4: while (true) { + let scrut, cur, tmp5, tmp6; /** scoped **/ + scrut = curHandler !== null; + if (scrut === true) { + let scrut1, tmp7, tmp8; /** scoped **/ + cur = curHandler.next; + tmp9: while (true) { + let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + scrut2 = cur !== null; + if (scrut2 === true) { + let scrut3, lambda, tmp19, tmp20; /** scoped **/ + locals = cur.getLocals; + tmp10 = locals.length - 1; + curLocals = runtime.safeCall(locals.at(tmp10)); + loc = cur.getLoc; + if (loc === null) { + tmp11 = "pc=" + cur.pc; + } else { + tmp11 = loc; + } + loc1 = tmp11; + split_root$: { + split_1$: { + if (showLocals === true) { + scrut3 = curLocals.locals.length > 0; + if (scrut3 === true) { + lambda = (undefined, function (l) { + let tmp21, tmp22; /** scoped **/ + tmp21 = l.localName + "="; + tmp22 = Rendering.render(l.value); + return tmp21 + tmp22 + }); + tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); + tmp20 = runtime.safeCall(tmp19.join(", ")); + tmp12 = " with locals: " + tmp20; + break split_root$ + } else { + break split_1$ } - break; - } - curHandler = curHandler.nextHandler; - scrut1 = curHandler !== null; - if (scrut1 === true) { - tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; - tmp8 = msg + tmp7; - msg = tmp8; - atTail = false; - tmp6 = runtime.Unit; } else { - tmp6 = runtime.Unit; + break split_1$ } - tmp = tmp6; - continue tmp4 } - } else { - tmp = runtime.Unit; + tmp12 = ""; } + localsMsg = tmp12; + tmp13 = "\n\tat " + curLocals.fnName; + tmp14 = tmp13 + " ("; + tmp15 = tmp14 + loc1; + tmp16 = tmp15 + ")"; + tmp17 = msg + tmp16; + msg = tmp17; + tmp18 = msg + localsMsg; + msg = tmp18; + cur = cur.next; + atTail = false; + tmp5 = runtime.Unit; + continue tmp9 + } else { + tmp5 = runtime.Unit; } break; } - if (atTail === true) { - tmp3 = msg + "\n\tat tail position"; - msg = tmp3; - tmp1 = runtime.Unit; + curHandler = curHandler.nextHandler; + scrut1 = curHandler !== null; + if (scrut1 === true) { + tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; + tmp8 = msg + tmp7; + msg = tmp8; + atTail = false; + tmp6 = runtime.Unit; } else { - tmp1 = runtime.Unit; + tmp6 = runtime.Unit; } - tmp2 = tmp1; + tmp = tmp6; + continue tmp4 + } else { + tmp = runtime.Unit; } + break; + } + if (atTail === true) { + tmp3 = msg + "\n\tat tail position"; + msg = tmp3; + tmp1 = runtime.Unit; } else { - tmp2 = runtime.Unit; + tmp1 = runtime.Unit; } - return msg + tmp2 = tmp1; + } else { + tmp2 = runtime.Unit; } + return msg } - static showFunctionContChain(cont, hl, vis, reps) {{ - let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ - if (cont instanceof Runtime.FunctionContFrame.class) {{ - let scrut, tmp5, tmp6, tmp7; /** scoped **/ - tmp = cont.constructor.name + "(pc="; - result = tmp + cont.pc; - lambda = (undefined, function (m, marker) {{ - let scrut1, tmp8, tmp9; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { - tmp8 = ", " + marker; - tmp9 = result + tmp8; - result = tmp9; - return runtime.Unit - } else { - return runtime.Unit - } - } - }); - tmp1 = runtime.safeCall(hl.forEach(lambda)); - scrut = runtime.safeCall(vis.has(cont)); - if (scrut === true) {{ - let scrut1; /** scoped **/ - tmp5 = reps + 1; - reps = tmp5; - scrut1 = reps > 10; - if (scrut1 === true) { - throw globalThis.Error("10 repeated continuation frame (loop?)") - } else { - tmp6 = runtime.Unit; - } - tmp7 = result + ", REPEAT"; - result = tmp7; - tmp2 = runtime.Unit; - } - } else { - tmp2 = runtime.safeCall(vis.add(cont)); - } - tmp3 = result + ") -> "; - tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp3 + tmp4 + static showFunctionContChain(cont, hl, vis, reps) { + let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + if (cont instanceof Runtime.FunctionContFrame.class) { + let scrut, tmp5, tmp6, tmp7; /** scoped **/ + tmp = cont.constructor.name + "(pc="; + result = tmp + cont.pc; + lambda = (undefined, function (m, marker) { + let scrut1, tmp8, tmp9; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { + tmp8 = ", " + marker; + tmp9 = result + tmp8; + result = tmp9; + return runtime.Unit + } else { + return runtime.Unit } - } else {{ - let scrut; /** scoped **/ - scrut = cont === null; - if (scrut === true) { - return "(null)" - } else { - return "(NOT CONT)" - } + }); + tmp1 = runtime.safeCall(hl.forEach(lambda)); + scrut = runtime.safeCall(vis.has(cont)); + if (scrut === true) { + let scrut1; /** scoped **/ + tmp5 = reps + 1; + reps = tmp5; + scrut1 = reps > 10; + if (scrut1 === true) { + throw globalThis.Error("10 repeated continuation frame (loop?)") + } else { + tmp6 = runtime.Unit; } + tmp7 = result + ", REPEAT"; + result = tmp7; + tmp2 = runtime.Unit; + } else { + tmp2 = runtime.safeCall(vis.add(cont)); + } + tmp3 = result + ") -> "; + tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp3 + tmp4 + } else { + let scrut; /** scoped **/ + scrut = cont === null; + if (scrut === true) { + return "(null)" + } else { + return "(NOT CONT)" } } } - static showHandlerContChain(cont, hl, vis, reps) {{ - let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ - if (cont instanceof Runtime.HandlerContFrame.class) {{ - let scrut, tmp4, tmp5, tmp6; /** scoped **/ - result = cont.handler.constructor.name; - lambda = (undefined, function (m, marker) {{ - let scrut1, tmp7, tmp8; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { - tmp7 = ", " + marker; - tmp8 = result + tmp7; - result = tmp8; - return runtime.Unit - } else { - return runtime.Unit - } - } - }); - tmp = runtime.safeCall(hl.forEach(lambda)); - scrut = runtime.safeCall(vis.has(cont)); - if (scrut === true) {{ - let scrut1; /** scoped **/ - tmp4 = reps + 1; - reps = tmp4; - scrut1 = reps > 10; - if (scrut1 === true) { - throw globalThis.Error("10 repeated continuation frame (loop?)") - } else { - tmp5 = runtime.Unit; - } - tmp6 = result + ", REPEAT"; - result = tmp6; - tmp1 = runtime.Unit; - } - } else { - tmp1 = runtime.safeCall(vis.add(cont)); - } - tmp2 = result + " -> "; - tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp2 + tmp3 + static showHandlerContChain(cont, hl, vis, reps) { + let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ + if (cont instanceof Runtime.HandlerContFrame.class) { + let scrut, tmp4, tmp5, tmp6; /** scoped **/ + result = cont.handler.constructor.name; + lambda = (undefined, function (m, marker) { + let scrut1, tmp7, tmp8; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { + tmp7 = ", " + marker; + tmp8 = result + tmp7; + result = tmp8; + return runtime.Unit + } else { + return runtime.Unit } - } else {{ - let scrut; /** scoped **/ - scrut = cont === null; - if (scrut === true) { - return "(null)" - } else { - return "(NOT HANDLER CONT)" - } + }); + tmp = runtime.safeCall(hl.forEach(lambda)); + scrut = runtime.safeCall(vis.has(cont)); + if (scrut === true) { + let scrut1; /** scoped **/ + tmp4 = reps + 1; + reps = tmp4; + scrut1 = reps > 10; + if (scrut1 === true) { + throw globalThis.Error("10 repeated continuation frame (loop?)") + } else { + tmp5 = runtime.Unit; } + tmp6 = result + ", REPEAT"; + result = tmp6; + tmp1 = runtime.Unit; + } else { + tmp1 = runtime.safeCall(vis.add(cont)); + } + tmp2 = result + " -> "; + tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp2 + tmp3 + } else { + let scrut; /** scoped **/ + scrut = cont === null; + if (scrut === true) { + return "(null)" + } else { + return "(NOT HANDLER CONT)" } } } - static debugCont(cont) {{ - let tmp, tmp1, tmp2; /** scoped **/ - tmp = globalThis.Object.freeze(new globalThis.Map()); - tmp1 = globalThis.Object.freeze(new globalThis.Set()); - tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); - return runtime.safeCall(globalThis.console.log(tmp2)) - } + static debugCont(cont) { + let tmp, tmp1, tmp2; /** scoped **/ + tmp = globalThis.Object.freeze(new globalThis.Map()); + tmp1 = globalThis.Object.freeze(new globalThis.Set()); + tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); + return runtime.safeCall(globalThis.console.log(tmp2)) } - static debugHandler(cont) {{ - let tmp, tmp1, tmp2; /** scoped **/ - tmp = globalThis.Object.freeze(new globalThis.Map()); - tmp1 = globalThis.Object.freeze(new globalThis.Set()); - tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); - return runtime.safeCall(globalThis.console.log(tmp2)) - } + static debugHandler(cont) { + let tmp, tmp1, tmp2; /** scoped **/ + tmp = globalThis.Object.freeze(new globalThis.Map()); + tmp1 = globalThis.Object.freeze(new globalThis.Set()); + tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); + return runtime.safeCall(globalThis.console.log(tmp2)) } - static debugContTrace(contTrace) {{ - let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ - if (contTrace instanceof Runtime.ContTrace.class) {{ - let scrut, scrut1; /** scoped **/ - tmp = globalThis.console.log("resumed: ", contTrace.resumed); - scrut = contTrace.last === contTrace; - if (scrut === true) { - tmp1 = runtime.safeCall(globalThis.console.log("")); - } else { - tmp1 = runtime.Unit; - } - scrut1 = contTrace.lastHandler === contTrace; - if (scrut1 === true) { - tmp2 = runtime.safeCall(globalThis.console.log("")); - } else { - tmp2 = runtime.Unit; - } - vis = globalThis.Object.freeze(new globalThis.Set()); - hl = globalThis.Object.freeze(new globalThis.Map()); - tmp3 = globalThis.Object.freeze([ - contTrace.last - ]); - tmp4 = globalThis.Object.freeze(new globalThis.Set(tmp3)); - tmp5 = hl.set("last", tmp4); - tmp6 = globalThis.Object.freeze([ - contTrace.lastHandler - ]); - tmp7 = globalThis.Object.freeze(new globalThis.Set(tmp6)); - tmp8 = hl.set("last-handler", tmp7); - tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); - tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); - cur = contTrace.nextHandler; - tmp13: while (true) {{ - let scrut2, tmp14, tmp15; /** scoped **/ - scrut2 = cur !== null; - if (scrut2 === true) { - tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); - tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); - cur = cur.nextHandler; - tmp11 = runtime.Unit; - continue tmp13 - } else { - tmp11 = runtime.Unit; - } - } - break; - } - return runtime.safeCall(globalThis.console.log()) - } + static debugContTrace(contTrace) { + let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + if (contTrace instanceof Runtime.ContTrace.class) { + let scrut, scrut1; /** scoped **/ + tmp = globalThis.console.log("resumed: ", contTrace.resumed); + scrut = contTrace.last === contTrace; + if (scrut === true) { + tmp1 = runtime.safeCall(globalThis.console.log("")); } else { - tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); - return runtime.safeCall(globalThis.console.log(contTrace)) + tmp1 = runtime.Unit; } - } - } - static debugEff(eff) {{ - let tmp, tmp1, tmp2, tmp3; /** scoped **/ - if (eff instanceof Runtime.EffectSig.class) { - tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); - tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); - tmp2 = globalThis.console.log("handlerFun: ", eff.handlerFun); - return Runtime.debugContTrace(eff.contTrace) + scrut1 = contTrace.lastHandler === contTrace; + if (scrut1 === true) { + tmp2 = runtime.safeCall(globalThis.console.log("")); } else { - tmp3 = runtime.safeCall(globalThis.console.log("Not an effect:")); - return runtime.safeCall(globalThis.console.log(eff)) + tmp2 = runtime.Unit; } + vis = globalThis.Object.freeze(new globalThis.Set()); + hl = globalThis.Object.freeze(new globalThis.Map()); + tmp3 = globalThis.Object.freeze([ + contTrace.last + ]); + tmp4 = globalThis.Object.freeze(new globalThis.Set(tmp3)); + tmp5 = hl.set("last", tmp4); + tmp6 = globalThis.Object.freeze([ + contTrace.lastHandler + ]); + tmp7 = globalThis.Object.freeze(new globalThis.Set(tmp6)); + tmp8 = hl.set("last-handler", tmp7); + tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); + tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); + cur = contTrace.nextHandler; + tmp13: while (true) { + let scrut2, tmp14, tmp15; /** scoped **/ + scrut2 = cur !== null; + if (scrut2 === true) { + tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); + tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); + cur = cur.nextHandler; + tmp11 = runtime.Unit; + continue tmp13 + } else { + tmp11 = runtime.Unit; + } + break; + } + return runtime.safeCall(globalThis.console.log()) + } else { + tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); + return runtime.safeCall(globalThis.console.log(contTrace)) } } - static mkEffect(handler, handlerFun) {{ - let res, tmp; /** scoped **/ - tmp = new Runtime.ContTrace.class(null, null, null, null, false); - res = new Runtime.EffectSig.class(tmp, handler, handlerFun); - res.contTrace.last = res.contTrace; - res.contTrace.lastHandler = res.contTrace; - return res + static debugEff(eff) { + let tmp, tmp1, tmp2, tmp3; /** scoped **/ + if (eff instanceof Runtime.EffectSig.class) { + tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); + tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); + tmp2 = globalThis.console.log("handlerFun: ", eff.handlerFun); + return Runtime.debugContTrace(eff.contTrace) + } else { + tmp3 = runtime.safeCall(globalThis.console.log("Not an effect:")); + return runtime.safeCall(globalThis.console.log(eff)) } } - static handleBlockImpl(cur, handler) {{ - let handlerFrame; /** scoped **/ - handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); - cur.contTrace.lastHandler.nextHandler = handlerFrame; - cur.contTrace.lastHandler = handlerFrame; - cur.contTrace.last = handlerFrame; - return Runtime.handleEffects(cur) + static mkEffect(handler, handlerFun) { + let res, tmp; /** scoped **/ + tmp = new Runtime.ContTrace.class(null, null, null, null, false); + res = new Runtime.EffectSig.class(tmp, handler, handlerFun); + res.contTrace.last = res.contTrace; + res.contTrace.lastHandler = res.contTrace; + return res + } + static handleBlockImpl(cur, handler) { + let handlerFrame; /** scoped **/ + handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); + cur.contTrace.lastHandler.nextHandler = handlerFrame; + cur.contTrace.lastHandler = handlerFrame; + cur.contTrace.last = handlerFrame; + return Runtime.handleEffects(cur) + } + static enterHandleBlock(handler, body) { + let cur; /** scoped **/ + cur = runtime.safeCall(body()); + if (cur instanceof Runtime.EffectSig.class) { + return Runtime.handleBlockImpl(cur, handler) + } else { + return cur } } - static enterHandleBlock(handler, body) {{ - let cur; /** scoped **/ - cur = runtime.safeCall(body()); + static handleEffects(cur) { + let tmp; /** scoped **/ + tmp1: while (true) { + let nxt, tmp2; /** scoped **/ if (cur instanceof Runtime.EffectSig.class) { - return Runtime.handleBlockImpl(cur, handler) + let scrut; /** scoped **/ + nxt = Runtime.handleEffect(cur); + scrut = cur === nxt; + if (scrut === true) { + return cur + } else { + cur = nxt; + tmp2 = runtime.Unit; + } + tmp = tmp2; + continue tmp1 } else { return cur } + break; } + return tmp } - static handleEffects(cur) {{ - let tmp; /** scoped **/ - tmp1: while (true) {{ - let nxt, tmp2; /** scoped **/ - if (cur instanceof Runtime.EffectSig.class) {{ - let scrut; /** scoped **/ - nxt = Runtime.handleEffect(cur); - scrut = cur === nxt; - if (scrut === true) { - return cur - } else { - cur = nxt; - tmp2 = runtime.Unit; - } - tmp = tmp2; - continue tmp1 + static handleEffect(cur) { + let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + prevHandlerFrame = cur.contTrace; + tmp6: while (true) { + let scrut1, scrut2; /** scoped **/ + split_root$: { + split_1$: { + scrut1 = prevHandlerFrame.nextHandler !== null; + if (scrut1 === true) { + scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; + if (scrut2 === true) { + prevHandlerFrame = prevHandlerFrame.nextHandler; + tmp = runtime.Unit; + continue tmp6 + } else { + break split_1$ } } else { - return cur + break split_1$ } } - break; + tmp = runtime.Unit; } - return tmp + break; } - } - static handleEffect(cur) {{ - let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ - prevHandlerFrame = cur.contTrace; - tmp6: while (true) {{ - let scrut1, scrut2; /** scoped **/ - split_root$: { - split_1$: { - scrut1 = prevHandlerFrame.nextHandler !== null; - if (scrut1 === true) { - scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; - if (scrut2 === true) { - prevHandlerFrame = prevHandlerFrame.nextHandler; - tmp = runtime.Unit; - continue tmp6 - } else { - break split_1$ - } - } else { - break split_1$ - } - } - tmp = runtime.Unit; - } - } - break; + scrut = prevHandlerFrame.nextHandler === null; + if (scrut === true) { + return cur + } else { + tmp1 = runtime.Unit; + } + handlerFrame = prevHandlerFrame.nextHandler; + saved = new Runtime.ContTrace.class(handlerFrame.next, cur.contTrace.last, handlerFrame.nextHandler, cur.contTrace.lastHandler, false); + cur.contTrace.last = handlerFrame; + cur.contTrace.lastHandler = handlerFrame; + handlerFrame.next = null; + handlerFrame.nextHandler = null; + tmp2 = Runtime.resume(cur.contTrace); + tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); + cur = tmp3; + if (cur instanceof Runtime.EffectSig.class) { + let scrut1, scrut2; /** scoped **/ + scrut1 = saved.next !== null; + if (scrut1 === true) { + cur.contTrace.last.next = saved.next; + cur.contTrace.last = saved.last; + tmp4 = runtime.Unit; + } else { + tmp4 = runtime.Unit; } - scrut = prevHandlerFrame.nextHandler === null; + scrut2 = saved.nextHandler !== null; + if (scrut2 === true) { + cur.contTrace.lastHandler.nextHandler = saved.nextHandler; + cur.contTrace.lastHandler = saved.lastHandler; + tmp5 = runtime.Unit; + } else { + tmp5 = runtime.Unit; + } + return cur + } else { + return Runtime.resumeContTrace(saved, cur) + } + } + static resume(contTrace) { + return (value) => { + let scrut, tmp, tmp1; /** scoped **/ + scrut = contTrace.resumed; if (scrut === true) { - return cur + throw globalThis.Error("Multiple resumption") } else { - tmp1 = runtime.Unit; + tmp = runtime.Unit; } - handlerFrame = prevHandlerFrame.nextHandler; - saved = new Runtime.ContTrace.class(handlerFrame.next, cur.contTrace.last, handlerFrame.nextHandler, cur.contTrace.lastHandler, false); - cur.contTrace.last = handlerFrame; - cur.contTrace.lastHandler = handlerFrame; - handlerFrame.next = null; - handlerFrame.nextHandler = null; - tmp2 = Runtime.resume(cur.contTrace); - tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); - cur = tmp3; - if (cur instanceof Runtime.EffectSig.class) {{ - let scrut1, scrut2; /** scoped **/ - scrut1 = saved.next !== null; - if (scrut1 === true) { - cur.contTrace.last.next = saved.next; - cur.contTrace.last = saved.last; + contTrace.resumed = true; + tmp1 = Runtime.resumeContTrace(contTrace, value); + return Runtime.handleEffects(tmp1) + } + } + static resumeContTrace(contTrace, value) { + let cont, handlerCont, curDepth, tmp; /** scoped **/ + cont = contTrace.next; + handlerCont = contTrace.nextHandler; + curDepth = Runtime.stackDepth; + tmp1: while (true) { + let tmp2, tmp3; /** scoped **/ + if (cont instanceof Runtime.FunctionContFrame.class) { + let tmp4, tmp5; /** scoped **/ + tmp2 = runtime.safeCall(cont.resume(value)); + value = tmp2; + Runtime.stackDepth = curDepth; + if (value instanceof Runtime.EffectSig.class) { + let scrut, scrut1; /** scoped **/ + value.contTrace.last.next = cont.next; + value.contTrace.lastHandler.nextHandler = handlerCont; + scrut = contTrace.last !== cont; + if (scrut === true) { + value.contTrace.last = contTrace.last; tmp4 = runtime.Unit; } else { tmp4 = runtime.Unit; } - scrut2 = saved.nextHandler !== null; - if (scrut2 === true) { - cur.contTrace.lastHandler.nextHandler = saved.nextHandler; - cur.contTrace.lastHandler = saved.lastHandler; + scrut1 = handlerCont !== null; + if (scrut1 === true) { + value.contTrace.lastHandler = contTrace.lastHandler; tmp5 = runtime.Unit; } else { tmp5 = runtime.Unit; } - return cur + return value + } else { + cont = cont.next; + tmp3 = runtime.Unit; } + tmp = tmp3; + continue tmp1 } else { - return Runtime.resumeContTrace(saved, cur) - } - } - } - static resume(contTrace) { - return (value) => {{ - let scrut, tmp, tmp1; /** scoped **/ - scrut = contTrace.resumed; - if (scrut === true) { - throw globalThis.Error("Multiple resumption") - } else { + if (handlerCont instanceof Runtime.HandlerContFrame.class) { + cont = handlerCont.next; + handlerCont = handlerCont.nextHandler; tmp = runtime.Unit; + continue tmp1 + } else { + return value } - contTrace.resumed = true; - tmp1 = Runtime.resumeContTrace(contTrace, value); - return Runtime.handleEffects(tmp1) } + break; } + return tmp } - static resumeContTrace(contTrace, value) {{ - let cont, handlerCont, curDepth, tmp; /** scoped **/ - cont = contTrace.next; - handlerCont = contTrace.nextHandler; - curDepth = Runtime.stackDepth; - tmp1: while (true) {{ - let tmp2, tmp3; /** scoped **/ - if (cont instanceof Runtime.FunctionContFrame.class) {{ - let tmp4, tmp5; /** scoped **/ - tmp2 = runtime.safeCall(cont.resume(value)); - value = tmp2; - Runtime.stackDepth = curDepth; - if (value instanceof Runtime.EffectSig.class) {{ - let scrut, scrut1; /** scoped **/ - value.contTrace.last.next = cont.next; - value.contTrace.lastHandler.nextHandler = handlerCont; - scrut = contTrace.last !== cont; - if (scrut === true) { - value.contTrace.last = contTrace.last; - tmp4 = runtime.Unit; - } else { - tmp4 = runtime.Unit; - } - scrut1 = handlerCont !== null; - if (scrut1 === true) { - value.contTrace.lastHandler = contTrace.lastHandler; - tmp5 = runtime.Unit; - } else { - tmp5 = runtime.Unit; - } - return value - } - } else { - cont = cont.next; - tmp3 = runtime.Unit; - } - tmp = tmp3; - continue tmp1 - } - } else { - if (handlerCont instanceof Runtime.HandlerContFrame.class) { - cont = handlerCont.next; - handlerCont = handlerCont.nextHandler; - tmp = runtime.Unit; - continue tmp1 - } else { - return value - } - } - } - break; - } - return tmp + static checkDepth() { + let scrut, tmp, lambda; /** scoped **/ + tmp = Runtime.stackDepth >= Runtime.stackLimit; + lambda = (undefined, function () { + return Runtime.stackHandler !== null + }); + scrut = runtime.short_and(tmp, lambda); + if (scrut === true) { + return runtime.safeCall(Runtime.stackHandler.delay()) + } else { + return runtime.Unit } } - static checkDepth() {{ - let scrut, tmp, lambda; /** scoped **/ - tmp = Runtime.stackDepth >= Runtime.stackLimit; - lambda = (undefined, function () { - return Runtime.stackHandler !== null - }); - scrut = runtime.short_and(tmp, lambda); + static runStackSafe(limit, f) { + let result, tmp; /** scoped **/ + Runtime.stackLimit = limit; + Runtime.stackDepth = 1; + Runtime.stackHandler = Runtime.StackDelayHandler; + result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); + Runtime.stackDepth = 1; + tmp1: while (true) { + let scrut, saved, tmp2; /** scoped **/ + scrut = Runtime.stackResume !== null; if (scrut === true) { - return runtime.safeCall(Runtime.stackHandler.delay()) + saved = Runtime.stackResume; + Runtime.stackResume = null; + tmp2 = runtime.safeCall(saved()); + result = tmp2; + Runtime.stackDepth = 1; + tmp = runtime.Unit; + continue tmp1 } else { - return runtime.Unit + tmp = runtime.Unit; } + break; } + Runtime.stackLimit = 0; + Runtime.stackDepth = 0; + Runtime.stackHandler = null; + return result } - static runStackSafe(limit, f) {{ - let result, tmp; /** scoped **/ - Runtime.stackLimit = limit; - Runtime.stackDepth = 1; - Runtime.stackHandler = Runtime.StackDelayHandler; - result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); - Runtime.stackDepth = 1; - tmp1: while (true) {{ - let scrut, saved, tmp2; /** scoped **/ - scrut = Runtime.stackResume !== null; - if (scrut === true) { - saved = Runtime.stackResume; - Runtime.stackResume = null; - tmp2 = runtime.safeCall(saved()); - result = tmp2; - Runtime.stackDepth = 1; - tmp = runtime.Unit; - continue tmp1 - } else { - tmp = runtime.Unit; - } - } - break; - } - Runtime.stackLimit = 0; - Runtime.stackDepth = 0; - Runtime.stackHandler = null; - return result - } - } - static plus_impl(lhs, rhs) {{ - let tmp; /** scoped **/ - split_root$: { - split_1$: { - if (lhs instanceof Runtime.Int31.class) { - if (rhs instanceof Runtime.Int31.class) { - tmp = lhs + rhs; - break split_root$ - } else { - break split_1$ - } + static plus_impl(lhs, rhs) { + let tmp; /** scoped **/ + split_root$: { + split_1$: { + if (lhs instanceof Runtime.Int31.class) { + if (rhs instanceof Runtime.Int31.class) { + tmp = lhs + rhs; + break split_root$ } else { break split_1$ } + } else { + break split_1$ } - tmp = Runtime.unreachable(); } - return tmp + tmp = Runtime.unreachable(); } + return tmp } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Runtime"]; diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index 7480913d4d..07725a0283 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,8 +81,7 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; /** scoped **/ -//│ f5 = function f() {{ let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 } }; +//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 }; :fixme :expect 42 @@ -134,10 +133,10 @@ module Foo :e fun f(x: Foo): Foo = x //│ ╔══[ERROR] Expected 1 type arguments, got none -//│ ║ l.135: fun f(x: Foo): Foo = x +//│ ║ l.134: fun f(x: Foo): Foo = x //│ ╙── ^^^ //│ ╔══[ERROR] Expected 1 type arguments, got none -//│ ║ l.135: fun f(x: Foo): Foo = x +//│ ║ l.134: fun f(x: Foo): Foo = x //│ ╙── ^^^ fun f(x: Foo[Int]): Foo[Int] = x @@ -147,10 +146,10 @@ fun f(module x: Foo): module Foo = x :e fun f(module x: Foo[Int]): module Foo[Int] = x //│ ╔══[ERROR] Expected no type arguments, got 1 -//│ ║ l.148: fun f(module x: Foo[Int]): module Foo[Int] = x +//│ ║ l.147: fun f(module x: Foo[Int]): module Foo[Int] = x //│ ╙── ^^^^^^^^ //│ ╔══[ERROR] Expected no type arguments, got 1 -//│ ║ l.148: fun f(module x: Foo[Int]): module Foo[Int] = x +//│ ║ l.147: fun f(module x: Foo[Int]): module Foo[Int] = x //│ ╙── ^^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 51186bf223..9ac7a2e8e9 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -255,7 +255,7 @@ fun bar() = baz //│ baz = function baz() { //│ return 1 //│ }; -//│ bar = function bar() {{ let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 } }; +//│ bar = function bar() { let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 }; // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index 8153dc1d64..dd9ce4d58f 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -20,17 +20,16 @@ fun buildPalindrome = case //│ let buildPalindrome; /** scoped **/ //│ buildPalindrome = function buildPalindrome() { //│ let lambda; -//│ lambda = (undefined, function (caseScrut) {{ -//│ let n, tmp3, tmp4, tmp5; /** scoped **/ -//│ if (caseScrut === 0) { -//│ return globalThis.Object.freeze([]) -//│ } else { -//│ n = caseScrut; -//│ tmp3 = buildPalindrome(); -//│ tmp4 = n - 1; -//│ tmp5 = tmp3(tmp4); -//│ return globalThis.Object.freeze(runtime.Tuple.lazyConcat(n, runtime.Tuple.split, tmp5, n)) -//│ } +//│ lambda = (undefined, function (caseScrut) { +//│ let n, tmp3, tmp4, tmp5; /** scoped **/ +//│ if (caseScrut === 0) { +//│ return globalThis.Object.freeze([]) +//│ } else { +//│ n = caseScrut; +//│ tmp3 = buildPalindrome(); +//│ tmp4 = n - 1; +//│ tmp5 = tmp3(tmp4); +//│ return globalThis.Object.freeze(runtime.Tuple.lazyConcat(n, runtime.Tuple.split, tmp5, n)) //│ } //│ }); //│ return lambda @@ -52,24 +51,23 @@ fun sum2 = case //│ let sum2; /** scoped **/ //│ sum2 = function sum2() { //│ let lambda; -//│ lambda = (undefined, function (caseScrut) {{ -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { -//│ return 0 -//│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { -//│ element0$ = runtime.Tuple.get(caseScrut, 0); -//│ middleElements3 = runtime.safeCall(runtime.Tuple.slice(caseScrut, 1, 1)); -//│ lastElement0$3 = runtime.Tuple.get(caseScrut, -1); -//│ y = lastElement0$3; -//│ xs = middleElements3; -//│ x = element0$; -//│ tmp5 = x + y; -//│ tmp6 = sum2(); -//│ tmp7 = tmp6(xs); -//│ return tmp5 + tmp7 -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } +//│ lambda = (undefined, function (caseScrut) { +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { +//│ return 0 +//│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { +//│ element0$ = runtime.Tuple.get(caseScrut, 0); +//│ middleElements3 = runtime.safeCall(runtime.Tuple.slice(caseScrut, 1, 1)); +//│ lastElement0$3 = runtime.Tuple.get(caseScrut, -1); +//│ y = lastElement0$3; +//│ xs = middleElements3; +//│ x = element0$; +//│ tmp5 = x + y; +//│ tmp6 = sum2(); +//│ tmp7 = tmp6(xs); +//│ return tmp5 + tmp7 +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ }); //│ return lambda @@ -82,13 +80,13 @@ sum2(arr) :e fun f(..xs) = xs //│ ╔══[ERROR] Lazy spread parameters not allowed. -//│ ║ l.83: fun f(..xs) = xs +//│ ║ l.81: fun f(..xs) = xs //│ ╙── ^^^^ :ge sum2(..arr) //│ ╔══[COMPILATION ERROR] Lazy spreads are not supported in call arguments -//│ ║ l.89: sum2(..arr) +//│ ║ l.87: sum2(..arr) //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: Function expected 1 argument but got 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index 13c14110a3..dae4535fa9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -79,11 +79,10 @@ let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): //│ let f; //│ let f1; /** scoped **/ -//│ f = function f(x, y) {{ -//│ let tmp4; /** scoped **/ -//│ tmp4 = Foo7(x, y); -//│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) -//│ } +//│ f = function f(x, y) { +//│ let tmp4; /** scoped **/ +//│ tmp4 = Foo7(x, y); +//│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) //│ }; //│ f1 = f; //│ f = fun f diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index f7a8de0017..9c2202fc04 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -20,9 +20,7 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): //│ let f1; /** scoped **/ -//│ f1 = function f(n1) { -//│ return (n2) => {{ let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } } -//│ }; +//│ f1 = function f(n1) { return (n2) => { let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } }; // TODO compile this to // this.f$(4, 2) @@ -37,13 +35,12 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ let f2; /** scoped **/ //│ f2 = function f(n1) { //│ return (n2) => { -//│ return (n3) => {{ -//│ let tmp1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = 10 * n1; -//│ tmp2 = tmp1 + n2; -//│ tmp3 = 10 * tmp2; -//│ return tmp3 + n3 -//│ } +//│ return (n3) => { +//│ let tmp1, tmp2, tmp3; /** scoped **/ +//│ tmp1 = 10 * n1; +//│ tmp2 = tmp1 + n2; +//│ tmp3 = 10 * tmp2; +//│ return tmp3 + n3 //│ } //│ } //│ }; @@ -62,15 +59,14 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ return (n4) => {{ -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ -//│ tmp3 = 10 * n1; -//│ tmp4 = tmp3 + n2; -//│ tmp5 = 10 * tmp4; -//│ tmp6 = tmp5 + n3; -//│ tmp7 = 10 * tmp6; -//│ return tmp7 + n4 -//│ } +//│ return (n4) => { +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ +//│ tmp3 = 10 * n1; +//│ tmp4 = tmp3 + n2; +//│ tmp5 = 10 * tmp4; +//│ tmp6 = tmp5 + n3; +//│ tmp7 = 10 * tmp6; +//│ return tmp7 + n4 //│ } //│ } //│ } diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index 868d71282e..aa73bdb0d9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,14 +80,12 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ { -//│ let scrut, tmp17; /** scoped **/ -//│ tmp17 = 2 * 3; -//│ scrut = 1 + tmp17; -//│ if (scrut === true) { -//│ 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let scrut, tmp17; /** scoped **/ +//│ tmp17 = 2 * 3; +//│ scrut = 1 + tmp17; +//│ if (scrut === true) { +//│ 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ ═══[RUNTIME ERROR] Error: match error :pt @@ -118,23 +116,23 @@ if 1 + 2 * 3 then 0 + 4 then 1 //│ ╔══[PARSE ERROR] Operator cannot be used inside this operator split -//│ ║ l.119: + 4 then 1 +//│ ║ l.117: + 4 then 1 //│ ║ ^ //│ ╟── as it has lower precedence than the splitting operator here -//│ ║ l.118: * 3 then 0 +//│ ║ l.116: * 3 then 0 //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected 'then' keyword in this operator split inner position -//│ ║ l.119: + 4 then 1 +//│ ║ l.117: + 4 then 1 //│ ║ ^^^^ //│ ╟── Note: the operator split starts here -//│ ║ l.118: * 3 then 0 +//│ ║ l.116: * 3 then 0 //│ ╙── ^ //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.117: if 1 + 2 +//│ ║ l.115: if 1 + 2 //│ ║ ^ -//│ ║ l.118: * 3 then 0 +//│ ║ l.116: * 3 then 0 //│ ║ ^^^^^^^^^^^^ -//│ ║ l.119: + 4 then 1 +//│ ║ l.117: + 4 then 1 //│ ╙── ^^^^^ :pt diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index 567f89950c..e1e6e3a5f0 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -111,7 +111,7 @@ let f = add(_, 1) //│ JS (unsanitized): //│ let f; //│ let f1; /** scoped **/ -//│ f = function f(_0) {{ let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) } }; +//│ f = function f(_0) { let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) }; //│ f1 = f; //│ f = fun f diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index 3ce3d11b78..02207fb129 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -37,13 +37,12 @@ fun test(x) = fold(||)(0, false, 42, 123) //│ JS (unsanitized): //│ let lambda5, tmp; /** scoped **/ -//│ lambda5 = (undefined, function (arg1, arg2) {{ -//│ let lambda6; /** scoped **/ -//│ lambda6 = (undefined, function () { -//│ return arg2 -//│ }); -//│ return runtime.short_or(arg1, lambda6) -//│ } +//│ lambda5 = (undefined, function (arg1, arg2) { +//│ let lambda6; /** scoped **/ +//│ lambda6 = (undefined, function () { +//│ return arg2 +//│ }); +//│ return runtime.short_or(arg1, lambda6) //│ }); //│ tmp = runtime.safeCall(Predef.fold(lambda5)); //│ runtime.safeCall(tmp(0, false, 42, 123)) diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index c92e767325..b9d1163e58 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ { let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } } +//│ let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 25738567f0..01726b0d7d 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ { let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } } +//│ let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ { let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } } +//│ let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } //│ = 1 //│ Type: Int @@ -150,17 +150,16 @@ fun pow(x) = case //│ let pow; /** scoped **/ //│ pow = function pow(x1) { //│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) {{ -//│ let n, tmp, tmp1, tmp2; /** scoped **/ -//│ if (caseScrut === 0) { -//│ return 1 -//│ } else { -//│ n = caseScrut; -//│ tmp = pow(x1); -//│ tmp1 = n - 1; -//│ tmp2 = runtime.safeCall(tmp(tmp1)); -//│ return x1 * tmp2 -//│ } +//│ lambda1 = (undefined, function (caseScrut) { +//│ let n, tmp, tmp1, tmp2; /** scoped **/ +//│ if (caseScrut === 0) { +//│ return 1 +//│ } else { +//│ n = caseScrut; +//│ tmp = pow(x1); +//│ tmp1 = n - 1; +//│ tmp2 = runtime.safeCall(tmp(tmp1)); +//│ return x1 * tmp2 //│ } //│ }); //│ return lambda1 @@ -207,17 +206,16 @@ fun fact = case //│ let fact; /** scoped **/ //│ fact = function fact() { //│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) {{ -//│ let n, tmp1, tmp2, tmp3; /** scoped **/ -//│ if (caseScrut === 0) { -//│ return 1 -//│ } else { -//│ n = caseScrut; -//│ tmp1 = fact(); -//│ tmp2 = n - 1; -//│ tmp3 = tmp1(tmp2); -//│ return n * tmp3 -//│ } +//│ lambda1 = (undefined, function (caseScrut) { +//│ let n, tmp1, tmp2, tmp3; /** scoped **/ +//│ if (caseScrut === 0) { +//│ return 1 +//│ } else { +//│ n = caseScrut; +//│ tmp1 = fact(); +//│ tmp2 = n - 1; +//│ tmp3 = tmp1(tmp2); +//│ return n * tmp3 //│ } //│ }); //│ return lambda1 diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index aaa6637bd8..87c687f79b 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -88,33 +88,30 @@ fun test2() = //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): //│ let test22; /** scoped **/ -//│ test22 = function test2() {{ -//│ let funny, tmp1; /** scoped **/ -//│ funny = function funny() { -//│ let lambda;{ -//│ let lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (caseScrut) { -//│ if (caseScrut === 0) { -//│ return 0 -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ }); -//│ lambda = (undefined, function (caseScrut) {{ -//│ let n, tmp2, tmp3, tmp4; /** scoped **/ -//│ n = caseScrut; -//│ tmp2 = funny(); -//│ tmp3 = n - 1; -//│ tmp4 = tmp2(tmp3); -//│ return tmp4 + 1 -//│ } -//│ }); -//│ return lambda +//│ test22 = function test2() { +//│ let funny, tmp1; /** scoped **/ +//│ funny = function funny() { +//│ let lambda; +//│ let lambda1; /** scoped **/ +//│ lambda1 = (undefined, function (caseScrut) { +//│ if (caseScrut === 0) { +//│ return 0 +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ }; -//│ tmp1 = funny(); -//│ return tmp1 -//│ } +//│ }); +//│ lambda = (undefined, function (caseScrut) { +//│ let n, tmp2, tmp3, tmp4; /** scoped **/ +//│ n = caseScrut; +//│ tmp2 = funny(); +//│ tmp3 = n - 1; +//│ tmp4 = tmp2(tmp3); +//│ return tmp4 + 1 +//│ }); +//│ return lambda +//│ }; +//│ tmp1 = funny(); +//│ return tmp1 //│ }; //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.83: case 0 then 0 @@ -134,7 +131,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.135: print("Hi") +//│ ║ l.132: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index e7081a77fd..8be8e2459a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -16,27 +16,26 @@ fun test(x) = None then print("none") //│ JS (unsanitized): //│ let test; /** scoped **/ -//│ test = function test(x) {{ -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ -//│ if (x instanceof Some.class) { -//│ argument0$1 = x.value; -//│ v = argument0$1; -//│ tmp = v + 1; -//│ tmp1 = Some(tmp); -//│ } else if (x instanceof None.class) { -//│ tmp1 = None; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ scrut = tmp1; -//│ if (scrut instanceof Some.class) { -//│ argument0$ = scrut.value; -//│ v1 = argument0$; -//│ return Predef.print(v1) -//│ } else if (scrut instanceof None.class) { -//│ return Predef.print("none") -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } +//│ test = function test(x) { +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ +//│ if (x instanceof Some.class) { +//│ argument0$1 = x.value; +//│ v = argument0$1; +//│ tmp = v + 1; +//│ tmp1 = Some(tmp); +//│ } else if (x instanceof None.class) { +//│ tmp1 = None; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } +//│ scrut = tmp1; +//│ if (scrut instanceof Some.class) { +//│ argument0$ = scrut.value; +//│ v1 = argument0$; +//│ return Predef.print(v1) +//│ } else if (scrut instanceof None.class) { +//│ return Predef.print("none") +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index a0de0717d3..2c5e5cee00 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -8,11 +8,10 @@ case x then x case { x then x } //│ JS (unsanitized): //│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) {{ -//│ let x; /** scoped **/ -//│ x = caseScrut; -//│ return x -//│ } +//│ lambda1 = (undefined, function (caseScrut) { +//│ let x; /** scoped **/ +//│ x = caseScrut; +//│ return x //│ }); //│ lambda1 //│ = fun @@ -60,10 +59,10 @@ case 0 then true :todo // TODO: support this braceless syntax? case [x] then x, [] then 0 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.61: case [x] then x, [] then 0 +//│ ║ l.60: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^ //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.61: case [x] then x, [] then 0 +//│ ║ l.60: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^^^^^^^ :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index e145bfd6cb..67640a68bf 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -59,11 +59,10 @@ data class Outer(a, b) with //│ o1(c) { //│ return this.Inner(c) //│ } -//│ o2(c, d) {{ -//│ let tmp6; /** scoped **/ -//│ tmp6 = this.Inner(c); -//│ return tmp6.i1(d) -//│ } +//│ o2(c, d) { +//│ let tmp6; /** scoped **/ +//│ tmp6 = this.Inner(c); +//│ return tmp6.i1(d) //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Outer", ["a", "b"]]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index 2689aaa7f2..696a1e22bd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -7,20 +7,19 @@ fun test(a) = new C //│ JS (unsanitized): //│ let test; /** scoped **/ -//│ test = function test(a) {{ -//│ let C1; /** scoped **/ -//│ globalThis.Object.freeze(class C { -//│ static { -//│ C1 = this -//│ } -//│ constructor() { -//│ this.x = a; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "C"]; -//│ }); -//│ return globalThis.Object.freeze(new C1()) -//│ } +//│ test = function test(a) { +//│ let C1; /** scoped **/ +//│ globalThis.Object.freeze(class C { +//│ static { +//│ C1 = this +//│ } +//│ constructor() { +//│ this.x = a; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "C"]; +//│ }); +//│ return globalThis.Object.freeze(new C1()) //│ }; test(12) @@ -39,25 +38,24 @@ fun test(x) = Foo(x, x + 1) //│ JS (unsanitized): //│ let test2; /** scoped **/ -//│ test2 = function test(x) {{ -//│ let Foo2, tmp; /** scoped **/ -//│ Foo2 = function Foo(a, b) { -//│ return globalThis.Object.freeze(new Foo.class(a, b)); -//│ }; -//│ globalThis.Object.freeze(class Foo1 { -//│ static { -//│ Foo2.class = this -//│ } -//│ constructor(a, b) { -//│ this.a = a; -//│ this.b = b; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Foo", ["a", "b"]]; -//│ }); -//│ tmp = x + 1; -//│ return Foo2(x, tmp) -//│ } +//│ test2 = function test(x) { +//│ let Foo2, tmp; /** scoped **/ +//│ Foo2 = function Foo(a, b) { +//│ return globalThis.Object.freeze(new Foo.class(a, b)); +//│ }; +//│ globalThis.Object.freeze(class Foo1 { +//│ static { +//│ Foo2.class = this +//│ } +//│ constructor(a, b) { +//│ this.a = a; +//│ this.b = b; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Foo", ["a", "b"]]; +//│ }); +//│ tmp = x + 1; +//│ return Foo2(x, tmp) //│ }; @@ -70,7 +68,7 @@ test(123) :re test() //│ ╔══[ERROR] Expected 1 arguments, got 0 -//│ ║ l.71: test() +//│ ║ l.69: test() //│ ╙── ^^ //│ ═══[RUNTIME ERROR] Error: Function 'test' expected 1 argument but got 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 9c37025e6a..b4df883b66 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,15 +9,13 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ { -//│ let scrut, x, argument0$; /** scoped **/ -//│ scrut = Some(0); -//│ if (scrut instanceof Some.class) { -//│ argument0$ = scrut.value; -//│ x = argument0$; -//│ x -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let scrut, x, argument0$; /** scoped **/ +//│ scrut = Some(0); +//│ if (scrut instanceof Some.class) { +//│ argument0$ = scrut.value; +//│ x = argument0$; +//│ x +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 0 @@ -28,14 +26,12 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ { -//│ let x1, argument0$1; /** scoped **/ -//│ if (s instanceof Some.class) { -//│ argument0$1 = s.value; -//│ x1 = argument0$1; -//│ x1 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let x1, argument0$1; /** scoped **/ +//│ if (s instanceof Some.class) { +//│ argument0$1 = s.value; +//│ x1 = argument0$1; +//│ x1 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 0 if s is @@ -58,14 +54,13 @@ if s is x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; -//│ lambda = (undefined, function (x3) {{ -//│ let x4, argument0$4; /** scoped **/ -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ x4 = argument0$4; -//│ return x4 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ lambda = (undefined, function (x3) { +//│ let x4, argument0$4; /** scoped **/ +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ x4 = argument0$4; +//│ return x4 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }); //│ lambda //│ = fun @@ -124,29 +119,28 @@ fun f(x) = if x is else print("oops") //│ JS (unsanitized): //│ let f4; /** scoped **/ -//│ f4 = function f(x3) {{ -//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ -//│ split_root$1: { -//│ split_1$: { -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ x4 = argument0$4; -//│ scrut1 = x4 > 0; -//│ if (scrut1 === true) { -//│ tmp6 = 42; -//│ break split_root$1 -//│ } else { -//│ break split_1$ -//│ } -//│ } else if (x3 instanceof None.class) { -//│ tmp6 = "ok"; +//│ f4 = function f(x3) { +//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ +//│ split_root$1: { +//│ split_1$: { +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ x4 = argument0$4; +//│ scrut1 = x4 > 0; +//│ if (scrut1 === true) { +//│ tmp6 = 42; //│ break split_root$1 -//│ } else { break split_1$ } -//│ } -//│ tmp6 = Predef.print("oops"); +//│ } else { +//│ break split_1$ +//│ } +//│ } else if (x3 instanceof None.class) { +//│ tmp6 = "ok"; +//│ break split_root$1 +//│ } else { break split_1$ } //│ } -//│ return tmp6 +//│ tmp6 = Predef.print("oops"); //│ } +//│ return tmp6 //│ }; f(Some(0)) @@ -171,20 +165,19 @@ fun f(x) = if x is Pair(a, b) then a + b //│ JS (unsanitized): //│ let f5; /** scoped **/ -//│ f5 = function f(x3) {{ -//│ let u, a, b, argument0$4, argument1$; /** scoped **/ -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ u = argument0$4; -//│ return u -//│ } else if (x3 instanceof Pair.class) { -//│ argument0$4 = x3.fst; -//│ argument1$ = x3.snd; -//│ b = argument1$; -//│ a = argument0$4; -//│ return a + b -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ f5 = function f(x3) { +//│ let u, a, b, argument0$4, argument1$; /** scoped **/ +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ u = argument0$4; +//│ return u +//│ } else if (x3 instanceof Pair.class) { +//│ argument0$4 = x3.fst; +//│ argument1$ = x3.snd; +//│ b = argument1$; +//│ a = argument0$4; +//│ return a + b +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; f(Some(123)) @@ -202,27 +195,26 @@ fun f(x) = print of if x is else "oops" //│ JS (unsanitized): //│ let f6; /** scoped **/ -//│ f6 = function f(x3) {{ -//│ let argument0$4, tmp10; /** scoped **/ -//│ split_root$1: { -//│ split_1$: { -//│ if (x3 instanceof Some.class) { -//│ argument0$4 = x3.value; -//│ if (argument0$4 === 0) { -//│ tmp10 = "0"; -//│ break split_root$1 -//│ } else { -//│ break split_1$ -//│ } -//│ } else if (x3 instanceof None.class) { -//│ tmp10 = "ok"; +//│ f6 = function f(x3) { +//│ let argument0$4, tmp10; /** scoped **/ +//│ split_root$1: { +//│ split_1$: { +//│ if (x3 instanceof Some.class) { +//│ argument0$4 = x3.value; +//│ if (argument0$4 === 0) { +//│ tmp10 = "0"; //│ break split_root$1 -//│ } else { break split_1$ } -//│ } -//│ tmp10 = "oops"; +//│ } else { +//│ break split_1$ +//│ } +//│ } else if (x3 instanceof None.class) { +//│ tmp10 = "ok"; +//│ break split_root$1 +//│ } else { break split_1$ } //│ } -//│ return Predef.print(tmp10) +//│ tmp10 = "oops"; //│ } +//│ return Predef.print(tmp10) //│ }; f(Some(0)) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index c697cc38ad..af16010767 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -8,21 +8,19 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): //│ let f; /** scoped **/ -//│ f = function f() {{ -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 -//│ } +//│ f = function f() { +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 //│ }; fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): //│ let f1; /** scoped **/ -//│ f1 = function f() {{ -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 -//│ } +//│ f1 = function f() { +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 //│ }; fun f() = console.log("ok"), 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 64e60adbba..1c439570a2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -19,15 +19,14 @@ fun f(x) = x + 1 //│ JS (unsanitized): //│ let f1; /** scoped **/ -//│ f1 = function f(x) {{ -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ scrut = x < 0; -//│ if (scrut === true) { -//│ tmp = Predef.print("whoops"); -//│ return 0 -//│ } else { tmp1 = runtime.Unit; } -//│ return x + 1 -//│ } +//│ f1 = function f(x) { +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ scrut = x < 0; +//│ if (scrut === true) { +//│ tmp = Predef.print("whoops"); +//│ return 0 +//│ } else { tmp1 = runtime.Unit; } +//│ return x + 1 //│ }; f(1) diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 7fb778805c..1078c18fd7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -20,45 +20,43 @@ fun test(a) = Inner(42) //│ JS (unsanitized): //│ let test1; /** scoped **/ -//│ test1 = function test(a) {{ -//│ let Inner1, tmp; /** scoped **/ -//│ Inner1 = function Inner(b) { -//│ return globalThis.Object.freeze(new Inner.class(b)); -//│ }; -//│ globalThis.Object.freeze(class Inner { -//│ static { -//│ Inner1.class = this -//│ } -//│ constructor(b) { -//│ this.b = b; -//│ tmp = Predef.print(a); -//│ } -//│ f(c) { +//│ test1 = function test(a) { +//│ let Inner1, tmp; /** scoped **/ +//│ Inner1 = function Inner(b) { +//│ return globalThis.Object.freeze(new Inner.class(b)); +//│ }; +//│ globalThis.Object.freeze(class Inner { +//│ static { +//│ Inner1.class = this +//│ } +//│ constructor(b) { +//│ this.b = b; +//│ tmp = Predef.print(a); +//│ } +//│ f(c) { +//│ return globalThis.Object.freeze([ +//│ a, +//│ this.b, +//│ c +//│ ]) +//│ } +//│ g(d) { +//│ let h; /** scoped **/ +//│ const this$Inner = this; +//│ h = function h(e) { //│ return globalThis.Object.freeze([ //│ a, -//│ this.b, -//│ c +//│ this$Inner.b, +//│ d, +//│ e //│ ]) -//│ } -//│ g(d) {{ -//│ let h; /** scoped **/ -//│ const this$Inner = this; -//│ h = function h(e) { -//│ return globalThis.Object.freeze([ -//│ a, -//│ this$Inner.b, -//│ d, -//│ e -//│ ]) -//│ }; -//│ return h(d) -//│ } -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Inner", ["b"]]; -//│ }); -//│ return Inner1(42) -//│ } +//│ }; +//│ return h(d) +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Inner", ["b"]]; +//│ }); +//│ return Inner1(42) //│ }; let i = test(100) @@ -81,48 +79,47 @@ fun test(a) = [C1(1), C2(2)] //│ JS (unsanitized): //│ let test2; /** scoped **/ -//│ test2 = function test(a) {{ -//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ C11 = function C1(b) { -//│ return globalThis.Object.freeze(new C1.class(b)); -//│ }; -//│ globalThis.Object.freeze(class C1 { -//│ static { -//│ C11.class = this -//│ } -//│ constructor(b) { -//│ this.b = b; -//│ tmp = globalThis.Object.freeze([ -//│ a, -//│ this.b -//│ ]); -//│ Predef.print(tmp); -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "C1", ["b"]]; -//│ }); -//│ C21 = function C2(b) { -//│ return globalThis.Object.freeze(new C2.class(b)); -//│ }; -//│ globalThis.Object.freeze(class C2 { -//│ static { -//│ C21.class = this -//│ } -//│ constructor(b) { -//│ this.b = b; -//│ tmp1 = globalThis.Object.freeze([ -//│ a, -//│ this.b -//│ ]); -//│ Predef.print(tmp1); -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "C2", ["b"]]; -//│ }); -//│ tmp2 = C11(1); -//│ tmp3 = C21(2); -//│ return globalThis.Object.freeze([ tmp2, tmp3 ]) -//│ } +//│ test2 = function test(a) { +//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ C11 = function C1(b) { +//│ return globalThis.Object.freeze(new C1.class(b)); +//│ }; +//│ globalThis.Object.freeze(class C1 { +//│ static { +//│ C11.class = this +//│ } +//│ constructor(b) { +//│ this.b = b; +//│ tmp = globalThis.Object.freeze([ +//│ a, +//│ this.b +//│ ]); +//│ Predef.print(tmp); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "C1", ["b"]]; +//│ }); +//│ C21 = function C2(b) { +//│ return globalThis.Object.freeze(new C2.class(b)); +//│ }; +//│ globalThis.Object.freeze(class C2 { +//│ static { +//│ C21.class = this +//│ } +//│ constructor(b) { +//│ this.b = b; +//│ tmp1 = globalThis.Object.freeze([ +//│ a, +//│ this.b +//│ ]); +//│ Predef.print(tmp1); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "C2", ["b"]]; +//│ }); +//│ tmp2 = C11(1); +//│ tmp3 = C21(2); +//│ return globalThis.Object.freeze([ tmp2, tmp3 ]) //│ }; test(123) @@ -153,18 +150,17 @@ Foo(123) //│ this.a = a; //│ this.foo(); //│ } -//│ foo() {{ -//│ let bar, baz, tmp; /** scoped **/ -//│ const this$Foo = this; -//│ bar = function bar() { -//│ return this$Foo.a -//│ }; -//│ baz = function baz() { -//│ return this$Foo.a -//│ }; -//│ tmp = bar(); -//│ return baz() -//│ } +//│ foo() { +//│ let bar, baz, tmp; /** scoped **/ +//│ const this$Foo = this; +//│ bar = function bar() { +//│ return this$Foo.a +//│ }; +//│ baz = function baz() { +//│ return this$Foo.a +//│ }; +//│ tmp = bar(); +//│ return baz() //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Foo", ["a"]]; @@ -195,14 +191,13 @@ Bar(1) //│ runtime.safeCall(tmp()); //│ } //│ foo() { -//│ return () => {{ -//│ let bar; /** scoped **/ -//│ const this$Bar = this; -//│ bar = function bar() { -//│ return this$Bar.x -//│ }; -//│ return bar() -//│ } +//│ return () => { +//│ let bar; /** scoped **/ +//│ const this$Bar = this; +//│ bar = function bar() { +//│ return this$Bar.x +//│ }; +//│ return bar() //│ } //│ } //│ toString() { return runtime.render(this); } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 3ff90f7008..23e4c2f5d8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -38,18 +38,16 @@ fun test() = whoops + whoops //│ JS (unsanitized): //│ let test; /** scoped **/ -//│ test = function test() {{ -//│ let whoops, tmp1, tmp2; /** scoped **/ -//│ whoops = function whoops() {{ -//│ let tmp3; /** scoped **/ -//│ tmp3 = Predef.print("ok"); -//│ return 42 -//│ } -//│ }; -//│ tmp1 = whoops(); -//│ tmp2 = whoops(); -//│ return tmp1 + tmp2 -//│ } +//│ test = function test() { +//│ let whoops, tmp1, tmp2; /** scoped **/ +//│ whoops = function whoops() { +//│ let tmp3; /** scoped **/ +//│ tmp3 = Predef.print("ok"); +//│ return 42 +//│ }; +//│ tmp1 = whoops(); +//│ tmp2 = whoops(); +//│ return tmp1 + tmp2 //│ }; test() @@ -138,12 +136,11 @@ fun test() = whoops //│ JS (unsanitized): //│ let test1; /** scoped **/ -//│ test1 = function test() {{ -//│ let whoops, tmp1; /** scoped **/ -//│ whoops = function whoops() { return 42 }; -//│ tmp1 = whoops(); -//│ return tmp1 -//│ } +//│ test1 = function test() { +//│ let whoops, tmp1; /** scoped **/ +//│ whoops = function whoops() { return 42 }; +//│ tmp1 = whoops(); +//│ return tmp1 //│ }; @@ -167,7 +164,7 @@ fun bar() = baz //│ JS (unsanitized): //│ let bar; /** scoped **/ -//│ bar = function bar() {{ let baz; /** scoped **/ baz = function baz() { return 42 }; return baz } }; +//│ bar = function bar() { let baz; /** scoped **/ baz = function baz() { return 42 }; return baz }; :sjs @@ -178,25 +175,23 @@ fun baz() = //│ JS (unsanitized): //│ let baz; /** scoped **/ //│ baz = function baz() { -//│ let lambda;{ -//│ let w, z; /** scoped **/ -//│ w = function w() { -//│ return 1 -//│ }; -//│ z = function z() { -//│ return 2 -//│ }; -//│ lambda = (undefined, function (x, y) {{ -//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ -//│ tmp1 = x + y; -//│ tmp2 = w(); -//│ tmp3 = tmp1 + tmp2; -//│ tmp4 = z(); -//│ return tmp3 + tmp4 -//│ } -//│ }); -//│ return lambda -//│ } +//│ let lambda; +//│ let w, z; /** scoped **/ +//│ w = function w() { +//│ return 1 +//│ }; +//│ z = function z() { +//│ return 2 +//│ }; +//│ lambda = (undefined, function (x, y) { +//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ tmp1 = x + y; +//│ tmp2 = w(); +//│ tmp3 = tmp1 + tmp2; +//│ tmp4 = z(); +//│ return tmp3 + tmp4 +//│ }); +//│ return lambda //│ }; @@ -213,21 +208,19 @@ fun a() = c //│ JS (unsanitized): //│ let a; /** scoped **/ -//│ a = function a() {{ -//│ let b, c; /** scoped **/ -//│ b = function b() { -//│ return 1 -//│ }; -//│ c = function c() {{ -//│ let d, tmp2, tmp3; /** scoped **/ -//│ d = function d() { return 2 }; -//│ tmp2 = b(); -//│ tmp3 = d(); -//│ return tmp2 + tmp3 -//│ } -//│ }; -//│ return c -//│ } +//│ a = function a() { +//│ let b, c; /** scoped **/ +//│ b = function b() { +//│ return 1 +//│ }; +//│ c = function c() { +//│ let d, tmp2, tmp3; /** scoped **/ +//│ d = function d() { return 2 }; +//│ tmp2 = b(); +//│ tmp3 = d(); +//│ return tmp2 + tmp3 +//│ }; +//│ return c //│ }; @@ -244,20 +237,18 @@ fun b() = d //│ JS (unsanitized): //│ let b; /** scoped **/ -//│ b = function b() {{ -//│ let c, d; /** scoped **/ -//│ c = function c() { -//│ return 1 -//│ }; -//│ d = function d() {{ -//│ let c1, tmp3; /** scoped **/ -//│ c1 = function c() { return 2 }; -//│ tmp3 = c1(); -//│ return tmp3 -//│ } -//│ }; -//│ return d -//│ } +//│ b = function b() { +//│ let c, d; /** scoped **/ +//│ c = function c() { +//│ return 1 +//│ }; +//│ d = function d() { +//│ let c1, tmp3; /** scoped **/ +//│ c1 = function c() { return 2 }; +//│ tmp3 = c1(); +//│ return tmp3 +//│ }; +//│ return d //│ }; @@ -274,24 +265,22 @@ fun c() = d //│ JS (unsanitized): //│ let c; /** scoped **/ -//│ c = function c() {{ -//│ let d, f, tmp4; /** scoped **/ -//│ f = function f() { +//│ c = function c() { +//│ let d, f, tmp4; /** scoped **/ +//│ f = function f() { +//│ return 1 +//│ }; +//│ d = function d() { +//│ let e, tmp5, tmp6; /** scoped **/ +//│ e = function e() { //│ return 1 //│ }; -//│ d = function d() {{ -//│ let e, tmp5, tmp6; /** scoped **/ -//│ e = function e() { -//│ return 1 -//│ }; -//│ tmp5 = e(); -//│ tmp6 = f(); -//│ return tmp5 + tmp6 -//│ } -//│ }; -//│ tmp4 = d(); -//│ return tmp4 -//│ } +//│ tmp5 = e(); +//│ tmp6 = f(); +//│ return tmp5 + tmp6 +//│ }; +//│ tmp4 = d(); +//│ return tmp4 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index 3f3a143f2a..7c40714263 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,13 +21,11 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ { -//│ let scrut; /** scoped **/ -//│ scrut = false; -//│ if (scrut === true) { -//│ 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let scrut; /** scoped **/ +//│ scrut = false; +//│ if (scrut === true) { +//│ 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'freeze') //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'freeze') @@ -40,13 +38,12 @@ fun foo() = foo() //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo() {{ -//│ let scrut1; /** scoped **/ -//│ scrut1 = false; -//│ if (scrut1 === true) { -//│ return 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ foo = function foo() { +//│ let scrut1; /** scoped **/ +//│ scrut1 = false; +//│ if (scrut1 === true) { +//│ return 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; //│ foo() //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'freeze') diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index e77ced4ebc..10433bdb39 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -21,11 +21,10 @@ object Test with //│ value: Test //│ }) //│ } -//│ foo() {{ -//│ let tmp; /** scoped **/ -//│ tmp = Predef.print(Test1); -//│ return Test1.x -//│ } +//│ foo() { +//│ let tmp; /** scoped **/ +//│ tmp = Predef.print(Test1); +//│ return Test1.x //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["object", "Test"]; @@ -79,10 +78,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.80: let x = 2 +//│ ║ l.79: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.79: val x = 1 +//│ ║ l.78: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test4 { @@ -233,7 +232,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.234: Runtime +//│ ║ l.233: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index 4a569315b8..e09edf1374 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -27,14 +27,13 @@ f(false) let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): //│ let f1, lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (x) {{ -//│ let tmp, tmp1; /** scoped **/ -//│ if (x === true) { -//│ tmp = "ok"; -//│ } else { tmp = "ko"; } -//│ tmp1 = tmp + "!"; -//│ return Predef.print(tmp1) -//│ } +//│ lambda1 = (undefined, function (x) { +//│ let tmp, tmp1; /** scoped **/ +//│ if (x === true) { +//│ tmp = "ok"; +//│ } else { tmp = "ko"; } +//│ tmp1 = tmp + "!"; +//│ return Predef.print(tmp1) //│ }); //│ f1 = lambda1; //│ f = fun @@ -43,14 +42,13 @@ let f = x => print((if x then "ok" else "ko") + "!") let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): //│ let f2, lambda2; /** scoped **/ -//│ lambda2 = (undefined, function (x) {{ -//│ let tmp, tmp1; /** scoped **/ -//│ if (x === true) { -//│ tmp = "ok"; -//│ } else { tmp = "ko"; } -//│ tmp1 = tmp + "!"; -//│ return Predef.print(tmp1) -//│ } +//│ lambda2 = (undefined, function (x) { +//│ let tmp, tmp1; /** scoped **/ +//│ if (x === true) { +//│ tmp = "ok"; +//│ } else { tmp = "ko"; } +//│ tmp1 = tmp + "!"; +//│ return Predef.print(tmp1) //│ }); //│ f2 = lambda2; //│ f = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index a99d4baea0..79d3e59992 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -12,11 +12,10 @@ fun foo() = foo() //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo() {{ -//│ let tmp; /** scoped **/ -//│ tmp = M.concat("a", "b"); -//│ return M.concat(tmp, "c") -//│ } +//│ foo = function foo() { +//│ let tmp; /** scoped **/ +//│ tmp = M.concat("a", "b"); +//│ return M.concat(tmp, "c") //│ }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index eb6fc23793..fbe153c46d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -4,14 +4,13 @@ (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): //│ let lambda; /** scoped **/ -//│ lambda = (undefined, function (x) {{ -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ tmp = x + 1; -//│ tmp1 = tmp + 1; -//│ tmp2 = tmp1 + 1; -//│ tmp3 = tmp2 + 1; -//│ return tmp3 + 1 -//│ } +//│ lambda = (undefined, function (x) { +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ tmp = x + 1; +//│ tmp1 = tmp + 1; +//│ tmp2 = tmp1 + 1; +//│ tmp3 = tmp2 + 1; +//│ return tmp3 + 1 //│ }); //│ lambda(1) //│ = 6 @@ -20,15 +19,14 @@ (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): //│ let lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (x) {{ -//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ -//│ tmp = x + 1; -//│ tmp1 = tmp + 1; -//│ tmp2 = tmp1 + 1; -//│ tmp3 = tmp2 + 1; -//│ tmp4 = tmp3 + 1; -//│ return tmp4 + 1 -//│ } +//│ lambda1 = (undefined, function (x) { +//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ tmp = x + 1; +//│ tmp1 = tmp + 1; +//│ tmp2 = tmp1 + 1; +//│ tmp3 = tmp2 + 1; +//│ tmp4 = tmp3 + 1; +//│ return tmp4 + 1 //│ }); //│ lambda1(1) //│ = 7 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls index 82aa43bbfd..86a50c8394 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls @@ -8,7 +8,7 @@ x => let y = x y //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function (x) {{ let y; /** scoped **/ y = x; return y } }); lambda +//│ let lambda; lambda = (undefined, function (x) { let y; /** scoped **/ y = x; return y }); lambda //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 04984a7233..d7b8eb9113 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -114,25 +114,24 @@ print(x) //│ tmp2 = 2; //│ } else if (a instanceof C.class) { //│ tmp2 = 3; -//│ } else {{ -//│ let tmp4; /** scoped **/ -//│ tmp1 = 3; -//│ if (a instanceof D.class) { -//│ if (a instanceof A.class) { -//│ tmp4 = 1 + tmp1; -//│ } else if (a instanceof B.class) { -//│ tmp4 = 2 + tmp1; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ tmp3 = tmp4; -//│ } else if (a instanceof E.class) { -//│ tmp3 = 5; +//│ } else { +//│ let tmp4; /** scoped **/ +//│ tmp1 = 3; +//│ if (a instanceof D.class) { +//│ if (a instanceof A.class) { +//│ tmp4 = 1 + tmp1; +//│ } else if (a instanceof B.class) { +//│ tmp4 = 2 + tmp1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp2 = Predef.print("done"); +//│ tmp3 = tmp4; +//│ } else if (a instanceof E.class) { +//│ tmp3 = 5; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } +//│ tmp2 = Predef.print("done"); //│ } //│ x = tmp2; //│ Predef.print(x) @@ -148,15 +147,13 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ { -//│ let tmp5; /** scoped **/ -//│ tmp5 = 2; -//│ if (a instanceof A.class) { -//│ 1 -//│ } else if (a instanceof B.class) { -//│ 2 + tmp5 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let tmp5; /** scoped **/ +//│ tmp5 = 2; +//│ if (a instanceof A.class) { +//│ 1 +//│ } else if (a instanceof B.class) { +//│ 2 + tmp5 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -172,16 +169,14 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ { -//│ let tmp6; /** scoped **/ -//│ if (a instanceof A.class) { -//│ 1 -//│ } else { -//│ tmp6 = printAndId(3); -//│ if (a instanceof B.class) { -//│ 2 + tmp6 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let tmp6; /** scoped **/ +//│ if (a instanceof A.class) { +//│ 1 +//│ } else { +//│ tmp6 = printAndId(3); +//│ if (a instanceof B.class) { +//│ 2 + tmp6 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ } //│ = 1 @@ -197,20 +192,18 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ { -//│ let x1, tmp7; /** scoped **/ -//│ if (a instanceof A.class) { -//│ 1 -//│ } else if (a instanceof B.class) { -//│ tmp7 = 2; -//│ x1 = tmp7; -//│ Predef.print(x1) -//│ } else if (a instanceof C.class) { -//│ tmp7 = 3; -//│ x1 = tmp7; -//│ Predef.print(x1) -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let x1, tmp7; /** scoped **/ +//│ if (a instanceof A.class) { +//│ 1 +//│ } else if (a instanceof B.class) { +//│ tmp7 = 2; +//│ x1 = tmp7; +//│ Predef.print(x1) +//│ } else if (a instanceof C.class) { +//│ tmp7 = 3; +//│ x1 = tmp7; +//│ Predef.print(x1) +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -228,25 +221,23 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ { -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ -//│ if (a instanceof B.class) { -//│ 1 +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ if (a instanceof B.class) { +//│ 1 +//│ } else { +//│ if (a instanceof A.class) { +//│ tmp8 = 2; +//│ } else if (a instanceof C.class) { +//│ tmp8 = 3; //│ } else { -//│ if (a instanceof A.class) { -//│ tmp8 = 2; -//│ } else if (a instanceof C.class) { -//│ tmp8 = 3; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ x2 = tmp8; -//│ tmp9 = Predef.print(x2); -//│ tmp10 = x2 + 1; -//│ tmp11 = Predef.print(tmp10); -//│ tmp12 = x2 + 2; -//│ Predef.print(tmp12) +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } +//│ x2 = tmp8; +//│ tmp9 = Predef.print(x2); +//│ tmp10 = x2 + 1; +//│ tmp11 = Predef.print(tmp10); +//│ tmp12 = x2 + 2; +//│ Predef.print(tmp12) //│ } //│ > 2 //│ > 3 diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 4bc8484693..2416214a74 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -32,16 +32,15 @@ val isDefined = case None then false //│ JS (unsanitized): //│ let isDefined1, lambda; /** scoped **/ -//│ lambda = (undefined, function (caseScrut) {{ -//│ let argument0$; /** scoped **/ -//│ if (caseScrut instanceof Some.class) { -//│ argument0$ = caseScrut.value; -//│ return true -//│ } else if (caseScrut instanceof None.class) { -//│ return false -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } +//│ lambda = (undefined, function (caseScrut) { +//│ let argument0$; /** scoped **/ +//│ if (caseScrut instanceof Some.class) { +//│ argument0$ = caseScrut.value; +//│ return true +//│ } else if (caseScrut instanceof None.class) { +//│ return false +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ }); //│ isDefined1 = lambda; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index 06e8647e58..9b85fd7887 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -269,10 +269,9 @@ fun f(x) = _ then 2 //│ JS (unsanitized): //│ let f16; /** scoped **/ -//│ f16 = function f(x3) {{ -//│ let scrut; /** scoped **/ -//│ scrut = x3 == 0; -//│ if (scrut === true) { return 1 } else { return 2 } -//│ } +//│ f16 = function f(x3) { +//│ let scrut; /** scoped **/ +//│ scrut = x3 == 0; +//│ if (scrut === true) { return 1 } else { return 2 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 6c559ab5cc..19591829d2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -23,11 +23,9 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ { -//│ let scrut; /** scoped **/ -//│ scrut = globalThis.Object.freeze(new Foo()); -//│ if (scrut instanceof Foo) { true } else { false } -//│ } +//│ let scrut; /** scoped **/ +//│ scrut = globalThis.Object.freeze(new Foo()); +//│ if (scrut instanceof Foo) { true } else { false } //│ = true new Foo @@ -75,21 +73,20 @@ fun test() = Foo //│ JS (unsanitized): //│ let test; /** scoped **/ -//│ test = function test() {{ -//│ let Foo5, tmp; /** scoped **/ -//│ globalThis.Object.freeze(class Foo4 { -//│ static { -//│ Foo5 = this -//│ } -//│ constructor() { -//│ Predef.print("hi"); -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Foo"]; -//│ }); -//│ tmp = Predef.print("ok"); -//│ return Foo5 -//│ } +//│ test = function test() { +//│ let Foo5, tmp; /** scoped **/ +//│ globalThis.Object.freeze(class Foo4 { +//│ static { +//│ Foo5 = this +//│ } +//│ constructor() { +//│ Predef.print("hi"); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Foo"]; +//│ }); +//│ tmp = Predef.print("ok"); +//│ return Foo5 //│ }; let t = test() @@ -101,8 +98,8 @@ let t = test() :e new t //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.102: new t -//│ ║ ^ +//│ ║ l.99: new t +//│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): //│ globalThis.Object.freeze(new t()) @@ -118,7 +115,7 @@ new! t :e new t() //│ ╔══[ERROR] Expected a statically known class; found reference. -//│ ║ l.119: new t() +//│ ║ l.116: new t() //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): @@ -206,11 +203,10 @@ class Foo with //│ foo(y) { //│ return this.x + y //│ } -//│ bar(z) {{ -//│ let tmp; /** scoped **/ -//│ tmp = this.foo(z); -//│ return tmp + 1 -//│ } +//│ bar(z) { +//│ let tmp; /** scoped **/ +//│ tmp = this.foo(z); +//│ return tmp + 1 //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Foo"]; @@ -241,10 +237,10 @@ class Foo with val x = 2 //│ ╔══[ERROR] Multiple definitions of symbol 'x' //│ ╟── defined here -//│ ║ l.240: val x = 1 +//│ ║ l.236: val x = 1 //│ ║ ^^^^^^^^^ //│ ╟── defined here -//│ ║ l.241: val x = 2 +//│ ║ l.237: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo12; /** scoped **/ @@ -265,10 +261,10 @@ class Foo with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.266: let x = 2 +//│ ║ l.262: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.265: val x = 1 +//│ ║ l.261: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo14; /** scoped **/ @@ -295,7 +291,7 @@ class Foo with :e class Foo with val x = 1 //│ ╔══[ERROR] Illegal body of class definition (should be a block; found term definition). -//│ ║ l.296: class Foo with val x = 1 +//│ ║ l.292: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): //│ let Foo16; /** scoped **/ diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index d85915ee33..a252994119 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -5,11 +5,10 @@ fun foo() = if false do foo() //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo() {{ -//│ let scrut; /** scoped **/ -//│ scrut = false; -//│ if (scrut === true) { return foo() } else { return runtime.Unit } -//│ } +//│ foo = function foo() { +//│ let scrut; /** scoped **/ +//│ scrut = false; +//│ if (scrut === true) { return foo() } else { return runtime.Unit } //│ }; :sjs @@ -56,11 +55,10 @@ fun foo() = bar() //│ JS (unsanitized): //│ let foo2; /** scoped **/ -//│ foo2 = function foo() {{ -//│ let bar; /** scoped **/ -//│ bar = function bar() { return bar() }; -//│ return bar() -//│ } +//│ foo2 = function foo() { +//│ let bar; /** scoped **/ +//│ bar = function bar() { return bar() }; +//│ return bar() //│ }; @@ -72,7 +70,7 @@ do //│ JS (unsanitized): //│ let f, f1; /** scoped **/ //│ f1 = 1; -//│ f = function f() {{ let tmp; /** scoped **/ tmp = f(); return tmp } }; +//│ f = function f() { let tmp; /** scoped **/ tmp = f(); return tmp }; //│ runtime.Unit //│ f = 1 @@ -81,10 +79,10 @@ do let foo = 1 fun foo(x) = foo //│ ╔══[ERROR] Name 'foo' is already used -//│ ║ l.81: let foo = 1 +//│ ║ l.79: let foo = 1 //│ ║ ^^^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.82: fun foo(x) = foo +//│ ║ l.80: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): //│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 5c9aba15e5..2cfe39c612 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -52,11 +52,10 @@ id(f)(3)(4) //│ return (...args1) => {{ //│ runtime.checkArgs("", 2, true, args1.length); //│ let y = args1[0]; -//│ let z = args1[1];{ -//│ let tmp4; /** scoped **/ -//│ tmp4 = x + y; -//│ return tmp4 + z -//│ } +//│ let z = args1[1]; +//│ let tmp4; /** scoped **/ +//│ tmp4 = x + y; +//│ return tmp4 + z //│ } //│ } //│ } @@ -113,12 +112,11 @@ id(Cls(1, 2)).f(3) //│ this.x = x; //│ this.y = y; //│ } -//│ f(z, p) {{ -//│ let tmp6, tmp7; /** scoped **/ -//│ tmp6 = this.x + this.y; -//│ tmp7 = tmp6 + z; -//│ return tmp7 + p -//│ } +//│ f(z, p) { +//│ let tmp6, tmp7; /** scoped **/ +//│ tmp6 = this.x + this.y; +//│ tmp7 = tmp6 + z; +//│ return tmp7 + p //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; @@ -150,12 +148,11 @@ id(Cls(1, 2)).f(3) //│ f(...args) {{ //│ runtime.checkArgs("f", 2, true, args.length); //│ let z = args[0]; -//│ let p = args[1];{ -//│ let tmp8, tmp9; /** scoped **/ -//│ tmp8 = this.x + this.y; -//│ tmp9 = tmp8 + z; -//│ return tmp9 + p -//│ } +//│ let p = args[1]; +//│ let tmp8, tmp9; /** scoped **/ +//│ tmp8 = this.x + this.y; +//│ tmp9 = tmp8 + z; +//│ return tmp9 + p //│ } //│ } //│ toString() { return runtime.render(this); } @@ -193,14 +190,13 @@ id(Cls(1, 2)).f(3, 4)(5) //│ return (...args1) => {{ //│ runtime.checkArgs("", 2, true, args1.length); //│ let q = args1[0]; -//│ let s = args1[1];{ -//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ -//│ tmp11 = this.x + this.y; -//│ tmp12 = tmp11 + z; -//│ tmp13 = tmp12 + p; -//│ tmp14 = tmp13 + q; -//│ return tmp14 + s -//│ } +//│ let s = args1[1]; +//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ +//│ tmp11 = this.x + this.y; +//│ tmp12 = tmp11 + z; +//│ tmp13 = tmp12 + p; +//│ tmp14 = tmp13 + q; +//│ return tmp14 + s //│ } //│ } //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 09ecbcae09..6c1ed8a013 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -20,11 +20,10 @@ fun f() = b + 9 //│ JS (unsanitized): //│ let f; /** scoped **/ -//│ f = function f() {{ -//│ let scrut, a, b; /** scoped **/ -//│ scrut = true; -//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } -//│ } +//│ f = function f() { +//│ let scrut, a, b; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -43,34 +42,33 @@ fun g(x, y, z) = //│ JS (unsanitized): //│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { -//│ let lambda;{ -//│ let a, tmp1, tmp2; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (x1 === true) { +//│ let lambda; +//│ let a, tmp1, tmp2; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (x1 === true) { +//│ break split_1$ +//│ } else { +//│ if (y1 === true) { //│ break split_1$ //│ } else { -//│ if (y1 === true) { -//│ break split_1$ +//│ if (z === true) { +//│ a = 1; +//│ lambda = (undefined, function (_0) { +//│ return _0 + a +//│ }); +//│ tmp1 = lambda; +//│ break split_root$ //│ } else { -//│ if (z === true) { -//│ a = 1; -//│ lambda = (undefined, function (_0) { -//│ return _0 + a -//│ }); -//│ tmp1 = lambda; -//│ break split_root$ -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ } //│ } -//│ tmp1 = 0; //│ } -//│ tmp2 = 4 * tmp1; -//│ return 3 + tmp2 +//│ tmp1 = 0; //│ } +//│ tmp2 = 4 * tmp1; +//│ return 3 + tmp2 //│ }; @@ -87,37 +85,36 @@ fun f() = if x is B(c, d) then false //│ JS (unsanitized): //│ let f1; /** scoped **/ -//│ f1 = function f() {{ -//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (x instanceof A.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ b = argument1$; -//│ a = argument0$; -//│ scrut = a > b; -//│ if (scrut === true) { -//│ tmp1 = true; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } -//│ } else if (x instanceof B.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ d = argument1$; -//│ c = argument0$; -//│ tmp1 = false; +//│ f1 = function f() { +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (x instanceof A.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ b = argument1$; +//│ a = argument0$; +//│ scrut = a > b; +//│ if (scrut === true) { +//│ tmp1 = true; //│ break split_root$ //│ } else { //│ break split_default$ //│ } +//│ } else if (x instanceof B.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ d = argument1$; +//│ c = argument0$; +//│ tmp1 = false; +//│ break split_root$ +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp1 +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp1 //│ }; @@ -126,32 +123,31 @@ fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): //│ let f2; /** scoped **/ -//│ f2 = function f() {{ -//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ scrut = A(1, Nil); -//│ if (scrut instanceof A.class) { -//│ argument0$ = scrut.a; -//│ argument1$ = scrut.b; -//│ if (argument0$ === 1) { -//│ if (argument1$ instanceof Nil.class) { -//│ tmp1 = 3; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } +//│ f2 = function f() { +//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ scrut = A(1, Nil); +//│ if (scrut instanceof A.class) { +//│ argument0$ = scrut.a; +//│ argument1$ = scrut.b; +//│ if (argument0$ === 1) { +//│ if (argument1$ instanceof Nil.class) { +//│ tmp1 = 3; +//│ break split_root$ //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp1 +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp1 //│ }; @@ -166,33 +162,31 @@ fun f(x) = x //│ JS (unsanitized): //│ let f3; /** scoped **/ -//│ f3 = function f(x1) {{ -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = undefined; -//│ while1 = (undefined, function () {{ -//│ let a, a1, argument0$, argument1$; /** scoped **/ -//│ if (x1 instanceof A.class) { -//│ argument0$ = x1.a; -//│ argument1$ = x1.b; -//│ a = argument0$; -//│ tmp1 = runtime.safeCall(a()); -//│ return while1() -//│ } else if (x1 instanceof B.class) { -//│ argument0$ = x1.a; -//│ argument1$ = x1.b; -//│ a1 = argument0$; -//│ tmp1 = runtime.safeCall(a1()); -//│ return while1() -//│ } else { -//│ tmp1 = runtime.safeCall(x1()); -//│ } -//│ return runtime.LoopEnd -//│ } -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { x1 = tmp1; return x1 } -//│ } +//│ f3 = function f(x1) { +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ tmp1 = undefined; +//│ while1 = (undefined, function () { +//│ let a, a1, argument0$, argument1$; /** scoped **/ +//│ if (x1 instanceof A.class) { +//│ argument0$ = x1.a; +//│ argument1$ = x1.b; +//│ a = argument0$; +//│ tmp1 = runtime.safeCall(a()); +//│ return while1() +//│ } else if (x1 instanceof B.class) { +//│ argument0$ = x1.a; +//│ argument1$ = x1.b; +//│ a1 = argument0$; +//│ tmp1 = runtime.safeCall(a1()); +//│ return while1() +//│ } else { +//│ tmp1 = runtime.safeCall(x1()); +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { x1 = tmp1; return x1 } //│ }; @@ -208,47 +202,45 @@ fun f() = y = a //│ JS (unsanitized): //│ let f4; /** scoped **/ -//│ f4 = function f() {{ -//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ y1 = x + 1; -//│ tmp1 = undefined; -//│ while1 = (undefined, function () {{ -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (y1 instanceof A.class) { -//│ argument0$ = y1.a; -//│ argument1$ = y1.b; -//│ a = argument1$; -//│ z = argument0$; -//│ scrut = false; -//│ if (scrut === true) { -//│ scrut1 = true; -//│ if (scrut1 === true) { -//│ tmp4 = z + 1; -//│ y1 = tmp4; -//│ tmp1 = runtime.Unit; -//│ return while1() -//│ } else { -//│ break split_1$ -//│ } -//│ } else { -//│ break split_1$ -//│ } -//│ } else { +//│ f4 = function f() { +//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ y1 = x + 1; +//│ tmp1 = undefined; +//│ while1 = (undefined, function () { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (y1 instanceof A.class) { +//│ argument0$ = y1.a; +//│ argument1$ = y1.b; +//│ a = argument1$; +//│ z = argument0$; +//│ scrut = false; +//│ if (scrut === true) { +//│ scrut1 = true; +//│ if (scrut1 === true) { +//│ tmp4 = z + 1; +//│ y1 = tmp4; //│ tmp1 = runtime.Unit; +//│ return while1() +//│ } else { +//│ break split_1$ //│ } +//│ } else { +//│ break split_1$ //│ } -//│ y1 = a; +//│ } else { //│ tmp1 = runtime.Unit; //│ } -//│ return runtime.LoopEnd //│ } -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } -//│ } +//│ y1 = a; +//│ tmp1 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } //│ }; @@ -258,29 +250,27 @@ fun f(x, y) = f(0, 0) + 4 //│ JS (unsanitized): //│ let f5; /** scoped **/ -//│ f5 = function f(x1, y1) {{ -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = undefined; -//│ while1 = (undefined, function () {{ -//│ let scrut, lambda, tmp4; /** scoped **/ -//│ lambda = (undefined, function () { -//│ return y1 -//│ }); -//│ scrut = runtime.short_and(x1, lambda); -//│ if (scrut === true) { -//│ tmp4 = f5(0, 0); -//│ tmp1 = tmp4 + 4; -//│ return while1() -//│ } else { -//│ tmp1 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ } +//│ f5 = function f(x1, y1) { +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ tmp1 = undefined; +//│ while1 = (undefined, function () { +//│ let scrut, lambda, tmp4; /** scoped **/ +//│ lambda = (undefined, function () { +//│ return y1 //│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } -//│ } +//│ scrut = runtime.short_and(x1, lambda); +//│ if (scrut === true) { +//│ tmp4 = f5(0, 0); +//│ tmp1 = tmp4 + 4; +//│ return while1() +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp2 = while1(); +//│ tmp3 = tmp2 !== runtime.LoopEnd; +//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 4d2bef3a02..0eed770a5e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -71,29 +71,28 @@ example() //│ JS (unsanitized): //│ let example2; /** scoped **/ //│ example2 = function example() { -//│ let get_x;{ -//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ -//│ x2 = 0; -//│ get_x = function get_x() { -//│ return x2 -//│ }; -//│ get_x1 = get_x; -//│ old1 = x2; -//│ try { -//│ tmp5 = x2 + 1; -//│ x2 = tmp5; -//│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x1()); -//│ tmp8 = Predef.print(tmp7); -//│ tmp9 = (tmp6 , tmp8); -//│ tmp4 = tmp9; -//│ } finally { -//│ x2 = old1; -//│ } -//│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x1()); -//│ return Predef.print(tmp11) +//│ let get_x; +//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ x2 = 0; +//│ get_x = function get_x() { +//│ return x2 +//│ }; +//│ get_x1 = get_x; +//│ old1 = x2; +//│ try { +//│ tmp5 = x2 + 1; +//│ x2 = tmp5; +//│ tmp6 = Predef.print(x2); +//│ tmp7 = runtime.safeCall(get_x1()); +//│ tmp8 = Predef.print(tmp7); +//│ tmp9 = (tmp6 , tmp8); +//│ tmp4 = tmp9; +//│ } finally { +//│ x2 = old1; //│ } +//│ tmp10 = Predef.print(x2); +//│ tmp11 = runtime.safeCall(get_x1()); +//│ return Predef.print(tmp11) //│ }; //│ example2() //│ > 1 @@ -114,29 +113,28 @@ example() //│ JS (unsanitized): //│ let example3; /** scoped **/ //│ example3 = function example() { -//│ let get_x;{ -//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ -//│ x2 = 0; -//│ get_x = function get_x() { -//│ return x2 -//│ }; -//│ get_x1 = get_x; -//│ old1 = x2; -//│ try { -//│ tmp5 = x2 + 1; -//│ x2 = tmp5; -//│ tmp6 = Predef.print(x2); -//│ tmp7 = (tmp6 , x2); -//│ tmp4 = tmp7; -//│ } finally { -//│ x2 = old1; -//│ } -//│ y = tmp4; -//│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x1()); -//│ tmp10 = Predef.print(tmp9); -//│ return y +//│ let get_x; +//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ +//│ x2 = 0; +//│ get_x = function get_x() { +//│ return x2 +//│ }; +//│ get_x1 = get_x; +//│ old1 = x2; +//│ try { +//│ tmp5 = x2 + 1; +//│ x2 = tmp5; +//│ tmp6 = Predef.print(x2); +//│ tmp7 = (tmp6 , x2); +//│ tmp4 = tmp7; +//│ } finally { +//│ x2 = old1; //│ } +//│ y = tmp4; +//│ tmp8 = Predef.print(x2); +//│ tmp9 = runtime.safeCall(get_x1()); +//│ tmp10 = Predef.print(tmp9); +//│ return y //│ }; //│ example3() //│ > 1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 760c5f6b53..fa1b38cc34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -32,7 +32,7 @@ fun f(x) = f(1) //│ JS (unsanitized): //│ let f2; /** scoped **/ -//│ f2 = function f(x) {{ let y; /** scoped **/ throw globalThis.Error("e") } }; +//│ f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; //│ f2(1) //│ ═══[RUNTIME ERROR] Error: e diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 09873ebd4a..993741a4b1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -8,13 +8,12 @@ fun fib(a) = if else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): //│ let fib; /** scoped **/ -//│ fib = function fib(a) {{ -//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ scrut = a <= 1; -//│ if (scrut === true) { -//│ return a -//│ } else { tmp = a - 1; tmp1 = fib(tmp); tmp2 = a - 2; tmp3 = fib(tmp2); return tmp1 + tmp3 } -//│ } +//│ fib = function fib(a) { +//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ scrut = a <= 1; +//│ if (scrut === true) { +//│ return a +//│ } else { tmp = a - 1; tmp1 = fib(tmp); tmp2 = a - 2; tmp3 = fib(tmp2); return tmp1 + tmp3 } //│ }; fun f(x) = g(x) diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 19c7ede879..eb618c68fb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -5,25 +5,23 @@ () => (while true then 0) //│ JS (unsanitized): //│ let lambda; -//│ lambda = (undefined, function () {{ -//│ let tmp, while1, tmp1, tmp2; /** scoped **/ -//│ tmp = undefined; -//│ while1 = (undefined, function () {{ -//│ let scrut; /** scoped **/ -//│ scrut = true; -//│ if (scrut === true) { -//│ tmp = 0; -//│ return while1() -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ return runtime.LoopEnd -//│ } -//│ }); -//│ tmp1 = while1(); -//│ tmp2 = tmp1 !== runtime.LoopEnd; -//│ if (tmp2 === true) { return tmp1 } else { return tmp } -//│ } +//│ lambda = (undefined, function () { +//│ let tmp, while1, tmp1, tmp2; /** scoped **/ +//│ tmp = undefined; +//│ while1 = (undefined, function () { +//│ let scrut; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { +//│ tmp = 0; +//│ return while1() +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp1 = while1(); +//│ tmp2 = tmp1 !== runtime.LoopEnd; +//│ if (tmp2 === true) { return tmp1 } else { return tmp } //│ }); //│ lambda //│ = fun @@ -61,16 +59,15 @@ while x //│ JS (unsanitized): //│ let tmp6, while3, tmp7, tmp8; /** scoped **/ //│ tmp6 = undefined; -//│ while3 = (undefined, function () {{ -//│ let tmp9; /** scoped **/ -//│ if (x2 === true) { -//│ tmp9 = Predef.print("Hello World"); -//│ x2 = false; -//│ tmp6 = runtime.Unit; -//│ return while3() -//│ } else { tmp6 = 42; } -//│ return runtime.LoopEnd -//│ } +//│ while3 = (undefined, function () { +//│ let tmp9; /** scoped **/ +//│ if (x2 === true) { +//│ tmp9 = Predef.print("Hello World"); +//│ x2 = false; +//│ tmp6 = runtime.Unit; +//│ return while3() +//│ } else { tmp6 = 42; } +//│ return runtime.LoopEnd //│ }); //│ tmp7 = while3(); //│ tmp6 @@ -114,28 +111,26 @@ while i < 10 do set i += 1 //│ JS (unsanitized): //│ let lambda2; -//│ lambda2 = (undefined, function () {{ -//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ -//│ tmp18 = undefined; -//│ while7 = (undefined, function () {{ -//│ let i2, scrut, tmp21; /** scoped **/ -//│ i2 = 0; -//│ scrut = i2 < 10; -//│ if (scrut === true) { -//│ tmp21 = i2 + 1; -//│ i2 = tmp21; -//│ tmp18 = runtime.Unit; -//│ return while7() -//│ } else { -//│ tmp18 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ } -//│ }); -//│ tmp19 = while7(); -//│ tmp20 = tmp19 !== runtime.LoopEnd; -//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } -//│ } +//│ lambda2 = (undefined, function () { +//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ +//│ tmp18 = undefined; +//│ while7 = (undefined, function () { +//│ let i2, scrut, tmp21; /** scoped **/ +//│ i2 = 0; +//│ scrut = i2 < 10; +//│ if (scrut === true) { +//│ tmp21 = i2 + 1; +//│ i2 = tmp21; +//│ tmp18 = runtime.Unit; +//│ return while7() +//│ } else { +//│ tmp18 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp19 = while7(); +//│ tmp20 = tmp19 !== runtime.LoopEnd; +//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } //│ }); //│ lambda2 //│ = fun @@ -214,29 +209,27 @@ fun f(ls) = else print("Done!") //│ JS (unsanitized): //│ let f; /** scoped **/ -//│ f = function f(ls) {{ -//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ -//│ tmp27 = undefined; -//│ while10 = (undefined, function () {{ -//│ let tl, h, argument0$, argument1$; /** scoped **/ -//│ if (ls instanceof Cons.class) { -//│ argument0$ = ls.hd; -//│ argument1$ = ls.tl; -//│ tl = argument1$; -//│ h = argument0$; -//│ ls = tl; -//│ tmp27 = Predef.print(h); -//│ return while10() -//│ } else { -//│ tmp27 = Predef.print("Done!"); -//│ } -//│ return runtime.LoopEnd -//│ } -//│ }); -//│ tmp28 = while10(); -//│ tmp29 = tmp28 !== runtime.LoopEnd; -//│ if (tmp29 === true) { return tmp28 } else { return tmp27 } -//│ } +//│ f = function f(ls) { +//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ +//│ tmp27 = undefined; +//│ while10 = (undefined, function () { +//│ let tl, h, argument0$, argument1$; /** scoped **/ +//│ if (ls instanceof Cons.class) { +//│ argument0$ = ls.hd; +//│ argument1$ = ls.tl; +//│ tl = argument1$; +//│ h = argument0$; +//│ ls = tl; +//│ tmp27 = Predef.print(h); +//│ return while10() +//│ } else { +//│ tmp27 = Predef.print("Done!"); +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp28 = while10(); +//│ tmp29 = tmp28 !== runtime.LoopEnd; +//│ if (tmp29 === true) { return tmp28 } else { return tmp27 } //│ }; f(0) @@ -304,10 +297,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.304: then 0(0) +//│ ║ l.297: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.303: while print("Hello World"); false +//│ ║ l.296: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -317,12 +310,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.316: while { print("Hello World"), false } +//│ ║ l.309: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.317: then 0(0) +//│ ║ l.310: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.318: else 1 +//│ ║ l.311: else 1 //│ ╙── ^^^^ :fixme @@ -332,14 +325,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.330: print("Hello World") +//│ ║ l.323: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.331: false +//│ ║ l.324: false //│ ║ ^^^^^^^^^ -//│ ║ l.332: then 0(0) +//│ ║ l.325: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.333: else 1 +//│ ║ l.326: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index be5a583ee1..23ed0cba02 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -33,11 +33,10 @@ let f1 = foo //│ JS (unsanitized): //│ let f1; //│ let f11; /** scoped **/ -//│ f1 = function f1() {{ -//│ let tmp; /** scoped **/ -//│ tmp = foo(); -//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) -//│ } +//│ f1 = function f1() { +//│ let tmp; /** scoped **/ +//│ tmp = foo(); +//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; //│ f11 = f1; //│ f1 = fun f1 @@ -56,11 +55,10 @@ let f2 = bar //│ JS (unsanitized): //│ let f2; //│ let f21; /** scoped **/ -//│ f2 = function f2(x) {{ -//│ let tmp; /** scoped **/ -//│ tmp = bar(x); -//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) -//│ } +//│ f2 = function f2(x) { +//│ let tmp; /** scoped **/ +//│ tmp = bar(x); +//│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; //│ f21 = f2; //│ f2 = fun f2 @@ -81,13 +79,12 @@ let f3 = baz //│ let f31; /** scoped **/ //│ f3 = function f3(x) { //│ let lambda1; -//│ lambda1 = (undefined, function (y) {{ -//│ let tmp, tmp1, tmp2; /** scoped **/ -//│ tmp = baz(x); -//│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); -//│ tmp2 = runtime.safeCall(tmp1(y)); -//│ return runtime.safeCall(tmp2(instance$Ident$_Num$_)) -//│ } +//│ lambda1 = (undefined, function (y) { +//│ let tmp, tmp1, tmp2; /** scoped **/ +//│ tmp = baz(x); +//│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); +//│ tmp2 = runtime.safeCall(tmp1(y)); +//│ return runtime.safeCall(tmp2(instance$Ident$_Num$_)) //│ }); //│ return lambda1 //│ }; @@ -133,11 +130,10 @@ module Test with //│ } //│ static main(tmp3) { //│ let lambda1; -//│ lambda1 = (undefined, function (j) {{ -//│ let tmp4; /** scoped **/ -//│ tmp4 = Test.test(j); -//│ return runtime.safeCall(tmp4(tmp3)) -//│ } +//│ lambda1 = (undefined, function (j) { +//│ let tmp4; /** scoped **/ +//│ tmp4 = Test.test(j); +//│ return runtime.safeCall(tmp4(tmp3)) //│ }); //│ return lambda1 //│ } diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index d7b6079082..4cd471afe4 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -28,92 +28,91 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let getLocals3, Cont$func$f$1, doUnwind;{ -//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ -//│ getLocals3 = function getLocals() { -//│ let i1, j1, k1, prev, thisInfo, arr, tmp2; -//│ prev = getLocals2(); -//│ i1 = new runtime.LocalVarInfo.class("i", i); -//│ j1 = new runtime.LocalVarInfo.class("j", j); -//│ k1 = new runtime.LocalVarInfo.class("k", k); -//│ arr = globalThis.Object.freeze([ -//│ i1, -//│ j1, -//│ k1 -//│ ]); -//│ thisInfo = new runtime.FnLocalsInfo.class("f", arr); -//│ tmp2 = runtime.safeCall(prev.push(thisInfo)); -//│ return prev -//│ }; -//│ globalThis.Object.freeze(class Cont$func$f$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$f$1 = this +//│ let getLocals3, Cont$func$f$1, doUnwind; +//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ +//│ getLocals3 = function getLocals() { +//│ let i1, j1, k1, prev, thisInfo, arr, tmp2; +//│ prev = getLocals2(); +//│ i1 = new runtime.LocalVarInfo.class("i", i); +//│ j1 = new runtime.LocalVarInfo.class("j", j); +//│ k1 = new runtime.LocalVarInfo.class("k", k); +//│ arr = globalThis.Object.freeze([ +//│ i1, +//│ j1, +//│ k1 +//│ ]); +//│ thisInfo = new runtime.FnLocalsInfo.class("f", arr); +//│ tmp2 = runtime.safeCall(prev.push(thisInfo)); +//│ return prev +//│ }; +//│ globalThis.Object.freeze(class Cont$func$f$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$f$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp2; +//│ tmp2 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 1) { +//│ tmp = value$; +//│ } else if (this.pc === 2) { +//│ tmp1 = value$; //│ } -//│ constructor(pc) { -//│ let tmp2; -//│ tmp2 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 1) { -//│ tmp = value$; -//│ } else if (this.pc === 2) { -//│ tmp1 = value$; -//│ } -//│ contLoop: while (true) { -//│ if (this.pc === 3) { -//│ return j / i -//│ } else if (this.pc === 4) { -//│ tmp1 = Predef.print(tmp); -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return this.doUnwind(tmp1, 2) -//│ } -//│ this.pc = 2; -//│ continue contLoop -//│ } else if (this.pc === 1) { -//│ this.pc = 4; -//│ continue contLoop -//│ } else if (this.pc === 2) { -//│ this.pc = 3; -//│ continue contLoop +//│ contLoop: while (true) { +//│ if (this.pc === 3) { +//│ return j / i +//│ } else if (this.pc === 4) { +//│ tmp1 = Predef.print(tmp); +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return this.doUnwind(tmp1, 2) //│ } -//│ break; -//│ } -//│ } -//│ get getLocals() { -//│ return getLocals3(); -//│ } -//│ get getLoc() { -//│ if (this.pc === 1) { -//│ return "Debugging.mls:17:14" +//│ this.pc = 2; +//│ continue contLoop +//│ } else if (this.pc === 1) { +//│ this.pc = 4; +//│ continue contLoop //│ } else if (this.pc === 2) { -//│ return "Debugging.mls:17:5" +//│ this.pc = 3; +//│ continue contLoop //│ } +//│ break; //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$f$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$func$f$1(pc); -//│ res1.contTrace.last = res1.contTrace.last.next; -//│ return res1 -//│ }; -//│ i = 0; -//│ j = 100; -//│ k = 2000; -//│ scrut = i == 0; -//│ if (scrut === true) { -//│ tmp = Predef.raiseUnhandledEffect(); -//│ if (tmp instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp, 1) -//│ } -//│ tmp1 = Predef.print(tmp); -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp1, 2) +//│ } +//│ get getLocals() { +//│ return getLocals3(); +//│ } +//│ get getLoc() { +//│ if (this.pc === 1) { +//│ return "Debugging.mls:17:14" +//│ } else if (this.pc === 2) { +//│ return "Debugging.mls:17:5" //│ } -//│ } else { tmp1 = runtime.Unit; } -//│ return j / i -//│ } +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$f$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$func$f$1(pc); +//│ res1.contTrace.last = res1.contTrace.last.next; +//│ return res1 +//│ }; +//│ i = 0; +//│ j = 100; +//│ k = 2000; +//│ scrut = i == 0; +//│ if (scrut === true) { +//│ tmp = Predef.raiseUnhandledEffect(); +//│ if (tmp instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp, 1) +//│ } +//│ tmp1 = Predef.print(tmp); +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp1, 2) +//│ } +//│ } else { tmp1 = runtime.Unit; } +//│ return j / i //│ }; :re @@ -129,8 +128,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:129:3) -//│ at lambda_test (Debugging.mls:126:3) +//│ at lambda (Debugging.mls:128:3) +//│ at lambda_test (Debugging.mls:125:3) import "../../mlscript-compile/Runtime.mls" @@ -209,9 +208,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:202:3) with locals: j=200 +//│ > at f (Debugging.mls:201:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:204:3) +//│ > at f (Debugging.mls:203:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:205:3) with locals: j=300 +//│ > at f (Debugging.mls:204:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 5c1f2971d8..4b3fa31cce 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -159,67 +159,66 @@ if true do //│ let handleBlock$11; //│ let tmp20; /** scoped **/ //│ handleBlock$11 = function handleBlock$() { -//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12;{ -//│ let f, scrut; /** scoped **/ -//│ globalThis.Object.freeze(class Handler$h$11 extends Effect { -//│ static { -//│ Handler$h$12 = this -//│ } -//│ constructor() { -//│ let tmp21; -//│ tmp21 = super(); -//│ } -//│ perform(arg) { -//│ let hdlrFun; -//│ hdlrFun = function hdlrFun(k) { -//│ return arg -//│ }; -//│ return runtime.mkEffect(this, hdlrFun) -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Handler$h$"]; -//│ }); -//│ h = new Handler$h$12(); -//│ globalThis.Object.freeze(class Cont$handleBlock$h$10 extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handleBlock$h$11 = this -//│ } -//│ constructor(pc) { -//│ let tmp21; -//│ tmp21 = super(null); -//│ this.pc = pc; +//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; +//│ let f, scrut; /** scoped **/ +//│ globalThis.Object.freeze(class Handler$h$11 extends Effect { +//│ static { +//│ Handler$h$12 = this +//│ } +//│ constructor() { +//│ let tmp21; +//│ tmp21 = super(); +//│ } +//│ perform(arg) { +//│ let hdlrFun; +//│ hdlrFun = function hdlrFun(k) { +//│ return arg +//│ }; +//│ return runtime.mkEffect(this, hdlrFun) +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Handler$h$"]; +//│ }); +//│ h = new Handler$h$12(); +//│ globalThis.Object.freeze(class Cont$handleBlock$h$10 extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handleBlock$h$11 = this +//│ } +//│ constructor(pc) { +//│ let tmp21; +//│ tmp21 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 1) { +//│ res = value$; //│ } -//│ resume(value$) { +//│ contLoop: while (true) { //│ if (this.pc === 1) { -//│ res = value$; +//│ return res //│ } -//│ contLoop: while (true) { -//│ if (this.pc === 1) { -//│ return res -//│ } -//│ break; -//│ } -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handleBlock$h$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$handleBlock$h$11(pc); -//│ return runtime.handleBlockImpl(res1, h) -//│ }; -//│ f = function f() { -//│ return 3 -//│ }; -//│ scrut = true; -//│ if (scrut === true) { -//│ res = f(); -//│ if (res instanceof runtime.EffectSig.class) { -//│ return doUnwind(res, 1) +//│ break; //│ } -//│ return res -//│ } else { -//│ return runtime.Unit //│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handleBlock$h$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$handleBlock$h$11(pc); +//│ return runtime.handleBlockImpl(res1, h) +//│ }; +//│ f = function f() { +//│ return 3 +//│ }; +//│ scrut = true; +//│ if (scrut === true) { +//│ res = f(); +//│ if (res instanceof runtime.EffectSig.class) { +//│ return doUnwind(res, 1) +//│ } +//│ return res +//│ } else { +//│ return runtime.Unit //│ } //│ }; //│ tmp20 = handleBlock$11(); diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index 2f00f0a268..3b0ece8c6e 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -14,31 +14,30 @@ fun foo(h): module M = A //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo(h) {{ -//│ let A2, A3, scrut; /** scoped **/ -//│ globalThis.Object.freeze(class A { -//│ static { -//│ A2 = this -//│ } -//│ constructor() { -//│ runtime.Unit; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A"]; -//│ }); -//│ globalThis.Object.freeze(class A1 { -//│ static { -//│ A3 = this -//│ } -//│ constructor() { -//│ runtime.Unit; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A"]; -//│ }); -//│ scrut = false; -//│ if (scrut === true) { return A2 } else { return A3 } -//│ } +//│ foo = function foo(h) { +//│ let A2, A3, scrut; /** scoped **/ +//│ globalThis.Object.freeze(class A { +//│ static { +//│ A2 = this +//│ } +//│ constructor() { +//│ runtime.Unit; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); +//│ globalThis.Object.freeze(class A1 { +//│ static { +//│ A3 = this +//│ } +//│ constructor() { +//│ runtime.Unit; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); +//│ scrut = false; +//│ if (scrut === true) { return A2 } else { return A3 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index cc4a69f256..be8ffedbb6 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -173,48 +173,47 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let Cont$handler$h1$perform$2, doUnwind1;{ -//│ let tmp28, tmp29, tmp30; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handler$h1$perform$2 = this -//│ } -//│ constructor(pc) { -//│ let tmp31; -//│ tmp31 = super(null); -//│ this.pc = pc; +//│ let Cont$handler$h1$perform$2, doUnwind1; +//│ let tmp28, tmp29, tmp30; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handler$h1$perform$2 = this +//│ } +//│ constructor(pc) { +//│ let tmp31; +//│ tmp31 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 7) { +//│ tmp29 = value$; //│ } -//│ resume(value$) { +//│ contLoop: while (true) { //│ if (this.pc === 7) { -//│ tmp29 = value$; -//│ } -//│ contLoop: while (true) { -//│ if (this.pc === 7) { -//│ tmp30 = str + "A"; -//│ str = tmp30; -//│ return runtime.Unit -//│ } -//│ break; +//│ tmp30 = str + "A"; +//│ str = tmp30; +//│ return runtime.Unit //│ } +//│ break; //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handler$h1$perform$"]; -//│ }); -//│ doUnwind1 = function doUnwind(res7, pc) { -//│ res7.contTrace.last.next = new Cont$handler$h1$perform$2(pc); -//│ res7.contTrace.last = res7.contTrace.last.next; -//│ return res7 -//│ }; -//│ tmp28 = str + "A"; -//│ str = tmp28; -//│ tmp29 = runtime.safeCall(k(arg)); -//│ if (tmp29 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp29, 7) //│ } -//│ tmp30 = str + "A"; -//│ str = tmp30; -//│ return runtime.Unit +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handler$h1$perform$"]; +//│ }); +//│ doUnwind1 = function doUnwind(res7, pc) { +//│ res7.contTrace.last.next = new Cont$handler$h1$perform$2(pc); +//│ res7.contTrace.last = res7.contTrace.last.next; +//│ return res7 +//│ }; +//│ tmp28 = str + "A"; +//│ str = tmp28; +//│ tmp29 = runtime.safeCall(k(arg)); +//│ if (tmp29 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp29, 7) //│ } +//│ tmp30 = str + "A"; +//│ str = tmp30; +//│ return runtime.Unit //│ }; //│ return runtime.mkEffect(this, hdlrFun) //│ } @@ -262,51 +261,50 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let Cont$handler$h2$perform$2, doUnwind2;{ -//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handler$h2$perform$2 = this -//│ } -//│ constructor(pc) { -//│ let tmp33; -//│ tmp33 = super(null); -//│ this.pc = pc; +//│ let Cont$handler$h2$perform$2, doUnwind2; +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handler$h2$perform$2 = this +//│ } +//│ constructor(pc) { +//│ let tmp33; +//│ tmp33 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 3) { +//│ tmp30 = value$; //│ } -//│ resume(value$) { +//│ contLoop: while (true) { //│ if (this.pc === 3) { -//│ tmp30 = value$; -//│ } -//│ contLoop: while (true) { -//│ if (this.pc === 3) { -//│ tmp31 = str + "B"; -//│ tmp32 = str + tmp31; -//│ str = tmp32; -//│ return runtime.Unit -//│ } -//│ break; +//│ tmp31 = str + "B"; +//│ tmp32 = str + tmp31; +//│ str = tmp32; +//│ return runtime.Unit //│ } +//│ break; //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handler$h2$perform$"]; -//│ }); -//│ doUnwind2 = function doUnwind(res8, pc) { -//│ res8.contTrace.last.next = new Cont$handler$h2$perform$2(pc); -//│ res8.contTrace.last = res8.contTrace.last.next; -//│ return res8 -//│ }; -//│ tmp28 = str + "B"; -//│ tmp29 = str + tmp28; -//│ str = tmp29; -//│ tmp30 = runtime.safeCall(k(arg)); -//│ if (tmp30 instanceof runtime.EffectSig.class) { -//│ return doUnwind2(tmp30, 3) //│ } -//│ tmp31 = str + "B"; -//│ tmp32 = str + tmp31; -//│ str = tmp32; -//│ return runtime.Unit +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handler$h2$perform$"]; +//│ }); +//│ doUnwind2 = function doUnwind(res8, pc) { +//│ res8.contTrace.last.next = new Cont$handler$h2$perform$2(pc); +//│ res8.contTrace.last = res8.contTrace.last.next; +//│ return res8 +//│ }; +//│ tmp28 = str + "B"; +//│ tmp29 = str + tmp28; +//│ str = tmp29; +//│ tmp30 = runtime.safeCall(k(arg)); +//│ if (tmp30 instanceof runtime.EffectSig.class) { +//│ return doUnwind2(tmp30, 3) //│ } +//│ tmp31 = str + "B"; +//│ tmp32 = str + tmp31; +//│ str = tmp32; +//│ return runtime.Unit //│ }; //│ return runtime.mkEffect(this, hdlrFun) //│ } diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index b506610f6d..688ae8e6d2 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -23,40 +23,39 @@ hi(0) //│ let res, $_stack$_safe$_body$_; //│ let hi; /** scoped **/ //│ hi = function hi(n) { -//│ let Cont$func$hi$1, doUnwind, stackDelayRes;{ -//│ let scrut, tmp; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$hi$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp1; -//│ tmp1 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ return hi(n) -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$hi$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$func$hi$1(pc); -//│ res1.contTrace.last = res1.contTrace.last.next; -//│ return res1 -//│ }; -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind(stackDelayRes, 0) +//│ let Cont$func$hi$1, doUnwind, stackDelayRes; +//│ let scrut, tmp; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$hi$1 = this //│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ return hi(tmp) +//│ constructor(pc) { +//│ let tmp1; +//│ tmp1 = super(null); +//│ this.pc = pc; //│ } +//│ resume(value$) { +//│ return hi(n) +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$hi$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$func$hi$1(pc); +//│ res1.contTrace.last = res1.contTrace.last.next; +//│ return res1 +//│ }; +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind(stackDelayRes, 0) +//│ } +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ return hi(tmp) //│ } //│ }; //│ $_stack$_safe$_body$_ = (undefined, function () { @@ -80,59 +79,58 @@ sum(10000) //│ let res1, $_stack$_safe$_body$_1; //│ let sum1; /** scoped **/ //│ sum1 = function sum(n) { -//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes;{ -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$sum$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp2; -//│ tmp2 = super(null); -//│ this.pc = pc; +//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$sum$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp2; +//│ tmp2 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ let curDepth1; +//│ curDepth1 = runtime.stackDepth; +//│ if (this.pc === 1) { +//│ tmp1 = value$; //│ } -//│ resume(value$) { -//│ let curDepth1; -//│ curDepth1 = runtime.stackDepth; -//│ if (this.pc === 1) { -//│ tmp1 = value$; -//│ } -//│ contLoop: while (true) { -//│ runtime.stackDepth = curDepth1; -//│ if (this.pc === 0) { -//│ return sum1(n) -//│ } else if (this.pc === 1) { -//│ return n + tmp1 -//│ } -//│ break; +//│ contLoop: while (true) { +//│ runtime.stackDepth = curDepth1; +//│ if (this.pc === 0) { +//│ return sum1(n) +//│ } else if (this.pc === 1) { +//│ return n + tmp1 //│ } +//│ break; //│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$sum$"]; -//│ }); -//│ doUnwind = function doUnwind(res2, pc) { -//│ res2.contTrace.last.next = new Cont$func$sum$1(pc); -//│ res2.contTrace.last = res2.contTrace.last.next; -//│ return res2 -//│ }; -//│ curDepth = runtime.stackDepth; -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind(stackDelayRes, 0) //│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ tmp1 = sum1(tmp); -//│ runtime.stackDepth = curDepth; -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp1, 1) -//│ } -//│ return n + tmp1 +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$sum$"]; +//│ }); +//│ doUnwind = function doUnwind(res2, pc) { +//│ res2.contTrace.last.next = new Cont$func$sum$1(pc); +//│ res2.contTrace.last = res2.contTrace.last.next; +//│ return res2 +//│ }; +//│ curDepth = runtime.stackDepth; +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind(stackDelayRes, 0) +//│ } +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ tmp1 = sum1(tmp); +//│ runtime.stackDepth = curDepth; +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp1, 1) //│ } +//│ return n + tmp1 //│ } //│ }; //│ $_stack$_safe$_body$_1 = (undefined, function () { @@ -263,11 +261,10 @@ foo(h) fun max(a, b) = if a < b then b else a //│ JS (unsanitized): //│ let max; /** scoped **/ -//│ max = function max(a, b) {{ -//│ let scrut; /** scoped **/ -//│ scrut = a < b; -//│ if (scrut === true) { return b } else { return a } -//│ } +//│ max = function max(a, b) { +//│ let scrut; /** scoped **/ +//│ scrut = a < b; +//│ if (scrut === true) { return b } else { return a } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 39bd2e8e55..21b5a1233e 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -130,13 +130,12 @@ f().foo() //│ set y(value) { this.#y = value; } //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } -//│ foo() {{ -//│ let tmp10, tmp11; /** scoped **/ -//│ this.z = 100; -//│ tmp10 = this.x + this.y; -//│ tmp11 = tmp10 + this.z; -//│ return tmp11 + this.f$capture.w$capture$0 -//│ } +//│ foo() { +//│ let tmp10, tmp11; /** scoped **/ +//│ this.z = 100; +//│ tmp10 = this.x + this.y; +//│ tmp11 = tmp10 + this.z; +//│ return tmp11 + this.f$capture.w$capture$0 //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Good", []]; @@ -187,17 +186,16 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let capture;{ -//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ -//│ capture = new f$capture3(null); -//│ x = 1; -//│ y = 10; -//│ z = 10; -//│ capture.w$capture$0 = 1000; -//│ tmp10 = Bad$(false, capture); -//│ tmp11 = tmp10.foo(); -//│ return Good$(false, x, y, z, capture) -//│ } +//│ let capture; +//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ +//│ capture = new f$capture3(null); +//│ x = 1; +//│ y = 10; +//│ z = 10; +//│ capture.w$capture$0 = 1000; +//│ tmp10 = Bad$(false, capture); +//│ tmp11 = tmp10.foo(); +//│ return Good$(false, x, y, z, capture) //│ }; //│ tmp9 = f5(); //│ runtime.safeCall(tmp9.foo()) diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index 99fb3ba51d..9be85d298a 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -16,21 +16,20 @@ fun f(x) = //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; -//│ f = function f(x) {{ -//│ let A1, g; /** scoped **/ -//│ globalThis.Object.freeze(class A { -//│ static { -//│ A1 = this -//│ } -//│ constructor() {} -//│ get get() { -//│ return x; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A"]; -//│ }); -//│ return runtime.Unit -//│ } +//│ f = function f(x) { +//│ let A1, g; /** scoped **/ +//│ globalThis.Object.freeze(class A { +//│ static { +//│ A1 = this +//│ } +//│ constructor() {} +//│ get get() { +//│ return x; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); +//│ return runtime.Unit //│ }; //│ ═══[WARNING] Modules are not yet lifted. diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index ac1a943970..2bce7ec269 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -54,11 +54,10 @@ data class A(x) with //│ constructor(x) { //│ this.x = x; //│ } -//│ getA() {{ -//│ let tmp; /** scoped **/ -//│ tmp = B$(false, this, 2); -//│ return tmp.getB() -//│ } +//│ getA() { +//│ let tmp; /** scoped **/ +//│ tmp = B$(false, this, 2); +//│ return tmp.getB() //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A", ["x"]]; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index 75303e1a13..3c83263869 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -49,11 +49,10 @@ f(1, 2) //│ JS (unsanitized): //│ let g1, g$3; //│ let f3; /** scoped **/ -//│ g$3 = function g$(used1, used2, g_arg) {{ -//│ let used3; /** scoped **/ -//│ used3 = 2; -//│ return used1 + used2 -//│ } +//│ g$3 = function g$(used1, used2, g_arg) { +//│ let used3; /** scoped **/ +//│ used3 = 2; +//│ return used1 + used2 //│ }; //│ g1 = function g(used1, used2) { //│ return (g_arg) => { @@ -61,15 +60,14 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let g$here;{ -//│ let used2, unused2, foo, tmp; /** scoped **/ -//│ used2 = unused1; -//│ unused2 = 2; -//│ g$here = runtime.safeCall(g1(used1, used2)); -//│ foo = g$here; -//│ tmp = runtime.safeCall(foo(used2)); -//│ return tmp + unused2 -//│ } +//│ let g$here; +//│ let used2, unused2, foo, tmp; /** scoped **/ +//│ used2 = unused1; +//│ unused2 = 2; +//│ g$here = runtime.safeCall(g1(used1, used2)); +//│ foo = g$here; +//│ tmp = runtime.safeCall(foo(used2)); +//│ return tmp + unused2 //│ }; //│ f3(1, 2) //│ = 5 @@ -83,20 +81,18 @@ f(1,2,3,4,5,6) //│ JS (unsanitized): //│ let g$4; //│ let f4; /** scoped **/ -//│ g$4 = function g$(a1, a2, a3, a4, a5, a6) {{ -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ tmp = a1 + a2; -//│ tmp1 = tmp + a3; -//│ tmp2 = tmp1 + a4; -//│ tmp3 = tmp2 + a5; -//│ return tmp3 + a6 -//│ } +//│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ tmp = a1 + a2; +//│ tmp1 = tmp + a3; +//│ tmp2 = tmp1 + a4; +//│ tmp3 = tmp2 + a5; +//│ return tmp3 + a6 //│ }; -//│ f4 = function f(a1, a2, a3, a4, a5, a6) {{ -//│ let g2, tmp; /** scoped **/ -//│ tmp = g$4(a1, a2, a3, a4, a5, a6); -//│ return tmp -//│ } +//│ f4 = function f(a1, a2, a3, a4, a5, a6) { +//│ let g2, tmp; /** scoped **/ +//│ tmp = g$4(a1, a2, a3, a4, a5, a6); +//│ return tmp //│ }; //│ f4(1, 2, 3, 4, 5, 6) //│ = 21 @@ -189,14 +185,13 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let capture;{ -//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ -//│ capture = new f$capture5(mutated); -//│ a1 = g$6(immutable, capture); -//│ tmp7 = h$2(capture); -//│ tmp8 = a1 + tmp7; -//│ return tmp8 + unused -//│ } +//│ let capture; +//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ +//│ capture = new f$capture5(mutated); +//│ a1 = g$6(immutable, capture); +//│ tmp7 = h$2(capture); +//│ tmp8 = a1 + tmp7; +//│ return tmp8 + unused //│ }; //│ f7(1, 2, 1000) //│ = 7 @@ -268,12 +263,11 @@ g()(1) //│ x1 = 4; //│ return x1 + g$capture2.y$capture$0 //│ }; -//│ f$1 = function f$(g$capture2, x1) {{ -//│ let h3, k; /** scoped **/ -//│ k = 4; -//│ g$capture2.y$capture$0 = 2; -//│ return x1 -//│ } +//│ f$1 = function f$(g$capture2, x1) { +//│ let h3, k; /** scoped **/ +//│ k = 4; +//│ g$capture2.y$capture$0 = 2; +//│ return x1 //│ }; //│ f14 = function f(g$capture2) { //│ return (x1) => { @@ -291,13 +285,12 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { -//│ let capture, f$here;{ -//│ let y1; /** scoped **/ -//│ capture = new g$capture1(null); -//│ capture.y$capture$0 = 0; -//│ f$here = runtime.safeCall(f14(capture)); -//│ return f$here -//│ } +//│ let capture, f$here; +//│ let y1; /** scoped **/ +//│ capture = new g$capture1(null); +//│ capture.y$capture$0 = 0; +//│ f$here = runtime.safeCall(f14(capture)); +//│ return f$here //│ }; //│ tmp7 = g6(); //│ runtime.safeCall(tmp7(1)) @@ -395,22 +388,21 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let g$here;{ -//│ let y1, scrut; /** scoped **/ -//│ y1 = undefined; -//│ scrut = x1 < 0; -//│ if (scrut === true) { -//│ y1 = 1; -//│ g$here = runtime.safeCall(g12(y1)); -//│ return globalThis.Object.freeze([ -//│ g$here, -//│ g$here -//│ ]) -//│ } else { -//│ y1 = 2; -//│ g$here = runtime.safeCall(g12(y1)); -//│ return globalThis.Object.freeze([ g$here, g$here ]) -//│ } +//│ let g$here; +//│ let y1, scrut; /** scoped **/ +//│ y1 = undefined; +//│ scrut = x1 < 0; +//│ if (scrut === true) { +//│ y1 = 1; +//│ g$here = runtime.safeCall(g12(y1)); +//│ return globalThis.Object.freeze([ +//│ g$here, +//│ g$here +//│ ]) +//│ } else { +//│ y1 = 2; +//│ g$here = runtime.safeCall(g12(y1)); +//│ return globalThis.Object.freeze([ g$here, g$here ]) //│ } //│ }; @@ -442,16 +434,15 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let capture, g$here;{ -//│ let a1, tmp10; /** scoped **/ -//│ capture = new f$capture17(x1); -//│ g$here = runtime.safeCall(g13(capture)); -//│ a1 = g$here; -//│ tmp10 = capture.x$capture$0 + 1; -//│ capture.x$capture$0 = tmp10; -//│ g$here = runtime.safeCall(g13(capture)); -//│ return globalThis.Object.freeze([ a1, g$here ]) -//│ } +//│ let capture, g$here; +//│ let a1, tmp10; /** scoped **/ +//│ capture = new f$capture17(x1); +//│ g$here = runtime.safeCall(g13(capture)); +//│ a1 = g$here; +//│ tmp10 = capture.x$capture$0 + 1; +//│ capture.x$capture$0 = tmp10; +//│ g$here = runtime.safeCall(g13(capture)); +//│ return globalThis.Object.freeze([ a1, g$here ]) //│ }; // Can be avoided in `h` by passing the initialized closure, but this is more complicated diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 9d90f95a3d..4931b1249b 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -56,19 +56,18 @@ fun foo() = //│ } //│ }); //│ while$1 = function while$(foo$capture6) { -//│ let lambda$here;{ -//│ let scrut, tmp2; /** scoped **/ -//│ scrut = true; -//│ if (scrut === true) { -//│ tmp2 = foo$capture6.x$capture$0 + 1; -//│ foo$capture6.x$capture$0 = tmp2; -//│ lambda$here = runtime.safeCall(lambda2(foo$capture6)); -//│ return lambda$here -//│ } else { -//│ foo$capture6.tmp$capture$1 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd +//│ let lambda$here; +//│ let scrut, tmp2; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { +//│ tmp2 = foo$capture6.x$capture$0 + 1; +//│ foo$capture6.x$capture$0 = tmp2; +//│ lambda$here = runtime.safeCall(lambda2(foo$capture6)); +//│ return lambda$here +//│ } else { +//│ foo$capture6.tmp$capture$1 = runtime.Unit; //│ } +//│ return runtime.LoopEnd //│ }; //│ globalThis.Object.freeze(class foo$capture4 { //│ static { @@ -82,15 +81,14 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo2 = function foo() { -//│ let capture;{ -//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ -//│ capture = new foo$capture5(null, null); -//│ capture.x$capture$0 = 1; -//│ capture.tmp$capture$1 = undefined; -//│ tmp3 = while$1(capture); -//│ tmp4 = tmp3 !== runtime.LoopEnd; -//│ if (tmp4 === true) { return tmp3 } else { return capture.tmp$capture$1 } -//│ } +//│ let capture; +//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ +//│ capture = new foo$capture5(null, null); +//│ capture.x$capture$0 = 1; +//│ capture.tmp$capture$1 = undefined; +//│ tmp3 = while$1(capture); +//│ tmp4 = tmp3 !== runtime.LoopEnd; +//│ if (tmp4 === true) { return tmp3 } else { return capture.tmp$capture$1 } //│ }; :expect 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index 7d1e56a774..c09ff52620 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -147,13 +147,12 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let M$1, capture;{ -//│ let foo31, tmp; /** scoped **/ -//│ capture = new foo$capture3(y); -//│ M$1 = globalThis.Object.freeze(new M5(x, capture)); -//│ tmp = foo3$(M$1, x, capture); -//│ return tmp -//│ } +//│ let M$1, capture; +//│ let foo31, tmp; /** scoped **/ +//│ capture = new foo$capture3(y); +//│ M$1 = globalThis.Object.freeze(new M5(x, capture)); +//│ tmp = foo3$(M$1, x, capture); +//│ return tmp //│ }; :expect 12 @@ -223,30 +222,29 @@ M.A().get //│ constructor() { //│ runtime.Unit; //│ } -//│ static {{ -//│ let scrut; /** scoped **/ -//│ this.A = function A() { -//│ return globalThis.Object.freeze(new A.class()); -//│ }; -//│ globalThis.Object.freeze(class A5 { -//│ static { -//│ M16.A.class = this -//│ } -//│ constructor() {} -//│ get get() { -//│ return M17.x + M16.x; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "A", []]; -//│ }); -//│ scrut = M16.A(); -//│ if (scrut instanceof M16.A.class) { -//│ tmp5 = 2; -//│ } else { -//│ tmp5 = 3; +//│ static { +//│ let scrut; /** scoped **/ +//│ this.A = function A() { +//│ return globalThis.Object.freeze(new A.class()); +//│ }; +//│ globalThis.Object.freeze(class A5 { +//│ static { +//│ M16.A.class = this +//│ } +//│ constructor() {} +//│ get get() { +//│ return M17.x + M16.x; //│ } -//│ this.x = tmp5; +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A", []]; +//│ }); +//│ scrut = M16.A(); +//│ if (scrut instanceof M16.A.class) { +//│ tmp5 = 2; +//│ } else { +//│ tmp5 = 3; //│ } +//│ this.x = tmp5; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index a3a89f256b..fae5e2cb30 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -33,15 +33,14 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let capture, bar$here;{ -//│ let x, tmp; /** scoped **/ -//│ capture = new foo$capture1(null); -//│ capture.x$capture$0 = 1; -//│ bar$here = runtime.safeCall(bar(capture)); -//│ tmp = runtime.safeCall(xs.push(bar$here)); -//│ capture.x$capture$0 = 2; -//│ return runtime.Unit -//│ } +//│ let capture, bar$here; +//│ let x, tmp; /** scoped **/ +//│ capture = new foo$capture1(null); +//│ capture.x$capture$0 = 1; +//│ bar$here = runtime.safeCall(bar(capture)); +//│ tmp = runtime.safeCall(xs.push(bar$here)); +//│ capture.x$capture$0 = 2; +//│ return runtime.Unit //│ }; :expect 2 @@ -89,11 +88,10 @@ fun foo() = //│ JS (unsanitized): //│ let bar3; //│ let foo3; /** scoped **/ -//│ bar3 = function bar() {{ -//│ let x; /** scoped **/ -//│ x = undefined; -//│ return x -//│ } +//│ bar3 = function bar() { +//│ let x; /** scoped **/ +//│ x = undefined; +//│ return x //│ }; //│ foo3 = function foo() { return bar3 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 540b7068ff..964c518180 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -61,20 +61,19 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let stackDelayRes;{ -//│ let scrut, tmp; /** scoped **/ -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind$(n, stackDelayRes, 0) -//│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ return hi(tmp) -//│ } +//│ let stackDelayRes; +//│ let scrut, tmp; /** scoped **/ +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind$(n, stackDelayRes, 0) +//│ } +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ return hi(tmp) //│ } //│ }; //│ $_stack$_safe$_body$_ = (undefined, function () { @@ -152,26 +151,25 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let curDepth, stackDelayRes;{ -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ curDepth = runtime.stackDepth; -//│ runtime.stackDepth = runtime.stackDepth + 1; -//│ stackDelayRes = runtime.checkDepth(); -//│ if (stackDelayRes instanceof runtime.EffectSig.class) { -//│ return doUnwind$1(n, tmp1, stackDelayRes, 0) -//│ } -//│ scrut = n == 0; -//│ if (scrut === true) { -//│ return 0 -//│ } else { -//│ tmp = n - 1; -//│ tmp1 = sum1(tmp); -//│ runtime.stackDepth = curDepth; -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind$1(n, tmp1, tmp1, 1) -//│ } -//│ return n + tmp1 +//│ let curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ curDepth = runtime.stackDepth; +//│ runtime.stackDepth = runtime.stackDepth + 1; +//│ stackDelayRes = runtime.checkDepth(); +//│ if (stackDelayRes instanceof runtime.EffectSig.class) { +//│ return doUnwind$1(n, tmp1, stackDelayRes, 0) +//│ } +//│ scrut = n == 0; +//│ if (scrut === true) { +//│ return 0 +//│ } else { +//│ tmp = n - 1; +//│ tmp1 = sum1(tmp); +//│ runtime.stackDepth = curDepth; +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind$1(n, tmp1, tmp1, 1) //│ } +//│ return n + tmp1 //│ } //│ }; //│ $_stack$_safe$_body$_1 = (undefined, function () { @@ -279,11 +277,10 @@ foo(h) fun max(a, b) = if a < b then b else a //│ JS (unsanitized): //│ let max; /** scoped **/ -//│ max = function max(a, b) {{ -//│ let scrut; /** scoped **/ -//│ scrut = a < b; -//│ if (scrut === true) { return b } else { return a } -//│ } +//│ max = function max(a, b) { +//│ let scrut; /** scoped **/ +//│ scrut = a < b; +//│ if (scrut === true) { return b } else { return a } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index a18bd747f6..ada3f10bb0 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -42,33 +42,31 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let idx1, idx2, idx3, idx4, idx5;{ -//│ let tmp, tmp1; /** scoped **/ -//│ idx1 = idx + 0; -//│ tmp = buf.buf.at(idx1) + 1; -//│ idx5 = idx + 0; -//│ buf.buf[idx5] = tmp; -//│ idx2 = idx + 1; -//│ tmp1 = buf.buf.at(idx2) + 2; -//│ idx4 = idx + 1; -//│ buf.buf[idx4] = tmp1; -//│ idx3 = idx + 0; -//│ return buf.buf.at(idx3) -//│ } +//│ let idx1, idx2, idx3, idx4, idx5; +//│ let tmp, tmp1; /** scoped **/ +//│ idx1 = idx + 0; +//│ tmp = buf.buf.at(idx1) + 1; +//│ idx5 = idx + 0; +//│ buf.buf[idx5] = tmp; +//│ idx2 = idx + 1; +//│ tmp1 = buf.buf.at(idx2) + 2; +//│ idx4 = idx + 1; +//│ buf.buf[idx4] = tmp1; +//│ idx3 = idx + 0; +//│ return buf.buf.at(idx3) //│ } //│ } //│ #x; //│ #z; //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } -//│ f(y) {{ -//│ let tmp, tmp1; /** scoped **/ -//│ tmp = this.#x + 1; -//│ this.#x = tmp; -//│ tmp1 = this.z + 2; -//│ this.z = tmp1; -//│ return this.#x -//│ } +//│ f(y) { +//│ let tmp, tmp1; /** scoped **/ +//│ tmp = this.#x + 1; +//│ this.#x = tmp; +//│ tmp1 = this.z + 2; +//│ this.z = tmp1; +//│ return this.#x //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A", [null]]; diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 63bbdc68d8..87eb91c7eb 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,19 +99,17 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ { -//│ let ls, a, middleElements, element0$; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { -//│ element0$ = runtime.Tuple.get(xs1, 0); -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); -//│ ls = middleElements; -//│ a = element0$; -//│ globalThis.Object.freeze([ -//│ a, -//│ ls -//│ ]) -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ let ls, a, middleElements, element0$; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { +//│ element0$ = runtime.Tuple.get(xs1, 0); +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); +//│ ls = middleElements; +//│ a = element0$; +//│ globalThis.Object.freeze([ +//│ a, +//│ ls +//│ ]) +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = [1, [2, 3]] fun addUntilN(xs, n) = @@ -136,20 +134,19 @@ fun popByIndex(start, end, acc, lft) = else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): //│ let popByIndex; /** scoped **/ -//│ popByIndex = function popByIndex(start, end, acc, lft) {{ -//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ -//│ scrut = start >= end; -//│ if (scrut === true) { -//│ return acc -//│ } else { -//│ tmp34 = start + 1; -//│ tmp35 = runtime.safeCall(lft.at(start)); -//│ tmp36 = globalThis.Object.freeze([ -//│ ...acc, -//│ tmp35 -//│ ]); -//│ return popByIndex(tmp34, end, tmp36, lft) -//│ } +//│ popByIndex = function popByIndex(start, end, acc, lft) { +//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ +//│ scrut = start >= end; +//│ if (scrut === true) { +//│ return acc +//│ } else { +//│ tmp34 = start + 1; +//│ tmp35 = runtime.safeCall(lft.at(start)); +//│ tmp36 = globalThis.Object.freeze([ +//│ ...acc, +//│ tmp35 +//│ ]); +//│ return popByIndex(tmp34, end, tmp36, lft) //│ } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 69e093afa5..72036ed9d4 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -19,31 +19,30 @@ x => if x is 0 then 1 x => if x is [[0]] then 1 //│ JS (unsanitized): //│ let lambda1; -//│ lambda1 = (undefined, function (x) {{ -//│ let element0$, element0$1, tmp; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { -//│ element0$ = runtime.Tuple.get(x, 0); -//│ if (runtime.Tuple.isArrayLike(element0$) && element0$.length === 1) { -//│ element0$1 = runtime.Tuple.get(element0$, 0); -//│ if (element0$1 === 0) { -//│ tmp = 1; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } +//│ lambda1 = (undefined, function (x) { +//│ let element0$, element0$1, tmp; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { +//│ element0$ = runtime.Tuple.get(x, 0); +//│ if (runtime.Tuple.isArrayLike(element0$) && element0$.length === 1) { +//│ element0$1 = runtime.Tuple.get(element0$, 0); +//│ if (element0$1 === 0) { +//│ tmp = 1; +//│ break split_root$ //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp //│ }); //│ lambda1 //│ = fun @@ -59,28 +58,25 @@ fun crazy(v) = _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): //│ let crazy; /** scoped **/ -//│ crazy = function crazy(v) {{ -//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ -//│ split_root$: { -//│ split_1$: { -//│ if (v instanceof S.class) { -//│ argument0$ = v.value; -//│ if (argument0$ instanceof S.class) { -//│ argument0$1 = argument0$.value; -//│ if (argument0$1 instanceof S.class) { -//│ argument0$2 = argument0$1.value; -//│ if (argument0$2 instanceof S.class) { -//│ argument0$3 = argument0$2.value; -//│ if (argument0$3 instanceof S.class) { -//│ argument0$4 = argument0$3.value; -//│ if (argument0$4 instanceof S.class) { -//│ argument0$5 = argument0$4.value; -//│ if (argument0$5 === 0) { -//│ tmp = "bruh!"; -//│ break split_root$ -//│ } else { -//│ break split_1$ -//│ } +//│ crazy = function crazy(v) { +//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ split_root$: { +//│ split_1$: { +//│ if (v instanceof S.class) { +//│ argument0$ = v.value; +//│ if (argument0$ instanceof S.class) { +//│ argument0$1 = argument0$.value; +//│ if (argument0$1 instanceof S.class) { +//│ argument0$2 = argument0$1.value; +//│ if (argument0$2 instanceof S.class) { +//│ argument0$3 = argument0$2.value; +//│ if (argument0$3 instanceof S.class) { +//│ argument0$4 = argument0$3.value; +//│ if (argument0$4 instanceof S.class) { +//│ argument0$5 = argument0$4.value; +//│ if (argument0$5 === 0) { +//│ tmp = "bruh!"; +//│ break split_root$ //│ } else { //│ break split_1$ //│ } @@ -99,16 +95,18 @@ fun crazy(v) = //│ } else { //│ break split_1$ //│ } +//│ } else { +//│ break split_1$ //│ } -//│ tmp1 = S(0); -//│ tmp2 = S(tmp1); -//│ tmp3 = S(tmp2); -//│ tmp4 = S(tmp3); -//│ tmp5 = S(tmp4); -//│ tmp = S(tmp5); //│ } -//│ return tmp +//│ tmp1 = S(0); +//│ tmp2 = S(tmp1); +//│ tmp3 = S(tmp2); +//│ tmp4 = S(tmp3); +//│ tmp5 = S(tmp4); +//│ tmp = S(tmp5); //│ } +//│ return tmp //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 7d9a4fdf27..ca253fe2be 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,7 +10,7 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ { let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } } +//│ let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } //│ = 1 :sjs @@ -163,36 +163,35 @@ fun foo(x, y, z) = else "" //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo(x1, y1, z1) {{ -//│ let tmp5; /** scoped **/ -//│ split_root$3: { -//│ split_1$3: { -//│ if (x1 === 0) { -//│ if (y1 === 0) { -//│ if (z1 === 0) { -//│ tmp5 = "000"; -//│ break split_root$3 -//│ } else if (z1 === 1) { -//│ tmp5 = "001"; -//│ break split_root$3 -//│ } else { -//│ break split_1$3 -//│ } -//│ } else if (y1 === 1) { -//│ tmp5 = "01"; +//│ foo = function foo(x1, y1, z1) { +//│ let tmp5; /** scoped **/ +//│ split_root$3: { +//│ split_1$3: { +//│ if (x1 === 0) { +//│ if (y1 === 0) { +//│ if (z1 === 0) { +//│ tmp5 = "000"; +//│ break split_root$3 +//│ } else if (z1 === 1) { +//│ tmp5 = "001"; //│ break split_root$3 //│ } else { //│ break split_1$3 //│ } -//│ } else if (x1 === 1) { -//│ tmp5 = "1"; +//│ } else if (y1 === 1) { +//│ tmp5 = "01"; //│ break split_root$3 -//│ } else { break split_1$3 } -//│ } -//│ tmp5 = ""; +//│ } else { +//│ break split_1$3 +//│ } +//│ } else if (x1 === 1) { +//│ tmp5 = "1"; +//│ break split_root$3 +//│ } else { break split_1$3 } //│ } -//│ return tmp5 +//│ tmp5 = ""; //│ } +//│ return tmp5 //│ }; foo(0, 0, 0) @@ -272,57 +271,56 @@ fun foo(x, y, z) = if else "___" //│ JS (unsanitized): //│ let foo1; /** scoped **/ -//│ foo1 = function foo(x1, y1, z1) {{ -//│ let tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_1$4: { -//│ split_2$1: { -//│ split_3$1: { -//│ if (x1 === 0) { -//│ if (y1 === 0) { -//│ if (z1 === 0) { -//│ tmp6 = "000"; -//│ break split_root$4 -//│ } else if (z1 === 1) { -//│ break split_1$4 -//│ } else { -//│ break split_2$1 -//│ } -//│ } else if (y1 === 1) { -//│ tmp6 = "01_"; +//│ foo1 = function foo(x1, y1, z1) { +//│ let tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_1$4: { +//│ split_2$1: { +//│ split_3$1: { +//│ if (x1 === 0) { +//│ if (y1 === 0) { +//│ if (z1 === 0) { +//│ tmp6 = "000"; //│ break split_root$4 +//│ } else if (z1 === 1) { +//│ break split_1$4 //│ } else { -//│ if (z1 === 1) { -//│ break split_1$4 -//│ } else { -//│ if (y1 === 2) { -//│ break split_3$1 -//│ } else { -//│ break split_2$1 -//│ } -//│ } +//│ break split_2$1 //│ } -//│ } else if (x1 === 1) { -//│ tmp6 = "1__"; +//│ } else if (y1 === 1) { +//│ tmp6 = "01_"; //│ break split_root$4 //│ } else { -//│ if (y1 === 2) { -//│ break split_3$1 +//│ if (z1 === 1) { +//│ break split_1$4 //│ } else { -//│ break split_2$1 +//│ if (y1 === 2) { +//│ break split_3$1 +//│ } else { +//│ break split_2$1 +//│ } //│ } //│ } +//│ } else if (x1 === 1) { +//│ tmp6 = "1__"; +//│ break split_root$4 +//│ } else { +//│ if (y1 === 2) { +//│ break split_3$1 +//│ } else { +//│ break split_2$1 +//│ } //│ } -//│ tmp6 = "_2_"; -//│ break split_root$4; //│ } -//│ tmp6 = "___"; +//│ tmp6 = "_2_"; //│ break split_root$4; //│ } -//│ tmp6 = "0_1"; +//│ tmp6 = "___"; +//│ break split_root$4; //│ } -//│ return tmp6 +//│ tmp6 = "0_1"; //│ } +//│ return tmp6 //│ }; fun expensive_call(str) = Math.pow(2, str.length) @@ -340,39 +338,36 @@ fun foo(x, y, z) = if x is expensive_call(value) //│ JS (unsanitized): //│ let foo2; /** scoped **/ -//│ foo2 = function foo(x1, y1, z1) {{ -//│ let value, tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_1$4: { -//│ if (x1 === 0) { -//│ if (y1 === 0) { -//│ if (z1 === 0) { -//│ tmp6 = "000"; -//│ break split_root$4 -//│ } else if (z1 === 1) { -//│ tmp6 = "001"; -//│ break split_root$4 -//│ } else { -//│ break split_1$4 -//│ } -//│ } else if (y1 === 1) { -//│ tmp6 = "01"; +//│ foo2 = function foo(x1, y1, z1) { +//│ let value, tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_1$4: { +//│ if (x1 === 0) { +//│ if (y1 === 0) { +//│ if (z1 === 0) { +//│ tmp6 = "000"; +//│ break split_root$4 +//│ } else if (z1 === 1) { +//│ tmp6 = "001"; //│ break split_root$4 //│ } else { //│ break split_1$4 //│ } -//│ } else if (x1 === 1) { -//│ tmp6 = "1"; +//│ } else if (y1 === 1) { +//│ tmp6 = "01"; //│ break split_root$4 //│ } else { //│ break split_1$4 //│ } -//│ } -//│ value = "hello"; -//│ tmp6 = expensive_call(value); +//│ } else if (x1 === 1) { +//│ tmp6 = "1"; +//│ break split_root$4 +//│ } else { break split_1$4 } //│ } -//│ return tmp6 +//│ value = "hello"; +//│ tmp6 = expensive_call(value); //│ } +//│ return tmp6 //│ }; object A @@ -407,27 +402,24 @@ fun foo(x, y, z) = else "Goodbye" //│ JS (unsanitized): //│ let foo4; /** scoped **/ -//│ foo4 = function foo(x1, y1, z1) {{ -//│ let tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_1$4: { -//│ if (x1 instanceof A.class) { -//│ if (y1 instanceof B.class) { -//│ if (z1 instanceof C.class) { -//│ tmp6 = "Hello"; -//│ break split_root$4 -//│ } else { -//│ break split_1$4 -//│ } +//│ foo4 = function foo(x1, y1, z1) { +//│ let tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_1$4: { +//│ if (x1 instanceof A.class) { +//│ if (y1 instanceof B.class) { +//│ if (z1 instanceof C.class) { +//│ tmp6 = "Hello"; +//│ break split_root$4 //│ } else { //│ break split_1$4 //│ } //│ } else { break split_1$4 } -//│ } -//│ tmp6 = "Goodbye"; +//│ } else { break split_1$4 } //│ } -//│ return tmp6 +//│ tmp6 = "Goodbye"; //│ } +//│ return tmp6 //│ }; :sjs @@ -436,29 +428,28 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): //│ let foo5; /** scoped **/ -//│ foo5 = function foo(x1, y1, z1) {{ -//│ let tmp6; /** scoped **/ -//│ split_root$4: { -//│ split_default$: { -//│ if (x1 instanceof A.class) { -//│ if (y1 instanceof B.class) { -//│ if (z1 instanceof C.class) { -//│ tmp6 = "Hello"; -//│ break split_root$4 -//│ } else { -//│ break split_default$ -//│ } +//│ foo5 = function foo(x1, y1, z1) { +//│ let tmp6; /** scoped **/ +//│ split_root$4: { +//│ split_default$: { +//│ if (x1 instanceof A.class) { +//│ if (y1 instanceof B.class) { +//│ if (z1 instanceof C.class) { +//│ tmp6 = "Hello"; +//│ break split_root$4 //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp6 +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp6 //│ }; :expect "Hello" diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index 2907b42354..04f680fc0b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -10,31 +10,30 @@ class B x => if x is Pair(A, B) then 1 //│ JS (unsanitized): //│ let lambda; -//│ lambda = (undefined, function (x) {{ -//│ let argument0$, argument1$, tmp; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (x instanceof Pair.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ if (argument0$ instanceof A) { -//│ if (argument1$ instanceof B) { -//│ tmp = 1; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } +//│ lambda = (undefined, function (x) { +//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (x instanceof Pair.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ if (argument0$ instanceof A) { +//│ if (argument1$ instanceof B) { +//│ tmp = 1; +//│ break split_root$ //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp //│ }); //│ lambda //│ = fun @@ -47,38 +46,37 @@ fun f(x) = if x is Pair(B, B) then 2 //│ JS (unsanitized): //│ let f; /** scoped **/ -//│ f = function f(x) {{ -//│ let argument0$, argument1$, tmp; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (x instanceof Pair.class) { -//│ argument0$ = x.a; -//│ argument1$ = x.b; -//│ if (argument0$ instanceof A) { -//│ if (argument1$ instanceof A) { -//│ tmp = 1; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } -//│ } else if (argument0$ instanceof B) { -//│ if (argument1$ instanceof B) { -//│ tmp = 2; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } +//│ f = function f(x) { +//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (x instanceof Pair.class) { +//│ argument0$ = x.a; +//│ argument1$ = x.b; +//│ if (argument0$ instanceof A) { +//│ if (argument1$ instanceof A) { +//│ tmp = 1; +//│ break split_root$ +//│ } else { +//│ break split_default$ +//│ } +//│ } else if (argument0$ instanceof B) { +//│ if (argument1$ instanceof B) { +//│ tmp = 2; +//│ break split_root$ //│ } else { //│ break split_default$ //│ } //│ } else { //│ break split_default$ //│ } +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 54381dba85..697105bd95 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -37,18 +37,15 @@ fun foo(v) = else 0 //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo(v) {{ -//│ let tmp2; /** scoped **/ -//│ split_root$2: { -//│ split_1$2: { -//│ if (v instanceof A) { -//│ break split_1$2 -//│ } else { break split_1$2 } -//│ } -//│ tmp2 = 0; +//│ foo = function foo(v) { +//│ let tmp2; /** scoped **/ +//│ split_root$2: { +//│ split_1$2: { +//│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } //│ } -//│ return tmp2 +//│ tmp2 = 0; //│ } +//│ return tmp2 //│ }; fun range(i, j) = diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index bc193b99b3..408479468b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -7,14 +7,13 @@ fun nonsense(xs) = if xs is [] then "empty" //│ JS (unsanitized): //│ let nonsense; /** scoped **/ -//│ nonsense = function nonsense(xs) {{ -//│ let ys, middleElements; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); -//│ ys = middleElements; -//│ return ys -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ nonsense = function nonsense(xs) { +//│ let ys, middleElements; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); +//│ ys = middleElements; +//│ return ys +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; nonsense([]) @@ -29,20 +28,19 @@ fun lead_and_last(xs) = if xs is [] then 0 //│ JS (unsanitized): //│ let lead_and_last; /** scoped **/ -//│ lead_and_last = function lead_and_last(xs) {{ -//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ -//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { -//│ element0$ = runtime.Tuple.get(xs, 0); -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); -//│ lastElement0$ = runtime.Tuple.get(xs, -1); -//│ y = lastElement0$; -//│ ys = middleElements; -//│ x = element0$; -//│ return x + y -//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { -//│ return 0 -//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } -//│ } +//│ lead_and_last = function lead_and_last(xs) { +//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ +//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { +//│ element0$ = runtime.Tuple.get(xs, 0); +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); +//│ lastElement0$ = runtime.Tuple.get(xs, -1); +//│ y = lastElement0$; +//│ ys = middleElements; +//│ x = element0$; +//│ return x + y +//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { +//│ return 0 +//│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; lead_and_last(["foo", "bar"]) @@ -64,39 +62,38 @@ fun nested_tuple_patterns(xs) = if xs is [] then 0 //│ JS (unsanitized): //│ let nested_tuple_patterns; /** scoped **/ -//│ nested_tuple_patterns = function nested_tuple_patterns(xs) {{ -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ -//│ split_root$: { -//│ split_default$: { -//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { -//│ element0$ = runtime.Tuple.get(xs, 0); -//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); -//│ lastElement0$ = runtime.Tuple.get(xs, -1); -//│ if (runtime.Tuple.isArrayLike(middleElements) && middleElements.length === 2) { -//│ element0$1 = runtime.Tuple.get(middleElements, 0); -//│ element1$ = runtime.Tuple.get(middleElements, 1); -//│ w = lastElement0$; -//│ z = element1$; -//│ y = element0$1; -//│ x = element0$; -//│ tmp6 = x + y; -//│ tmp7 = tmp6 + z; -//│ tmp8 = tmp7 + w; -//│ break split_root$ -//│ } else { -//│ break split_default$ -//│ } -//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { -//│ tmp8 = 0; +//│ nested_tuple_patterns = function nested_tuple_patterns(xs) { +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ +//│ split_root$: { +//│ split_default$: { +//│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { +//│ element0$ = runtime.Tuple.get(xs, 0); +//│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); +//│ lastElement0$ = runtime.Tuple.get(xs, -1); +//│ if (runtime.Tuple.isArrayLike(middleElements) && middleElements.length === 2) { +//│ element0$1 = runtime.Tuple.get(middleElements, 0); +//│ element1$ = runtime.Tuple.get(middleElements, 1); +//│ w = lastElement0$; +//│ z = element1$; +//│ y = element0$1; +//│ x = element0$; +//│ tmp6 = x + y; +//│ tmp7 = tmp6 + z; +//│ tmp8 = tmp7 + w; //│ break split_root$ //│ } else { //│ break split_default$ //│ } +//│ } else if (runtime.Tuple.isArrayLike(xs) && xs.length === 0) { +//│ tmp8 = 0; +//│ break split_root$ +//│ } else { +//│ break split_default$ //│ } -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } -//│ return tmp8 +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")); //│ } +//│ return tmp8 //│ }; nested_tuple_patterns of [1, 2, 3, 4] diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index 34bdc937e6..daa8dac993 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -34,13 +34,12 @@ Cross.unapply("0") fun foo(x) = x is Cross //│ JS (unsanitized): //│ let foo; /** scoped **/ -//│ foo = function foo(x2) {{ -//│ let unapplyResult, output, bindings; /** scoped **/ -//│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); -//│ if (unapplyResult instanceof runtime.MatchSuccess.class) { -//│ output = unapplyResult.output; -//│ bindings = unapplyResult.bindings; -//│ return true -//│ } else { return false } -//│ } +//│ foo = function foo(x2) { +//│ let unapplyResult, output, bindings; /** scoped **/ +//│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); +//│ if (unapplyResult instanceof runtime.MatchSuccess.class) { +//│ output = unapplyResult.output; +//│ bindings = unapplyResult.bindings; +//│ return true +//│ } else { return false } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index f162b5ee1a..b5e544bd4f 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -18,21 +18,20 @@ pattern SumPair = Pair(a, b) => a + b //│ } //│ constructor() {} //│ unapply(input) { -//│ let lambda;{ -//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ -//│ lambda = (undefined, function (a, b) { -//│ return a + b -//│ }); -//│ transform = lambda; -//│ if (input instanceof Pair.class) { -//│ argument0$ = input.first; -//│ argument1$ = input.second; -//│ transformResult = runtime.safeCall(transform(argument0$, argument1$)); -//│ tmp = globalThis.Object.freeze({}); -//│ return globalThis.Object.freeze(new runtime.MatchSuccess.class(transformResult, tmp)) -//│ } else { -//│ return globalThis.Object.freeze(new runtime.MatchFailure.class(null)) -//│ } +//│ let lambda; +//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ +//│ lambda = (undefined, function (a, b) { +//│ return a + b +//│ }); +//│ transform = lambda; +//│ if (input instanceof Pair.class) { +//│ argument0$ = input.first; +//│ argument1$ = input.second; +//│ transformResult = runtime.safeCall(transform(argument0$, argument1$)); +//│ tmp = globalThis.Object.freeze({}); +//│ return globalThis.Object.freeze(new runtime.MatchSuccess.class(transformResult, tmp)) +//│ } else { +//│ return globalThis.Object.freeze(new runtime.MatchFailure.class(null)) //│ } //│ } //│ unapplyStringPrefix(input) { From 0f2f0692968570081fcf2dbb76f2a013aa1f151f Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Sat, 29 Nov 2025 21:40:59 +0800 Subject: [PATCH 37/72] WIP: Revert & try to handle non-nested Scoped separately --- .../src/main/scala/hkmc2/codegen/Block.scala | 52 +++---- .../hkmc2/codegen/BlockTransformer.scala | 4 +- .../scala/hkmc2/codegen/BlockTraverser.scala | 2 +- .../scala/hkmc2/codegen/HandlerLowering.scala | 2 +- .../main/scala/hkmc2/codegen/Lowering.scala | 14 +- .../main/scala/hkmc2/codegen/Printer.scala | 2 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 58 ++++---- .../scala/hkmc2/codegen/llir/Builder.scala | 4 +- .../hkmc2/codegen/wasm/text/WatBuilder.scala | 2 +- .../hkmc2/semantics/ucs/Normalization.scala | 3 +- .../src/test/mlscript-compile/Predef.mjs | 22 +-- .../src/test/mlscript-compile/Runtime.mjs | 138 +++++++++--------- .../OverloadedModulesInSignatures.mls | 4 +- .../backlog/NonReturningStatements.mls | 2 +- .../src/test/mlscript/backlog/ToTriage.mls | 60 ++++---- .../src/test/mlscript/basics/BadDefs.mls | 4 +- .../basics/CompanionModules_Classes.mls | 2 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../shared/src/test/mlscript/basics/Drop.mls | 10 +- .../test/mlscript/basics/FunnyRecordKeys.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 2 +- .../src/test/mlscript/basics/LazySpreads.mls | 8 +- .../mlscript/basics/MemberProjections.mls | 4 +- .../test/mlscript/basics/MiscArrayTests.mls | 2 +- .../mlscript/basics/MultiParamListClasses.mls | 8 +- .../test/mlscript/basics/MultiParamLists.mls | 22 ++- .../mlscript/basics/MultilineExpressions.mls | 2 +- .../src/test/mlscript/basics/MutArr.mls | 4 +- .../src/test/mlscript/basics/MutRcd.mls | 2 +- .../src/test/mlscript/basics/MutVal.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../src/test/mlscript/basics/PartialApps.mls | 9 +- .../mlscript/basics/PureTermStatements.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 12 +- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/basics/Underscores.mls | 6 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 30 ++-- .../src/test/mlscript/bbml/bbGetters.mls | 14 +- .../src/test/mlscript/codegen/Arrays.mls | 4 +- .../src/test/mlscript/codegen/BadInit.mls | 4 +- .../src/test/mlscript/codegen/BadNew.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 3 +- .../src/test/mlscript/codegen/BuiltinOps.mls | 4 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 4 +- .../test/mlscript/codegen/CaseShorthand.mls | 14 +- .../test/mlscript/codegen/ClassInClass.mls | 4 +- .../src/test/mlscript/codegen/ClassInFun.mls | 8 +- .../test/mlscript/codegen/ClassMatching.mls | 18 +-- .../src/test/mlscript/codegen/Classes.mls | 8 +- .../src/test/mlscript/codegen/Comma.mls | 20 +-- .../src/test/mlscript/codegen/ConsoleLog.mls | 8 +- .../test/mlscript/codegen/DelayedLetInit.mls | 18 +-- .../src/test/mlscript/codegen/EarlyReturn.mls | 4 +- .../test/mlscript/codegen/FieldSymbols.mls | 1 - .../src/test/mlscript/codegen/Formatting.mls | 22 +-- .../src/test/mlscript/codegen/FunInClass.mls | 18 +-- .../test/mlscript/codegen/FunctionsThis.mls | 4 +- .../src/test/mlscript/codegen/Getters.mls | 47 +++--- .../src/test/mlscript/codegen/GlobalThis.mls | 6 +- .../src/test/mlscript/codegen/Hygiene.mls | 24 ++- .../src/test/mlscript/codegen/IfThenElse.mls | 10 +- .../src/test/mlscript/codegen/ImportMLs.mls | 10 +- .../src/test/mlscript/codegen/ImportedOps.mls | 8 +- .../test/mlscript/codegen/InlineLambdas.mls | 12 +- .../src/test/mlscript/codegen/Lambdas.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 34 ++--- .../test/mlscript/codegen/ModuleMethods.mls | 2 +- .../src/test/mlscript/codegen/Modules.mls | 8 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 6 +- .../test/mlscript/codegen/ParamClasses.mls | 28 ++-- .../src/test/mlscript/codegen/PartialApps.mls | 27 ++-- .../test/mlscript/codegen/PlainClasses.mls | 28 ++-- .../src/test/mlscript/codegen/Primes.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/Quasiquotes.mls | 16 +- .../src/test/mlscript/codegen/RandomStuff.mls | 26 ++-- .../test/mlscript/codegen/SanityChecks.mls | 8 +- .../src/test/mlscript/codegen/Scoped.mls | 36 ++--- .../test/mlscript/codegen/SelfReferences.mls | 4 +- .../src/test/mlscript/codegen/SetIn.mls | 36 ++--- .../src/test/mlscript/codegen/Spreads.mls | 2 +- .../shared/src/test/mlscript/codegen/This.mls | 7 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/ThisCalls.mls | 2 +- .../src/test/mlscript/codegen/Throw.mls | 6 +- .../src/test/mlscript/codegen/TraceLog.mls | 4 +- .../src/test/mlscript/codegen/UnitValue.mls | 10 +- .../src/test/mlscript/codegen/While.mls | 38 ++--- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/ctx/EtaExpansion.mls | 32 ++-- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../src/test/mlscript/handlers/Debugging.mls | 4 +- .../src/test/mlscript/handlers/Effects.mls | 4 +- .../test/mlscript/handlers/EffectsHygiene.mls | 4 +- .../mlscript/handlers/EffectsInClasses.mls | 2 +- .../mlscript/handlers/RecursiveHandlers.mls | 36 ++--- .../test/mlscript/handlers/SetInHandlers.mls | 14 +- .../test/mlscript/handlers/StackSafety.mls | 14 +- .../src/test/mlscript/interop/Arrays.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 6 +- .../test/mlscript/lifter/CompanionsInFun.mls | 4 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 6 +- .../src/test/mlscript/lifter/FunInFun.mls | 30 ++-- .../src/test/mlscript/lifter/Imports.mls | 2 +- .../shared/src/test/mlscript/lifter/Loops.mls | 6 +- .../test/mlscript/lifter/ModulesObjects.mls | 12 +- .../src/test/mlscript/lifter/Mutation.mls | 12 +- .../test/mlscript/lifter/StackSafetyLift.mls | 14 +- .../src/test/mlscript/objbuf/Mutation.mls | 6 +- .../src/test/mlscript/parser/PrefixOps.mls | 4 +- .../test/mlscript/std/FingerTreeListTest.mls | 6 +- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 6 +- .../ucs/general/LogicalConnectives.mls | 10 +- .../ucs/normalization/Deduplication.mls | 41 +++--- .../normalization/ExcessiveDeduplication.mls | 2 +- .../ucs/normalization/SimplePairMatches.mls | 6 +- .../ucs/patterns/ConjunctionPattern.mls | 4 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 12 +- .../src/test/mlscript/ups/MatchResult.mls | 4 +- .../src/test/mlscript/ups/SimpleTransform.mls | 4 +- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 15 +- 123 files changed, 712 insertions(+), 773 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 17be1b40d9..244eda5cb0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -49,7 +49,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVarsNoScoped + res case TryBlock(sub, fin, rst) => sub.definedVarsNoScoped ++ fin.definedVarsNoScoped ++ rst.definedVarsNoScoped case Label(lbl, _, bod, rst) => bod.definedVarsNoScoped ++ rst.definedVarsNoScoped - case Scoped(syms, body, _) => body.definedVarsNoScoped -- syms + case Scoped(syms, body) => body.definedVarsNoScoped -- syms lazy val definedVars: Set[Local] = this match case _: Return | _: Throw => Set.empty @@ -70,7 +70,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - case Scoped(syms, body, _) => body.definedVars// -- syms + case Scoped(syms, body) => body.definedVars// -- syms lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -84,7 +84,7 @@ sealed abstract class Block extends Product: case TryBlock(sub, fin, rst) => 1 + sub.size + fin.size + rst.size case Label(_, _, bod, rst) => 1 + bod.size + rst.size case HandleBlock(lhs, res, par, args, cls, handlers, bdy, rst) => 1 + handlers.map(_.body.size).sum + bdy.size + rst.size - case Scoped(_, body, _) => body.size + case Scoped(_, body) => body.size // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match @@ -124,7 +124,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body, _) => body.freeVars// -- syms + case Scoped(syms, body) => body.freeVars// -- syms case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -145,7 +145,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body, _) => body.freeVarsLLIR// -- syms + case Scoped(syms, body) => body.freeVarsLLIR// -- syms case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match @@ -158,7 +158,7 @@ sealed abstract class Block extends Product: case Define(d, rest) => d.subBlocks ::: rest :: Nil case HandleBlock(_, _, par, args, _, handlers, body, rest) => par.subBlocks ++ args.flatMap(_.subBlocks) ++ handlers.map(_.body) :+ body :+ rest case Label(_, _, body, rest) => body :: rest :: Nil - case Scoped(_, body, _) => body :: Nil + case Scoped(_, body) => body :: Nil // TODO rm Lam from values and thus the need for these cases case Return(r, _) => r.subBlocks @@ -273,11 +273,11 @@ sealed abstract class Block extends Product: then this else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) - case Scoped(syms, body, topLevel) => + case Scoped(syms, body) => val newBody = body.flatten(k) if newBody is body then this - else Scoped(syms, newBody, topLevel) + else Scoped(syms, newBody) case e: End => k(e) case t: BlockTail => this @@ -304,7 +304,7 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: collection.Set[Local], body: Block, topLevel: Bool) extends BlockTail +case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail @@ -322,44 +322,44 @@ case class Define(defn: Defn, rest: Block) extends Block with ProductWithTail object Match: def apply(scrut: Path, arms: Ls[Case -> Block], dflt: Opt[Block], rest: Block): Block = rest match - case Scoped(syms, body, tlvl) => Scoped(syms, Match(scrut, arms, dflt, body), tlvl) + case Scoped(syms, body) => Scoped(syms, Match(scrut, arms, dflt, body)) case _ => new Match(scrut, arms, dflt, rest) object Label: def apply(label: Local, loop: Bool, body: Block, rest: Block): Block = rest match - case Scoped(syms, rest, tlvl) => Scoped(syms, Label(label, loop, body, rest), tlvl) + case Scoped(syms, rest) => Scoped(syms, Label(label, loop, body, rest)) case _ => new Label(label, loop, body, rest) object Scoped: - def apply(syms: collection.Set[Local], body: Block, tlvl: Bool): Block = body match - case Scoped(syms2, body, _) => - if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body, tlvl) else Scoped(syms ++ syms2, body, tlvl) + def apply(syms: collection.Set[Local], body: Block): Block = body match + case Scoped(syms2, body) => + if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body) else Scoped(syms ++ syms2, body) case _ => - if syms.isEmpty then body else new Scoped(syms, body, tlvl) + if syms.isEmpty then body else new Scoped(syms, body) object Begin: def apply(sub: Block, rest: Block): Block = (sub, rest) match - case (Scoped(symsSub, bodySub, tlvl1), Scoped(symsRest, bodyRest, tlvl2)) => - Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest), tlvl1 || tlvl2) - case (Scoped(symsSub, bodySub, tlvl), _) => Scoped(symsSub, Begin(bodySub, rest), tlvl) - case (_, Scoped(symsRest, bodyRest, tlvl)) => Scoped(symsRest, Begin(sub, bodyRest), tlvl) + case (Scoped(symsSub, bodySub), Scoped(symsRest, bodyRest)) => + Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest)) + case (Scoped(symsSub, bodySub), _) => Scoped(symsSub, Begin(bodySub, rest)) + case (_, Scoped(symsRest, bodyRest)) => Scoped(symsRest, Begin(sub, bodyRest)) case _ => new Begin(sub, rest) object TryBlock: def apply(sub: Block, finallyDo: Block, rest: Block): Block = rest match - case Scoped(syms, body, tlvl) => Scoped(syms, TryBlock(sub, finallyDo, body), tlvl) + case Scoped(syms, body) => Scoped(syms, TryBlock(sub, finallyDo, body)) case _ => new TryBlock(sub, finallyDo, rest) object Assign: def apply(lhs: Local, rhs: Result, rest: Block): Block = rest match - case Scoped(syms, body, tlvl) => Scoped(syms, Assign(lhs, rhs, body), tlvl) + case Scoped(syms, body) => Scoped(syms, Assign(lhs, rhs, body)) case _ => new Assign(lhs, rhs, rest) object AssignField: def apply(lhs: Path, nme: Tree.Ident, rhs: Result, rest: Block)(symbol: Opt[MemberSymbol]): Block = rest match - case Scoped(syms, body, tlvl) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol), tlvl) + case Scoped(syms, body) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol)) case _ => new AssignField(lhs, nme, rhs, rest)(symbol) object AssignDynField: def apply(lhs: Path, fld: Path, arrayIdx: Bool, rhs: Result, rest: Block): Block = rest match - case Scoped(syms, body, tlvl) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body), tlvl) + case Scoped(syms, body) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body)) case _ => new AssignDynField(lhs, fld, arrayIdx, rhs, rest) object Define: def apply(defn: Defn, rest: Block): Block = rest match - case Scoped(syms, body, tlvl) => Scoped(syms, Define(defn, body), tlvl) + case Scoped(syms, body) => Scoped(syms, Define(defn, body)) case _ => new Define(defn, rest) case class HandleBlock( @@ -385,7 +385,7 @@ object HandleBlock: body: Block, rest: Block ) = rest match - case Scoped(syms, rest, tlvl) => + case Scoped(syms, rest) => Scoped( syms, new HandleBlock( @@ -397,7 +397,7 @@ object HandleBlock: handlers, body, rest - ), tlvl) + )) case _ => new HandleBlock( lhs, res, diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala index 6575afda49..c3dc63c211 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala @@ -95,9 +95,9 @@ class BlockTransformer(subst: SymbolSubst): if (lhs2 is lhs) && (fld2 is fld) && (rhs2 is rhs) && (rest2 is rest) then b else AssignDynField(lhs2, fld2, arrayIdx, rhs2, rest2) - case Scoped(s, bd, topLevel) => + case Scoped(s, bd) => val nb = applySubBlock(bd) - if nb is bd then b else Scoped(s, nb, topLevel) + if nb is bd then b else Scoped(s, nb) def applyRcdArg(rcdArg: RcdArg)(k: RcdArg => Block): Block = val RcdArg(idx, p) = rcdArg diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala index 68201e292f..f8f41dd373 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTraverser.scala @@ -51,7 +51,7 @@ class BlockTraverser: applyResult(rhs) applyPath(fld) applySubBlock(rest) - case Scoped(_, body, _) => applySubBlock(body) + case Scoped(_, body) => applySubBlock(body) def applyResult(r: Result): Unit = r match case r @ Call(fun, args) => applyPath(fun); args.foreach(applyArg) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index cc6cda7293..97a13183b4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -420,7 +420,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, // ignored cases case TryBlock(sub, finallyDo, rest) => ??? // ignore case Throw(_) => PartRet(blk, Nil) - case Scoped(_, body, _) => go(body) + case Scoped(_, body) => go(body) case _: HandleBlock => lastWords("unexpected handleBlock") // already translated at this point val PartRet(head, states) = go(blk)(using labelIds, N) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 87d87e13e0..16d264cf85 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -971,7 +971,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) val blk = - inScopedBlock(main.stats.foldLeft(main.res.definedSyms)(_ ++ _.definedSyms), true)(using LoweringCtx.empty): + inScopedBlock(main.stats.foldLeft(main.res.definedSyms)(_ ++ _.definedSyms))(using LoweringCtx.empty): block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -1016,18 +1016,18 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case ps => ps setupFunctionDef(physicalParams, bodyTerm, name) - def inScopedBlock(definedSymsInElaborated: Set[Symbol], topLevel: Bool)(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = + def inScopedBlock(definedSymsInElaborated: Set[Symbol])(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - possiblyScoped(scopedSyms, body, topLevel) + possiblyScoped(scopedSyms, body) - inline def possiblyScoped(syms: collection.Set[Symbol], body: Block, topLevel: Bool) = - if syms.isEmpty then body else Scoped(syms, body, topLevel) + inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = + if syms.isEmpty then body else Scoped(syms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = - val scopedBody = inScopedBlock(bodyTerm.definedSyms, false)(returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(bodyTerm.definedSyms)(returnedTerm(bodyTerm)) (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = @@ -1140,7 +1140,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) go(paramLists.reverse, bod) def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - inScopedBlock(bod.definedSyms, false): + inScopedBlock(bod.definedSyms): val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") val resSym = TempSymbol(N, dbgNme = "traceLogRes") diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala index c5c9df6290..00dc4e6160 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala @@ -55,7 +55,7 @@ object Printer: doc"set ${mkDocument(lhs)}.${nme.name} = ${mkDocument(rhs)} in # ${mkDocument(rest)}" case Define(defn, rest) => doc"define ${mkDocument(defn)} in # ${mkDocument(rest)}" - case Scoped(_, body, _) => mkDocument(body) + case Scoped(_, body) => mkDocument(body) case End("") => doc"end" case End(msg) => doc"end ${msg}" case _ => TODO(blk) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 250c56c569..f2224ef7e1 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -444,7 +444,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case Match(scrut, Nil, els, rest) => val e = els match - case S(el) => returningTerm(el, endSemi = true) + case S(el) => nonNestedScoped(el)(bod => returningTerm(bod, endSemi = true)) case N => doc"" e :: returningTerm(rest, endSemi) case Match(scrut, hd :: tl, els, rest) => @@ -469,12 +469,12 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc"""typeof $sd === "object" && $sd !== null && "${n.name}" in $sd""" case Case.Field(name = n, safe = true) => doc""""${n.name}" in $sd""" - val h = doc" # if (${ cond(hd._1) }) ${ braced(returningTerm(hd._2, endSemi = false)) }" + val h = doc" # if (${ cond(hd._1) }) ${ braced(nonNestedScoped(hd._2)(res => returningTerm(res, endSemi = false))) }" val t = tl.foldLeft(h)((acc, arm) => - acc :: doc" else if (${ cond(arm._1) }) ${ braced(returningTerm(arm._2, endSemi = false)) }") + acc :: doc" else if (${ cond(arm._1) }) ${ braced(nonNestedScoped(arm._2)(res => returningTerm(res, endSemi = false))) }") val e = els match case S(el) => - doc" else ${ braced(returningTerm(el, endSemi = false)) }" + doc" else ${ braced(nonNestedScoped(el)(res => returningTerm(res, endSemi = false))) }" case N => doc"" t :: e :: returningTerm(rest, endSemi) @@ -498,9 +498,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: scope.allocateName(lbl) // [fixme:0] TODO check scope and allocate local variables here (see: https://github.com/hkust-taco/mlscript/pull/293#issuecomment-2792229849) - + doc" # ${getVar(lbl, lbl.toLoc)}:${if loop then doc" while (true)" else ""} " :: braced { - returningTerm(bod, endSemi = true) :: (if loop then doc" # break;" else doc"") + nonNestedScoped(bod)(bd => returningTerm(bd, endSemi = true)) :: (if loop then doc" # break;" else doc"") } :: returningTerm(rst, endSemi) case TryBlock(sub, fin, rst) => @@ -509,9 +509,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: } # ${ returningTerm(rst, endSemi).stripBreaks}" - case Scoped(syms, body, topLevel) => - val nextScope = if topLevel then scope else scope.nest - nextScope.givenIn: + // Only nested scopes will be handled here. + case Scoped(syms, body) => + scope.nest.givenIn: val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => if scope.lookup(l).isDefined then // NOTE: this warning is turned off because the lifter is not @@ -524,12 +524,8 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: None else Some(l -> scope.allocateName(l)) - // NOTE: currently this does not generate pretty JS codes... - (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc"; /** scoped **/") :: returningTerm(body, endSemi) + braced: + genLetDecls(vars, true) :: returningTerm(body, endSemi) // case _ => ??? @@ -596,10 +592,10 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc"""import ${getVar(i._1, N)} from "${relPath}";""" // NOTE: this is to make sure that we are NOT generating the top level // block in a nested scope, because for exported symbols they are looked up in the outer scope - // val unscopedMain = p.main match - // case Scoped(syms, body, _) /* if exprt.isDefined */ => body - // case _ => p.main - imps.mkDocument(doc" # ") :/: block(p.main, endSemi = false).stripBreaks :: ( + val (scopedSyms, unscopedMain) = p.main match + case Scoped(syms, body) /* if exprt.isDefined */ => (syms, body) + case _ => (Set.empty, p.main) + imps.mkDocument(doc" # ") :/: (genLetDecls(scopedSyms.iterator.map(l => l -> scope.allocateName(l)), false) :: block(unscopedMain, endSemi = false)).stripBreaks :: ( exprt match case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" case N => doc"" @@ -611,16 +607,28 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: doc"""${getVar(i._1, N)} = await import("${i._2.toString}").then(m => m.default ?? m);""" blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVars.toSeq) -> (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) + + def genLetDecls(vars: Iterator[(Symbol, Str)], isScoped: Bool): Document = + if vars.isEmpty then doc"" else + doc" # let " :: vars.map: (_, nme) => + nme + .toList.mkDocument(", ") + :: (if isScoped then doc"; /** scoped **/" else doc";") def blockPreamble(ss: Iterable[Symbol])(using Raise, Scope): Document = // TODO document: mutable var assnts require the lookup val vars = ss.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) - if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc";" + genLetDecls(vars, false) + + // Only handle non-nested Scoped nodes: we output the bindings, but do not add another brace + def nonNestedScoped(blk: Block)(k: Block => Document)(using Raise, Scope): Document = blk match + case Scoped(syms, body) => + val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => + l -> scope.allocateName(l)) + genLetDecls(vars, false) :: k(body) + case _ => k(blk) + def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = // println(s"$t :::::::: ${t.definedVars}") @@ -629,7 +637,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: pre :: rest def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn: - block(t, endSemi) + nonNestedScoped(t)(bd => block(bd, endSemi)) def defineProperty(target: Document, prop: Str, value: Document, enumerable: Bool = false): Document = doc"Object.defineProperty(${target}, ${prop.escaped}, ${ diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala index 4048b8ecb5..a7c1621a29 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/llir/Builder.scala @@ -523,7 +523,7 @@ final class LlirBuilder(using Elaborator.State)(tl: TraceLogger, uid: FreshInt): bBlock(rest)(k)(ct) case Define(_: ClsLikeDefn, rest) => bBlock(rest)(k)(ct) case End(msg) => k(Expr.Literal(Tree.UnitLit(false))) - case Scoped(_, body, _) => bBlock(body)(k)(ct) + case Scoped(_, body) => bBlock(body)(k)(ct) case _: Block => val docBlock = blk.showAsTree bErrStop(msg"Unsupported block: $docBlock") @@ -567,7 +567,7 @@ final class LlirBuilder(using Elaborator.State)(tl: TraceLogger, uid: FreshInt): case AssignField(_, _, _, rest) => applyBlock(rest) case AssignDynField(lhs, fld, arrayIdx, rhs, rest) => applyBlock(rest) case Define(defn, rest) => applyDefn(defn); applyBlock(rest) - case Scoped(_, body, _) => applyBlock(body) + case Scoped(_, body) => applyBlock(body) case HandleBlock(lhs, res, par, args, cls, handlers, body, rest) => applyBlock(rest) case End(msg) => diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala index a7f8350505..00cdae4b46 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala @@ -600,7 +600,7 @@ class WatBuilder(using TraceLogger, State) extends CodeBuilder: `return`(S(resWat)) - case Scoped(_, body, _) => returningTerm(body) + case Scoped(_, body) => returningTerm(body) case Match(scrut, arms, dflt, rst) => val matchLabelSym = TempSymbol(N, "match") val matchLabel = scope.allocateName(matchLabelSym) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 986ac61a29..b0d5618c75 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -399,8 +399,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) Scoped( LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()), - false + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) ) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index e7f4e8a4fa..56efea3c58 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -5,7 +5,7 @@ import Term from "./Term.mjs"; import RuntimeJS from "./RuntimeJS.mjs"; import Runtime from "./Runtime.mjs"; import Rendering from "./Rendering.mjs"; -let Predef1; /** scoped **/ +let Predef1; globalThis.Object.freeze(class Predef { static { Predef1 = this @@ -57,12 +57,12 @@ globalThis.Object.freeze(class Predef { return runtime.safeCall(f(x)) } static tap(x, f) { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(f(x)); return (tmp , x) } static pat(f, x) { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(f(x)); return (tmp , x) } @@ -71,14 +71,14 @@ globalThis.Object.freeze(class Predef { } static andThen(f, g) { return (x) => { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(f(x)); return runtime.safeCall(g(tmp)) } } static compose(f, g) { return (x) => { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(g(x)); return runtime.safeCall(f(tmp)) } @@ -99,7 +99,7 @@ globalThis.Object.freeze(class Predef { } } static print(...xs) { - let tmp, tmp1; /** scoped **/ + let tmp, tmp1; tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); tmp1 = runtime.safeCall(tmp(...xs)); return runtime.safeCall(globalThis.console.log(...tmp1)) @@ -112,7 +112,7 @@ globalThis.Object.freeze(class Predef { } } static notImplemented(msg) { - let tmp; /** scoped **/ + let tmp; tmp = "Not implemented: " + msg; throw globalThis.Error(tmp) } @@ -124,7 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, tmp; /** scoped **/ + let len, scrut, i, init, tmp; len = rest.length; scrut = len == 0; if (scrut === true) { @@ -133,7 +133,7 @@ globalThis.Object.freeze(class Predef { i = len - 1; init = runtime.safeCall(rest.at(i)); tmp1: while (true) { - let scrut1, tmp2, tmp3, tmp4; /** scoped **/ + let scrut1, tmp2, tmp3, tmp4; scrut1 = i > 0; if (scrut1 === true) { tmp2 = i - 1; @@ -153,9 +153,9 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda, tmp; /** scoped **/ + let lambda, tmp; lambda = (undefined, function (acc, x) { - let tmp1, tmp2, tmp3; /** scoped **/ + let tmp1, tmp2, tmp3; if (typeof x === 'string') { tmp1 = true; } else { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 42a7977412..0c1552eb61 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -6,7 +6,7 @@ import RuntimeJS from "./RuntimeJS.mjs"; import Rendering from "./Rendering.mjs"; import LazyArray from "./LazyArray.mjs"; import Iter from "./Iter.mjs"; -let Runtime1; /** scoped **/ +let Runtime1; globalThis.Object.freeze(class Runtime { static { Runtime1 = this @@ -74,10 +74,10 @@ globalThis.Object.freeze(class Runtime { } #_reified; resumeWith(value) { - let lambda; /** scoped **/ + let lambda; const this$EffectHandle = this; lambda = (undefined, function () { - let tmp; /** scoped **/ + let tmp; tmp = Runtime.resume(this$EffectHandle.reified.contTrace); return runtime.safeCall(tmp(value)) }); @@ -127,12 +127,12 @@ globalThis.Object.freeze(class Runtime { this.split = LazyArray.__split; } static slice(xs, i, j) { - let tmp; /** scoped **/ + let tmp; tmp = xs.length - j; return xs.slice(i, tmp) } static lazySlice(xs, i, j) { - let tmp; /** scoped **/ + let tmp; tmp = LazyArray.dropLeftRight(i, j); return runtime.safeCall(tmp(xs)) } @@ -140,7 +140,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(LazyArray.__concat(...args)) } static get(xs, i) { - let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ + let scrut, scrut1, tmp, tmp1, tmp2; scrut = i >= xs.length; if (scrut === true) { throw globalThis.RangeError("Tuple.get: index out of bounds") @@ -173,7 +173,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(string.startsWith(prefix)) } static get(string, i) { - let scrut; /** scoped **/ + let scrut; scrut = i >= string.length; if (scrut === true) { throw globalThis.RangeError("Str.get: index out of bounds") @@ -209,7 +209,7 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let scrut, prev, tmp; /** scoped **/ + let scrut, prev, tmp; scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -221,7 +221,7 @@ globalThis.Object.freeze(class Runtime { } } static resetIndent(n) { - let scrut; /** scoped **/ + let scrut; scrut = TraceLogger.enabled; if (scrut === true) { TraceLogger.indentLvl = n; @@ -231,7 +231,7 @@ globalThis.Object.freeze(class Runtime { } } static log(msg) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + let scrut, tmp, tmp1, tmp2, tmp3, tmp4; scrut = TraceLogger.enabled; if (scrut === true) { tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); @@ -387,7 +387,7 @@ globalThis.Object.freeze(class Runtime { }) } delay() { - let lambda; /** scoped **/ + let lambda; lambda = (undefined, function (k) { Runtime.stackResume = k; return runtime.Unit @@ -409,13 +409,13 @@ globalThis.Object.freeze(class Runtime { } #v; zext() { - let tmp, tmp1; /** scoped **/ + let tmp, tmp1; tmp = Runtime.shl(1, 31); tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); return Runtime.bitand(this.#v, tmp1) } sext() { - let tmp; /** scoped **/ + let tmp; tmp = Runtime.shl(1, 31); return Runtime.bitor(this.#v, tmp) } @@ -427,7 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ + let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -437,7 +437,7 @@ globalThis.Object.freeze(class Runtime { }); scrut = runtime.short_or(tmp, lambda); if (scrut === true) { - let scrut1, scrut2, tmp12; /** scoped **/ + let scrut1, scrut2, tmp12; scrut1 = functionName.length > 0; if (scrut1 === true) { tmp12 = " '" + functionName; @@ -485,7 +485,7 @@ globalThis.Object.freeze(class Runtime { } } static deboundMethod(mtdName, clsName) { - let tmp, tmp1, tmp2, tmp3; /** scoped **/ + let tmp, tmp1, tmp2, tmp3; tmp = "[debinding error] Method '" + mtdName; tmp1 = tmp + "' of class '"; tmp2 = tmp1 + clsName; @@ -493,7 +493,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error(tmp3) } static try(f) { - let res; /** scoped **/ + let res; res = runtime.safeCall(f()); if (res instanceof Runtime.EffectSig.class) { return Runtime.EffectHandle(res) @@ -502,7 +502,7 @@ globalThis.Object.freeze(class Runtime { } } static printRaw(x) { - let rcd, tmp; /** scoped **/ + let rcd, tmp; rcd = globalThis.Object.freeze({ indent: 2, breakLength: 76 @@ -514,9 +514,9 @@ globalThis.Object.freeze(class Runtime { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } static topLevelEffect(tr, debug) { - let tmp, tmp1; /** scoped **/ + let tmp, tmp1; tmp2: while (true) { - let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + let scrut, tmp3, tmp4, tmp5, tmp6; scrut = tr.handler === Runtime.PrintStackEffect; if (scrut === true) { tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); @@ -539,23 +539,23 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ + let msg, curHandler, atTail, tmp, tmp1, tmp2; msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { - let tmp3; /** scoped **/ + let tmp3; tmp4: while (true) { - let scrut, cur, tmp5, tmp6; /** scoped **/ + let scrut, cur, tmp5, tmp6; scrut = curHandler !== null; if (scrut === true) { - let scrut1, tmp7, tmp8; /** scoped **/ + let scrut1, tmp7, tmp8; cur = curHandler.next; tmp9: while (true) { - let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; scrut2 = cur !== null; if (scrut2 === true) { - let scrut3, lambda, tmp19, tmp20; /** scoped **/ + let scrut3, lambda, tmp19, tmp20; locals = cur.getLocals; tmp10 = locals.length - 1; curLocals = runtime.safeCall(locals.at(tmp10)); @@ -572,7 +572,7 @@ globalThis.Object.freeze(class Runtime { scrut3 = curLocals.locals.length > 0; if (scrut3 === true) { lambda = (undefined, function (l) { - let tmp21, tmp22; /** scoped **/ + let tmp21, tmp22; tmp21 = l.localName + "="; tmp22 = Rendering.render(l.value); return tmp21 + tmp22 @@ -640,13 +640,13 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; if (cont instanceof Runtime.FunctionContFrame.class) { - let scrut, tmp5, tmp6, tmp7; /** scoped **/ + let scrut, tmp5, tmp6, tmp7; tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut1, tmp8, tmp9; /** scoped **/ + let scrut1, tmp8, tmp9; scrut1 = runtime.safeCall(m.has(cont)); if (scrut1 === true) { tmp8 = ", " + marker; @@ -660,7 +660,7 @@ globalThis.Object.freeze(class Runtime { tmp1 = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; /** scoped **/ + let scrut1; tmp5 = reps + 1; reps = tmp5; scrut1 = reps > 10; @@ -679,9 +679,9 @@ globalThis.Object.freeze(class Runtime { tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); return tmp3 + tmp4 } else { - let scrut; /** scoped **/ - scrut = cont === null; - if (scrut === true) { + let scrut2; + scrut2 = cont === null; + if (scrut2 === true) { return "(null)" } else { return "(NOT CONT)" @@ -689,12 +689,12 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ + let result, lambda, tmp, tmp1, tmp2, tmp3; if (cont instanceof Runtime.HandlerContFrame.class) { - let scrut, tmp4, tmp5, tmp6; /** scoped **/ + let scrut, tmp4, tmp5, tmp6; result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut1, tmp7, tmp8; /** scoped **/ + let scrut1, tmp7, tmp8; scrut1 = runtime.safeCall(m.has(cont)); if (scrut1 === true) { tmp7 = ", " + marker; @@ -708,7 +708,7 @@ globalThis.Object.freeze(class Runtime { tmp = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; /** scoped **/ + let scrut1; tmp4 = reps + 1; reps = tmp4; scrut1 = reps > 10; @@ -727,9 +727,9 @@ globalThis.Object.freeze(class Runtime { tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); return tmp2 + tmp3 } else { - let scrut; /** scoped **/ - scrut = cont === null; - if (scrut === true) { + let scrut2; + scrut2 = cont === null; + if (scrut2 === true) { return "(null)" } else { return "(NOT HANDLER CONT)" @@ -737,23 +737,23 @@ globalThis.Object.freeze(class Runtime { } } static debugCont(cont) { - let tmp, tmp1, tmp2; /** scoped **/ + let tmp, tmp1, tmp2; tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugHandler(cont) { - let tmp, tmp1, tmp2; /** scoped **/ + let tmp, tmp1, tmp2; tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; if (contTrace instanceof Runtime.ContTrace.class) { - let scrut, scrut1; /** scoped **/ + let scrut, scrut1; tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; if (scrut === true) { @@ -783,7 +783,7 @@ globalThis.Object.freeze(class Runtime { tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); cur = contTrace.nextHandler; tmp13: while (true) { - let scrut2, tmp14, tmp15; /** scoped **/ + let scrut2, tmp14, tmp15; scrut2 = cur !== null; if (scrut2 === true) { tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); @@ -803,7 +803,7 @@ globalThis.Object.freeze(class Runtime { } } static debugEff(eff) { - let tmp, tmp1, tmp2, tmp3; /** scoped **/ + let tmp, tmp1, tmp2, tmp3; if (eff instanceof Runtime.EffectSig.class) { tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); @@ -815,7 +815,7 @@ globalThis.Object.freeze(class Runtime { } } static mkEffect(handler, handlerFun) { - let res, tmp; /** scoped **/ + let res, tmp; tmp = new Runtime.ContTrace.class(null, null, null, null, false); res = new Runtime.EffectSig.class(tmp, handler, handlerFun); res.contTrace.last = res.contTrace; @@ -823,7 +823,7 @@ globalThis.Object.freeze(class Runtime { return res } static handleBlockImpl(cur, handler) { - let handlerFrame; /** scoped **/ + let handlerFrame; handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); cur.contTrace.lastHandler.nextHandler = handlerFrame; cur.contTrace.lastHandler = handlerFrame; @@ -831,7 +831,7 @@ globalThis.Object.freeze(class Runtime { return Runtime.handleEffects(cur) } static enterHandleBlock(handler, body) { - let cur; /** scoped **/ + let cur; cur = runtime.safeCall(body()); if (cur instanceof Runtime.EffectSig.class) { return Runtime.handleBlockImpl(cur, handler) @@ -840,11 +840,11 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let tmp; /** scoped **/ + let tmp; tmp1: while (true) { - let nxt, tmp2; /** scoped **/ + let nxt, tmp2; if (cur instanceof Runtime.EffectSig.class) { - let scrut; /** scoped **/ + let scrut; nxt = Runtime.handleEffect(cur); scrut = cur === nxt; if (scrut === true) { @@ -863,10 +863,10 @@ globalThis.Object.freeze(class Runtime { return tmp } static handleEffect(cur) { - let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; prevHandlerFrame = cur.contTrace; tmp6: while (true) { - let scrut1, scrut2; /** scoped **/ + let scrut1, scrut2; split_root$: { split_1$: { scrut1 = prevHandlerFrame.nextHandler !== null; @@ -903,17 +903,17 @@ globalThis.Object.freeze(class Runtime { tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); cur = tmp3; if (cur instanceof Runtime.EffectSig.class) { - let scrut1, scrut2; /** scoped **/ - scrut1 = saved.next !== null; - if (scrut1 === true) { + let scrut3, scrut4; + scrut3 = saved.next !== null; + if (scrut3 === true) { cur.contTrace.last.next = saved.next; cur.contTrace.last = saved.last; tmp4 = runtime.Unit; } else { tmp4 = runtime.Unit; } - scrut2 = saved.nextHandler !== null; - if (scrut2 === true) { + scrut4 = saved.nextHandler !== null; + if (scrut4 === true) { cur.contTrace.lastHandler.nextHandler = saved.nextHandler; cur.contTrace.lastHandler = saved.lastHandler; tmp5 = runtime.Unit; @@ -927,7 +927,7 @@ globalThis.Object.freeze(class Runtime { } static resume(contTrace) { return (value) => { - let scrut, tmp, tmp1; /** scoped **/ + let scrut, tmp, tmp1; scrut = contTrace.resumed; if (scrut === true) { throw globalThis.Error("Multiple resumption") @@ -940,19 +940,19 @@ globalThis.Object.freeze(class Runtime { } } static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, tmp; /** scoped **/ + let cont, handlerCont, curDepth, tmp; cont = contTrace.next; handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; tmp1: while (true) { - let tmp2, tmp3; /** scoped **/ + let tmp2, tmp3; if (cont instanceof Runtime.FunctionContFrame.class) { - let tmp4, tmp5; /** scoped **/ + let tmp4, tmp5; tmp2 = runtime.safeCall(cont.resume(value)); value = tmp2; Runtime.stackDepth = curDepth; if (value instanceof Runtime.EffectSig.class) { - let scrut, scrut1; /** scoped **/ + let scrut, scrut1; value.contTrace.last.next = cont.next; value.contTrace.lastHandler.nextHandler = handlerCont; scrut = contTrace.last !== cont; @@ -991,7 +991,7 @@ globalThis.Object.freeze(class Runtime { return tmp } static checkDepth() { - let scrut, tmp, lambda; /** scoped **/ + let scrut, tmp, lambda; tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -1004,14 +1004,14 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, tmp; /** scoped **/ + let result, tmp; Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); Runtime.stackDepth = 1; tmp1: while (true) { - let scrut, saved, tmp2; /** scoped **/ + let scrut, saved, tmp2; scrut = Runtime.stackResume !== null; if (scrut === true) { saved = Runtime.stackResume; @@ -1032,7 +1032,7 @@ globalThis.Object.freeze(class Runtime { return result } static plus_impl(lhs, rhs) { - let tmp; /** scoped **/ + let tmp; split_root$: { split_1$: { if (lhs instanceof Runtime.Int31.class) { diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index 07725a0283..da40af046e 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,7 +81,7 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 }; +//│ let f5; f5 = function f() { let tmp4; tmp4 = TrmMod(); return tmp4 }; :fixme :expect 42 @@ -92,7 +92,7 @@ f :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; /** scoped **/ f6 = function f() { return TrmMod.class }; +//│ let f6; f6 = function f() { return TrmMod.class }; fun assertModule(module m: ClsMod): module ClsMod = m diff --git a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls index 18ba41070d..630a7d1107 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls @@ -31,7 +31,7 @@ fun foo = :sjs foo //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = foo2(); tmp2 +//│ let tmp2; tmp2 = foo2(); tmp2 //│ > 1 //│ > ... diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 9ac7a2e8e9..71e5c6cda1 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -64,7 +64,7 @@ Infinity :sjs val Infinity = 1 //│ JS (unsanitized): -//│ let Infinity; /** scoped **/ Infinity = 1; +//│ let Infinity; Infinity = 1; //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Identifier 'Infinity' has already been declared //│ Infinity = Infinity @@ -105,7 +105,7 @@ fun main() = object Cls(val x) with fun huh = x //│ JS (unsanitized): -//│ let Cls1; /** scoped **/ +//│ let Cls1; //│ globalThis.Object.freeze(class Cls { //│ static { //│ Cls1.class = globalThis.Object.freeze(new this) @@ -202,7 +202,7 @@ class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let D1; /** scoped **/ +//│ let D1; //│ globalThis.Object.freeze(class D extends Predef.id { //│ static { //│ D1 = this @@ -251,11 +251,9 @@ set (x += 1; ()) fun baz = 1 fun bar() = baz //│ JS (unsanitized): -//│ let bar, baz; /** scoped **/ -//│ baz = function baz() { -//│ return 1 -//│ }; -//│ bar = function bar() { let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 }; +//│ let bar, baz; +//│ baz = function baz() { return 1 }; +//│ bar = function bar() { let tmp4; tmp4 = baz(); return tmp4 }; // ——— ——— ——— @@ -264,13 +262,13 @@ import "../../mlscript-compile/Stack.mls" // The parser rejects this, but it should be allowed. open Stack { ::, Nil } //│ ╔══[ERROR] Illegal 'open' statement shape. -//│ ║ l.265: open Stack { ::, Nil } +//│ ║ l.263: open Stack { ::, Nil } //│ ╙── ^^^^^^^^^^^^^^^ // Instead, if we parenthesize the operator, it is rejected by the elaborator. open Stack { (::), Nil } //│ ╔══[ERROR] Illegal 'open' statement element. -//│ ║ l.271: open Stack { (::), Nil } +//│ ║ l.269: open Stack { (::), Nil } //│ ╙── ^^^^ open Stack { Nil, :: } @@ -278,7 +276,7 @@ open Stack { Nil, :: } :sjs 1 :: Nil //│ ╔══[ERROR] Module 'Stack' does not contain member '::' -//│ ║ l.279: 1 :: Nil +//│ ║ l.277: 1 :: Nil //│ ╙── ^^ //│ JS (unsanitized): //│ Stack["::"](1, Stack.Nil) @@ -289,9 +287,9 @@ open Stack { Nil, :: } mkStr of ... // hello "oops" //│ ╔══[PARSE ERROR] Expected start of expression in this position; found new line instead -//│ ║ l.289: mkStr of ... // hello +//│ ║ l.287: mkStr of ... // hello //│ ║ ^ -//│ ║ l.290: "oops" +//│ ║ l.288: "oops" //│ ╙── //│ = "oops" @@ -314,25 +312,25 @@ Foo(1, 2, 3).args :todo if Foo(1, 2, 3) is Foo(...args) then args //│ ╔══[ERROR] Unrecognized pattern (spread). -//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^^^^ //│ ╔══[ERROR] Name not found: args -//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^ //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╔══[ERROR] The constructor does not take any arguments but found three arguments. -//│ ║ l.327: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] +//│ ║ l.325: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(arg) then arg //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.333: if Foo(1, 2, 3) is Foo(arg) then arg +//│ ║ l.331: if Foo(1, 2, 3) is Foo(arg) then arg //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error @@ -354,7 +352,7 @@ fun w(txt) = fs.writeFileSync(outFilePath, txt) // () //│ JS (unsanitized): -//│ let w; /** scoped **/ w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; +//│ let w; w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; w("whoops") //│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value. @@ -366,10 +364,10 @@ fun foo() = let bar(x: Str): Str = x + x bar("test") //│ ╔══[ERROR] Unsupported let binding shape -//│ ║ l.366: let bar(x: Str): Str = x + x +//│ ║ l.364: let bar(x: Str): Str = x + x //│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Expected 0 arguments, got 1 -//│ ║ l.367: bar("test") +//│ ║ l.365: bar("test") //│ ╙── ^^^^^^^^ // ——— ——— ——— @@ -379,10 +377,10 @@ fun foo() = module Foo(x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.379: module Foo(x) +//│ ║ l.377: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -403,10 +401,10 @@ module Foo(x) module Foo(val x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.403: module Foo(val x) +//│ ║ l.401: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ globalThis.Object.freeze(class Foo6 { //│ static { //│ Foo7 = this @@ -426,7 +424,7 @@ module Foo(val x) // TODO support syntax? data class Foo(mut x) //│ ╔══[ERROR] Expected a valid parameter, found 'mut'-modified identifier -//│ ║ l.427: data class Foo(mut x) +//│ ║ l.425: data class Foo(mut x) //│ ╙── ^ // ——— ——— ——— @@ -443,10 +441,10 @@ set this.pc = 0 set (this).pc = 0 //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.442: set this.pc = 0 +//│ ║ l.440: set this.pc = 0 //│ ╙── ^^^^ //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.444: (this).pc = 0 +//│ ║ l.442: (this).pc = 0 //│ ╙── ^^^^ // But this doesn't... @@ -455,10 +453,10 @@ set set this.pc = 0 //│ ╔══[PARSE ERROR] Unexpected static selector here -//│ ║ l.456: this.pc = 0 +//│ ║ l.454: this.pc = 0 //│ ╙── ^^^ //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.456: this.pc = 0 +//│ ║ l.454: this.pc = 0 //│ ╙── ^^^^ // ——— ——— ——— @@ -470,7 +468,7 @@ pattern A(pattern B) = B fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ║ l.471: fun foo(x) = if x is @annotations.compile A(0) as y then y +//│ ║ l.469: fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╙── ^ // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls index 8c99b7c926..037ca2f59d 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls @@ -15,7 +15,7 @@ x :sjs val ++ = 0 //│ JS (unsanitized): -//│ let $_$_; /** scoped **/ $_$_ = 0; +//│ let $_$_; $_$_ = 0; //│ ++ = 0 :sjs @@ -51,7 +51,7 @@ fun ++ z = 0 :sjs ++ //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = $_$_2(); tmp +//│ let tmp; tmp = $_$_2(); tmp //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 1dc6b6f2b7..4982f7cc87 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1, tmp1, tmp2; /** scoped **/ +//│ let tmp1, Foo1, tmp2; //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index df691692ad..0a8b33ff10 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index ce93f69fb6..cce678bd28 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; /** scoped **/ tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; /** scoped **/ +//│ let a, b, tmp2, tmp3; //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a @@ -23,9 +23,9 @@ drop { a: 0, b: 1 } let discard = drop _ //│ JS (unsanitized): //│ let discard; -//│ let discard1; /** scoped **/ -//│ discard = function discard(_0) { return runtime.Unit }; -//│ discard1 = discard; +//│ let discard1; +//│ discard1 = function discard(_0) { return runtime.Unit }; +//│ discard = discard1; //│ discard = fun discard discard of { a: 0, b: 1 } diff --git a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls index 708cfa4897..d91b3fba16 100644 --- a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls +++ b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls @@ -6,7 +6,7 @@ { a: 1 } //│ JS (unsanitized): -//│ let a; /** scoped **/ a = 1; globalThis.Object.freeze({ a: a }) +//│ let a; a = 1; globalThis.Object.freeze({ a: a }) //│ = {a: 1} { "a": 1 } @@ -35,7 +35,7 @@ :sjs { (id(0)): 1 } //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) +//│ let tmp; tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) //│ = {"0": 1} diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index 7f9f887e0c..6908159775 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,7 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let Baz1, tmp; /** scoped **/ +//│ let tmp, Baz1; //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index dd9ce4d58f..26fb4b1450 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -17,11 +17,11 @@ fun buildPalindrome = case 0 then [] n then [n, ..buildPalindrome(n - 1), n] //│ JS (unsanitized): -//│ let buildPalindrome; /** scoped **/ +//│ let buildPalindrome; //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; /** scoped **/ +//│ let n, tmp3, tmp4, tmp5; //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -48,11 +48,11 @@ fun sum2 = case [] then 0 [x, ..xs, y] then x + y + sum2(xs) //│ JS (unsanitized): -//│ let sum2; /** scoped **/ +//│ let sum2; //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls index 1464825716..39b9941e38 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls @@ -46,7 +46,7 @@ M.Foo:: n(foo, 2) :sjs let m = M.Foo::m //│ JS (unsanitized): -//│ let m; let m1; /** scoped **/ m = function m(self, ...args) { return self.m(...args) }; m1 = m; +//│ let m; let m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; //│ m = fun m m(foo) @@ -132,7 +132,7 @@ Foo::n(foo, 2) //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ═══[ERROR] Expected a statically known class; found ‹error›. //│ JS (unsanitized): -//│ let lambda9; /** scoped **/ +//│ let lambda9; //│ lambda9 = (undefined, function (self, ...args) { //│ return runtime.safeCall(self.n(...args)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls index 4cffa34572..2c3bbe2920 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls @@ -54,7 +54,7 @@ xs \ //│ ║ l.52: map(x => x * 2) //│ ╙── ^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let lambda5, tmp4; /** scoped **/ +//│ let lambda5, tmp4; //│ lambda5 = (undefined, function (x) { return x * 2 }); //│ tmp4 = map(lambda5); //│ Predef.passTo(xs1, tmp4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index dae4535fa9..c76804e25b 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -78,13 +78,13 @@ Foo(1, 2) let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): //│ let f; -//│ let f1; /** scoped **/ -//│ f = function f(x, y) { -//│ let tmp4; /** scoped **/ +//│ let f1; +//│ f1 = function f(x, y) { +//│ let tmp4; //│ tmp4 = Foo7(x, y); //│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) //│ }; -//│ f1 = f; +//│ f = f1; //│ f = fun f // Eta-expansion happens here. See EtaExpansion.mls. diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 9c2202fc04..178203f9e4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -6,7 +6,7 @@ fun f(n1: Int): Int = n1 //│ JS (unsanitized): -//│ let f; /** scoped **/ f = function f(n1) { return n1 }; +//│ let f; f = function f(n1) { return n1 }; f(42) //│ JS (unsanitized): @@ -19,24 +19,23 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f(n1) { return (n2) => { let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } }; +//│ let f1; f1 = function f(n1) { return (n2) => { let tmp; tmp = 10 * n1; return tmp + n2 } }; // TODO compile this to // this.f$(4, 2) f(4)(2) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = f1(4); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = f1(4); runtime.safeCall(tmp(2)) //│ = 42 fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ let tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, tmp2, tmp3; //│ tmp1 = 10 * n1; //│ tmp2 = tmp1 + n2; //│ tmp3 = 10 * tmp2; @@ -47,20 +46,17 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ -//│ tmp1 = f2(4); -//│ tmp2 = runtime.safeCall(tmp1(2)); -//│ runtime.safeCall(tmp2(0)) +//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { //│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; //│ tmp3 = 10 * n1; //│ tmp4 = tmp3 + n2; //│ tmp5 = 10 * tmp4; @@ -74,7 +70,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; /** scoped **/ +//│ let tmp3, tmp4, tmp5; //│ tmp3 = f3(3); //│ tmp4 = runtime.safeCall(tmp3(0)); //│ tmp5 = runtime.safeCall(tmp4(3)); diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index aa73bdb0d9..5025320ce4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,7 +80,7 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; /** scoped **/ +//│ let scrut, tmp17; //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls index d95bc500e7..58a398ea87 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls @@ -4,7 +4,7 @@ :sjs let t = [0, 1, 2] //│ JS (unsanitized): -//│ let t; /** scoped **/ t = globalThis.Object.freeze([ 0, 1, 2 ]); +//│ let t; t = globalThis.Object.freeze([ 0, 1, 2 ]); //│ t = [0, 1, 2] :re @@ -21,7 +21,7 @@ t :sjs let t = mut [0, 1, 2] //│ JS (unsanitized): -//│ let t1; /** scoped **/ t1 = [ 0, 1, 2 ]; +//│ let t1; t1 = [ 0, 1, 2 ]; //│ t = [0, 1, 2] t.push(4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls index 7efae81a91..0c8a48f3a4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls @@ -46,7 +46,7 @@ r.foo :sjs let r = {} //│ JS (unsanitized): -//│ let r3; /** scoped **/ r3 = runtime.Unit; +//│ let r3; r3 = runtime.Unit; //│ r = () :re diff --git a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls index 655c4d0a45..408d7db105 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls @@ -33,7 +33,7 @@ O.gy :sjs class Foo(x, val y, mut val z) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x, y, z) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index 790005a3c5..660664e019 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo()); +//│ let f; f = globalThis.Object.freeze(new Foo()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = new Foo(); +//│ let f1; f1 = new Foo(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index e1e6e3a5f0..72c154d117 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -109,19 +109,16 @@ passTo(1, add(., 1) @ _ + _)(2) :sjs let f = add(_, 1) //│ JS (unsanitized): -//│ let f; -//│ let f1; /** scoped **/ -//│ f = function f(_0) { let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) }; -//│ f1 = f; +//│ let f; let f1; f1 = function f(_0) { let tmp15; tmp15 = add(); return tmp15(_0, 1) }; f = f1; //│ f = fun f :fixme let f = add(., 1) //│ ╔══[PARSE ERROR] Expected an expression; found period instead -//│ ║ l.119: let f = add(., 1) +//│ ║ l.116: let f = add(., 1) //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected period here -//│ ║ l.119: let f = add(., 1) +//│ ║ l.116: let f = add(., 1) //│ ╙── ^ //│ ═══[RUNTIME ERROR] Error: Function expected 2 arguments but got 0 //│ f = undefined diff --git a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls index c9d079ed1c..36ad46feac 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls @@ -56,7 +56,7 @@ fun foo() = :sjs 1; id(2) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.id(2); (1 , tmp) +//│ let tmp; tmp = Predef.id(2); (1 , tmp) //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index 02207fb129..df1eb55c0e 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -4,9 +4,7 @@ :sjs 1 && 2 //│ JS (unsanitized): -//│ let lambda; /** scoped **/ -//│ lambda = (undefined, function () { return 2 }); -//│ runtime.short_and(1, lambda) +//│ let lambda; lambda = (undefined, function () { return 2 }); runtime.short_and(1, lambda) //│ = 2 1 || 2 @@ -20,9 +18,7 @@ fun test(x) = :sjs 123 || test(42) //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ -//│ lambda2 = (undefined, function () { return test(42) }); -//│ runtime.short_or(123, lambda2) +//│ let lambda2; lambda2 = (undefined, function () { return test(42) }); runtime.short_or(123, lambda2) //│ = 123 0 || test(42) @@ -36,9 +32,9 @@ fun test(x) = :sjs fold(||)(0, false, 42, 123) //│ JS (unsanitized): -//│ let lambda5, tmp; /** scoped **/ +//│ let lambda5, tmp; //│ lambda5 = (undefined, function (arg1, arg2) { -//│ let lambda6; /** scoped **/ +//│ let lambda6; //│ lambda6 = (undefined, function () { //│ return arg2 //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index b9d1163e58..666d7e6f4c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } +//│ let scrut; scrut = "a"; if (typeof scrut === 'string') { true } else { false } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index dcb0d0ba4b..22d75bb559 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -62,9 +62,9 @@ print(_) let test = _.f(0, _, 2) //│ JS (unsanitized): //│ let test; -//│ let test1; /** scoped **/ -//│ test = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; -//│ test1 = test; +//│ let test1; +//│ test1 = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; +//│ test = test1; //│ test = fun test :re diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 01726b0d7d..e3195424de 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -31,7 +31,7 @@ :sjs let x = 1 in x + 1 //│ JS (unsanitized): -//│ let x; /** scoped **/ x = 1; x + 1 +//│ let x; x = 1; x + 1 //│ = 2 //│ Type: Int @@ -61,7 +61,7 @@ false :sjs data class Foo(x: Int) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x1) { //│ return globalThis.Object.freeze(new Foo.class(x1)); //│ }; @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo.class(42)); foo.x +//│ let foo; foo = globalThis.Object.freeze(new Foo.class(42)); foo.x //│ = 42 //│ Type: Int @@ -97,7 +97,7 @@ let foo = new Foo(42) in foo.Foo#x :sjs fun inc(x) = x + 1 //│ JS (unsanitized): -//│ let inc; /** scoped **/ inc = function inc(x1) { return x1 + 1 }; +//│ let inc; inc = function inc(x1) { return x1 + 1 }; //│ Type: ⊤ @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } +//│ let scrut; scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } +//│ let scrut1; scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } //│ = 1 //│ Type: Int @@ -134,7 +134,7 @@ data class Foo() let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ foo1 = globalThis.Object.freeze(new Foo2.class()); //│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } //│ = 1 @@ -147,11 +147,11 @@ fun pow(x) = case 0 then 1 n then x * pow(x)(n-1) //│ JS (unsanitized): -//│ let pow; /** scoped **/ +//│ let pow; //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; /** scoped **/ +//│ let n, tmp, tmp1, tmp2; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -172,7 +172,7 @@ fun nott = case true then false false then true //│ JS (unsanitized): -//│ let nott; /** scoped **/ +//│ let nott; //│ nott = function nott() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { @@ -193,7 +193,7 @@ fun nott = case :sjs nott of false //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = nott(); tmp(false) +//│ let tmp; tmp = nott(); tmp(false) //│ = true //│ Type: Bool @@ -203,11 +203,11 @@ fun fact = case 0 then 1 n then n * fact(n - 1) //│ JS (unsanitized): -//│ let fact; /** scoped **/ +//│ let fact; //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; /** scoped **/ +//│ let n, tmp1, tmp2, tmp3; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -258,8 +258,8 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): +//│ let y; //│ let x4; -//│ let y; /** scoped **/ //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); //│ y.value @@ -270,8 +270,8 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): +//│ let y1; //│ let x5; -//│ let y1; /** scoped **/ //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); //│ y1.value = 0; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 87c687f79b..48f541fb92 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -87,28 +87,28 @@ fun test2() = //│ ║ l.83: case 0 then 0 //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let test22; /** scoped **/ +//│ let test22; //│ test22 = function test2() { -//│ let funny, tmp1; /** scoped **/ +//│ let funny, tmp1; //│ funny = function funny() { //│ let lambda; -//│ let lambda1; /** scoped **/ -//│ lambda1 = (undefined, function (caseScrut) { +//│ let lambda1; +//│ lambda = (undefined, function (caseScrut) { //│ if (caseScrut === 0) { //│ return 0 //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ }); -//│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp2, tmp3, tmp4; /** scoped **/ +//│ lambda1 = (undefined, function (caseScrut) { +//│ let n, tmp2, tmp3, tmp4; //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; //│ tmp4 = tmp2(tmp3); //│ return tmp4 + 1 //│ }); -//│ return lambda +//│ return lambda1 //│ }; //│ tmp1 = funny(); //│ return tmp1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index d7ff1e8b8d..9cb5c5210c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -11,7 +11,7 @@ empty.0 :sjs let single = [1] //│ JS (unsanitized): -//│ let single; /** scoped **/ single = globalThis.Object.freeze([ 1 ]); +//│ let single; single = globalThis.Object.freeze([ 1 ]); //│ single = [1] single.0 @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls index 0d443fdd05..ce774c920f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls @@ -9,7 +9,7 @@ object Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar1; /** scoped **/ +//│ let Bar1; //│ globalThis.Object.freeze(class Bar { //│ static { //│ Bar1 = globalThis.Object.freeze(new this) @@ -49,7 +49,7 @@ module Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar3; /** scoped **/ +//│ let Bar3; //│ globalThis.Object.freeze(class Bar2 { //│ static { //│ Bar3 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls index 454e29826f..ed25930a28 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls @@ -29,7 +29,7 @@ new 2 + 2 :re new! 2 + 2 //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 +//│ let tmp1; tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 //│ ═══[RUNTIME ERROR] TypeError: 2 is not a constructor :e diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index eb9457bb23..7bf650836b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -39,7 +39,7 @@ print("Hi") print("Hi") 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.print("Hi"); 2 +//│ let tmp; tmp = Predef.print("Hi"); 2 //│ Lowered: //│ Program: //│ imports = Nil @@ -60,7 +60,6 @@ print("Hi") //│ rest = Return: \ //│ res = Lit of IntLit of 2 //│ implct = true -//│ topLevel = true //│ > Hi //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls index 70dc34b630..51390245ed 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls @@ -74,7 +74,7 @@ id(+)(1, 2) :re id(+)(1) //│ JS (unsanitized): -//│ let lambda4, tmp1; /** scoped **/ +//│ let lambda4, tmp1; //│ lambda4 = (undefined, function (arg1, arg2) { //│ return arg1 + arg2 //│ }); @@ -95,7 +95,7 @@ fun (+) lol(a, b) = [a, b] :sjs id(~)(2) //│ JS (unsanitized): -//│ let lambda5, tmp2; /** scoped **/ +//│ let lambda5, tmp2; //│ lambda5 = (undefined, function (arg) { //│ return ~ arg //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 8be8e2459a..6bc047ff4f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -15,9 +15,9 @@ fun test(x) = Some(v) then print(v) None then print("none") //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; //│ if (x instanceof Some.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 2c5e5cee00..3a0f89e47f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -7,13 +7,7 @@ case x then x :sjs case { x then x } //│ JS (unsanitized): -//│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) { -//│ let x; /** scoped **/ -//│ x = caseScrut; -//│ return x -//│ }); -//│ lambda1 +//│ let lambda1; lambda1 = (undefined, function (caseScrut) { let x; x = caseScrut; return x }); lambda1 //│ = fun :sjs @@ -59,10 +53,10 @@ case 0 then true :todo // TODO: support this braceless syntax? case [x] then x, [] then 0 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.60: case [x] then x, [] then 0 +//│ ║ l.54: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^ //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.60: case [x] then x, [] then 0 +//│ ║ l.54: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^^^^^^^ :sjs @@ -85,7 +79,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let isDefined, lambda12; /** scoped **/ +//│ let lambda12, isDefined; //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index 67640a68bf..e633f026e7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,7 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let Outer1, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, Outer1; //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -60,7 +60,7 @@ data class Outer(a, b) with //│ return this.Inner(c) //│ } //│ o2(c, d) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ tmp6 = this.Inner(c); //│ return tmp6.i1(d) //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index 696a1e22bd..b5c1b26b34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -6,9 +6,9 @@ fun test(a) = class C with { val x = a } new C //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test(a) { -//│ let C1; /** scoped **/ +//│ let C1; //│ globalThis.Object.freeze(class C { //│ static { //│ C1 = this @@ -37,9 +37,9 @@ fun test(x) = data class Foo(a, b) Foo(x, x + 1) //│ JS (unsanitized): -//│ let test2; /** scoped **/ +//│ let test2; //│ test2 = function test(x) { -//│ let Foo2, tmp; /** scoped **/ +//│ let Foo2, tmp; //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index b4df883b66..0e8998d346 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,7 +9,7 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; /** scoped **/ +//│ let scrut, x, argument0$; //│ scrut = Some(0); //│ if (scrut instanceof Some.class) { //│ argument0$ = scrut.value; @@ -26,7 +26,7 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ let x1, argument0$1; /** scoped **/ +//│ let x1, argument0$1; //│ if (s instanceof Some.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; @@ -55,7 +55,7 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; /** scoped **/ +//│ let x4, argument0$4; //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -118,9 +118,9 @@ fun f(x) = if x is None then "ok" else print("oops") //│ JS (unsanitized): -//│ let f4; /** scoped **/ +//│ let f4; //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ +//│ let x4, scrut1, argument0$4, tmp6; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { @@ -164,9 +164,9 @@ fun f(x) = if x is Some(u) then u Pair(a, b) then a + b //│ JS (unsanitized): -//│ let f5; /** scoped **/ +//│ let f5; //│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; /** scoped **/ +//│ let u, a, b, argument0$4, argument1$; //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; @@ -194,9 +194,9 @@ fun f(x) = print of if x is None then "ok" else "oops" //│ JS (unsanitized): -//│ let f6; /** scoped **/ +//│ let f6; //│ f6 = function f(x3) { -//│ let argument0$4, tmp10; /** scoped **/ +//│ let argument0$4, tmp10; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls index fc79ff4035..1c596f3fc1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls @@ -6,7 +6,7 @@ data class Foo(arguments) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(arguments1) { //│ return globalThis.Object.freeze(new Foo.class(arguments1)); //│ }; @@ -24,7 +24,7 @@ data class Foo(arguments) data class Foo(eval) //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ Foo3 = function Foo(eval1) { //│ return globalThis.Object.freeze(new Foo.class(eval1)); //│ }; @@ -42,7 +42,7 @@ data class Foo(eval) data class Foo(static) //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ Foo5 = function Foo(static1) { //│ return globalThis.Object.freeze(new Foo.class(static1)); //│ }; @@ -60,7 +60,7 @@ data class Foo(static) data class Foo(v') //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ Foo7 = function Foo(v$_) { //│ return globalThis.Object.freeze(new Foo.class(v$_)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index af16010767..7ba95074d3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -7,27 +7,17 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ f = function f() { -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 -//│ }; +//│ let f; +//│ f = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f() { -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 -//│ }; +//│ let f1; +//│ f1 = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; -//│ 42 +//│ let f2; f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; 42 //│ = 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index 1770b21e64..e3840f5a0c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,14 +7,14 @@ :silent declare val console //│ JS (unsanitized): -//│ let console; /** scoped **/ +//│ let console; console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ tmp = runtime.safeCall(globalThis.console.log("a")); //│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 @@ -25,7 +25,7 @@ console.log("b") let l = console.log l(123) //│ JS (unsanitized): -//│ let l; /** scoped **/ l = globalThis.console.log; runtime.safeCall(l(123)) +//│ let l; l = globalThis.console.log; runtime.safeCall(l(123)) //│ > 123 //│ l = fun log @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3, tmp4; /** scoped **/ +//│ let tmp2, x, tmp3, tmp4, y; //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index 421a23afac..9d733318be 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; /** scoped **/ x = undefined; +//│ let x; x = undefined; //│ x = undefined x = 1 @@ -31,7 +31,7 @@ x let y = 1 //│ JS (unsanitized): -//│ let y; /** scoped **/ y = 1; +//│ let y; y = 1; //│ y = 1 :e @@ -45,7 +45,7 @@ z = 1 fun f() = 1 //│ JS (unsanitized): -//│ let f; /** scoped **/ f = function f() { return 1 }; +//│ let f; f = function f() { return 1 }; f //│ JS (unsanitized): @@ -56,7 +56,7 @@ f let f f(x) = x + 1 //│ JS (unsanitized): -//│ let f1; let f2; /** scoped **/ f1 = function f(x1) { return x1 + 1 }; f2 = f1; +//│ let f1; let f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; //│ f = fun f f(1) @@ -68,7 +68,7 @@ f(1) let foo foo = 0 //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = 0; +//│ let foo; foo = 0; //│ foo = 0 :fixme @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; /** scoped **/ +//│ let foo1, scrut; //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; /** scoped **/ +//│ let foo2, scrut1; //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -105,7 +105,7 @@ else fun f() = foo = 42 //│ JS (unsanitized): -//│ let f3; /** scoped **/ f3 = function f() { foo2 = 42; return runtime.Unit }; +//│ let f3; f3 = function f() { foo2 = 42; return runtime.Unit }; f() //│ JS (unsanitized): @@ -123,6 +123,6 @@ fun f() = foo = 0 //│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let f4; /** scoped **/ f4 = function f() { return foo2 }; +//│ let f4; f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 1c439570a2..797b5c0c47 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -18,9 +18,9 @@ fun f(x) = return 0 x + 1 //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ f1 = function f(x) { -//│ let scrut, tmp, tmp1; /** scoped **/ +//│ let scrut, tmp, tmp1; //│ scrut = x < 0; //│ if (scrut === true) { //│ tmp = Predef.print("whoops"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls index 548e76af38..5f13db5813 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FieldSymbols.mls @@ -141,7 +141,6 @@ case //│ spread = N //│ value = Lit of StrLit of "match error" //│ rest = End of "" -//│ topLevel = false //│ rest = Return: \ //│ res = Ref: //│ l = member:lambda diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index e353c2a33e..e1c8f61c3f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -8,9 +8,9 @@ let discard = drop _ //│ JS (unsanitized): //│ let discard; -//│ let discard1; /** scoped **/ -//│ discard = function discard(_0) { return runtime.Unit }; -//│ discard1 = discard; +//│ let discard1; +//│ discard1 = function discard(_0) { return runtime.Unit }; +//│ discard = discard1; //│ discard = fun discard @@ -18,7 +18,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; /** scoped **/ +//│ let a, b, tmp; //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -26,7 +26,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; /** scoped **/ +//│ let a1, b1, tmp1; //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -35,14 +35,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; /** scoped **/ +//│ let aaaa, tmp2; //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let aaaaaaaaa, bbbb, tmp3; /** scoped **/ +//│ let bbbb, tmp3, aaaaaaaaa; //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -53,7 +53,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; /** scoped **/ +//│ let ccccccccc, dddddddddd, tmp4, aaaaaaaaa1, bbbbbbbb; //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -69,7 +69,7 @@ discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ tmp5 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb" @@ -78,7 +78,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ] //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ tmp6 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", @@ -89,7 +89,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; /** scoped **/ +//│ let tmp7, tmp8; //│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 1078c18fd7..590e01ab7e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -19,9 +19,9 @@ fun test(a) = h(d) Inner(42) //│ JS (unsanitized): -//│ let test1; /** scoped **/ +//│ let test1; //│ test1 = function test(a) { -//│ let Inner1, tmp; /** scoped **/ +//│ let Inner1, tmp; //│ Inner1 = function Inner(b) { //│ return globalThis.Object.freeze(new Inner.class(b)); //│ }; @@ -41,7 +41,7 @@ fun test(a) = //│ ]) //│ } //│ g(d) { -//│ let h; /** scoped **/ +//│ let h; //│ const this$Inner = this; //│ h = function h(e) { //│ return globalThis.Object.freeze([ @@ -78,9 +78,9 @@ fun test(a) = print of [a, b] [C1(1), C2(2)] //│ JS (unsanitized): -//│ let test2; /** scoped **/ +//│ let test2; //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let C11, C21, tmp, tmp1, tmp2, tmp3; //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -138,7 +138,7 @@ data class Foo(a) with foo() Foo(123) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -151,7 +151,7 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz, tmp; /** scoped **/ +//│ let bar, baz, tmp; //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a @@ -177,7 +177,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1, tmp; /** scoped **/ +//│ let Bar1, tmp; //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -192,7 +192,7 @@ Bar(1) //│ } //│ foo() { //│ return () => { -//│ let bar; /** scoped **/ +//│ let bar; //│ const this$Bar = this; //│ bar = function bar() { //│ return this$Bar.x diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index e3460c4b18..2056e412ce 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -6,14 +6,14 @@ val x = 2 fun foo() = x + 1 //│ JS (unsanitized): -//│ let foo, x; /** scoped **/ foo = function foo() { return x + 1 }; x = 2; +//│ let foo, x; foo = function foo() { return x + 1 }; x = 2; //│ x = 2 :sjs class Test with print(foo()) //│ JS (unsanitized): -//│ let Test1, tmp; /** scoped **/ +//│ let tmp, Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 23e4c2f5d8..47705c69c8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -4,14 +4,14 @@ :sjs fun t = 42 //│ JS (unsanitized): -//│ let t; /** scoped **/ t = function t() { return 42 }; +//│ let t; t = function t() { return 42 }; :expect 42 :sjs t //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = t(); tmp +//│ let tmp; tmp = t(); tmp //│ = 42 @@ -37,11 +37,11 @@ fun test() = 42 whoops + whoops //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test() { -//│ let whoops, tmp1, tmp2; /** scoped **/ +//│ let whoops, tmp1, tmp2; //│ whoops = function whoops() { -//│ let tmp3; /** scoped **/ +//│ let tmp3; //│ tmp3 = Predef.print("ok"); //│ return 42 //│ }; @@ -65,7 +65,7 @@ module T with val c = p val d = this.p //│ JS (unsanitized): -//│ let T1; /** scoped **/ +//│ let T1; //│ globalThis.Object.freeze(class T { //│ static { //│ T1 = this @@ -107,7 +107,7 @@ T.d module M with fun t = 0 //│ JS (unsanitized): -//│ let M1; /** scoped **/ +//│ let M1; //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -135,9 +135,9 @@ fun test() = fun whoops = 42 whoops //│ JS (unsanitized): -//│ let test1; /** scoped **/ +//│ let test1; //│ test1 = function test() { -//│ let whoops, tmp1; /** scoped **/ +//│ let whoops, tmp1; //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -163,8 +163,7 @@ fun bar() = fun baz() = 42 baz //│ JS (unsanitized): -//│ let bar; /** scoped **/ -//│ bar = function bar() { let baz; /** scoped **/ baz = function baz() { return 42 }; return baz }; +//│ let bar; bar = function bar() { let baz; baz = function baz() { return 42 }; return baz }; :sjs @@ -173,10 +172,10 @@ fun baz() = fun z = 2 (x, y) => x + y + w + z //│ JS (unsanitized): -//│ let baz; /** scoped **/ +//│ let baz; //│ baz = function baz() { +//│ let w, z; //│ let lambda; -//│ let w, z; /** scoped **/ //│ w = function w() { //│ return 1 //│ }; @@ -184,7 +183,7 @@ fun baz() = //│ return 2 //│ }; //│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ let tmp1, tmp2, tmp3, tmp4; //│ tmp1 = x + y; //│ tmp2 = w(); //│ tmp3 = tmp1 + tmp2; @@ -207,14 +206,14 @@ fun a() = b + d c //│ JS (unsanitized): -//│ let a; /** scoped **/ +//│ let a; //│ a = function a() { -//│ let b, c; /** scoped **/ +//│ let b, c; //│ b = function b() { //│ return 1 //│ }; //│ c = function c() { -//│ let d, tmp2, tmp3; /** scoped **/ +//│ let d, tmp2, tmp3; //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -236,14 +235,14 @@ fun b() = c d //│ JS (unsanitized): -//│ let b; /** scoped **/ +//│ let b; //│ b = function b() { -//│ let c, d; /** scoped **/ +//│ let c, d; //│ c = function c() { //│ return 1 //│ }; //│ d = function d() { -//│ let c1, tmp3; /** scoped **/ +//│ let c1, tmp3; //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -264,14 +263,14 @@ fun c() = e + f d //│ JS (unsanitized): -//│ let c; /** scoped **/ +//│ let c; //│ c = function c() { -//│ let d, f, tmp4; /** scoped **/ +//│ let d, f, tmp4; //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e, tmp5, tmp6; /** scoped **/ +//│ let e, tmp5, tmp6; //│ e = function e() { //│ return 1 //│ }; @@ -292,7 +291,7 @@ c() data class Foo(x) with fun oops = x //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index 7c40714263..883fdd4d3e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,7 +21,7 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = false; //│ if (scrut === true) { //│ 0 @@ -37,9 +37,9 @@ fun foo() = if false then 0 foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo() { -//│ let scrut1; /** scoped **/ +//│ let scrut1; //│ scrut1 = false; //│ if (scrut1 === true) { //│ return 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index 10433bdb39..cf22d5043d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -10,7 +10,7 @@ object Test with print(Test) Test.x //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = globalThis.Object.freeze(new this) @@ -22,7 +22,7 @@ object Test with //│ }) //│ } //│ foo() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = Predef.print(Test1); //│ return Test1.x //│ } @@ -46,7 +46,7 @@ Test.foo() :sjs val Test = "oops" //│ JS (unsanitized): -//│ let Test2; /** scoped **/ Test2 = "oops"; +//│ let Test2; Test2 = "oops"; //│ Test = "oops" :re @@ -60,13 +60,7 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let f; -//│ let x, f1, x1; /** scoped **/ -//│ x = 1; -//│ f = function f() { return x }; -//│ f1 = f; -//│ x1 = 2; -//│ runtime.safeCall(f1()) +//│ let x, f, x1; let f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) //│ = 1 //│ f = fun f //│ x = 2 @@ -78,10 +72,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.79: let x = 2 +//│ ║ l.73: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.78: val x = 1 +//│ ║ l.72: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test4 { @@ -121,7 +115,7 @@ data class Cls(x) with fun bar = x print(this.x, x) //│ JS (unsanitized): -//│ let Cls1; /** scoped **/ +//│ let Cls1; //│ Cls1 = function Cls(x2) { //│ return globalThis.Object.freeze(new Cls.class(x2)); //│ }; @@ -185,7 +179,7 @@ module Whoops with val w: module Whoops = this fun g() = f() //│ JS (unsanitized): -//│ let Whoops2; /** scoped **/ +//│ let Whoops2; //│ globalThis.Object.freeze(class Whoops { //│ static { //│ Whoops2 = this @@ -232,7 +226,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.233: Runtime +//│ ║ l.227: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index e09edf1374..fcfb8fc9e7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let f, lambda; /** scoped **/ +//│ let lambda, f; //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,9 +26,9 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f1, lambda1; /** scoped **/ +//│ let lambda1, f1; //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } @@ -41,9 +41,9 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f2, lambda2; /** scoped **/ +//│ let lambda2, f2; //│ lambda2 = (undefined, function (x) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls index e2425e33b6..07b13f5dfb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls @@ -39,9 +39,7 @@ Some(1) :sjs (new Some(1)) isDefined() //│ JS (unsanitized): -//│ let tmp3; /** scoped **/ -//│ tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); -//│ Option.isDefined(tmp3) +//│ let tmp3; tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); Option.isDefined(tmp3) //│ = true new Some(1) isDefined() @@ -68,14 +66,14 @@ None == Option.None :re Option.oops //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.69: Option.oops +//│ ║ l.67: Option.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' :e open Option { oops } //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.76: open Option { oops } +//│ ║ l.74: open Option { oops } //│ ╙── ^^^^ oops @@ -84,7 +82,7 @@ oops :re Option.None.oops //│ ╔══[ERROR] Object 'None' does not contain member 'oops' -//│ ║ l.85: Option.None.oops +//│ ║ l.83: Option.None.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index 79d3e59992..9ade6db65e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -11,12 +11,8 @@ fun foo() = "a" ~ "b" ~ "c" foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo() { -//│ let tmp; /** scoped **/ -//│ tmp = M.concat("a", "b"); -//│ return M.concat(tmp, "c") -//│ }; +//│ let foo; +//│ foo = function foo() { let tmp; tmp = M.concat("a", "b"); return M.concat(tmp, "c") }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index fbe153c46d..a23da7d877 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -3,9 +3,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda; /** scoped **/ +//│ let lambda; //│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3; //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -18,9 +18,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda1; /** scoped **/ +//│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3, tmp4; //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -34,13 +34,13 @@ :sjs (x => x) + 1 //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 +//│ let lambda2; lambda2 = (undefined, function (x) { return x }); lambda2 + 1 //│ = "function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }1" :sjs 1 + (x => x) //│ JS (unsanitized): -//│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 +//│ let lambda3; lambda3 = (undefined, function (x) { return x }); 1 + lambda3 //│ = "1function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls index 86a50c8394..cf8b2bf680 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls @@ -8,7 +8,7 @@ x => let y = x y //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function (x) { let y; /** scoped **/ y = x; return y }); lambda +//│ let lambda; lambda = (undefined, function (x) { let y; y = x; return y }); lambda //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index d7b8eb9113..070e1f96ea 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -58,7 +58,7 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ +//│ let tmp; //│ split_root$: { //│ split_default$: { //│ split_1$: { @@ -107,33 +107,33 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, x, tmp2, tmp3; //│ if (a instanceof A.class) { -//│ tmp2 = 1; +//│ tmp1 = 1; //│ } else if (a instanceof B.class) { -//│ tmp2 = 2; +//│ tmp1 = 2; //│ } else if (a instanceof C.class) { -//│ tmp2 = 3; -//│ } else { -//│ let tmp4; /** scoped **/ //│ tmp1 = 3; +//│ } else { +//│ let tmp4; +//│ tmp3 = 3; //│ if (a instanceof D.class) { //│ if (a instanceof A.class) { -//│ tmp4 = 1 + tmp1; +//│ tmp4 = 1 + tmp3; //│ } else if (a instanceof B.class) { -//│ tmp4 = 2 + tmp1; +//│ tmp4 = 2 + tmp3; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp3 = tmp4; +//│ tmp2 = tmp4; //│ } else if (a instanceof E.class) { -//│ tmp3 = 5; +//│ tmp2 = 5; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp2 = Predef.print("done"); +//│ tmp1 = Predef.print("done"); //│ } -//│ x = tmp2; +//│ x = tmp1; //│ Predef.print(x) //│ > 1 //│ x = 1 @@ -147,7 +147,7 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ tmp5 = 2; //│ if (a instanceof A.class) { //│ 1 @@ -169,7 +169,7 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ if (a instanceof A.class) { //│ 1 //│ } else { @@ -192,7 +192,7 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ let x1, tmp7; /** scoped **/ +//│ let x1, tmp7; //│ if (a instanceof A.class) { //│ 1 //│ } else if (a instanceof B.class) { @@ -221,7 +221,7 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; //│ if (a instanceof B.class) { //│ 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index 19b5d0c1e1..927503ab39 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,7 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let Test1, tmp1; /** scoped **/ +//│ let tmp1, Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index dc8f7d5442..0005648013 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -4,7 +4,7 @@ :sjs module None //│ JS (unsanitized): -//│ let None1; /** scoped **/ +//│ let None1; //│ globalThis.Object.freeze(class None { //│ static { //│ None1 = this @@ -55,7 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1, tmp; /** scoped **/ +//│ let M1, tmp; //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -122,7 +122,7 @@ M.oops module M with val m: module M = M //│ JS (unsanitized): -//│ let M3; /** scoped **/ +//│ let M3; //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -155,7 +155,7 @@ module AA with val y = 2 [x, y] //│ JS (unsanitized): -//│ let AA1; /** scoped **/ +//│ let AA1; //│ globalThis.Object.freeze(class AA { //│ static { //│ AA1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 9f2d151267..1318302565 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -52,7 +52,7 @@ none() :sjs val Option = "Oops" //│ JS (unsanitized): -//│ let Option1; /** scoped **/ Option1 = "Oops"; +//│ let Option1; Option1 = "Oops"; //│ Option = "Oops" :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 2416214a74..f2e726848b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -10,7 +10,7 @@ fun isDefined(x) = if x is Some then true None then false //│ JS (unsanitized): -//│ let isDefined; /** scoped **/ +//│ let isDefined; //│ isDefined = function isDefined(x) { //│ if (x instanceof Some.class) { //│ return true @@ -31,9 +31,9 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; /** scoped **/ +//│ let isDefined1, lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let argument0$; /** scoped **/ +//│ let argument0$; //│ if (caseScrut instanceof Some.class) { //│ argument0$ = caseScrut.value; //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 04625446e1..46460ce327 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -6,7 +6,7 @@ data class Foo() //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo() { //│ return globalThis.Object.freeze(new Foo.class()); //│ }; @@ -37,7 +37,7 @@ Foo.class data class Foo(a) //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ Foo3 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -64,19 +64,19 @@ Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Foo2(1); tmp.a +//│ let tmp; tmp = Foo2(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = function foo(y) { return Foo2(y) }; foo(27) +//│ let foo; foo = function foo(y) { return Foo2(y) }; foo(27) //│ = Foo(27) data class Foo(a, b) //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ Foo5 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -94,17 +94,17 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = Foo4; +//│ let foo1; foo1 = Foo4; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) //│ JS (unsanitized): -//│ let f; /** scoped **/ f = runtime.safeCall(foo1(1, 2)); +//│ let f; f = runtime.safeCall(foo1(1, 2)); //│ f = Foo(1, 2) let f = new! foo(1, 2) //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = globalThis.Object.freeze(new foo1(1, 2)); +//│ let f1; f1 = globalThis.Object.freeze(new foo1(1, 2)); //│ f = Foo(1, 2) f.a @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; /** scoped **/ f2 = Foo4(1, 2); +//│ let f2; f2 = Foo4(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) +//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -144,7 +144,7 @@ data class Inner(c) with fun i1(d) = c + d print(c) //│ JS (unsanitized): -//│ let Inner1; /** scoped **/ +//│ let Inner1; //│ Inner1 = function Inner(c) { //│ return globalThis.Object.freeze(new Inner.class(c)); //│ }; @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner.class(100)); +//│ let i; i = globalThis.Object.freeze(new Inner.class(100)); //│ > 100 //│ i = Inner(100) @@ -185,7 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -242,7 +242,7 @@ class Foo(val z, val z) //│ ║ l.236: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo9; /** scoped **/ +//│ let Foo9; //│ Foo9 = function Foo(z, z1) { //│ return globalThis.Object.freeze(new Foo.class(z, z1)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index 9b85fd7887..b04a1ebca2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -12,7 +12,7 @@ f(2) :sjs let f = foo(1, _, _) //│ JS (unsanitized): -//│ let f2; let f3; /** scoped **/ f2 = function f(_0, _1) { return foo(1, _0, _1) }; f3 = f2; +//│ let f2; let f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; //│ f = fun f f(2, 3) @@ -79,10 +79,7 @@ h(1) :sjs let i = _(0, 1, _) //│ JS (unsanitized): -//│ let i; -//│ let i1; /** scoped **/ -//│ i = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; -//│ i1 = i; +//│ let i; let i1; i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; i = i1; //│ i = fun i i(print, 2) @@ -202,7 +199,7 @@ _ - _ of 1, 2 :pe |> 1 //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.203: |> 1 +//│ ║ l.200: |> 1 //│ ╙── ^ //│ = fun pipeInto @@ -210,7 +207,7 @@ _ - _ of 1, 2 :re |> (1) //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.211: |> (1) +//│ ║ l.208: |> (1) //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] TypeError: f is not a function @@ -228,15 +225,13 @@ _ - _ of 1, 2 :sjs 1 \ (_ - 2) //│ JS (unsanitized): -//│ let lambda38; /** scoped **/ -//│ lambda38 = (undefined, function (_0) { return _0 - 2 }); -//│ Predef.passTo(1, lambda38) +//│ let lambda38; lambda38 = (undefined, function (_0) { return _0 - 2 }); Predef.passTo(1, lambda38) //│ = fun :sjs 1 \ (_ - 2)() //│ JS (unsanitized): -//│ let lambda39, tmp19; /** scoped **/ +//│ let lambda39, tmp19; //│ lambda39 = (undefined, function (_0) { //│ return _0 - 2 //│ }); @@ -253,13 +248,13 @@ _ - _ of 1, 2 :w let f = if _ then 1 else 0 //│ ╔══[WARNING] This catch-all clause makes the following branches unreachable. -//│ ║ l.254: let f = if _ then 1 else 0 +//│ ║ l.249: let f = if _ then 1 else 0 //│ ║ ^^^^^^ //│ ╟── This branch is unreachable. -//│ ║ l.254: let f = if _ then 1 else 0 +//│ ║ l.249: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; /** scoped **/ tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs @@ -268,9 +263,9 @@ fun f(x) = 0 then 1 _ then 2 //│ JS (unsanitized): -//│ let f16; /** scoped **/ +//│ let f16; //│ f16 = function f(x3) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = x3 == 0; //│ if (scrut === true) { return 1 } else { return 2 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 19591829d2..9d43f7f4cc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -6,7 +6,7 @@ class Foo //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -23,7 +23,7 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = globalThis.Object.freeze(new Foo()); //│ if (scrut instanceof Foo) { true } else { false } //│ = true @@ -53,7 +53,7 @@ Foo() data class Foo with { print("hi") } print("ok") //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ globalThis.Object.freeze(class Foo2 { //│ static { //│ Foo3 = this @@ -72,9 +72,9 @@ fun test() = print("ok") Foo //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test() { -//│ let Foo5, tmp; /** scoped **/ +//│ let Foo5, tmp; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -91,7 +91,7 @@ fun test() = let t = test() //│ JS (unsanitized): -//│ let t; /** scoped **/ t = test(); +//│ let t; t = test(); //│ > ok //│ t = class Foo @@ -135,7 +135,7 @@ class Foo with let y = x + 1 fun z() = y + x //│ JS (unsanitized): -//│ let Foo6; /** scoped **/ +//│ let Foo6; //│ globalThis.Object.freeze(class Foo5 { //│ static { //│ Foo6 = this @@ -162,7 +162,7 @@ class Foo with fun z2() = 6 print("hello") //│ JS (unsanitized): -//│ let Foo8; /** scoped **/ +//│ let Foo8; //│ globalThis.Object.freeze(class Foo7 { //│ static { //│ Foo8 = this @@ -192,7 +192,7 @@ class Foo with fun foo(y) = x + y fun bar(z) = foo(z) + 1 //│ JS (unsanitized): -//│ let Foo10; /** scoped **/ +//│ let Foo10; //│ globalThis.Object.freeze(class Foo9 { //│ static { //│ Foo10 = this @@ -204,7 +204,7 @@ class Foo with //│ return this.x + y //│ } //│ bar(z) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = this.foo(z); //│ return tmp + 1 //│ } @@ -217,7 +217,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp, tmp1, a, tmp2, tmp3; //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); @@ -243,7 +243,7 @@ class Foo with //│ ║ l.237: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo12; /** scoped **/ +//│ let Foo12; //│ globalThis.Object.freeze(class Foo11 { //│ static { //│ Foo12 = this @@ -267,7 +267,7 @@ class Foo with //│ ║ l.261: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo14; /** scoped **/ +//│ let Foo14; //│ globalThis.Object.freeze(class Foo13 { //│ static { //│ Foo14 = this @@ -294,7 +294,7 @@ class Foo with val x = 1 //│ ║ l.292: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo16; /** scoped **/ +//│ let Foo16; //│ globalThis.Object.freeze(class Foo15 { //│ static { //│ Foo16 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls index ea8a6fed34..8a571ad88e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls @@ -7,7 +7,7 @@ let x = 1 :sjs let x' = 2 //│ JS (unsanitized): -//│ let x$_; /** scoped **/ x$_ = 2; +//│ let x$_; x$_ = 2; //│ x' = 2 x' diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 3cd21a98c2..3200ac70a4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3, lambda, folderName1, folderName2; //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index 3ae4eea381..aba13f589b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,16 +76,16 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let x1, tmp6, tmp7, arr2; -//│ let tmp8; /** scoped **/ -//│ tmp6 = globalThis.Object.freeze(new Term.Symbol("x")); -//│ x1 = globalThis.Object.freeze(new Term.Ref(tmp6)); -//│ tmp8 = Predef.print(x1); +//│ let tmp6; +//│ let x1, tmp7, tmp8, arr2; +//│ tmp7 = globalThis.Object.freeze(new Term.Symbol("x")); +//│ x1 = globalThis.Object.freeze(new Term.Ref(tmp7)); +//│ tmp6 = Predef.print(x1); //│ arr2 = globalThis.Object.freeze([ -//│ tmp6 +//│ tmp7 //│ ]); -//│ tmp7 = x1; -//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp7)) +//│ tmp8 = x1; +//│ globalThis.Object.freeze(new Term.Lam(arr2, tmp8)) //│ > Ref(Symbol("x")) //│ = Lam([Symbol("x")], Ref(Symbol("x"))) diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index a252994119..1b1cd7779a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -4,9 +4,9 @@ :sjs fun foo() = if false do foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo() { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = false; //│ if (scrut === true) { return foo() } else { return runtime.Unit } //│ }; @@ -14,7 +14,7 @@ fun foo() = if false do foo() :sjs fun foo() = foo() //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = function foo() { return foo1() }; +//│ let foo1; foo1 = function foo() { return foo1() }; :sjs @@ -22,7 +22,7 @@ data class Foo(x) with class Bar with val y = x //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; @@ -54,12 +54,7 @@ fun foo() = fun bar() = bar() bar() //│ JS (unsanitized): -//│ let foo2; /** scoped **/ -//│ foo2 = function foo() { -//│ let bar; /** scoped **/ -//│ bar = function bar() { return bar() }; -//│ return bar() -//│ }; +//│ let foo2; foo2 = function foo() { let bar; bar = function bar() { return bar() }; return bar() }; :sjs @@ -68,10 +63,7 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; /** scoped **/ -//│ f1 = 1; -//│ f = function f() { let tmp; /** scoped **/ tmp = f(); return tmp }; -//│ runtime.Unit +//│ let f, f1; f = 1; f1 = function f() { let tmp; tmp = f1(); return tmp }; runtime.Unit //│ f = 1 :sjs @@ -79,13 +71,13 @@ do let foo = 1 fun foo(x) = foo //│ ╔══[ERROR] Name 'foo' is already used -//│ ║ l.79: let foo = 1 +//│ ║ l.71: let foo = 1 //│ ║ ^^^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.80: fun foo(x) = foo +//│ ║ l.72: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; +//│ let foo3, foo4; foo3 = function foo(x) { return foo4 }; foo4 = 1; //│ foo = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 2cfe39c612..7d9d449e62 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -53,7 +53,7 @@ id(f)(3)(4) //│ runtime.checkArgs("", 2, true, args1.length); //│ let y = args1[0]; //│ let z = args1[1]; -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ tmp4 = x + y; //│ return tmp4 + z //│ } @@ -113,7 +113,7 @@ id(Cls(1, 2)).f(3) //│ this.y = y; //│ } //│ f(z, p) { -//│ let tmp6, tmp7; /** scoped **/ +//│ let tmp6, tmp7; //│ tmp6 = this.x + this.y; //│ tmp7 = tmp6 + z; //│ return tmp7 + p @@ -149,7 +149,7 @@ id(Cls(1, 2)).f(3) //│ runtime.checkArgs("f", 2, true, args.length); //│ let z = args[0]; //│ let p = args[1]; -//│ let tmp8, tmp9; /** scoped **/ +//│ let tmp8, tmp9; //│ tmp8 = this.x + this.y; //│ tmp9 = tmp8 + z; //│ return tmp9 + p @@ -191,7 +191,7 @@ id(Cls(1, 2)).f(3, 4)(5) //│ runtime.checkArgs("", 2, true, args1.length); //│ let q = args1[0]; //│ let s = args1[1]; -//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ +//│ let tmp11, tmp12, tmp13, tmp14; //│ tmp11 = this.x + this.y; //│ tmp12 = tmp11 + z; //│ tmp13 = tmp12 + p; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 6c1ed8a013..7d6d50e31e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,9 +19,9 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f() { -//│ let scrut, a, b; /** scoped **/ +//│ let scrut, a, b; //│ scrut = true; //│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -40,10 +40,10 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; /** scoped **/ +//│ let g; //│ g = function g(x1, y1, z) { +//│ let a, tmp1, tmp2; //│ let lambda; -//│ let a, tmp1, tmp2; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x1 === true) { @@ -84,9 +84,9 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ f1 = function f() { -//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; //│ split_root$: { //│ split_default$: { //│ if (x instanceof A.class) { @@ -122,9 +122,9 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ f2 = function f() { -//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ +//│ let scrut, argument0$, argument1$, tmp1; //│ split_root$: { //│ split_default$: { //│ scrut = A(1, Nil); @@ -161,12 +161,12 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(x1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, while1, tmp2, tmp3; //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, a1, argument0$, argument1$; /** scoped **/ +//│ let a, a1, argument0$, argument1$; //│ if (x1 instanceof A.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; @@ -201,13 +201,13 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; /** scoped **/ +//│ let f4; //│ f4 = function f() { -//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let y1, tmp1, while1, tmp2, tmp3; //│ y1 = x + 1; //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; //│ split_root$: { //│ split_1$: { //│ if (y1 instanceof A.class) { @@ -249,12 +249,12 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; /** scoped **/ +//│ let f5; //│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, while1, tmp2, tmp3; //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; /** scoped **/ +//│ let scrut, lambda, tmp4; //│ lambda = (undefined, function () { //│ return y1 //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 56d63f1ecd..2bd0927b56 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -5,7 +5,7 @@ module Foo with val self: module Foo = Foo //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -61,7 +61,7 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11, tmp; /** scoped **/ +//│ let Foo11, tmp; //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 0eed770a5e..21ab63ce2c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -7,7 +7,7 @@ let x = 0 :sjs let x += 1 //│ JS (unsanitized): -//│ let x1; /** scoped **/ x1 = x + 1; +//│ let x1; x1 = x + 1; //│ x = 1 x @@ -21,7 +21,7 @@ set x = 0 :sjs set x += 1 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = x1 + 1; x1 = tmp; runtime.Unit +//│ let tmp; tmp = x1 + 1; x1 = tmp; runtime.Unit x //│ = 1 @@ -30,10 +30,10 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let old, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, tmp2, old, tmp3; //│ old = x1; -//│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } -//│ tmp1 +//│ try { tmp1 = x1 + 1; x1 = tmp1; tmp2 = Predef.print(x1); tmp3 = tmp2; } finally { x1 = old; } +//│ tmp3 //│ > 2 x @@ -69,21 +69,21 @@ fun example() = print(get_x()) example() //│ JS (unsanitized): -//│ let example2; /** scoped **/ +//│ let example2; //│ example2 = function example() { -//│ let get_x; -//│ let x2, get_x1, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; +//│ let get_x1; //│ x2 = 0; -//│ get_x = function get_x() { +//│ get_x1 = function get_x() { //│ return x2 //│ }; -//│ get_x1 = get_x; +//│ get_x = get_x1; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; //│ x2 = tmp5; //│ tmp6 = Predef.print(x2); -//│ tmp7 = runtime.safeCall(get_x1()); +//│ tmp7 = runtime.safeCall(get_x()); //│ tmp8 = Predef.print(tmp7); //│ tmp9 = (tmp6 , tmp8); //│ tmp4 = tmp9; @@ -91,7 +91,7 @@ example() //│ x2 = old1; //│ } //│ tmp10 = Predef.print(x2); -//│ tmp11 = runtime.safeCall(get_x1()); +//│ tmp11 = runtime.safeCall(get_x()); //│ return Predef.print(tmp11) //│ }; //│ example2() @@ -111,15 +111,15 @@ fun example() = y example() //│ JS (unsanitized): -//│ let example3; /** scoped **/ +//│ let example3; //│ example3 = function example() { -//│ let get_x; -//│ let x2, get_x1, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ +//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; +//│ let get_x1; //│ x2 = 0; -//│ get_x = function get_x() { +//│ get_x1 = function get_x() { //│ return x2 //│ }; -//│ get_x1 = get_x; +//│ get_x = get_x1; //│ old1 = x2; //│ try { //│ tmp5 = x2 + 1; @@ -132,7 +132,7 @@ example() //│ } //│ y = tmp4; //│ tmp8 = Predef.print(x2); -//│ tmp9 = runtime.safeCall(get_x1()); +//│ tmp9 = runtime.safeCall(get_x()); //│ tmp10 = Predef.print(tmp9); //│ return y //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls index 228d0613df..9be3a3a5b7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls @@ -19,7 +19,7 @@ foo(0, ...a) :sjs foo(1, ...[2, 3], 4) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) +//│ let tmp; tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) //│ = [1, 2, 3, 4] :re diff --git a/hkmc2/shared/src/test/mlscript/codegen/This.mls b/hkmc2/shared/src/test/mlscript/codegen/This.mls index 9d809eb9e6..64418595dd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/This.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/This.mls @@ -10,14 +10,13 @@ fun test(x) = //│ ║ l.8: [this.a, x] //│ ╙── ^^^^ //│ JS (unsanitized): -//│ let test; /** scoped **/ test = function test(x) { return globalThis.Object.freeze([]) }; +//│ let test; test = function test(x) { return globalThis.Object.freeze([]) }; :sjs fun test(x) = [globalThis.a, x] //│ JS (unsanitized): -//│ let test1; /** scoped **/ -//│ test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; +//│ let test1; test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; :re test(123) @@ -43,7 +42,7 @@ module Test with fun test2(x) = [this.a, x] //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 3bb6a4a271..44fb9381b8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = call(Example, oops); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = call(Example, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) +//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls index b2171fa4c0..cad43e858a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls @@ -19,7 +19,7 @@ s(123) :sjs ex |>. s(123) //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) +//│ let tmp1; tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) //│ = [123, 456] diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index fa1b38cc34..60ab9f2c5c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,9 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; -//│ f2(1) +//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e @@ -43,7 +41,7 @@ fun f(x) = throw (if x then Error("x") else Error("y")) f(false) //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(x) { //│ if (x === true) { throw globalThis.Error("x") } else { throw globalThis.Error("y") } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 993741a4b1..0336784324 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -7,9 +7,9 @@ fun fib(a) = if a <= 1 then a else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): -//│ let fib; /** scoped **/ +//│ let fib; //│ fib = function fib(a) { -//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let scrut, tmp, tmp1, tmp2, tmp3; //│ scrut = a <= 1; //│ if (scrut === true) { //│ return a diff --git a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls index 72170fa838..c0ec266ced 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls @@ -30,9 +30,7 @@ print of foo() :sjs print of globalThis.console.log("Hello, world!") //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ -//│ tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); -//│ Predef.print(tmp4) +//│ let tmp4; tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); Predef.print(tmp4) //│ > Hello, world! //│ > () @@ -56,7 +54,7 @@ print of Box(foo()).value :sjs fun foo() = {} //│ JS (unsanitized): -//│ let foo5; /** scoped **/ foo5 = function foo() { return runtime.Unit }; +//│ let foo5; foo5 = function foo() { return runtime.Unit }; print of Box(foo()).value //│ > () @@ -66,7 +64,7 @@ print of Box(foo()).value fun foo(x) = set x = 1 //│ JS (unsanitized): -//│ let foo6; /** scoped **/ foo6 = function foo(x) { x = 1; return runtime.Unit }; +//│ let foo6; foo6 = function foo(x) { x = 1; return runtime.Unit }; print of Box(foo(1)).value //│ > () @@ -75,7 +73,7 @@ print of Box(foo(1)).value :e fun f = let x = 1 //│ ╔══[ERROR] Expected a body for let bindings in expression position -//│ ║ l.76: fun f = let x = 1 +//│ ║ l.74: fun f = let x = 1 //│ ╙── ^^^^^ print of f diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index eb618c68fb..8544c1fa3a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -6,10 +6,10 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function () { -//│ let tmp, while1, tmp1, tmp2; /** scoped **/ +//│ let tmp, while1, tmp1, tmp2; //│ tmp = undefined; //│ while1 = (undefined, function () { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = true; //│ if (scrut === true) { //│ tmp = 0; @@ -57,20 +57,20 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let tmp6, while3, tmp7, tmp8; /** scoped **/ -//│ tmp6 = undefined; +//│ let while3, tmp6, tmp7, tmp8; +//│ tmp8 = undefined; //│ while3 = (undefined, function () { -//│ let tmp9; /** scoped **/ +//│ let tmp9; //│ if (x2 === true) { //│ tmp9 = Predef.print("Hello World"); //│ x2 = false; -//│ tmp6 = runtime.Unit; +//│ tmp8 = runtime.Unit; //│ return while3() -//│ } else { tmp6 = 42; } +//│ } else { tmp8 = 42; } //│ return runtime.LoopEnd //│ }); -//│ tmp7 = while3(); -//│ tmp6 +//│ tmp6 = while3(); +//│ tmp8 //│ > Hello World //│ = 42 @@ -112,10 +112,10 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ +//│ let tmp18, while7, tmp19, tmp20; //│ tmp18 = undefined; //│ while7 = (undefined, function () { -//│ let i2, scrut, tmp21; /** scoped **/ +//│ let i2, scrut, tmp21; //│ i2 = 0; //│ scrut = i2 < 10; //│ if (scrut === true) { @@ -208,12 +208,12 @@ fun f(ls) = print(h) else print("Done!") //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f(ls) { -//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ +//│ let tmp27, while10, tmp28, tmp29; //│ tmp27 = undefined; //│ while10 = (undefined, function () { -//│ let tl, h, argument0$, argument1$; /** scoped **/ +//│ let tl, h, argument0$, argument1$; //│ if (ls instanceof Cons.class) { //│ argument0$ = ls.hd; //│ argument1$ = ls.tl; @@ -267,8 +267,8 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, tmp39, while11; /** scoped **/ -//│ tmp37 = undefined; +//│ let tmp37, tmp38, while11, tmp39; +//│ tmp39 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { //│ split_1$: { @@ -278,12 +278,12 @@ while x is {} do() //│ break split_1$ //│ } //│ } -//│ tmp37 = runtime.Unit; +//│ tmp39 = runtime.Unit; //│ } //│ return runtime.LoopEnd //│ }); -//│ tmp38 = while11(); -//│ tmp37 +//│ tmp37 = while11(); +//│ tmp39 // ——— FIXME: ——— diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 9452424ea9..885a6dd1ed 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -101,7 +101,7 @@ class T class Foo(using T) with print(T) //│ JS (unsanitized): -//│ let Foo9; /** scoped **/ +//│ let Foo9; //│ Foo9 = function Foo(tmp5) { //│ return globalThis.Object.freeze(new Foo.class(tmp5)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index 23ed0cba02..005ed3b089 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -32,13 +32,13 @@ using Num = 0.42 let f1 = foo //│ JS (unsanitized): //│ let f1; -//│ let f11; /** scoped **/ -//│ f1 = function f1() { -//│ let tmp; /** scoped **/ +//│ let f11; +//│ f11 = function f1() { +//│ let tmp; //│ tmp = foo(); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; -//│ f11 = f1; +//│ f1 = f11; //│ f1 = fun f1 :expect [42] @@ -54,13 +54,13 @@ f1() let f2 = bar //│ JS (unsanitized): //│ let f2; -//│ let f21; /** scoped **/ -//│ f2 = function f2(x) { -//│ let tmp; /** scoped **/ +//│ let f21; +//│ f21 = function f2(x) { +//│ let tmp; //│ tmp = bar(x); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; -//│ f21 = f2; +//│ f2 = f21; //│ f2 = fun f2 :expect [0.42, 42] @@ -76,11 +76,11 @@ f2(0.42) let f3 = baz //│ JS (unsanitized): //│ let f3; -//│ let f31; /** scoped **/ -//│ f3 = function f3(x) { +//│ let f31; +//│ f31 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1, tmp2; /** scoped **/ +//│ let tmp, tmp1, tmp2; //│ tmp = baz(x); //│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); //│ tmp2 = runtime.safeCall(tmp1(y)); @@ -88,7 +88,7 @@ let f3 = baz //│ }); //│ return lambda1 //│ }; -//│ f31 = f3; +//│ f3 = f31; //│ f3 = fun f3 :expect [43, 42, 44, 0.42] @@ -99,14 +99,14 @@ f3(43)(44) :sjs foo() //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) +//│ let tmp1; tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) //│ = [42] // This should not expand :sjs bar(0.42) //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) +//│ let tmp2; tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) //│ = [0.42, 42] @@ -115,7 +115,7 @@ module Test with fun test(j)(using Int) = 0 fun main(using Int) = test //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -131,7 +131,7 @@ module Test with //│ static main(tmp3) { //│ let lambda1; //│ lambda1 = (undefined, function (j) { -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ tmp4 = Test.test(j); //│ return runtime.safeCall(tmp4(tmp3)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index a58b71139f..482d1d6532 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) +//│ let tmp2; tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index 4cd471afe4..11eee81727 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -17,8 +17,8 @@ fun f() = print of raiseUnhandledEffect() j / i //│ JS (unsanitized): +//│ let f; //│ let getLocals2; -//│ let f; /** scoped **/ //│ getLocals2 = function getLocals() { //│ let prev, thisInfo, arr, tmp; //│ prev = []; @@ -28,8 +28,8 @@ fun f() = //│ return prev //│ }; //│ f = function f() { +//│ let i, j, k, scrut, tmp, tmp1; //│ let getLocals3, Cont$func$f$1, doUnwind; -//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ //│ getLocals3 = function getLocals() { //│ let i1, j1, k1, prev, thisInfo, arr, tmp2; //│ prev = getLocals2(); diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 4b3fa31cce..ff4375a632 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,11 +156,11 @@ if true do fun f() = 3 f() //│ JS (unsanitized): +//│ let tmp20; //│ let handleBlock$11; -//│ let tmp20; /** scoped **/ //│ handleBlock$11 = function handleBlock$() { +//│ let f, scrut; //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; -//│ let f, scrut; /** scoped **/ //│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { //│ Handler$h$12 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index 3b0ece8c6e..ee16b6ecbd 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -13,9 +13,9 @@ fun foo(h): module M = module A A //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(h) { -//│ let A2, A3, scrut; /** scoped **/ +//│ let A2, A3, scrut; //│ globalThis.Object.freeze(class A { //│ static { //│ A2 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 72893cfea0..9eee2040f8 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,7 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1, tmp; /** scoped **/ +//│ let Lol1, tmp; //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index be8ffedbb6..0df80c6f15 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,8 +155,8 @@ if true do h1.perform(()) str //│ JS (unsanitized): +//│ let tmp24, tmp25, tmp26, str, scrut, tmp27; //│ let handleBlock$7; -//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; /** scoped **/ //│ str = ""; //│ scrut = true; //│ if (scrut === true) { @@ -173,8 +173,8 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { +//│ let tmp28, tmp29, tmp30; //│ let Cont$handler$h1$perform$2, doUnwind1; -//│ let tmp28, tmp29, tmp30; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h1$perform$2 = this @@ -232,11 +232,11 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 6) { -//│ tmp25 = value$; +//│ tmp24 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 6) { -//│ return tmp25 +//│ return tmp24 //│ } //│ break; //│ } @@ -261,8 +261,8 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; //│ let Cont$handler$h2$perform$2, doUnwind2; -//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h2$perform$2 = this @@ -323,7 +323,7 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 1) { -//│ tmp26 = value$; +//│ tmp25 = value$; //│ } else if (this.pc === 2) { //│ res7 = value$; //│ } @@ -348,9 +348,9 @@ str //│ res8.contTrace.last.next = new Cont$handleBlock$h2$2(pc); //│ return runtime.handleBlockImpl(res8, h2) //│ }; -//│ tmp26 = runtime.safeCall(h2.perform(runtime.Unit)); -//│ if (tmp26 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp26, 1) +//│ tmp25 = runtime.safeCall(h2.perform(runtime.Unit)); +//│ if (tmp25 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp25, 1) //│ } //│ res7 = runtime.safeCall(h1.perform(runtime.Unit)); //│ if (res7 instanceof runtime.EffectSig.class) { @@ -358,18 +358,18 @@ str //│ } //│ return res7 //│ }; -//│ tmp25 = handleBlock$8(); -//│ if (tmp25 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp25, 6) +//│ tmp24 = handleBlock$8(); +//│ if (tmp24 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp24, 6) //│ } -//│ return tmp25 +//│ return tmp24 //│ }; -//│ tmp24 = handleBlock$7(); -//│ if (tmp24 instanceof runtime.EffectSig.class) { -//│ tmp24 = runtime.topLevelEffect(tmp24, false); +//│ tmp27 = handleBlock$7(); +//│ if (tmp27 instanceof runtime.EffectSig.class) { +//│ tmp27 = runtime.topLevelEffect(tmp27, false); //│ } -//│ tmp27 = tmp24; -//│ } else { tmp27 = runtime.Unit; } +//│ tmp26 = tmp27; +//│ } else { tmp26 = runtime.Unit; } //│ str //│ = "BABABA" //│ str = "BABABA" diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 6dd7795543..e4b6108c4f 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,17 +8,17 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let x, old, tmp, tmp1, tmp2; /** scoped **/ +//│ let tmp, tmp1, x, old, tmp2; //│ x = 1; //│ old = x; //│ try { -//│ tmp1 = x + 1; -//│ x = tmp1; -//│ tmp2 = Predef.print(x); -//│ if (tmp2 instanceof runtime.EffectSig.class) { -//│ tmp2 = runtime.topLevelEffect(tmp2, false); +//│ tmp = x + 1; +//│ x = tmp; +//│ tmp1 = Predef.print(x); +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ tmp1 = runtime.topLevelEffect(tmp1, false); //│ } -//│ tmp = tmp2; +//│ tmp2 = tmp1; //│ } finally { x = old; } //│ x //│ > 2 diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 688ae8e6d2..470faa6f12 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -20,11 +20,11 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): +//│ let hi; //│ let res, $_stack$_safe$_body$_; -//│ let hi; /** scoped **/ //│ hi = function hi(n) { +//│ let scrut, tmp; //│ let Cont$func$hi$1, doUnwind, stackDelayRes; -//│ let scrut, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$hi$1 = this @@ -76,11 +76,11 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): +//│ let sum1; //│ let res1, $_stack$_safe$_body$_1; -//│ let sum1; /** scoped **/ //│ sum1 = function sum(n) { +//│ let scrut, tmp, tmp1; //│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; -//│ let scrut, tmp, tmp1; /** scoped **/ //│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$sum$1 = this @@ -260,9 +260,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; /** scoped **/ +//│ let max; //│ max = function max(a, b) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -274,7 +274,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls index a1596055ef..76a8e46f8f 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls @@ -13,7 +13,7 @@ arr.map(_ === false) :sjs arr.map((e, ...) => e === false) //│ JS (unsanitized): -//│ let lambda1; /** scoped **/ +//│ let lambda1; //│ lambda1 = (undefined, function (e, ..._) { return e === false }); //│ runtime.safeCall(arr.map(lambda1)) //│ = [false, true] @@ -21,7 +21,7 @@ arr.map((e, ...) => e === false) :sjs arr.map((_, ...) => 1) //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ +//│ let lambda2; //│ lambda2 = (undefined, function (_, ..._1) { return 1 }); //│ runtime.safeCall(arr.map(lambda2)) //│ = [1, 1] diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 21b5a1233e..9d6342ec9a 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,8 +88,8 @@ fun f() = Good() f().foo() //│ JS (unsanitized): +//│ let tmp9, f5; //│ let Bad1, Good1, Bad$, Good$, f$capture3; -//│ let f5, tmp9; /** scoped **/ //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; //│ if (isMut === true) { @@ -131,7 +131,7 @@ f().foo() //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ foo() { -//│ let tmp10, tmp11; /** scoped **/ +//│ let tmp10, tmp11; //│ this.z = 100; //│ tmp10 = this.x + this.y; //│ tmp11 = tmp10 + this.z; @@ -186,8 +186,8 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { +//│ let x, y, z, w, tmp10, tmp11; //│ let capture; -//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ //│ capture = new f$capture3(null); //│ x = 1; //│ y = 10; diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index 9be85d298a..9ae2b447c7 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -11,13 +11,13 @@ fun f(x) = fun g = new A //│ ═══[WARNING] Modules are not yet lifted. //│ JS (unsanitized): +//│ let f; //│ let g$; -//│ let f; /** scoped **/ //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; //│ f = function f(x) { -//│ let A1, g; /** scoped **/ +//│ let A1, g; //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 2bce7ec269..9162254161 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -7,8 +7,8 @@ data class A(x) with fun getB() = x + y fun getA() = B(2).getB() //│ JS (unsanitized): +//│ let A1; //│ let B1, B$; -//│ let A1; /** scoped **/ //│ B$ = function B$(isMut, A$instance, y) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -55,7 +55,7 @@ data class A(x) with //│ this.x = x; //│ } //│ getA() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = B$(false, this, 2); //│ return tmp.getB() //│ } @@ -74,8 +74,8 @@ class A with g (new A).x() //│ JS (unsanitized): +//│ let A3, tmp1; //│ let g, g$; -//│ let A3, tmp1; /** scoped **/ //│ g$ = function g$(A$instance) { //│ return 2 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index 3c83263869..eec1c56126 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -47,10 +47,10 @@ fun f(used1, unused1) = foo(used2) + unused2 f(1, 2) //│ JS (unsanitized): +//│ let f3; //│ let g1, g$3; -//│ let f3; /** scoped **/ //│ g$3 = function g$(used1, used2, g_arg) { -//│ let used3; /** scoped **/ +//│ let used3; //│ used3 = 2; //│ return used1 + used2 //│ }; @@ -60,8 +60,8 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { +//│ let used2, unused2, foo, tmp; //│ let g$here; -//│ let used2, unused2, foo, tmp; /** scoped **/ //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); @@ -79,10 +79,10 @@ fun f(a1, a2, a3, a4, a5, a6) = g f(1,2,3,4,5,6) //│ JS (unsanitized): +//│ let f4; //│ let g$4; -//│ let f4; /** scoped **/ //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3; //│ tmp = a1 + a2; //│ tmp1 = tmp + a3; //│ tmp2 = tmp1 + a4; @@ -90,7 +90,7 @@ f(1,2,3,4,5,6) //│ return tmp3 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { -//│ let g2, tmp; /** scoped **/ +//│ let g2, tmp; //│ tmp = g$4(a1, a2, a3, a4, a5, a6); //│ return tmp //│ }; @@ -165,8 +165,8 @@ fun f(unused, immutable, mutated) = a + h() + unused f(1, 2, 1000) //│ JS (unsanitized): +//│ let f7; //│ let h$2, g$6, f$capture5; -//│ let f7; /** scoped **/ //│ g$6 = function g$(immutable, f$capture6) { //│ f$capture6.mutated$capture$0 = 2; //│ return immutable + f$capture6.mutated$capture$0 @@ -185,8 +185,8 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { +//│ let g3, h2, a1, tmp7, tmp8; //│ let capture; -//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ //│ capture = new f$capture5(mutated); //│ a1 = g$6(immutable, capture); //│ tmp7 = h$2(capture); @@ -256,15 +256,15 @@ fun g() = f g()(1) //│ JS (unsanitized): +//│ let g6, tmp7; //│ let f14, f$1, h$4, g$capture1; -//│ let g6, tmp7; /** scoped **/ //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; //│ x1 = 4; //│ return x1 + g$capture2.y$capture$0 //│ }; //│ f$1 = function f$(g$capture2, x1) { -//│ let h3, k; /** scoped **/ +//│ let h3, k; //│ k = 4; //│ g$capture2.y$capture$0 = 2; //│ return x1 @@ -285,8 +285,8 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { +//│ let y1; //│ let capture, f$here; -//│ let y1; /** scoped **/ //│ capture = new g$capture1(null); //│ capture.y$capture$0 = 0; //│ f$here = runtime.safeCall(f14(capture)); @@ -377,8 +377,8 @@ fun f(x) = set y = 2 [g, g] //│ JS (unsanitized): +//│ let f23; //│ let g12, g$14; -//│ let f23; /** scoped **/ //│ g$14 = function g$(y1) { //│ return y1 //│ }; @@ -388,8 +388,8 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { +//│ let y1, scrut; //│ let g$here; -//│ let y1, scrut; /** scoped **/ //│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { @@ -413,8 +413,8 @@ fun f(x) = set x += 1 [a, g] //│ JS (unsanitized): +//│ let f24; //│ let g13, g$15, f$capture17; -//│ let f24; /** scoped **/ //│ g$15 = function g$(f$capture18) { //│ return f$capture18.x$capture$0 //│ }; @@ -434,8 +434,8 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { +//│ let a1, tmp10; //│ let capture, g$here; -//│ let a1, tmp10; /** scoped **/ //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); //│ a1 = g$here; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls index 581a224319..d5bc28e321 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls @@ -7,7 +7,7 @@ import "../../mlscript-compile/Option.mls" module A with fun f(x) = x is Option.Some //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 4931b1249b..a1bc4b23c1 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -45,8 +45,8 @@ fun foo() = set x += 1 return () => x //│ JS (unsanitized): +//│ let foo2; //│ let lambda2, while$1, lambda$2, foo$capture5; -//│ let foo2; /** scoped **/ //│ lambda$2 = function lambda$(foo$capture6) { //│ return foo$capture6.x$capture$0 //│ }; @@ -56,8 +56,8 @@ fun foo() = //│ } //│ }); //│ while$1 = function while$(foo$capture6) { +//│ let scrut, tmp2; //│ let lambda$here; -//│ let scrut, tmp2; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ tmp2 = foo$capture6.x$capture$0 + 1; @@ -81,8 +81,8 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo2 = function foo() { +//│ let x, tmp2, tmp3, tmp4, while1; //│ let capture; -//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ //│ capture = new foo$capture5(null, null); //│ capture.x$capture$0 = 1; //│ capture.tmp$capture$1 = undefined; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index c09ff52620..adfa8e4b89 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -27,8 +27,8 @@ fun foo(y) = (new M).foo() foo(10) //│ JS (unsanitized): +//│ let foo1; //│ let M3, M$; -//│ let foo1; /** scoped **/ //│ M$ = function M$(isMut, y) { //│ let tmp; //│ if (isMut === true) { @@ -55,7 +55,7 @@ foo(10) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ foo1 = function foo(y) { let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() }; +//│ foo1 = function foo(y) { let tmp; tmp = M$(false, y); return tmp.foo() }; //│ foo1(10) @@ -110,8 +110,8 @@ fun foo(x, y) = fun foo3 = M.foo2() foo3 //│ JS (unsanitized): +//│ let foo4; //│ let M5, foo3$, foo$capture3; -//│ let foo4; /** scoped **/ //│ foo3$ = function foo3$(M6, x, foo$capture4) { //│ return M6.foo2() //│ }; @@ -147,8 +147,8 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { +//│ let foo31, tmp; //│ let M$1, capture; -//│ let foo31, tmp; /** scoped **/ //│ capture = new foo$capture3(y); //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); //│ tmp = foo3$(M$1, x, capture); @@ -214,7 +214,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp5, tmp6; /** scoped **/ +//│ let M17, tmp5, tmp6; //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -223,7 +223,7 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index fae5e2cb30..0e670441d0 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -12,8 +12,8 @@ fun foo() = xs.push(bar) set x = 2 //│ JS (unsanitized): +//│ let foo; //│ let bar, bar$, foo$capture1; -//│ let foo; /** scoped **/ //│ bar$ = function bar$(foo$capture2) { //│ return foo$capture2.x$capture$0 //│ }; @@ -33,8 +33,8 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { +//│ let x, tmp; //│ let capture, bar$here; -//│ let x, tmp; /** scoped **/ //│ capture = new foo$capture1(null); //│ capture.x$capture$0 = 1; //│ bar$here = runtime.safeCall(bar(capture)); @@ -86,13 +86,9 @@ fun foo() = x bar //│ JS (unsanitized): +//│ let foo3; //│ let bar3; -//│ let foo3; /** scoped **/ -//│ bar3 = function bar() { -//│ let x; /** scoped **/ -//│ x = undefined; -//│ return x -//│ }; +//│ bar3 = function bar() { let x; x = undefined; return x }; //│ foo3 = function foo() { return bar3 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 964c518180..7c0b2622b6 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -21,8 +21,8 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): +//│ let hi; //│ let Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; -//│ let hi; /** scoped **/ //│ Cont$func$hi$$ = function Cont$func$hi$$(isMut, n, pc) { //│ let tmp, tmp1; //│ if (isMut === true) { @@ -61,8 +61,8 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { +//│ let scrut, tmp; //│ let stackDelayRes; -//│ let scrut, tmp; /** scoped **/ //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); //│ if (stackDelayRes instanceof runtime.EffectSig.class) { @@ -94,8 +94,8 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): +//│ let sum1; //│ let Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; -//│ let sum1; /** scoped **/ //│ Cont$func$sum$$ = function Cont$func$sum$$(isMut, n, tmp, pc) { //│ let tmp1, tmp2; //│ if (isMut === true) { @@ -151,8 +151,8 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { +//│ let scrut, tmp, tmp1; //│ let curDepth, stackDelayRes; -//│ let scrut, tmp, tmp1; /** scoped **/ //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); @@ -276,9 +276,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; /** scoped **/ +//│ let max; //│ max = function max(a, b) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -290,7 +290,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index ada3f10bb0..3ebc7254e9 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -14,7 +14,7 @@ class A(x) with set z += 2 x //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ A1 = function A(x) { //│ return globalThis.Object.freeze(new A.class(x)); //│ }; @@ -42,8 +42,8 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { +//│ let tmp, tmp1; //│ let idx1, idx2, idx3, idx4, idx5; -//│ let tmp, tmp1; /** scoped **/ //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; //│ idx5 = idx + 0; @@ -61,7 +61,7 @@ class A(x) with //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ f(y) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ tmp = this.#x + 1; //│ this.#x = tmp; //│ tmp1 = this.z + 2; diff --git a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls index 226fd3ed63..41df93f70a 100644 --- a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls +++ b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls @@ -20,7 +20,7 @@ //│ ║ l.16: 1 //│ ╙── ^ //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = + 2; + 3 +//│ let tmp2; tmp2 = + 2; + 3 //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.16: 1 //│ ╙── ^ @@ -31,7 +31,7 @@ + 2 + 3 //│ JS (unsanitized): -//│ let tmp3; /** scoped **/ tmp3 = 1 + 2; tmp3 + 3 +//│ let tmp3; tmp3 = 1 + 2; tmp3 + 3 //│ = 6 1 diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 87eb91c7eb..bcae9dd616 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let ls, a, middleElements, element0$; /** scoped **/ +//│ let element0$, ls, a, middleElements; //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); @@ -133,9 +133,9 @@ fun popByIndex(start, end, acc, lft) = if start >= end then acc else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): -//│ let popByIndex; /** scoped **/ +//│ let popByIndex; //│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ +//│ let scrut, tmp34, tmp35, tmp36; //│ scrut = start >= end; //│ if (scrut === true) { //│ return acc diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 783e6d19df..25ac7189a4 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) +//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 72036ed9d4..27429ef9e1 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -20,7 +20,7 @@ x => if x is [[0]] then 1 //│ JS (unsanitized): //│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let element0$, element0$1, tmp; /** scoped **/ +//│ let element0$, element0$1, tmp; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { @@ -57,9 +57,9 @@ fun crazy(v) = S(S(S(S(S(S(0)))))) then "bruh!" _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): -//│ let crazy; /** scoped **/ +//│ let crazy; //│ crazy = function crazy(v) { -//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; //│ split_root$: { //│ split_1$: { //│ if (v instanceof S.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index de445d712f..2ec685ad76 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,13 +25,13 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, scrut5, tmp5; /** scoped **/ +//│ let scrut4, tmp5, scrut5; //│ split_root$2: { //│ split_1$2: { -//│ scrut4 = true; -//│ if (scrut4 === true) { -//│ scrut5 = test(42); -//│ if (scrut5 === true) { +//│ scrut5 = true; +//│ if (scrut5 === true) { +//│ scrut4 = test(42); +//│ if (scrut4 === true) { //│ tmp5 = true; //│ break split_root$2 //│ } else { break split_1$2 } diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index ca253fe2be..0ebb0ea24b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,22 +10,19 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } +//│ let scrut; scrut = x === 0; if (scrut === true) { 1 } else { 2 } //│ = 1 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; /** scoped **/ -//│ scrut1 = x === 0; -//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } -//│ a = tmp; +//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; /** scoped **/ +//│ let scrut2, tmp1; //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -41,7 +38,7 @@ if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ split_root$: { //│ split_1$: { //│ if (x === 0) { @@ -81,7 +78,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; /** scoped **/ +//│ let qqq, tmp3; //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -121,7 +118,7 @@ print of if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ split_root$2: { //│ split_1$2: { //│ if (x === 0) { @@ -162,9 +159,9 @@ fun foo(x, y, z) = 1 then "1" else "" //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(x1, y1, z1) { -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ split_root$3: { //│ split_1$3: { //│ if (x1 === 0) { @@ -208,7 +205,7 @@ print of if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ split_root$3: { //│ split_1$3: { //│ split_2$: { @@ -270,9 +267,9 @@ fun foo(x, y, z) = if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ foo1 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ split_root$4: { //│ split_1$4: { //│ split_2$1: { @@ -337,9 +334,9 @@ fun foo(x, y, z) = if x is let value = "hello" expensive_call(value) //│ JS (unsanitized): -//│ let foo2; /** scoped **/ +//│ let foo2; //│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; /** scoped **/ +//│ let value, tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { @@ -380,7 +377,7 @@ fun foo(x, y, z) = if x is A then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo3; /** scoped **/ +//│ let foo3; //│ foo3 = function foo(x1, y1, z1) { //│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } //│ }; @@ -401,9 +398,9 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo4; /** scoped **/ +//│ let foo4; //│ foo4 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 instanceof A.class) { @@ -427,9 +424,9 @@ fun foo(x, y, z) = fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): -//│ let foo5; /** scoped **/ +//│ let foo5; //│ foo5 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ split_root$4: { //│ split_default$: { //│ if (x1 instanceof A.class) { @@ -467,7 +464,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; /** scoped **/ +//│ let y1, scrut3, tmp6; //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls index db15ad49ed..18fb0d1700 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls @@ -17,7 +17,7 @@ if x then 0 y then 0 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ +//│ let tmp; //│ split_root$: { //│ split_1$: { //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index 04f680fc0b..feb39394f2 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -11,7 +11,7 @@ x => if x is Pair(A, B) then 1 //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x) { -//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ let argument0$, argument1$, tmp; //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair.class) { @@ -45,9 +45,9 @@ fun f(x) = if x is Pair(A, A) then 1 Pair(B, B) then 2 //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f(x) { -//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ let argument0$, argument1$, tmp; //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 697105bd95..970b684cf9 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -36,9 +36,9 @@ fun foo(v) = A & B then 1 else 0 //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(v) { -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ split_root$2: { //│ split_1$2: { //│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 408479468b..33339b6e3a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -6,9 +6,9 @@ fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ JS (unsanitized): -//│ let nonsense; /** scoped **/ +//│ let nonsense; //│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; /** scoped **/ +//│ let ys, middleElements; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -27,9 +27,9 @@ fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ JS (unsanitized): -//│ let lead_and_last; /** scoped **/ +//│ let lead_and_last; //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ +//│ let x, ys, y, lastElement0$, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -61,9 +61,9 @@ fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 //│ JS (unsanitized): -//│ let nested_tuple_patterns; /** scoped **/ +//│ let nested_tuple_patterns; //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index daa8dac993..b203efa621 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -33,9 +33,9 @@ Cross.unapply("0") :sjs fun foo(x) = x is Cross //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(x2) { -//│ let unapplyResult, output, bindings; /** scoped **/ +//│ let unapplyResult, output, bindings; //│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index b5e544bd4f..8b6e1f28b0 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -11,15 +11,15 @@ data class Pair[A, B](first: A, second: B) :sjs pattern SumPair = Pair(a, b) => a + b //│ JS (unsanitized): -//│ let SumPair1; /** scoped **/ +//│ let SumPair1; //│ globalThis.Object.freeze(class SumPair { //│ static { //│ SumPair1 = globalThis.Object.freeze(new this) //│ } //│ constructor() {} //│ unapply(input) { +//│ let transform, argument0$, argument1$, transformResult, tmp; //│ let lambda; -//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ //│ lambda = (undefined, function (a, b) { //│ return a + b //│ }); diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 273c3bfbb1..08c772d47d 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -111,20 +111,21 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: // TODO: remove mutation var toplvlDefinedVars = collection.Set.empty[Symbol] - def assignResSym(b: Block): Block = + def assignResSym(b: Block, toplvl: Boolean): Block = b.mapTail: case e: End => Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) case Return(res, implct) => assert(implct) Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case Scoped(xs, body, true) => - toplvlDefinedVars = xs - assignResSym(body) - case Scoped(xs, body, false) => - Scoped(xs, assignResSym(body), false) + case s@Scoped(xs, body) => + if toplvl then + toplvlDefinedVars = xs + assignResSym(body, false) + else + Scoped(xs, assignResSym(body, false)) case tl: (Throw | Break | Continue) => tl - val le = lowered0.copy(main = assignResSym(lowered0.main)) + val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then output(s"Lowered:") output(lowered0.showAsTree) From 495956cca0b8b558631a57713cafc5069928a8cd Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 30 Nov 2025 00:19:33 +0800 Subject: [PATCH 38/72] more tests; add a fixme; minor --- .../main/scala/hkmc2/codegen/Lowering.scala | 5 +- .../scala/hkmc2/codegen/js/JSBuilder.scala | 4 +- .../src/test/mlscript-compile/Predef.mjs | 22 ++-- .../src/test/mlscript-compile/Runtime.mjs | 122 +++++++++--------- .../OverloadedModulesInSignatures.mls | 4 +- .../backlog/NonReturningStatements.mls | 2 +- .../src/test/mlscript/backlog/ToTriage.mls | 60 ++++----- .../src/test/mlscript/basics/BadDefs.mls | 4 +- .../basics/CompanionModules_Classes.mls | 2 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../shared/src/test/mlscript/basics/Drop.mls | 6 +- .../test/mlscript/basics/FunnyRecordKeys.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 2 +- .../src/test/mlscript/basics/LazySpreads.mls | 8 +- .../mlscript/basics/MemberProjections.mls | 4 +- .../test/mlscript/basics/MiscArrayTests.mls | 2 +- .../mlscript/basics/MultiParamListClasses.mls | 4 +- .../test/mlscript/basics/MultiParamLists.mls | 22 ++-- .../mlscript/basics/MultilineExpressions.mls | 2 +- .../src/test/mlscript/basics/MutArr.mls | 4 +- .../src/test/mlscript/basics/MutRcd.mls | 2 +- .../src/test/mlscript/basics/MutVal.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../src/test/mlscript/basics/PartialApps.mls | 9 +- .../mlscript/basics/PureTermStatements.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 12 +- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/basics/Underscores.mls | 2 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 30 ++--- .../src/test/mlscript/bbml/bbGetters.mls | 8 +- .../src/test/mlscript/codegen/Arrays.mls | 4 +- .../src/test/mlscript/codegen/BadInit.mls | 4 +- .../src/test/mlscript/codegen/BadNew.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 2 +- .../src/test/mlscript/codegen/BuiltinOps.mls | 4 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 4 +- .../test/mlscript/codegen/CaseShorthand.mls | 14 +- .../test/mlscript/codegen/ClassInClass.mls | 4 +- .../src/test/mlscript/codegen/ClassInFun.mls | 8 +- .../test/mlscript/codegen/ClassMatching.mls | 18 +-- .../src/test/mlscript/codegen/Classes.mls | 8 +- .../src/test/mlscript/codegen/Comma.mls | 20 ++- .../src/test/mlscript/codegen/ConsoleLog.mls | 8 +- .../test/mlscript/codegen/DelayedLetInit.mls | 18 +-- .../src/test/mlscript/codegen/EarlyReturn.mls | 4 +- .../src/test/mlscript/codegen/Formatting.mls | 18 +-- .../src/test/mlscript/codegen/FunInClass.mls | 18 +-- .../test/mlscript/codegen/FunctionsThis.mls | 4 +- .../src/test/mlscript/codegen/Getters.mls | 47 +++---- .../src/test/mlscript/codegen/GlobalThis.mls | 6 +- .../src/test/mlscript/codegen/Hygiene.mls | 24 ++-- .../src/test/mlscript/codegen/IfThenElse.mls | 10 +- .../src/test/mlscript/codegen/ImportMLs.mls | 10 +- .../src/test/mlscript/codegen/ImportedOps.mls | 8 +- .../test/mlscript/codegen/InlineLambdas.mls | 12 +- .../src/test/mlscript/codegen/Lambdas.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 14 +- .../test/mlscript/codegen/ModuleMethods.mls | 2 +- .../src/test/mlscript/codegen/Modules.mls | 8 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 6 +- .../test/mlscript/codegen/ParamClasses.mls | 28 ++-- .../src/test/mlscript/codegen/PartialApps.mls | 27 ++-- .../test/mlscript/codegen/PlainClasses.mls | 28 ++-- .../src/test/mlscript/codegen/Primes.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/Quasiquotes.mls | 2 +- .../src/test/mlscript/codegen/RandomStuff.mls | 26 ++-- .../test/mlscript/codegen/SanityChecks.mls | 8 +- .../src/test/mlscript/codegen/Scoped.mls | 58 ++++++--- .../test/mlscript/codegen/SelfReferences.mls | 4 +- .../src/test/mlscript/codegen/SetIn.mls | 14 +- .../src/test/mlscript/codegen/Spreads.mls | 2 +- .../shared/src/test/mlscript/codegen/This.mls | 7 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/ThisCalls.mls | 2 +- .../src/test/mlscript/codegen/Throw.mls | 6 +- .../src/test/mlscript/codegen/TraceLog.mls | 4 +- .../src/test/mlscript/codegen/UnitValue.mls | 10 +- .../src/test/mlscript/codegen/While.mls | 20 +-- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/ctx/EtaExpansion.mls | 20 +-- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../src/test/mlscript/handlers/Debugging.mls | 4 +- .../src/test/mlscript/handlers/Effects.mls | 4 +- .../test/mlscript/handlers/EffectsHygiene.mls | 4 +- .../mlscript/handlers/EffectsInClasses.mls | 2 +- .../mlscript/handlers/RecursiveHandlers.mls | 6 +- .../test/mlscript/handlers/SetInHandlers.mls | 2 +- .../test/mlscript/handlers/StackSafety.mls | 14 +- .../src/test/mlscript/interop/Arrays.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 6 +- .../test/mlscript/lifter/CompanionsInFun.mls | 4 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 6 +- .../src/test/mlscript/lifter/FunInFun.mls | 30 ++--- .../src/test/mlscript/lifter/Imports.mls | 2 +- .../shared/src/test/mlscript/lifter/Loops.mls | 6 +- .../test/mlscript/lifter/ModulesObjects.mls | 12 +- .../src/test/mlscript/lifter/Mutation.mls | 12 +- .../test/mlscript/lifter/StackSafetyLift.mls | 14 +- .../src/test/mlscript/objbuf/Mutation.mls | 6 +- .../src/test/mlscript/parser/PrefixOps.mls | 4 +- .../test/mlscript/std/FingerTreeListTest.mls | 6 +- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 6 +- .../ucs/general/LogicalConnectives.mls | 2 +- .../ucs/normalization/Deduplication.mls | 41 +++--- .../normalization/ExcessiveDeduplication.mls | 2 +- .../ucs/normalization/SimplePairMatches.mls | 6 +- .../ucs/patterns/ConjunctionPattern.mls | 4 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 12 +- .../src/test/mlscript/ups/MatchResult.mls | 4 +- .../src/test/mlscript/ups/SimpleTransform.mls | 4 +- 113 files changed, 631 insertions(+), 551 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 16d264cf85..901044053f 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -1020,10 +1020,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated - possiblyScoped(scopedSyms, body) - - inline def possiblyScoped(syms: collection.Set[Symbol], body: Block) = - if syms.isEmpty then body else Scoped(syms, body) + Scoped(scopedSyms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index f2224ef7e1..22ec968088 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -595,7 +595,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: val (scopedSyms, unscopedMain) = p.main match case Scoped(syms, body) /* if exprt.isDefined */ => (syms, body) case _ => (Set.empty, p.main) - imps.mkDocument(doc" # ") :/: (genLetDecls(scopedSyms.iterator.map(l => l -> scope.allocateName(l)), false) :: block(unscopedMain, endSemi = false)).stripBreaks :: ( + imps.mkDocument(doc" # ") :/: (genLetDecls(scopedSyms.iterator.map(l => l -> scope.allocateName(l)), true) :: block(unscopedMain, endSemi = false)).stripBreaks :: ( exprt match case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" case N => doc"" @@ -626,7 +626,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case Scoped(syms, body) => val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) - genLetDecls(vars, false) :: k(body) + genLetDecls(vars, true) :: k(body) case _ => k(blk) diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index 56efea3c58..e7f4e8a4fa 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -5,7 +5,7 @@ import Term from "./Term.mjs"; import RuntimeJS from "./RuntimeJS.mjs"; import Runtime from "./Runtime.mjs"; import Rendering from "./Rendering.mjs"; -let Predef1; +let Predef1; /** scoped **/ globalThis.Object.freeze(class Predef { static { Predef1 = this @@ -57,12 +57,12 @@ globalThis.Object.freeze(class Predef { return runtime.safeCall(f(x)) } static tap(x, f) { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(f(x)); return (tmp , x) } static pat(f, x) { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(f(x)); return (tmp , x) } @@ -71,14 +71,14 @@ globalThis.Object.freeze(class Predef { } static andThen(f, g) { return (x) => { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(f(x)); return runtime.safeCall(g(tmp)) } } static compose(f, g) { return (x) => { - let tmp; + let tmp; /** scoped **/ tmp = runtime.safeCall(g(x)); return runtime.safeCall(f(tmp)) } @@ -99,7 +99,7 @@ globalThis.Object.freeze(class Predef { } } static print(...xs) { - let tmp, tmp1; + let tmp, tmp1; /** scoped **/ tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); tmp1 = runtime.safeCall(tmp(...xs)); return runtime.safeCall(globalThis.console.log(...tmp1)) @@ -112,7 +112,7 @@ globalThis.Object.freeze(class Predef { } } static notImplemented(msg) { - let tmp; + let tmp; /** scoped **/ tmp = "Not implemented: " + msg; throw globalThis.Error(tmp) } @@ -124,7 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, tmp; + let len, scrut, i, init, tmp; /** scoped **/ len = rest.length; scrut = len == 0; if (scrut === true) { @@ -133,7 +133,7 @@ globalThis.Object.freeze(class Predef { i = len - 1; init = runtime.safeCall(rest.at(i)); tmp1: while (true) { - let scrut1, tmp2, tmp3, tmp4; + let scrut1, tmp2, tmp3, tmp4; /** scoped **/ scrut1 = i > 0; if (scrut1 === true) { tmp2 = i - 1; @@ -153,9 +153,9 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda, tmp; + let lambda, tmp; /** scoped **/ lambda = (undefined, function (acc, x) { - let tmp1, tmp2, tmp3; + let tmp1, tmp2, tmp3; /** scoped **/ if (typeof x === 'string') { tmp1 = true; } else { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 0c1552eb61..a2cb048716 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -6,7 +6,7 @@ import RuntimeJS from "./RuntimeJS.mjs"; import Rendering from "./Rendering.mjs"; import LazyArray from "./LazyArray.mjs"; import Iter from "./Iter.mjs"; -let Runtime1; +let Runtime1; /** scoped **/ globalThis.Object.freeze(class Runtime { static { Runtime1 = this @@ -74,10 +74,10 @@ globalThis.Object.freeze(class Runtime { } #_reified; resumeWith(value) { - let lambda; + let lambda; /** scoped **/ const this$EffectHandle = this; lambda = (undefined, function () { - let tmp; + let tmp; /** scoped **/ tmp = Runtime.resume(this$EffectHandle.reified.contTrace); return runtime.safeCall(tmp(value)) }); @@ -127,12 +127,12 @@ globalThis.Object.freeze(class Runtime { this.split = LazyArray.__split; } static slice(xs, i, j) { - let tmp; + let tmp; /** scoped **/ tmp = xs.length - j; return xs.slice(i, tmp) } static lazySlice(xs, i, j) { - let tmp; + let tmp; /** scoped **/ tmp = LazyArray.dropLeftRight(i, j); return runtime.safeCall(tmp(xs)) } @@ -140,7 +140,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(LazyArray.__concat(...args)) } static get(xs, i) { - let scrut, scrut1, tmp, tmp1, tmp2; + let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ scrut = i >= xs.length; if (scrut === true) { throw globalThis.RangeError("Tuple.get: index out of bounds") @@ -173,7 +173,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(string.startsWith(prefix)) } static get(string, i) { - let scrut; + let scrut; /** scoped **/ scrut = i >= string.length; if (scrut === true) { throw globalThis.RangeError("Str.get: index out of bounds") @@ -209,7 +209,7 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let scrut, prev, tmp; + let scrut, prev, tmp; /** scoped **/ scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -221,7 +221,7 @@ globalThis.Object.freeze(class Runtime { } } static resetIndent(n) { - let scrut; + let scrut; /** scoped **/ scrut = TraceLogger.enabled; if (scrut === true) { TraceLogger.indentLvl = n; @@ -231,7 +231,7 @@ globalThis.Object.freeze(class Runtime { } } static log(msg) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4; + let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ scrut = TraceLogger.enabled; if (scrut === true) { tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); @@ -387,7 +387,7 @@ globalThis.Object.freeze(class Runtime { }) } delay() { - let lambda; + let lambda; /** scoped **/ lambda = (undefined, function (k) { Runtime.stackResume = k; return runtime.Unit @@ -409,13 +409,13 @@ globalThis.Object.freeze(class Runtime { } #v; zext() { - let tmp, tmp1; + let tmp, tmp1; /** scoped **/ tmp = Runtime.shl(1, 31); tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); return Runtime.bitand(this.#v, tmp1) } sext() { - let tmp; + let tmp; /** scoped **/ tmp = Runtime.shl(1, 31); return Runtime.bitor(this.#v, tmp) } @@ -427,7 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; + let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -437,7 +437,7 @@ globalThis.Object.freeze(class Runtime { }); scrut = runtime.short_or(tmp, lambda); if (scrut === true) { - let scrut1, scrut2, tmp12; + let scrut1, scrut2, tmp12; /** scoped **/ scrut1 = functionName.length > 0; if (scrut1 === true) { tmp12 = " '" + functionName; @@ -485,7 +485,7 @@ globalThis.Object.freeze(class Runtime { } } static deboundMethod(mtdName, clsName) { - let tmp, tmp1, tmp2, tmp3; + let tmp, tmp1, tmp2, tmp3; /** scoped **/ tmp = "[debinding error] Method '" + mtdName; tmp1 = tmp + "' of class '"; tmp2 = tmp1 + clsName; @@ -493,7 +493,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error(tmp3) } static try(f) { - let res; + let res; /** scoped **/ res = runtime.safeCall(f()); if (res instanceof Runtime.EffectSig.class) { return Runtime.EffectHandle(res) @@ -502,7 +502,7 @@ globalThis.Object.freeze(class Runtime { } } static printRaw(x) { - let rcd, tmp; + let rcd, tmp; /** scoped **/ rcd = globalThis.Object.freeze({ indent: 2, breakLength: 76 @@ -514,9 +514,9 @@ globalThis.Object.freeze(class Runtime { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } static topLevelEffect(tr, debug) { - let tmp, tmp1; + let tmp, tmp1; /** scoped **/ tmp2: while (true) { - let scrut, tmp3, tmp4, tmp5, tmp6; + let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ scrut = tr.handler === Runtime.PrintStackEffect; if (scrut === true) { tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); @@ -539,23 +539,23 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, tmp, tmp1, tmp2; + let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { - let tmp3; + let tmp3; /** scoped **/ tmp4: while (true) { - let scrut, cur, tmp5, tmp6; + let scrut, cur, tmp5, tmp6; /** scoped **/ scrut = curHandler !== null; if (scrut === true) { - let scrut1, tmp7, tmp8; + let scrut1, tmp7, tmp8; /** scoped **/ cur = curHandler.next; tmp9: while (true) { - let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; + let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ scrut2 = cur !== null; if (scrut2 === true) { - let scrut3, lambda, tmp19, tmp20; + let scrut3, lambda, tmp19, tmp20; /** scoped **/ locals = cur.getLocals; tmp10 = locals.length - 1; curLocals = runtime.safeCall(locals.at(tmp10)); @@ -572,7 +572,7 @@ globalThis.Object.freeze(class Runtime { scrut3 = curLocals.locals.length > 0; if (scrut3 === true) { lambda = (undefined, function (l) { - let tmp21, tmp22; + let tmp21, tmp22; /** scoped **/ tmp21 = l.localName + "="; tmp22 = Rendering.render(l.value); return tmp21 + tmp22 @@ -640,13 +640,13 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; + let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - let scrut, tmp5, tmp6, tmp7; + let scrut, tmp5, tmp6, tmp7; /** scoped **/ tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut1, tmp8, tmp9; + let scrut1, tmp8, tmp9; /** scoped **/ scrut1 = runtime.safeCall(m.has(cont)); if (scrut1 === true) { tmp8 = ", " + marker; @@ -660,7 +660,7 @@ globalThis.Object.freeze(class Runtime { tmp1 = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; + let scrut1; /** scoped **/ tmp5 = reps + 1; reps = tmp5; scrut1 = reps > 10; @@ -679,7 +679,7 @@ globalThis.Object.freeze(class Runtime { tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); return tmp3 + tmp4 } else { - let scrut2; + let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -689,12 +689,12 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda, tmp, tmp1, tmp2, tmp3; + let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ if (cont instanceof Runtime.HandlerContFrame.class) { - let scrut, tmp4, tmp5, tmp6; + let scrut, tmp4, tmp5, tmp6; /** scoped **/ result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut1, tmp7, tmp8; + let scrut1, tmp7, tmp8; /** scoped **/ scrut1 = runtime.safeCall(m.has(cont)); if (scrut1 === true) { tmp7 = ", " + marker; @@ -708,7 +708,7 @@ globalThis.Object.freeze(class Runtime { tmp = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; + let scrut1; /** scoped **/ tmp4 = reps + 1; reps = tmp4; scrut1 = reps > 10; @@ -727,7 +727,7 @@ globalThis.Object.freeze(class Runtime { tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); return tmp2 + tmp3 } else { - let scrut2; + let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -737,23 +737,23 @@ globalThis.Object.freeze(class Runtime { } } static debugCont(cont) { - let tmp, tmp1, tmp2; + let tmp, tmp1, tmp2; /** scoped **/ tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugHandler(cont) { - let tmp, tmp1, tmp2; + let tmp, tmp1, tmp2; /** scoped **/ tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ if (contTrace instanceof Runtime.ContTrace.class) { - let scrut, scrut1; + let scrut, scrut1; /** scoped **/ tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; if (scrut === true) { @@ -783,7 +783,7 @@ globalThis.Object.freeze(class Runtime { tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); cur = contTrace.nextHandler; tmp13: while (true) { - let scrut2, tmp14, tmp15; + let scrut2, tmp14, tmp15; /** scoped **/ scrut2 = cur !== null; if (scrut2 === true) { tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); @@ -803,7 +803,7 @@ globalThis.Object.freeze(class Runtime { } } static debugEff(eff) { - let tmp, tmp1, tmp2, tmp3; + let tmp, tmp1, tmp2, tmp3; /** scoped **/ if (eff instanceof Runtime.EffectSig.class) { tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); @@ -815,7 +815,7 @@ globalThis.Object.freeze(class Runtime { } } static mkEffect(handler, handlerFun) { - let res, tmp; + let res, tmp; /** scoped **/ tmp = new Runtime.ContTrace.class(null, null, null, null, false); res = new Runtime.EffectSig.class(tmp, handler, handlerFun); res.contTrace.last = res.contTrace; @@ -823,7 +823,7 @@ globalThis.Object.freeze(class Runtime { return res } static handleBlockImpl(cur, handler) { - let handlerFrame; + let handlerFrame; /** scoped **/ handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); cur.contTrace.lastHandler.nextHandler = handlerFrame; cur.contTrace.lastHandler = handlerFrame; @@ -831,7 +831,7 @@ globalThis.Object.freeze(class Runtime { return Runtime.handleEffects(cur) } static enterHandleBlock(handler, body) { - let cur; + let cur; /** scoped **/ cur = runtime.safeCall(body()); if (cur instanceof Runtime.EffectSig.class) { return Runtime.handleBlockImpl(cur, handler) @@ -840,11 +840,11 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let tmp; + let tmp; /** scoped **/ tmp1: while (true) { - let nxt, tmp2; + let nxt, tmp2; /** scoped **/ if (cur instanceof Runtime.EffectSig.class) { - let scrut; + let scrut; /** scoped **/ nxt = Runtime.handleEffect(cur); scrut = cur === nxt; if (scrut === true) { @@ -863,10 +863,10 @@ globalThis.Object.freeze(class Runtime { return tmp } static handleEffect(cur) { - let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; + let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ prevHandlerFrame = cur.contTrace; tmp6: while (true) { - let scrut1, scrut2; + let scrut1, scrut2; /** scoped **/ split_root$: { split_1$: { scrut1 = prevHandlerFrame.nextHandler !== null; @@ -903,7 +903,7 @@ globalThis.Object.freeze(class Runtime { tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); cur = tmp3; if (cur instanceof Runtime.EffectSig.class) { - let scrut3, scrut4; + let scrut3, scrut4; /** scoped **/ scrut3 = saved.next !== null; if (scrut3 === true) { cur.contTrace.last.next = saved.next; @@ -927,7 +927,7 @@ globalThis.Object.freeze(class Runtime { } static resume(contTrace) { return (value) => { - let scrut, tmp, tmp1; + let scrut, tmp, tmp1; /** scoped **/ scrut = contTrace.resumed; if (scrut === true) { throw globalThis.Error("Multiple resumption") @@ -940,19 +940,19 @@ globalThis.Object.freeze(class Runtime { } } static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, tmp; + let cont, handlerCont, curDepth, tmp; /** scoped **/ cont = contTrace.next; handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; tmp1: while (true) { - let tmp2, tmp3; + let tmp2, tmp3; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - let tmp4, tmp5; + let tmp4, tmp5; /** scoped **/ tmp2 = runtime.safeCall(cont.resume(value)); value = tmp2; Runtime.stackDepth = curDepth; if (value instanceof Runtime.EffectSig.class) { - let scrut, scrut1; + let scrut, scrut1; /** scoped **/ value.contTrace.last.next = cont.next; value.contTrace.lastHandler.nextHandler = handlerCont; scrut = contTrace.last !== cont; @@ -991,7 +991,7 @@ globalThis.Object.freeze(class Runtime { return tmp } static checkDepth() { - let scrut, tmp, lambda; + let scrut, tmp, lambda; /** scoped **/ tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -1004,14 +1004,14 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, tmp; + let result, tmp; /** scoped **/ Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); Runtime.stackDepth = 1; tmp1: while (true) { - let scrut, saved, tmp2; + let scrut, saved, tmp2; /** scoped **/ scrut = Runtime.stackResume !== null; if (scrut === true) { saved = Runtime.stackResume; @@ -1032,7 +1032,7 @@ globalThis.Object.freeze(class Runtime { return result } static plus_impl(lhs, rhs) { - let tmp; + let tmp; /** scoped **/ split_root$: { split_1$: { if (lhs instanceof Runtime.Int31.class) { diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index da40af046e..07725a0283 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,7 +81,7 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; f5 = function f() { let tmp4; tmp4 = TrmMod(); return tmp4 }; +//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 }; :fixme :expect 42 @@ -92,7 +92,7 @@ f :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; f6 = function f() { return TrmMod.class }; +//│ let f6; /** scoped **/ f6 = function f() { return TrmMod.class }; fun assertModule(module m: ClsMod): module ClsMod = m diff --git a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls index 630a7d1107..18ba41070d 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls @@ -31,7 +31,7 @@ fun foo = :sjs foo //│ JS (unsanitized): -//│ let tmp2; tmp2 = foo2(); tmp2 +//│ let tmp2; /** scoped **/ tmp2 = foo2(); tmp2 //│ > 1 //│ > ... diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 71e5c6cda1..9ac7a2e8e9 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -64,7 +64,7 @@ Infinity :sjs val Infinity = 1 //│ JS (unsanitized): -//│ let Infinity; Infinity = 1; +//│ let Infinity; /** scoped **/ Infinity = 1; //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Identifier 'Infinity' has already been declared //│ Infinity = Infinity @@ -105,7 +105,7 @@ fun main() = object Cls(val x) with fun huh = x //│ JS (unsanitized): -//│ let Cls1; +//│ let Cls1; /** scoped **/ //│ globalThis.Object.freeze(class Cls { //│ static { //│ Cls1.class = globalThis.Object.freeze(new this) @@ -202,7 +202,7 @@ class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let D1; +//│ let D1; /** scoped **/ //│ globalThis.Object.freeze(class D extends Predef.id { //│ static { //│ D1 = this @@ -251,9 +251,11 @@ set (x += 1; ()) fun baz = 1 fun bar() = baz //│ JS (unsanitized): -//│ let bar, baz; -//│ baz = function baz() { return 1 }; -//│ bar = function bar() { let tmp4; tmp4 = baz(); return tmp4 }; +//│ let bar, baz; /** scoped **/ +//│ baz = function baz() { +//│ return 1 +//│ }; +//│ bar = function bar() { let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 }; // ——— ——— ——— @@ -262,13 +264,13 @@ import "../../mlscript-compile/Stack.mls" // The parser rejects this, but it should be allowed. open Stack { ::, Nil } //│ ╔══[ERROR] Illegal 'open' statement shape. -//│ ║ l.263: open Stack { ::, Nil } +//│ ║ l.265: open Stack { ::, Nil } //│ ╙── ^^^^^^^^^^^^^^^ // Instead, if we parenthesize the operator, it is rejected by the elaborator. open Stack { (::), Nil } //│ ╔══[ERROR] Illegal 'open' statement element. -//│ ║ l.269: open Stack { (::), Nil } +//│ ║ l.271: open Stack { (::), Nil } //│ ╙── ^^^^ open Stack { Nil, :: } @@ -276,7 +278,7 @@ open Stack { Nil, :: } :sjs 1 :: Nil //│ ╔══[ERROR] Module 'Stack' does not contain member '::' -//│ ║ l.277: 1 :: Nil +//│ ║ l.279: 1 :: Nil //│ ╙── ^^ //│ JS (unsanitized): //│ Stack["::"](1, Stack.Nil) @@ -287,9 +289,9 @@ open Stack { Nil, :: } mkStr of ... // hello "oops" //│ ╔══[PARSE ERROR] Expected start of expression in this position; found new line instead -//│ ║ l.287: mkStr of ... // hello +//│ ║ l.289: mkStr of ... // hello //│ ║ ^ -//│ ║ l.288: "oops" +//│ ║ l.290: "oops" //│ ╙── //│ = "oops" @@ -312,25 +314,25 @@ Foo(1, 2, 3).args :todo if Foo(1, 2, 3) is Foo(...args) then args //│ ╔══[ERROR] Unrecognized pattern (spread). -//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^^^^ //│ ╔══[ERROR] Name not found: args -//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^ //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.313: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.315: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╔══[ERROR] The constructor does not take any arguments but found three arguments. -//│ ║ l.325: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] +//│ ║ l.327: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(arg) then arg //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.331: if Foo(1, 2, 3) is Foo(arg) then arg +//│ ║ l.333: if Foo(1, 2, 3) is Foo(arg) then arg //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error @@ -352,7 +354,7 @@ fun w(txt) = fs.writeFileSync(outFilePath, txt) // () //│ JS (unsanitized): -//│ let w; w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; +//│ let w; /** scoped **/ w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; w("whoops") //│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value. @@ -364,10 +366,10 @@ fun foo() = let bar(x: Str): Str = x + x bar("test") //│ ╔══[ERROR] Unsupported let binding shape -//│ ║ l.364: let bar(x: Str): Str = x + x +//│ ║ l.366: let bar(x: Str): Str = x + x //│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Expected 0 arguments, got 1 -//│ ║ l.365: bar("test") +//│ ║ l.367: bar("test") //│ ╙── ^^^^^^^^ // ——— ——— ——— @@ -377,10 +379,10 @@ fun foo() = module Foo(x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.377: module Foo(x) +//│ ║ l.379: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -401,10 +403,10 @@ module Foo(x) module Foo(val x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.401: module Foo(val x) +//│ ║ l.403: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7; /** scoped **/ //│ globalThis.Object.freeze(class Foo6 { //│ static { //│ Foo7 = this @@ -424,7 +426,7 @@ module Foo(val x) // TODO support syntax? data class Foo(mut x) //│ ╔══[ERROR] Expected a valid parameter, found 'mut'-modified identifier -//│ ║ l.425: data class Foo(mut x) +//│ ║ l.427: data class Foo(mut x) //│ ╙── ^ // ——— ——— ——— @@ -441,10 +443,10 @@ set this.pc = 0 set (this).pc = 0 //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.440: set this.pc = 0 +//│ ║ l.442: set this.pc = 0 //│ ╙── ^^^^ //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.442: (this).pc = 0 +//│ ║ l.444: (this).pc = 0 //│ ╙── ^^^^ // But this doesn't... @@ -453,10 +455,10 @@ set set this.pc = 0 //│ ╔══[PARSE ERROR] Unexpected static selector here -//│ ║ l.454: this.pc = 0 +//│ ║ l.456: this.pc = 0 //│ ╙── ^^^ //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.454: this.pc = 0 +//│ ║ l.456: this.pc = 0 //│ ╙── ^^^^ // ——— ——— ——— @@ -468,7 +470,7 @@ pattern A(pattern B) = B fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ║ l.469: fun foo(x) = if x is @annotations.compile A(0) as y then y +//│ ║ l.471: fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╙── ^ // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls index 037ca2f59d..8c99b7c926 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls @@ -15,7 +15,7 @@ x :sjs val ++ = 0 //│ JS (unsanitized): -//│ let $_$_; $_$_ = 0; +//│ let $_$_; /** scoped **/ $_$_ = 0; //│ ++ = 0 :sjs @@ -51,7 +51,7 @@ fun ++ z = 0 :sjs ++ //│ JS (unsanitized): -//│ let tmp; tmp = $_$_2(); tmp +//│ let tmp; /** scoped **/ tmp = $_$_2(); tmp //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 4982f7cc87..798b3547b0 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let tmp1, Foo1, tmp2; +//│ let tmp1, Foo1, tmp2; /** scoped **/ //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index 0a8b33ff10..df691692ad 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index cce678bd28..1c0092c0d9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp, tmp1; /** scoped **/ tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; +//│ let a, b, tmp2, tmp3; /** scoped **/ //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a @@ -22,7 +22,7 @@ drop { a: 0, b: 1 } :sjs let discard = drop _ //│ JS (unsanitized): -//│ let discard; +//│ let discard; /** scoped **/ //│ let discard1; //│ discard1 = function discard(_0) { return runtime.Unit }; //│ discard = discard1; diff --git a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls index d91b3fba16..708cfa4897 100644 --- a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls +++ b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls @@ -6,7 +6,7 @@ { a: 1 } //│ JS (unsanitized): -//│ let a; a = 1; globalThis.Object.freeze({ a: a }) +//│ let a; /** scoped **/ a = 1; globalThis.Object.freeze({ a: a }) //│ = {a: 1} { "a": 1 } @@ -35,7 +35,7 @@ :sjs { (id(0)): 1 } //│ JS (unsanitized): -//│ let tmp; tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) +//│ let tmp; /** scoped **/ tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) //│ = {"0": 1} diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index 6908159775..08805e2040 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,7 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let tmp, Baz1; +//│ let tmp, Baz1; /** scoped **/ //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index 26fb4b1450..dd9ce4d58f 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -17,11 +17,11 @@ fun buildPalindrome = case 0 then [] n then [n, ..buildPalindrome(n - 1), n] //│ JS (unsanitized): -//│ let buildPalindrome; +//│ let buildPalindrome; /** scoped **/ //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; +//│ let n, tmp3, tmp4, tmp5; /** scoped **/ //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -48,11 +48,11 @@ fun sum2 = case [] then 0 [x, ..xs, y] then x + y + sum2(xs) //│ JS (unsanitized): -//│ let sum2; +//│ let sum2; /** scoped **/ //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls index 39b9941e38..2299883a06 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls @@ -46,7 +46,7 @@ M.Foo:: n(foo, 2) :sjs let m = M.Foo::m //│ JS (unsanitized): -//│ let m; let m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; +//│ let m; /** scoped **/ let m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; //│ m = fun m m(foo) @@ -132,7 +132,7 @@ Foo::n(foo, 2) //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ═══[ERROR] Expected a statically known class; found ‹error›. //│ JS (unsanitized): -//│ let lambda9; +//│ let lambda9; /** scoped **/ //│ lambda9 = (undefined, function (self, ...args) { //│ return runtime.safeCall(self.n(...args)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls index 2c3bbe2920..4cffa34572 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls @@ -54,7 +54,7 @@ xs \ //│ ║ l.52: map(x => x * 2) //│ ╙── ^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let lambda5, tmp4; +//│ let lambda5, tmp4; /** scoped **/ //│ lambda5 = (undefined, function (x) { return x * 2 }); //│ tmp4 = map(lambda5); //│ Predef.passTo(xs1, tmp4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index c76804e25b..b0d56d3949 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -77,10 +77,10 @@ Foo(1, 2) :sjs let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ let f1; //│ f1 = function f(x, y) { -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ tmp4 = Foo7(x, y); //│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 178203f9e4..9c2202fc04 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -6,7 +6,7 @@ fun f(n1: Int): Int = n1 //│ JS (unsanitized): -//│ let f; f = function f(n1) { return n1 }; +//│ let f; /** scoped **/ f = function f(n1) { return n1 }; f(42) //│ JS (unsanitized): @@ -19,23 +19,24 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): -//│ let f1; f1 = function f(n1) { return (n2) => { let tmp; tmp = 10 * n1; return tmp + n2 } }; +//│ let f1; /** scoped **/ +//│ f1 = function f(n1) { return (n2) => { let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } }; // TODO compile this to // this.f$(4, 2) f(4)(2) //│ JS (unsanitized): -//│ let tmp; tmp = f1(4); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = f1(4); runtime.safeCall(tmp(2)) //│ = 42 fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ let tmp1, tmp2, tmp3; +//│ let tmp1, tmp2, tmp3; /** scoped **/ //│ tmp1 = 10 * n1; //│ tmp2 = tmp1 + n2; //│ tmp3 = 10 * tmp2; @@ -46,17 +47,20 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) +//│ let tmp1, tmp2; /** scoped **/ +//│ tmp1 = f2(4); +//│ tmp2 = runtime.safeCall(tmp1(2)); +//│ runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { //│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ //│ tmp3 = 10 * n1; //│ tmp4 = tmp3 + n2; //│ tmp5 = 10 * tmp4; @@ -70,7 +74,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; +//│ let tmp3, tmp4, tmp5; /** scoped **/ //│ tmp3 = f3(3); //│ tmp4 = runtime.safeCall(tmp3(0)); //│ tmp5 = runtime.safeCall(tmp4(3)); diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index 5025320ce4..aa73bdb0d9 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,7 +80,7 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; +//│ let scrut, tmp17; /** scoped **/ //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls index 58a398ea87..d95bc500e7 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls @@ -4,7 +4,7 @@ :sjs let t = [0, 1, 2] //│ JS (unsanitized): -//│ let t; t = globalThis.Object.freeze([ 0, 1, 2 ]); +//│ let t; /** scoped **/ t = globalThis.Object.freeze([ 0, 1, 2 ]); //│ t = [0, 1, 2] :re @@ -21,7 +21,7 @@ t :sjs let t = mut [0, 1, 2] //│ JS (unsanitized): -//│ let t1; t1 = [ 0, 1, 2 ]; +//│ let t1; /** scoped **/ t1 = [ 0, 1, 2 ]; //│ t = [0, 1, 2] t.push(4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls index 0c8a48f3a4..7efae81a91 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls @@ -46,7 +46,7 @@ r.foo :sjs let r = {} //│ JS (unsanitized): -//│ let r3; r3 = runtime.Unit; +//│ let r3; /** scoped **/ r3 = runtime.Unit; //│ r = () :re diff --git a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls index 408d7db105..655c4d0a45 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls @@ -33,7 +33,7 @@ O.gy :sjs class Foo(x, val y, mut val z) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x, y, z) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index 660664e019..790005a3c5 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; f = globalThis.Object.freeze(new Foo()); +//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; f1 = new Foo(); +//│ let f1; /** scoped **/ f1 = new Foo(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index 72c154d117..100957af54 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -109,16 +109,19 @@ passTo(1, add(., 1) @ _ + _)(2) :sjs let f = add(_, 1) //│ JS (unsanitized): -//│ let f; let f1; f1 = function f(_0) { let tmp15; tmp15 = add(); return tmp15(_0, 1) }; f = f1; +//│ let f; /** scoped **/ +//│ let f1; +//│ f1 = function f(_0) { let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) }; +//│ f = f1; //│ f = fun f :fixme let f = add(., 1) //│ ╔══[PARSE ERROR] Expected an expression; found period instead -//│ ║ l.116: let f = add(., 1) +//│ ║ l.119: let f = add(., 1) //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected period here -//│ ║ l.116: let f = add(., 1) +//│ ║ l.119: let f = add(., 1) //│ ╙── ^ //│ ═══[RUNTIME ERROR] Error: Function expected 2 arguments but got 0 //│ f = undefined diff --git a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls index 36ad46feac..c9d079ed1c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls @@ -56,7 +56,7 @@ fun foo() = :sjs 1; id(2) //│ JS (unsanitized): -//│ let tmp; tmp = Predef.id(2); (1 , tmp) +//│ let tmp; /** scoped **/ tmp = Predef.id(2); (1 , tmp) //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index df1eb55c0e..02207fb129 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -4,7 +4,9 @@ :sjs 1 && 2 //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function () { return 2 }); runtime.short_and(1, lambda) +//│ let lambda; /** scoped **/ +//│ lambda = (undefined, function () { return 2 }); +//│ runtime.short_and(1, lambda) //│ = 2 1 || 2 @@ -18,7 +20,9 @@ fun test(x) = :sjs 123 || test(42) //│ JS (unsanitized): -//│ let lambda2; lambda2 = (undefined, function () { return test(42) }); runtime.short_or(123, lambda2) +//│ let lambda2; /** scoped **/ +//│ lambda2 = (undefined, function () { return test(42) }); +//│ runtime.short_or(123, lambda2) //│ = 123 0 || test(42) @@ -32,9 +36,9 @@ fun test(x) = :sjs fold(||)(0, false, 42, 123) //│ JS (unsanitized): -//│ let lambda5, tmp; +//│ let lambda5, tmp; /** scoped **/ //│ lambda5 = (undefined, function (arg1, arg2) { -//│ let lambda6; +//│ let lambda6; /** scoped **/ //│ lambda6 = (undefined, function () { //│ return arg2 //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index 666d7e6f4c..b9d1163e58 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ let scrut; scrut = "a"; if (typeof scrut === 'string') { true } else { false } +//│ let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index 22d75bb559..8e2f87f7a8 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -61,7 +61,7 @@ print(_) :sjs let test = _.f(0, _, 2) //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ let test1; //│ test1 = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; //│ test = test1; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index e3195424de..81c6dbe260 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -31,7 +31,7 @@ :sjs let x = 1 in x + 1 //│ JS (unsanitized): -//│ let x; x = 1; x + 1 +//│ let x; /** scoped **/ x = 1; x + 1 //│ = 2 //│ Type: Int @@ -61,7 +61,7 @@ false :sjs data class Foo(x: Int) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x1) { //│ return globalThis.Object.freeze(new Foo.class(x1)); //│ }; @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; foo = globalThis.Object.freeze(new Foo.class(42)); foo.x +//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo.class(42)); foo.x //│ = 42 //│ Type: Int @@ -97,7 +97,7 @@ let foo = new Foo(42) in foo.Foo#x :sjs fun inc(x) = x + 1 //│ JS (unsanitized): -//│ let inc; inc = function inc(x1) { return x1 + 1 }; +//│ let inc; /** scoped **/ inc = function inc(x1) { return x1 + 1 }; //│ Type: ⊤ @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ let scrut; scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } +//│ let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ let scrut1; scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } +//│ let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } //│ = 1 //│ Type: Int @@ -134,7 +134,7 @@ data class Foo() let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ foo1 = globalThis.Object.freeze(new Foo2.class()); //│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } //│ = 1 @@ -147,11 +147,11 @@ fun pow(x) = case 0 then 1 n then x * pow(x)(n-1) //│ JS (unsanitized): -//│ let pow; +//│ let pow; /** scoped **/ //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; +//│ let n, tmp, tmp1, tmp2; /** scoped **/ //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -172,7 +172,7 @@ fun nott = case true then false false then true //│ JS (unsanitized): -//│ let nott; +//│ let nott; /** scoped **/ //│ nott = function nott() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { @@ -193,7 +193,7 @@ fun nott = case :sjs nott of false //│ JS (unsanitized): -//│ let tmp; tmp = nott(); tmp(false) +//│ let tmp; /** scoped **/ tmp = nott(); tmp(false) //│ = true //│ Type: Bool @@ -203,11 +203,11 @@ fun fact = case 0 then 1 n then n * fact(n - 1) //│ JS (unsanitized): -//│ let fact; +//│ let fact; /** scoped **/ //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; +//│ let n, tmp1, tmp2, tmp3; /** scoped **/ //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -258,7 +258,7 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let y; +//│ let y; /** scoped **/ //│ let x4; //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); @@ -270,7 +270,7 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let y1; +//│ let y1; /** scoped **/ //│ let x5; //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 48f541fb92..50986bddaa 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -87,11 +87,11 @@ fun test2() = //│ ║ l.83: case 0 then 0 //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let test22; +//│ let test22; /** scoped **/ //│ test22 = function test2() { -//│ let funny, tmp1; +//│ let funny, tmp1; /** scoped **/ //│ funny = function funny() { -//│ let lambda; +//│ let lambda; /** scoped **/ //│ let lambda1; //│ lambda = (undefined, function (caseScrut) { //│ if (caseScrut === 0) { @@ -101,7 +101,7 @@ fun test2() = //│ } //│ }); //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp2, tmp3, tmp4; +//│ let n, tmp2, tmp3, tmp4; /** scoped **/ //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index 9cb5c5210c..d7ff1e8b8d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -11,7 +11,7 @@ empty.0 :sjs let single = [1] //│ JS (unsanitized): -//│ let single; single = globalThis.Object.freeze([ 1 ]); +//│ let single; /** scoped **/ single = globalThis.Object.freeze([ 1 ]); //│ single = [1] single.0 @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls index ce774c920f..0d443fdd05 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls @@ -9,7 +9,7 @@ object Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar1; +//│ let Bar1; /** scoped **/ //│ globalThis.Object.freeze(class Bar { //│ static { //│ Bar1 = globalThis.Object.freeze(new this) @@ -49,7 +49,7 @@ module Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar3; +//│ let Bar3; /** scoped **/ //│ globalThis.Object.freeze(class Bar2 { //│ static { //│ Bar3 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls index ed25930a28..454e29826f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls @@ -29,7 +29,7 @@ new 2 + 2 :re new! 2 + 2 //│ JS (unsanitized): -//│ let tmp1; tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 +//│ let tmp1; /** scoped **/ tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 //│ ═══[RUNTIME ERROR] TypeError: 2 is not a constructor :e diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index 7bf650836b..db1624542a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -39,7 +39,7 @@ print("Hi") print("Hi") 2 //│ JS (unsanitized): -//│ let tmp; tmp = Predef.print("Hi"); 2 +//│ let tmp; /** scoped **/ tmp = Predef.print("Hi"); 2 //│ Lowered: //│ Program: //│ imports = Nil diff --git a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls index 51390245ed..70dc34b630 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls @@ -74,7 +74,7 @@ id(+)(1, 2) :re id(+)(1) //│ JS (unsanitized): -//│ let lambda4, tmp1; +//│ let lambda4, tmp1; /** scoped **/ //│ lambda4 = (undefined, function (arg1, arg2) { //│ return arg1 + arg2 //│ }); @@ -95,7 +95,7 @@ fun (+) lol(a, b) = [a, b] :sjs id(~)(2) //│ JS (unsanitized): -//│ let lambda5, tmp2; +//│ let lambda5, tmp2; /** scoped **/ //│ lambda5 = (undefined, function (arg) { //│ return ~ arg //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 6bc047ff4f..8be8e2459a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -15,9 +15,9 @@ fun test(x) = Some(v) then print(v) None then print("none") //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ //│ if (x instanceof Some.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 3a0f89e47f..8e0b86561a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -7,7 +7,13 @@ case x then x :sjs case { x then x } //│ JS (unsanitized): -//│ let lambda1; lambda1 = (undefined, function (caseScrut) { let x; x = caseScrut; return x }); lambda1 +//│ let lambda1; +//│ lambda1 = (undefined, function (caseScrut) { +//│ let x; /** scoped **/ +//│ x = caseScrut; +//│ return x +//│ }); +//│ lambda1 //│ = fun :sjs @@ -53,10 +59,10 @@ case 0 then true :todo // TODO: support this braceless syntax? case [x] then x, [] then 0 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.54: case [x] then x, [] then 0 +//│ ║ l.60: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^ //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.54: case [x] then x, [] then 0 +//│ ║ l.60: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^^^^^^^ :sjs @@ -79,7 +85,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let lambda12, isDefined; +//│ let lambda12, isDefined; /** scoped **/ //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index e633f026e7..a3946765e5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,7 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, Outer1; +//│ let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, Outer1; /** scoped **/ //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -60,7 +60,7 @@ data class Outer(a, b) with //│ return this.Inner(c) //│ } //│ o2(c, d) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ tmp6 = this.Inner(c); //│ return tmp6.i1(d) //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index b5c1b26b34..696a1e22bd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -6,9 +6,9 @@ fun test(a) = class C with { val x = a } new C //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test(a) { -//│ let C1; +//│ let C1; /** scoped **/ //│ globalThis.Object.freeze(class C { //│ static { //│ C1 = this @@ -37,9 +37,9 @@ fun test(x) = data class Foo(a, b) Foo(x, x + 1) //│ JS (unsanitized): -//│ let test2; +//│ let test2; /** scoped **/ //│ test2 = function test(x) { -//│ let Foo2, tmp; +//│ let Foo2, tmp; /** scoped **/ //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 0e8998d346..b4df883b66 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,7 +9,7 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; +//│ let scrut, x, argument0$; /** scoped **/ //│ scrut = Some(0); //│ if (scrut instanceof Some.class) { //│ argument0$ = scrut.value; @@ -26,7 +26,7 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ let x1, argument0$1; +//│ let x1, argument0$1; /** scoped **/ //│ if (s instanceof Some.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; @@ -55,7 +55,7 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; +//│ let x4, argument0$4; /** scoped **/ //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -118,9 +118,9 @@ fun f(x) = if x is None then "ok" else print("oops") //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp6; +//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { @@ -164,9 +164,9 @@ fun f(x) = if x is Some(u) then u Pair(a, b) then a + b //│ JS (unsanitized): -//│ let f5; +//│ let f5; /** scoped **/ //│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; +//│ let u, a, b, argument0$4, argument1$; /** scoped **/ //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; @@ -194,9 +194,9 @@ fun f(x) = print of if x is None then "ok" else "oops" //│ JS (unsanitized): -//│ let f6; +//│ let f6; /** scoped **/ //│ f6 = function f(x3) { -//│ let argument0$4, tmp10; +//│ let argument0$4, tmp10; /** scoped **/ //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls index 1c596f3fc1..fc79ff4035 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls @@ -6,7 +6,7 @@ data class Foo(arguments) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(arguments1) { //│ return globalThis.Object.freeze(new Foo.class(arguments1)); //│ }; @@ -24,7 +24,7 @@ data class Foo(arguments) data class Foo(eval) //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ Foo3 = function Foo(eval1) { //│ return globalThis.Object.freeze(new Foo.class(eval1)); //│ }; @@ -42,7 +42,7 @@ data class Foo(eval) data class Foo(static) //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ Foo5 = function Foo(static1) { //│ return globalThis.Object.freeze(new Foo.class(static1)); //│ }; @@ -60,7 +60,7 @@ data class Foo(static) data class Foo(v') //│ JS (unsanitized): -//│ let Foo7; +//│ let Foo7; /** scoped **/ //│ Foo7 = function Foo(v$_) { //│ return globalThis.Object.freeze(new Foo.class(v$_)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index 7ba95074d3..af16010767 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -7,17 +7,27 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f; -//│ f = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; +//│ let f; /** scoped **/ +//│ f = function f() { +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 +//│ }; fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): -//│ let f1; -//│ f1 = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; +//│ let f1; /** scoped **/ +//│ f1 = function f() { +//│ let tmp; /** scoped **/ +//│ tmp = runtime.safeCall(globalThis.console.log("ok")); +//│ return 42 +//│ }; fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f2; f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; 42 +//│ let f2; /** scoped **/ +//│ f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; +//│ 42 //│ = 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index e3840f5a0c..a4d877e00c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,14 +7,14 @@ :silent declare val console //│ JS (unsanitized): -//│ let console; +//│ let console; /** scoped **/ console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.console.log("a")); //│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 @@ -25,7 +25,7 @@ console.log("b") let l = console.log l(123) //│ JS (unsanitized): -//│ let l; l = globalThis.console.log; runtime.safeCall(l(123)) +//│ let l; /** scoped **/ l = globalThis.console.log; runtime.safeCall(l(123)) //│ > 123 //│ l = fun log @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let tmp2, x, tmp3, tmp4, y; +//│ let tmp2, x, tmp3, tmp4, y; /** scoped **/ //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index 9d733318be..c2ef84645e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; x = undefined; +//│ let x; /** scoped **/ x = undefined; //│ x = undefined x = 1 @@ -31,7 +31,7 @@ x let y = 1 //│ JS (unsanitized): -//│ let y; y = 1; +//│ let y; /** scoped **/ y = 1; //│ y = 1 :e @@ -45,7 +45,7 @@ z = 1 fun f() = 1 //│ JS (unsanitized): -//│ let f; f = function f() { return 1 }; +//│ let f; /** scoped **/ f = function f() { return 1 }; f //│ JS (unsanitized): @@ -56,7 +56,7 @@ f let f f(x) = x + 1 //│ JS (unsanitized): -//│ let f1; let f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; +//│ let f1; /** scoped **/ let f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; //│ f = fun f f(1) @@ -68,7 +68,7 @@ f(1) let foo foo = 0 //│ JS (unsanitized): -//│ let foo; foo = 0; +//│ let foo; /** scoped **/ foo = 0; //│ foo = 0 :fixme @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; +//│ let foo1, scrut; /** scoped **/ //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; +//│ let foo2, scrut1; /** scoped **/ //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -105,7 +105,7 @@ else fun f() = foo = 42 //│ JS (unsanitized): -//│ let f3; f3 = function f() { foo2 = 42; return runtime.Unit }; +//│ let f3; /** scoped **/ f3 = function f() { foo2 = 42; return runtime.Unit }; f() //│ JS (unsanitized): @@ -123,6 +123,6 @@ fun f() = foo = 0 //│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let f4; f4 = function f() { return foo2 }; +//│ let f4; /** scoped **/ f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 797b5c0c47..1c439570a2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -18,9 +18,9 @@ fun f(x) = return 0 x + 1 //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f(x) { -//│ let scrut, tmp, tmp1; +//│ let scrut, tmp, tmp1; /** scoped **/ //│ scrut = x < 0; //│ if (scrut === true) { //│ tmp = Predef.print("whoops"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index e1c8f61c3f..05a4d15e22 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -7,7 +7,7 @@ let discard = drop _ //│ JS (unsanitized): -//│ let discard; +//│ let discard; /** scoped **/ //│ let discard1; //│ discard1 = function discard(_0) { return runtime.Unit }; //│ discard = discard1; @@ -18,7 +18,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; +//│ let a, b, tmp; /** scoped **/ //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -26,7 +26,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; +//│ let a1, b1, tmp1; /** scoped **/ //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -35,14 +35,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; +//│ let aaaa, tmp2; /** scoped **/ //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let bbbb, tmp3, aaaaaaaaa; +//│ let bbbb, tmp3, aaaaaaaaa; /** scoped **/ //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -53,7 +53,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let ccccccccc, dddddddddd, tmp4, aaaaaaaaa1, bbbbbbbb; +//│ let ccccccccc, dddddddddd, tmp4, aaaaaaaaa1, bbbbbbbb; /** scoped **/ //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -69,7 +69,7 @@ discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ tmp5 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb" @@ -78,7 +78,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ] //│ JS (unsanitized): -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ tmp6 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", @@ -89,7 +89,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; +//│ let tmp7, tmp8; /** scoped **/ //│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 590e01ab7e..1078c18fd7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -19,9 +19,9 @@ fun test(a) = h(d) Inner(42) //│ JS (unsanitized): -//│ let test1; +//│ let test1; /** scoped **/ //│ test1 = function test(a) { -//│ let Inner1, tmp; +//│ let Inner1, tmp; /** scoped **/ //│ Inner1 = function Inner(b) { //│ return globalThis.Object.freeze(new Inner.class(b)); //│ }; @@ -41,7 +41,7 @@ fun test(a) = //│ ]) //│ } //│ g(d) { -//│ let h; +//│ let h; /** scoped **/ //│ const this$Inner = this; //│ h = function h(e) { //│ return globalThis.Object.freeze([ @@ -78,9 +78,9 @@ fun test(a) = print of [a, b] [C1(1), C2(2)] //│ JS (unsanitized): -//│ let test2; +//│ let test2; /** scoped **/ //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1, tmp2, tmp3; +//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -138,7 +138,7 @@ data class Foo(a) with foo() Foo(123) //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -151,7 +151,7 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz, tmp; +//│ let bar, baz, tmp; /** scoped **/ //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a @@ -177,7 +177,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1, tmp; +//│ let Bar1, tmp; /** scoped **/ //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -192,7 +192,7 @@ Bar(1) //│ } //│ foo() { //│ return () => { -//│ let bar; +//│ let bar; /** scoped **/ //│ const this$Bar = this; //│ bar = function bar() { //│ return this$Bar.x diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index 2056e412ce..8bb0ca62d1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -6,14 +6,14 @@ val x = 2 fun foo() = x + 1 //│ JS (unsanitized): -//│ let foo, x; foo = function foo() { return x + 1 }; x = 2; +//│ let foo, x; /** scoped **/ foo = function foo() { return x + 1 }; x = 2; //│ x = 2 :sjs class Test with print(foo()) //│ JS (unsanitized): -//│ let tmp, Test1; +//│ let tmp, Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 47705c69c8..033d71e19c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -4,14 +4,14 @@ :sjs fun t = 42 //│ JS (unsanitized): -//│ let t; t = function t() { return 42 }; +//│ let t; /** scoped **/ t = function t() { return 42 }; :expect 42 :sjs t //│ JS (unsanitized): -//│ let tmp; tmp = t(); tmp +//│ let tmp; /** scoped **/ tmp = t(); tmp //│ = 42 @@ -37,11 +37,11 @@ fun test() = 42 whoops + whoops //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test() { -//│ let whoops, tmp1, tmp2; +//│ let whoops, tmp1, tmp2; /** scoped **/ //│ whoops = function whoops() { -//│ let tmp3; +//│ let tmp3; /** scoped **/ //│ tmp3 = Predef.print("ok"); //│ return 42 //│ }; @@ -65,7 +65,7 @@ module T with val c = p val d = this.p //│ JS (unsanitized): -//│ let T1; +//│ let T1; /** scoped **/ //│ globalThis.Object.freeze(class T { //│ static { //│ T1 = this @@ -107,7 +107,7 @@ T.d module M with fun t = 0 //│ JS (unsanitized): -//│ let M1; +//│ let M1; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -135,9 +135,9 @@ fun test() = fun whoops = 42 whoops //│ JS (unsanitized): -//│ let test1; +//│ let test1; /** scoped **/ //│ test1 = function test() { -//│ let whoops, tmp1; +//│ let whoops, tmp1; /** scoped **/ //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -163,7 +163,8 @@ fun bar() = fun baz() = 42 baz //│ JS (unsanitized): -//│ let bar; bar = function bar() { let baz; baz = function baz() { return 42 }; return baz }; +//│ let bar; /** scoped **/ +//│ bar = function bar() { let baz; /** scoped **/ baz = function baz() { return 42 }; return baz }; :sjs @@ -172,9 +173,9 @@ fun baz() = fun z = 2 (x, y) => x + y + w + z //│ JS (unsanitized): -//│ let baz; +//│ let baz; /** scoped **/ //│ baz = function baz() { -//│ let w, z; +//│ let w, z; /** scoped **/ //│ let lambda; //│ w = function w() { //│ return 1 @@ -183,7 +184,7 @@ fun baz() = //│ return 2 //│ }; //│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3, tmp4; +//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp1 = x + y; //│ tmp2 = w(); //│ tmp3 = tmp1 + tmp2; @@ -206,14 +207,14 @@ fun a() = b + d c //│ JS (unsanitized): -//│ let a; +//│ let a; /** scoped **/ //│ a = function a() { -//│ let b, c; +//│ let b, c; /** scoped **/ //│ b = function b() { //│ return 1 //│ }; //│ c = function c() { -//│ let d, tmp2, tmp3; +//│ let d, tmp2, tmp3; /** scoped **/ //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -235,14 +236,14 @@ fun b() = c d //│ JS (unsanitized): -//│ let b; +//│ let b; /** scoped **/ //│ b = function b() { -//│ let c, d; +//│ let c, d; /** scoped **/ //│ c = function c() { //│ return 1 //│ }; //│ d = function d() { -//│ let c1, tmp3; +//│ let c1, tmp3; /** scoped **/ //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -263,14 +264,14 @@ fun c() = e + f d //│ JS (unsanitized): -//│ let c; +//│ let c; /** scoped **/ //│ c = function c() { -//│ let d, f, tmp4; +//│ let d, f, tmp4; /** scoped **/ //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e, tmp5, tmp6; +//│ let e, tmp5, tmp6; /** scoped **/ //│ e = function e() { //│ return 1 //│ }; @@ -291,7 +292,7 @@ c() data class Foo(x) with fun oops = x //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index 883fdd4d3e..7c40714263 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,7 +21,7 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = false; //│ if (scrut === true) { //│ 0 @@ -37,9 +37,9 @@ fun foo() = if false then 0 foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() { -//│ let scrut1; +//│ let scrut1; /** scoped **/ //│ scrut1 = false; //│ if (scrut1 === true) { //│ return 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index cf22d5043d..379380d95c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -10,7 +10,7 @@ object Test with print(Test) Test.x //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = globalThis.Object.freeze(new this) @@ -22,7 +22,7 @@ object Test with //│ }) //│ } //│ foo() { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = Predef.print(Test1); //│ return Test1.x //│ } @@ -46,7 +46,7 @@ Test.foo() :sjs val Test = "oops" //│ JS (unsanitized): -//│ let Test2; Test2 = "oops"; +//│ let Test2; /** scoped **/ Test2 = "oops"; //│ Test = "oops" :re @@ -60,7 +60,13 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let x, f, x1; let f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) +//│ let x, f, x1; /** scoped **/ +//│ let f1; +//│ x = 1; +//│ f1 = function f() { return x }; +//│ f = f1; +//│ x1 = 2; +//│ runtime.safeCall(f()) //│ = 1 //│ f = fun f //│ x = 2 @@ -72,10 +78,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.73: let x = 2 +//│ ║ l.79: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.72: val x = 1 +//│ ║ l.78: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test4 { @@ -115,7 +121,7 @@ data class Cls(x) with fun bar = x print(this.x, x) //│ JS (unsanitized): -//│ let Cls1; +//│ let Cls1; /** scoped **/ //│ Cls1 = function Cls(x2) { //│ return globalThis.Object.freeze(new Cls.class(x2)); //│ }; @@ -179,7 +185,7 @@ module Whoops with val w: module Whoops = this fun g() = f() //│ JS (unsanitized): -//│ let Whoops2; +//│ let Whoops2; /** scoped **/ //│ globalThis.Object.freeze(class Whoops { //│ static { //│ Whoops2 = this @@ -226,7 +232,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.227: Runtime +//│ ║ l.233: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index fcfb8fc9e7..acf18dbe75 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let lambda, f; +//│ let lambda, f; /** scoped **/ //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,9 +26,9 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let lambda1, f1; +//│ let lambda1, f1; /** scoped **/ //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } @@ -41,9 +41,9 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let lambda2, f2; +//│ let lambda2, f2; /** scoped **/ //│ lambda2 = (undefined, function (x) { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls index 07b13f5dfb..e2425e33b6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls @@ -39,7 +39,9 @@ Some(1) :sjs (new Some(1)) isDefined() //│ JS (unsanitized): -//│ let tmp3; tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); Option.isDefined(tmp3) +//│ let tmp3; /** scoped **/ +//│ tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); +//│ Option.isDefined(tmp3) //│ = true new Some(1) isDefined() @@ -66,14 +68,14 @@ None == Option.None :re Option.oops //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.67: Option.oops +//│ ║ l.69: Option.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' :e open Option { oops } //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.74: open Option { oops } +//│ ║ l.76: open Option { oops } //│ ╙── ^^^^ oops @@ -82,7 +84,7 @@ oops :re Option.None.oops //│ ╔══[ERROR] Object 'None' does not contain member 'oops' -//│ ║ l.83: Option.None.oops +//│ ║ l.85: Option.None.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index 9ade6db65e..79d3e59992 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -11,8 +11,12 @@ fun foo() = "a" ~ "b" ~ "c" foo() //│ JS (unsanitized): -//│ let foo; -//│ foo = function foo() { let tmp; tmp = M.concat("a", "b"); return M.concat(tmp, "c") }; +//│ let foo; /** scoped **/ +//│ foo = function foo() { +//│ let tmp; /** scoped **/ +//│ tmp = M.concat("a", "b"); +//│ return M.concat(tmp, "c") +//│ }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index a23da7d877..fbe153c46d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -3,9 +3,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda; +//│ let lambda; /** scoped **/ //│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -18,9 +18,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda1; +//│ let lambda1; /** scoped **/ //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3, tmp4; +//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -34,13 +34,13 @@ :sjs (x => x) + 1 //│ JS (unsanitized): -//│ let lambda2; lambda2 = (undefined, function (x) { return x }); lambda2 + 1 +//│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 //│ = "function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }1" :sjs 1 + (x => x) //│ JS (unsanitized): -//│ let lambda3; lambda3 = (undefined, function (x) { return x }); 1 + lambda3 +//│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 //│ = "1function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls index cf8b2bf680..86a50c8394 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls @@ -8,7 +8,7 @@ x => let y = x y //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function (x) { let y; y = x; return y }); lambda +//│ let lambda; lambda = (undefined, function (x) { let y; /** scoped **/ y = x; return y }); lambda //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 070e1f96ea..41a5aa52ba 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -58,7 +58,7 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ let tmp; +//│ let tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ split_1$: { @@ -107,7 +107,7 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let tmp1, x, tmp2, tmp3; +//│ let tmp1, x, tmp2, tmp3; /** scoped **/ //│ if (a instanceof A.class) { //│ tmp1 = 1; //│ } else if (a instanceof B.class) { @@ -115,7 +115,7 @@ print(x) //│ } else if (a instanceof C.class) { //│ tmp1 = 3; //│ } else { -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ tmp3 = 3; //│ if (a instanceof D.class) { //│ if (a instanceof A.class) { @@ -147,7 +147,7 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ tmp5 = 2; //│ if (a instanceof A.class) { //│ 1 @@ -169,7 +169,7 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ if (a instanceof A.class) { //│ 1 //│ } else { @@ -192,7 +192,7 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ let x1, tmp7; +//│ let x1, tmp7; /** scoped **/ //│ if (a instanceof A.class) { //│ 1 //│ } else if (a instanceof B.class) { @@ -221,7 +221,7 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ //│ if (a instanceof B.class) { //│ 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index 927503ab39..18f3fbd3bc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,7 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let tmp1, Test1; +//│ let tmp1, Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index 0005648013..dc8f7d5442 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -4,7 +4,7 @@ :sjs module None //│ JS (unsanitized): -//│ let None1; +//│ let None1; /** scoped **/ //│ globalThis.Object.freeze(class None { //│ static { //│ None1 = this @@ -55,7 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1, tmp; +//│ let M1, tmp; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -122,7 +122,7 @@ M.oops module M with val m: module M = M //│ JS (unsanitized): -//│ let M3; +//│ let M3; /** scoped **/ //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -155,7 +155,7 @@ module AA with val y = 2 [x, y] //│ JS (unsanitized): -//│ let AA1; +//│ let AA1; /** scoped **/ //│ globalThis.Object.freeze(class AA { //│ static { //│ AA1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 1318302565..9f2d151267 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -52,7 +52,7 @@ none() :sjs val Option = "Oops" //│ JS (unsanitized): -//│ let Option1; Option1 = "Oops"; +//│ let Option1; /** scoped **/ Option1 = "Oops"; //│ Option = "Oops" :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index f2e726848b..2416214a74 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -10,7 +10,7 @@ fun isDefined(x) = if x is Some then true None then false //│ JS (unsanitized): -//│ let isDefined; +//│ let isDefined; /** scoped **/ //│ isDefined = function isDefined(x) { //│ if (x instanceof Some.class) { //│ return true @@ -31,9 +31,9 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; +//│ let isDefined1, lambda; /** scoped **/ //│ lambda = (undefined, function (caseScrut) { -//│ let argument0$; +//│ let argument0$; /** scoped **/ //│ if (caseScrut instanceof Some.class) { //│ argument0$ = caseScrut.value; //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 46460ce327..04625446e1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -6,7 +6,7 @@ data class Foo() //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo() { //│ return globalThis.Object.freeze(new Foo.class()); //│ }; @@ -37,7 +37,7 @@ Foo.class data class Foo(a) //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ Foo3 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -64,19 +64,19 @@ Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; tmp = Foo2(1); tmp.a +//│ let tmp; /** scoped **/ tmp = Foo2(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; foo = function foo(y) { return Foo2(y) }; foo(27) +//│ let foo; /** scoped **/ foo = function foo(y) { return Foo2(y) }; foo(27) //│ = Foo(27) data class Foo(a, b) //│ JS (unsanitized): -//│ let Foo5; +//│ let Foo5; /** scoped **/ //│ Foo5 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -94,17 +94,17 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; foo1 = Foo4; +//│ let foo1; /** scoped **/ foo1 = Foo4; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) //│ JS (unsanitized): -//│ let f; f = runtime.safeCall(foo1(1, 2)); +//│ let f; /** scoped **/ f = runtime.safeCall(foo1(1, 2)); //│ f = Foo(1, 2) let f = new! foo(1, 2) //│ JS (unsanitized): -//│ let f1; f1 = globalThis.Object.freeze(new foo1(1, 2)); +//│ let f1; /** scoped **/ f1 = globalThis.Object.freeze(new foo1(1, 2)); //│ f = Foo(1, 2) f.a @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; f2 = Foo4(1, 2); +//│ let f2; /** scoped **/ f2 = Foo4(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) +//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -144,7 +144,7 @@ data class Inner(c) with fun i1(d) = c + d print(c) //│ JS (unsanitized): -//│ let Inner1; +//│ let Inner1; /** scoped **/ //│ Inner1 = function Inner(c) { //│ return globalThis.Object.freeze(new Inner.class(c)); //│ }; @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; i = globalThis.Object.freeze(new Inner.class(100)); +//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner.class(100)); //│ > 100 //│ i = Inner(100) @@ -185,7 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; +//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -242,7 +242,7 @@ class Foo(val z, val z) //│ ║ l.236: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo9; +//│ let Foo9; /** scoped **/ //│ Foo9 = function Foo(z, z1) { //│ return globalThis.Object.freeze(new Foo.class(z, z1)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index b04a1ebca2..3ccd81f5d8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -12,7 +12,7 @@ f(2) :sjs let f = foo(1, _, _) //│ JS (unsanitized): -//│ let f2; let f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; +//│ let f2; /** scoped **/ let f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; //│ f = fun f f(2, 3) @@ -79,7 +79,10 @@ h(1) :sjs let i = _(0, 1, _) //│ JS (unsanitized): -//│ let i; let i1; i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; i = i1; +//│ let i; /** scoped **/ +//│ let i1; +//│ i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; +//│ i = i1; //│ i = fun i i(print, 2) @@ -199,7 +202,7 @@ _ - _ of 1, 2 :pe |> 1 //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.200: |> 1 +//│ ║ l.203: |> 1 //│ ╙── ^ //│ = fun pipeInto @@ -207,7 +210,7 @@ _ - _ of 1, 2 :re |> (1) //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.208: |> (1) +//│ ║ l.211: |> (1) //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] TypeError: f is not a function @@ -225,13 +228,15 @@ _ - _ of 1, 2 :sjs 1 \ (_ - 2) //│ JS (unsanitized): -//│ let lambda38; lambda38 = (undefined, function (_0) { return _0 - 2 }); Predef.passTo(1, lambda38) +//│ let lambda38; /** scoped **/ +//│ lambda38 = (undefined, function (_0) { return _0 - 2 }); +//│ Predef.passTo(1, lambda38) //│ = fun :sjs 1 \ (_ - 2)() //│ JS (unsanitized): -//│ let lambda39, tmp19; +//│ let lambda39, tmp19; /** scoped **/ //│ lambda39 = (undefined, function (_0) { //│ return _0 - 2 //│ }); @@ -248,13 +253,13 @@ _ - _ of 1, 2 :w let f = if _ then 1 else 0 //│ ╔══[WARNING] This catch-all clause makes the following branches unreachable. -//│ ║ l.249: let f = if _ then 1 else 0 +//│ ║ l.254: let f = if _ then 1 else 0 //│ ║ ^^^^^^ //│ ╟── This branch is unreachable. -//│ ║ l.249: let f = if _ then 1 else 0 +//│ ║ l.254: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; /** scoped **/ tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs @@ -263,9 +268,9 @@ fun f(x) = 0 then 1 _ then 2 //│ JS (unsanitized): -//│ let f16; +//│ let f16; /** scoped **/ //│ f16 = function f(x3) { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = x3 == 0; //│ if (scrut === true) { return 1 } else { return 2 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 9d43f7f4cc..4697651570 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -6,7 +6,7 @@ class Foo //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -23,7 +23,7 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = globalThis.Object.freeze(new Foo()); //│ if (scrut instanceof Foo) { true } else { false } //│ = true @@ -53,7 +53,7 @@ Foo() data class Foo with { print("hi") } print("ok") //│ JS (unsanitized): -//│ let Foo3; +//│ let Foo3; /** scoped **/ //│ globalThis.Object.freeze(class Foo2 { //│ static { //│ Foo3 = this @@ -72,9 +72,9 @@ fun test() = print("ok") Foo //│ JS (unsanitized): -//│ let test; +//│ let test; /** scoped **/ //│ test = function test() { -//│ let Foo5, tmp; +//│ let Foo5, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -91,7 +91,7 @@ fun test() = let t = test() //│ JS (unsanitized): -//│ let t; t = test(); +//│ let t; /** scoped **/ t = test(); //│ > ok //│ t = class Foo @@ -135,7 +135,7 @@ class Foo with let y = x + 1 fun z() = y + x //│ JS (unsanitized): -//│ let Foo6; +//│ let Foo6; /** scoped **/ //│ globalThis.Object.freeze(class Foo5 { //│ static { //│ Foo6 = this @@ -162,7 +162,7 @@ class Foo with fun z2() = 6 print("hello") //│ JS (unsanitized): -//│ let Foo8; +//│ let Foo8; /** scoped **/ //│ globalThis.Object.freeze(class Foo7 { //│ static { //│ Foo8 = this @@ -192,7 +192,7 @@ class Foo with fun foo(y) = x + y fun bar(z) = foo(z) + 1 //│ JS (unsanitized): -//│ let Foo10; +//│ let Foo10; /** scoped **/ //│ globalThis.Object.freeze(class Foo9 { //│ static { //│ Foo10 = this @@ -204,7 +204,7 @@ class Foo with //│ return this.x + y //│ } //│ bar(z) { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = this.foo(z); //│ return tmp + 1 //│ } @@ -217,7 +217,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let tmp, tmp1, a, tmp2, tmp3; +//│ let tmp, tmp1, a, tmp2, tmp3; /** scoped **/ //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); @@ -243,7 +243,7 @@ class Foo with //│ ║ l.237: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo12; +//│ let Foo12; /** scoped **/ //│ globalThis.Object.freeze(class Foo11 { //│ static { //│ Foo12 = this @@ -267,7 +267,7 @@ class Foo with //│ ║ l.261: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo14; +//│ let Foo14; /** scoped **/ //│ globalThis.Object.freeze(class Foo13 { //│ static { //│ Foo14 = this @@ -294,7 +294,7 @@ class Foo with val x = 1 //│ ║ l.292: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo16; +//│ let Foo16; /** scoped **/ //│ globalThis.Object.freeze(class Foo15 { //│ static { //│ Foo16 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls index 8a571ad88e..ea8a6fed34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls @@ -7,7 +7,7 @@ let x = 1 :sjs let x' = 2 //│ JS (unsanitized): -//│ let x$_; x$_ = 2; +//│ let x$_; /** scoped **/ x$_ = 2; //│ x' = 2 x' diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 3200ac70a4..17d83ddd0f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let tmp, tmp1, tmp2, tmp3, lambda, folderName1, folderName2; +//│ let tmp, tmp1, tmp2, tmp3, lambda, folderName1, folderName2; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index aba13f589b..f0288a38e5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,7 +76,7 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ let x1, tmp7, tmp8, arr2; //│ tmp7 = globalThis.Object.freeze(new Term.Symbol("x")); //│ x1 = globalThis.Object.freeze(new Term.Ref(tmp7)); diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index 1b1cd7779a..4281427856 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -4,9 +4,9 @@ :sjs fun foo() = if false do foo() //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo() { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = false; //│ if (scrut === true) { return foo() } else { return runtime.Unit } //│ }; @@ -14,7 +14,7 @@ fun foo() = if false do foo() :sjs fun foo() = foo() //│ JS (unsanitized): -//│ let foo1; foo1 = function foo() { return foo1() }; +//│ let foo1; /** scoped **/ foo1 = function foo() { return foo1() }; :sjs @@ -22,7 +22,7 @@ data class Foo(x) with class Bar with val y = x //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; @@ -54,7 +54,12 @@ fun foo() = fun bar() = bar() bar() //│ JS (unsanitized): -//│ let foo2; foo2 = function foo() { let bar; bar = function bar() { return bar() }; return bar() }; +//│ let foo2; /** scoped **/ +//│ foo2 = function foo() { +//│ let bar; /** scoped **/ +//│ bar = function bar() { return bar() }; +//│ return bar() +//│ }; :sjs @@ -63,7 +68,10 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; f = 1; f1 = function f() { let tmp; tmp = f1(); return tmp }; runtime.Unit +//│ let f, f1; /** scoped **/ +//│ f = 1; +//│ f1 = function f() { let tmp; /** scoped **/ tmp = f1(); return tmp }; +//│ runtime.Unit //│ f = 1 :sjs @@ -71,13 +79,13 @@ do let foo = 1 fun foo(x) = foo //│ ╔══[ERROR] Name 'foo' is already used -//│ ║ l.71: let foo = 1 +//│ ║ l.79: let foo = 1 //│ ║ ^^^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.72: fun foo(x) = foo +//│ ║ l.80: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let foo3, foo4; foo3 = function foo(x) { return foo4 }; foo4 = 1; +//│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; //│ foo = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 7d9d449e62..2cfe39c612 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -53,7 +53,7 @@ id(f)(3)(4) //│ runtime.checkArgs("", 2, true, args1.length); //│ let y = args1[0]; //│ let z = args1[1]; -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ tmp4 = x + y; //│ return tmp4 + z //│ } @@ -113,7 +113,7 @@ id(Cls(1, 2)).f(3) //│ this.y = y; //│ } //│ f(z, p) { -//│ let tmp6, tmp7; +//│ let tmp6, tmp7; /** scoped **/ //│ tmp6 = this.x + this.y; //│ tmp7 = tmp6 + z; //│ return tmp7 + p @@ -149,7 +149,7 @@ id(Cls(1, 2)).f(3) //│ runtime.checkArgs("f", 2, true, args.length); //│ let z = args[0]; //│ let p = args[1]; -//│ let tmp8, tmp9; +//│ let tmp8, tmp9; /** scoped **/ //│ tmp8 = this.x + this.y; //│ tmp9 = tmp8 + z; //│ return tmp9 + p @@ -191,7 +191,7 @@ id(Cls(1, 2)).f(3, 4)(5) //│ runtime.checkArgs("", 2, true, args1.length); //│ let q = args1[0]; //│ let s = args1[1]; -//│ let tmp11, tmp12, tmp13, tmp14; +//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ //│ tmp11 = this.x + this.y; //│ tmp12 = tmp11 + z; //│ tmp13 = tmp12 + p; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 7d6d50e31e..2ffd7e3b0b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,17 +19,16 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f() { -//│ let scrut, a, b; +//│ let scrut, a, b; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; - -// `let a` is declared in the correct place, -// but not `lambda` (because this is created in `LambdaRewriter`, a later pass) +// TODO: improve later: `let a` can be moved to the branch body +// TODO: `lambda` is not currently included in the symbols in the `Scoped` block (because this is created in `LambdaRewriter`, a later pass) // NOTE: `tmp1` and `tmp2` should indeed be declared in the top level of the function body :sjs fun g(x, y, z) = @@ -40,9 +39,9 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; +//│ let g; /** scoped **/ //│ g = function g(x1, y1, z) { -//│ let a, tmp1, tmp2; +//│ let a, tmp1, tmp2; /** scoped **/ //│ let lambda; //│ split_root$: { //│ split_1$: { @@ -84,9 +83,9 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ f1 = function f() { -//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (x instanceof A.class) { @@ -122,9 +121,9 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ f2 = function f() { -//│ let scrut, argument0$, argument1$, tmp1; +//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ scrut = A(1, Nil); @@ -161,12 +160,12 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(x1) { -//│ let tmp1, while1, tmp2, tmp3; +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, a1, argument0$, argument1$; +//│ let a, a1, argument0$, argument1$; /** scoped **/ //│ if (x1 instanceof A.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; @@ -201,13 +200,13 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ f4 = function f() { -//│ let y1, tmp1, while1, tmp2, tmp3; +//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ //│ y1 = x + 1; //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (y1 instanceof A.class) { @@ -249,12 +248,12 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; +//│ let f5; /** scoped **/ //│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; +//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ //│ tmp1 = undefined; //│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; +//│ let scrut, lambda, tmp4; /** scoped **/ //│ lambda = (undefined, function () { //│ return y1 //│ }); @@ -275,3 +274,20 @@ fun f(x, y) = +// FIXME: `tmp1` is declared in a wrong place +:sjs +class A with + val x = 1 + 2 +//│ JS (unsanitized): +//│ let A3, tmp1; /** scoped **/ +//│ globalThis.Object.freeze(class A2 { +//│ static { +//│ A3 = this +//│ } +//│ constructor() { +//│ tmp1 = 1 + 2; +//│ this.x = tmp1; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A"]; +//│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 2bd0927b56..56d63f1ecd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -5,7 +5,7 @@ module Foo with val self: module Foo = Foo //│ JS (unsanitized): -//│ let Foo1; +//│ let Foo1; /** scoped **/ //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -61,7 +61,7 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11, tmp; +//│ let Foo11, tmp; /** scoped **/ //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 21ab63ce2c..6c6b09fbe7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -7,7 +7,7 @@ let x = 0 :sjs let x += 1 //│ JS (unsanitized): -//│ let x1; x1 = x + 1; +//│ let x1; /** scoped **/ x1 = x + 1; //│ x = 1 x @@ -21,7 +21,7 @@ set x = 0 :sjs set x += 1 //│ JS (unsanitized): -//│ let tmp; tmp = x1 + 1; x1 = tmp; runtime.Unit +//│ let tmp; /** scoped **/ tmp = x1 + 1; x1 = tmp; runtime.Unit x //│ = 1 @@ -30,7 +30,7 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let tmp1, tmp2, old, tmp3; +//│ let tmp1, tmp2, old, tmp3; /** scoped **/ //│ old = x1; //│ try { tmp1 = x1 + 1; x1 = tmp1; tmp2 = Predef.print(x1); tmp3 = tmp2; } finally { x1 = old; } //│ tmp3 @@ -69,9 +69,9 @@ fun example() = print(get_x()) example() //│ JS (unsanitized): -//│ let example2; +//│ let example2; /** scoped **/ //│ example2 = function example() { -//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; +//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ let get_x1; //│ x2 = 0; //│ get_x1 = function get_x() { @@ -111,9 +111,9 @@ fun example() = y example() //│ JS (unsanitized): -//│ let example3; +//│ let example3; /** scoped **/ //│ example3 = function example() { -//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; +//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ //│ let get_x1; //│ x2 = 0; //│ get_x1 = function get_x() { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls index 9be3a3a5b7..228d0613df 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls @@ -19,7 +19,7 @@ foo(0, ...a) :sjs foo(1, ...[2, 3], 4) //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) //│ = [1, 2, 3, 4] :re diff --git a/hkmc2/shared/src/test/mlscript/codegen/This.mls b/hkmc2/shared/src/test/mlscript/codegen/This.mls index 64418595dd..9d809eb9e6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/This.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/This.mls @@ -10,13 +10,14 @@ fun test(x) = //│ ║ l.8: [this.a, x] //│ ╙── ^^^^ //│ JS (unsanitized): -//│ let test; test = function test(x) { return globalThis.Object.freeze([]) }; +//│ let test; /** scoped **/ test = function test(x) { return globalThis.Object.freeze([]) }; :sjs fun test(x) = [globalThis.a, x] //│ JS (unsanitized): -//│ let test1; test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; +//│ let test1; /** scoped **/ +//│ test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; :re test(123) @@ -42,7 +43,7 @@ module Test with fun test2(x) = [this.a, x] //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 44fb9381b8..3bb6a4a271 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; tmp = call(Example, oops); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = call(Example, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) +//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls index cad43e858a..b2171fa4c0 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls @@ -19,7 +19,7 @@ s(123) :sjs ex |>. s(123) //│ JS (unsanitized): -//│ let tmp1; tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) +//│ let tmp1; /** scoped **/ tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) //│ = [123, 456] diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index 60ab9f2c5c..fa1b38cc34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,7 +31,9 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) +//│ let f2; /** scoped **/ +//│ f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; +//│ f2(1) //│ ═══[RUNTIME ERROR] Error: e @@ -41,7 +43,7 @@ fun f(x) = throw (if x then Error("x") else Error("y")) f(false) //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ f3 = function f(x) { //│ if (x === true) { throw globalThis.Error("x") } else { throw globalThis.Error("y") } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 0336784324..993741a4b1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -7,9 +7,9 @@ fun fib(a) = if a <= 1 then a else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): -//│ let fib; +//│ let fib; /** scoped **/ //│ fib = function fib(a) { -//│ let scrut, tmp, tmp1, tmp2, tmp3; +//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ scrut = a <= 1; //│ if (scrut === true) { //│ return a diff --git a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls index c0ec266ced..72170fa838 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls @@ -30,7 +30,9 @@ print of foo() :sjs print of globalThis.console.log("Hello, world!") //│ JS (unsanitized): -//│ let tmp4; tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); Predef.print(tmp4) +//│ let tmp4; /** scoped **/ +//│ tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); +//│ Predef.print(tmp4) //│ > Hello, world! //│ > () @@ -54,7 +56,7 @@ print of Box(foo()).value :sjs fun foo() = {} //│ JS (unsanitized): -//│ let foo5; foo5 = function foo() { return runtime.Unit }; +//│ let foo5; /** scoped **/ foo5 = function foo() { return runtime.Unit }; print of Box(foo()).value //│ > () @@ -64,7 +66,7 @@ print of Box(foo()).value fun foo(x) = set x = 1 //│ JS (unsanitized): -//│ let foo6; foo6 = function foo(x) { x = 1; return runtime.Unit }; +//│ let foo6; /** scoped **/ foo6 = function foo(x) { x = 1; return runtime.Unit }; print of Box(foo(1)).value //│ > () @@ -73,7 +75,7 @@ print of Box(foo(1)).value :e fun f = let x = 1 //│ ╔══[ERROR] Expected a body for let bindings in expression position -//│ ║ l.74: fun f = let x = 1 +//│ ║ l.76: fun f = let x = 1 //│ ╙── ^^^^^ print of f diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 8544c1fa3a..91ebaefac6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -6,10 +6,10 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function () { -//│ let tmp, while1, tmp1, tmp2; +//│ let tmp, while1, tmp1, tmp2; /** scoped **/ //│ tmp = undefined; //│ while1 = (undefined, function () { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ tmp = 0; @@ -57,10 +57,10 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let while3, tmp6, tmp7, tmp8; +//│ let while3, tmp6, tmp7, tmp8; /** scoped **/ //│ tmp8 = undefined; //│ while3 = (undefined, function () { -//│ let tmp9; +//│ let tmp9; /** scoped **/ //│ if (x2 === true) { //│ tmp9 = Predef.print("Hello World"); //│ x2 = false; @@ -112,10 +112,10 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let tmp18, while7, tmp19, tmp20; +//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ //│ tmp18 = undefined; //│ while7 = (undefined, function () { -//│ let i2, scrut, tmp21; +//│ let i2, scrut, tmp21; /** scoped **/ //│ i2 = 0; //│ scrut = i2 < 10; //│ if (scrut === true) { @@ -208,12 +208,12 @@ fun f(ls) = print(h) else print("Done!") //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f(ls) { -//│ let tmp27, while10, tmp28, tmp29; +//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ //│ tmp27 = undefined; //│ while10 = (undefined, function () { -//│ let tl, h, argument0$, argument1$; +//│ let tl, h, argument0$, argument1$; /** scoped **/ //│ if (ls instanceof Cons.class) { //│ argument0$ = ls.hd; //│ argument1$ = ls.tl; @@ -267,7 +267,7 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, while11, tmp39; +//│ let tmp37, tmp38, while11, tmp39; /** scoped **/ //│ tmp39 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 885a6dd1ed..9452424ea9 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -101,7 +101,7 @@ class T class Foo(using T) with print(T) //│ JS (unsanitized): -//│ let Foo9; +//│ let Foo9; /** scoped **/ //│ Foo9 = function Foo(tmp5) { //│ return globalThis.Object.freeze(new Foo.class(tmp5)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index 005ed3b089..18422737ba 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -31,10 +31,10 @@ using Num = 0.42 :sjs let f1 = foo //│ JS (unsanitized): -//│ let f1; +//│ let f1; /** scoped **/ //│ let f11; //│ f11 = function f1() { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = foo(); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; @@ -53,10 +53,10 @@ f1() :sjs let f2 = bar //│ JS (unsanitized): -//│ let f2; +//│ let f2; /** scoped **/ //│ let f21; //│ f21 = function f2(x) { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = bar(x); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; @@ -75,12 +75,12 @@ f2(0.42) :sjs let f3 = baz //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ let f31; //│ f31 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1, tmp2; +//│ let tmp, tmp1, tmp2; /** scoped **/ //│ tmp = baz(x); //│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); //│ tmp2 = runtime.safeCall(tmp1(y)); @@ -99,14 +99,14 @@ f3(43)(44) :sjs foo() //│ JS (unsanitized): -//│ let tmp1; tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) +//│ let tmp1; /** scoped **/ tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) //│ = [42] // This should not expand :sjs bar(0.42) //│ JS (unsanitized): -//│ let tmp2; tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) +//│ let tmp2; /** scoped **/ tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) //│ = [0.42, 42] @@ -115,7 +115,7 @@ module Test with fun test(j)(using Int) = 0 fun main(using Int) = test //│ JS (unsanitized): -//│ let Test1; +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -131,7 +131,7 @@ module Test with //│ static main(tmp3) { //│ let lambda1; //│ lambda1 = (undefined, function (j) { -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ tmp4 = Test.test(j); //│ return runtime.safeCall(tmp4(tmp3)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index 482d1d6532..a58b71139f 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; tmp2 = test(); tmp2(1, 1) +//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index 11eee81727..ec3ef74cf6 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -17,7 +17,7 @@ fun f() = print of raiseUnhandledEffect() j / i //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ let getLocals2; //│ getLocals2 = function getLocals() { //│ let prev, thisInfo, arr, tmp; @@ -28,7 +28,7 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let i, j, k, scrut, tmp, tmp1; +//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ //│ let getLocals3, Cont$func$f$1, doUnwind; //│ getLocals3 = function getLocals() { //│ let i1, j1, k1, prev, thisInfo, arr, tmp2; diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index ff4375a632..0bd1b3914f 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,10 +156,10 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let tmp20; +//│ let tmp20; /** scoped **/ //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { -//│ let f, scrut; +//│ let f, scrut; /** scoped **/ //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; //│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index ee16b6ecbd..3b0ece8c6e 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -13,9 +13,9 @@ fun foo(h): module M = module A A //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(h) { -//│ let A2, A3, scrut; +//│ let A2, A3, scrut; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A2 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 9eee2040f8..72893cfea0 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,7 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1, tmp; +//│ let Lol1, tmp; /** scoped **/ //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 0df80c6f15..936b367f53 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,7 +155,7 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let tmp24, tmp25, tmp26, str, scrut, tmp27; +//│ let tmp24, tmp25, tmp26, str, scrut, tmp27; /** scoped **/ //│ let handleBlock$7; //│ str = ""; //│ scrut = true; @@ -173,7 +173,7 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30; +//│ let tmp28, tmp29, tmp30; /** scoped **/ //│ let Cont$handler$h1$perform$2, doUnwind1; //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { @@ -261,7 +261,7 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, tmp31, tmp32; +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ //│ let Cont$handler$h2$perform$2, doUnwind2; //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index e4b6108c4f..47b74b929d 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,7 +8,7 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let tmp, tmp1, x, old, tmp2; +//│ let tmp, tmp1, x, old, tmp2; /** scoped **/ //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 470faa6f12..61cef5c3bf 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -20,10 +20,10 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi; +//│ let hi; /** scoped **/ //│ let res, $_stack$_safe$_body$_; //│ hi = function hi(n) { -//│ let scrut, tmp; +//│ let scrut, tmp; /** scoped **/ //│ let Cont$func$hi$1, doUnwind, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { //│ static { @@ -76,10 +76,10 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1; +//│ let sum1; /** scoped **/ //│ let res1, $_stack$_safe$_body$_1; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1; +//│ let scrut, tmp, tmp1; /** scoped **/ //│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { //│ static { @@ -260,9 +260,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; +//│ let max; /** scoped **/ //│ max = function max(a, b) { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -274,7 +274,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls index 76a8e46f8f..a1596055ef 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls @@ -13,7 +13,7 @@ arr.map(_ === false) :sjs arr.map((e, ...) => e === false) //│ JS (unsanitized): -//│ let lambda1; +//│ let lambda1; /** scoped **/ //│ lambda1 = (undefined, function (e, ..._) { return e === false }); //│ runtime.safeCall(arr.map(lambda1)) //│ = [false, true] @@ -21,7 +21,7 @@ arr.map((e, ...) => e === false) :sjs arr.map((_, ...) => 1) //│ JS (unsanitized): -//│ let lambda2; +//│ let lambda2; /** scoped **/ //│ lambda2 = (undefined, function (_, ..._1) { return 1 }); //│ runtime.safeCall(arr.map(lambda2)) //│ = [1, 1] diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 9d6342ec9a..8ac673ca75 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,7 +88,7 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let tmp9, f5; +//│ let tmp9, f5; /** scoped **/ //│ let Bad1, Good1, Bad$, Good$, f$capture3; //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; @@ -131,7 +131,7 @@ f().foo() //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ foo() { -//│ let tmp10, tmp11; +//│ let tmp10, tmp11; /** scoped **/ //│ this.z = 100; //│ tmp10 = this.x + this.y; //│ tmp11 = tmp10 + this.z; @@ -186,7 +186,7 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f5 = function f() { -//│ let x, y, z, w, tmp10, tmp11; +//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ //│ let capture; //│ capture = new f$capture3(null); //│ x = 1; diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index 9ae2b447c7..740ffcbb6f 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -11,13 +11,13 @@ fun f(x) = fun g = new A //│ ═══[WARNING] Modules are not yet lifted. //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ let g$; //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; //│ f = function f(x) { -//│ let A1, g; +//│ let A1, g; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 9162254161..40b369eb47 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -7,7 +7,7 @@ data class A(x) with fun getB() = x + y fun getA() = B(2).getB() //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ let B1, B$; //│ B$ = function B$(isMut, A$instance, y) { //│ let tmp, tmp1; @@ -55,7 +55,7 @@ data class A(x) with //│ this.x = x; //│ } //│ getA() { -//│ let tmp; +//│ let tmp; /** scoped **/ //│ tmp = B$(false, this, 2); //│ return tmp.getB() //│ } @@ -74,7 +74,7 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let A3, tmp1; +//│ let A3, tmp1; /** scoped **/ //│ let g, g$; //│ g$ = function g$(A$instance) { //│ return 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index eec1c56126..358881b02a 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -47,10 +47,10 @@ fun f(used1, unused1) = foo(used2) + unused2 f(1, 2) //│ JS (unsanitized): -//│ let f3; +//│ let f3; /** scoped **/ //│ let g1, g$3; //│ g$3 = function g$(used1, used2, g_arg) { -//│ let used3; +//│ let used3; /** scoped **/ //│ used3 = 2; //│ return used1 + used2 //│ }; @@ -60,7 +60,7 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let used2, unused2, foo, tmp; +//│ let used2, unused2, foo, tmp; /** scoped **/ //│ let g$here; //│ used2 = unused1; //│ unused2 = 2; @@ -79,10 +79,10 @@ fun f(a1, a2, a3, a4, a5, a6) = g f(1,2,3,4,5,6) //│ JS (unsanitized): -//│ let f4; +//│ let f4; /** scoped **/ //│ let g$4; //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; +//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ tmp = a1 + a2; //│ tmp1 = tmp + a3; //│ tmp2 = tmp1 + a4; @@ -90,7 +90,7 @@ f(1,2,3,4,5,6) //│ return tmp3 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { -//│ let g2, tmp; +//│ let g2, tmp; /** scoped **/ //│ tmp = g$4(a1, a2, a3, a4, a5, a6); //│ return tmp //│ }; @@ -165,7 +165,7 @@ fun f(unused, immutable, mutated) = a + h() + unused f(1, 2, 1000) //│ JS (unsanitized): -//│ let f7; +//│ let f7; /** scoped **/ //│ let h$2, g$6, f$capture5; //│ g$6 = function g$(immutable, f$capture6) { //│ f$capture6.mutated$capture$0 = 2; @@ -185,7 +185,7 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let g3, h2, a1, tmp7, tmp8; +//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ //│ let capture; //│ capture = new f$capture5(mutated); //│ a1 = g$6(immutable, capture); @@ -256,7 +256,7 @@ fun g() = f g()(1) //│ JS (unsanitized): -//│ let g6, tmp7; +//│ let g6, tmp7; /** scoped **/ //│ let f14, f$1, h$4, g$capture1; //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; @@ -264,7 +264,7 @@ g()(1) //│ return x1 + g$capture2.y$capture$0 //│ }; //│ f$1 = function f$(g$capture2, x1) { -//│ let h3, k; +//│ let h3, k; /** scoped **/ //│ k = 4; //│ g$capture2.y$capture$0 = 2; //│ return x1 @@ -285,7 +285,7 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { -//│ let y1; +//│ let y1; /** scoped **/ //│ let capture, f$here; //│ capture = new g$capture1(null); //│ capture.y$capture$0 = 0; @@ -377,7 +377,7 @@ fun f(x) = set y = 2 [g, g] //│ JS (unsanitized): -//│ let f23; +//│ let f23; /** scoped **/ //│ let g12, g$14; //│ g$14 = function g$(y1) { //│ return y1 @@ -388,7 +388,7 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let y1, scrut; +//│ let y1, scrut; /** scoped **/ //│ let g$here; //│ y1 = undefined; //│ scrut = x1 < 0; @@ -413,7 +413,7 @@ fun f(x) = set x += 1 [a, g] //│ JS (unsanitized): -//│ let f24; +//│ let f24; /** scoped **/ //│ let g13, g$15, f$capture17; //│ g$15 = function g$(f$capture18) { //│ return f$capture18.x$capture$0 @@ -434,7 +434,7 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let a1, tmp10; +//│ let a1, tmp10; /** scoped **/ //│ let capture, g$here; //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); diff --git a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls index d5bc28e321..581a224319 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls @@ -7,7 +7,7 @@ import "../../mlscript-compile/Option.mls" module A with fun f(x) = x is Option.Some //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index a1bc4b23c1..1c9a994104 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -45,7 +45,7 @@ fun foo() = set x += 1 return () => x //│ JS (unsanitized): -//│ let foo2; +//│ let foo2; /** scoped **/ //│ let lambda2, while$1, lambda$2, foo$capture5; //│ lambda$2 = function lambda$(foo$capture6) { //│ return foo$capture6.x$capture$0 @@ -56,7 +56,7 @@ fun foo() = //│ } //│ }); //│ while$1 = function while$(foo$capture6) { -//│ let scrut, tmp2; +//│ let scrut, tmp2; /** scoped **/ //│ let lambda$here; //│ scrut = true; //│ if (scrut === true) { @@ -81,7 +81,7 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo2 = function foo() { -//│ let x, tmp2, tmp3, tmp4, while1; +//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ //│ let capture; //│ capture = new foo$capture5(null, null); //│ capture.x$capture$0 = 1; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index adfa8e4b89..1b932865d0 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -27,7 +27,7 @@ fun foo(y) = (new M).foo() foo(10) //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ let M3, M$; //│ M$ = function M$(isMut, y) { //│ let tmp; @@ -55,7 +55,7 @@ foo(10) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ foo1 = function foo(y) { let tmp; tmp = M$(false, y); return tmp.foo() }; +//│ foo1 = function foo(y) { let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() }; //│ foo1(10) @@ -110,7 +110,7 @@ fun foo(x, y) = fun foo3 = M.foo2() foo3 //│ JS (unsanitized): -//│ let foo4; +//│ let foo4; /** scoped **/ //│ let M5, foo3$, foo$capture3; //│ foo3$ = function foo3$(M6, x, foo$capture4) { //│ return M6.foo2() @@ -147,7 +147,7 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let foo31, tmp; +//│ let foo31, tmp; /** scoped **/ //│ let M$1, capture; //│ capture = new foo$capture3(y); //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); @@ -214,7 +214,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp5, tmp6; +//│ let M17, tmp5, tmp6; /** scoped **/ //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -223,7 +223,7 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 0e670441d0..15a720556b 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -12,7 +12,7 @@ fun foo() = xs.push(bar) set x = 2 //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ let bar, bar$, foo$capture1; //│ bar$ = function bar$(foo$capture2) { //│ return foo$capture2.x$capture$0 @@ -33,7 +33,7 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let x, tmp; +//│ let x, tmp; /** scoped **/ //│ let capture, bar$here; //│ capture = new foo$capture1(null); //│ capture.x$capture$0 = 1; @@ -86,9 +86,13 @@ fun foo() = x bar //│ JS (unsanitized): -//│ let foo3; +//│ let foo3; /** scoped **/ //│ let bar3; -//│ bar3 = function bar() { let x; x = undefined; return x }; +//│ bar3 = function bar() { +//│ let x; /** scoped **/ +//│ x = undefined; +//│ return x +//│ }; //│ foo3 = function foo() { return bar3 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 7c0b2622b6..36e71bb305 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -21,7 +21,7 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi; +//│ let hi; /** scoped **/ //│ let Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; //│ Cont$func$hi$$ = function Cont$func$hi$$(isMut, n, pc) { //│ let tmp, tmp1; @@ -61,7 +61,7 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let scrut, tmp; +//│ let scrut, tmp; /** scoped **/ //│ let stackDelayRes; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); @@ -94,7 +94,7 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1; +//│ let sum1; /** scoped **/ //│ let Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; //│ Cont$func$sum$$ = function Cont$func$sum$$(isMut, n, tmp, pc) { //│ let tmp1, tmp2; @@ -151,7 +151,7 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1; +//│ let scrut, tmp, tmp1; /** scoped **/ //│ let curDepth, stackDelayRes; //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; @@ -276,9 +276,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; +//│ let max; /** scoped **/ //│ max = function max(a, b) { -//│ let scrut; +//│ let scrut; /** scoped **/ //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -290,7 +290,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index 3ebc7254e9..f9db8f56b2 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -14,7 +14,7 @@ class A(x) with set z += 2 x //│ JS (unsanitized): -//│ let A1; +//│ let A1; /** scoped **/ //│ A1 = function A(x) { //│ return globalThis.Object.freeze(new A.class(x)); //│ }; @@ -42,7 +42,7 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ let idx1, idx2, idx3, idx4, idx5; //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; @@ -61,7 +61,7 @@ class A(x) with //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ f(y) { -//│ let tmp, tmp1; +//│ let tmp, tmp1; /** scoped **/ //│ tmp = this.#x + 1; //│ this.#x = tmp; //│ tmp1 = this.z + 2; diff --git a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls index 41df93f70a..226fd3ed63 100644 --- a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls +++ b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls @@ -20,7 +20,7 @@ //│ ║ l.16: 1 //│ ╙── ^ //│ JS (unsanitized): -//│ let tmp2; tmp2 = + 2; + 3 +//│ let tmp2; /** scoped **/ tmp2 = + 2; + 3 //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.16: 1 //│ ╙── ^ @@ -31,7 +31,7 @@ + 2 + 3 //│ JS (unsanitized): -//│ let tmp3; tmp3 = 1 + 2; tmp3 + 3 +//│ let tmp3; /** scoped **/ tmp3 = 1 + 2; tmp3 + 3 //│ = 6 1 diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index bcae9dd616..0b0f5ad043 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let element0$, ls, a, middleElements; +//│ let element0$, ls, a, middleElements; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); @@ -133,9 +133,9 @@ fun popByIndex(start, end, acc, lft) = if start >= end then acc else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): -//│ let popByIndex; +//│ let popByIndex; /** scoped **/ //│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let scrut, tmp34, tmp35, tmp36; +//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ //│ scrut = start >= end; //│ if (scrut === true) { //│ return acc diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 25ac7189a4..783e6d19df 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; /** scoped **/ tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 27429ef9e1..72036ed9d4 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -20,7 +20,7 @@ x => if x is [[0]] then 1 //│ JS (unsanitized): //│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let element0$, element0$1, tmp; +//│ let element0$, element0$1, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { @@ -57,9 +57,9 @@ fun crazy(v) = S(S(S(S(S(S(0)))))) then "bruh!" _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): -//│ let crazy; +//│ let crazy; /** scoped **/ //│ crazy = function crazy(v) { -//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; +//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (v instanceof S.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index 2ec685ad76..291d7f8c47 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,7 +25,7 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, tmp5, scrut5; +//│ let scrut4, tmp5, scrut5; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ scrut5 = true; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 0ebb0ea24b..ca253fe2be 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,19 +10,22 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut; scrut = x === 0; if (scrut === true) { 1 } else { 2 } +//│ let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } //│ = 1 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; +//│ let a, scrut1, tmp; /** scoped **/ +//│ scrut1 = x === 0; +//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } +//│ a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; +//│ let scrut2, tmp1; /** scoped **/ //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -38,7 +41,7 @@ if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp2; +//│ let tmp2; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x === 0) { @@ -78,7 +81,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; +//│ let qqq, tmp3; /** scoped **/ //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -118,7 +121,7 @@ print of if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp4; +//│ let tmp4; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ if (x === 0) { @@ -159,9 +162,9 @@ fun foo(x, y, z) = 1 then "1" else "" //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(x1, y1, z1) { -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ split_root$3: { //│ split_1$3: { //│ if (x1 === 0) { @@ -205,7 +208,7 @@ print of if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let tmp5; +//│ let tmp5; /** scoped **/ //│ split_root$3: { //│ split_1$3: { //│ split_2$: { @@ -267,9 +270,9 @@ fun foo(x, y, z) = if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let foo1; +//│ let foo1; /** scoped **/ //│ foo1 = function foo(x1, y1, z1) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { //│ split_2$1: { @@ -334,9 +337,9 @@ fun foo(x, y, z) = if x is let value = "hello" expensive_call(value) //│ JS (unsanitized): -//│ let foo2; +//│ let foo2; /** scoped **/ //│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; +//│ let value, tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { @@ -377,7 +380,7 @@ fun foo(x, y, z) = if x is A then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo3; +//│ let foo3; /** scoped **/ //│ foo3 = function foo(x1, y1, z1) { //│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } //│ }; @@ -398,9 +401,9 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo4; +//│ let foo4; /** scoped **/ //│ foo4 = function foo(x1, y1, z1) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { //│ if (x1 instanceof A.class) { @@ -424,9 +427,9 @@ fun foo(x, y, z) = fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): -//│ let foo5; +//│ let foo5; /** scoped **/ //│ foo5 = function foo(x1, y1, z1) { -//│ let tmp6; +//│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_default$: { //│ if (x1 instanceof A.class) { @@ -464,7 +467,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; +//│ let y1, scrut3, tmp6; /** scoped **/ //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls index 18fb0d1700..db15ad49ed 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls @@ -17,7 +17,7 @@ if x then 0 y then 0 //│ JS (unsanitized): -//│ let tmp; +//│ let tmp; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index feb39394f2..04f680fc0b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -11,7 +11,7 @@ x => if x is Pair(A, B) then 1 //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x) { -//│ let argument0$, argument1$, tmp; +//│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair.class) { @@ -45,9 +45,9 @@ fun f(x) = if x is Pair(A, A) then 1 Pair(B, B) then 2 //│ JS (unsanitized): -//│ let f; +//│ let f; /** scoped **/ //│ f = function f(x) { -//│ let argument0$, argument1$, tmp; +//│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 970b684cf9..697105bd95 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -36,9 +36,9 @@ fun foo(v) = A & B then 1 else 0 //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(v) { -//│ let tmp2; +//│ let tmp2; /** scoped **/ //│ split_root$2: { //│ split_1$2: { //│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 33339b6e3a..408479468b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -6,9 +6,9 @@ fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ JS (unsanitized): -//│ let nonsense; +//│ let nonsense; /** scoped **/ //│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; +//│ let ys, middleElements; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -27,9 +27,9 @@ fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ JS (unsanitized): -//│ let lead_and_last; +//│ let lead_and_last; /** scoped **/ //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; +//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -61,9 +61,9 @@ fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 //│ JS (unsanitized): -//│ let nested_tuple_patterns; +//│ let nested_tuple_patterns; /** scoped **/ //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index b203efa621..daa8dac993 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -33,9 +33,9 @@ Cross.unapply("0") :sjs fun foo(x) = x is Cross //│ JS (unsanitized): -//│ let foo; +//│ let foo; /** scoped **/ //│ foo = function foo(x2) { -//│ let unapplyResult, output, bindings; +//│ let unapplyResult, output, bindings; /** scoped **/ //│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index 8b6e1f28b0..cfe7256b4b 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -11,14 +11,14 @@ data class Pair[A, B](first: A, second: B) :sjs pattern SumPair = Pair(a, b) => a + b //│ JS (unsanitized): -//│ let SumPair1; +//│ let SumPair1; /** scoped **/ //│ globalThis.Object.freeze(class SumPair { //│ static { //│ SumPair1 = globalThis.Object.freeze(new this) //│ } //│ constructor() {} //│ unapply(input) { -//│ let transform, argument0$, argument1$, transformResult, tmp; +//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ //│ let lambda; //│ lambda = (undefined, function (a, b) { //│ return a + b From 9b5d327a5e7cf4876c98f1be386937404ab41900 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sun, 30 Nov 2025 22:03:16 +0800 Subject: [PATCH 39/72] fix incorrect scoped symbols --- .../main/scala/hkmc2/codegen/Lowering.scala | 11 +- .../src/main/scala/hkmc2/semantics/Term.scala | 10 +- .../basics/CompanionModules_Classes.mls | 3 +- .../test/mlscript/codegen/ClassInClass.mls | 28 ++--- .../test/mlscript/codegen/ClassMatching.mls | 20 ++-- .../src/test/mlscript/codegen/FunInClass.mls | 24 ++-- .../test/mlscript/codegen/FunctionsThis.mls | 3 +- .../test/mlscript/codegen/ModuleMethods.mls | 3 +- .../src/test/mlscript/codegen/Modules.mls | 5 +- .../test/mlscript/codegen/ParamClasses.mls | 7 +- .../src/test/mlscript/codegen/Scoped.mls | 103 +++++++++++++++++- .../test/mlscript/codegen/SelfReferences.mls | 3 +- .../mlscript/handlers/EffectsInClasses.mls | 3 +- .../test/mlscript/lifter/ModulesObjects.mls | 14 +-- 14 files changed, 177 insertions(+), 60 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 901044053f..7ac92e2794 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -886,11 +886,12 @@ class Lowering()(using Config, TL, Raise, State, Ctx): reportAnnotations(decl, annotations) sym val ctor = - term_nonTail(Blk(clsBody.nonMethods, clsBody.blk.res))(ImplctRet) - // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }` - .mapTail: - case Return(Value.Lit(syntax.Tree.UnitLit(true)), true) => End() - case t => t + inScopedBlock(clsBody.blk.res.definedSyms ++ clsBody.nonMethods.flatMap(_.definedSyms)): + term_nonTail(Blk(clsBody.nonMethods, clsBody.blk.res))(ImplctRet) + // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }` + .mapTail: + case Return(Value.Lit(syntax.Tree.UnitLit(true)), true) => End() + case t => t (mtds, publicFlds, privateFlds, ctor) def args(elems: Ls[Elem])(k: Ls[Arg] => Block)(using LoweringCtx): Block = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index 62c95229e3..bd85674321 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -464,11 +464,15 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case Annotated(annot, target) => target.definedSyms case Handle(lhs, rhs, args, derivedClsSym, defs, body) => Set.empty // TODO: - case LetDecl(sym, annotations) => Set(sym) + case LetDecl(sym, annotations) if sym.asTrm.fold(true)(_.owner.isEmpty) => Set(sym) case RcdField(field, rhs) => field.definedSyms ++ rhs.definedSyms case RcdSpread(rcd) => rcd.definedSyms - case termdef: TermDefinition => Set(termdef.sym) - case tpeLikeDef: TypeLikeDef if tpeLikeDef.hasDeclareModifier.isEmpty => Set(tpeLikeDef.bsym) + case termdef: TermDefinition if termdef.owner.isEmpty => Set(termdef.sym) + case tpeLikeDef: TypeLikeDef + if tpeLikeDef.hasDeclareModifier.isEmpty + && !tpeLikeDef.sym.isInstanceOf[TypeAliasSymbol] + && (!tpeLikeDef.isInstanceOf[ClassLikeDef] || tpeLikeDef.asInstanceOf[ClassLikeDef].owner.isEmpty) + => Set(tpeLikeDef.bsym) case _ => Set.empty // this match // case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 798b3547b0..b32f8cbaa6 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let tmp1, Foo1, tmp2; /** scoped **/ +//│ let tmp1, Foo1; /** scoped **/ //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { @@ -107,6 +107,7 @@ module Foo with //│ } //│ constructor() {} //│ static { +//│ let tmp2; /** scoped **/ //│ tmp2 = Foo1(); //│ this.res = tmp2.foo; //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index a3946765e5..f99ade0950 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,7 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, Outer1; /** scoped **/ +//│ let Outer1; /** scoped **/ //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -23,6 +23,7 @@ data class Outer(a, b) with //│ Outer1.class = this //│ } //│ constructor(a, b) { +//│ let tmp, tmp1, tmp2; /** scoped **/ //│ this.a = a; //│ this.b = b; //│ const this$Outer = this; @@ -34,11 +35,12 @@ data class Outer(a, b) with //│ this$Outer.Inner.class = this //│ } //│ constructor(c) { +//│ let tmp3, tmp4, tmp5; /** scoped **/ //│ this.c = c; -//│ tmp = Predef.print(this$Outer.a); -//│ tmp1 = Predef.print(this.c); -//│ tmp2 = this.i1(this$Outer.a); -//│ Predef.print(tmp2); +//│ tmp3 = Predef.print(this$Outer.a); +//│ tmp4 = Predef.print(this.c); +//│ tmp5 = this.i1(this$Outer.a); +//│ Predef.print(tmp5); //│ } //│ i1(d) { //│ return globalThis.Object.freeze([ @@ -50,19 +52,19 @@ data class Outer(a, b) with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Inner", ["c"]]; //│ }); -//│ tmp3 = this.Inner(this.a); -//│ this.i = tmp3; -//│ tmp4 = Predef.print(this.i.c); -//│ tmp5 = runtime.safeCall(this.i.i1(1)); -//│ Predef.print(tmp5); +//│ tmp = this.Inner(this.a); +//│ this.i = tmp; +//│ tmp1 = Predef.print(this.i.c); +//│ tmp2 = runtime.safeCall(this.i.i1(1)); +//│ Predef.print(tmp2); //│ } //│ o1(c) { //│ return this.Inner(c) //│ } //│ o2(c, d) { -//│ let tmp6; /** scoped **/ -//│ tmp6 = this.Inner(c); -//│ return tmp6.i1(d) +//│ let tmp; /** scoped **/ +//│ tmp = this.Inner(c); +//│ return tmp.i1(d) //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Outer", ["a", "b"]]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index b4df883b66..b64f7a1a8d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -120,7 +120,7 @@ fun f(x) = if x is //│ JS (unsanitized): //│ let f4; /** scoped **/ //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp6; /** scoped **/ +//│ let x4, scrut1, argument0$4, tmp5; /** scoped **/ //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { @@ -128,19 +128,19 @@ fun f(x) = if x is //│ x4 = argument0$4; //│ scrut1 = x4 > 0; //│ if (scrut1 === true) { -//│ tmp6 = 42; +//│ tmp5 = 42; //│ break split_root$1 //│ } else { //│ break split_1$ //│ } //│ } else if (x3 instanceof None.class) { -//│ tmp6 = "ok"; +//│ tmp5 = "ok"; //│ break split_root$1 //│ } else { break split_1$ } //│ } -//│ tmp6 = Predef.print("oops"); +//│ tmp5 = Predef.print("oops"); //│ } -//│ return tmp6 +//│ return tmp5 //│ }; f(Some(0)) @@ -196,25 +196,25 @@ fun f(x) = print of if x is //│ JS (unsanitized): //│ let f6; /** scoped **/ //│ f6 = function f(x3) { -//│ let argument0$4, tmp10; /** scoped **/ +//│ let argument0$4, tmp9; /** scoped **/ //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some.class) { //│ argument0$4 = x3.value; //│ if (argument0$4 === 0) { -//│ tmp10 = "0"; +//│ tmp9 = "0"; //│ break split_root$1 //│ } else { //│ break split_1$ //│ } //│ } else if (x3 instanceof None.class) { -//│ tmp10 = "ok"; +//│ tmp9 = "ok"; //│ break split_root$1 //│ } else { break split_1$ } //│ } -//│ tmp10 = "oops"; +//│ tmp9 = "oops"; //│ } -//│ return Predef.print(tmp10) +//│ return Predef.print(tmp9) //│ }; f(Some(0)) diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 1078c18fd7..09b2e2a3c5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -21,7 +21,7 @@ fun test(a) = //│ JS (unsanitized): //│ let test1; /** scoped **/ //│ test1 = function test(a) { -//│ let Inner1, tmp; /** scoped **/ +//│ let Inner1; /** scoped **/ //│ Inner1 = function Inner(b) { //│ return globalThis.Object.freeze(new Inner.class(b)); //│ }; @@ -30,6 +30,7 @@ fun test(a) = //│ Inner1.class = this //│ } //│ constructor(b) { +//│ let tmp; /** scoped **/ //│ this.b = b; //│ tmp = Predef.print(a); //│ } @@ -80,7 +81,7 @@ fun test(a) = //│ JS (unsanitized): //│ let test2; /** scoped **/ //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let C11, C21, tmp, tmp1; /** scoped **/ //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -89,12 +90,13 @@ fun test(a) = //│ C11.class = this //│ } //│ constructor(b) { +//│ let tmp2; /** scoped **/ //│ this.b = b; -//│ tmp = globalThis.Object.freeze([ +//│ tmp2 = globalThis.Object.freeze([ //│ a, //│ this.b //│ ]); -//│ Predef.print(tmp); +//│ Predef.print(tmp2); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "C1", ["b"]]; @@ -107,19 +109,20 @@ fun test(a) = //│ C21.class = this //│ } //│ constructor(b) { +//│ let tmp2; /** scoped **/ //│ this.b = b; -//│ tmp1 = globalThis.Object.freeze([ +//│ tmp2 = globalThis.Object.freeze([ //│ a, //│ this.b //│ ]); -//│ Predef.print(tmp1); +//│ Predef.print(tmp2); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "C2", ["b"]]; //│ }); -//│ tmp2 = C11(1); -//│ tmp3 = C21(2); -//│ return globalThis.Object.freeze([ tmp2, tmp3 ]) +//│ tmp = C11(1); +//│ tmp1 = C21(2); +//│ return globalThis.Object.freeze([ tmp, tmp1 ]) //│ }; test(123) @@ -177,7 +180,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1, tmp; /** scoped **/ +//│ let Bar1; /** scoped **/ //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -186,6 +189,7 @@ Bar(1) //│ Bar1.class = this //│ } //│ constructor(x) { +//│ let tmp; /** scoped **/ //│ this.x = x; //│ tmp = this.foo(); //│ runtime.safeCall(tmp()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index 8bb0ca62d1..39096f59fa 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -13,12 +13,13 @@ fun foo() = class Test with print(foo()) //│ JS (unsanitized): -//│ let tmp, Test1; /** scoped **/ +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this //│ } //│ constructor() { +//│ let tmp; /** scoped **/ //│ tmp = foo(); //│ Predef.print(tmp); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index 18f3fbd3bc..e2bda5c876 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,7 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let tmp1, Test1; /** scoped **/ +//│ let Test1; /** scoped **/ //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -51,6 +51,7 @@ module Test with //│ } //│ static #s; //│ static { +//│ let tmp1; /** scoped **/ //│ Test.#s = 1; //│ tmp1 = Predef.print(Test.#s); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index dc8f7d5442..2d2522364b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -55,7 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1, tmp; /** scoped **/ +//│ let M1; /** scoped **/ //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -64,6 +64,7 @@ module M with //│ runtime.Unit; //│ } //│ static { +//│ let tmp; /** scoped **/ //│ globalThis.Object.freeze(class C { //│ static { //│ M.C = this @@ -110,7 +111,7 @@ M.y :re M.oops //│ ╔══[ERROR] Module 'M' does not contain member 'oops' -//│ ║ l.111: M.oops +//│ ║ l.112: M.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 04625446e1..f7054a8a6e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -185,7 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let Foo7; /** scoped **/ //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -194,6 +194,7 @@ class Foo(x, val y, z, val z, z) with //│ Foo7.class = this //│ } //│ constructor(x, y, z, z1, z2) { +//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ //│ this.#x = x; //│ this.y = y; //│ this.#z = z; @@ -236,10 +237,10 @@ Foo(10, 20, 30, 40, 50) class Foo(val z, val z) //│ ╔══[ERROR] Duplicate definition of member named 'z'. //│ ╟── Defined at: -//│ ║ l.236: class Foo(val z, val z) +//│ ║ l.237: class Foo(val z, val z) //│ ║ ^ //│ ╟── Defined at: -//│ ║ l.236: class Foo(val z, val z) +//│ ║ l.237: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): //│ let Foo9; /** scoped **/ diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 2ffd7e3b0b..86786c79dc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -274,20 +274,119 @@ fun f(x, y) = -// FIXME: `tmp1` is declared in a wrong place :sjs class A with val x = 1 + 2 //│ JS (unsanitized): -//│ let A3, tmp1; /** scoped **/ +//│ let A3; /** scoped **/ //│ globalThis.Object.freeze(class A2 { //│ static { //│ A3 = this //│ } //│ constructor() { +//│ let tmp1; /** scoped **/ //│ tmp1 = 1 + 2; //│ this.x = tmp1; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A"]; //│ }); + + +let state = 0 +//│ state = 0 + +:sjs +class Foo() with // the class definition is moved to the location of its companion + fun foo() = state +set state += 1 +module Foo with + val res = Foo().foo() +//│ JS (unsanitized): +//│ let Foo1, tmp1; /** scoped **/ +//│ tmp1 = state + 1; +//│ state = tmp1; +//│ Foo1 = function Foo() { +//│ return globalThis.Object.freeze(new Foo.class()); +//│ }; +//│ globalThis.Object.freeze(class Foo { +//│ static { +//│ Foo1.class = this +//│ } +//│ constructor() {} +//│ static { +//│ let tmp2, tmp3; /** scoped **/ +//│ tmp2 = Foo1(); +//│ tmp3 = tmp2.foo(); +//│ this.res = tmp3; +//│ } +//│ foo() { +//│ return state +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Foo", []]; +//│ }); + + + +:sjs +type I = Int +//│ JS (unsanitized): +//│ + + + +:sjs +class A(x) with + val y = x +//│ JS (unsanitized): +//│ let A5; /** scoped **/ +//│ A5 = function A(x1) { +//│ return globalThis.Object.freeze(new A.class(x1)); +//│ }; +//│ globalThis.Object.freeze(class A4 { +//│ static { +//│ A5.class = this +//│ } +//│ constructor(x1) { +//│ this.#x = x1; +//│ this.y = this.#x; +//│ } +//│ #x; +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A", [null]]; +//│ }); + + + +:sjs +module M with + data class A(x, y) +//│ JS (unsanitized): +//│ let M3; /** scoped **/ +//│ globalThis.Object.freeze(class M2 { +//│ static { +//│ M3 = this +//│ } +//│ constructor() { +//│ runtime.Unit; +//│ } +//│ static { +//│ this.A = function A(x1, y1) { +//│ return globalThis.Object.freeze(new A.class(x1, y1)); +//│ }; +//│ globalThis.Object.freeze(class A6 { +//│ static { +//│ M2.A.class = this +//│ } +//│ constructor(x1, y1) { +//│ this.x = x1; +//│ this.y = y1; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "A", ["x", "y"]]; +//│ }); +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "M"]; +//│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index 56d63f1ecd..ea5487f089 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -61,12 +61,13 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11, tmp; /** scoped **/ +//│ let Foo11; /** scoped **/ //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) //│ } //│ constructor() { +//│ let tmp; /** scoped **/ //│ tmp = Predef.id(Foo11); //│ this.self = tmp; //│ Object.defineProperty(this, "class", { diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 72893cfea0..83c9524e92 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,7 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1, tmp; /** scoped **/ +//│ let Lol1; /** scoped **/ //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; @@ -19,6 +19,7 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { +//│ let tmp; /** scoped **/ //│ let res, Cont$ctor$Lol$1, doUnwind; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index 1b932865d0..9a33020896 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -214,7 +214,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp5, tmp6; /** scoped **/ +//│ let M17, tmp3; /** scoped **/ //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -223,7 +223,7 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut; /** scoped **/ +//│ let scrut, tmp4; /** scoped **/ //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); //│ }; @@ -240,17 +240,17 @@ M.A().get //│ }); //│ scrut = M16.A(); //│ if (scrut instanceof M16.A.class) { -//│ tmp5 = 2; +//│ tmp4 = 2; //│ } else { -//│ tmp5 = 3; +//│ tmp4 = 3; //│ } -//│ this.x = tmp5; +//│ this.x = tmp4; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp6 = M17.A(); -//│ tmp6.get +//│ tmp3 = M17.A(); +//│ tmp3.get //│ = 4 module M with From a64ac1f6240e4f66db3616ce0ab7a6c172d12db8 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Mon, 1 Dec 2025 18:30:52 +0800 Subject: [PATCH 40/72] get rid of `Term.definedSyms` --- .../main/scala/hkmc2/codegen/Lowering.scala | 17 ++-- .../main/scala/hkmc2/semantics/Split.scala | 15 --- .../src/main/scala/hkmc2/semantics/Term.scala | 97 ------------------- .../hkmc2/semantics/ucs/Normalization.scala | 5 +- .../src/test/mlscript/basics/Underscores.mls | 10 +- .../src/test/mlscript/codegen/ConsoleLog.mls | 2 +- .../src/test/mlscript/codegen/Formatting.mls | 2 +- .../test/mlscript/codegen/PlainClasses.mls | 2 +- .../src/test/mlscript/codegen/RandomStuff.mls | 4 +- .../test/mlscript/handlers/SetInHandlers.mls | 14 +-- .../src/test/mlscript/lifter/ClassInFun.mls | 6 +- 11 files changed, 37 insertions(+), 137 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 7ac92e2794..58938107a7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -159,9 +159,11 @@ class Lowering()(using Config, TL, Raise, State, Ctx): blockImpl(stats, L((mut, RcdArg(S(l), r) :: flds)))(k) case (decl @ LetDecl(sym, annotations)) :: (stats @ ((_: DefineVar) :: _)) => reportAnnotations(decl, annotations) + if sym.asTrm.fold(true)(_.owner.isEmpty) then subst.collectScopedSym(sym) blockImpl(stats, res)(k) case (decl @ LetDecl(sym, annotations)) :: stats => reportAnnotations(decl, annotations) + if sym.asTrm.fold(true)(_.owner.isEmpty) then subst.collectScopedSym(sym) blockImpl(DefineVar(sym, Term.Lit(Tree.UnitLit(false))) :: stats, res)(k) case DefineVar(sym, rhs) :: stats => term(rhs): r => @@ -176,6 +178,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): d match case td: TermDefinition => reportAnnotations(td, td.extraAnnotations) + if td.owner.isEmpty then subst.collectScopedSym(td.sym) td.body match case N => // abstract declarations have no lowering blockImpl(stats, res)(k) @@ -213,6 +216,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): reportAnnotations(cls, cls.extraAnnotations) blockImpl(stats, res)(k) case _defn: ClassLikeDef => + if _defn.owner.isEmpty then subst.collectScopedSym(_defn.bsym) val defn = _defn match case cls: ClassDef => cls case mod: ModuleOrObjectDef if mod.kind is syntax.Mod => // * Currently, both objects and modules are represented as `ModuleOrObjectDef`s @@ -849,6 +853,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): rec(rhs, Nil)(k) case Blk(LetDecl(sym, _) :: DefineVar(sym2, rhs) :: Nil, res) => // Let bindings require(sym2 is sym) + subst.collectScopedSym(sym) setupSymbol(sym){r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) val arrSym = new TempSymbol(N, "arr") @@ -886,7 +891,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): reportAnnotations(decl, annotations) sym val ctor = - inScopedBlock(clsBody.blk.res.definedSyms ++ clsBody.nonMethods.flatMap(_.definedSyms)): + inScopedBlock: term_nonTail(Blk(clsBody.nonMethods, clsBody.blk.res))(ImplctRet) // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }` .mapTail: @@ -972,7 +977,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) val blk = - inScopedBlock(main.stats.foldLeft(main.res.definedSyms)(_ ++ _.definedSyms))(using LoweringCtx.empty): + inScopedBlock(using LoweringCtx.empty): block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -1017,15 +1022,15 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case ps => ps setupFunctionDef(physicalParams, bodyTerm, name) - def inScopedBlock(definedSymsInElaborated: Set[Symbol])(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = + def inScopedBlock(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = LoweringCtx.nestScoped.givenIn: val body = mkBlock - val scopedSyms = subst.getCollectedSym ++ definedSymsInElaborated + val scopedSyms = subst.getCollectedSym Scoped(scopedSyms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = - val scopedBody = inScopedBlock(bodyTerm.definedSyms)(returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(returnedTerm(bodyTerm)) (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = @@ -1138,7 +1143,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) go(paramLists.reverse, bod) def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - inScopedBlock(bod.definedSyms): + inScopedBlock: val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") val resSym = TempSymbol(N, dbgNme = "traceLogRes") diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index b16e22bc04..03ab1ba2d5 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,21 +24,6 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End - def definedSyms: Set[Symbol] = this match - case Cons(Branch(scrutinee, pattern, continuation), tail) => - tail.definedSyms ++ - locally: - continuation.definedSyms ++ - locally: - pattern match - case c: FlatPattern.ClassLike => c.arguments.fold(Set.empty)(_.unzip._1) - case FlatPattern.Tuple(size, inf) => Set.empty // TODO: seems to be ok to leave this as empty? - case FlatPattern.Record(entries) => entries.unzip._2 - case FlatPattern.Lit(_) => Set.empty - case Let(sym, term, tail) => term.definedSyms ++ tail.definedSyms + sym - case Else(default) => default.definedSyms - case End => Set.empty - inline def ~:(head: Branch): Split = Split.Cons(head, this) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala index bd85674321..3c18cae966 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala @@ -399,103 +399,6 @@ sealed trait Statement extends AutoLocated, ProductWithExtraInfo: case RcdField(field, rhs) => RcdField(field.mkClone, rhs.mkClone) case RcdSpread(rcd) => RcdSpread(rcd.mkClone) case DefineVar(sym, rhs) => DefineVar(sym, rhs.mkClone) - - lazy val definedSyms: Set[Symbol] = this match - // case Error => - // case UnitVal() => - // case Missing => - // case Lit(lit) => - // case Ref(tree, refNum, typ) => - // case Import(sym, str, file) => - case Resolved(typ, _) => typ.definedSyms - case App(l, r) => l.definedSyms ++ r.definedSyms - case TyApp(l, targs) => targs.foldLeft(l.definedSyms)(_ ++ _.definedSyms) - case Sel(s, _) => s.definedSyms - case SynthSel(s, _) => s.definedSyms - case SelProj(s, c, _) => s.definedSyms ++ c.definedSyms - case DynSel(prefix, fld, _) => prefix.definedSyms ++ fld.definedSyms - case Tup(tree) => tree - .flatMap: - case Fld(flags, term, asc) => term.definedSyms ++ asc.fold(Set.empty)(_.definedSyms) - case Spd(eager, term) => term.definedSyms - .toSet - case Mut(underlying) => underlying.definedSyms - case CtxTup(tree) => tree - .flatMap: - case Fld(flags, term, asc) => term.definedSyms ++ asc.fold(Set.empty)(_.definedSyms) - case Spd(eager, term) => term.definedSyms - .toSet - // IfLikes get their own scope - case IfLike(kw, split) => Set.empty - case SynthIf(split) => Set.empty - case Lam(params, body) => Set.empty - case FunTy(lhs, rhs, eff) => lhs.definedSyms ++ rhs.definedSyms ++ eff.fold(Set.empty)(_.definedSyms) - case Forall(tvs, outer, body) => body.definedSyms - case WildcardTy(in, out) => in.fold(Set.empty)(_.definedSyms ++ out.fold(Set.empty)(_.definedSyms)) - case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) - case Rcd(mut, stats) => stats.foldLeft(Set.empty)(_ ++ _.definedSyms) - case Quoted(body) => body.definedSyms - case Unquoted(body) => body.definedSyms - case New(typ, args, _) => args.foldLeft(typ.definedSyms)(_ ++ _.definedSyms) - case DynNew(cls, args) => args.foldLeft(cls.definedSyms)(_ ++ _.definedSyms) - case Asc(term, ty) => term.definedSyms ++ ty.definedSyms - case CompType(lhs, rhs, pol) => lhs.definedSyms ++ rhs.definedSyms - case Neg(rhs) => rhs.definedSyms - case Region(name, body) => body.definedSyms - case RegRef(reg, value) => reg.definedSyms ++ value.definedSyms - // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. - // And including the sym here may cause error for delayed init in a function: - // ``` - // let x - // fun f() = - // x = 2 - // ``` - // the definedSyms of the rhs of these two cases should be included - // because those needs to be included in the - // same current `Scoped` anyway - case Assgn(_, rhs) => rhs.definedSyms - case DefineVar(_, rhs) => rhs.definedSyms - case Drop(trm) => trm.definedSyms - case Deref(ref) => ref.definedSyms - case SetRef(ref, value) => ref.definedSyms ++ value.definedSyms - case Ret(result) => result.definedSyms - case Throw(result) => result.definedSyms - case Try(body, finallyDo) => body.definedSyms ++ finallyDo.definedSyms - case Annotated(annot, target) => target.definedSyms - case Handle(lhs, rhs, args, derivedClsSym, defs, body) => - Set.empty // TODO: - case LetDecl(sym, annotations) if sym.asTrm.fold(true)(_.owner.isEmpty) => Set(sym) - case RcdField(field, rhs) => field.definedSyms ++ rhs.definedSyms - case RcdSpread(rcd) => rcd.definedSyms - case termdef: TermDefinition if termdef.owner.isEmpty => Set(termdef.sym) - case tpeLikeDef: TypeLikeDef - if tpeLikeDef.hasDeclareModifier.isEmpty - && !tpeLikeDef.sym.isInstanceOf[TypeAliasSymbol] - && (!tpeLikeDef.isInstanceOf[ClassLikeDef] || tpeLikeDef.asInstanceOf[ClassLikeDef].owner.isEmpty) - => Set(tpeLikeDef.bsym) - case _ => Set.empty - // this match - // case Error | Missing | _: Lit | _: Ref | _: UnitVal | FunTy | TyApp => Set.empty - // case Blk(stats, res) => stats.foldLeft(res.definedSyms)((r, s) => r ++ s.definedSyms) - // case LetDecl(sym, annotations) => Set(sym) - // case termdef: TermDefinition => Set(termdef.sym) - // case tpeLikeDef: TypeLikeDef => Set(tpeLikeDef.bsym) - // // case imp: Import => Set(imp.sym) - // // IfLikes get their own scope - // case _: (IfLike | SynthIf) => Set.empty - // // `DefinedVar` is the actual definition of a symbol (not re-assignment), not decl. - // // And including the sym here may cause error for delayed init in a function: - // // ``` - // // let x - // // fun f() = - // // x = 2 - // // ``` - // // the definedSyms of the rhs of these two cases should be included - // // because those needs to be included in the - // // same current `Scoped` anyway - // case DefineVar(_, rhs) => rhs.definedSyms - // case Assgn(_, rhs) => rhs.definedSyms - // case _ => Set.empty def describe: Str = val desc = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index b0d5618c75..3cc4b333a4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -237,6 +237,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e topLevel: Bool )(using labels: Labels)(using LoweringCtx): Block = split match case Split.Let(sym, trm, tl) => + LoweringCtx.subst.collectScopedSym(sym) term_nonTail(trm): r => Assign(sym, r, lowerSplit(tl, cont, topLevel)) case Split.Cons(Branch(scrut, pat, tail), restSplit) => @@ -249,6 +250,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e pat match case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => + for args <- argsOpt; (arg, _) <- args do LoweringCtx.subst.collectScopedSym(arg) /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = val args = argsOpt.map(_.map(_._1)).getOrElse(Nil) @@ -277,6 +279,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctor)(k(mod, Nil)) case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.Record(entries) => + for (_, s) <- entries do LoweringCtx.subst.collectScopedSym(s) val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), @@ -398,7 +401,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e mainBlock // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) Scoped( - LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + LoweringCtx.subst.getCollectedSym/* ++ inputSplit.definedSyms */, if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) ) // Embed the `body` into `Label` if the term is a `while`. diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index 8e2f87f7a8..157da75cea 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -132,15 +132,19 @@ mkObj(1, 2) // * Note: this is parsed as `{let mkObj = x: _}, y: _, z: 3` // * TODO: improve or at least raise a warning :e -// :ge -// :re +:ge +:re let mkObj = x: _, y: _, z: 3 //│ ╔══[ERROR] Illegal position for '_' placeholder. //│ ║ l.137: let mkObj = x: _, y: _, z: 3 //│ ╙── ^ //│ mkObj = fun mkObj //│ y = undefined -//│ z = undefined +//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'z' +//│ ╟── which references the symbol introduced here +//│ ║ l.137: let mkObj = x: _, y: _, z: 3 +//│ ╙── ^ +//│ ═══[RUNTIME ERROR] ReferenceError: z is not defined // mkObj(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index a4d877e00c..ab2d6cfc34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let tmp2, x, tmp3, tmp4, y; /** scoped **/ +//│ let tmp2, tmp3, tmp4, x, y; /** scoped **/ //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index 05a4d15e22..b8ac39359c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -42,7 +42,7 @@ discard of { aaaa: 1 } discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let bbbb, tmp3, aaaaaaaaa; /** scoped **/ +//│ let aaaaaaaaa, bbbb, tmp3; /** scoped **/ //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 4697651570..fe5ef3c424 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -217,7 +217,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let tmp, tmp1, a, tmp2, tmp3; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3, a; /** scoped **/ //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index 4281427856..a252994119 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -69,8 +69,8 @@ do () //│ JS (unsanitized): //│ let f, f1; /** scoped **/ -//│ f = 1; -//│ f1 = function f() { let tmp; /** scoped **/ tmp = f1(); return tmp }; +//│ f1 = 1; +//│ f = function f() { let tmp; /** scoped **/ tmp = f(); return tmp }; //│ runtime.Unit //│ f = 1 diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 47b74b929d..6dd7795543 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,17 +8,17 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let tmp, tmp1, x, old, tmp2; /** scoped **/ +//│ let x, old, tmp, tmp1, tmp2; /** scoped **/ //│ x = 1; //│ old = x; //│ try { -//│ tmp = x + 1; -//│ x = tmp; -//│ tmp1 = Predef.print(x); -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ tmp1 = runtime.topLevelEffect(tmp1, false); +//│ tmp1 = x + 1; +//│ x = tmp1; +//│ tmp2 = Predef.print(x); +//│ if (tmp2 instanceof runtime.EffectSig.class) { +//│ tmp2 = runtime.topLevelEffect(tmp2, false); //│ } -//│ tmp2 = tmp1; +//│ tmp = tmp2; //│ } finally { x = old; } //│ x //│ > 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 8ac673ca75..8fc7c97d05 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,7 +88,7 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let tmp9, f5; /** scoped **/ +//│ let f6, tmp9; /** scoped **/ //│ let Bad1, Good1, Bad$, Good$, f$capture3; //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; @@ -185,7 +185,7 @@ f().foo() //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); -//│ f5 = function f() { +//│ f6 = function f() { //│ let x, y, z, w, tmp10, tmp11; /** scoped **/ //│ let capture; //│ capture = new f$capture3(null); @@ -197,7 +197,7 @@ f().foo() //│ tmp11 = tmp10.foo(); //│ return Good$(false, x, y, z, capture) //│ }; -//│ tmp9 = f5(); +//│ tmp9 = f6(); //│ runtime.safeCall(tmp9.foo()) //│ = 10111 From ab74823c15aad6122a5491e296c56fe37e68ce0b Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Mon, 1 Dec 2025 18:38:10 +0800 Subject: [PATCH 41/72] get rid of vararg --- .../main/scala/hkmc2/codegen/Lowering.scala | 20 ++++++++++--------- .../hkmc2/semantics/ucs/Normalization.scala | 3 ++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 58938107a7..e227c2a687 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -43,7 +43,7 @@ class LoweringCtx( ): val map = initMap - def collectScopedSym(s: Symbol*) = definedSymsDuringLowering.addAll(s) + def collectScopedSym(s: Symbol) = definedSymsDuringLowering.add(s) def getCollectedSym: collection.Set[Symbol] = definedSymsDuringLowering /* def +(kv: (Local, Value)): Subst = @@ -1150,13 +1150,13 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg") val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") - subst.collectScopedSym( - enterMsgSym, - prevIndentLvlSym, - resSym, - retMsgSym, - resInspectedSym) - subst.collectScopedSym(psInspectedSyms.unzip._1*) + + subst.collectScopedSym(enterMsgSym) + subst.collectScopedSym(prevIndentLvlSym) + subst.collectScopedSym(resSym) + subst.collectScopedSym(retMsgSym) + subst.collectScopedSym(resInspectedSym) + for (s, _) <- psInspectedSyms do subst.collectScopedSym(s) val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 @@ -1164,7 +1164,9 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc val tmp1, tmp2, tmp3 = TempSymbol(N) - subst.collectScopedSym(tmp1, tmp2, tmp3) + subst.collectScopedSym(tmp1) + subst.collectScopedSym(tmp2) + subst.collectScopedSym(tmp3) assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 3cc4b333a4..50cd1a9d85 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -411,7 +411,8 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e if config.rewriteWhileLoops then val loopResult = TempSymbol(N) val isReturned = TempSymbol(N) - outerCtx.collectScopedSym(loopResult, isReturned) + outerCtx.collectScopedSym(loopResult) + outerCtx.collectScopedSym(isReturned) val loopEnd: Path = Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) val blk = blockBuilder From d20dcca75e7c76c2f0b70a6d1dc7a1c2f01f5979 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Mon, 1 Dec 2025 18:39:22 +0800 Subject: [PATCH 42/72] rename --- .../main/scala/hkmc2/codegen/Lowering.scala | 62 +++++++++---------- .../hkmc2/semantics/ucs/Normalization.scala | 8 +-- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index e227c2a687..ab93190a8d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -58,12 +58,12 @@ class LoweringCtx( case _ => v object LoweringCtx: val empty = LoweringCtx(Map.empty, false, collection.mutable.Set.empty) - def subst(using sub: LoweringCtx): LoweringCtx = sub + def loweringCtx(using sub: LoweringCtx): LoweringCtx = sub def nestFunc(using sub: LoweringCtx): LoweringCtx = LoweringCtx(sub.map, true, sub.definedSymsDuringLowering) def nestScoped(using sub: LoweringCtx): LoweringCtx = LoweringCtx(sub.map, sub.mayRet, collection.mutable.Set.empty) end LoweringCtx -import LoweringCtx.subst +import LoweringCtx.loweringCtx class Lowering()(using Config, TL, Raise, State, Ctx): @@ -159,11 +159,11 @@ class Lowering()(using Config, TL, Raise, State, Ctx): blockImpl(stats, L((mut, RcdArg(S(l), r) :: flds)))(k) case (decl @ LetDecl(sym, annotations)) :: (stats @ ((_: DefineVar) :: _)) => reportAnnotations(decl, annotations) - if sym.asTrm.fold(true)(_.owner.isEmpty) then subst.collectScopedSym(sym) + if sym.asTrm.fold(true)(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) blockImpl(stats, res)(k) case (decl @ LetDecl(sym, annotations)) :: stats => reportAnnotations(decl, annotations) - if sym.asTrm.fold(true)(_.owner.isEmpty) then subst.collectScopedSym(sym) + if sym.asTrm.fold(true)(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) blockImpl(DefineVar(sym, Term.Lit(Tree.UnitLit(false))) :: stats, res)(k) case DefineVar(sym, rhs) :: stats => term(rhs): r => @@ -178,7 +178,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): d match case td: TermDefinition => reportAnnotations(td, td.extraAnnotations) - if td.owner.isEmpty then subst.collectScopedSym(td.sym) + if td.owner.isEmpty then loweringCtx.collectScopedSym(td.sym) td.body match case N => // abstract declarations have no lowering blockImpl(stats, res)(k) @@ -216,7 +216,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): reportAnnotations(cls, cls.extraAnnotations) blockImpl(stats, res)(k) case _defn: ClassLikeDef => - if _defn.owner.isEmpty then subst.collectScopedSym(_defn.bsym) + if _defn.owner.isEmpty then loweringCtx.collectScopedSym(_defn.bsym) val defn = _defn match case cls: ClassDef => cls case mod: ModuleOrObjectDef if mod.kind is syntax.Mod => // * Currently, both objects and modules are represented as `ModuleOrObjectDef`s @@ -407,13 +407,13 @@ class Lowering()(using Config, TL, Raise, State, Ctx): // * (non-local functions are compiled into getter methods selected on some prefix) if td.params.isEmpty then val l = new TempSymbol(S(ref)) - subst.collectScopedSym(l) + loweringCtx.collectScopedSym(l) return Assign(l, Call(Value.Ref(bs, disamb).withLocOf(ref), Nil)(true, true, annots.contains(Annot.TailCall)), k(Value.Ref(l, disamb))) case S(_) => () case N => () // TODO panic here; can only lower refs to elab'd symbols case _ => () warnStmt - k(subst(Value.Ref(sym, disamb).withLocOf(ref))) + k(loweringCtx(Value.Ref(sym, disamb).withLocOf(ref))) @tailrec final def term(t: st, inStmtPos: Bool = false)(k: Result => Block)(using LoweringCtx): Block = @@ -481,7 +481,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val isOr = sym is State.orSymbol if isAnd || isOr then val lamSym = BlockMemberSymbol("lambda", Nil, false) - subst.collectScopedSym(lamSym) + loweringCtx.collectScopedSym(lamSym) val lamDef = FunDefn.withFreshSymbol(N, lamSym, PlainParamList(Nil) :: Nil, returnedTerm(arg2))(isTailRec = false) Define( lamDef, @@ -577,7 +577,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): S(Handler(td.sym, resumeSym, paramLists, bodyBlock)) }.collect{ case Some(v) => v } val resSym = TempSymbol(S(t)) - subst.collectScopedSym(resSym) + loweringCtx.collectScopedSym(resSym) subTerm(rhs): par => subTerms(as): asr => HandleBlock(lhs, resSym, par, asr, cls, handlers, @@ -614,7 +614,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): then k(Lambda(paramLists.head, bodyBlock)) else val lamSym = new BlockMemberSymbol("lambda", Nil, false) - subst.collectScopedSym(lamSym) + loweringCtx.collectScopedSym(lamSym) val lamDef = FunDefn.withFreshSymbol(N, lamSym, paramLists, bodyBlock)(isTailRec = false) Define( lamDef, @@ -665,14 +665,14 @@ class Lowering()(using Config, TL, Raise, State, Ctx): inner => lowerArg(arg): asr2 => val ts = TempSymbol(N) - subst.collectScopedSym(ts) + loweringCtx.collectScopedSym(ts) Assign(ts, Call(inner, asr2)(true, true, false), acc(Value.Ref(ts))) val ts = TempSymbol(N) - subst.collectScopedSym(ts) + loweringCtx.collectScopedSym(ts) Assign(ts, Instantiate(mut, sr, asr), z(Value.Ref(ts))) case S((isym, rft)) => val sym = new BlockMemberSymbol(isym.name, Nil) - subst.collectScopedSym(sym) + loweringCtx.collectScopedSym(sym) val (mtds, publicFlds, privateFlds, ctor) = gatherMembers(rft) val pctor = parentConstructor(cls, as) val clsDef = ClsLikeDefn(N, isym, sym, syntax.Cls, N, Nil, S(sr), @@ -682,7 +682,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case Try(sub, finallyDo) => val l = new TempSymbol(S(sub)) - subst.collectScopedSym(l) + loweringCtx.collectScopedSym(l) TryBlock( subTerm_nonTail(sub)(p => Assign(l, p, End())), subTerm_nonTail(finallyDo)(_ => End()), @@ -853,7 +853,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): rec(rhs, Nil)(k) case Blk(LetDecl(sym, _) :: DefineVar(sym2, rhs) :: Nil, res) => // Let bindings require(sym2 is sym) - subst.collectScopedSym(sym) + loweringCtx.collectScopedSym(sym) setupSymbol(sym){r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) val arrSym = new TempSymbol(N, "arr") @@ -933,7 +933,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): Begin(b, k(asr.reverse)) else val rcdSym = new TempSymbol(N, "rcd") - subst.collectScopedSym(rcdSym) + loweringCtx.collectScopedSym(rcdSym) Begin( b, Assign( @@ -963,12 +963,12 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case p: Path => k(p) case Lambda(params, body) => val lamSym = BlockMemberSymbol("lambda", Nil, false) - subst.collectScopedSym(lamSym) + loweringCtx.collectScopedSym(lamSym) val lamDef = FunDefn.withFreshSymbol(N, lamSym, params :: Nil, body)(isTailRec = false) Define(lamDef, k(Value.Ref(lamSym, N))) case r => val l = new TempSymbol(N) - subst.collectScopedSym(l) + loweringCtx.collectScopedSym(l) Assign(l, r, k(l |> Value.Ref.apply)) @@ -1025,7 +1025,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): def inScopedBlock(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = LoweringCtx.nestScoped.givenIn: val body = mkBlock - val scopedSyms = subst.getCollectedSym + val scopedSyms = loweringCtx.getCollectedSym Scoped(scopedSyms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) @@ -1079,7 +1079,7 @@ trait LoweringSelSanityChecks(using Config, TL, Raise, State) if !instrument then return super.setupSelection(prefix, nme, disamb)(k) subTerm(prefix): p => val selRes = TempSymbol(N, "selRes") - subst.collectScopedSym(selRes) + loweringCtx.collectScopedSym(selRes) // * We are careful to access `x.f` before `x.f$__checkNotMethod` in case `x` is, eg, `undefined` and // * the access should throw an error like `TypeError: Cannot read property 'f' of undefined`. val b0 = blockBuilder @@ -1089,7 +1089,7 @@ trait LoweringSelSanityChecks(using Config, TL, Raise, State) b0 else val discardedSym = TempSymbol(N, "discarded") - subst.collectScopedSym(discardedSym) + loweringCtx.collectScopedSym(discardedSym) b0 .assign(discardedSym, Select(p, Tree.Ident(nme.name+"$__checkNotMethod"))(N))) .ifthen(selRes.asPath, @@ -1151,12 +1151,12 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") - subst.collectScopedSym(enterMsgSym) - subst.collectScopedSym(prevIndentLvlSym) - subst.collectScopedSym(resSym) - subst.collectScopedSym(retMsgSym) - subst.collectScopedSym(resInspectedSym) - for (s, _) <- psInspectedSyms do subst.collectScopedSym(s) + loweringCtx.collectScopedSym(enterMsgSym) + loweringCtx.collectScopedSym(prevIndentLvlSym) + loweringCtx.collectScopedSym(resSym) + loweringCtx.collectScopedSym(retMsgSym) + loweringCtx.collectScopedSym(resInspectedSym) + for (s, _) <- psInspectedSyms do loweringCtx.collectScopedSym(s) val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 @@ -1164,9 +1164,9 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc val tmp1, tmp2, tmp3 = TempSymbol(N) - subst.collectScopedSym(tmp1) - subst.collectScopedSym(tmp2) - subst.collectScopedSym(tmp3) + loweringCtx.collectScopedSym(tmp1) + loweringCtx.collectScopedSym(tmp2) + loweringCtx.collectScopedSym(tmp3) assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 50cd1a9d85..f4db72a690 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -237,7 +237,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e topLevel: Bool )(using labels: Labels)(using LoweringCtx): Block = split match case Split.Let(sym, trm, tl) => - LoweringCtx.subst.collectScopedSym(sym) + LoweringCtx.loweringCtx.collectScopedSym(sym) term_nonTail(trm): r => Assign(sym, r, lowerSplit(tl, cont, topLevel)) case Split.Cons(Branch(scrut, pat, tail), restSplit) => @@ -250,7 +250,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e pat match case FlatPattern.Lit(lit) => mkMatch(Case.Lit(lit) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.ClassLike(ctor, symbol, argsOpt, _refined) => - for args <- argsOpt; (arg, _) <- args do LoweringCtx.subst.collectScopedSym(arg) + for args <- argsOpt; (arg, _) <- args do LoweringCtx.loweringCtx.collectScopedSym(arg) /** Make a continuation that creates the match. */ def k(ctorSym: ClassLikeSymbol, clsParams: Ls[TermSymbol])(st: Path): Block = val args = argsOpt.map(_.map(_._1)).getOrElse(Nil) @@ -279,7 +279,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e subTerm_nonTail(ctor)(k(mod, Nil)) case FlatPattern.Tuple(len, inf) => mkMatch(Case.Tup(len, inf) -> lowerSplit(tail, cont, topLevel = false)) case FlatPattern.Record(entries) => - for (_, s) <- entries do LoweringCtx.subst.collectScopedSym(s) + for (_, s) <- entries do LoweringCtx.loweringCtx.collectScopedSym(s) val objectSym = ctx.builtins.Object mkMatch( // checking that we have an object Case.Cls(objectSym, Value.Ref(BuiltinSymbol(objectSym.nme, false, false, true, false))), @@ -401,7 +401,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e mainBlock // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) Scoped( - LoweringCtx.subst.getCollectedSym/* ++ inputSplit.definedSyms */, + LoweringCtx.loweringCtx.getCollectedSym/* ++ inputSplit.definedSyms */, if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) ) // Embed the `body` into `Label` if the term is a `while`. From 09ea124f8d8b41078f8306b09f14b49158627267 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 2 Dec 2025 01:28:06 +0800 Subject: [PATCH 43/72] fix problems related to handlers in nested `if`/`while`s: currently while loops that are not rewritten into a function do not get a nested scope for their bodies --- .../main/scala/hkmc2/codegen/Lowering.scala | 2 +- .../hkmc2/semantics/ucs/Normalization.scala | 238 +++++++------- .../src/test/mlscript-compile/Predef.mjs | 21 +- .../src/test/mlscript-compile/Runtime.mjs | 309 ++++++++---------- .../test/mlscript/codegen/MergeMatchArms.mls | 28 +- .../src/test/mlscript/codegen/Scoped.mls | 34 ++ .../src/test/mlscript/handlers/Effects.mls | 3 +- .../test/mlscript/std/FingerTreeListTest.mls | 2 +- 8 files changed, 323 insertions(+), 314 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index ab93190a8d..303d88b5dc 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -265,7 +265,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val mtds = methods.map: case (sym, params, split) => val paramLists = params :: Nil - val bodyBlock = ucs.Normalization(this)(split)(Ret) + val bodyBlock = inScopedBlock(ucs.Normalization(this)(split)(Ret)) FunDefn.withFreshSymbol(N, sym, paramLists, bodyBlock)(isTailRec = false) // The return type is intended to be consistent with `gatherMembers` (mtds, Nil, Nil, End()) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index f4db72a690..c43523df81 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -318,127 +318,129 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e def apply(split: Split)(k: Result => Block)(using Config, LoweringCtx): Block = this(split, `if`, N, k) - private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = LoweringCtx.nestScoped.givenIn: - var usesResTmp = false - // The symbol of the temporary variable for the result of the `if`-like term. - // It will be created in one of the following situations. - // 1. The continuation `k` is not a tail operation. - // 2. There are shared consequents in the `if`-like term. - // 3. The term is a `while` and the result is used. - lazy val l = - usesResTmp = true - val res = new TempSymbol(t) - outerCtx.collectScopedSym(res) - res - // The symbol for the loop label if the term is a `while`. - lazy val loopLabel = new TempSymbol(t) - lazy val f = - val res = new BlockMemberSymbol("while", Nil, false) - outerCtx.collectScopedSym(res) - res - val normalized = tl.scoped("ucs:normalize"): - normalize(inputSplit)(using VarSet()) - tl.scoped("ucs:normalized"): - tl.log(s"Normalized:\n${normalized.prettyPrint}") - // Collect consequents that are shared in more than one branch. - given labels: Labels = createLabelsForDuplicatedBranches(normalized) - lazy val rootBreakLabel = new TempSymbol(N, "split_root$") - lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) - lazy val assignResult = (r: Result) => Assign(l, r, End()) - lazy val loopCont = if config.rewriteWhileLoops - then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) - else Continue(loopLabel) - val cont = - if kw === `while` then - // If the term is a `while`, the action of `else` branches depends on - // whether the the enclosing split is at the top level or not. - R((topLevel: Bool) => (r: Result) => Assign(l, r, if topLevel then End() else loopCont)) - else if labels.isEmpty then - if k.isInstanceOf[TailOp] then - // If there are no shared consequents and the continuation is a tail - // operation, we can call it directly. - L(k) + private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = + val nestScope = config.rewriteWhileLoops && kw === `while` + (if nestScope then LoweringCtx.nestScoped else outerCtx).givenIn: + var usesResTmp = false + // The symbol of the temporary variable for the result of the `if`-like term. + // It will be created in one of the following situations. + // 1. The continuation `k` is not a tail operation. + // 2. There are shared consequents in the `if`-like term. + // 3. The term is a `while` and the result is used. + lazy val l = + usesResTmp = true + val res = new TempSymbol(t) + outerCtx.collectScopedSym(res) + res + // The symbol for the loop label if the term is a `while`. + lazy val loopLabel = new TempSymbol(t) + lazy val f = + val res = new BlockMemberSymbol("while", Nil, false) + outerCtx.collectScopedSym(res) + res + val normalized = tl.scoped("ucs:normalize"): + normalize(inputSplit)(using VarSet()) + tl.scoped("ucs:normalized"): + tl.log(s"Normalized:\n${normalized.prettyPrint}") + // Collect consequents that are shared in more than one branch. + given labels: Labels = createLabelsForDuplicatedBranches(normalized) + lazy val rootBreakLabel = new TempSymbol(N, "split_root$") + lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) + lazy val assignResult = (r: Result) => Assign(l, r, End()) + lazy val loopCont = if config.rewriteWhileLoops + then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) + else Continue(loopLabel) + val cont = + if kw === `while` then + // If the term is a `while`, the action of `else` branches depends on + // whether the the enclosing split is at the top level or not. + R((topLevel: Bool) => (r: Result) => Assign(l, r, if topLevel then End() else loopCont)) + else if labels.isEmpty then + if k.isInstanceOf[TailOp] then + // If there are no shared consequents and the continuation is a tail + // operation, we can call it directly. + L(k) + else + // Otherwise, if the continuation is not a tail operation, we should + // save the result in a temporary variable and call the continuation + // in the end. + L(assignResult) else - // Otherwise, if the continuation is not a tail operation, we should - // save the result in a temporary variable and call the continuation - // in the end. - L(assignResult) - else - // When there are shared consequents, we are forced to save the result - // in the temporary variable nevertheless. Note that `cont` only gets - // called for non-shared consequents, so we should break to the end of - // the entire split after the assignment. - L(breakRoot) - // The main block contains the lowered split, where each shared consequent - // is replaced with a `Break` to the corresponding label. - val mainBlock = - val innermostBlock = lowerSplit(normalized, cont, topLevel = true) - // Wrap the main block in a labelled block for each shared consequent. The - // `rest` of each `Label` is the lowered consequent plus a `Break` to the - // end of the entire `if` term. Otherwise, it will fall through to the outer - // consequent, which is the wrong semantics. - val innerBlock: Block = labels.consequents match - case Nil => innermostBlock - case all @ (head :: tail) => - def wrap(consequents: Ls[(Term, TempSymbol)]): Block = - consequents.foldRight(innermostBlock): - case ((term, label), innerBlock) => - Label(label, false, innerBlock, term_nonTail(term)(breakRoot)) - // There is no need to generate `break` for the outermost split. - if labels.default.isEmpty then - Label(head._2, false, wrap(tail), term_nonTail(head._1)(assignResult)) - else wrap(all) - labels.default match - case S(label) => Label(label, false, innerBlock, throwMatchErrorBlock) - case N => innerBlock - // If there are shared consequents, we need a wrap the entire block in a - // `Label` so that `Break`s in the shared consequents can jump to the end. - val body = - val possiblyScoped = - // lowering.possiblyScoped( - // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - // mainBlock) - mainBlock - // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - Scoped( - LoweringCtx.loweringCtx.getCollectedSym/* ++ inputSplit.definedSyms */, - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - ) - // Embed the `body` into `Label` if the term is a `while`. - lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) - val block = - if kw === `while` then - if config.rewriteWhileLoops then - val loopResult = TempSymbol(N) - val isReturned = TempSymbol(N) - outerCtx.collectScopedSym(loopResult) - outerCtx.collectScopedSym(isReturned) - val loopEnd: Path = - Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) - val blk = blockBuilder - .assign(l, Value.Lit(Tree.UnitLit(false))) - .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) - .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) - if summon[LoweringCtx].mayRet then - blk - .assign(isReturned, Call(Value.Ref(State.builtinOpsMap("!==")), - loopResult.asPath.asArg :: loopEnd.asArg :: Nil)(true, false, false)) - .ifthen(Value.Ref(isReturned), Case.Lit(Tree.BoolLit(true)), - Return(Value.Ref(loopResult), false), - S(rest) - ) - .end + // When there are shared consequents, we are forced to save the result + // in the temporary variable nevertheless. Note that `cont` only gets + // called for non-shared consequents, so we should break to the end of + // the entire split after the assignment. + L(breakRoot) + // The main block contains the lowered split, where each shared consequent + // is replaced with a `Break` to the corresponding label. + val mainBlock = + val innermostBlock = lowerSplit(normalized, cont, topLevel = true) + // Wrap the main block in a labelled block for each shared consequent. The + // `rest` of each `Label` is the lowered consequent plus a `Break` to the + // end of the entire `if` term. Otherwise, it will fall through to the outer + // consequent, which is the wrong semantics. + val innerBlock: Block = labels.consequents match + case Nil => innermostBlock + case all @ (head :: tail) => + def wrap(consequents: Ls[(Term, TempSymbol)]): Block = + consequents.foldRight(innermostBlock): + case ((term, label), innerBlock) => + Label(label, false, innerBlock, term_nonTail(term)(breakRoot)) + // There is no need to generate `break` for the outermost split. + if labels.default.isEmpty then + Label(head._2, false, wrap(tail), term_nonTail(head._1)(assignResult)) + else wrap(all) + labels.default match + case S(label) => Label(label, false, innerBlock, throwMatchErrorBlock) + case N => innerBlock + // If there are shared consequents, we need a wrap the entire block in a + // `Label` so that `Break`s in the shared consequents can jump to the end. + val body = + val possiblyScoped = + // lowering.possiblyScoped( + // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + // mainBlock) + mainBlock + // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + Scoped( + if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty/* ++ inputSplit.definedSyms */, + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + ) + // Embed the `body` into `Label` if the term is a `while`. + lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) + val block = + if kw === `while` then + if config.rewriteWhileLoops then + val loopResult = TempSymbol(N) + val isReturned = TempSymbol(N) + outerCtx.collectScopedSym(loopResult) + outerCtx.collectScopedSym(isReturned) + val loopEnd: Path = + Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) + val blk = blockBuilder + .assign(l, Value.Lit(Tree.UnitLit(false))) + .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) + .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) + if summon[LoweringCtx].mayRet then + blk + .assign(isReturned, Call(Value.Ref(State.builtinOpsMap("!==")), + loopResult.asPath.asArg :: loopEnd.asArg :: Nil)(true, false, false)) + .ifthen(Value.Ref(isReturned), Case.Lit(Tree.BoolLit(true)), + Return(Value.Ref(loopResult), false), + S(rest) + ) + .end + else + blk.rest(rest) else - blk.rest(rest) + Begin(Label(loopLabel, true, body, End()), rest) + else if labels.isEmpty && k.isInstanceOf[TailOp] then + body else - Begin(Label(loopLabel, true, body, End()), rest) - else if labels.isEmpty && k.isInstanceOf[TailOp] then - body - else - Begin(body, rest) - scoped("ucs:lowered"): - log(s"Lowered:\n${block.showAsTree}") - block + Begin(body, rest) + scoped("ucs:lowered"): + log(s"Lowered:\n${block.showAsTree}") + block end Normalization object Normalization: diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index e7f4e8a4fa..6c203b33e1 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -124,7 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, tmp; /** scoped **/ + let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; /** scoped **/ len = rest.length; scrut = len == 0; if (scrut === true) { @@ -132,19 +132,18 @@ globalThis.Object.freeze(class Predef { } else { i = len - 1; init = runtime.safeCall(rest.at(i)); - tmp1: while (true) { - let scrut1, tmp2, tmp3, tmp4; /** scoped **/ + tmp4: while (true) { scrut1 = i > 0; if (scrut1 === true) { - tmp2 = i - 1; - i = tmp2; - tmp3 = runtime.safeCall(rest.at(i)); - tmp4 = runtime.safeCall(f(tmp3, init)); - init = tmp4; - tmp = runtime.Unit; - continue tmp1 + tmp = i - 1; + i = tmp; + tmp1 = runtime.safeCall(rest.at(i)); + tmp2 = runtime.safeCall(f(tmp1, init)); + init = tmp2; + tmp3 = runtime.Unit; + continue tmp4 } else { - tmp = runtime.Unit; + tmp3 = runtime.Unit; } break; } diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index a2cb048716..6708dba415 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -427,7 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ + let scrut, name, scrut1, scrut2, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -437,35 +437,34 @@ globalThis.Object.freeze(class Runtime { }); scrut = runtime.short_or(tmp, lambda); if (scrut === true) { - let scrut1, scrut2, tmp12; /** scoped **/ scrut1 = functionName.length > 0; if (scrut1 === true) { - tmp12 = " '" + functionName; - tmp1 = tmp12 + "'"; + tmp1 = " '" + functionName; + tmp2 = tmp1 + "'"; } else { - tmp1 = ""; + tmp2 = ""; } - name = tmp1; - tmp2 = "Function" + name; - tmp3 = tmp2 + " expected "; + name = tmp2; + tmp3 = "Function" + name; + tmp4 = tmp3 + " expected "; if (isUB === true) { - tmp4 = ""; + tmp5 = ""; } else { - tmp4 = "at least "; + tmp5 = "at least "; } - tmp5 = tmp3 + tmp4; - tmp6 = tmp5 + expected; - tmp7 = tmp6 + " argument"; + tmp6 = tmp4 + tmp5; + tmp7 = tmp6 + expected; + tmp8 = tmp7 + " argument"; scrut2 = expected === 1; if (scrut2 === true) { - tmp8 = ""; + tmp9 = ""; } else { - tmp8 = "s"; + tmp9 = "s"; } - tmp9 = tmp7 + tmp8; - tmp10 = tmp9 + " but got "; - tmp11 = tmp10 + got; - throw globalThis.Error(tmp11) + tmp10 = tmp8 + tmp9; + tmp11 = tmp10 + " but got "; + tmp12 = tmp11 + got; + throw globalThis.Error(tmp12) } else { return runtime.Unit } @@ -514,72 +513,66 @@ globalThis.Object.freeze(class Runtime { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } static topLevelEffect(tr, debug) { - let tmp, tmp1; /** scoped **/ - tmp2: while (true) { - let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + let scrut, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + tmp6: while (true) { scrut = tr.handler === Runtime.PrintStackEffect; if (scrut === true) { - tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); - tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); - tmp5 = Runtime.resume(tr.contTrace); - tmp6 = runtime.safeCall(tmp5(runtime.Unit)); - tr = tmp6; - tmp = runtime.Unit; - continue tmp2 + tmp = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); + tmp1 = runtime.safeCall(globalThis.console.log(tmp)); + tmp2 = Runtime.resume(tr.contTrace); + tmp3 = runtime.safeCall(tmp2(runtime.Unit)); + tr = tmp3; + tmp4 = runtime.Unit; + continue tmp6 } else { - tmp = runtime.Unit; + tmp4 = runtime.Unit; } break; } if (tr instanceof Runtime.EffectSig.class) { - tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; - throw Runtime.showStackTrace(tmp1, tr, debug, false) + tmp5 = "Error: Unhandled effect " + tr.handler.constructor.name; + throw Runtime.showStackTrace(tmp5, tr, debug, false) } else { return tr } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ + let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { - let tmp3; /** scoped **/ - tmp4: while (true) { - let scrut, cur, tmp5, tmp6; /** scoped **/ + tmp19: while (true) { scrut = curHandler !== null; if (scrut === true) { - let scrut1, tmp7, tmp8; /** scoped **/ cur = curHandler.next; - tmp9: while (true) { - let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ - scrut2 = cur !== null; - if (scrut2 === true) { - let scrut3, lambda, tmp19, tmp20; /** scoped **/ + tmp20: while (true) { + scrut1 = cur !== null; + if (scrut1 === true) { locals = cur.getLocals; - tmp10 = locals.length - 1; - curLocals = runtime.safeCall(locals.at(tmp10)); + tmp = locals.length - 1; + curLocals = runtime.safeCall(locals.at(tmp)); loc = cur.getLoc; if (loc === null) { - tmp11 = "pc=" + cur.pc; + tmp1 = "pc=" + cur.pc; } else { - tmp11 = loc; + tmp1 = loc; } - loc1 = tmp11; + loc1 = tmp1; split_root$: { split_1$: { if (showLocals === true) { - scrut3 = curLocals.locals.length > 0; - if (scrut3 === true) { + scrut2 = curLocals.locals.length > 0; + if (scrut2 === true) { lambda = (undefined, function (l) { let tmp21, tmp22; /** scoped **/ tmp21 = l.localName + "="; tmp22 = Rendering.render(l.value); return tmp21 + tmp22 }); - tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); - tmp20 = runtime.safeCall(tmp19.join(", ")); - tmp12 = " with locals: " + tmp20; + tmp2 = runtime.safeCall(curLocals.locals.map(lambda)); + tmp3 = runtime.safeCall(tmp2.join(", ")); + tmp4 = " with locals: " + tmp3; break split_root$ } else { break split_1$ @@ -588,67 +581,66 @@ globalThis.Object.freeze(class Runtime { break split_1$ } } - tmp12 = ""; + tmp4 = ""; } - localsMsg = tmp12; - tmp13 = "\n\tat " + curLocals.fnName; - tmp14 = tmp13 + " ("; - tmp15 = tmp14 + loc1; - tmp16 = tmp15 + ")"; - tmp17 = msg + tmp16; - msg = tmp17; - tmp18 = msg + localsMsg; - msg = tmp18; + localsMsg = tmp4; + tmp5 = "\n\tat " + curLocals.fnName; + tmp6 = tmp5 + " ("; + tmp7 = tmp6 + loc1; + tmp8 = tmp7 + ")"; + tmp9 = msg + tmp8; + msg = tmp9; + tmp10 = msg + localsMsg; + msg = tmp10; cur = cur.next; atTail = false; - tmp5 = runtime.Unit; - continue tmp9 + tmp11 = runtime.Unit; + continue tmp20 } else { - tmp5 = runtime.Unit; + tmp11 = runtime.Unit; } break; } curHandler = curHandler.nextHandler; - scrut1 = curHandler !== null; - if (scrut1 === true) { - tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; - tmp8 = msg + tmp7; - msg = tmp8; + scrut3 = curHandler !== null; + if (scrut3 === true) { + tmp12 = "\n\twith handler " + curHandler.handler.constructor.name; + tmp13 = msg + tmp12; + msg = tmp13; atTail = false; - tmp6 = runtime.Unit; + tmp14 = runtime.Unit; } else { - tmp6 = runtime.Unit; + tmp14 = runtime.Unit; } - tmp = tmp6; - continue tmp4 + tmp15 = tmp14; + continue tmp19 } else { - tmp = runtime.Unit; + tmp15 = runtime.Unit; } break; } if (atTail === true) { - tmp3 = msg + "\n\tat tail position"; - msg = tmp3; - tmp1 = runtime.Unit; + tmp16 = msg + "\n\tat tail position"; + msg = tmp16; + tmp17 = runtime.Unit; } else { - tmp1 = runtime.Unit; + tmp17 = runtime.Unit; } - tmp2 = tmp1; + tmp18 = tmp17; } else { - tmp2 = runtime.Unit; + tmp18 = runtime.Unit; } return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - let scrut, tmp5, tmp6, tmp7; /** scoped **/ tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut1, tmp8, tmp9; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { + let scrut3, tmp8, tmp9; /** scoped **/ + scrut3 = runtime.safeCall(m.has(cont)); + if (scrut3 === true) { tmp8 = ", " + marker; tmp9 = result + tmp8; result = tmp9; @@ -660,26 +652,24 @@ globalThis.Object.freeze(class Runtime { tmp1 = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; /** scoped **/ - tmp5 = reps + 1; - reps = tmp5; + tmp2 = reps + 1; + reps = tmp2; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp6 = runtime.Unit; + tmp3 = runtime.Unit; } - tmp7 = result + ", REPEAT"; - result = tmp7; - tmp2 = runtime.Unit; + tmp4 = result + ", REPEAT"; + result = tmp4; + tmp5 = runtime.Unit; } else { - tmp2 = runtime.safeCall(vis.add(cont)); + tmp5 = runtime.safeCall(vis.add(cont)); } - tmp3 = result + ") -> "; - tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp3 + tmp4 + tmp6 = result + ") -> "; + tmp7 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp6 + tmp7 } else { - let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -689,14 +679,13 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ + let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; /** scoped **/ if (cont instanceof Runtime.HandlerContFrame.class) { - let scrut, tmp4, tmp5, tmp6; /** scoped **/ result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut1, tmp7, tmp8; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { + let scrut3, tmp7, tmp8; /** scoped **/ + scrut3 = runtime.safeCall(m.has(cont)); + if (scrut3 === true) { tmp7 = ", " + marker; tmp8 = result + tmp7; result = tmp8; @@ -708,26 +697,24 @@ globalThis.Object.freeze(class Runtime { tmp = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; /** scoped **/ - tmp4 = reps + 1; - reps = tmp4; + tmp1 = reps + 1; + reps = tmp1; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp5 = runtime.Unit; + tmp2 = runtime.Unit; } - tmp6 = result + ", REPEAT"; - result = tmp6; - tmp1 = runtime.Unit; + tmp3 = result + ", REPEAT"; + result = tmp3; + tmp4 = runtime.Unit; } else { - tmp1 = runtime.safeCall(vis.add(cont)); + tmp4 = runtime.safeCall(vis.add(cont)); } - tmp2 = result + " -> "; - tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp2 + tmp3 + tmp5 = result + " -> "; + tmp6 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp5 + tmp6 } else { - let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -751,9 +738,8 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; /** scoped **/ if (contTrace instanceof Runtime.ContTrace.class) { - let scrut, scrut1; /** scoped **/ tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; if (scrut === true) { @@ -782,23 +768,22 @@ globalThis.Object.freeze(class Runtime { tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); cur = contTrace.nextHandler; - tmp13: while (true) { - let scrut2, tmp14, tmp15; /** scoped **/ + tmp15: while (true) { scrut2 = cur !== null; if (scrut2 === true) { - tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); - tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); + tmp11 = Runtime.showHandlerContChain(cur, hl, vis, 0); + tmp12 = runtime.safeCall(globalThis.console.log(tmp11)); cur = cur.nextHandler; - tmp11 = runtime.Unit; - continue tmp13 + tmp13 = runtime.Unit; + continue tmp15 } else { - tmp11 = runtime.Unit; + tmp13 = runtime.Unit; } break; } return runtime.safeCall(globalThis.console.log()) } else { - tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); + tmp14 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); return runtime.safeCall(globalThis.console.log(contTrace)) } } @@ -840,39 +825,36 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let tmp; /** scoped **/ - tmp1: while (true) { - let nxt, tmp2; /** scoped **/ + let nxt, scrut, tmp, tmp1; /** scoped **/ + tmp2: while (true) { if (cur instanceof Runtime.EffectSig.class) { - let scrut; /** scoped **/ nxt = Runtime.handleEffect(cur); scrut = cur === nxt; if (scrut === true) { return cur } else { cur = nxt; - tmp2 = runtime.Unit; + tmp = runtime.Unit; } - tmp = tmp2; - continue tmp1 + tmp1 = tmp; + continue tmp2 } else { return cur } break; } - return tmp + return tmp1 } static handleEffect(cur) { - let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + let prevHandlerFrame, scrut, scrut1, scrut2, handlerFrame, saved, scrut3, scrut4, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ prevHandlerFrame = cur.contTrace; tmp6: while (true) { - let scrut1, scrut2; /** scoped **/ split_root$: { split_1$: { - scrut1 = prevHandlerFrame.nextHandler !== null; - if (scrut1 === true) { - scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; - if (scrut2 === true) { + scrut = prevHandlerFrame.nextHandler !== null; + if (scrut === true) { + scrut1 = prevHandlerFrame.nextHandler.handler !== cur.handler; + if (scrut1 === true) { prevHandlerFrame = prevHandlerFrame.nextHandler; tmp = runtime.Unit; continue tmp6 @@ -887,8 +869,8 @@ globalThis.Object.freeze(class Runtime { } break; } - scrut = prevHandlerFrame.nextHandler === null; - if (scrut === true) { + scrut2 = prevHandlerFrame.nextHandler === null; + if (scrut2 === true) { return cur } else { tmp1 = runtime.Unit; @@ -903,7 +885,6 @@ globalThis.Object.freeze(class Runtime { tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); cur = tmp3; if (cur instanceof Runtime.EffectSig.class) { - let scrut3, scrut4; /** scoped **/ scrut3 = saved.next !== null; if (scrut3 === true) { cur.contTrace.last.next = saved.next; @@ -940,55 +921,52 @@ globalThis.Object.freeze(class Runtime { } } static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, tmp; /** scoped **/ + let cont, handlerCont, curDepth, scrut, scrut1, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ cont = contTrace.next; handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; - tmp1: while (true) { - let tmp2, tmp3; /** scoped **/ + tmp5: while (true) { if (cont instanceof Runtime.FunctionContFrame.class) { - let tmp4, tmp5; /** scoped **/ - tmp2 = runtime.safeCall(cont.resume(value)); - value = tmp2; + tmp = runtime.safeCall(cont.resume(value)); + value = tmp; Runtime.stackDepth = curDepth; if (value instanceof Runtime.EffectSig.class) { - let scrut, scrut1; /** scoped **/ value.contTrace.last.next = cont.next; value.contTrace.lastHandler.nextHandler = handlerCont; scrut = contTrace.last !== cont; if (scrut === true) { value.contTrace.last = contTrace.last; - tmp4 = runtime.Unit; + tmp1 = runtime.Unit; } else { - tmp4 = runtime.Unit; + tmp1 = runtime.Unit; } scrut1 = handlerCont !== null; if (scrut1 === true) { value.contTrace.lastHandler = contTrace.lastHandler; - tmp5 = runtime.Unit; + tmp2 = runtime.Unit; } else { - tmp5 = runtime.Unit; + tmp2 = runtime.Unit; } return value } else { cont = cont.next; tmp3 = runtime.Unit; } - tmp = tmp3; - continue tmp1 + tmp4 = tmp3; + continue tmp5 } else { if (handlerCont instanceof Runtime.HandlerContFrame.class) { cont = handlerCont.next; handlerCont = handlerCont.nextHandler; - tmp = runtime.Unit; - continue tmp1 + tmp4 = runtime.Unit; + continue tmp5 } else { return value } } break; } - return tmp + return tmp4 } static checkDepth() { let scrut, tmp, lambda; /** scoped **/ @@ -1004,25 +982,24 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, tmp; /** scoped **/ + let result, scrut, saved, tmp, tmp1; /** scoped **/ Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); Runtime.stackDepth = 1; - tmp1: while (true) { - let scrut, saved, tmp2; /** scoped **/ + tmp2: while (true) { scrut = Runtime.stackResume !== null; if (scrut === true) { saved = Runtime.stackResume; Runtime.stackResume = null; - tmp2 = runtime.safeCall(saved()); - result = tmp2; + tmp = runtime.safeCall(saved()); + result = tmp; Runtime.stackDepth = 1; - tmp = runtime.Unit; - continue tmp1 + tmp1 = runtime.Unit; + continue tmp2 } else { - tmp = runtime.Unit; + tmp1 = runtime.Unit; } break; } diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 41a5aa52ba..727374f250 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -107,31 +107,29 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let tmp1, x, tmp2, tmp3; /** scoped **/ +//│ let tmp1, tmp2, tmp3, x, tmp4; /** scoped **/ +//│ tmp4 = 3; //│ if (a instanceof A.class) { //│ tmp1 = 1; //│ } else if (a instanceof B.class) { //│ tmp1 = 2; //│ } else if (a instanceof C.class) { //│ tmp1 = 3; -//│ } else { -//│ let tmp4; /** scoped **/ -//│ tmp3 = 3; -//│ if (a instanceof D.class) { -//│ if (a instanceof A.class) { -//│ tmp4 = 1 + tmp3; -//│ } else if (a instanceof B.class) { -//│ tmp4 = 2 + tmp3; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ tmp2 = tmp4; -//│ } else if (a instanceof E.class) { -//│ tmp2 = 5; +//│ } else if (a instanceof D.class) { +//│ if (a instanceof A.class) { +//│ tmp2 = 1 + tmp4; +//│ } else if (a instanceof B.class) { +//│ tmp2 = 2 + tmp4; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } +//│ tmp3 = tmp2; +//│ tmp1 = Predef.print("done"); +//│ } else if (a instanceof E.class) { +//│ tmp3 = 5; //│ tmp1 = Predef.print("done"); +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ x = tmp1; //│ Predef.print(x) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 86786c79dc..53b3ab6b79 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -390,3 +390,37 @@ module M with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); + + + + +abstract class Effect with + fun perform(arg: Str): Str + + + +:effectHandlers +fun f(x) = + if x then + if x then + let z = 4 + handle h = Effect with + fun perform(x)(k) = + print(x) + k(x + z) + print(h.perform(32)) + else 0 + + +:dontRewriteWhile +:effectHandlers +fun f(x) = + while x do + while x do + let z = 4 + handle h = Effect with + fun perform(x)(k) = + print(x) + k(x + z) + print(h.perform(32)) + else 0 diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 0bd1b3914f..2d36117f61 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,10 +156,9 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let tmp20; /** scoped **/ +//│ let f, tmp20, scrut; /** scoped **/ //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { -//│ let f, scrut; /** scoped **/ //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; //│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 0b0f5ad043..87eb91c7eb 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let element0$, ls, a, middleElements; /** scoped **/ +//│ let ls, a, middleElements, element0$; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); From 1948288e24f2b8af3bfbdf89cf95cb54d70f54bc Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 2 Dec 2025 03:26:34 +0800 Subject: [PATCH 44/72] cleanup --- .../src/main/scala/hkmc2/semantics/Split.scala | 1 - .../scala/hkmc2/semantics/ucs/Normalization.scala | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 03ab1ba2d5..695e40453b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,7 +24,6 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End - inline def ~:(head: Branch): Split = Split.Cons(head, this) def mkClone(using State): Split = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index c43523df81..25a06a2354 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -395,17 +395,10 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e case N => innerBlock // If there are shared consequents, we need a wrap the entire block in a // `Label` so that `Break`s in the shared consequents can jump to the end. - val body = - val possiblyScoped = - // lowering.possiblyScoped( - // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - // mainBlock) - mainBlock - // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - Scoped( - if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty/* ++ inputSplit.definedSyms */, - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - ) + val body = Scoped( + if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty, + if labels.isEmpty then mainBlock else + Label(rootBreakLabel, false, mainBlock, End())) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = From 4c7904e0adcda1375e875ce477ab7cc1a9002f57 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 2 Dec 2025 13:17:00 +0800 Subject: [PATCH 45/72] Revert "cleanup" This reverts commit 1948288e24f2b8af3bfbdf89cf95cb54d70f54bc. --- .../src/main/scala/hkmc2/semantics/Split.scala | 1 + .../scala/hkmc2/semantics/ucs/Normalization.scala | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 695e40453b..03ab1ba2d5 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,6 +24,7 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End + inline def ~:(head: Branch): Split = Split.Cons(head, this) def mkClone(using State): Split = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 25a06a2354..c43523df81 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -395,10 +395,17 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e case N => innerBlock // If there are shared consequents, we need a wrap the entire block in a // `Label` so that `Break`s in the shared consequents can jump to the end. - val body = Scoped( - if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty, - if labels.isEmpty then mainBlock else - Label(rootBreakLabel, false, mainBlock, End())) + val body = + val possiblyScoped = + // lowering.possiblyScoped( + // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + // mainBlock) + mainBlock + // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + Scoped( + if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty/* ++ inputSplit.definedSyms */, + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + ) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = From 5ce54b6e918053a80b219eaf13380fe376333df8 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 2 Dec 2025 13:17:02 +0800 Subject: [PATCH 46/72] Revert "fix problems related to handlers in nested `if`/`while`s:" This reverts commit 09ea124f8d8b41078f8306b09f14b49158627267. --- .../main/scala/hkmc2/codegen/Lowering.scala | 2 +- .../hkmc2/semantics/ucs/Normalization.scala | 238 +++++++------- .../src/test/mlscript-compile/Predef.mjs | 21 +- .../src/test/mlscript-compile/Runtime.mjs | 309 ++++++++++-------- .../test/mlscript/codegen/MergeMatchArms.mls | 28 +- .../src/test/mlscript/codegen/Scoped.mls | 34 -- .../src/test/mlscript/handlers/Effects.mls | 3 +- .../test/mlscript/std/FingerTreeListTest.mls | 2 +- 8 files changed, 314 insertions(+), 323 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 303d88b5dc..ab93190a8d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -265,7 +265,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val mtds = methods.map: case (sym, params, split) => val paramLists = params :: Nil - val bodyBlock = inScopedBlock(ucs.Normalization(this)(split)(Ret)) + val bodyBlock = ucs.Normalization(this)(split)(Ret) FunDefn.withFreshSymbol(N, sym, paramLists, bodyBlock)(isTailRec = false) // The return type is intended to be consistent with `gatherMembers` (mtds, Nil, Nil, End()) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index c43523df81..f4db72a690 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -318,129 +318,127 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e def apply(split: Split)(k: Result => Block)(using Config, LoweringCtx): Block = this(split, `if`, N, k) - private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = - val nestScope = config.rewriteWhileLoops && kw === `while` - (if nestScope then LoweringCtx.nestScoped else outerCtx).givenIn: - var usesResTmp = false - // The symbol of the temporary variable for the result of the `if`-like term. - // It will be created in one of the following situations. - // 1. The continuation `k` is not a tail operation. - // 2. There are shared consequents in the `if`-like term. - // 3. The term is a `while` and the result is used. - lazy val l = - usesResTmp = true - val res = new TempSymbol(t) - outerCtx.collectScopedSym(res) - res - // The symbol for the loop label if the term is a `while`. - lazy val loopLabel = new TempSymbol(t) - lazy val f = - val res = new BlockMemberSymbol("while", Nil, false) - outerCtx.collectScopedSym(res) - res - val normalized = tl.scoped("ucs:normalize"): - normalize(inputSplit)(using VarSet()) - tl.scoped("ucs:normalized"): - tl.log(s"Normalized:\n${normalized.prettyPrint}") - // Collect consequents that are shared in more than one branch. - given labels: Labels = createLabelsForDuplicatedBranches(normalized) - lazy val rootBreakLabel = new TempSymbol(N, "split_root$") - lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) - lazy val assignResult = (r: Result) => Assign(l, r, End()) - lazy val loopCont = if config.rewriteWhileLoops - then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) - else Continue(loopLabel) - val cont = - if kw === `while` then - // If the term is a `while`, the action of `else` branches depends on - // whether the the enclosing split is at the top level or not. - R((topLevel: Bool) => (r: Result) => Assign(l, r, if topLevel then End() else loopCont)) - else if labels.isEmpty then - if k.isInstanceOf[TailOp] then - // If there are no shared consequents and the continuation is a tail - // operation, we can call it directly. - L(k) - else - // Otherwise, if the continuation is not a tail operation, we should - // save the result in a temporary variable and call the continuation - // in the end. - L(assignResult) + private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = LoweringCtx.nestScoped.givenIn: + var usesResTmp = false + // The symbol of the temporary variable for the result of the `if`-like term. + // It will be created in one of the following situations. + // 1. The continuation `k` is not a tail operation. + // 2. There are shared consequents in the `if`-like term. + // 3. The term is a `while` and the result is used. + lazy val l = + usesResTmp = true + val res = new TempSymbol(t) + outerCtx.collectScopedSym(res) + res + // The symbol for the loop label if the term is a `while`. + lazy val loopLabel = new TempSymbol(t) + lazy val f = + val res = new BlockMemberSymbol("while", Nil, false) + outerCtx.collectScopedSym(res) + res + val normalized = tl.scoped("ucs:normalize"): + normalize(inputSplit)(using VarSet()) + tl.scoped("ucs:normalized"): + tl.log(s"Normalized:\n${normalized.prettyPrint}") + // Collect consequents that are shared in more than one branch. + given labels: Labels = createLabelsForDuplicatedBranches(normalized) + lazy val rootBreakLabel = new TempSymbol(N, "split_root$") + lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) + lazy val assignResult = (r: Result) => Assign(l, r, End()) + lazy val loopCont = if config.rewriteWhileLoops + then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) + else Continue(loopLabel) + val cont = + if kw === `while` then + // If the term is a `while`, the action of `else` branches depends on + // whether the the enclosing split is at the top level or not. + R((topLevel: Bool) => (r: Result) => Assign(l, r, if topLevel then End() else loopCont)) + else if labels.isEmpty then + if k.isInstanceOf[TailOp] then + // If there are no shared consequents and the continuation is a tail + // operation, we can call it directly. + L(k) else - // When there are shared consequents, we are forced to save the result - // in the temporary variable nevertheless. Note that `cont` only gets - // called for non-shared consequents, so we should break to the end of - // the entire split after the assignment. - L(breakRoot) - // The main block contains the lowered split, where each shared consequent - // is replaced with a `Break` to the corresponding label. - val mainBlock = - val innermostBlock = lowerSplit(normalized, cont, topLevel = true) - // Wrap the main block in a labelled block for each shared consequent. The - // `rest` of each `Label` is the lowered consequent plus a `Break` to the - // end of the entire `if` term. Otherwise, it will fall through to the outer - // consequent, which is the wrong semantics. - val innerBlock: Block = labels.consequents match - case Nil => innermostBlock - case all @ (head :: tail) => - def wrap(consequents: Ls[(Term, TempSymbol)]): Block = - consequents.foldRight(innermostBlock): - case ((term, label), innerBlock) => - Label(label, false, innerBlock, term_nonTail(term)(breakRoot)) - // There is no need to generate `break` for the outermost split. - if labels.default.isEmpty then - Label(head._2, false, wrap(tail), term_nonTail(head._1)(assignResult)) - else wrap(all) - labels.default match - case S(label) => Label(label, false, innerBlock, throwMatchErrorBlock) - case N => innerBlock - // If there are shared consequents, we need a wrap the entire block in a - // `Label` so that `Break`s in the shared consequents can jump to the end. - val body = - val possiblyScoped = - // lowering.possiblyScoped( - // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - // mainBlock) - mainBlock - // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - Scoped( - if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty/* ++ inputSplit.definedSyms */, - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - ) - // Embed the `body` into `Label` if the term is a `while`. - lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) - val block = - if kw === `while` then - if config.rewriteWhileLoops then - val loopResult = TempSymbol(N) - val isReturned = TempSymbol(N) - outerCtx.collectScopedSym(loopResult) - outerCtx.collectScopedSym(isReturned) - val loopEnd: Path = - Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) - val blk = blockBuilder - .assign(l, Value.Lit(Tree.UnitLit(false))) - .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) - .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) - if summon[LoweringCtx].mayRet then - blk - .assign(isReturned, Call(Value.Ref(State.builtinOpsMap("!==")), - loopResult.asPath.asArg :: loopEnd.asArg :: Nil)(true, false, false)) - .ifthen(Value.Ref(isReturned), Case.Lit(Tree.BoolLit(true)), - Return(Value.Ref(loopResult), false), - S(rest) - ) - .end - else - blk.rest(rest) + // Otherwise, if the continuation is not a tail operation, we should + // save the result in a temporary variable and call the continuation + // in the end. + L(assignResult) + else + // When there are shared consequents, we are forced to save the result + // in the temporary variable nevertheless. Note that `cont` only gets + // called for non-shared consequents, so we should break to the end of + // the entire split after the assignment. + L(breakRoot) + // The main block contains the lowered split, where each shared consequent + // is replaced with a `Break` to the corresponding label. + val mainBlock = + val innermostBlock = lowerSplit(normalized, cont, topLevel = true) + // Wrap the main block in a labelled block for each shared consequent. The + // `rest` of each `Label` is the lowered consequent plus a `Break` to the + // end of the entire `if` term. Otherwise, it will fall through to the outer + // consequent, which is the wrong semantics. + val innerBlock: Block = labels.consequents match + case Nil => innermostBlock + case all @ (head :: tail) => + def wrap(consequents: Ls[(Term, TempSymbol)]): Block = + consequents.foldRight(innermostBlock): + case ((term, label), innerBlock) => + Label(label, false, innerBlock, term_nonTail(term)(breakRoot)) + // There is no need to generate `break` for the outermost split. + if labels.default.isEmpty then + Label(head._2, false, wrap(tail), term_nonTail(head._1)(assignResult)) + else wrap(all) + labels.default match + case S(label) => Label(label, false, innerBlock, throwMatchErrorBlock) + case N => innerBlock + // If there are shared consequents, we need a wrap the entire block in a + // `Label` so that `Break`s in the shared consequents can jump to the end. + val body = + val possiblyScoped = + // lowering.possiblyScoped( + // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, + // mainBlock) + mainBlock + // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + Scoped( + LoweringCtx.loweringCtx.getCollectedSym/* ++ inputSplit.definedSyms */, + if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) + ) + // Embed the `body` into `Label` if the term is a `while`. + lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) + val block = + if kw === `while` then + if config.rewriteWhileLoops then + val loopResult = TempSymbol(N) + val isReturned = TempSymbol(N) + outerCtx.collectScopedSym(loopResult) + outerCtx.collectScopedSym(isReturned) + val loopEnd: Path = + Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) + val blk = blockBuilder + .assign(l, Value.Lit(Tree.UnitLit(false))) + .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) + .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) + if summon[LoweringCtx].mayRet then + blk + .assign(isReturned, Call(Value.Ref(State.builtinOpsMap("!==")), + loopResult.asPath.asArg :: loopEnd.asArg :: Nil)(true, false, false)) + .ifthen(Value.Ref(isReturned), Case.Lit(Tree.BoolLit(true)), + Return(Value.Ref(loopResult), false), + S(rest) + ) + .end else - Begin(Label(loopLabel, true, body, End()), rest) - else if labels.isEmpty && k.isInstanceOf[TailOp] then - body + blk.rest(rest) else - Begin(body, rest) - scoped("ucs:lowered"): - log(s"Lowered:\n${block.showAsTree}") - block + Begin(Label(loopLabel, true, body, End()), rest) + else if labels.isEmpty && k.isInstanceOf[TailOp] then + body + else + Begin(body, rest) + scoped("ucs:lowered"): + log(s"Lowered:\n${block.showAsTree}") + block end Normalization object Normalization: diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index 6c203b33e1..e7f4e8a4fa 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -124,7 +124,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, scrut1, tmp, tmp1, tmp2, tmp3; /** scoped **/ + let len, scrut, i, init, tmp; /** scoped **/ len = rest.length; scrut = len == 0; if (scrut === true) { @@ -132,18 +132,19 @@ globalThis.Object.freeze(class Predef { } else { i = len - 1; init = runtime.safeCall(rest.at(i)); - tmp4: while (true) { + tmp1: while (true) { + let scrut1, tmp2, tmp3, tmp4; /** scoped **/ scrut1 = i > 0; if (scrut1 === true) { - tmp = i - 1; - i = tmp; - tmp1 = runtime.safeCall(rest.at(i)); - tmp2 = runtime.safeCall(f(tmp1, init)); - init = tmp2; - tmp3 = runtime.Unit; - continue tmp4 + tmp2 = i - 1; + i = tmp2; + tmp3 = runtime.safeCall(rest.at(i)); + tmp4 = runtime.safeCall(f(tmp3, init)); + init = tmp4; + tmp = runtime.Unit; + continue tmp1 } else { - tmp3 = runtime.Unit; + tmp = runtime.Unit; } break; } diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 6708dba415..a2cb048716 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -427,7 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, scrut1, scrut2, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -437,34 +437,35 @@ globalThis.Object.freeze(class Runtime { }); scrut = runtime.short_or(tmp, lambda); if (scrut === true) { + let scrut1, scrut2, tmp12; /** scoped **/ scrut1 = functionName.length > 0; if (scrut1 === true) { - tmp1 = " '" + functionName; - tmp2 = tmp1 + "'"; + tmp12 = " '" + functionName; + tmp1 = tmp12 + "'"; } else { - tmp2 = ""; + tmp1 = ""; } - name = tmp2; - tmp3 = "Function" + name; - tmp4 = tmp3 + " expected "; + name = tmp1; + tmp2 = "Function" + name; + tmp3 = tmp2 + " expected "; if (isUB === true) { - tmp5 = ""; + tmp4 = ""; } else { - tmp5 = "at least "; + tmp4 = "at least "; } - tmp6 = tmp4 + tmp5; - tmp7 = tmp6 + expected; - tmp8 = tmp7 + " argument"; + tmp5 = tmp3 + tmp4; + tmp6 = tmp5 + expected; + tmp7 = tmp6 + " argument"; scrut2 = expected === 1; if (scrut2 === true) { - tmp9 = ""; + tmp8 = ""; } else { - tmp9 = "s"; + tmp8 = "s"; } - tmp10 = tmp8 + tmp9; - tmp11 = tmp10 + " but got "; - tmp12 = tmp11 + got; - throw globalThis.Error(tmp12) + tmp9 = tmp7 + tmp8; + tmp10 = tmp9 + " but got "; + tmp11 = tmp10 + got; + throw globalThis.Error(tmp11) } else { return runtime.Unit } @@ -513,66 +514,72 @@ globalThis.Object.freeze(class Runtime { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } static topLevelEffect(tr, debug) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ - tmp6: while (true) { + let tmp, tmp1; /** scoped **/ + tmp2: while (true) { + let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ scrut = tr.handler === Runtime.PrintStackEffect; if (scrut === true) { - tmp = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); - tmp1 = runtime.safeCall(globalThis.console.log(tmp)); - tmp2 = Runtime.resume(tr.contTrace); - tmp3 = runtime.safeCall(tmp2(runtime.Unit)); - tr = tmp3; - tmp4 = runtime.Unit; - continue tmp6 + tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); + tmp4 = runtime.safeCall(globalThis.console.log(tmp3)); + tmp5 = Runtime.resume(tr.contTrace); + tmp6 = runtime.safeCall(tmp5(runtime.Unit)); + tr = tmp6; + tmp = runtime.Unit; + continue tmp2 } else { - tmp4 = runtime.Unit; + tmp = runtime.Unit; } break; } if (tr instanceof Runtime.EffectSig.class) { - tmp5 = "Error: Unhandled effect " + tr.handler.constructor.name; - throw Runtime.showStackTrace(tmp5, tr, debug, false) + tmp1 = "Error: Unhandled effect " + tr.handler.constructor.name; + throw Runtime.showStackTrace(tmp1, tr, debug, false) } else { return tr } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, scrut, cur, scrut1, locals, curLocals, loc, loc1, localsMsg, scrut2, scrut3, tmp, tmp1, lambda, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { - tmp19: while (true) { + let tmp3; /** scoped **/ + tmp4: while (true) { + let scrut, cur, tmp5, tmp6; /** scoped **/ scrut = curHandler !== null; if (scrut === true) { + let scrut1, tmp7, tmp8; /** scoped **/ cur = curHandler.next; - tmp20: while (true) { - scrut1 = cur !== null; - if (scrut1 === true) { + tmp9: while (true) { + let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + scrut2 = cur !== null; + if (scrut2 === true) { + let scrut3, lambda, tmp19, tmp20; /** scoped **/ locals = cur.getLocals; - tmp = locals.length - 1; - curLocals = runtime.safeCall(locals.at(tmp)); + tmp10 = locals.length - 1; + curLocals = runtime.safeCall(locals.at(tmp10)); loc = cur.getLoc; if (loc === null) { - tmp1 = "pc=" + cur.pc; + tmp11 = "pc=" + cur.pc; } else { - tmp1 = loc; + tmp11 = loc; } - loc1 = tmp1; + loc1 = tmp11; split_root$: { split_1$: { if (showLocals === true) { - scrut2 = curLocals.locals.length > 0; - if (scrut2 === true) { + scrut3 = curLocals.locals.length > 0; + if (scrut3 === true) { lambda = (undefined, function (l) { let tmp21, tmp22; /** scoped **/ tmp21 = l.localName + "="; tmp22 = Rendering.render(l.value); return tmp21 + tmp22 }); - tmp2 = runtime.safeCall(curLocals.locals.map(lambda)); - tmp3 = runtime.safeCall(tmp2.join(", ")); - tmp4 = " with locals: " + tmp3; + tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); + tmp20 = runtime.safeCall(tmp19.join(", ")); + tmp12 = " with locals: " + tmp20; break split_root$ } else { break split_1$ @@ -581,66 +588,67 @@ globalThis.Object.freeze(class Runtime { break split_1$ } } - tmp4 = ""; + tmp12 = ""; } - localsMsg = tmp4; - tmp5 = "\n\tat " + curLocals.fnName; - tmp6 = tmp5 + " ("; - tmp7 = tmp6 + loc1; - tmp8 = tmp7 + ")"; - tmp9 = msg + tmp8; - msg = tmp9; - tmp10 = msg + localsMsg; - msg = tmp10; + localsMsg = tmp12; + tmp13 = "\n\tat " + curLocals.fnName; + tmp14 = tmp13 + " ("; + tmp15 = tmp14 + loc1; + tmp16 = tmp15 + ")"; + tmp17 = msg + tmp16; + msg = tmp17; + tmp18 = msg + localsMsg; + msg = tmp18; cur = cur.next; atTail = false; - tmp11 = runtime.Unit; - continue tmp20 + tmp5 = runtime.Unit; + continue tmp9 } else { - tmp11 = runtime.Unit; + tmp5 = runtime.Unit; } break; } curHandler = curHandler.nextHandler; - scrut3 = curHandler !== null; - if (scrut3 === true) { - tmp12 = "\n\twith handler " + curHandler.handler.constructor.name; - tmp13 = msg + tmp12; - msg = tmp13; + scrut1 = curHandler !== null; + if (scrut1 === true) { + tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; + tmp8 = msg + tmp7; + msg = tmp8; atTail = false; - tmp14 = runtime.Unit; + tmp6 = runtime.Unit; } else { - tmp14 = runtime.Unit; + tmp6 = runtime.Unit; } - tmp15 = tmp14; - continue tmp19 + tmp = tmp6; + continue tmp4 } else { - tmp15 = runtime.Unit; + tmp = runtime.Unit; } break; } if (atTail === true) { - tmp16 = msg + "\n\tat tail position"; - msg = tmp16; - tmp17 = runtime.Unit; + tmp3 = msg + "\n\tat tail position"; + msg = tmp3; + tmp1 = runtime.Unit; } else { - tmp17 = runtime.Unit; + tmp1 = runtime.Unit; } - tmp18 = tmp17; + tmp2 = tmp1; } else { - tmp18 = runtime.Unit; + tmp2 = runtime.Unit; } return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ + let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { + let scrut, tmp5, tmp6, tmp7; /** scoped **/ tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut3, tmp8, tmp9; /** scoped **/ - scrut3 = runtime.safeCall(m.has(cont)); - if (scrut3 === true) { + let scrut1, tmp8, tmp9; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { tmp8 = ", " + marker; tmp9 = result + tmp8; result = tmp9; @@ -652,24 +660,26 @@ globalThis.Object.freeze(class Runtime { tmp1 = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - tmp2 = reps + 1; - reps = tmp2; + let scrut1; /** scoped **/ + tmp5 = reps + 1; + reps = tmp5; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp3 = runtime.Unit; + tmp6 = runtime.Unit; } - tmp4 = result + ", REPEAT"; - result = tmp4; - tmp5 = runtime.Unit; + tmp7 = result + ", REPEAT"; + result = tmp7; + tmp2 = runtime.Unit; } else { - tmp5 = runtime.safeCall(vis.add(cont)); + tmp2 = runtime.safeCall(vis.add(cont)); } - tmp6 = result + ") -> "; - tmp7 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp6 + tmp7 + tmp3 = result + ") -> "; + tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp3 + tmp4 } else { + let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -679,13 +689,14 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ if (cont instanceof Runtime.HandlerContFrame.class) { + let scrut, tmp4, tmp5, tmp6; /** scoped **/ result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut3, tmp7, tmp8; /** scoped **/ - scrut3 = runtime.safeCall(m.has(cont)); - if (scrut3 === true) { + let scrut1, tmp7, tmp8; /** scoped **/ + scrut1 = runtime.safeCall(m.has(cont)); + if (scrut1 === true) { tmp7 = ", " + marker; tmp8 = result + tmp7; result = tmp8; @@ -697,24 +708,26 @@ globalThis.Object.freeze(class Runtime { tmp = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - tmp1 = reps + 1; - reps = tmp1; + let scrut1; /** scoped **/ + tmp4 = reps + 1; + reps = tmp4; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp2 = runtime.Unit; + tmp5 = runtime.Unit; } - tmp3 = result + ", REPEAT"; - result = tmp3; - tmp4 = runtime.Unit; + tmp6 = result + ", REPEAT"; + result = tmp6; + tmp1 = runtime.Unit; } else { - tmp4 = runtime.safeCall(vis.add(cont)); + tmp1 = runtime.safeCall(vis.add(cont)); } - tmp5 = result + " -> "; - tmp6 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp5 + tmp6 + tmp2 = result + " -> "; + tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp2 + tmp3 } else { + let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -738,8 +751,9 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let scrut, scrut1, vis, hl, cur, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14; /** scoped **/ + let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ if (contTrace instanceof Runtime.ContTrace.class) { + let scrut, scrut1; /** scoped **/ tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; if (scrut === true) { @@ -768,22 +782,23 @@ globalThis.Object.freeze(class Runtime { tmp9 = Runtime.showFunctionContChain(contTrace.next, hl, vis, 0); tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); cur = contTrace.nextHandler; - tmp15: while (true) { + tmp13: while (true) { + let scrut2, tmp14, tmp15; /** scoped **/ scrut2 = cur !== null; if (scrut2 === true) { - tmp11 = Runtime.showHandlerContChain(cur, hl, vis, 0); - tmp12 = runtime.safeCall(globalThis.console.log(tmp11)); + tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); + tmp15 = runtime.safeCall(globalThis.console.log(tmp14)); cur = cur.nextHandler; - tmp13 = runtime.Unit; - continue tmp15 + tmp11 = runtime.Unit; + continue tmp13 } else { - tmp13 = runtime.Unit; + tmp11 = runtime.Unit; } break; } return runtime.safeCall(globalThis.console.log()) } else { - tmp14 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); + tmp12 = runtime.safeCall(globalThis.console.log("Not a cont trace:")); return runtime.safeCall(globalThis.console.log(contTrace)) } } @@ -825,36 +840,39 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let nxt, scrut, tmp, tmp1; /** scoped **/ - tmp2: while (true) { + let tmp; /** scoped **/ + tmp1: while (true) { + let nxt, tmp2; /** scoped **/ if (cur instanceof Runtime.EffectSig.class) { + let scrut; /** scoped **/ nxt = Runtime.handleEffect(cur); scrut = cur === nxt; if (scrut === true) { return cur } else { cur = nxt; - tmp = runtime.Unit; + tmp2 = runtime.Unit; } - tmp1 = tmp; - continue tmp2 + tmp = tmp2; + continue tmp1 } else { return cur } break; } - return tmp1 + return tmp } static handleEffect(cur) { - let prevHandlerFrame, scrut, scrut1, scrut2, handlerFrame, saved, scrut3, scrut4, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ prevHandlerFrame = cur.contTrace; tmp6: while (true) { + let scrut1, scrut2; /** scoped **/ split_root$: { split_1$: { - scrut = prevHandlerFrame.nextHandler !== null; - if (scrut === true) { - scrut1 = prevHandlerFrame.nextHandler.handler !== cur.handler; - if (scrut1 === true) { + scrut1 = prevHandlerFrame.nextHandler !== null; + if (scrut1 === true) { + scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; + if (scrut2 === true) { prevHandlerFrame = prevHandlerFrame.nextHandler; tmp = runtime.Unit; continue tmp6 @@ -869,8 +887,8 @@ globalThis.Object.freeze(class Runtime { } break; } - scrut2 = prevHandlerFrame.nextHandler === null; - if (scrut2 === true) { + scrut = prevHandlerFrame.nextHandler === null; + if (scrut === true) { return cur } else { tmp1 = runtime.Unit; @@ -885,6 +903,7 @@ globalThis.Object.freeze(class Runtime { tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); cur = tmp3; if (cur instanceof Runtime.EffectSig.class) { + let scrut3, scrut4; /** scoped **/ scrut3 = saved.next !== null; if (scrut3 === true) { cur.contTrace.last.next = saved.next; @@ -921,52 +940,55 @@ globalThis.Object.freeze(class Runtime { } } static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, scrut, scrut1, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + let cont, handlerCont, curDepth, tmp; /** scoped **/ cont = contTrace.next; handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; - tmp5: while (true) { + tmp1: while (true) { + let tmp2, tmp3; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - tmp = runtime.safeCall(cont.resume(value)); - value = tmp; + let tmp4, tmp5; /** scoped **/ + tmp2 = runtime.safeCall(cont.resume(value)); + value = tmp2; Runtime.stackDepth = curDepth; if (value instanceof Runtime.EffectSig.class) { + let scrut, scrut1; /** scoped **/ value.contTrace.last.next = cont.next; value.contTrace.lastHandler.nextHandler = handlerCont; scrut = contTrace.last !== cont; if (scrut === true) { value.contTrace.last = contTrace.last; - tmp1 = runtime.Unit; + tmp4 = runtime.Unit; } else { - tmp1 = runtime.Unit; + tmp4 = runtime.Unit; } scrut1 = handlerCont !== null; if (scrut1 === true) { value.contTrace.lastHandler = contTrace.lastHandler; - tmp2 = runtime.Unit; + tmp5 = runtime.Unit; } else { - tmp2 = runtime.Unit; + tmp5 = runtime.Unit; } return value } else { cont = cont.next; tmp3 = runtime.Unit; } - tmp4 = tmp3; - continue tmp5 + tmp = tmp3; + continue tmp1 } else { if (handlerCont instanceof Runtime.HandlerContFrame.class) { cont = handlerCont.next; handlerCont = handlerCont.nextHandler; - tmp4 = runtime.Unit; - continue tmp5 + tmp = runtime.Unit; + continue tmp1 } else { return value } } break; } - return tmp4 + return tmp } static checkDepth() { let scrut, tmp, lambda; /** scoped **/ @@ -982,24 +1004,25 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, scrut, saved, tmp, tmp1; /** scoped **/ + let result, tmp; /** scoped **/ Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); Runtime.stackDepth = 1; - tmp2: while (true) { + tmp1: while (true) { + let scrut, saved, tmp2; /** scoped **/ scrut = Runtime.stackResume !== null; if (scrut === true) { saved = Runtime.stackResume; Runtime.stackResume = null; - tmp = runtime.safeCall(saved()); - result = tmp; + tmp2 = runtime.safeCall(saved()); + result = tmp2; Runtime.stackDepth = 1; - tmp1 = runtime.Unit; - continue tmp2 + tmp = runtime.Unit; + continue tmp1 } else { - tmp1 = runtime.Unit; + tmp = runtime.Unit; } break; } diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 727374f250..41a5aa52ba 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -107,29 +107,31 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let tmp1, tmp2, tmp3, x, tmp4; /** scoped **/ -//│ tmp4 = 3; +//│ let tmp1, x, tmp2, tmp3; /** scoped **/ //│ if (a instanceof A.class) { //│ tmp1 = 1; //│ } else if (a instanceof B.class) { //│ tmp1 = 2; //│ } else if (a instanceof C.class) { //│ tmp1 = 3; -//│ } else if (a instanceof D.class) { -//│ if (a instanceof A.class) { -//│ tmp2 = 1 + tmp4; -//│ } else if (a instanceof B.class) { -//│ tmp2 = 2 + tmp4; +//│ } else { +//│ let tmp4; /** scoped **/ +//│ tmp3 = 3; +//│ if (a instanceof D.class) { +//│ if (a instanceof A.class) { +//│ tmp4 = 1 + tmp3; +//│ } else if (a instanceof B.class) { +//│ tmp4 = 2 + tmp3; +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ tmp2 = tmp4; +//│ } else if (a instanceof E.class) { +//│ tmp2 = 5; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp3 = tmp2; -//│ tmp1 = Predef.print("done"); -//│ } else if (a instanceof E.class) { -//│ tmp3 = 5; //│ tmp1 = Predef.print("done"); -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ x = tmp1; //│ Predef.print(x) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 53b3ab6b79..86786c79dc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -390,37 +390,3 @@ module M with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); - - - - -abstract class Effect with - fun perform(arg: Str): Str - - - -:effectHandlers -fun f(x) = - if x then - if x then - let z = 4 - handle h = Effect with - fun perform(x)(k) = - print(x) - k(x + z) - print(h.perform(32)) - else 0 - - -:dontRewriteWhile -:effectHandlers -fun f(x) = - while x do - while x do - let z = 4 - handle h = Effect with - fun perform(x)(k) = - print(x) - k(x + z) - print(h.perform(32)) - else 0 diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 2d36117f61..0bd1b3914f 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,9 +156,10 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let f, tmp20, scrut; /** scoped **/ +//│ let tmp20; /** scoped **/ //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { +//│ let f, scrut; /** scoped **/ //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; //│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 87eb91c7eb..0b0f5ad043 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let ls, a, middleElements, element0$; /** scoped **/ +//│ let element0$, ls, a, middleElements; /** scoped **/ //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); From 10c2ae59fde8e462cb4a54f4174c5614f4fc86a7 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Tue, 2 Dec 2025 20:45:31 +0800 Subject: [PATCH 47/72] disable while loop rewriting; a temp fix --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../src/test/mlscript/codegen/Scoped.mls | 72 +++++------ .../src/test/mlscript/codegen/While.mls | 121 ++++++++---------- .../mlscript/handlers/UserThreadsSafe.mls | 10 +- .../mlscript/handlers/UserThreadsUnsafe.mls | 10 +- .../shared/src/test/mlscript/lifter/Loops.mls | 56 +++----- .../ucs/general/LogicalConnectives.mls | 26 ++-- .../src/test/scala/hkmc2/MLsDiffMaker.scala | 2 +- 8 files changed, 122 insertions(+), 177 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 22ec968088..1725e17be9 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -605,7 +605,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: reserveNames(p) lazy val imps = p.imports.map: i => doc"""${getVar(i._1, N)} = await import("${i._2.toString}").then(m => m.default ?? m);""" - blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVars.toSeq) -> + blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVarsNoScoped.toSeq) -> (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) def genLetDecls(vars: Iterator[(Symbol, Str)], isScoped: Bool): Document = diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 86786c79dc..b722f33ba5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -162,30 +162,26 @@ fun f(x) = //│ JS (unsanitized): //│ let f3; /** scoped **/ //│ f3 = function f(x1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = undefined; -//│ while1 = (undefined, function () { +//│ let tmp1; /** scoped **/ +//│ tmp2: while (true) { //│ let a, a1, argument0$, argument1$; /** scoped **/ //│ if (x1 instanceof A.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a = argument0$; //│ tmp1 = runtime.safeCall(a()); -//│ return while1() +//│ continue tmp2 //│ } else if (x1 instanceof B.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a1 = argument0$; //│ tmp1 = runtime.safeCall(a1()); -//│ return while1() -//│ } else { -//│ tmp1 = runtime.safeCall(x1()); -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { x1 = tmp1; return x1 } +//│ continue tmp2 +//│ } else { tmp1 = runtime.safeCall(x1()); } +//│ break; +//│ } +//│ x1 = tmp1; +//│ return x1 //│ }; @@ -202,11 +198,10 @@ fun f() = //│ JS (unsanitized): //│ let f4; /** scoped **/ //│ f4 = function f() { -//│ let y1, tmp1, while1, tmp2, tmp3; /** scoped **/ +//│ let y1, tmp1; /** scoped **/ //│ y1 = x + 1; -//│ tmp1 = undefined; -//│ while1 = (undefined, function () { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp4; /** scoped **/ +//│ tmp2: while (true) { +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp3; /** scoped **/ //│ split_root$: { //│ split_1$: { //│ if (y1 instanceof A.class) { @@ -218,28 +213,24 @@ fun f() = //│ if (scrut === true) { //│ scrut1 = true; //│ if (scrut1 === true) { -//│ tmp4 = z + 1; -//│ y1 = tmp4; +//│ tmp3 = z + 1; +//│ y1 = tmp3; //│ tmp1 = runtime.Unit; -//│ return while1() +//│ continue tmp2 //│ } else { //│ break split_1$ //│ } //│ } else { //│ break split_1$ //│ } -//│ } else { -//│ tmp1 = runtime.Unit; -//│ } +//│ } else { tmp1 = runtime.Unit; } //│ } //│ y1 = a; //│ tmp1 = runtime.Unit; //│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ break; +//│ } +//│ return tmp1 //│ }; @@ -250,26 +241,21 @@ fun f(x, y) = //│ JS (unsanitized): //│ let f5; /** scoped **/ //│ f5 = function f(x1, y1) { -//│ let tmp1, while1, tmp2, tmp3; /** scoped **/ -//│ tmp1 = undefined; -//│ while1 = (undefined, function () { -//│ let scrut, lambda, tmp4; /** scoped **/ +//│ let tmp1; /** scoped **/ +//│ tmp2: while (true) { +//│ let scrut, lambda, tmp3; /** scoped **/ //│ lambda = (undefined, function () { //│ return y1 //│ }); //│ scrut = runtime.short_and(x1, lambda); //│ if (scrut === true) { -//│ tmp4 = f5(0, 0); -//│ tmp1 = tmp4 + 4; -//│ return while1() -//│ } else { -//│ tmp1 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp2 = while1(); -//│ tmp3 = tmp2 !== runtime.LoopEnd; -//│ if (tmp3 === true) { return tmp2 } else { return tmp1 } +//│ tmp3 = f5(0, 0); +//│ tmp1 = tmp3 + 4; +//│ continue tmp2 +//│ } else { tmp1 = runtime.Unit; } +//│ break; +//│ } +//│ return tmp1 //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 91ebaefac6..ebee1cd3e2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -6,22 +6,19 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function () { -//│ let tmp, while1, tmp1, tmp2; /** scoped **/ -//│ tmp = undefined; -//│ while1 = (undefined, function () { +//│ let tmp; /** scoped **/ +//│ tmp1: while (true) { //│ let scrut; /** scoped **/ //│ scrut = true; //│ if (scrut === true) { //│ tmp = 0; -//│ return while1() +//│ continue tmp1 //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp1 = while1(); -//│ tmp2 = tmp1 !== runtime.LoopEnd; -//│ if (tmp2 === true) { return tmp1 } else { return tmp } +//│ break; +//│ } +//│ return tmp //│ }); //│ lambda //│ = fun @@ -57,20 +54,18 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let while3, tmp6, tmp7, tmp8; /** scoped **/ -//│ tmp8 = undefined; -//│ while3 = (undefined, function () { -//│ let tmp9; /** scoped **/ +//│ let tmp4; /** scoped **/ +//│ tmp5: while (true) { +//│ let tmp6; /** scoped **/ //│ if (x2 === true) { -//│ tmp9 = Predef.print("Hello World"); +//│ tmp6 = Predef.print("Hello World"); //│ x2 = false; -//│ tmp8 = runtime.Unit; -//│ return while3() -//│ } else { tmp8 = 42; } -//│ return runtime.LoopEnd -//│ }); -//│ tmp6 = while3(); -//│ tmp8 +//│ tmp4 = runtime.Unit; +//│ continue tmp5 +//│ } else { tmp4 = 42; } +//│ break; +//│ } +//│ tmp4 //│ > Hello World //│ = 42 @@ -112,25 +107,20 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let tmp18, while7, tmp19, tmp20; /** scoped **/ -//│ tmp18 = undefined; -//│ while7 = (undefined, function () { -//│ let i2, scrut, tmp21; /** scoped **/ +//│ let tmp19; /** scoped **/ +//│ tmp20: while (true) { +//│ let i2, scrut3, tmp21; /** scoped **/ //│ i2 = 0; -//│ scrut = i2 < 10; -//│ if (scrut === true) { +//│ scrut3 = i2 < 10; +//│ if (scrut3 === true) { //│ tmp21 = i2 + 1; //│ i2 = tmp21; -//│ tmp18 = runtime.Unit; -//│ return while7() -//│ } else { -//│ tmp18 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp19 = while7(); -//│ tmp20 = tmp19 !== runtime.LoopEnd; -//│ if (tmp20 === true) { return tmp19 } else { return tmp18 } +//│ tmp19 = runtime.Unit; +//│ continue tmp20 +//│ } else { tmp19 = runtime.Unit; } +//│ break; +//│ } +//│ return tmp19 //│ }); //│ lambda2 //│ = fun @@ -210,9 +200,8 @@ fun f(ls) = //│ JS (unsanitized): //│ let f; /** scoped **/ //│ f = function f(ls) { -//│ let tmp27, while10, tmp28, tmp29; /** scoped **/ -//│ tmp27 = undefined; -//│ while10 = (undefined, function () { +//│ let tmp28; /** scoped **/ +//│ tmp29: while (true) { //│ let tl, h, argument0$, argument1$; /** scoped **/ //│ if (ls instanceof Cons.class) { //│ argument0$ = ls.hd; @@ -220,16 +209,12 @@ fun f(ls) = //│ tl = argument1$; //│ h = argument0$; //│ ls = tl; -//│ tmp27 = Predef.print(h); -//│ return while10() -//│ } else { -//│ tmp27 = Predef.print("Done!"); -//│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp28 = while10(); -//│ tmp29 = tmp28 !== runtime.LoopEnd; -//│ if (tmp29 === true) { return tmp28 } else { return tmp27 } +//│ tmp28 = Predef.print(h); +//│ continue tmp29 +//│ } else { tmp28 = Predef.print("Done!"); } +//│ break; +//│ } +//│ return tmp28 //│ }; f(0) @@ -267,23 +252,19 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, while11, tmp39; /** scoped **/ -//│ tmp39 = undefined; -//│ while11 = (undefined, function () { +//│ let tmp37; /** scoped **/ +//│ tmp38: while (true) { //│ split_root$: { //│ split_1$: { //│ if (x3 instanceof Object) { //│ break split_1$ -//│ } else { -//│ break split_1$ -//│ } +//│ } else { break split_1$ } //│ } -//│ tmp39 = runtime.Unit; +//│ tmp37 = runtime.Unit; //│ } -//│ return runtime.LoopEnd -//│ }); -//│ tmp37 = while11(); -//│ tmp39 +//│ break; +//│ } +//│ tmp37 // ——— FIXME: ——— @@ -297,10 +278,10 @@ while print("Hello World"); false then 0(0) else 1 //│ ╔══[PARSE ERROR] Unexpected 'then' keyword here -//│ ║ l.297: then 0(0) +//│ ║ l.278: then 0(0) //│ ╙── ^^^^ //│ ╔══[ERROR] Unrecognized term split (false literal) -//│ ║ l.296: while print("Hello World"); false +//│ ║ l.277: while print("Hello World"); false //│ ╙── ^^^^^ //│ > Hello World //│ ═══[RUNTIME ERROR] Error: match error @@ -310,12 +291,12 @@ while { print("Hello World"), false } then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.309: while { print("Hello World"), false } +//│ ║ l.290: while { print("Hello World"), false } //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.310: then 0(0) +//│ ║ l.291: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.311: else 1 +//│ ║ l.292: else 1 //│ ╙── ^^^^ :fixme @@ -325,14 +306,14 @@ while then 0(0) else 1 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.323: print("Hello World") +//│ ║ l.304: print("Hello World") //│ ║ ^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.324: false +//│ ║ l.305: false //│ ║ ^^^^^^^^^ -//│ ║ l.325: then 0(0) +//│ ║ l.306: then 0(0) //│ ╙── ^^^^^^^^^^^ //│ ╔══[ERROR] Illegal position for prefix keyword 'else'. -//│ ║ l.326: else 1 +//│ ║ l.307: else 1 //│ ╙── ^^^^ diff --git a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls index 26059f2a32..c240949e57 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls @@ -44,8 +44,7 @@ in //│ > f 2 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$ //│ at f (UserThreadsSafe.mls:17:3) -//│ at while (UserThreadsSafe.mls:10:7) -//│ at drain (pc=6) +//│ at drain (UserThreadsSafe.mls:10:7) //│ at fork (UserThreadsSafe.mls:35:5) //│ at fork (UserThreadsSafe.mls:35:5) @@ -67,9 +66,8 @@ in //│ > f 0 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$1 //│ at f (UserThreadsSafe.mls:17:3) -//│ at while (UserThreadsSafe.mls:10:7) -//│ at drain (pc=6) -//│ at fork (UserThreadsSafe.mls:58:5) -//│ at fork (UserThreadsSafe.mls:58:5) +//│ at drain (UserThreadsSafe.mls:10:7) +//│ at fork (UserThreadsSafe.mls:57:5) +//│ at fork (UserThreadsSafe.mls:57:5) diff --git a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls index c22ef1eadf..402bfe41c3 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls @@ -47,8 +47,7 @@ in //│ > f 2 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$ //│ at f (UserThreadsUnsafe.mls:12:3) -//│ at while (UserThreadsUnsafe.mls:29:5) -//│ at drain (pc=6) +//│ at drain (UserThreadsUnsafe.mls:29:5) //│ at fork (UserThreadsUnsafe.mls:38:5) //│ at fork (UserThreadsUnsafe.mls:38:5) @@ -70,9 +69,8 @@ in //│ > f 1 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$ //│ at f (UserThreadsUnsafe.mls:12:3) -//│ at while (UserThreadsUnsafe.mls:29:5) -//│ at drain (pc=6) -//│ at fork (UserThreadsUnsafe.mls:61:5) -//│ at fork (UserThreadsUnsafe.mls:61:5) +//│ at drain (UserThreadsUnsafe.mls:29:5) +//│ at fork (UserThreadsUnsafe.mls:60:5) +//│ at fork (UserThreadsUnsafe.mls:60:5) diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 1c9a994104..8906b7b43d 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -46,49 +46,31 @@ fun foo() = return () => x //│ JS (unsanitized): //│ let foo2; /** scoped **/ -//│ let lambda2, while$1, lambda$2, foo$capture5; -//│ lambda$2 = function lambda$(foo$capture6) { -//│ return foo$capture6.x$capture$0 +//│ let lambda2, lambda$2; +//│ lambda$2 = function lambda$(x) { +//│ return x //│ }; -//│ lambda2 = (undefined, function (foo$capture6) { +//│ lambda2 = (undefined, function (x) { //│ return () => { -//│ return lambda$2(foo$capture6) +//│ return lambda$2(x) //│ } //│ }); -//│ while$1 = function while$(foo$capture6) { -//│ let scrut, tmp2; /** scoped **/ +//│ foo2 = function foo() { +//│ let x, tmp2; /** scoped **/ //│ let lambda$here; -//│ scrut = true; -//│ if (scrut === true) { -//│ tmp2 = foo$capture6.x$capture$0 + 1; -//│ foo$capture6.x$capture$0 = tmp2; -//│ lambda$here = runtime.safeCall(lambda2(foo$capture6)); -//│ return lambda$here -//│ } else { -//│ foo$capture6.tmp$capture$1 = runtime.Unit; -//│ } -//│ return runtime.LoopEnd -//│ }; -//│ globalThis.Object.freeze(class foo$capture4 { -//│ static { -//│ foo$capture5 = this +//│ x = 1; +//│ tmp3: while (true) { +//│ let scrut, tmp4; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { +//│ tmp4 = x + 1; +//│ x = tmp4; +//│ lambda$here = runtime.safeCall(lambda2(x)); +//│ return lambda$here +//│ } else { tmp2 = runtime.Unit; } +//│ break; //│ } -//│ constructor(x$capture$0, tmp$capture$1) { -//│ this.tmp$capture$1 = tmp$capture$1; -//│ this.x$capture$0 = x$capture$0; -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "foo$capture"]; -//│ }); -//│ foo2 = function foo() { -//│ let x, tmp2, tmp3, tmp4, while1; /** scoped **/ -//│ let capture; -//│ capture = new foo$capture5(null, null); -//│ capture.x$capture$0 = 1; -//│ capture.tmp$capture$1 = undefined; -//│ tmp3 = while$1(capture); -//│ tmp4 = tmp3 !== runtime.LoopEnd; -//│ if (tmp4 === true) { return tmp3 } else { return capture.tmp$capture$1 } +//│ return tmp2 //│ }; :expect 2 diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index 291d7f8c47..5fdf8ed31f 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,21 +25,21 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, tmp5, scrut5; /** scoped **/ -//│ split_root$2: { -//│ split_1$2: { -//│ scrut5 = true; -//│ if (scrut5 === true) { -//│ scrut4 = test(42); -//│ if (scrut4 === true) { -//│ tmp5 = true; -//│ break split_root$2 -//│ } else { break split_1$2 } -//│ } else { break split_1$2 } +//│ let scrut6, scrut7, tmp4; /** scoped **/ +//│ split_root$3: { +//│ split_1$3: { +//│ scrut6 = true; +//│ if (scrut6 === true) { +//│ scrut7 = test(42); +//│ if (scrut7 === true) { +//│ tmp4 = true; +//│ break split_root$3 +//│ } else { break split_1$3 } +//│ } else { break split_1$3 } //│ } -//│ tmp5 = false; +//│ tmp4 = false; //│ } -//│ tmp5 +//│ tmp4 //│ > 42 //│ = false diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala index f527cb10d8..5f6f148b2e 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala @@ -99,7 +99,7 @@ abstract class MLsDiffMaker extends DiffMaker: liftDefns = Opt.when(liftDefns.isSet)(LiftDefns()), stageCode = stageCode.isSet, target = if wasm.isSet then CompilationTarget.Wasm else CompilationTarget.JS, - rewriteWhileLoops = !dontRewriteWhile.isSet, + rewriteWhileLoops = false //!dontRewriteWhile.isSet, ) From 80fa081cae598e15889cb619930d7746801b7ef6 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Wed, 3 Dec 2025 16:21:20 +0800 Subject: [PATCH 48/72] WIP: Add scope.locally --- .../src/main/scala/hkmc2/codegen/Block.scala | 42 ++-- .../hkmc2/codegen/BlockTransformer.scala | 4 +- .../main/scala/hkmc2/codegen/Lowering.scala | 24 +- .../scala/hkmc2/semantics/Elaborator.scala | 2 + .../hkmc2/semantics/ucs/Normalization.scala | 2 +- .../src/test/mlscript/bbml/bbGetters.mls | 30 +-- .../src/test/mlscript/bbml/bbPrelude.mls | 3 + .../src/test/mlscript/codegen/Arrays.mls | 2 +- .../test/mlscript/codegen/CaseShorthand.mls | 2 +- .../src/test/mlscript/codegen/ConsoleLog.mls | 2 +- .../src/test/mlscript/codegen/Formatting.mls | 2 +- .../src/test/mlscript/codegen/IfThenElse.mls | 6 +- .../test/mlscript/codegen/MergeMatchArms.mls | 42 ++-- .../test/mlscript/codegen/NestedScoped.mls | 207 ++++++++++++++++++ .../src/test/mlscript/codegen/OptMatch.mls | 2 +- .../test/mlscript/codegen/ParamClasses.mls | 2 +- .../test/mlscript/codegen/PlainClasses.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/SetIn.mls | 6 +- .../src/test/mlscript/codegen/While.mls | 22 +- .../src/test/mlscript/decls/Prelude.mls | 3 + .../src/test/mlscript/handlers/Effects.mls | 2 +- .../mlscript/handlers/RecursiveHandlers.mls | 32 +-- .../src/test/mlscript/lifter/DefnsInClass.mls | 2 +- .../ucs/general/LogicalConnectives.mls | 10 +- .../ucs/normalization/Deduplication.mls | 2 +- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 2 +- 27 files changed, 343 insertions(+), 116 deletions(-) create mode 100644 hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 244eda5cb0..9540c773af 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -273,11 +273,11 @@ sealed abstract class Block extends Product: then this else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) - case Scoped(syms, body) => + case s @ Scoped(syms, body) => val newBody = body.flatten(k) if newBody is body then this - else Scoped(syms, newBody) + else Scoped(syms, newBody)(s.dontFlatten) case e: End => k(e) case t: BlockTail => this @@ -304,7 +304,9 @@ case class Label(label: Local, loop: Bool, body: Block, rest: Block) extends Blo case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail + +// set dontFlatten to true to force a nested Scoped +case class Scoped(syms: collection.Set[Local], body: Block)(val dontFlatten: Bool) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail @@ -322,44 +324,44 @@ case class Define(defn: Defn, rest: Block) extends Block with ProductWithTail object Match: def apply(scrut: Path, arms: Ls[Case -> Block], dflt: Opt[Block], rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, Match(scrut, arms, dflt, body)) + case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, Match(scrut, arms, dflt, body))(false) case _ => new Match(scrut, arms, dflt, rest) object Label: def apply(label: Local, loop: Bool, body: Block, rest: Block): Block = rest match - case Scoped(syms, rest) => Scoped(syms, Label(label, loop, body, rest)) + case s @ Scoped(syms, rest) if !s.dontFlatten => Scoped(syms, Label(label, loop, body, rest))(false) case _ => new Label(label, loop, body, rest) object Scoped: - def apply(syms: collection.Set[Local], body: Block): Block = body match - case Scoped(syms2, body) => - if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body) else Scoped(syms ++ syms2, body) + def apply(syms: collection.Set[Local], body: Block)(dontFlatten: Bool): Block = body match + case s @ Scoped(syms2, body) if !s.dontFlatten => + if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body)(dontFlatten) else Scoped(syms ++ syms2, body)(dontFlatten) case _ => - if syms.isEmpty then body else new Scoped(syms, body) + if syms.isEmpty && !dontFlatten then body else new Scoped(syms, body)(dontFlatten) object Begin: def apply(sub: Block, rest: Block): Block = (sub, rest) match - case (Scoped(symsSub, bodySub), Scoped(symsRest, bodyRest)) => - Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest)) - case (Scoped(symsSub, bodySub), _) => Scoped(symsSub, Begin(bodySub, rest)) - case (_, Scoped(symsRest, bodyRest)) => Scoped(symsRest, Begin(sub, bodyRest)) + case (s1 @ Scoped(symsSub, bodySub), s2 @ Scoped(symsRest, bodyRest)) if !s1.dontFlatten && !s2.dontFlatten => + Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest))(false) + case (s @ Scoped(symsSub, bodySub), _) if !s.dontFlatten => Scoped(symsSub, Begin(bodySub, rest))(false) + case (_, s @ Scoped(symsRest, bodyRest)) if !s.dontFlatten => Scoped(symsRest, Begin(sub, bodyRest))(false) case _ => new Begin(sub, rest) object TryBlock: def apply(sub: Block, finallyDo: Block, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, TryBlock(sub, finallyDo, body)) + case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, TryBlock(sub, finallyDo, body))(false) case _ => new TryBlock(sub, finallyDo, rest) object Assign: def apply(lhs: Local, rhs: Result, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, Assign(lhs, rhs, body)) + case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, Assign(lhs, rhs, body))(false) case _ => new Assign(lhs, rhs, rest) object AssignField: def apply(lhs: Path, nme: Tree.Ident, rhs: Result, rest: Block)(symbol: Opt[MemberSymbol]): Block = rest match - case Scoped(syms, body) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol)) + case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol))(false) case _ => new AssignField(lhs, nme, rhs, rest)(symbol) object AssignDynField: def apply(lhs: Path, fld: Path, arrayIdx: Bool, rhs: Result, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body)) + case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body))(false) case _ => new AssignDynField(lhs, fld, arrayIdx, rhs, rest) object Define: def apply(defn: Defn, rest: Block): Block = rest match - case Scoped(syms, body) => Scoped(syms, Define(defn, body)) + case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, Define(defn, body))(false) case _ => new Define(defn, rest) case class HandleBlock( @@ -385,7 +387,7 @@ object HandleBlock: body: Block, rest: Block ) = rest match - case Scoped(syms, rest) => + case s @ Scoped(syms, rest) if !s.dontFlatten => Scoped( syms, new HandleBlock( @@ -397,7 +399,7 @@ object HandleBlock: handlers, body, rest - )) + ))(false) case _ => new HandleBlock( lhs, res, diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala index c3dc63c211..3b8c1fe8a8 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala @@ -95,9 +95,9 @@ class BlockTransformer(subst: SymbolSubst): if (lhs2 is lhs) && (fld2 is fld) && (rhs2 is rhs) && (rest2 is rest) then b else AssignDynField(lhs2, fld2, arrayIdx, rhs2, rest2) - case Scoped(s, bd) => + case sc @ Scoped(s, bd) => val nb = applySubBlock(bd) - if nb is bd then b else Scoped(s, nb) + if nb is bd then b else Scoped(s, nb)(sc.dontFlatten) def applyRcdArg(rcdArg: RcdArg)(k: RcdArg => Block): Block = val RcdArg(idx, p) = rcdArg diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 303d88b5dc..975d0e27ce 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -265,7 +265,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val mtds = methods.map: case (sym, params, split) => val paramLists = params :: Nil - val bodyBlock = inScopedBlock(ucs.Normalization(this)(split)(Ret)) + val bodyBlock = inScopedBlock(false)(ucs.Normalization(this)(split)(Ret)) FunDefn.withFreshSymbol(N, sym, paramLists, bodyBlock)(isTailRec = false) // The return type is intended to be consistent with `gatherMembers` (mtds, Nil, Nil, End()) @@ -544,6 +544,16 @@ class Lowering()(using Config, TL, Raise, State, Ctx): t.toLoc :: Nil, source = Diagnostic.Source.Compilation) conclude(Value.Ref(ctx.builtins.debug.getLocals, N).withLocOf(f)) + case t if instantiatedResolvedBms.exists(_ is ctx.builtins.scope.locally) => + arg match + case Tup(Fld(_, Lam(ParamList(_, Nil, N), body), N) :: Nil) => + inScopedBlock(true)(block(Nil, R(body))(k)) + case _ => + return fail: + ErrorReport( + msg"locally requires a lambda with no parameter." -> + t.toLoc :: Nil, + source = Diagnostic.Source.Compilation) // * Due to whacky JS semantics, we need to make sure that selections leading to a call // * are preserved in the call and not moved to a temporary variable. case sel @ Sel(prefix, nme) => @@ -891,7 +901,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): reportAnnotations(decl, annotations) sym val ctor = - inScopedBlock: + inScopedBlock(false): term_nonTail(Blk(clsBody.nonMethods, clsBody.blk.res))(ImplctRet) // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }` .mapTail: @@ -977,7 +987,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) val blk = - inScopedBlock(using LoweringCtx.empty): + inScopedBlock(false)(using LoweringCtx.empty): block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -1022,15 +1032,15 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case ps => ps setupFunctionDef(physicalParams, bodyTerm, name) - def inScopedBlock(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = + def inScopedBlock(dontFlatten: Bool)(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = loweringCtx.getCollectedSym - Scoped(scopedSyms, body) + Scoped(scopedSyms, body)(dontFlatten) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = - val scopedBody = inScopedBlock(returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(false)(returnedTerm(bodyTerm)) (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = @@ -1143,7 +1153,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) go(paramLists.reverse, bod) def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - inScopedBlock: + inScopedBlock(false): val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") val resSym = TempSymbol(N, dbgNme = "traceLogRes") diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala index 9b4c220046..29da509477 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala @@ -188,6 +188,8 @@ object Elaborator: val compile = assumeObject("compile") val buffered = assumeObject("buffered") val bufferable = assumeObject("bufferable") + object scope extends VirtualModule(assumeBuiltinMod("scope")): + val locally = assumeObject("locally") def getBuiltinOp(op: Str): Opt[Str] = if getBuiltin(op).isDefined then builtinBinOps.get(op) else N /** Classes that do not use `instanceof` in pattern matching. */ diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 25a06a2354..fcd14c2ad7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -398,7 +398,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e val body = Scoped( if nestScope then LoweringCtx.loweringCtx.getCollectedSym else Set.empty, if labels.isEmpty then mainBlock else - Label(rootBreakLabel, false, mainBlock, End())) + Label(rootBreakLabel, false, mainBlock, End()))(false) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 50986bddaa..20c26d1dd7 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -38,13 +38,13 @@ fun test2() = n then funny(n - 1) + 1 funny //│ ╔══[ERROR] Expected a monomorphic type or an instantiable type here, but () ->{⊥} [outer, 'caseScrut, 'eff] -> 'caseScrut ->{'eff} Int found -//│ ║ l.36: fun funny = case +//│ ║ l.32: fun funny = case //│ ║ ^^^^ -//│ ║ l.37: 0 then 0 +//│ ║ l.33: 0 then 0 //│ ║ ^^^^^^^^^^^^ -//│ ║ l.38: n then funny(n - 1) + 1 +//│ ║ l.34: n then funny(n - 1) + 1 //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.39: funny +//│ ║ l.35: funny //│ ╙── ^^^^^^^ //│ Type: ⊤ @@ -84,8 +84,8 @@ fun test2() = case n then funny(n - 1) + 1 funny //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.83: case 0 then 0 -//│ ╙── ^^^^^^^^^^^^^ +//│ ║ l.138: case 0 then 0 +//│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): //│ let test22; /** scoped **/ //│ test22 = function test2() { @@ -114,16 +114,16 @@ fun test2() = //│ return tmp1 //│ }; //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.83: case 0 then 0 -//│ ╙── ^^^^^^^^^^^^^ +//│ ║ l.138: case 0 then 0 +//│ ╙── ^^^^^^^^^^^^^ //│ ╔══[ERROR] Function definition shape not yet supported for funny -//│ ║ l.83: case 0 then 0 -//│ ║ ^^^^^^^^ -//│ ║ l.84: case n then funny(n - 1) + 1 -//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +//│ ║ l.138: case 0 then 0 +//│ ║ ^^^^^^^^ +//│ ║ l.139: case n then funny(n - 1) + 1 +//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Variable not found: funny -//│ ║ l.85: funny -//│ ╙── ^^^^^ +//│ ║ l.140: funny +//│ ╙── ^^^^^ //│ Type: ⊤ @@ -131,7 +131,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.132: print("Hi") +//│ ║ l.160: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbPrelude.mls b/hkmc2/shared/src/test/mlscript/bbml/bbPrelude.mls index 3034f58335..c13dcce0de 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbPrelude.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbPrelude.mls @@ -75,6 +75,9 @@ declare module annotations with object buffered object bufferable +declare module scope with + fun locally + declare fun run: [T] -> CodeBase[out T, out Nothing, out Any] -> T declare fun log: Str -> Any diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index d7ff1e8b8d..9a97ca3050 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let tmp, single1; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 8e0b86561a..2c5e5cee00 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -85,7 +85,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let lambda12, isDefined; /** scoped **/ +//│ let isDefined, lambda12; /** scoped **/ //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index ab2d6cfc34..1770b21e64 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let tmp2, tmp3, tmp4, x, y; /** scoped **/ +//│ let x, y, tmp2, tmp3, tmp4; /** scoped **/ //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index b8ac39359c..0f1fbe2161 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -53,7 +53,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let ccccccccc, dddddddddd, tmp4, aaaaaaaaa1, bbbbbbbb; /** scoped **/ +//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; /** scoped **/ //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index acf18dbe75..e09edf1374 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let lambda, f; /** scoped **/ +//│ let f, lambda; /** scoped **/ //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,7 +26,7 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let lambda1, f1; /** scoped **/ +//│ let f1, lambda1; /** scoped **/ //│ lambda1 = (undefined, function (x) { //│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { @@ -41,7 +41,7 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let lambda2, f2; /** scoped **/ +//│ let f2, lambda2; /** scoped **/ //│ lambda2 = (undefined, function (x) { //│ let tmp, tmp1; /** scoped **/ //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 727374f250..79d477b6e6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -107,31 +107,31 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let tmp1, tmp2, tmp3, x, tmp4; /** scoped **/ -//│ tmp4 = 3; +//│ let x, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ tmp1 = 3; //│ if (a instanceof A.class) { -//│ tmp1 = 1; +//│ tmp2 = 1; //│ } else if (a instanceof B.class) { -//│ tmp1 = 2; +//│ tmp2 = 2; //│ } else if (a instanceof C.class) { -//│ tmp1 = 3; +//│ tmp2 = 3; //│ } else if (a instanceof D.class) { //│ if (a instanceof A.class) { -//│ tmp2 = 1 + tmp4; +//│ tmp3 = 1 + tmp1; //│ } else if (a instanceof B.class) { -//│ tmp2 = 2 + tmp4; +//│ tmp3 = 2 + tmp1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp3 = tmp2; -//│ tmp1 = Predef.print("done"); +//│ tmp4 = tmp3; +//│ tmp2 = Predef.print("done"); //│ } else if (a instanceof E.class) { -//│ tmp3 = 5; -//│ tmp1 = Predef.print("done"); +//│ tmp4 = 5; +//│ tmp2 = Predef.print("done"); //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ x = tmp1; +//│ x = tmp2; //│ Predef.print(x) //│ > 1 //│ x = 1 @@ -219,23 +219,23 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ let tmp8, tmp9, tmp10, tmp11, x2, tmp12; /** scoped **/ //│ if (a instanceof B.class) { //│ 1 //│ } else { //│ if (a instanceof A.class) { -//│ tmp8 = 2; +//│ tmp12 = 2; //│ } else if (a instanceof C.class) { -//│ tmp8 = 3; +//│ tmp12 = 3; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ x2 = tmp8; -//│ tmp9 = Predef.print(x2); -//│ tmp10 = x2 + 1; -//│ tmp11 = Predef.print(tmp10); -//│ tmp12 = x2 + 2; -//│ Predef.print(tmp12) +//│ x2 = tmp12; +//│ tmp8 = Predef.print(x2); +//│ tmp9 = x2 + 1; +//│ tmp10 = Predef.print(tmp9); +//│ tmp11 = x2 + 2; +//│ Predef.print(tmp11) //│ } //│ > 2 //│ > 3 diff --git a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls new file mode 100644 index 0000000000..e4436c11e1 --- /dev/null +++ b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls @@ -0,0 +1,207 @@ +:js + + +:lot +:sjs +scope.locally of () => + 42 +//│ JS (unsanitized): +//│ 42 +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped: +//│ syms = HashSet() +//│ body = Return: +//│ res = Lit of IntLit of 42 +//│ implct = true +//│ = 42 + + +:lot +:sjs +scope.locally of () => + let x = 42 in x +//│ JS (unsanitized): +//│ let x; /** scoped **/ x = 42; x +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped: +//│ syms = HashSet(x) +//│ body = Assign: +//│ lhs = x +//│ rhs = Lit of IntLit of 42 +//│ rest = Return: \ +//│ res = Ref: +//│ l = x +//│ disamb = N +//│ implct = true +//│ = 42 + + +:e +x +//│ ╔══[ERROR] Name not found: x +//│ ║ l.44: x +//│ ╙── ^ + + +:ge +scope.locally of x => + let y = 1 in x + y +//│ ╔══[COMPILATION ERROR] locally requires a lambda with no parameter. +//│ ║ l.51: scope.locally of x => +//│ ╙── ^^^^^^^^^^^^^ + + + +:ssjs +fun foo(x) = + let z = 1 + scope.locally of () => + let y = 1 in x + y + z +//│ JS: +//│ foo = function foo(...args) {{ +//│ runtime.checkArgs("foo", 1, true, args.length); +//│ let x1 = args[0]; +//│ let z; /** scoped **/ +//│ z = 1;{ +//│ let y, tmp; /** scoped **/ +//│ y = 1; +//│ tmp = x1 + y; +//│ return tmp + z +//│ } +//│ } +//│ }; +//│ block$res5 = undefined; + + +foo(1) +//│ = 3 + + +:sjs +fun f() = + if true then + scope.locally of () => + let a = 4 + a + 4 + else + let b = 5 + b + 9 +//│ JS (unsanitized): +//│ let f; /** scoped **/ +//│ f = function f() { +//│ let scrut, b; /** scoped **/ +//│ scrut = true; +//│ if (scrut === true) { let a; /** scoped **/ a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ }; + + +f() +//│ = 8 + + +:sjs +fun f() = + if false then + let a = 4 + a + 4 + else + scope.locally of () => + let b = 5 + b + 9 +//│ JS (unsanitized): +//│ let f1; /** scoped **/ +//│ f1 = function f() { +//│ let scrut, a; /** scoped **/ +//│ scrut = false; +//│ if (scrut === true) { a = 4; return a + 4 } else { let b; /** scoped **/ b = 5; return b + 9 } +//│ }; + + +f() +//│ = 14 + + +:sjs +fun g(x, y, z) = + 3 + 4 * if + x then 0 + y then 0 + z then + scope.locally of () => + let a = 1 + (_ + a) +//│ JS (unsanitized): +//│ let g; /** scoped **/ +//│ g = function g(x1, y, z) { +//│ let tmp, tmp1; /** scoped **/ +//│ let lambda; +//│ split_root$: { +//│ split_1$: { +//│ if (x1 === true) { +//│ break split_1$ +//│ } else { +//│ if (y === true) { +//│ break split_1$ +//│ } else { +//│ if (z === true) { +//│ let a; /** scoped **/ +//│ a = 1; +//│ lambda = (undefined, function (_0) { +//│ return _0 + a +//│ }); +//│ tmp = lambda; +//│ break split_root$ +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) +//│ } +//│ } +//│ } +//│ } +//│ tmp = 0; +//│ } +//│ tmp1 = 4 * tmp; +//│ return 3 + tmp1 +//│ }; + + + +:sjs +fun foo(x, y) = + let z = [1, 2, 3] + if not x > z.0 do + print("rua") + let m = x + y + scope.locally of () => + let w1 = z.1 + m + let w2 = z.2 - m + if w1 == w2 do + print("ruarua") +//│ JS (unsanitized): +//│ let foo1; /** scoped **/ +//│ foo1 = function foo(x1, y) { +//│ let z, scrut, m, tmp, tmp1; /** scoped **/ +//│ z = globalThis.Object.freeze([ +//│ 1, +//│ 2, +//│ 3 +//│ ]); +//│ tmp = x1 > z[0]; +//│ scrut = ! tmp; +//│ if (scrut === true) { +//│ tmp1 = Predef.print("rua"); +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ m = x1 + y;{ +//│ let w1, w2, scrut1; /** scoped **/ +//│ w1 = z[1] + m; +//│ w2 = z[2] - m; +//│ scrut1 = w1 == w2; +//│ if (scrut1 === true) { return Predef.print("ruarua") } else { return runtime.Unit } +//│ } +//│ }; + diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 2416214a74..3306b06107 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -31,7 +31,7 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; /** scoped **/ +//│ let lambda, isDefined1; /** scoped **/ //│ lambda = (undefined, function (caseScrut) { //│ let argument0$; /** scoped **/ //│ if (caseScrut instanceof Some.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index f7054a8a6e..5c512eb5e1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo4(tmp1, tmp2) +//│ let tmp1, tmp2; /** scoped **/ tmp2 = Predef.print(1); tmp1 = Predef.print(2); Foo4(tmp2, tmp1) //│ > 1 //│ > 2 //│ = Foo((), ()) diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index fe5ef3c424..19591829d2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -217,7 +217,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let tmp, tmp1, tmp2, tmp3, a; /** scoped **/ +//│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ //│ a = globalThis.Object.freeze(new Foo9()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 17d83ddd0f..3cd21a98c2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let tmp, tmp1, tmp2, tmp3, lambda, folderName1, folderName2; /** scoped **/ +//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; /** scoped **/ //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 6c6b09fbe7..7ac70bc6b4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -30,10 +30,10 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let tmp1, tmp2, old, tmp3; /** scoped **/ +//│ let old, tmp1, tmp2, tmp3; /** scoped **/ //│ old = x1; -//│ try { tmp1 = x1 + 1; x1 = tmp1; tmp2 = Predef.print(x1); tmp3 = tmp2; } finally { x1 = old; } -//│ tmp3 +//│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } +//│ tmp1 //│ > 2 x diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 91ebaefac6..eb618c68fb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -57,20 +57,20 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let while3, tmp6, tmp7, tmp8; /** scoped **/ -//│ tmp8 = undefined; +//│ let tmp6, while3, tmp7, tmp8; /** scoped **/ +//│ tmp6 = undefined; //│ while3 = (undefined, function () { //│ let tmp9; /** scoped **/ //│ if (x2 === true) { //│ tmp9 = Predef.print("Hello World"); //│ x2 = false; -//│ tmp8 = runtime.Unit; +//│ tmp6 = runtime.Unit; //│ return while3() -//│ } else { tmp8 = 42; } +//│ } else { tmp6 = 42; } //│ return runtime.LoopEnd //│ }); -//│ tmp6 = while3(); -//│ tmp8 +//│ tmp7 = while3(); +//│ tmp6 //│ > Hello World //│ = 42 @@ -267,8 +267,8 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37, tmp38, while11, tmp39; /** scoped **/ -//│ tmp39 = undefined; +//│ let tmp37, tmp38, tmp39, while11; /** scoped **/ +//│ tmp37 = undefined; //│ while11 = (undefined, function () { //│ split_root$: { //│ split_1$: { @@ -278,12 +278,12 @@ while x is {} do() //│ break split_1$ //│ } //│ } -//│ tmp39 = runtime.Unit; +//│ tmp37 = runtime.Unit; //│ } //│ return runtime.LoopEnd //│ }); -//│ tmp37 = while11(); -//│ tmp39 +//│ tmp38 = while11(); +//│ tmp37 // ——— FIXME: ——— diff --git a/hkmc2/shared/src/test/mlscript/decls/Prelude.mls b/hkmc2/shared/src/test/mlscript/decls/Prelude.mls index 66475d947c..3ffa843380 100644 --- a/hkmc2/shared/src/test/mlscript/decls/Prelude.mls +++ b/hkmc2/shared/src/test/mlscript/decls/Prelude.mls @@ -195,6 +195,9 @@ declare module annotations with object buffered object bufferable +declare module scope with + fun locally + // HTML DOM API definitions. // Move them to a separate Prelude file when there are enough of them. diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 2d36117f61..15c9db1020 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,7 +156,7 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let f, tmp20, scrut; /** scoped **/ +//│ let scrut, f, tmp20; /** scoped **/ //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 936b367f53..90c2c62a51 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,7 +155,7 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let tmp24, tmp25, tmp26, str, scrut, tmp27; /** scoped **/ +//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; /** scoped **/ //│ let handleBlock$7; //│ str = ""; //│ scrut = true; @@ -232,11 +232,11 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 6) { -//│ tmp24 = value$; +//│ tmp25 = value$; //│ } //│ contLoop: while (true) { //│ if (this.pc === 6) { -//│ return tmp24 +//│ return tmp25 //│ } //│ break; //│ } @@ -323,7 +323,7 @@ str //│ } //│ resume(value$) { //│ if (this.pc === 1) { -//│ tmp25 = value$; +//│ tmp26 = value$; //│ } else if (this.pc === 2) { //│ res7 = value$; //│ } @@ -348,9 +348,9 @@ str //│ res8.contTrace.last.next = new Cont$handleBlock$h2$2(pc); //│ return runtime.handleBlockImpl(res8, h2) //│ }; -//│ tmp25 = runtime.safeCall(h2.perform(runtime.Unit)); -//│ if (tmp25 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp25, 1) +//│ tmp26 = runtime.safeCall(h2.perform(runtime.Unit)); +//│ if (tmp26 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp26, 1) //│ } //│ res7 = runtime.safeCall(h1.perform(runtime.Unit)); //│ if (res7 instanceof runtime.EffectSig.class) { @@ -358,18 +358,18 @@ str //│ } //│ return res7 //│ }; -//│ tmp24 = handleBlock$8(); -//│ if (tmp24 instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp24, 6) +//│ tmp25 = handleBlock$8(); +//│ if (tmp25 instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp25, 6) //│ } -//│ return tmp24 +//│ return tmp25 //│ }; -//│ tmp27 = handleBlock$7(); -//│ if (tmp27 instanceof runtime.EffectSig.class) { -//│ tmp27 = runtime.topLevelEffect(tmp27, false); +//│ tmp24 = handleBlock$7(); +//│ if (tmp24 instanceof runtime.EffectSig.class) { +//│ tmp24 = runtime.topLevelEffect(tmp24, false); //│ } -//│ tmp26 = tmp27; -//│ } else { tmp26 = runtime.Unit; } +//│ tmp27 = tmp24; +//│ } else { tmp27 = runtime.Unit; } //│ str //│ = "BABABA" //│ str = "BABABA" diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 40b369eb47..47a6d9147d 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -74,7 +74,7 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let A3, tmp1; /** scoped **/ +//│ let tmp1, A3; /** scoped **/ //│ let g, g$; //│ g$ = function g$(A$instance) { //│ return 2 diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index 291d7f8c47..de445d712f 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,13 +25,13 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut4, tmp5, scrut5; /** scoped **/ +//│ let scrut4, scrut5, tmp5; /** scoped **/ //│ split_root$2: { //│ split_1$2: { -//│ scrut5 = true; -//│ if (scrut5 === true) { -//│ scrut4 = test(42); -//│ if (scrut4 === true) { +//│ scrut4 = true; +//│ if (scrut4 === true) { +//│ scrut5 = test(42); +//│ if (scrut5 === true) { //│ tmp5 = true; //│ break split_root$2 //│ } else { break split_1$2 } diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index ca253fe2be..6d88aa0a07 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -81,7 +81,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; /** scoped **/ +//│ let tmp3, qqq; /** scoped **/ //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 08c772d47d..6770f3235f 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -123,7 +123,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: toplvlDefinedVars = xs assignResSym(body, false) else - Scoped(xs, assignResSym(body, false)) + Scoped(xs, assignResSym(body, false))(false) case tl: (Throw | Break | Continue) => tl val le = lowered0.copy(main = assignResSym(lowered0.main, true)) if showLoweredTree.isSet then From 90efc09ae4450e0581cdeef32dab22562a58dea9 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Wed, 3 Dec 2025 16:30:01 +0800 Subject: [PATCH 49/72] Rerun testss --- .../src/test/mlscript/bbml/bbGetters.mls | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 20c26d1dd7..50986bddaa 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -38,13 +38,13 @@ fun test2() = n then funny(n - 1) + 1 funny //│ ╔══[ERROR] Expected a monomorphic type or an instantiable type here, but () ->{⊥} [outer, 'caseScrut, 'eff] -> 'caseScrut ->{'eff} Int found -//│ ║ l.32: fun funny = case +//│ ║ l.36: fun funny = case //│ ║ ^^^^ -//│ ║ l.33: 0 then 0 +//│ ║ l.37: 0 then 0 //│ ║ ^^^^^^^^^^^^ -//│ ║ l.34: n then funny(n - 1) + 1 +//│ ║ l.38: n then funny(n - 1) + 1 //│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -//│ ║ l.35: funny +//│ ║ l.39: funny //│ ╙── ^^^^^^^ //│ Type: ⊤ @@ -84,8 +84,8 @@ fun test2() = case n then funny(n - 1) + 1 funny //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.138: case 0 then 0 -//│ ╙── ^^^^^^^^^^^^^ +//│ ║ l.83: case 0 then 0 +//│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): //│ let test22; /** scoped **/ //│ test22 = function test2() { @@ -114,16 +114,16 @@ fun test2() = //│ return tmp1 //│ }; //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.138: case 0 then 0 -//│ ╙── ^^^^^^^^^^^^^ +//│ ║ l.83: case 0 then 0 +//│ ╙── ^^^^^^^^^^^^^ //│ ╔══[ERROR] Function definition shape not yet supported for funny -//│ ║ l.138: case 0 then 0 -//│ ║ ^^^^^^^^ -//│ ║ l.139: case n then funny(n - 1) + 1 -//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +//│ ║ l.83: case 0 then 0 +//│ ║ ^^^^^^^^ +//│ ║ l.84: case n then funny(n - 1) + 1 +//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Variable not found: funny -//│ ║ l.140: funny -//│ ╙── ^^^^^ +//│ ║ l.85: funny +//│ ╙── ^^^^^ //│ Type: ⊤ @@ -131,7 +131,7 @@ fun test2() = fun test3 = print("Hi") //│ ╔══[ERROR] Function definition shape not yet supported for test3 -//│ ║ l.160: print("Hi") +//│ ║ l.132: print("Hi") //│ ╙── ^^^^^^^^^^^ //│ Type: ⊤ From 8b3b675c920520ebd39d9b63cd665975b688c594 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Wed, 3 Dec 2025 17:43:17 +0800 Subject: [PATCH 50/72] more fixes related to handlers --- .../shared/src/main/scala/hkmc2/Config.scala | 6 ++ .../src/main/scala/hkmc2/codegen/Block.scala | 26 ++++++++ .../scala/hkmc2/codegen/HandlerLowering.scala | 4 +- .../hkmc2/semantics/ucs/Normalization.scala | 8 ++- .../src/test/mlscript/codegen/Scoped.mls | 64 +++++++++++++++++++ .../src/test/mlscript/handlers/Debugging.mls | 13 ++-- .../src/test/mlscript/handlers/Effects.mls | 4 +- .../mlscript/handlers/EffectsInClasses.mls | 4 +- .../mlscript/handlers/RecursiveHandlers.mls | 6 +- .../test/mlscript/handlers/StackSafety.mls | 8 +-- .../mlscript/handlers/UserThreadsSafe.mls | 10 +-- .../mlscript/handlers/UserThreadsUnsafe.mls | 10 +-- .../test/mlscript/lifter/StackSafetyLift.mls | 4 +- 13 files changed, 135 insertions(+), 32 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/Config.scala b/hkmc2/shared/src/main/scala/hkmc2/Config.scala index 6e6a7f183b..0c6a886f9c 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/Config.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/Config.scala @@ -27,6 +27,12 @@ case class Config( def stackSafety: Opt[StackSafety] = effectHandlers.flatMap(_.stackSafety) + // NOTE: with `Scoped` blocks inside loop bodies, currently this flag + // forces the rewriting of while loops to make sure that + // the handler lowering will not create classes containing unbound variables + def shouldRewriteWhile: Bool = + rewriteWhileLoops || effectHandlers.isDefined + end Config diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 244eda5cb0..a12962f1d6 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -190,6 +190,32 @@ sealed abstract class Block extends Product: (transformer.applyBlock(this), defns) + + // This is currently only used in `HanlderLowering` to prevent + // floating things out too aggressively such that the captured variables become unbound. + // TODO: clean up this and `floatOutDefns` above; currently there are two variations of this + // "floatOut" function because the lifter also uses "floatOut" and + // this `floatOutDefnsUntilScoped` below breaks the lifter + def floatOutDefnsUntilScoped( + ignore: Defn => Bool = _ => false, + preserve: Defn => Bool = _ => false + ): (Block, List[Defn]) = + var defns: List[Defn] = Nil + val transformer = new BlockTransformerShallow(SymbolSubst()): + override def applyBlock(b: Block): Block = b match + case Scoped(syms, blk) => + val (inner, defs) = blk.floatOutDefnsUntilScoped(ignore, preserve) + Scoped(syms ++ defs.map(_.sym), defs.foldLeft(inner)((acc, defn) => Define(defn, acc))) + case Define(defn, rest) if !ignore(defn) => defn match + case v: ValDefn => super.applyBlock(b) + case _ => + defns ::= defn + if preserve(defn) then super.applyBlock(b) + else applyBlock(rest) + case _ => super.applyBlock(b) + + (transformer.applyBlock(this), defns) + lazy val flattened: Block = this.flatten(identity) private def flatten(k: End => Block): Block = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index 97a13183b4..b5f91ce19e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -579,7 +579,9 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, private def thirdPass(b: Block): Block = // to ensure the fun and class references in the continuation class are properly scoped, // we move all function defns to the top level of the handler block - val (blk, defns) = b.floatOutDefns() + // NOTE: this is to prevent floating things out too aggressively + // such that the captured variables become unbound. + val (blk, defns) = b.floatOutDefnsUntilScoped() defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) private def locToStr(l: Loc): Str = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index f4db72a690..b1d9c536f8 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -345,7 +345,9 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e lazy val rootBreakLabel = new TempSymbol(N, "split_root$") lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) lazy val assignResult = (r: Result) => Assign(l, r, End()) - lazy val loopCont = if config.rewriteWhileLoops + // NOTE: `shouldRewriteWhile` is not the same as `config.rewriteWhileLoops` + // as shouldRewriteWhile is always true when effect handler lowering is on + lazy val loopCont = if config.shouldRewriteWhile then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) else Continue(loopLabel) val cont = @@ -408,7 +410,9 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = if kw === `while` then - if config.rewriteWhileLoops then + // NOTE: `shouldRewriteWhile` is not the same as `config.rewriteWhileLoops` + // as shouldRewriteWhile is always true when effect handler lowering is on + if config.shouldRewriteWhile then val loopResult = TempSymbol(N) val isReturned = TempSymbol(N) outerCtx.collectScopedSym(loopResult) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index b722f33ba5..7bfeb7930c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -376,3 +376,67 @@ module M with //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); + + +:effectHandlers +fun start(x, f) = + while x do + f(() => x) + + +:effectHandlers +:sjs +fun f(x) = + if x do + if x do + let z = 3 + fun g() = z +//│ JS (unsanitized): +//│ let f6; /** scoped **/ +//│ f6 = function f(x1) { +//│ if (x1 === true) { +//│ let g1, z; /** scoped **/ +//│ g1 = function g() { +//│ return z +//│ }; +//│ if (x1 === true) { +//│ z = 3; +//│ return runtime.Unit +//│ } else { return runtime.Unit } +//│ } else { return runtime.Unit } +//│ }; + + + +abstract class Effect with + fun perform(arg: Str): Str + + + +:effectHandlers +fun f(x) = + if x then + if x then + let z = 4 + handle h = Effect with + fun perform(x)(k) = + print(x) + k(x + z) + print(h.perform(32)) + else 0 + + +// NOTE: currently while loops are always rewritten into functions +// when `:effectHanlders` are set, so this "dontRewriteWhile" has no effect here +:dontRewriteWhile +:effectHandlers +fun f(x) = + while x do + while x do + let z = 4 + handle h = Effect with + fun perform(x)(k) = + print(x) + k(x + z) + print(h.perform(32)) + else 0 diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index ec3ef74cf6..dd4b90f8f1 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -28,8 +28,7 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ -//│ let getLocals3, Cont$func$f$1, doUnwind; +//│ let i, j, k, scrut, tmp, tmp1, getLocals3, Cont$func$f$1, doUnwind; /** scoped **/ //│ getLocals3 = function getLocals() { //│ let i1, j1, k1, prev, thisInfo, arr, tmp2; //│ prev = getLocals2(); @@ -128,8 +127,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:128:3) -//│ at lambda_test (Debugging.mls:125:3) +//│ at lambda (Debugging.mls:127:3) +//│ at lambda_test (Debugging.mls:124:3) import "../../mlscript-compile/Runtime.mls" @@ -208,9 +207,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:201:3) with locals: j=200 +//│ > at f (Debugging.mls:200:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:203:3) +//│ > at f (Debugging.mls:202:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:204:3) with locals: j=300 +//│ > at f (Debugging.mls:203:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 0bd1b3914f..af13982975 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -159,8 +159,8 @@ if true do //│ let tmp20; /** scoped **/ //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { -//│ let f, scrut; /** scoped **/ -//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; +//│ let f, scrut, Cont$handleBlock$h$11, doUnwind; /** scoped **/ +//│ let h, res, Handler$h$12; //│ globalThis.Object.freeze(class Handler$h$11 extends Effect { //│ static { //│ Handler$h$12 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 83c9524e92..0a530b3001 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -19,8 +19,8 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { -//│ let tmp; /** scoped **/ -//│ let res, Cont$ctor$Lol$1, doUnwind; +//│ let tmp, Cont$ctor$Lol$1, doUnwind; /** scoped **/ +//│ let res; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 936b367f53..bc537a4a36 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -173,8 +173,7 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30; /** scoped **/ -//│ let Cont$handler$h1$perform$2, doUnwind1; +//│ let tmp28, tmp29, tmp30, Cont$handler$h1$perform$2, doUnwind1; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h1$perform$2 = this @@ -261,8 +260,7 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ -//│ let Cont$handler$h2$perform$2, doUnwind2; +//│ let tmp28, tmp29, tmp30, tmp31, tmp32, Cont$handler$h2$perform$2, doUnwind2; /** scoped **/ //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h2$perform$2 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 61cef5c3bf..66815a2fa5 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -23,8 +23,8 @@ hi(0) //│ let hi; /** scoped **/ //│ let res, $_stack$_safe$_body$_; //│ hi = function hi(n) { -//│ let scrut, tmp; /** scoped **/ -//│ let Cont$func$hi$1, doUnwind, stackDelayRes; +//│ let scrut, tmp, Cont$func$hi$1, doUnwind; /** scoped **/ +//│ let stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$hi$1 = this @@ -79,8 +79,8 @@ sum(10000) //│ let sum1; /** scoped **/ //│ let res1, $_stack$_safe$_body$_1; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1; /** scoped **/ -//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1, Cont$func$sum$1, doUnwind; /** scoped **/ +//│ let curDepth, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$sum$1 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls index c240949e57..26059f2a32 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsSafe.mls @@ -44,7 +44,8 @@ in //│ > f 2 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$ //│ at f (UserThreadsSafe.mls:17:3) -//│ at drain (UserThreadsSafe.mls:10:7) +//│ at while (UserThreadsSafe.mls:10:7) +//│ at drain (pc=6) //│ at fork (UserThreadsSafe.mls:35:5) //│ at fork (UserThreadsSafe.mls:35:5) @@ -66,8 +67,9 @@ in //│ > f 0 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$1 //│ at f (UserThreadsSafe.mls:17:3) -//│ at drain (UserThreadsSafe.mls:10:7) -//│ at fork (UserThreadsSafe.mls:57:5) -//│ at fork (UserThreadsSafe.mls:57:5) +//│ at while (UserThreadsSafe.mls:10:7) +//│ at drain (pc=6) +//│ at fork (UserThreadsSafe.mls:58:5) +//│ at fork (UserThreadsSafe.mls:58:5) diff --git a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls index 402bfe41c3..c22ef1eadf 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/UserThreadsUnsafe.mls @@ -47,7 +47,8 @@ in //│ > f 2 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$ //│ at f (UserThreadsUnsafe.mls:12:3) -//│ at drain (UserThreadsUnsafe.mls:29:5) +//│ at while (UserThreadsUnsafe.mls:29:5) +//│ at drain (pc=6) //│ at fork (UserThreadsUnsafe.mls:38:5) //│ at fork (UserThreadsUnsafe.mls:38:5) @@ -69,8 +70,9 @@ in //│ > f 1 //│ ═══[RUNTIME ERROR] Error: Unhandled effect Handler$h$ //│ at f (UserThreadsUnsafe.mls:12:3) -//│ at drain (UserThreadsUnsafe.mls:29:5) -//│ at fork (UserThreadsUnsafe.mls:60:5) -//│ at fork (UserThreadsUnsafe.mls:60:5) +//│ at while (UserThreadsUnsafe.mls:29:5) +//│ at drain (pc=6) +//│ at fork (UserThreadsUnsafe.mls:61:5) +//│ at fork (UserThreadsUnsafe.mls:61:5) diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 36e71bb305..25340985c2 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -61,7 +61,7 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let scrut, tmp; /** scoped **/ +//│ let scrut, tmp, doUnwind; /** scoped **/ //│ let stackDelayRes; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); @@ -151,7 +151,7 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1; /** scoped **/ +//│ let scrut, tmp, tmp1, doUnwind; /** scoped **/ //│ let curDepth, stackDelayRes; //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; From cbe96e04475b888280e38fa0889fa87269d9e3e8 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Thu, 4 Dec 2025 15:15:20 +0800 Subject: [PATCH 51/72] Minor fix and clean up --- .../src/main/scala/hkmc2/codegen/Block.scala | 1 + .../scala/hkmc2/codegen/js/JSBuilder.scala | 11 ++- .../OverloadedModulesInSignatures.mls | 10 +- .../src/test/mlscript/backlog/ToTriage.mls | 10 +- .../test/mlscript/basics/BadModuleUses.mls | 4 +- .../basics/CompanionModules_Classes.mls | 2 +- .../basics/CompanionModules_Functions.mls | 4 +- .../mlscript/basics/DynamicInstantiation.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 4 +- hkmc2/shared/src/test/mlscript/basics/New.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../test/mlscript/basics/ObjectExtensions.mls | 4 +- .../src/test/mlscript/basics/Overloading.mls | 2 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 8 +- .../src/test/mlscript/codegen/Arrays.mls | 2 +- .../src/test/mlscript/codegen/BadOpen.mls | 2 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 12 +-- .../test/mlscript/codegen/CaseShorthand.mls | 4 +- .../test/mlscript/codegen/ClassMatching.mls | 20 ++-- .../test/mlscript/codegen/DelayedLetInit.mls | 2 +- .../src/test/mlscript/codegen/FunnyOpen.mls | 2 +- .../src/test/mlscript/codegen/Getters.mls | 2 +- .../src/test/mlscript/codegen/Hygiene.mls | 8 +- .../src/test/mlscript/codegen/ImportedOps.mls | 4 +- .../test/mlscript/codegen/MergeMatchArms.mls | 98 +++++++++---------- .../test/mlscript/codegen/ModuleMethods.mls | 4 +- .../src/test/mlscript/codegen/Modules.mls | 10 +- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 10 +- .../test/mlscript/codegen/ParamClasses.mls | 26 ++--- .../test/mlscript/codegen/PlainClasses.mls | 18 ++-- .../test/mlscript/codegen/SanityChecks.mls | 47 +++++---- .../src/test/mlscript/codegen/Scoped.mls | 16 +-- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/TraceLog.mls | 20 ++-- .../src/test/mlscript/codegen/While.mls | 2 +- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/handlers/Effects.mls | 2 +- .../mlscript/handlers/RecursiveHandlers.mls | 4 +- .../src/test/mlscript/interop/CtorBypass.mls | 2 +- .../src/test/mlscript/interop/Objects.mls | 4 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 2 +- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 24 ++--- .../ucs/normalization/Deduplication.mls | 16 +-- .../ucs/normalization/SimplePairMatches.mls | 16 +-- .../ucs/patterns/ConjunctionPattern.mls | 2 +- .../src/test/mlscript/ups/MatchResult.mls | 2 +- .../src/test/mlscript/ups/SimpleTransform.mls | 2 +- .../test/mlscript/ups/regex/Separation.mls | 2 +- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 28 +----- 51 files changed, 244 insertions(+), 253 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 24b9cbbbfd..f3000ed8a5 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -88,6 +88,7 @@ sealed abstract class Block extends Product: // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match + case s @ Scoped(syms, body) => Scoped(syms, body.mapTail(f))(s.dontFlatten) case b: BlockTail => f(b) case Begin(sub, rst) => Begin(sub, rst.mapTail(f)) case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f)) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 1725e17be9..c7b688b079 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -595,7 +595,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: val (scopedSyms, unscopedMain) = p.main match case Scoped(syms, body) /* if exprt.isDefined */ => (syms, body) case _ => (Set.empty, p.main) - imps.mkDocument(doc" # ") :/: (genLetDecls(scopedSyms.iterator.map(l => l -> scope.allocateName(l)), true) :: block(unscopedMain, endSemi = false)).stripBreaks :: ( + imps.mkDocument(doc" # ") :/: (genLetDecls(scopedSyms.toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)), true) :: block(unscopedMain, endSemi = false)).stripBreaks :: ( exprt match case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" case N => doc"" @@ -605,8 +605,13 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: reserveNames(p) lazy val imps = p.imports.map: i => doc"""${getVar(i._1, N)} = await import("${i._2.toString}").then(m => m.default ?? m);""" - blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVarsNoScoped.toSeq) -> - (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) + p.main match + case Scoped(syms, body) => + blockPreamble(p.imports.map(_._1).toSeq ++ syms.toSeq) -> + (imps.mkDocument(doc" # ") :/: block(body, endSemi = false).stripBreaks) + case _ => + blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVarsNoScoped.toSeq) -> + (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) def genLetDecls(vars: Iterator[(Symbol, Str)], isScoped: Bool): Document = if vars.isEmpty then doc"" else diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index 07725a0283..f75cc10893 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,18 +81,18 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod(); return tmp4 }; +//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod1(); return tmp4 }; :fixme :expect 42 f -//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod cannot be invoked without 'new' //│ ═══[RUNTIME ERROR] Expected: '42', got: 'undefined' :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; /** scoped **/ f6 = function f() { return TrmMod.class }; +//│ let f6; /** scoped **/ f6 = function f() { return TrmMod1.class }; fun assertModule(module m: ClsMod): module ClsMod = m @@ -104,7 +104,7 @@ assertModule(TypMod).whoami :fixme assertModule(TrmMod).whoami -//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod cannot be invoked without 'new' fun assertNonModule(m) = m @@ -124,7 +124,7 @@ assertNonModule(TypMod).whoami :breakme :fixme assertNonModule(TrmMod).whoami -//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor TrmMod cannot be invoked without 'new' type Foo[A] = Int diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index 9ac7a2e8e9..0861dde7b5 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -140,7 +140,7 @@ let c = new Cls(123) //│ ║ l.138: let c = new Cls(123) //│ ║ ^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ═══[RUNTIME ERROR] TypeError: Cls is not a constructor +//│ ═══[RUNTIME ERROR] TypeError: Cls1 is not a constructor //│ c = undefined c.x @@ -150,8 +150,8 @@ c.x class Foo' with class Bar' -//│ > let Foo$_;try { globalThis.Object.freeze(class Foo$_1 { static { Foo$_ = this } constructor() { const this$Foo$_ = this; globalThis.Object.freeze(class Bar$_ { static { this$Foo$_.Bar' = this } constructor() {} toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Bar'"]; }); } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Foo'"]; }); block$res20 = undefined; } catch (e) { console.log('\u200B' + e + '\u200B'); } -//│ > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +//│ > let Foo$_1;try { globalThis.Object.freeze(class Foo$_ { static { Foo$_1 = this } constructor() { const this$Foo$_ = this; globalThis.Object.freeze(class Bar$_ { static { this$Foo$_.Bar' = this } constructor() {} toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Bar'"]; }); } toString() { return runtime.render(this); } static [definitionMetadata] = ["class", "Foo'"]; }); block$res20 = undefined; } catch (e) { console.log('\u200B' + e + '\u200B'); } +//│ > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Unexpected string // ——— ——— ——— @@ -208,7 +208,7 @@ class D extends id(C) //│ D1 = this //│ } //│ constructor() { -//│ super(C); +//│ super(C1); //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "D"]; @@ -341,7 +341,7 @@ if Foo(1, 2, 3) is Foo(arg) then arg module M with val name = "Student" -//│ ═══[RUNTIME ERROR] TypeError: Cannot assign to read only property 'name' of function 'class M1 { static { M = this } constructor() { runtime.Unit; } static { this.name = "Stud...... }' +//│ ═══[RUNTIME ERROR] TypeError: Cannot assign to read only property 'name' of function 'class M { static { M1 = this } constructor() { runtime.Unit; } static { this.name = "Stud...... }' // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls index 417c7de2ad..315fe478ee 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls @@ -47,14 +47,14 @@ Example.foo() + 1 //│ ╔══[ERROR] Unexpected moduleful application of type M. //│ ║ l.46: Example.foo() + 1 //│ ╙── ^^^^^^^^^^^^^ -//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M + 1 //│ ╔══[ERROR] Unexpected moduleful reference of type M. //│ ║ l.53: M + 1 //│ ╙── ^ -//│ = "class M1 { static { M = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M |> id diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index b32f8cbaa6..879780f51f 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let tmp1, Foo1; /** scoped **/ +//│ let Foo1, tmp1; /** scoped **/ //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls index c045045957..7a7a7d299d 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Functions.mls @@ -14,10 +14,10 @@ module foo with //│ ║ ^^^^^^^^^^^^^^^ //│ ║ l.8: val zero = foo(0) //│ ╙── ^^^^^^^^^^^^^^^^^^^ -//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo cannot be invoked without 'new' :fixme foo(123) -//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor foo cannot be invoked without 'new' diff --git a/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls b/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls index 8001ff0155..2353a93c51 100644 --- a/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls +++ b/hkmc2/shared/src/test/mlscript/basics/DynamicInstantiation.mls @@ -6,7 +6,7 @@ class C :sjs new! C //│ JS (unsanitized): -//│ globalThis.Object.freeze(new C()) +//│ globalThis.Object.freeze(new C1()) //│ = C let c = C @@ -38,7 +38,7 @@ new! id(C) //│ rhs = Tup of Ls of //│ Ident of "C" //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Predef.id(C)) +//│ globalThis.Object.freeze(new Predef.id(C1)) //│ ═══[RUNTIME ERROR] TypeError: Predef.id is not a constructor // * Ok (as in JS) diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index 08805e2040..b0027fd8b2 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,11 +51,11 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let tmp, Baz1; /** scoped **/ +//│ let Baz1, tmp; /** scoped **/ //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; -//│ globalThis.Object.freeze(class Baz extends Bar2.class { +//│ globalThis.Object.freeze(class Baz extends Bar3.class { //│ static { //│ Baz1.class = this //│ } diff --git a/hkmc2/shared/src/test/mlscript/basics/New.mls b/hkmc2/shared/src/test/mlscript/basics/New.mls index f774bb4d4d..2bd38e7674 100644 --- a/hkmc2/shared/src/test/mlscript/basics/New.mls +++ b/hkmc2/shared/src/test/mlscript/basics/New.mls @@ -36,7 +36,7 @@ new //│ ║ l.34: Foo() //│ ║ ^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo cannot be invoked without 'new' :e new diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index 790005a3c5..fb241cc248 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo()); +//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo1()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = new Foo(); +//│ let f1; /** scoped **/ f1 = new Foo1(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls b/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls index aa7506e715..303968c6b4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ObjectExtensions.mls @@ -9,11 +9,11 @@ new Foo //│ ║ l.7: new Foo //│ ║ ^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. -//│ ═══[RUNTIME ERROR] TypeError: Foo is not a constructor +//│ ═══[RUNTIME ERROR] TypeError: Foo1 is not a constructor :re new! Foo -//│ ═══[RUNTIME ERROR] TypeError: Foo is not a constructor +//│ ═══[RUNTIME ERROR] TypeError: Foo1 is not a constructor :fixme // Support? new! Foo.class diff --git a/hkmc2/shared/src/test/mlscript/basics/Overloading.mls b/hkmc2/shared/src/test/mlscript/basics/Overloading.mls index 93f7395114..ba9d7b1d7d 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Overloading.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Overloading.mls @@ -19,6 +19,6 @@ Foo :todo Foo() -//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo1 cannot be invoked without 'new' +//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo cannot be invoked without 'new' diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 81c6dbe260..4091bd3623 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -81,7 +81,7 @@ data class Foo(x: Int) :sjs new Foo(42) //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Foo.class(42)) +//│ globalThis.Object.freeze(new Foo1.class(42)) //│ = Foo(42) //│ Type: Foo @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo.class(42)); foo.x +//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo1.class(42)); foo.x //│ = 42 //│ Type: Int @@ -135,8 +135,8 @@ let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): //│ let foo1; /** scoped **/ -//│ foo1 = globalThis.Object.freeze(new Foo2.class()); -//│ if (foo1 instanceof Foo2.class) { 1 } else { 0 } +//│ foo1 = globalThis.Object.freeze(new Foo3.class()); +//│ if (foo1 instanceof Foo3.class) { 1 } else { 0 } //│ = 1 //│ foo = Foo() //│ Type: Int diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index 9a97ca3050..d7ff1e8b8d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let tmp, single1; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls b/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls index 93a395f5a0..e2a1c73393 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadOpen.mls @@ -32,7 +32,7 @@ open Foo { y } :sjs y //│ JS (unsanitized): -//│ Foo.y +//│ Foo1.y val Oops = "oops" diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 8be8e2459a..040882fb73 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -18,22 +18,22 @@ fun test(x) = //│ let test; /** scoped **/ //│ test = function test(x) { //│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ -//│ if (x instanceof Some.class) { +//│ if (x instanceof Some1.class) { //│ argument0$1 = x.value; //│ v = argument0$1; //│ tmp = v + 1; -//│ tmp1 = Some(tmp); -//│ } else if (x instanceof None.class) { -//│ tmp1 = None; +//│ tmp1 = Some1(tmp); +//│ } else if (x instanceof None1.class) { +//│ tmp1 = None1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ scrut = tmp1; -//│ if (scrut instanceof Some.class) { +//│ if (scrut instanceof Some1.class) { //│ argument0$ = scrut.value; //│ v1 = argument0$; //│ return Predef.print(v1) -//│ } else if (scrut instanceof None.class) { +//│ } else if (scrut instanceof None1.class) { //│ return Predef.print("none") //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 2c5e5cee00..6e09f1ea71 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -87,9 +87,9 @@ val isDefined = case //│ JS (unsanitized): //│ let isDefined, lambda12; /** scoped **/ //│ lambda12 = (undefined, function (caseScrut) { -//│ if (caseScrut instanceof Some.class) { +//│ if (caseScrut instanceof Some1.class) { //│ return true -//│ } else if (caseScrut instanceof None.class) { +//│ } else if (caseScrut instanceof None1.class) { //│ return false //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index b64f7a1a8d..6c679f4917 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -10,8 +10,8 @@ object None if Some(0) is Some(x) then x //│ JS (unsanitized): //│ let scrut, x, argument0$; /** scoped **/ -//│ scrut = Some(0); -//│ if (scrut instanceof Some.class) { +//│ scrut = Some1(0); +//│ if (scrut instanceof Some1.class) { //│ argument0$ = scrut.value; //│ x = argument0$; //│ x @@ -27,7 +27,7 @@ if s is Some(x) then x //│ JS (unsanitized): //│ let x1, argument0$1; /** scoped **/ -//│ if (s instanceof Some.class) { +//│ if (s instanceof Some1.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; //│ x1 @@ -56,7 +56,7 @@ x => if x is Some(x) then x //│ let lambda; //│ lambda = (undefined, function (x3) { //│ let x4, argument0$4; /** scoped **/ -//│ if (x3 instanceof Some.class) { +//│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; //│ return x4 @@ -123,7 +123,7 @@ fun f(x) = if x is //│ let x4, scrut1, argument0$4, tmp5; /** scoped **/ //│ split_root$1: { //│ split_1$: { -//│ if (x3 instanceof Some.class) { +//│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; //│ scrut1 = x4 > 0; @@ -133,7 +133,7 @@ fun f(x) = if x is //│ } else { //│ break split_1$ //│ } -//│ } else if (x3 instanceof None.class) { +//│ } else if (x3 instanceof None1.class) { //│ tmp5 = "ok"; //│ break split_root$1 //│ } else { break split_1$ } @@ -167,11 +167,11 @@ fun f(x) = if x is //│ let f5; /** scoped **/ //│ f5 = function f(x3) { //│ let u, a, b, argument0$4, argument1$; /** scoped **/ -//│ if (x3 instanceof Some.class) { +//│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; //│ return u -//│ } else if (x3 instanceof Pair.class) { +//│ } else if (x3 instanceof Pair1.class) { //│ argument0$4 = x3.fst; //│ argument1$ = x3.snd; //│ b = argument1$; @@ -199,7 +199,7 @@ fun f(x) = print of if x is //│ let argument0$4, tmp9; /** scoped **/ //│ split_root$1: { //│ split_1$: { -//│ if (x3 instanceof Some.class) { +//│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ if (argument0$4 === 0) { //│ tmp9 = "0"; @@ -207,7 +207,7 @@ fun f(x) = print of if x is //│ } else { //│ break split_1$ //│ } -//│ } else if (x3 instanceof None.class) { +//│ } else if (x3 instanceof None1.class) { //│ tmp9 = "ok"; //│ break split_root$1 //│ } else { break split_1$ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index 2286295234..c2ef84645e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let scrut, foo1; /** scoped **/ +//│ let foo1, scrut; /** scoped **/ //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls b/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls index 9429c32a49..d213c06158 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunnyOpen.mls @@ -67,6 +67,6 @@ open Mod { printt, |>> } //│ ╔══[ERROR] Module 'Mod' does not contain member '|>>' //│ ║ l.66: 12 |>> printt //│ ╙── ^^^ -//│ ═══[RUNTIME ERROR] TypeError: Mod.|>> is not a function +//│ ═══[RUNTIME ERROR] TypeError: Mod1.|>> is not a function diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 033d71e19c..48913d17dc 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -126,7 +126,7 @@ module M with :sjs M.t //│ JS (unsanitized): -//│ M.t +//│ M1.t //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index 379380d95c..ffe988ac02 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -39,7 +39,7 @@ print(Test) :sjs Test.foo() //│ JS (unsanitized): -//│ Test.foo() +//│ Test1.foo() //│ > Test { x: 12, class: object Test } //│ = 12 @@ -84,9 +84,9 @@ module Test with //│ ║ l.78: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: -//│ globalThis.Object.freeze(class Test4 { +//│ globalThis.Object.freeze(class Test3 { //│ static { -//│ Test3 = this +//│ Test4 = this //│ } //│ constructor() { //│ runtime.Unit; @@ -94,7 +94,7 @@ module Test with //│ static #x; //│ static { //│ this.x = 1; -//│ Test4.#x = 2; +//│ Test3.#x = 2; //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Test"]; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index 79d3e59992..ca8e1d58ae 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -14,8 +14,8 @@ foo() //│ let foo; /** scoped **/ //│ foo = function foo() { //│ let tmp; /** scoped **/ -//│ tmp = M.concat("a", "b"); -//│ return M.concat(tmp, "c") +//│ tmp = M1.concat("a", "b"); +//│ return M1.concat(tmp, "c") //│ }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index 27a6da03a1..b7bb41de5b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -19,13 +19,13 @@ if a is C then 3 D then 4 //│ JS (unsanitized): -//│ if (a instanceof A.class) { +//│ if (a instanceof A1.class) { //│ 1 -//│ } else if (a instanceof B.class) { +//│ } else if (a instanceof B1.class) { //│ 2 -//│ } else if (a instanceof C.class) { +//│ } else if (a instanceof C1.class) { //│ 3 -//│ } else if (a instanceof D.class) { +//│ } else if (a instanceof D1.class) { //│ 4 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -40,11 +40,11 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ if (a instanceof A.class) { +//│ if (a instanceof A1.class) { //│ 1 -//│ } else if (a instanceof B.class) { +//│ } else if (a instanceof B1.class) { //│ 2 -//│ } else if (a instanceof C.class) { +//│ } else if (a instanceof C1.class) { //│ 3 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -62,18 +62,18 @@ if a is //│ split_root$: { //│ split_default$: { //│ split_1$: { -//│ if (a instanceof A.class) { -//│ if (b instanceof A.class) { +//│ if (a instanceof A1.class) { +//│ if (b instanceof A1.class) { //│ tmp = 1; //│ break split_root$ -//│ } else if (b instanceof B.class) { +//│ } else if (b instanceof B1.class) { //│ break split_1$ //│ } else { //│ break split_default$ //│ } -//│ } else if (a instanceof B.class) { +//│ } else if (a instanceof B1.class) { //│ break split_1$ -//│ } else if (a instanceof C.class) { +//│ } else if (a instanceof C1.class) { //│ tmp = 3; //│ break split_root$ //│ } else { @@ -107,33 +107,33 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let tmp1, x, tmp2, tmp3; /** scoped **/ -//│ if (a instanceof A.class) { -//│ tmp1 = 1; -//│ } else if (a instanceof B.class) { -//│ tmp1 = 2; -//│ } else if (a instanceof C.class) { -//│ tmp1 = 3; +//│ let x, tmp1, tmp2, tmp3; /** scoped **/ +//│ if (a instanceof A1.class) { +//│ tmp2 = 1; +//│ } else if (a instanceof B1.class) { +//│ tmp2 = 2; +//│ } else if (a instanceof C1.class) { +//│ tmp2 = 3; //│ } else { //│ let tmp4; /** scoped **/ -//│ tmp3 = 3; -//│ if (a instanceof D.class) { -//│ if (a instanceof A.class) { -//│ tmp4 = 1 + tmp3; -//│ } else if (a instanceof B.class) { -//│ tmp4 = 2 + tmp3; +//│ tmp1 = 3; +//│ if (a instanceof D1.class) { +//│ if (a instanceof A1.class) { +//│ tmp4 = 1 + tmp1; +//│ } else if (a instanceof B1.class) { +//│ tmp4 = 2 + tmp1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp2 = tmp4; -//│ } else if (a instanceof E.class) { -//│ tmp2 = 5; +//│ tmp3 = tmp4; +//│ } else if (a instanceof E1.class) { +//│ tmp3 = 5; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ tmp1 = Predef.print("done"); +//│ tmp2 = Predef.print("done"); //│ } -//│ x = tmp1; +//│ x = tmp2; //│ Predef.print(x) //│ > 1 //│ x = 1 @@ -149,9 +149,9 @@ if a is //│ JS (unsanitized): //│ let tmp5; /** scoped **/ //│ tmp5 = 2; -//│ if (a instanceof A.class) { +//│ if (a instanceof A1.class) { //│ 1 -//│ } else if (a instanceof B.class) { +//│ } else if (a instanceof B1.class) { //│ 2 + tmp5 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ = 1 @@ -170,11 +170,11 @@ if a is B then 2 + tmp //│ JS (unsanitized): //│ let tmp6; /** scoped **/ -//│ if (a instanceof A.class) { +//│ if (a instanceof A1.class) { //│ 1 //│ } else { //│ tmp6 = printAndId(3); -//│ if (a instanceof B.class) { +//│ if (a instanceof B1.class) { //│ 2 + tmp6 //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ } @@ -193,13 +193,13 @@ if a is print(x) //│ JS (unsanitized): //│ let x1, tmp7; /** scoped **/ -//│ if (a instanceof A.class) { +//│ if (a instanceof A1.class) { //│ 1 -//│ } else if (a instanceof B.class) { +//│ } else if (a instanceof B1.class) { //│ tmp7 = 2; //│ x1 = tmp7; //│ Predef.print(x1) -//│ } else if (a instanceof C.class) { +//│ } else if (a instanceof C1.class) { //│ tmp7 = 3; //│ x1 = tmp7; //│ Predef.print(x1) @@ -221,23 +221,23 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let tmp8, tmp9, tmp10, tmp11, x2, tmp12; /** scoped **/ -//│ if (a instanceof B.class) { +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ if (a instanceof B1.class) { //│ 1 //│ } else { -//│ if (a instanceof A.class) { -//│ tmp12 = 2; -//│ } else if (a instanceof C.class) { -//│ tmp12 = 3; +//│ if (a instanceof A1.class) { +//│ tmp8 = 2; +//│ } else if (a instanceof C1.class) { +//│ tmp8 = 3; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } -//│ x2 = tmp12; -//│ tmp8 = Predef.print(x2); -//│ tmp9 = x2 + 1; -//│ tmp10 = Predef.print(tmp9); -//│ tmp11 = x2 + 2; -//│ Predef.print(tmp11) +//│ x2 = tmp8; +//│ tmp9 = Predef.print(x2); +//│ tmp10 = x2 + 1; +//│ tmp11 = Predef.print(tmp10); +//│ tmp12 = x2 + 2; +//│ Predef.print(tmp12) //│ } //│ > 2 //│ > 3 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index e2bda5c876..ddff7a89fe 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -8,13 +8,13 @@ module Example with :ssjs Example.f(123) //│ JS: -//│ block$res2 = runtime.checkCall(Example.f(123)); undefined +//│ block$res2 = runtime.checkCall(Example1.f(123)); undefined //│ = [123, 456] :ssjs let s = Example.f //│ JS: -//│ selRes = Example.f; +//│ selRes = Example1.f; //│ if (selRes === undefined) { //│ throw globalThis.Object.freeze(new globalThis.Error("Access to required field 'f' yielded 'undefined'")) //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index 2d2522364b..bcc15a4dce 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -19,15 +19,15 @@ module None :sjs None //│ JS (unsanitized): -//│ None +//│ None1 //│ = class None :sjs :re None() //│ JS (unsanitized): -//│ runtime.safeCall(None()) -//│ ═══[RUNTIME ERROR] TypeError: Class constructor None1 cannot be invoked without 'new' +//│ runtime.safeCall(None1()) +//│ ═══[RUNTIME ERROR] TypeError: Class constructor None cannot be invoked without 'new' :e new None @@ -44,7 +44,7 @@ new! None //│ ║ l.42: new! None //│ ╙── ^^^^ //│ JS (unsanitized): -//│ globalThis.Object.freeze(new None()) +//│ globalThis.Object.freeze(new None1()) //│ = None @@ -141,7 +141,7 @@ module M with :sjs M.m //│ JS (unsanitized): -//│ M2.m +//│ M3.m //│ = class M { m: ref'1 } as ref'1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 9f2d151267..9544421bdd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -78,7 +78,7 @@ Some :sjs None //│ JS (unsanitized): -//│ Option2.None +//│ Option3.None //│ = 123 diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 3306b06107..9edf77ba6a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -12,9 +12,9 @@ fun isDefined(x) = if x is //│ JS (unsanitized): //│ let isDefined; /** scoped **/ //│ isDefined = function isDefined(x) { -//│ if (x instanceof Some.class) { +//│ if (x instanceof Some1.class) { //│ return true -//│ } else if (x instanceof None.class) { +//│ } else if (x instanceof None1.class) { //│ return false //│ } else { throw globalThis.Object.freeze(new globalThis.Error("match error")) } //│ }; @@ -31,13 +31,13 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let lambda, isDefined1; /** scoped **/ +//│ let isDefined1, lambda; /** scoped **/ //│ lambda = (undefined, function (caseScrut) { //│ let argument0$; /** scoped **/ -//│ if (caseScrut instanceof Some.class) { +//│ if (caseScrut instanceof Some1.class) { //│ argument0$ = caseScrut.value; //│ return true -//│ } else if (caseScrut instanceof None.class) { +//│ } else if (caseScrut instanceof None1.class) { //│ return false //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 5c512eb5e1..2e93a38133 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -21,17 +21,17 @@ data class Foo() Foo //│ JS (unsanitized): -//│ Foo +//│ Foo1 //│ = fun Foo { class: class Foo } Foo() //│ JS (unsanitized): -//│ Foo() +//│ Foo1() //│ = Foo() Foo.class //│ JS (unsanitized): -//│ Foo.class +//│ Foo1.class //│ = class Foo @@ -54,23 +54,23 @@ data class Foo(a) Foo //│ JS (unsanitized): -//│ Foo2 +//│ Foo3 //│ = fun Foo { class: class Foo } Foo(1) //│ JS (unsanitized): -//│ Foo2(1) +//│ Foo3(1) //│ = Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Foo2(1); tmp.a +//│ let tmp; /** scoped **/ tmp = Foo3(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = function foo(y) { return Foo2(y) }; foo(27) +//│ let foo; /** scoped **/ foo = function foo(y) { return Foo3(y) }; foo(27) //│ = Foo(27) @@ -94,7 +94,7 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = Foo4; +//│ let foo1; /** scoped **/ foo1 = Foo5; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; /** scoped **/ f2 = Foo4(1, 2); +//│ let f2; /** scoped **/ f2 = Foo5(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ tmp2 = Predef.print(1); tmp1 = Predef.print(2); Foo4(tmp2, tmp1) +//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo5(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner.class(100)); +//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner1.class(100)); //│ > 100 //│ i = Inner(100) @@ -224,7 +224,7 @@ class Foo(x, val y, z, val z, z) with Foo(10, 20, 30, 40, 50) //│ JS (unsanitized): -//│ Foo6(10, 20, 30, 40, 50) +//│ Foo7(10, 20, 30, 40, 50) //│ > x = 1 //│ > y = 21 //│ > this.y = 20 @@ -258,7 +258,7 @@ class Foo(val z, val z) Foo(1, 2) //│ JS (unsanitized): -//│ Foo8(1, 2) +//│ Foo9(1, 2) //│ = Foo(undefined, undefined) diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index 19591829d2..eb2618f1fd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -18,36 +18,36 @@ class Foo Foo is Foo //│ JS (unsanitized): -//│ if (Foo instanceof Foo) { true } else { false } +//│ if (Foo1 instanceof Foo1) { true } else { false } //│ = false (new Foo) is Foo //│ JS (unsanitized): //│ let scrut; /** scoped **/ -//│ scrut = globalThis.Object.freeze(new Foo()); -//│ if (scrut instanceof Foo) { true } else { false } +//│ scrut = globalThis.Object.freeze(new Foo1()); +//│ if (scrut instanceof Foo1) { true } else { false } //│ = true new Foo //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Foo()) +//│ globalThis.Object.freeze(new Foo1()) //│ = Foo new Foo() //│ JS (unsanitized): -//│ globalThis.Object.freeze(new Foo()) +//│ globalThis.Object.freeze(new Foo1()) //│ = Foo Foo //│ JS (unsanitized): -//│ Foo +//│ Foo1 //│ = class Foo :re Foo() //│ JS (unsanitized): -//│ runtime.safeCall(Foo()) -//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo1 cannot be invoked without 'new' +//│ runtime.safeCall(Foo1()) +//│ ═══[RUNTIME ERROR] TypeError: Class constructor Foo cannot be invoked without 'new' data class Foo with { print("hi") } @@ -218,7 +218,7 @@ print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): //│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ -//│ a = globalThis.Object.freeze(new Foo9()); +//│ a = globalThis.Object.freeze(new Foo10()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); //│ tmp2 = Predef.print(tmp1); diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 2cfe39c612..2dfe46945b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -72,7 +72,11 @@ id(f)(3)(4) :noSanityCheck let f = (x, y) => x + y in f(2) //│ JS: -//│ f4 = function f(x, y) { return x + y }; f3 = f4; block$res5 = runtime.safeCall(f3(2)); undefined +//│ let f4; +//│ f4 = function f(x, y) { return x + y }; +//│ f3 = f4; +//│ block$res5 = runtime.safeCall(f3(2)); +//│ undefined //│ = NaN :ssjs @@ -80,6 +84,7 @@ let f = (x, y) => x + y in f(2) let f = (x, y) => x + y f(2) //│ JS: +//│ let f6; //│ f6 = function f(...args) {{ //│ runtime.checkArgs("f", 2, true, args.length); //│ let x = args[0]; @@ -101,12 +106,12 @@ data class Cls(x, y) with fun f(z, p) = x + y + z + p id(Cls(1, 2)).f(3) //│ JS: -//│ Cls = function Cls(x, y) { +//│ Cls1 = function Cls(x, y) { //│ return globalThis.Object.freeze(new Cls.class(x, y)); //│ }; -//│ globalThis.Object.freeze(class Cls1 { +//│ globalThis.Object.freeze(class Cls { //│ static { -//│ Cls.class = this +//│ Cls1.class = this //│ } //│ constructor(x, y) { //│ this.x = x; @@ -121,7 +126,7 @@ id(Cls(1, 2)).f(3) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; //│ }); -//│ tmp4 = Cls(1, 2); +//│ tmp4 = Cls1(1, 2); //│ tmp5 = Predef.id(tmp4); //│ block$res7 = runtime.safeCall(tmp5.f(3)); //│ undefined @@ -133,12 +138,12 @@ data class Cls(x, y) with fun f(z, p) = x + y + z + p id(Cls(1, 2)).f(3) //│ JS: -//│ Cls2 = function Cls(...args) { +//│ Cls3 = function Cls(...args) { //│ return globalThis.Object.freeze(new Cls.class(...args)); //│ }; -//│ globalThis.Object.freeze(class Cls3 { +//│ globalThis.Object.freeze(class Cls2 { //│ static { -//│ Cls2.class = this +//│ Cls3.class = this //│ } //│ constructor(x, y) { //│ this.x = x; @@ -158,7 +163,7 @@ id(Cls(1, 2)).f(3) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; //│ }); -//│ tmp6 = runtime.checkCall(Cls2(1, 2)); +//│ tmp6 = runtime.checkCall(Cls3(1, 2)); //│ tmp7 = runtime.checkCall(Predef.id(tmp6)); //│ block$res8 = runtime.safeCall(tmp7.f(3)); //│ undefined @@ -171,12 +176,12 @@ data class Cls(x, y) with fun f(z, p)(q, s) = x + y + z + p + q + s id(Cls(1, 2)).f(3, 4)(5) //│ JS: -//│ Cls4 = function Cls(...args) { +//│ Cls5 = function Cls(...args) { //│ return globalThis.Object.freeze(new Cls.class(...args)); //│ }; -//│ globalThis.Object.freeze(class Cls5 { +//│ globalThis.Object.freeze(class Cls4 { //│ static { -//│ Cls4.class = this +//│ Cls5.class = this //│ } //│ constructor(x, y) { //│ this.x = x; @@ -204,7 +209,7 @@ id(Cls(1, 2)).f(3, 4)(5) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; //│ }); -//│ tmp8 = runtime.checkCall(Cls4(1, 2)); +//│ tmp8 = runtime.checkCall(Cls5(1, 2)); //│ tmp9 = runtime.checkCall(Predef.id(tmp8)); //│ tmp10 = tmp9.f(3, 4); //│ block$res9 = runtime.safeCall(tmp10(5)); @@ -257,9 +262,9 @@ if M.A(1).y is x and x == 1 then x else 0 //│ JS: -//│ globalThis.Object.freeze(class M1 { +//│ globalThis.Object.freeze(class M { //│ static { -//│ M = this +//│ M1 = this //│ } //│ constructor() { //│ runtime.Unit; @@ -270,7 +275,7 @@ if M.A(1).y is //│ }; //│ globalThis.Object.freeze(class A { //│ static { -//│ M1.A.class = this +//│ M.A.class = this //│ } //│ constructor(x1) { //│ this.x = x1; @@ -289,7 +294,7 @@ if M.A(1).y is //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ tmp11 = runtime.checkCall(M.A(1)); +//│ tmp11 = runtime.checkCall(M1.A(1)); //│ selRes2 = tmp11.y; //│ discarded2 = tmp11.y$__checkNotMethod; //│ if (selRes2 === undefined) { @@ -313,7 +318,7 @@ M.A(1).y //│ if (selRes3 === undefined) { //│ throw globalThis.Object.freeze(new globalThis.Error("Access to required field 'console' yielded 'undefined'")) //│ } -//│ tmp12 = runtime.checkCall(M.A(1)); +//│ tmp12 = runtime.checkCall(M1.A(1)); //│ selRes4 = tmp12.y; //│ discarded4 = tmp12.y$__checkNotMethod; //│ if (selRes4 === undefined) { @@ -330,7 +335,7 @@ M.A(1).y M.A(1).y console.log() //│ JS: -//│ tmp13 = M.A(1); block$res15 = runtime.safeCall(globalThis.console.log(tmp13.y)); undefined +//│ tmp13 = M1.A(1); block$res15 = runtime.safeCall(globalThis.console.log(tmp13.y)); undefined //│ > undefined :noSanityCheck @@ -346,7 +351,7 @@ M.A(2).y > 1 :ssjs M.A(1).g(0) //│ JS: -//│ tmp16 = runtime.checkCall(M.A(1)); block$res18 = runtime.safeCall(tmp16.g(0)); undefined +//│ tmp16 = runtime.checkCall(M1.A(1)); block$res18 = runtime.safeCall(tmp16.g(0)); undefined //│ ═══[RUNTIME ERROR] TypeError: tmp16.g is not a function @@ -354,7 +359,7 @@ M.A(1).g(0) :ssjs M.A(1).f(0) //│ JS: -//│ tmp17 = runtime.checkCall(M.A(1)); block$res19 = runtime.checkCall(tmp17.f(0)); undefined +//│ tmp17 = runtime.checkCall(M1.A(1)); block$res19 = runtime.checkCall(tmp17.f(0)); undefined //│ = 1 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls index 7bfeb7930c..a049776664 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls @@ -88,7 +88,7 @@ fun f() = if x is //│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { //│ split_default$: { -//│ if (x instanceof A.class) { +//│ if (x instanceof A1.class) { //│ argument0$ = x.a; //│ argument1$ = x.b; //│ b = argument1$; @@ -100,7 +100,7 @@ fun f() = if x is //│ } else { //│ break split_default$ //│ } -//│ } else if (x instanceof B.class) { +//│ } else if (x instanceof B1.class) { //│ argument0$ = x.a; //│ argument1$ = x.b; //│ d = argument1$; @@ -126,12 +126,12 @@ fun f() = if A(1, Nil) is //│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ //│ split_root$: { //│ split_default$: { -//│ scrut = A(1, Nil); -//│ if (scrut instanceof A.class) { +//│ scrut = A1(1, Nil1); +//│ if (scrut instanceof A1.class) { //│ argument0$ = scrut.a; //│ argument1$ = scrut.b; //│ if (argument0$ === 1) { -//│ if (argument1$ instanceof Nil.class) { +//│ if (argument1$ instanceof Nil1.class) { //│ tmp1 = 3; //│ break split_root$ //│ } else { @@ -165,13 +165,13 @@ fun f(x) = //│ let tmp1; /** scoped **/ //│ tmp2: while (true) { //│ let a, a1, argument0$, argument1$; /** scoped **/ -//│ if (x1 instanceof A.class) { +//│ if (x1 instanceof A1.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a = argument0$; //│ tmp1 = runtime.safeCall(a()); //│ continue tmp2 -//│ } else if (x1 instanceof B.class) { +//│ } else if (x1 instanceof B1.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; //│ a1 = argument0$; @@ -204,7 +204,7 @@ fun f() = //│ let a, z, scrut, scrut1, argument0$, argument1$, tmp3; /** scoped **/ //│ split_root$: { //│ split_1$: { -//│ if (y1 instanceof A.class) { +//│ if (y1 instanceof A1.class) { //│ argument0$ = y1.a; //│ argument1$ = y1.b; //│ a = argument1$; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index 3bb6a4a271..f68cf9062b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = call(Example, oops); runtime.safeCall(tmp(2)) +//│ let tmp; /** scoped **/ tmp = call(Example1, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example, tmp22) +//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example1, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 993741a4b1..63ea5b79ad 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -75,18 +75,18 @@ fun toInt(x) = if x is //│ > | CALL toString() //│ > | | CALL toString() //│ > | | => 'N: 0' -//│ > | | CALL succ(N1 {}) -//│ > | | => S1 { x: N1 {} } -//│ > | | CALL toInt(S1 { x: N1 {} }) -//│ > | | | CALL toInt(N1 {}) +//│ > | | CALL succ(N {}) +//│ > | | => S { x: N {} } +//│ > | | CALL toInt(S { x: N {} }) +//│ > | | | CALL toInt(N {}) //│ > | | | => 0 //│ > | | => 1 //│ > | => 'S(N: 0): 1' -//│ > | CALL succ(S1 { x: N1 {} }) -//│ > | => S1 { x: S1 { x: N1 {} } } -//│ > | CALL toInt(S1 { x: S1 { x: N1 {} } }) -//│ > | | CALL toInt(S1 { x: N1 {} }) -//│ > | | | CALL toInt(N1 {}) +//│ > | CALL succ(S { x: N {} }) +//│ > | => S { x: S { x: N {} } } +//│ > | CALL toInt(S { x: S { x: N {} } }) +//│ > | | CALL toInt(S { x: N {} }) +//│ > | | | CALL toInt(N {}) //│ > | | | => 0 //│ > | | => 1 //│ > | => 2 @@ -105,7 +105,7 @@ id(2) //│ > CALL id(2) //│ > | CALL id(1) //│ > | | CALL id(0) -//│ > | | => Cls1 { +//│ > | | => Cls { //│ > a: 'aaaa', //│ > b: 'bbbb', //│ > c: 'cccc', diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index ebee1cd3e2..1a52c71596 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -203,7 +203,7 @@ fun f(ls) = //│ let tmp28; /** scoped **/ //│ tmp29: while (true) { //│ let tl, h, argument0$, argument1$; /** scoped **/ -//│ if (ls instanceof Cons.class) { +//│ if (ls instanceof Cons1.class) { //│ argument0$ = ls.hd; //│ argument1$ = ls.tl; //│ tl = argument1$; diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 9452424ea9..2ee44be02f 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -111,7 +111,7 @@ class Foo(using T) with //│ } //│ constructor(tmp5) { //│ this.#tmp = tmp5; -//│ Predef.print(T); +//│ Predef.print(T1); //│ } //│ #tmp; //│ toString() { return runtime.render(this); } diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index af13982975..6ad3dc746e 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -161,7 +161,7 @@ if true do //│ handleBlock$11 = function handleBlock$() { //│ let f, scrut, Cont$handleBlock$h$11, doUnwind; /** scoped **/ //│ let h, res, Handler$h$12; -//│ globalThis.Object.freeze(class Handler$h$11 extends Effect { +//│ globalThis.Object.freeze(class Handler$h$11 extends Effect1 { //│ static { //│ Handler$h$12 = this //│ } diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index 91fc3114e2..cba16a16ad 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -162,7 +162,7 @@ str //│ if (scrut === true) { //│ handleBlock$7 = function handleBlock$() { //│ let h1, handleBlock$8, Cont$handleBlock$h1$2, doUnwind, Handler$h1$2; -//│ globalThis.Object.freeze(class Handler$h1$1 extends Effect { +//│ globalThis.Object.freeze(class Handler$h1$1 extends Effect1 { //│ static { //│ Handler$h1$2 = this //│ } @@ -249,7 +249,7 @@ str //│ }; //│ handleBlock$8 = function handleBlock$() { //│ let h2, res7, Cont$handleBlock$h2$2, doUnwind1, Handler$h2$2; -//│ globalThis.Object.freeze(class Handler$h2$1 extends Effect { +//│ globalThis.Object.freeze(class Handler$h2$1 extends Effect1 { //│ static { //│ Handler$h2$2 = this //│ } diff --git a/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls b/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls index c6aa169f29..58a58516ee 100644 --- a/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls +++ b/hkmc2/shared/src/test/mlscript/interop/CtorBypass.mls @@ -23,7 +23,7 @@ let b = Object.create(A.prototype) :sjs b is A //│ JS (unsanitized): -//│ if (b instanceof A) { true } else { false } +//│ if (b instanceof A1) { true } else { false } //│ = true :re diff --git a/hkmc2/shared/src/test/mlscript/interop/Objects.mls b/hkmc2/shared/src/test/mlscript/interop/Objects.mls index 96898b9208..5fd07cbc93 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Objects.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Objects.mls @@ -34,14 +34,14 @@ Foo.name //│ = "Foo" Foo.class.name -//│ = "Foo1" +//│ = "Foo" foo.constructor //│ = class Foo // ! will be changed by minification foo.constructor.name -//│ = "Foo1" +//│ = "Foo" typeof(foo) //│ = "object" diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 47a6d9147d..40b369eb47 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -74,7 +74,7 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let tmp1, A3; /** scoped **/ +//│ let A3, tmp1; /** scoped **/ //│ let g, g$; //│ g$ = function g$(A$instance) { //│ return 2 diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index 783e6d19df..dbb5921435 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons(tmp, 2) +//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons1(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Cons(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; /** scoped **/ tmp1 = Cons1(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 72036ed9d4..1bc365e3c9 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -62,17 +62,17 @@ fun crazy(v) = //│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ //│ split_root$: { //│ split_1$: { -//│ if (v instanceof S.class) { +//│ if (v instanceof S1.class) { //│ argument0$ = v.value; -//│ if (argument0$ instanceof S.class) { +//│ if (argument0$ instanceof S1.class) { //│ argument0$1 = argument0$.value; -//│ if (argument0$1 instanceof S.class) { +//│ if (argument0$1 instanceof S1.class) { //│ argument0$2 = argument0$1.value; -//│ if (argument0$2 instanceof S.class) { +//│ if (argument0$2 instanceof S1.class) { //│ argument0$3 = argument0$2.value; -//│ if (argument0$3 instanceof S.class) { +//│ if (argument0$3 instanceof S1.class) { //│ argument0$4 = argument0$3.value; -//│ if (argument0$4 instanceof S.class) { +//│ if (argument0$4 instanceof S1.class) { //│ argument0$5 = argument0$4.value; //│ if (argument0$5 === 0) { //│ tmp = "bruh!"; @@ -99,12 +99,12 @@ fun crazy(v) = //│ break split_1$ //│ } //│ } -//│ tmp1 = S(0); -//│ tmp2 = S(tmp1); -//│ tmp3 = S(tmp2); -//│ tmp4 = S(tmp3); -//│ tmp5 = S(tmp4); -//│ tmp = S(tmp5); +//│ tmp1 = S1(0); +//│ tmp2 = S1(tmp1); +//│ tmp3 = S1(tmp2); +//│ tmp4 = S1(tmp3); +//│ tmp5 = S1(tmp4); +//│ tmp = S1(tmp5); //│ } //│ return tmp //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index 6d88aa0a07..f08820694b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -81,7 +81,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp3, qqq; /** scoped **/ +//│ let qqq, tmp3; /** scoped **/ //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -382,7 +382,7 @@ fun foo(x, y, z) = //│ JS (unsanitized): //│ let foo3; /** scoped **/ //│ foo3 = function foo(x1, y1, z1) { -//│ if (x1 instanceof A.class) { return "Hello" } else { return "Goodbye" } +//│ if (x1 instanceof A1.class) { return "Hello" } else { return "Goodbye" } //│ }; :sjs @@ -406,9 +406,9 @@ fun foo(x, y, z) = //│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_1$4: { -//│ if (x1 instanceof A.class) { -//│ if (y1 instanceof B.class) { -//│ if (z1 instanceof C.class) { +//│ if (x1 instanceof A1.class) { +//│ if (y1 instanceof B1.class) { +//│ if (z1 instanceof C1.class) { //│ tmp6 = "Hello"; //│ break split_root$4 //│ } else { @@ -432,9 +432,9 @@ fun foo(x, y, z) = //│ let tmp6; /** scoped **/ //│ split_root$4: { //│ split_default$: { -//│ if (x1 instanceof A.class) { -//│ if (y1 instanceof B.class) { -//│ if (z1 instanceof C.class) { +//│ if (x1 instanceof A1.class) { +//│ if (y1 instanceof B1.class) { +//│ if (z1 instanceof C1.class) { //│ tmp6 = "Hello"; //│ break split_root$4 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index 04f680fc0b..cfb98a2376 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -14,11 +14,11 @@ x => if x is Pair(A, B) then 1 //│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { -//│ if (x instanceof Pair.class) { +//│ if (x instanceof Pair1.class) { //│ argument0$ = x.a; //│ argument1$ = x.b; -//│ if (argument0$ instanceof A) { -//│ if (argument1$ instanceof B) { +//│ if (argument0$ instanceof A1) { +//│ if (argument1$ instanceof B1) { //│ tmp = 1; //│ break split_root$ //│ } else { @@ -50,18 +50,18 @@ fun f(x) = if x is //│ let argument0$, argument1$, tmp; /** scoped **/ //│ split_root$: { //│ split_default$: { -//│ if (x instanceof Pair.class) { +//│ if (x instanceof Pair1.class) { //│ argument0$ = x.a; //│ argument1$ = x.b; -//│ if (argument0$ instanceof A) { -//│ if (argument1$ instanceof A) { +//│ if (argument0$ instanceof A1) { +//│ if (argument1$ instanceof A1) { //│ tmp = 1; //│ break split_root$ //│ } else { //│ break split_default$ //│ } -//│ } else if (argument0$ instanceof B) { -//│ if (argument1$ instanceof B) { +//│ } else if (argument0$ instanceof B1) { +//│ if (argument1$ instanceof B1) { //│ tmp = 2; //│ break split_root$ //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 697105bd95..45662130ff 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -41,7 +41,7 @@ fun foo(v) = //│ let tmp2; /** scoped **/ //│ split_root$2: { //│ split_1$2: { -//│ if (v instanceof A) { break split_1$2 } else { break split_1$2 } +//│ if (v instanceof A1) { break split_1$2 } else { break split_1$2 } //│ } //│ tmp2 = 0; //│ } diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index daa8dac993..3d8b177c07 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -36,7 +36,7 @@ fun foo(x) = x is Cross //│ let foo; /** scoped **/ //│ foo = function foo(x2) { //│ let unapplyResult, output, bindings; /** scoped **/ -//│ unapplyResult = runtime.safeCall(Cross.unapply(x2)); +//│ unapplyResult = runtime.safeCall(Cross1.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; //│ bindings = unapplyResult.bindings; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index cfe7256b4b..c1486578fd 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -24,7 +24,7 @@ pattern SumPair = Pair(a, b) => a + b //│ return a + b //│ }); //│ transform = lambda; -//│ if (input instanceof Pair.class) { +//│ if (input instanceof Pair1.class) { //│ argument0$ = input.first; //│ argument1$ = input.second; //│ transformResult = runtime.safeCall(transform(argument0$, argument1$)); diff --git a/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls b/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls index 9e5f309f9f..188d640c44 100644 --- a/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls +++ b/hkmc2/shared/src/test/mlscript/ups/regex/Separation.mls @@ -49,4 +49,4 @@ parseIntegers of "123\n456\n789" //│ ╔══[ERROR] Illegal juxtaposition right-hand side (identifier). //│ ║ l.48: "123\n456\n789" through Lines(Integer) // ==> [123, 456, 789] //│ ╙── ^^^^^^^ -//│ ═══[RUNTIME ERROR] TypeError: Lines is not a function +//│ ═══[RUNTIME ERROR] TypeError: Lines1 is not a function diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index 6770f3235f..dae06e5fc6 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -108,24 +108,14 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: with JSBuilderArgNumSanityChecks val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) - - // TODO: remove mutation - var toplvlDefinedVars = collection.Set.empty[Symbol] - def assignResSym(b: Block, toplvl: Boolean): Block = - b.mapTail: + val le = lowered0.copy(main = lowered0.main.mapTail: case e: End => - Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) case Return(res, implct) => assert(implct) Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case s@Scoped(xs, body) => - if toplvl then - toplvlDefinedVars = xs - assignResSym(body, false) - else - Scoped(xs, assignResSym(body, false))(false) case tl: (Throw | Break | Continue) => tl - val le = lowered0.copy(main = assignResSym(lowered0.main, true)) + ) if showLoweredTree.isSet then output(s"Lowered:") output(lowered0.showAsTree) @@ -141,19 +131,9 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: output(s"Pretty Lowered:") output(Printer.mkDocument(le)(using summon[Raise], nestedScp).mkString()) - // NOTE: `blockPreamble` should still take care of those vars that are generated during or after the lowering stage, - // while `Scoped` more reflects the declared vars in the source (or elaborated?) level? - // Or, during the lowering, we should also find the correct tmp vars and add them to the set in `Scoped` - val varsFromScopedStr = - val vars = toplvlDefinedVars.toArray.sortBy(_.uid).map(l => l -> baseScp.allocateName(l)) - (if vars.isEmpty then doc"" else - doc" # let " :: vars.map: (_, nme) => - nme - .toList.mkDocument(", ") - :: doc";").stripBreaks.mkString(100) val (pre, js) = nestedScp.givenIn: jsb.worksheet(le) - val preStr = pre.stripBreaks.mkString(100) + varsFromScopedStr + val preStr = pre.stripBreaks.mkString(100) //+ varsFromScopedStr val jsStr = js.stripBreaks.mkString(100) if showSanitizedJS.isSet then output(s"JS:") From e060b40e420bbf0ffe2660d03875153c3f4a8bb9 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Thu, 4 Dec 2025 16:29:41 +0800 Subject: [PATCH 52/72] Remove dontFlatten field of Scoped and remove some other unexpected changes --- .../src/main/scala/hkmc2/codegen/Block.scala | 47 ++++---- .../hkmc2/codegen/BlockTransformer.scala | 4 +- .../main/scala/hkmc2/codegen/Lowering.scala | 19 ++-- .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../hkmc2/semantics/ucs/Normalization.scala | 2 +- .../mlscript/basics/BadMemberProjections.mls | 19 ++-- .../test/mlscript/basics/BadModuleUses.mls | 4 +- .../src/test/mlscript/codegen/Functions.mls | 2 +- .../test/mlscript/codegen/InlineLambdas.mls | 4 +- .../test/mlscript/codegen/NestedScoped.mls | 47 ++++---- .../shared/src/test/mlscript/codegen/Repl.mls | 2 +- .../test/mlscript/codegen/SanityChecks.mls | 100 ++++++++---------- .../test/scala/hkmc2/JSBackendDiffMaker.scala | 14 +-- 13 files changed, 129 insertions(+), 137 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index f3000ed8a5..9ae2b7d2ff 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -88,7 +88,7 @@ sealed abstract class Block extends Product: // TODO conserve if no changes def mapTail(f: BlockTail => Block): Block = this match - case s @ Scoped(syms, body) => Scoped(syms, body.mapTail(f))(s.dontFlatten) + case Scoped(syms, body) => Scoped(syms, body.mapTail(f)) case b: BlockTail => f(b) case Begin(sub, rst) => Begin(sub, rst.mapTail(f)) case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f)) @@ -204,9 +204,9 @@ sealed abstract class Block extends Product: var defns: List[Defn] = Nil val transformer = new BlockTransformerShallow(SymbolSubst()): override def applyBlock(b: Block): Block = b match - case s@Scoped(syms, blk) => + case Scoped(syms, blk) => val (inner, defs) = blk.floatOutDefnsUntilScoped(ignore, preserve) - Scoped(syms ++ defs.map(_.sym), defs.foldLeft(inner)((acc, defn) => Define(defn, acc)))(s.dontFlatten) + Scoped(syms ++ defs.map(_.sym), defs.foldLeft(inner)((acc, defn) => Define(defn, acc))) case Define(defn, rest) if !ignore(defn) => defn match case v: ValDefn => super.applyBlock(b) case _ => @@ -300,11 +300,11 @@ sealed abstract class Block extends Product: then this else HandleBlock(lhs, res, par, args, cls, newHandlers, newBody, newRest) - case s @ Scoped(syms, body) => + case Scoped(syms, body) => val newBody = body.flatten(k) if newBody is body then this - else Scoped(syms, newBody)(s.dontFlatten) + else new Scoped(syms, newBody) case e: End => k(e) case t: BlockTail => this @@ -332,8 +332,7 @@ case class Break(label: Local) extends BlockTail case class Continue(label: Local) extends BlockTail -// set dontFlatten to true to force a nested Scoped -case class Scoped(syms: collection.Set[Local], body: Block)(val dontFlatten: Bool) extends BlockTail +case class Scoped(syms: collection.Set[Local], body: Block) extends BlockTail // TODO: remove this form? case class Begin(sub: Block, rest: Block) extends Block with ProductWithTail @@ -351,44 +350,44 @@ case class Define(defn: Defn, rest: Block) extends Block with ProductWithTail object Match: def apply(scrut: Path, arms: Ls[Case -> Block], dflt: Opt[Block], rest: Block): Block = rest match - case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, Match(scrut, arms, dflt, body))(false) + case Scoped(syms, body) => Scoped(syms, Match(scrut, arms, dflt, body)) case _ => new Match(scrut, arms, dflt, rest) object Label: def apply(label: Local, loop: Bool, body: Block, rest: Block): Block = rest match - case s @ Scoped(syms, rest) if !s.dontFlatten => Scoped(syms, Label(label, loop, body, rest))(false) + case Scoped(syms, rest) => Scoped(syms, Label(label, loop, body, rest)) case _ => new Label(label, loop, body, rest) object Scoped: - def apply(syms: collection.Set[Local], body: Block)(dontFlatten: Bool): Block = body match - case s @ Scoped(syms2, body) if !s.dontFlatten => - if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body)(dontFlatten) else Scoped(syms ++ syms2, body)(dontFlatten) + def apply(syms: collection.Set[Local], body: Block): Block = body match + case Scoped(syms2, body) => + if syms2.isEmpty && syms.isEmpty then Scoped(Set.empty, body) else Scoped(syms ++ syms2, body) case _ => - if syms.isEmpty && !dontFlatten then body else new Scoped(syms, body)(dontFlatten) + if syms.isEmpty then body else new Scoped(syms, body) object Begin: def apply(sub: Block, rest: Block): Block = (sub, rest) match - case (s1 @ Scoped(symsSub, bodySub), s2 @ Scoped(symsRest, bodyRest)) if !s1.dontFlatten && !s2.dontFlatten => - Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest))(false) - case (s @ Scoped(symsSub, bodySub), _) if !s.dontFlatten => Scoped(symsSub, Begin(bodySub, rest))(false) - case (_, s @ Scoped(symsRest, bodyRest)) if !s.dontFlatten => Scoped(symsRest, Begin(sub, bodyRest))(false) + case (Scoped(symsSub, bodySub), Scoped(symsRest, bodyRest)) => + Scoped(symsSub ++ symsRest, Begin(bodySub, bodyRest)) + case (Scoped(symsSub, bodySub), _) => Scoped(symsSub, Begin(bodySub, rest)) + case (_, Scoped(symsRest, bodyRest)) => Scoped(symsRest, Begin(sub, bodyRest)) case _ => new Begin(sub, rest) object TryBlock: def apply(sub: Block, finallyDo: Block, rest: Block): Block = rest match - case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, TryBlock(sub, finallyDo, body))(false) + case Scoped(syms, body) => Scoped(syms, TryBlock(sub, finallyDo, body)) case _ => new TryBlock(sub, finallyDo, rest) object Assign: def apply(lhs: Local, rhs: Result, rest: Block): Block = rest match - case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, Assign(lhs, rhs, body))(false) + case Scoped(syms, body) => Scoped(syms, Assign(lhs, rhs, body)) case _ => new Assign(lhs, rhs, rest) object AssignField: def apply(lhs: Path, nme: Tree.Ident, rhs: Result, rest: Block)(symbol: Opt[MemberSymbol]): Block = rest match - case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol))(false) + case Scoped(syms, body) => Scoped(syms, AssignField(lhs, nme, rhs, body)(symbol)) case _ => new AssignField(lhs, nme, rhs, rest)(symbol) object AssignDynField: def apply(lhs: Path, fld: Path, arrayIdx: Bool, rhs: Result, rest: Block): Block = rest match - case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body))(false) + case Scoped(syms, body) => Scoped(syms, AssignDynField(lhs, fld, arrayIdx, rhs, body)) case _ => new AssignDynField(lhs, fld, arrayIdx, rhs, rest) object Define: def apply(defn: Defn, rest: Block): Block = rest match - case s @ Scoped(syms, body) if !s.dontFlatten => Scoped(syms, Define(defn, body))(false) + case Scoped(syms, body) => Scoped(syms, Define(defn, body)) case _ => new Define(defn, rest) case class HandleBlock( @@ -414,7 +413,7 @@ object HandleBlock: body: Block, rest: Block ) = rest match - case s @ Scoped(syms, rest) if !s.dontFlatten => + case Scoped(syms, rest) => Scoped( syms, new HandleBlock( @@ -426,7 +425,7 @@ object HandleBlock: handlers, body, rest - ))(false) + )) case _ => new HandleBlock( lhs, res, diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala index 3b8c1fe8a8..c3dc63c211 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/BlockTransformer.scala @@ -95,9 +95,9 @@ class BlockTransformer(subst: SymbolSubst): if (lhs2 is lhs) && (fld2 is fld) && (rhs2 is rhs) && (rest2 is rest) then b else AssignDynField(lhs2, fld2, arrayIdx, rhs2, rest2) - case sc @ Scoped(s, bd) => + case Scoped(s, bd) => val nb = applySubBlock(bd) - if nb is bd then b else Scoped(s, nb)(sc.dontFlatten) + if nb is bd then b else Scoped(s, nb) def applyRcdArg(rcdArg: RcdArg)(k: RcdArg => Block): Block = val RcdArg(idx, p) = rcdArg diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 975d0e27ce..7ca8998a07 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -265,7 +265,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val mtds = methods.map: case (sym, params, split) => val paramLists = params :: Nil - val bodyBlock = inScopedBlock(false)(ucs.Normalization(this)(split)(Ret)) + val bodyBlock = inScopedBlock(ucs.Normalization(this)(split)(Ret)) FunDefn.withFreshSymbol(N, sym, paramLists, bodyBlock)(isTailRec = false) // The return type is intended to be consistent with `gatherMembers` (mtds, Nil, Nil, End()) @@ -547,7 +547,10 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case t if instantiatedResolvedBms.exists(_ is ctx.builtins.scope.locally) => arg match case Tup(Fld(_, Lam(ParamList(_, Nil, N), body), N) :: Nil) => - inScopedBlock(true)(block(Nil, R(body))(k)) + LoweringCtx.nestScoped.givenIn: + val res = block(Nil, R(body))(k) + val scopedSyms = loweringCtx.getCollectedSym + new Begin(new Scoped(scopedSyms, res), End()) case _ => return fail: ErrorReport( @@ -901,7 +904,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): reportAnnotations(decl, annotations) sym val ctor = - inScopedBlock(false): + inScopedBlock: term_nonTail(Blk(clsBody.nonMethods, clsBody.blk.res))(ImplctRet) // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }` .mapTail: @@ -987,7 +990,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): val (imps, funs, rest) = splitBlock(main.stats, Nil, Nil, Nil) val blk = - inScopedBlock(false)(using LoweringCtx.empty): + inScopedBlock(using LoweringCtx.empty): block(funs ::: rest, R(main.res))(ImplctRet) val desug = LambdaRewriter.desugar(blk) @@ -1032,15 +1035,15 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case ps => ps setupFunctionDef(physicalParams, bodyTerm, name) - def inScopedBlock(dontFlatten: Bool)(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = + def inScopedBlock(using LoweringCtx)(mkBlock: LoweringCtx ?=> Block): Block = LoweringCtx.nestScoped.givenIn: val body = mkBlock val scopedSyms = loweringCtx.getCollectedSym - Scoped(scopedSyms, body)(dontFlatten) + Scoped(scopedSyms, body) def setupFunctionDef(paramLists: List[ParamList], bodyTerm: Term, name: Option[Str]) (using LoweringCtx): (List[ParamList], Block) = - val scopedBody = inScopedBlock(false)(returnedTerm(bodyTerm)) + val scopedBody = inScopedBlock(returnedTerm(bodyTerm)) (paramLists, scopedBody) def reportAnnotations(target: Statement, annotations: Ls[Annot]): Unit = @@ -1153,7 +1156,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) go(paramLists.reverse, bod) def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - inScopedBlock(false): + inScopedBlock: val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") val resSym = TempSymbol(N, dbgNme = "traceLogRes") diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index c7b688b079..ed968a587a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -787,7 +787,7 @@ trait JSBuilderArgNumSanityChecks(using Config, Elaborator.State) val restAssign = paramRest match case N => doc"" case S(p) => doc"\nlet $p = $runtimeVar.Tuple.slice($paramsStr, ${params.paramCountLB}, 0);" - (doc"...$paramsStr", braced(doc"$checkArgsNum$paramsAssign$restAssign${this.body(body, endSemi = false)}")) + (doc"...$paramsStr", doc"$checkArgsNum$paramsAssign$restAssign${this.body(body, endSemi = false)}") else super.setupFunction(name, params, body) diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 8dfddeeef2..b1d9c536f8 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -405,7 +405,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e Scoped( LoweringCtx.loweringCtx.getCollectedSym/* ++ inputSplit.definedSyms */, if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - )(false) + ) // Embed the `body` into `Label` if the term is a `while`. lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) val block = diff --git a/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls index 8619f92a40..3873c91b9a 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadMemberProjections.mls @@ -31,12 +31,11 @@ //│ ║ l.24: 1::x() //│ ╙── ^ //│ JS: -//│ lambda1 = (undefined, function (...args1) {{ -//│ runtime.checkArgs("", 1, false, args1.length); -//│ let self = args1[0]; -//│ let args = runtime.Tuple.slice(args1, 1, 0); -//│ return runtime.safeCall(self.x(...args)) -//│ } +//│ lambda1 = (undefined, function (...args1) { +//│ runtime.checkArgs("", 1, false, args1.length); +//│ let self = args1[0]; +//│ let args = runtime.Tuple.slice(args1, 1, 0); +//│ return runtime.safeCall(self.x(...args)) //│ }); //│ block$res2 = runtime.checkCall(lambda1()); //│ undefined @@ -57,24 +56,24 @@ let x = 1 :e "A"::x //│ ╔══[ERROR] String literal is not a known class. -//│ ║ l.58: "A"::x +//│ ║ l.57: "A"::x //│ ║ ^^^ //│ ╟── Note: any expression of the form `‹expression›::‹identifier›` is a member projection; //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ╔══[ERROR] Expected a statically known class; found string literal. -//│ ║ l.58: "A"::x +//│ ║ l.57: "A"::x //│ ╙── ^^^ //│ = fun :e "A" ::x //│ ╔══[ERROR] String literal is not a known class. -//│ ║ l.70: "A" ::x +//│ ║ l.69: "A" ::x //│ ║ ^^^ //│ ╟── Note: any expression of the form `‹expression›::‹identifier›` is a member projection; //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ╔══[ERROR] Expected a statically known class; found string literal. -//│ ║ l.70: "A" ::x +//│ ║ l.69: "A" ::x //│ ╙── ^^^ //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls index 315fe478ee..5b0eae7a0a 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadModuleUses.mls @@ -47,14 +47,14 @@ Example.foo() + 1 //│ ╔══[ERROR] Unexpected moduleful application of type M. //│ ║ l.46: Example.foo() + 1 //│ ╙── ^^^^^^^^^^^^^ -//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M + 1 //│ ╔══[ERROR] Unexpected moduleful reference of type M. //│ ║ l.53: M + 1 //│ ╙── ^ -//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) {{ runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" +//│ = "class M { static { M1 = this } constructor() { runtime.Unit; } static mtd(...args) { runtime.checkArgs(\"mtd\", 0, true, args.length); return 42 } toString() { return runtime.render(this); } static [definitionMetadata] = [\"class\", \"M\"]; }1" :e M |> id diff --git a/hkmc2/shared/src/test/mlscript/codegen/Functions.mls b/hkmc2/shared/src/test/mlscript/codegen/Functions.mls index 4b3dd3c22f..67d3f0e56c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Functions.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Functions.mls @@ -89,7 +89,7 @@ fun test2(y) = y + 1 //│ REPL> Sending: block$res18 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let test2, test1;try { test1 = function test1(...args) {{ runtime.checkArgs("test1", 1, true, args.length); let x = args[0]; return runtime.checkCall(test2(x)) } }; test2 = function test2(...args) {{ runtime.checkArgs("test2", 1, true, args.length); let y = args[0]; return y + 1 } }; block$res18 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let test2, test1;try { test1 = function test1(...args) { runtime.checkArgs("test1", 1, true, args.length); let x = args[0]; return runtime.checkCall(test2(x)) }; test2 = function test2(...args) { runtime.checkArgs("test2", 1, true, args.length); let y = args[0]; return y + 1 }; block$res18 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index fbe153c46d..eb040590d5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -35,12 +35,12 @@ (x => x) + 1 //│ JS (unsanitized): //│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 -//│ = "function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }1" +//│ = "function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }1" :sjs 1 + (x => x) //│ JS (unsanitized): //│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 -//│ = "1function (...args) {{ runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x } }" +//│ = "1function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls index e4436c11e1..962b3c03b9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls @@ -62,16 +62,15 @@ fun foo(x) = scope.locally of () => let y = 1 in x + y + z //│ JS: -//│ foo = function foo(...args) {{ -//│ runtime.checkArgs("foo", 1, true, args.length); -//│ let x1 = args[0]; -//│ let z; /** scoped **/ -//│ z = 1;{ -//│ let y, tmp; /** scoped **/ -//│ y = 1; -//│ tmp = x1 + y; -//│ return tmp + z -//│ } +//│ foo = function foo(...args) { +//│ runtime.checkArgs("foo", 1, true, args.length); +//│ let x1 = args[0]; +//│ let z; /** scoped **/{ +//│ let y, tmp; /** scoped **/ +//│ z = 1; +//│ y = 1; +//│ tmp = x1 + y; +//│ return tmp + z //│ } //│ }; //│ block$res5 = undefined; @@ -183,21 +182,21 @@ fun foo(x, y) = //│ JS (unsanitized): //│ let foo1; /** scoped **/ //│ foo1 = function foo(x1, y) { -//│ let z, scrut, m, tmp, tmp1; /** scoped **/ -//│ z = globalThis.Object.freeze([ -//│ 1, -//│ 2, -//│ 3 -//│ ]); -//│ tmp = x1 > z[0]; -//│ scrut = ! tmp; -//│ if (scrut === true) { -//│ tmp1 = Predef.print("rua"); -//│ } else { -//│ tmp1 = runtime.Unit; -//│ } -//│ m = x1 + y;{ +//│ let z, scrut, m, tmp, tmp1; /** scoped **/{ //│ let w1, w2, scrut1; /** scoped **/ +//│ z = globalThis.Object.freeze([ +//│ 1, +//│ 2, +//│ 3 +//│ ]); +//│ tmp = x1 > z[0]; +//│ scrut = ! tmp; +//│ if (scrut === true) { +//│ tmp1 = Predef.print("rua"); +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ m = x1 + y; //│ w1 = z[1] + m; //│ w2 = z[2] - m; //│ scrut1 = w1 == w2; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Repl.mls b/hkmc2/shared/src/test/mlscript/codegen/Repl.mls index a4c490fccb..9fad84082a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Repl.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Repl.mls @@ -10,7 +10,7 @@ fun res() = 1 //│ REPL> Sending: block$res3 = undefined //│ REPL> Collected: //│ > undefined -//│ REPL> Sending: let res2;try { res2 = function res(...args) {{ runtime.checkArgs("res", 0, true, args.length); return 1 } }; block$res3 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } +//│ REPL> Sending: let res2;try { res2 = function res(...args) { runtime.checkArgs("res", 0, true, args.length); return 1 }; block$res3 = undefined; } catch (e) { console.log('\u200B' + e.stack + '\u200B'); } //│ REPL> Collected: //│ > undefined //│ REPL> Parsed: diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 2dfe46945b..0808454300 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -7,12 +7,11 @@ fun f(x, y) = x + y f(2, 3) //│ JS: -//│ f = function f(...args) {{ -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let x = args[0]; -//│ let y = args[1]; -//│ return x + y -//│ } +//│ f = function f(...args) { +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let x = args[0]; +//│ let y = args[1]; +//│ return x + y //│ }; //│ block$res1 = runtime.checkCall(f(2, 3)); //│ undefined @@ -46,18 +45,16 @@ id(f)(2) fun f(x)(y, z) = x + y + z id(f)(3)(4) //│ JS: -//│ f1 = function f(...args) {{ -//│ runtime.checkArgs("f", 1, true, args.length); -//│ let x = args[0]; -//│ return (...args1) => {{ -//│ runtime.checkArgs("", 2, true, args1.length); -//│ let y = args1[0]; -//│ let z = args1[1]; -//│ let tmp4; /** scoped **/ -//│ tmp4 = x + y; -//│ return tmp4 + z -//│ } -//│ } +//│ f1 = function f(...args) { +//│ runtime.checkArgs("f", 1, true, args.length); +//│ let x = args[0]; +//│ return (...args1) => { +//│ runtime.checkArgs("", 2, true, args1.length); +//│ let y = args1[0]; +//│ let z = args1[1]; +//│ let tmp4; /** scoped **/ +//│ tmp4 = x + y; +//│ return tmp4 + z //│ } //│ }; //│ tmp2 = runtime.checkCall(Predef.id(f1)); @@ -85,12 +82,11 @@ let f = (x, y) => x + y f(2) //│ JS: //│ let f6; -//│ f6 = function f(...args) {{ -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let x = args[0]; -//│ let y = args[1]; -//│ return x + y -//│ } +//│ f6 = function f(...args) { +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let x = args[0]; +//│ let y = args[1]; +//│ return x + y //│ }; //│ f5 = f6; //│ block$res6 = runtime.safeCall(f5(2)); @@ -150,15 +146,14 @@ id(Cls(1, 2)).f(3) //│ this.y = y; //│ } //│ get f$__checkNotMethod() { runtime.deboundMethod("f", "Cls"); } -//│ f(...args) {{ -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let z = args[0]; -//│ let p = args[1]; -//│ let tmp8, tmp9; /** scoped **/ -//│ tmp8 = this.x + this.y; -//│ tmp9 = tmp8 + z; -//│ return tmp9 + p -//│ } +//│ f(...args) { +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let z = args[0]; +//│ let p = args[1]; +//│ let tmp8, tmp9; /** scoped **/ +//│ tmp8 = this.x + this.y; +//│ tmp9 = tmp8 + z; +//│ return tmp9 + p //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "Cls", ["x", "y"]]; @@ -188,22 +183,20 @@ id(Cls(1, 2)).f(3, 4)(5) //│ this.y = y; //│ } //│ get f$__checkNotMethod() { runtime.deboundMethod("f", "Cls"); } -//│ f(...args) {{ -//│ runtime.checkArgs("f", 2, true, args.length); -//│ let z = args[0]; -//│ let p = args[1]; -//│ return (...args1) => {{ -//│ runtime.checkArgs("", 2, true, args1.length); -//│ let q = args1[0]; -//│ let s = args1[1]; -//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ -//│ tmp11 = this.x + this.y; -//│ tmp12 = tmp11 + z; -//│ tmp13 = tmp12 + p; -//│ tmp14 = tmp13 + q; -//│ return tmp14 + s -//│ } -//│ } +//│ f(...args) { +//│ runtime.checkArgs("f", 2, true, args.length); +//│ let z = args[0]; +//│ let p = args[1]; +//│ return (...args1) => { +//│ runtime.checkArgs("", 2, true, args1.length); +//│ let q = args1[0]; +//│ let s = args1[1]; +//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ +//│ tmp11 = this.x + this.y; +//│ tmp12 = tmp11 + z; +//│ tmp13 = tmp12 + p; +//│ tmp14 = tmp13 + q; +//│ return tmp14 + s //│ } //│ } //│ toString() { return runtime.render(this); } @@ -281,11 +274,10 @@ if M.A(1).y is //│ this.x = x1; //│ } //│ get f$__checkNotMethod() { runtime.deboundMethod("f", "A"); } -//│ f(...args) {{ -//│ runtime.checkArgs("f", 1, true, args.length); -//│ let y = args[0]; -//│ return this.x + y -//│ } +//│ f(...args) { +//│ runtime.checkArgs("f", 1, true, args.length); +//│ let y = args[0]; +//│ return this.x + y //│ } //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "A", ["x"]]; diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index dae06e5fc6..e5a22e21f5 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -109,13 +109,13 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: val resSym = new TempSymbol(S(blk), "block$res") val lowered0 = low.program(blk) val le = lowered0.copy(main = lowered0.main.mapTail: - case e: End => - Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) - case Return(res, implct) => - assert(implct) - Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) - case tl: (Throw | Break | Continue) => tl - ) + case e: End => + Assign(resSym, Value.Lit(syntax.Tree.UnitLit(false)), e) + case Return(res, implct) => + assert(implct) + Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case tl: (Throw | Break | Continue) => tl + ) if showLoweredTree.isSet then output(s"Lowered:") output(lowered0.showAsTree) From 1ac0ae9c17125e03ae345aaba5dc4cc586fc8e7c Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Thu, 4 Dec 2025 16:48:30 +0800 Subject: [PATCH 53/72] fix scopes for nested if and `floatOutUntilScope` --- .../src/main/scala/hkmc2/codegen/Block.scala | 25 -- .../scala/hkmc2/codegen/HandlerLowering.scala | 4 +- .../hkmc2/semantics/ucs/Normalization.scala | 243 ++++++++-------- .../src/test/mlscript-compile/Runtime.mjs | 201 +++++++------ .../test/mlscript/codegen/MergeMatchArms.mls | 28 +- .../codegen/{Scoped.mls => ScopedBlocks.mls} | 63 ----- .../codegen/ScopedBlocksAndHandlers.mls | 263 ++++++++++++++++++ .../src/test/mlscript/handlers/Debugging.mls | 13 +- .../src/test/mlscript/handlers/Effects.mls | 5 +- .../mlscript/handlers/EffectsInClasses.mls | 4 +- .../mlscript/handlers/RecursiveHandlers.mls | 6 +- .../test/mlscript/handlers/StackSafety.mls | 8 +- .../test/mlscript/lifter/StackSafetyLift.mls | 4 +- 13 files changed, 512 insertions(+), 355 deletions(-) rename hkmc2/shared/src/test/mlscript/codegen/{Scoped.mls => ScopedBlocks.mls} (88%) create mode 100644 hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index f3000ed8a5..82f05beecf 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -192,31 +192,6 @@ sealed abstract class Block extends Product: (transformer.applyBlock(this), defns) - // This is currently only used in `HanlderLowering` to prevent - // floating things out too aggressively such that the captured variables become unbound. - // TODO: clean up this and `floatOutDefns` above; currently there are two variations of this - // "floatOut" function because the lifter also uses "floatOut" and - // this `floatOutDefnsUntilScoped` below breaks the lifter - def floatOutDefnsUntilScoped( - ignore: Defn => Bool = _ => false, - preserve: Defn => Bool = _ => false - ): (Block, List[Defn]) = - var defns: List[Defn] = Nil - val transformer = new BlockTransformerShallow(SymbolSubst()): - override def applyBlock(b: Block): Block = b match - case s@Scoped(syms, blk) => - val (inner, defs) = blk.floatOutDefnsUntilScoped(ignore, preserve) - Scoped(syms ++ defs.map(_.sym), defs.foldLeft(inner)((acc, defn) => Define(defn, acc)))(s.dontFlatten) - case Define(defn, rest) if !ignore(defn) => defn match - case v: ValDefn => super.applyBlock(b) - case _ => - defns ::= defn - if preserve(defn) then super.applyBlock(b) - else applyBlock(rest) - case _ => super.applyBlock(b) - - (transformer.applyBlock(this), defns) - lazy val flattened: Block = this.flatten(identity) private def flatten(k: End => Block): Block = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index b5f91ce19e..97a13183b4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -579,9 +579,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, private def thirdPass(b: Block): Block = // to ensure the fun and class references in the continuation class are properly scoped, // we move all function defns to the top level of the handler block - // NOTE: this is to prevent floating things out too aggressively - // such that the captured variables become unbound. - val (blk, defns) = b.floatOutDefnsUntilScoped() + val (blk, defns) = b.floatOutDefns() defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) private def locToStr(l: Loc): Str = diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 8dfddeeef2..5b023cd28d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -318,131 +318,130 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e def apply(split: Split)(k: Result => Block)(using Config, LoweringCtx): Block = this(split, `if`, N, k) - private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = LoweringCtx.nestScoped.givenIn: - var usesResTmp = false - // The symbol of the temporary variable for the result of the `if`-like term. - // It will be created in one of the following situations. - // 1. The continuation `k` is not a tail operation. - // 2. There are shared consequents in the `if`-like term. - // 3. The term is a `while` and the result is used. - lazy val l = - usesResTmp = true - val res = new TempSymbol(t) - outerCtx.collectScopedSym(res) - res - // The symbol for the loop label if the term is a `while`. - lazy val loopLabel = new TempSymbol(t) - lazy val f = - val res = new BlockMemberSymbol("while", Nil, false) - outerCtx.collectScopedSym(res) - res - val normalized = tl.scoped("ucs:normalize"): - normalize(inputSplit)(using VarSet()) - tl.scoped("ucs:normalized"): - tl.log(s"Normalized:\n${normalized.prettyPrint}") - // Collect consequents that are shared in more than one branch. - given labels: Labels = createLabelsForDuplicatedBranches(normalized) - lazy val rootBreakLabel = new TempSymbol(N, "split_root$") - lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) - lazy val assignResult = (r: Result) => Assign(l, r, End()) - // NOTE: `shouldRewriteWhile` is not the same as `config.rewriteWhileLoops` - // as shouldRewriteWhile is always true when effect handler lowering is on - lazy val loopCont = if config.shouldRewriteWhile - then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) - else Continue(loopLabel) - val cont = - if kw === `while` then - // If the term is a `while`, the action of `else` branches depends on - // whether the the enclosing split is at the top level or not. - R((topLevel: Bool) => (r: Result) => Assign(l, r, if topLevel then End() else loopCont)) - else if labels.isEmpty then - if k.isInstanceOf[TailOp] then - // If there are no shared consequents and the continuation is a tail - // operation, we can call it directly. - L(k) + private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = + // if it's `while`, we always make sure that loop bodies are properly scoped nestedly + // - when handler lowering is off (which means that loops are not rewritten to functions by default), this is crucial for the correct semantics + // - when handler lowering is on (which means loops are always rewritten to functions), this nested scoped blocks also matches the scope of the "while" functions + val useNestedScoped = kw === `while` + (if useNestedScoped then LoweringCtx.nestScoped else outerCtx).givenIn: + var usesResTmp = false + // The symbol of the temporary variable for the result of the `if`-like term. + // It will be created in one of the following situations. + // 1. The continuation `k` is not a tail operation. + // 2. There are shared consequents in the `if`-like term. + // 3. The term is a `while` and the result is used. + lazy val l = + usesResTmp = true + val res = new TempSymbol(t) + outerCtx.collectScopedSym(res) + res + // The symbol for the loop label if the term is a `while`. + lazy val loopLabel = new TempSymbol(t) + lazy val f = + val res = new BlockMemberSymbol("while", Nil, false) + outerCtx.collectScopedSym(res) + res + val normalized = tl.scoped("ucs:normalize"): + normalize(inputSplit)(using VarSet()) + tl.scoped("ucs:normalized"): + tl.log(s"Normalized:\n${normalized.prettyPrint}") + // Collect consequents that are shared in more than one branch. + given labels: Labels = createLabelsForDuplicatedBranches(normalized) + lazy val rootBreakLabel = new TempSymbol(N, "split_root$") + lazy val breakRoot = (r: Result) => Assign(l, r, Break(rootBreakLabel)) + lazy val assignResult = (r: Result) => Assign(l, r, End()) + // NOTE: `shouldRewriteWhile` is not the same as `config.rewriteWhileLoops` + // as shouldRewriteWhile is always true when effect handler lowering is on + lazy val loopCont = if config.shouldRewriteWhile + then Return(Call(Value.Ref(f, N), Nil)(true, true, false), false) + else Continue(loopLabel) + val cont = + if kw === `while` then + // If the term is a `while`, the action of `else` branches depends on + // whether the the enclosing split is at the top level or not. + R((topLevel: Bool) => (r: Result) => Assign(l, r, if topLevel then End() else loopCont)) + else if labels.isEmpty then + if k.isInstanceOf[TailOp] then + // If there are no shared consequents and the continuation is a tail + // operation, we can call it directly. + L(k) + else + // Otherwise, if the continuation is not a tail operation, we should + // save the result in a temporary variable and call the continuation + // in the end. + L(assignResult) else - // Otherwise, if the continuation is not a tail operation, we should - // save the result in a temporary variable and call the continuation - // in the end. - L(assignResult) - else - // When there are shared consequents, we are forced to save the result - // in the temporary variable nevertheless. Note that `cont` only gets - // called for non-shared consequents, so we should break to the end of - // the entire split after the assignment. - L(breakRoot) - // The main block contains the lowered split, where each shared consequent - // is replaced with a `Break` to the corresponding label. - val mainBlock = - val innermostBlock = lowerSplit(normalized, cont, topLevel = true) - // Wrap the main block in a labelled block for each shared consequent. The - // `rest` of each `Label` is the lowered consequent plus a `Break` to the - // end of the entire `if` term. Otherwise, it will fall through to the outer - // consequent, which is the wrong semantics. - val innerBlock: Block = labels.consequents match - case Nil => innermostBlock - case all @ (head :: tail) => - def wrap(consequents: Ls[(Term, TempSymbol)]): Block = - consequents.foldRight(innermostBlock): - case ((term, label), innerBlock) => - Label(label, false, innerBlock, term_nonTail(term)(breakRoot)) - // There is no need to generate `break` for the outermost split. - if labels.default.isEmpty then - Label(head._2, false, wrap(tail), term_nonTail(head._1)(assignResult)) - else wrap(all) - labels.default match - case S(label) => Label(label, false, innerBlock, throwMatchErrorBlock) - case N => innerBlock - // If there are shared consequents, we need a wrap the entire block in a - // `Label` so that `Break`s in the shared consequents can jump to the end. - val body = - val possiblyScoped = - // lowering.possiblyScoped( - // LoweringCtx.subst.getCollectedSym ++ inputSplit.definedSyms, - // mainBlock) - mainBlock - // if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - Scoped( - LoweringCtx.loweringCtx.getCollectedSym/* ++ inputSplit.definedSyms */, - if labels.isEmpty then possiblyScoped else Label(rootBreakLabel, false, possiblyScoped, End()) - )(false) - // Embed the `body` into `Label` if the term is a `while`. - lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) - val block = - if kw === `while` then - // NOTE: `shouldRewriteWhile` is not the same as `config.rewriteWhileLoops` - // as shouldRewriteWhile is always true when effect handler lowering is on - if config.shouldRewriteWhile then - val loopResult = TempSymbol(N) - val isReturned = TempSymbol(N) - outerCtx.collectScopedSym(loopResult) - outerCtx.collectScopedSym(isReturned) - val loopEnd: Path = - Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) - val blk = blockBuilder - .assign(l, Value.Lit(Tree.UnitLit(false))) - .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) - .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) - if summon[LoweringCtx].mayRet then - blk - .assign(isReturned, Call(Value.Ref(State.builtinOpsMap("!==")), - loopResult.asPath.asArg :: loopEnd.asArg :: Nil)(true, false, false)) - .ifthen(Value.Ref(isReturned), Case.Lit(Tree.BoolLit(true)), - Return(Value.Ref(loopResult), false), - S(rest) - ) - .end + // When there are shared consequents, we are forced to save the result + // in the temporary variable nevertheless. Note that `cont` only gets + // called for non-shared consequents, so we should break to the end of + // the entire split after the assignment. + L(breakRoot) + // The main block contains the lowered split, where each shared consequent + // is replaced with a `Break` to the corresponding label. + val mainBlock = + val innermostBlock = lowerSplit(normalized, cont, topLevel = true) + // Wrap the main block in a labelled block for each shared consequent. The + // `rest` of each `Label` is the lowered consequent plus a `Break` to the + // end of the entire `if` term. Otherwise, it will fall through to the outer + // consequent, which is the wrong semantics. + val innerBlock: Block = labels.consequents match + case Nil => innermostBlock + case all @ (head :: tail) => + def wrap(consequents: Ls[(Term, TempSymbol)]): Block = + consequents.foldRight(innermostBlock): + case ((term, label), innerBlock) => + Label(label, false, innerBlock, term_nonTail(term)(breakRoot)) + // There is no need to generate `break` for the outermost split. + if labels.default.isEmpty then + Label(head._2, false, wrap(tail), term_nonTail(head._1)(assignResult)) + else wrap(all) + labels.default match + case S(label) => Label(label, false, innerBlock, throwMatchErrorBlock) + case N => innerBlock + // If there are shared consequents, we need a wrap the entire block in a + // `Label` so that `Break`s in the shared consequents can jump to the end. + val body = + Scoped( + if useNestedScoped then LoweringCtx.loweringCtx.getCollectedSym else Set.empty, + if labels.isEmpty then mainBlock else Label(rootBreakLabel, false, mainBlock, End()) + )(false) + // Embed the `body` into `Label` if the term is a `while`. + lazy val rest = if usesResTmp then k(Value.Ref(l)) else k(lowering.unit) + val block = + if kw === `while` then + // NOTE: `shouldRewriteWhile` is not the same as `config.rewriteWhileLoops` + // as shouldRewriteWhile is always true when effect handler lowering is on + if config.shouldRewriteWhile then + val loopResult = TempSymbol(N) + val isReturned = TempSymbol(N) + outerCtx.collectScopedSym(loopResult) + outerCtx.collectScopedSym(isReturned) + val loopEnd: Path = + Select(Value.Ref(State.runtimeSymbol), Tree.Ident("LoopEnd"))(S(State.loopEndSymbol)) + val blk = blockBuilder + .assign(l, Value.Lit(Tree.UnitLit(false))) + .define(FunDefn.withFreshSymbol(N, f, PlainParamList(Nil) :: Nil, Begin(body, Return(loopEnd, false)))(isTailRec = false)) + .assign(loopResult, Call(Value.Ref(f, N), Nil)(true, true, false)) + if summon[LoweringCtx].mayRet then + blk + .assign(isReturned, Call(Value.Ref(State.builtinOpsMap("!==")), + loopResult.asPath.asArg :: loopEnd.asArg :: Nil)(true, false, false)) + .ifthen(Value.Ref(isReturned), Case.Lit(Tree.BoolLit(true)), + Return(Value.Ref(loopResult), false), + S(rest) + ) + .end + else + blk.rest(rest) else - blk.rest(rest) + Begin(Label(loopLabel, true, body, End()), rest) + else if labels.isEmpty && k.isInstanceOf[TailOp] then + body else - Begin(Label(loopLabel, true, body, End()), rest) - else if labels.isEmpty && k.isInstanceOf[TailOp] then - body - else - Begin(body, rest) - scoped("ucs:lowered"): - log(s"Lowered:\n${block.showAsTree}") - block + Begin(body, rest) + scoped("ucs:lowered"): + log(s"Lowered:\n${block.showAsTree}") + block end Normalization object Normalization: diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index a2cb048716..65c5fdda1e 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -427,7 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ + let scrut, name, scrut1, scrut2, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -437,35 +437,34 @@ globalThis.Object.freeze(class Runtime { }); scrut = runtime.short_or(tmp, lambda); if (scrut === true) { - let scrut1, scrut2, tmp12; /** scoped **/ scrut1 = functionName.length > 0; if (scrut1 === true) { - tmp12 = " '" + functionName; - tmp1 = tmp12 + "'"; + tmp1 = " '" + functionName; + tmp2 = tmp1 + "'"; } else { - tmp1 = ""; + tmp2 = ""; } - name = tmp1; - tmp2 = "Function" + name; - tmp3 = tmp2 + " expected "; + name = tmp2; + tmp3 = "Function" + name; + tmp4 = tmp3 + " expected "; if (isUB === true) { - tmp4 = ""; + tmp5 = ""; } else { - tmp4 = "at least "; + tmp5 = "at least "; } - tmp5 = tmp3 + tmp4; - tmp6 = tmp5 + expected; - tmp7 = tmp6 + " argument"; + tmp6 = tmp4 + tmp5; + tmp7 = tmp6 + expected; + tmp8 = tmp7 + " argument"; scrut2 = expected === 1; if (scrut2 === true) { - tmp8 = ""; + tmp9 = ""; } else { - tmp8 = "s"; + tmp9 = "s"; } - tmp9 = tmp7 + tmp8; - tmp10 = tmp9 + " but got "; - tmp11 = tmp10 + got; - throw globalThis.Error(tmp11) + tmp10 = tmp8 + tmp9; + tmp11 = tmp10 + " but got "; + tmp12 = tmp11 + got; + throw globalThis.Error(tmp12) } else { return runtime.Unit } @@ -539,23 +538,20 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, tmp, tmp1, tmp2; /** scoped **/ + let msg, curHandler, atTail, tmp, tmp1, tmp2, tmp3; /** scoped **/ msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { - let tmp3; /** scoped **/ tmp4: while (true) { - let scrut, cur, tmp5, tmp6; /** scoped **/ + let scrut, cur, scrut1, tmp5, tmp6, tmp7, tmp8; /** scoped **/ scrut = curHandler !== null; if (scrut === true) { - let scrut1, tmp7, tmp8; /** scoped **/ cur = curHandler.next; tmp9: while (true) { - let scrut2, locals, curLocals, loc, loc1, localsMsg, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18; /** scoped **/ + let scrut2, locals, curLocals, loc, loc1, localsMsg, scrut3, tmp10, tmp11, lambda, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18, tmp19, tmp20; /** scoped **/ scrut2 = cur !== null; if (scrut2 === true) { - let scrut3, lambda, tmp19, tmp20; /** scoped **/ locals = cur.getLocals; tmp10 = locals.length - 1; curLocals = runtime.safeCall(locals.at(tmp10)); @@ -577,9 +573,9 @@ globalThis.Object.freeze(class Runtime { tmp22 = Rendering.render(l.value); return tmp21 + tmp22 }); - tmp19 = runtime.safeCall(curLocals.locals.map(lambda)); - tmp20 = runtime.safeCall(tmp19.join(", ")); - tmp12 = " with locals: " + tmp20; + tmp12 = runtime.safeCall(curLocals.locals.map(lambda)); + tmp13 = runtime.safeCall(tmp12.join(", ")); + tmp14 = " with locals: " + tmp13; break split_root$ } else { break split_1$ @@ -588,17 +584,17 @@ globalThis.Object.freeze(class Runtime { break split_1$ } } - tmp12 = ""; + tmp14 = ""; } - localsMsg = tmp12; - tmp13 = "\n\tat " + curLocals.fnName; - tmp14 = tmp13 + " ("; - tmp15 = tmp14 + loc1; - tmp16 = tmp15 + ")"; - tmp17 = msg + tmp16; - msg = tmp17; - tmp18 = msg + localsMsg; - msg = tmp18; + localsMsg = tmp14; + tmp15 = "\n\tat " + curLocals.fnName; + tmp16 = tmp15 + " ("; + tmp17 = tmp16 + loc1; + tmp18 = tmp17 + ")"; + tmp19 = msg + tmp18; + msg = tmp19; + tmp20 = msg + localsMsg; + msg = tmp20; cur = cur.next; atTail = false; tmp5 = runtime.Unit; @@ -611,15 +607,15 @@ globalThis.Object.freeze(class Runtime { curHandler = curHandler.nextHandler; scrut1 = curHandler !== null; if (scrut1 === true) { - tmp7 = "\n\twith handler " + curHandler.handler.constructor.name; - tmp8 = msg + tmp7; - msg = tmp8; + tmp6 = "\n\twith handler " + curHandler.handler.constructor.name; + tmp7 = msg + tmp6; + msg = tmp7; atTail = false; - tmp6 = runtime.Unit; + tmp8 = runtime.Unit; } else { - tmp6 = runtime.Unit; + tmp8 = runtime.Unit; } - tmp = tmp6; + tmp = tmp8; continue tmp4 } else { tmp = runtime.Unit; @@ -627,28 +623,27 @@ globalThis.Object.freeze(class Runtime { break; } if (atTail === true) { - tmp3 = msg + "\n\tat tail position"; - msg = tmp3; - tmp1 = runtime.Unit; + tmp1 = msg + "\n\tat tail position"; + msg = tmp1; + tmp2 = runtime.Unit; } else { - tmp1 = runtime.Unit; + tmp2 = runtime.Unit; } - tmp2 = tmp1; + tmp3 = tmp2; } else { - tmp2 = runtime.Unit; + tmp3 = runtime.Unit; } return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, tmp, lambda, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - let scrut, tmp5, tmp6, tmp7; /** scoped **/ tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut1, tmp8, tmp9; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { + let scrut3, tmp8, tmp9; /** scoped **/ + scrut3 = runtime.safeCall(m.has(cont)); + if (scrut3 === true) { tmp8 = ", " + marker; tmp9 = result + tmp8; result = tmp9; @@ -660,26 +655,24 @@ globalThis.Object.freeze(class Runtime { tmp1 = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; /** scoped **/ - tmp5 = reps + 1; - reps = tmp5; + tmp2 = reps + 1; + reps = tmp2; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp6 = runtime.Unit; + tmp3 = runtime.Unit; } - tmp7 = result + ", REPEAT"; - result = tmp7; - tmp2 = runtime.Unit; + tmp4 = result + ", REPEAT"; + result = tmp4; + tmp5 = runtime.Unit; } else { - tmp2 = runtime.safeCall(vis.add(cont)); + tmp5 = runtime.safeCall(vis.add(cont)); } - tmp3 = result + ") -> "; - tmp4 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp3 + tmp4 + tmp6 = result + ") -> "; + tmp7 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp6 + tmp7 } else { - let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -689,14 +682,13 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, lambda, tmp, tmp1, tmp2, tmp3; /** scoped **/ + let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; /** scoped **/ if (cont instanceof Runtime.HandlerContFrame.class) { - let scrut, tmp4, tmp5, tmp6; /** scoped **/ result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut1, tmp7, tmp8; /** scoped **/ - scrut1 = runtime.safeCall(m.has(cont)); - if (scrut1 === true) { + let scrut3, tmp7, tmp8; /** scoped **/ + scrut3 = runtime.safeCall(m.has(cont)); + if (scrut3 === true) { tmp7 = ", " + marker; tmp8 = result + tmp7; result = tmp8; @@ -708,26 +700,24 @@ globalThis.Object.freeze(class Runtime { tmp = runtime.safeCall(hl.forEach(lambda)); scrut = runtime.safeCall(vis.has(cont)); if (scrut === true) { - let scrut1; /** scoped **/ - tmp4 = reps + 1; - reps = tmp4; + tmp1 = reps + 1; + reps = tmp1; scrut1 = reps > 10; if (scrut1 === true) { throw globalThis.Error("10 repeated continuation frame (loop?)") } else { - tmp5 = runtime.Unit; + tmp2 = runtime.Unit; } - tmp6 = result + ", REPEAT"; - result = tmp6; - tmp1 = runtime.Unit; + tmp3 = result + ", REPEAT"; + result = tmp3; + tmp4 = runtime.Unit; } else { - tmp1 = runtime.safeCall(vis.add(cont)); + tmp4 = runtime.safeCall(vis.add(cont)); } - tmp2 = result + " -> "; - tmp3 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); - return tmp2 + tmp3 + tmp5 = result + " -> "; + tmp6 = Runtime.showFunctionContChain(cont.next, hl, vis, reps); + return tmp5 + tmp6 } else { - let scrut2; /** scoped **/ scrut2 = cont === null; if (scrut2 === true) { return "(null)" @@ -751,9 +741,8 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + let scrut, scrut1, vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ if (contTrace instanceof Runtime.ContTrace.class) { - let scrut, scrut1; /** scoped **/ tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; if (scrut === true) { @@ -842,9 +831,8 @@ globalThis.Object.freeze(class Runtime { static handleEffects(cur) { let tmp; /** scoped **/ tmp1: while (true) { - let nxt, tmp2; /** scoped **/ + let nxt, scrut, tmp2; /** scoped **/ if (cur instanceof Runtime.EffectSig.class) { - let scrut; /** scoped **/ nxt = Runtime.handleEffect(cur); scrut = cur === nxt; if (scrut === true) { @@ -863,16 +851,16 @@ globalThis.Object.freeze(class Runtime { return tmp } static handleEffect(cur) { - let prevHandlerFrame, scrut, handlerFrame, saved, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + let prevHandlerFrame, scrut, handlerFrame, saved, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ prevHandlerFrame = cur.contTrace; tmp6: while (true) { - let scrut1, scrut2; /** scoped **/ + let scrut3, scrut4; /** scoped **/ split_root$: { split_1$: { - scrut1 = prevHandlerFrame.nextHandler !== null; - if (scrut1 === true) { - scrut2 = prevHandlerFrame.nextHandler.handler !== cur.handler; - if (scrut2 === true) { + scrut3 = prevHandlerFrame.nextHandler !== null; + if (scrut3 === true) { + scrut4 = prevHandlerFrame.nextHandler.handler !== cur.handler; + if (scrut4 === true) { prevHandlerFrame = prevHandlerFrame.nextHandler; tmp = runtime.Unit; continue tmp6 @@ -903,17 +891,16 @@ globalThis.Object.freeze(class Runtime { tmp3 = runtime.safeCall(cur.handlerFun(tmp2)); cur = tmp3; if (cur instanceof Runtime.EffectSig.class) { - let scrut3, scrut4; /** scoped **/ - scrut3 = saved.next !== null; - if (scrut3 === true) { + scrut1 = saved.next !== null; + if (scrut1 === true) { cur.contTrace.last.next = saved.next; cur.contTrace.last = saved.last; tmp4 = runtime.Unit; } else { tmp4 = runtime.Unit; } - scrut4 = saved.nextHandler !== null; - if (scrut4 === true) { + scrut2 = saved.nextHandler !== null; + if (scrut2 === true) { cur.contTrace.lastHandler.nextHandler = saved.nextHandler; cur.contTrace.lastHandler = saved.lastHandler; tmp5 = runtime.Unit; @@ -945,36 +932,34 @@ globalThis.Object.freeze(class Runtime { handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; tmp1: while (true) { - let tmp2, tmp3; /** scoped **/ + let scrut, scrut1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ if (cont instanceof Runtime.FunctionContFrame.class) { - let tmp4, tmp5; /** scoped **/ tmp2 = runtime.safeCall(cont.resume(value)); value = tmp2; Runtime.stackDepth = curDepth; if (value instanceof Runtime.EffectSig.class) { - let scrut, scrut1; /** scoped **/ value.contTrace.last.next = cont.next; value.contTrace.lastHandler.nextHandler = handlerCont; scrut = contTrace.last !== cont; if (scrut === true) { value.contTrace.last = contTrace.last; - tmp4 = runtime.Unit; + tmp3 = runtime.Unit; } else { - tmp4 = runtime.Unit; + tmp3 = runtime.Unit; } scrut1 = handlerCont !== null; if (scrut1 === true) { value.contTrace.lastHandler = contTrace.lastHandler; - tmp5 = runtime.Unit; + tmp4 = runtime.Unit; } else { - tmp5 = runtime.Unit; + tmp4 = runtime.Unit; } return value } else { cont = cont.next; - tmp3 = runtime.Unit; + tmp5 = runtime.Unit; } - tmp = tmp3; + tmp = tmp5; continue tmp1 } else { if (handlerCont instanceof Runtime.HandlerContFrame.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index b7bb41de5b..e157ace779 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -107,31 +107,29 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3; /** scoped **/ +//│ let x, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ tmp1 = 3; //│ if (a instanceof A1.class) { //│ tmp2 = 1; //│ } else if (a instanceof B1.class) { //│ tmp2 = 2; //│ } else if (a instanceof C1.class) { //│ tmp2 = 3; -//│ } else { -//│ let tmp4; /** scoped **/ -//│ tmp1 = 3; -//│ if (a instanceof D1.class) { -//│ if (a instanceof A1.class) { -//│ tmp4 = 1 + tmp1; -//│ } else if (a instanceof B1.class) { -//│ tmp4 = 2 + tmp1; -//│ } else { -//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) -//│ } -//│ tmp3 = tmp4; -//│ } else if (a instanceof E1.class) { -//│ tmp3 = 5; +//│ } else if (a instanceof D1.class) { +//│ if (a instanceof A1.class) { +//│ tmp3 = 1 + tmp1; +//│ } else if (a instanceof B1.class) { +//│ tmp3 = 2 + tmp1; //│ } else { //│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } +//│ tmp4 = tmp3; +//│ tmp2 = Predef.print("done"); +//│ } else if (a instanceof E1.class) { +//│ tmp4 = 5; //│ tmp2 = Predef.print("done"); +//│ } else { +//│ throw globalThis.Object.freeze(new globalThis.Error("match error")) //│ } //│ x = tmp2; //│ Predef.print(x) diff --git a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls similarity index 88% rename from hkmc2/shared/src/test/mlscript/codegen/Scoped.mls rename to hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls index a049776664..ad3a9ca872 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Scoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls @@ -377,66 +377,3 @@ module M with //│ static [definitionMetadata] = ["class", "M"]; //│ }); - -:effectHandlers -fun start(x, f) = - while x do - f(() => x) - - -:effectHandlers -:sjs -fun f(x) = - if x do - if x do - let z = 3 - fun g() = z -//│ JS (unsanitized): -//│ let f6; /** scoped **/ -//│ f6 = function f(x1) { -//│ if (x1 === true) { -//│ let g1, z; /** scoped **/ -//│ g1 = function g() { -//│ return z -//│ }; -//│ if (x1 === true) { -//│ z = 3; -//│ return runtime.Unit -//│ } else { return runtime.Unit } -//│ } else { return runtime.Unit } -//│ }; - - - -abstract class Effect with - fun perform(arg: Str): Str - - - -:effectHandlers -fun f(x) = - if x then - if x then - let z = 4 - handle h = Effect with - fun perform(x)(k) = - print(x) - k(x + z) - print(h.perform(32)) - else 0 - - -// NOTE: currently while loops are always rewritten into functions -// when `:effectHanlders` are set, so this "dontRewriteWhile" has no effect here -:dontRewriteWhile -:effectHandlers -fun f(x) = - while x do - while x do - let z = 4 - handle h = Effect with - fun perform(x)(k) = - print(x) - k(x + z) - print(h.perform(32)) - else 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls new file mode 100644 index 0000000000..7ba9fe55f2 --- /dev/null +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls @@ -0,0 +1,263 @@ +:js +:effectHandlers + + + + +:effectHandlers +fun start(x, f) = + while x do + f(() => x) + +// NOTE: if `z` is defined in a nested scoped block +// and `g` is floated out to the top level, `z` in `g` gets unbound. +:effectHandlers +:sjs +fun f(x) = + if x do + if x do + let z = 3 + fun g() = z +//│ JS (unsanitized): +//│ let f; /** scoped **/ +//│ f = function f(x) { +//│ let g, z; /** scoped **/ +//│ g = function g() { +//│ return z +//│ }; +//│ if (x === true) { +//│ if (x === true) { +//│ z = 3; +//│ return runtime.Unit +//│ } else { return runtime.Unit } +//│ } else { return runtime.Unit } +//│ }; + + + +abstract class Effect with + fun perform() + + + +:effectHandlers +:sjs +fun f(x) = + if x then + if x then + let z = 4 + handle h = Effect with + fun perform(x)(k) = + print(x) + k(x + z) + print(h.perform()) + else 0 +//│ JS (unsanitized): +//│ let f1; /** scoped **/ +//│ f1 = function f(x) { +//│ let z, tmp, tmp1; /** scoped **/ +//│ let handleBlock$, res, Cont$func$f$1, doUnwind; +//│ globalThis.Object.freeze(class Cont$func$f$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$func$f$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp2; +//│ tmp2 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 9) { +//│ res = value$; +//│ } else if (this.pc === 8) { +//│ tmp = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 9) { +//│ throw res +//│ } else if (this.pc === 8) { +//│ return tmp +//│ } +//│ break; +//│ } +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$func$f$"]; +//│ }); +//│ doUnwind = function doUnwind(res1, pc) { +//│ res1.contTrace.last.next = new Cont$func$f$1(pc); +//│ res1.contTrace.last = res1.contTrace.last.next; +//│ return res1 +//│ }; +//│ handleBlock$ = function handleBlock$() { +//│ let h, res1, Cont$handleBlock$h$1, doUnwind1, Handler$h$1; +//│ globalThis.Object.freeze(class Handler$h$ extends Effect1 { +//│ static { +//│ Handler$h$1 = this +//│ } +//│ constructor() { +//│ let tmp2; +//│ tmp2 = super(); +//│ } +//│ perform(x1) { +//│ let hdlrFun; +//│ hdlrFun = function hdlrFun(k) { +//│ let tmp2, tmp3; /** scoped **/ +//│ let Cont$handler$h$perform$1, doUnwind2; +//│ globalThis.Object.freeze(class Cont$handler$h$perform$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handler$h$perform$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp4; +//│ tmp4 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 4) { +//│ tmp2 = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 4) { +//│ tmp3 = x1 + z; +//│ this.pc = 5; +//│ continue contLoop +//│ } else if (this.pc === 5) { +//│ return runtime.safeCall(k(tmp3)) +//│ } +//│ break; +//│ } +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handler$h$perform$"]; +//│ }); +//│ doUnwind2 = function doUnwind(res2, pc) { +//│ res2.contTrace.last.next = new Cont$handler$h$perform$1(pc); +//│ res2.contTrace.last = res2.contTrace.last.next; +//│ return res2 +//│ }; +//│ tmp2 = Predef.print(x1); +//│ if (tmp2 instanceof runtime.EffectSig.class) { +//│ return doUnwind2(tmp2, 4) +//│ } +//│ tmp3 = x1 + z; +//│ return runtime.safeCall(k(tmp3)) +//│ }; +//│ return runtime.mkEffect(this, hdlrFun) +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Handler$h$"]; +//│ }); +//│ h = new Handler$h$1(); +//│ globalThis.Object.freeze(class Cont$handleBlock$h$ extends runtime.FunctionContFrame.class { +//│ static { +//│ Cont$handleBlock$h$1 = this +//│ } +//│ constructor(pc) { +//│ let tmp2; +//│ tmp2 = super(null); +//│ this.pc = pc; +//│ } +//│ resume(value$) { +//│ if (this.pc === 1) { +//│ tmp1 = value$; +//│ } else if (this.pc === 2) { +//│ res1 = value$; +//│ } +//│ contLoop: while (true) { +//│ if (this.pc === 3) { +//│ res1 = Predef.print(tmp1); +//│ if (res1 instanceof runtime.EffectSig.class) { +//│ return this.doUnwind(res1, 2) +//│ } +//│ this.pc = 2; +//│ continue contLoop +//│ } else if (this.pc === 1) { +//│ this.pc = 3; +//│ continue contLoop +//│ } else if (this.pc === 2) { +//│ return res1 +//│ } +//│ break; +//│ } +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Cont$handleBlock$h$"]; +//│ }); +//│ doUnwind1 = function doUnwind(res2, pc) { +//│ res2.contTrace.last.next = new Cont$handleBlock$h$1(pc); +//│ return runtime.handleBlockImpl(res2, h) +//│ }; +//│ tmp1 = runtime.safeCall(h.perform()); +//│ if (tmp1 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(tmp1, 1) +//│ } +//│ res1 = Predef.print(tmp1); +//│ if (res1 instanceof runtime.EffectSig.class) { +//│ return doUnwind1(res1, 2) +//│ } +//│ return res1 +//│ }; +//│ if (x === true) { +//│ if (x === true) { +//│ z = 4; +//│ tmp = handleBlock$(); +//│ if (tmp instanceof runtime.EffectSig.class) { +//│ return doUnwind(tmp, 8) +//│ } +//│ return tmp +//│ } else { +//│ res = globalThis.Object.freeze(new globalThis.Error("match error")); +//│ if (res instanceof runtime.EffectSig.class) { +//│ return doUnwind(res, 9) +//│ } +//│ throw res +//│ } +//│ } else { return 0 } +//│ }; + + +// NOTE: currently while loops are always rewritten into functions +// when `:effectHanlders` are set, so this "dontRewriteWhile" has no effect here +:dontRewriteWhile +:effectHandlers +fun f(x) = + while x do + while x do + let z = 4 + handle h = Effect with + fun perform(x)(k) = + print(x) + k(x + z) + print(h.perform()) + else 0 + + +// The class body generated by the handler is in the +// top level of the function, and it refers to `g`. +// So there would be problems if the declartion of `g` +// is nested inside the first `if`. +:expect 2 +fun foo(h) = + h.perform() + if true then + if true then + fun g() = 2 + g() + else + fun g() = 3 + g() +handle h = Effect with + fun perform()(k) = k(()) +foo(h) +//│ = 2 + + + +:effectHandlers +fun f(x) = + while x do + while x do + let z = 3 + fun g() = z + g() diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index dd4b90f8f1..ec3ef74cf6 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -28,7 +28,8 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let i, j, k, scrut, tmp, tmp1, getLocals3, Cont$func$f$1, doUnwind; /** scoped **/ +//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ +//│ let getLocals3, Cont$func$f$1, doUnwind; //│ getLocals3 = function getLocals() { //│ let i1, j1, k1, prev, thisInfo, arr, tmp2; //│ prev = getLocals2(); @@ -127,8 +128,8 @@ lambda_test(() => raiseUnhandledEffect() 100) //│ ═══[RUNTIME ERROR] Error: Unhandled effect FatalEffect -//│ at lambda (Debugging.mls:127:3) -//│ at lambda_test (Debugging.mls:124:3) +//│ at lambda (Debugging.mls:128:3) +//│ at lambda_test (Debugging.mls:125:3) import "../../mlscript-compile/Runtime.mls" @@ -207,9 +208,9 @@ fun f() = f() //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 200)])] //│ > Stack Trace: -//│ > at f (Debugging.mls:200:3) with locals: j=200 +//│ > at f (Debugging.mls:201:3) with locals: j=200 //│ > Stack Trace: -//│ > at f (Debugging.mls:202:3) +//│ > at f (Debugging.mls:203:3) //│ > Stack Trace: -//│ > at f (Debugging.mls:203:3) with locals: j=300 +//│ > at f (Debugging.mls:204:3) with locals: j=300 //│ > [FnLocalsInfo("‹top level›", [LocalVarInfo("i", 100)]), FnLocalsInfo("f", [LocalVarInfo("j", 300)])] diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index 6ad3dc746e..f677b2d3d1 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,11 +156,10 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let tmp20; /** scoped **/ +//│ let f, scrut, tmp20; /** scoped **/ //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { -//│ let f, scrut, Cont$handleBlock$h$11, doUnwind; /** scoped **/ -//│ let h, res, Handler$h$12; +//│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; //│ globalThis.Object.freeze(class Handler$h$11 extends Effect1 { //│ static { //│ Handler$h$12 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 0a530b3001..83c9524e92 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -19,8 +19,8 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { -//│ let tmp, Cont$ctor$Lol$1, doUnwind; /** scoped **/ -//│ let res; +//│ let tmp; /** scoped **/ +//│ let res, Cont$ctor$Lol$1, doUnwind; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index cba16a16ad..f4b53fdfcd 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -173,7 +173,8 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, Cont$handler$h1$perform$2, doUnwind1; /** scoped **/ +//│ let tmp28, tmp29, tmp30; /** scoped **/ +//│ let Cont$handler$h1$perform$2, doUnwind1; //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h1$perform$2 = this @@ -260,7 +261,8 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, tmp31, tmp32, Cont$handler$h2$perform$2, doUnwind2; /** scoped **/ +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ +//│ let Cont$handler$h2$perform$2, doUnwind2; //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { //│ Cont$handler$h2$perform$2 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 66815a2fa5..61cef5c3bf 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -23,8 +23,8 @@ hi(0) //│ let hi; /** scoped **/ //│ let res, $_stack$_safe$_body$_; //│ hi = function hi(n) { -//│ let scrut, tmp, Cont$func$hi$1, doUnwind; /** scoped **/ -//│ let stackDelayRes; +//│ let scrut, tmp; /** scoped **/ +//│ let Cont$func$hi$1, doUnwind, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$hi$1 = this @@ -79,8 +79,8 @@ sum(10000) //│ let sum1; /** scoped **/ //│ let res1, $_stack$_safe$_body$_1; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1, Cont$func$sum$1, doUnwind; /** scoped **/ -//│ let curDepth, stackDelayRes; +//│ let scrut, tmp, tmp1; /** scoped **/ +//│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { //│ static { //│ Cont$func$sum$1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 25340985c2..36e71bb305 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -61,7 +61,7 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let scrut, tmp, doUnwind; /** scoped **/ +//│ let scrut, tmp; /** scoped **/ //│ let stackDelayRes; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); @@ -151,7 +151,7 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1, doUnwind; /** scoped **/ +//│ let scrut, tmp, tmp1; /** scoped **/ //│ let curDepth, stackDelayRes; //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; From 63fe1c41e2f509c99ccb9560bcd109117e145bfa Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 5 Dec 2025 01:03:24 +0800 Subject: [PATCH 54/72] try to add some comments to the entangled kludge --- .../shared/src/main/scala/hkmc2/Config.scala | 6 ++++-- .../hkmc2/semantics/ucs/Normalization.scala | 3 +-- .../codegen/ScopedBlocksAndHandlers.mls | 19 ++++++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/Config.scala b/hkmc2/shared/src/main/scala/hkmc2/Config.scala index 0c6a886f9c..4d4540b594 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/Config.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/Config.scala @@ -28,8 +28,10 @@ case class Config( def stackSafety: Opt[StackSafety] = effectHandlers.flatMap(_.stackSafety) // NOTE: with `Scoped` blocks inside loop bodies, currently this flag - // forces the rewriting of while loops to make sure that - // the handler lowering will not create classes containing unbound variables + // forces the rewriting of while loops when handler lowering is on to make sure that + // we do create programs with unbound variables + // see https://github.com/hkust-taco/mlscript/pull/356#discussion_r2579529893 + // and https://github.com/hkust-taco/mlscript/pull/356#discussion_r2585183902 def shouldRewriteWhile: Bool = rewriteWhileLoops || effectHandlers.isDefined diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala index 2a9e9406a3..7352b2e223 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/ucs/Normalization.scala @@ -320,8 +320,7 @@ class Normalization(lowering: Lowering)(using tl: TL)(using Raise, Ctx, State) e private def apply(inputSplit: Split, kw: `if`.type | `while`.type, t: Opt[Term], k: Result => Block)(using cfg: Config, outerCtx: LoweringCtx) = // if it's `while`, we always make sure that loop bodies are properly scoped nestedly - // - when handler lowering is off (which means that loops are not rewritten to functions by default), this is crucial for the correct semantics - // - when handler lowering is on (which means loops are always rewritten to functions), this nested scoped blocks also matches the scope of the "while" functions + // see https://github.com/hkust-taco/mlscript/pull/356#discussion_r2588412258 val useNestedScoped = kw === `while` (if useNestedScoped then LoweringCtx.nestScoped else outerCtx).givenIn: var usesResTmp = false diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls index 7ba9fe55f2..4de738aba9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls @@ -4,14 +4,13 @@ -:effectHandlers fun start(x, f) = while x do f(() => x) -// NOTE: if `z` is defined in a nested scoped block +// if `z` and `g` is defined in a nested scoped block // and `g` is floated out to the top level, `z` in `g` gets unbound. -:effectHandlers +// so currently nested `if`s do not get nested `Scoped` blocks :sjs fun f(x) = if x do @@ -40,7 +39,6 @@ abstract class Effect with -:effectHandlers :sjs fun f(x) = if x then @@ -217,10 +215,6 @@ fun f(x) = //│ }; -// NOTE: currently while loops are always rewritten into functions -// when `:effectHanlders` are set, so this "dontRewriteWhile" has no effect here -:dontRewriteWhile -:effectHandlers fun f(x) = while x do while x do @@ -254,10 +248,17 @@ foo(h) -:effectHandlers fun f(x) = while x do while x do let z = 3 fun g() = z g() + + + +fun start(x, f) = + while x do + let z = 2 + fun g() = z + g() From 43fdc78b728c47211c2eaa437970ff8bf53aefac Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 5 Dec 2025 01:07:45 +0800 Subject: [PATCH 55/72] update loop rewriting flag --- hkmc2/shared/src/main/scala/hkmc2/Config.scala | 2 +- hkmc2/shared/src/test/mlscript/lifter/Loops.mls | 1 - hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/Config.scala b/hkmc2/shared/src/main/scala/hkmc2/Config.scala index 4d4540b594..109a40726d 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/Config.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/Config.scala @@ -46,7 +46,7 @@ object Config: effectHandlers = N, liftDefns = N, target = CompilationTarget.JS, - rewriteWhileLoops = true, + rewriteWhileLoops = false, stageCode = false, ) diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index 8906b7b43d..d6c1a0d75a 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -20,7 +20,6 @@ fs.0() let fs = mut [] //│ fs = [] -:dontRewriteWhile fun foo() = let i = 1 while i < 5 do diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala index 5f6f148b2e..0b2f3bf736 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/MLsDiffMaker.scala @@ -71,7 +71,7 @@ abstract class MLsDiffMaker extends DiffMaker: val liftDefns = NullaryCommand("lift") val importQQ = NullaryCommand("qq") val stageCode = NullaryCommand("staging") - val dontRewriteWhile = NullaryCommand("dontRewriteWhile") + val rewriteWhile = NullaryCommand("rewriteWhile") def mkConfig: Config = import Config.* @@ -99,7 +99,7 @@ abstract class MLsDiffMaker extends DiffMaker: liftDefns = Opt.when(liftDefns.isSet)(LiftDefns()), stageCode = stageCode.isSet, target = if wasm.isSet then CompilationTarget.Wasm else CompilationTarget.JS, - rewriteWhileLoops = false //!dontRewriteWhile.isSet, + rewriteWhileLoops = rewriteWhile.isSet, ) From 08d4467433684ced96ead7987e15e50665d4305c Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 5 Dec 2025 01:14:20 +0800 Subject: [PATCH 56/72] some cleanup --- hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala | 2 -- hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala | 1 - hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala | 1 - hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala | 2 +- 4 files changed, 1 insertion(+), 5 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index ed64f3bb3d..bc507f26d6 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -191,7 +191,6 @@ sealed abstract class Block extends Product: (transformer.applyBlock(this), defns) - lazy val flattened: Block = this.flatten(identity) private def flatten(k: End => Block): Block = this match @@ -376,7 +375,6 @@ case class HandleBlock( rest: Block ) extends Block with ProductWithTail - object HandleBlock: def apply( lhs: Local, diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index ed968a587a..84b9936a25 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -636,7 +636,6 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: def block(t: Block, endSemi: Bool)(using Raise, Scope): Document = - // println(s"$t :::::::: ${t.definedVars}") val pre = blockPreamble(t.definedVarsNoScoped) val rest = returningTerm(t, endSemi) pre :: rest diff --git a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala index 03ab1ba2d5..695e40453b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/semantics/Split.scala @@ -24,7 +24,6 @@ enum Split extends AutoLocated with ProductWithTail: case Else(default: Term) case End - inline def ~:(head: Branch): Split = Split.Cons(head, this) def mkClone(using State): Split = this match diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index e5a22e21f5..a2f933df4e 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -133,7 +133,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: val (pre, js) = nestedScp.givenIn: jsb.worksheet(le) - val preStr = pre.stripBreaks.mkString(100) //+ varsFromScopedStr + val preStr = pre.stripBreaks.mkString(100) val jsStr = js.stripBreaks.mkString(100) if showSanitizedJS.isSet then output(s"JS:") From 9d6b59cc8d064637b9ed4c9b6642cca48d58d92c Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 5 Dec 2025 17:43:32 +0800 Subject: [PATCH 57/72] Change the form of scope.locally --- .../main/scala/hkmc2/codegen/Lowering.scala | 17 ++- .../test/mlscript/codegen/NestedScoped.mls | 114 ++++++++++++++---- 2 files changed, 103 insertions(+), 28 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 7ca8998a07..1602dc6fd2 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -546,7 +546,20 @@ class Lowering()(using Config, TL, Raise, State, Ctx): conclude(Value.Ref(ctx.builtins.debug.getLocals, N).withLocOf(f)) case t if instantiatedResolvedBms.exists(_ is ctx.builtins.scope.locally) => arg match - case Tup(Fld(_, Lam(ParamList(_, Nil, N), body), N) :: Nil) => + case Tup(flds) => + val body = (flds.foldRight[Term](UnitVal()) { + case (Fld(_, t, _), UnitVal()) => Blk(Nil, t) + case (Fld(_, t, _), Blk(stmts, res)) => Blk(t :: stmts, res) + case (_, _: UnitVal | Blk) => + ErrorReport( + msg"Unsupported form for scope.locally." -> + t.toLoc :: Nil, + source = Diagnostic.Source.Compilation) + Error + case _ => ??? // impossible + }) match + case Blk(stmts, res) => Blk(stmts.reverse, res) + case t => t LoweringCtx.nestScoped.givenIn: val res = block(Nil, R(body))(k) val scopedSyms = loweringCtx.getCollectedSym @@ -554,7 +567,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case _ => return fail: ErrorReport( - msg"locally requires a lambda with no parameter." -> + msg"Unsupported form for scope.locally." -> t.toLoc :: Nil, source = Diagnostic.Source.Compilation) // * Due to whacky JS semantics, we need to make sure that selections leading to a call diff --git a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls index 388728962a..a249ca7f30 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls @@ -3,8 +3,7 @@ :lot :sjs -scope.locally of () => - 42 +scope.locally of 42 //│ JS (unsanitized): //│ 42 //│ Lowered: @@ -20,7 +19,7 @@ scope.locally of () => :lot :sjs -scope.locally of () => +scope.locally of let x = 42 in x //│ JS (unsanitized): //│ let x; /** scoped **/ x = 42; x @@ -40,40 +39,82 @@ scope.locally of () => //│ = 42 +:lot +:sjs +scope.locally of ( + let x = 42 + x +) +//│ JS (unsanitized): +//│ let x1; /** scoped **/ x1 = 42; x1 +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped: +//│ syms = HashSet(x) +//│ body = Assign: +//│ lhs = x +//│ rhs = Lit of IntLit of 42 +//│ rest = Return: \ +//│ res = Ref: +//│ l = x +//│ disamb = N +//│ implct = true +//│ = 42 + + +:lot +:sjs +scope.locally of scope.locally of ( + let x = 42 + x +) +//│ JS (unsanitized): +//│ { let x2; /** scoped **/ x2 = 42; x2 } +//│ Lowered: +//│ Program: +//│ imports = Nil +//│ main = Scoped: +//│ syms = HashSet() +//│ body = Scoped: +//│ syms = HashSet(x) +//│ body = Assign: +//│ lhs = x +//│ rhs = Lit of IntLit of 42 +//│ rest = Return: \ +//│ res = Ref: +//│ l = x +//│ disamb = N +//│ implct = true +//│ = 42 + + + :e x //│ ╔══[ERROR] Name not found: x -//│ ║ l.44: x +//│ ║ l.94: x //│ ╙── ^ -:ge -scope.locally of x => - let y = 1 in x + y -//│ ╔══[COMPILATION ERROR] locally requires a lambda with no parameter. -//│ ║ l.51: scope.locally of x => -//│ ╙── ^^^^^^^^^^^^^ - - - :ssjs fun foo(x) = let z = 1 - scope.locally of () => + scope.locally of let y = 1 in x + y + z //│ JS: //│ foo = function foo(...args) { //│ runtime.checkArgs("foo", 1, true, args.length); -//│ let x1 = args[0]; +//│ let x3 = args[0]; //│ let z; /** scoped **/{ //│ let y, tmp; /** scoped **/ //│ z = 1; //│ y = 1; -//│ tmp = x1 + y; +//│ tmp = x3 + y; //│ return tmp + z //│ } //│ }; -//│ block$res5 = undefined; +//│ block$res6 = undefined; foo(1) @@ -83,9 +124,10 @@ foo(1) :sjs fun f() = if true then - scope.locally of () => + scope.locally of ( let a = 4 a + 4 + ) else let b = 5 b + 9 @@ -108,9 +150,10 @@ fun f() = let a = 4 a + 4 else - scope.locally of () => + scope.locally of ( let b = 5 b + 9 + ) //│ JS (unsanitized): //│ let f1; /** scoped **/ //│ f1 = function f() { @@ -130,17 +173,18 @@ fun g(x, y, z) = x then 0 y then 0 z then - scope.locally of () => + scope.locally of ( let a = 1 (_ + a) + ) //│ JS (unsanitized): //│ let g; /** scoped **/ -//│ g = function g(x1, y, z) { +//│ g = function g(x3, y, z) { //│ let tmp, tmp1; /** scoped **/ //│ let lambda; //│ split_root$: { //│ split_1$: { -//│ if (x1 === true) { +//│ if (x3 === true) { //│ break split_1$ //│ } else { //│ if (y === true) { @@ -174,14 +218,15 @@ fun foo(x, y) = if not x > z.0 do print("rua") let m = x + y - scope.locally of () => + scope.locally of ( let w1 = z.1 + m let w2 = z.2 - m if w1 == w2 do print("ruarua") + ) //│ JS (unsanitized): //│ let foo1; /** scoped **/ -//│ foo1 = function foo(x1, y) { +//│ foo1 = function foo(x3, y) { //│ let z, scrut, m, tmp, tmp1; /** scoped **/{ //│ let w1, w2, scrut1; /** scoped **/ //│ z = globalThis.Object.freeze([ @@ -189,14 +234,14 @@ fun foo(x, y) = //│ 2, //│ 3 //│ ]); -//│ tmp = x1 > z[0]; +//│ tmp = x3 > z[0]; //│ scrut = ! tmp; //│ if (scrut === true) { //│ tmp1 = Predef.print("rua"); //│ } else { //│ tmp1 = runtime.Unit; //│ } -//│ m = x1 + y; +//│ m = x3 + y; //│ w1 = z[1] + m; //│ w2 = z[2] - m; //│ scrut1 = Predef.equals(w1, w2); @@ -204,3 +249,20 @@ fun foo(x, y) = //│ } //│ }; + +// does nothing +scope.locally() + + +:sjs +scope.locally of (let x = 1 in x), (let y = 2 in y) +//│ JS (unsanitized): +//│ let x3, y; /** scoped **/ x3 = 1; y = 2; y +//│ = 2 + + +:e +scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ╔══[ERROR] Name not found: x +//│ ║ l.265: scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ╙── ^ From 9f23fed021d596052a4da05a364c2d2b84ce1881 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 5 Dec 2025 17:51:44 +0800 Subject: [PATCH 58/72] Fix the missing case --- hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala index a2f933df4e..b1f17f0821 100644 --- a/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala +++ b/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala @@ -114,6 +114,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker: case Return(res, implct) => assert(implct) Assign(resSym, res, Return(Value.Lit(syntax.Tree.UnitLit(false)), true)) + case _: Scoped => ??? // impossible. mapTail has handled this case specially case tl: (Throw | Break | Continue) => tl ) if showLoweredTree.isSet then From 70ed84a84cd079a3a2ad9937cf91818d3eff45d3 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 5 Dec 2025 18:11:31 +0800 Subject: [PATCH 59/72] pr comments --- .../main/scala/hkmc2/codegen/Lowering.scala | 20 +-- .../test/mlscript/codegen/ScopedBlocks.mls | 48 +++++ .../codegen/ScopedBlocksAndHandlers.mls | 164 ------------------ 3 files changed, 58 insertions(+), 174 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 1602dc6fd2..0fb860f5d5 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -44,6 +44,7 @@ class LoweringCtx( val map = initMap def collectScopedSym(s: Symbol) = definedSymsDuringLowering.add(s) + def collectScopedSym(s: Symbol*) = definedSymsDuringLowering.addAll(s) def getCollectedSym: collection.Set[Symbol] = definedSymsDuringLowering /* def +(kv: (Local, Value)): Subst = @@ -159,11 +160,11 @@ class Lowering()(using Config, TL, Raise, State, Ctx): blockImpl(stats, L((mut, RcdArg(S(l), r) :: flds)))(k) case (decl @ LetDecl(sym, annotations)) :: (stats @ ((_: DefineVar) :: _)) => reportAnnotations(decl, annotations) - if sym.asTrm.fold(true)(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) + if sym.asTrm.forall(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) blockImpl(stats, res)(k) case (decl @ LetDecl(sym, annotations)) :: stats => reportAnnotations(decl, annotations) - if sym.asTrm.fold(true)(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) + if sym.asTrm.forall(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) blockImpl(DefineVar(sym, Term.Lit(Tree.UnitLit(false))) :: stats, res)(k) case DefineVar(sym, rhs) :: stats => term(rhs): r => @@ -1177,11 +1178,12 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") - loweringCtx.collectScopedSym(enterMsgSym) - loweringCtx.collectScopedSym(prevIndentLvlSym) - loweringCtx.collectScopedSym(resSym) - loweringCtx.collectScopedSym(retMsgSym) - loweringCtx.collectScopedSym(resInspectedSym) + loweringCtx.collectScopedSym( + enterMsgSym, + prevIndentLvlSym, + resSym, + retMsgSym, + resInspectedSym) for (s, _) <- psInspectedSyms do loweringCtx.collectScopedSym(s) val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): @@ -1190,9 +1192,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc val tmp1, tmp2, tmp3 = TempSymbol(N) - loweringCtx.collectScopedSym(tmp1) - loweringCtx.collectScopedSym(tmp2) - loweringCtx.collectScopedSym(tmp3) + loweringCtx.collectScopedSym(tmp1, tmp2, tmp3) assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls index ad3a9ca872..faea7790d6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls @@ -377,3 +377,51 @@ module M with //│ static [definitionMetadata] = ["class", "M"]; //│ }); +:sjs +fun f(x) = + while x do + let y = x + x +//│ JS (unsanitized): +//│ let f6; /** scoped **/ +//│ f6 = function f(x1) { +//│ let tmp2; /** scoped **/ +//│ tmp3: while (true) { +//│ let y1; /** scoped **/ +//│ if (x1 === true) { +//│ y1 = x1; +//│ tmp2 = x1; +//│ continue tmp3 +//│ } else { tmp2 = runtime.Unit; } +//│ break; +//│ } +//│ return tmp2 +//│ }; + + +:sjs +:rewriteWhile +fun f(x) = + while x do + let y = x + x +//│ JS (unsanitized): +//│ let f7; /** scoped **/ +//│ f7 = function f(x1) { +//│ let tmp2, while1, tmp3, tmp4; /** scoped **/ +//│ tmp2 = undefined; +//│ while1 = (undefined, function () { +//│ let y1; /** scoped **/ +//│ if (x1 === true) { +//│ y1 = x1; +//│ tmp2 = x1; +//│ return while1() +//│ } else { +//│ tmp2 = runtime.Unit; +//│ } +//│ return runtime.LoopEnd +//│ }); +//│ tmp3 = while1(); +//│ tmp4 = tmp3 !== runtime.LoopEnd; +//│ if (tmp4 === true) { return tmp3 } else { return tmp2 } +//│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls index 4de738aba9..76ad2c3e21 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls @@ -39,7 +39,6 @@ abstract class Effect with -:sjs fun f(x) = if x then if x then @@ -50,169 +49,6 @@ fun f(x) = k(x + z) print(h.perform()) else 0 -//│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f(x) { -//│ let z, tmp, tmp1; /** scoped **/ -//│ let handleBlock$, res, Cont$func$f$1, doUnwind; -//│ globalThis.Object.freeze(class Cont$func$f$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$func$f$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp2; -//│ tmp2 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 9) { -//│ res = value$; -//│ } else if (this.pc === 8) { -//│ tmp = value$; -//│ } -//│ contLoop: while (true) { -//│ if (this.pc === 9) { -//│ throw res -//│ } else if (this.pc === 8) { -//│ return tmp -//│ } -//│ break; -//│ } -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$func$f$"]; -//│ }); -//│ doUnwind = function doUnwind(res1, pc) { -//│ res1.contTrace.last.next = new Cont$func$f$1(pc); -//│ res1.contTrace.last = res1.contTrace.last.next; -//│ return res1 -//│ }; -//│ handleBlock$ = function handleBlock$() { -//│ let h, res1, Cont$handleBlock$h$1, doUnwind1, Handler$h$1; -//│ globalThis.Object.freeze(class Handler$h$ extends Effect1 { -//│ static { -//│ Handler$h$1 = this -//│ } -//│ constructor() { -//│ let tmp2; -//│ tmp2 = super(); -//│ } -//│ perform(x1) { -//│ let hdlrFun; -//│ hdlrFun = function hdlrFun(k) { -//│ let tmp2, tmp3; /** scoped **/ -//│ let Cont$handler$h$perform$1, doUnwind2; -//│ globalThis.Object.freeze(class Cont$handler$h$perform$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handler$h$perform$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp4; -//│ tmp4 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 4) { -//│ tmp2 = value$; -//│ } -//│ contLoop: while (true) { -//│ if (this.pc === 4) { -//│ tmp3 = x1 + z; -//│ this.pc = 5; -//│ continue contLoop -//│ } else if (this.pc === 5) { -//│ return runtime.safeCall(k(tmp3)) -//│ } -//│ break; -//│ } -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handler$h$perform$"]; -//│ }); -//│ doUnwind2 = function doUnwind(res2, pc) { -//│ res2.contTrace.last.next = new Cont$handler$h$perform$1(pc); -//│ res2.contTrace.last = res2.contTrace.last.next; -//│ return res2 -//│ }; -//│ tmp2 = Predef.print(x1); -//│ if (tmp2 instanceof runtime.EffectSig.class) { -//│ return doUnwind2(tmp2, 4) -//│ } -//│ tmp3 = x1 + z; -//│ return runtime.safeCall(k(tmp3)) -//│ }; -//│ return runtime.mkEffect(this, hdlrFun) -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Handler$h$"]; -//│ }); -//│ h = new Handler$h$1(); -//│ globalThis.Object.freeze(class Cont$handleBlock$h$ extends runtime.FunctionContFrame.class { -//│ static { -//│ Cont$handleBlock$h$1 = this -//│ } -//│ constructor(pc) { -//│ let tmp2; -//│ tmp2 = super(null); -//│ this.pc = pc; -//│ } -//│ resume(value$) { -//│ if (this.pc === 1) { -//│ tmp1 = value$; -//│ } else if (this.pc === 2) { -//│ res1 = value$; -//│ } -//│ contLoop: while (true) { -//│ if (this.pc === 3) { -//│ res1 = Predef.print(tmp1); -//│ if (res1 instanceof runtime.EffectSig.class) { -//│ return this.doUnwind(res1, 2) -//│ } -//│ this.pc = 2; -//│ continue contLoop -//│ } else if (this.pc === 1) { -//│ this.pc = 3; -//│ continue contLoop -//│ } else if (this.pc === 2) { -//│ return res1 -//│ } -//│ break; -//│ } -//│ } -//│ toString() { return runtime.render(this); } -//│ static [definitionMetadata] = ["class", "Cont$handleBlock$h$"]; -//│ }); -//│ doUnwind1 = function doUnwind(res2, pc) { -//│ res2.contTrace.last.next = new Cont$handleBlock$h$1(pc); -//│ return runtime.handleBlockImpl(res2, h) -//│ }; -//│ tmp1 = runtime.safeCall(h.perform()); -//│ if (tmp1 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(tmp1, 1) -//│ } -//│ res1 = Predef.print(tmp1); -//│ if (res1 instanceof runtime.EffectSig.class) { -//│ return doUnwind1(res1, 2) -//│ } -//│ return res1 -//│ }; -//│ if (x === true) { -//│ if (x === true) { -//│ z = 4; -//│ tmp = handleBlock$(); -//│ if (tmp instanceof runtime.EffectSig.class) { -//│ return doUnwind(tmp, 8) -//│ } -//│ return tmp -//│ } else { -//│ res = globalThis.Object.freeze(new globalThis.Error("match error")); -//│ if (res instanceof runtime.EffectSig.class) { -//│ return doUnwind(res, 9) -//│ } -//│ throw res -//│ } -//│ } else { return 0 } -//│ }; fun f(x) = From 8c11671377d4ac969f221aeb7d85aec86c977870 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 5 Dec 2025 18:27:12 +0800 Subject: [PATCH 60/72] cleanup and no "/** scoped **/" --- .../src/main/scala/hkmc2/codegen/Block.scala | 11 ++- .../main/scala/hkmc2/codegen/Lowering.scala | 2 - .../scala/hkmc2/codegen/js/JSBuilder.scala | 32 +++---- .../src/test/mlscript-compile/Predef.mjs | 30 +++--- .../src/test/mlscript-compile/Runtime.mjs | 92 +++++++++---------- .../OverloadedModulesInSignatures.mls | 4 +- .../backlog/NonReturningStatements.mls | 2 +- .../src/test/mlscript/backlog/ToTriage.mls | 60 ++++++------ .../src/test/mlscript/basics/BadDefs.mls | 4 +- .../basics/CompanionModules_Classes.mls | 4 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../shared/src/test/mlscript/basics/Drop.mls | 6 +- .../test/mlscript/basics/FunnyRecordKeys.mls | 4 +- .../src/test/mlscript/basics/Inheritance.mls | 2 +- .../src/test/mlscript/basics/LazySpreads.mls | 8 +- .../mlscript/basics/MemberProjections.mls | 4 +- .../test/mlscript/basics/MiscArrayTests.mls | 2 +- .../mlscript/basics/MultiParamListClasses.mls | 4 +- .../test/mlscript/basics/MultiParamLists.mls | 22 ++--- .../mlscript/basics/MultilineExpressions.mls | 2 +- .../src/test/mlscript/basics/MutArr.mls | 4 +- .../src/test/mlscript/basics/MutRcd.mls | 2 +- .../src/test/mlscript/basics/MutVal.mls | 2 +- .../src/test/mlscript/basics/NewMut.mls | 4 +- .../src/test/mlscript/basics/PartialApps.mls | 9 +- .../mlscript/basics/PureTermStatements.mls | 2 +- .../mlscript/basics/ShortcircuitingOps.mls | 12 +-- .../src/test/mlscript/basics/StrTest.mls | 2 +- .../src/test/mlscript/basics/Underscores.mls | 2 +- .../src/test/mlscript/bbml/bbCodeGen.mls | 30 +++--- .../src/test/mlscript/bbml/bbGetters.mls | 8 +- .../src/test/mlscript/codegen/Arrays.mls | 4 +- .../src/test/mlscript/codegen/BadInit.mls | 4 +- .../src/test/mlscript/codegen/BadNew.mls | 2 +- .../src/test/mlscript/codegen/BasicTerms.mls | 2 +- .../src/test/mlscript/codegen/BuiltinOps.mls | 4 +- .../src/test/mlscript/codegen/CaseOfCase.mls | 4 +- .../test/mlscript/codegen/CaseShorthand.mls | 14 +-- .../test/mlscript/codegen/ClassInClass.mls | 8 +- .../src/test/mlscript/codegen/ClassInFun.mls | 8 +- .../test/mlscript/codegen/ClassMatching.mls | 18 ++-- .../src/test/mlscript/codegen/Classes.mls | 8 +- .../src/test/mlscript/codegen/Comma.mls | 20 +--- .../src/test/mlscript/codegen/ConsoleLog.mls | 8 +- .../test/mlscript/codegen/DelayedLetInit.mls | 18 ++-- .../src/test/mlscript/codegen/EarlyReturn.mls | 4 +- .../src/test/mlscript/codegen/Formatting.mls | 18 ++-- .../src/test/mlscript/codegen/FunInClass.mls | 26 +++--- .../test/mlscript/codegen/FunctionsThis.mls | 6 +- .../src/test/mlscript/codegen/Getters.mls | 47 +++++----- .../src/test/mlscript/codegen/GlobalThis.mls | 6 +- .../src/test/mlscript/codegen/Hygiene.mls | 24 ++--- .../src/test/mlscript/codegen/IfThenElse.mls | 10 +- .../src/test/mlscript/codegen/ImportMLs.mls | 10 +- .../src/test/mlscript/codegen/ImportedOps.mls | 8 +- .../test/mlscript/codegen/InlineLambdas.mls | 12 +-- .../src/test/mlscript/codegen/Lambdas.mls | 2 +- .../test/mlscript/codegen/MergeMatchArms.mls | 12 +-- .../test/mlscript/codegen/ModuleMethods.mls | 4 +- .../src/test/mlscript/codegen/Modules.mls | 10 +- .../test/mlscript/codegen/NestedScoped.mls | 42 ++++----- .../test/mlscript/codegen/OpenWildcard.mls | 2 +- .../src/test/mlscript/codegen/OptMatch.mls | 6 +- .../test/mlscript/codegen/ParamClasses.mls | 30 +++--- .../src/test/mlscript/codegen/PartialApps.mls | 27 +++--- .../test/mlscript/codegen/PlainClasses.mls | 28 +++--- .../src/test/mlscript/codegen/Primes.mls | 2 +- .../shared/src/test/mlscript/codegen/Pwd.mls | 2 +- .../src/test/mlscript/codegen/Quasiquotes.mls | 2 +- .../src/test/mlscript/codegen/RandomStuff.mls | 26 ++---- .../test/mlscript/codegen/SanityChecks.mls | 8 +- .../test/mlscript/codegen/ScopedBlocks.mls | 60 ++++++------ .../codegen/ScopedBlocksAndHandlers.mls | 4 +- .../test/mlscript/codegen/SelfReferences.mls | 6 +- .../src/test/mlscript/codegen/SetIn.mls | 14 +-- .../src/test/mlscript/codegen/Spreads.mls | 2 +- .../shared/src/test/mlscript/codegen/This.mls | 7 +- .../mlscript/codegen/ThisCallVariations.mls | 4 +- .../src/test/mlscript/codegen/ThisCalls.mls | 2 +- .../src/test/mlscript/codegen/Throw.mls | 6 +- .../src/test/mlscript/codegen/TraceLog.mls | 4 +- .../src/test/mlscript/codegen/UnitValue.mls | 10 +- .../src/test/mlscript/codegen/While.mls | 20 ++-- .../src/test/mlscript/ctx/ClassCtxParams.mls | 2 +- .../src/test/mlscript/ctx/EtaExpansion.mls | 20 ++-- .../test/mlscript/ctx/MissingDefinitions2.mls | 2 +- .../src/test/mlscript/handlers/Debugging.mls | 4 +- .../src/test/mlscript/handlers/Effects.mls | 2 +- .../test/mlscript/handlers/EffectsHygiene.mls | 4 +- .../mlscript/handlers/EffectsInClasses.mls | 4 +- .../mlscript/handlers/RecursiveHandlers.mls | 6 +- .../test/mlscript/handlers/SetInHandlers.mls | 2 +- .../test/mlscript/handlers/StackSafety.mls | 14 +-- .../src/test/mlscript/interop/Arrays.mls | 4 +- .../src/test/mlscript/lifter/ClassInFun.mls | 6 +- .../test/mlscript/lifter/CompanionsInFun.mls | 4 +- .../src/test/mlscript/lifter/DefnsInClass.mls | 6 +- .../src/test/mlscript/lifter/FunInFun.mls | 30 +++--- .../src/test/mlscript/lifter/Imports.mls | 2 +- .../shared/src/test/mlscript/lifter/Loops.mls | 6 +- .../test/mlscript/lifter/ModulesObjects.mls | 12 +-- .../src/test/mlscript/lifter/Mutation.mls | 12 +-- .../test/mlscript/lifter/StackSafetyLift.mls | 14 +-- .../src/test/mlscript/objbuf/Mutation.mls | 6 +- .../src/test/mlscript/parser/PrefixOps.mls | 4 +- .../test/mlscript/std/FingerTreeListTest.mls | 6 +- .../mlscript/ucs/future/SymbolicClass.mls | 4 +- .../test/mlscript/ucs/general/JoinPoints.mls | 6 +- .../ucs/general/LogicalConnectives.mls | 2 +- .../ucs/normalization/Deduplication.mls | 41 ++++----- .../normalization/ExcessiveDeduplication.mls | 2 +- .../ucs/normalization/SimplePairMatches.mls | 6 +- .../ucs/patterns/ConjunctionPattern.mls | 4 +- .../test/mlscript/ucs/patterns/RestTuple.mls | 12 +-- .../src/test/mlscript/ups/MatchResult.mls | 4 +- .../src/test/mlscript/ups/SimpleTransform.mls | 4 +- 116 files changed, 595 insertions(+), 671 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index bc507f26d6..a21a6980b7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -29,7 +29,10 @@ sealed abstract class Block extends Product: case _: End => true case _ => false - // FIXME: clean up this along with `definedVars` below... + // this variation of `definedVars` excludes the `syms` in `Scoped` blocks + // so that `Scoped` blocks' vars can be seen in the output js code; + // otherwise `blockPreamble` allocates all the `definedVars` such that + // we cannot see and test vars generated because of the existence of the `Scoped` blocks lazy val definedVarsNoScoped: Set[Local] = this match case _: Return | _: Throw => Set.empty case Begin(sub, rst) => sub.definedVarsNoScoped ++ rst.definedVarsNoScoped @@ -70,7 +73,7 @@ sealed abstract class Block extends Product: case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => rst.definedVars + res case TryBlock(sub, fin, rst) => sub.definedVars ++ fin.definedVars ++ rst.definedVars case Label(lbl, _, bod, rst) => bod.definedVars ++ rst.definedVars - case Scoped(syms, body) => body.definedVars// -- syms + case Scoped(syms, body) => body.definedVars lazy val size: Int = this match case _: Return | _: Throw | _: End | _: Break | _: Continue => 1 @@ -125,7 +128,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVars ++ rest.freeVars case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars) - case Scoped(syms, body) => body.freeVars// -- syms + case Scoped(syms, body) => body.freeVars case End(msg) => Set.empty lazy val freeVarsLLIR: Set[Local] = this match @@ -146,7 +149,7 @@ sealed abstract class Block extends Product: case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym) case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) => (bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR) - case Scoped(syms, body) => body.freeVarsLLIR// -- syms + case Scoped(syms, body) => body.freeVarsLLIR case End(msg) => Set.empty lazy val subBlocks: Ls[Block] = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 0fb860f5d5..cddbec09d6 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -782,7 +782,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): source = Diagnostic.Source.Compilation ) - // NOTE: nothing in `quote...` is handled yet def quoteSplit(split: Split)(k: Result => Block)(using LoweringCtx): Block = split match case Split.Cons(Branch(scrutinee, pattern, continuation), tail) => quote(scrutinee): r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) @@ -880,7 +879,6 @@ class Lowering()(using Config, TL, Raise, State, Ctx): rec(rhs, Nil)(k) case Blk(LetDecl(sym, _) :: DefineVar(sym2, rhs) :: Nil, res) => // Let bindings require(sym2 is sym) - loweringCtx.collectScopedSym(sym) setupSymbol(sym){r1 => val l1, l2, l3, l4, l5 = new TempSymbol(N) val arrSym = new TempSymbol(N, "arr") diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 84b9936a25..9b6b972a2c 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -498,7 +498,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: scope.allocateName(lbl) // [fixme:0] TODO check scope and allocate local variables here (see: https://github.com/hkust-taco/mlscript/pull/293#issuecomment-2792229849) - + doc" # ${getVar(lbl, lbl.toLoc)}:${if loop then doc" while (true)" else ""} " :: braced { nonNestedScoped(bod)(bd => returningTerm(bd, endSemi = true)) :: (if loop then doc" # break;" else doc"") } :: returningTerm(rst, endSemi) @@ -590,35 +590,31 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: then "./" + os.Path(path).relativeTo(wd).toString else path doc"""import ${getVar(i._1, N)} from "${relPath}";""" - // NOTE: this is to make sure that we are NOT generating the top level - // block in a nested scope, because for exported symbols they are looked up in the outer scope - val (scopedSyms, unscopedMain) = p.main match - case Scoped(syms, body) /* if exprt.isDefined */ => (syms, body) - case _ => (Set.empty, p.main) - imps.mkDocument(doc" # ") :/: (genLetDecls(scopedSyms.toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)), true) :: block(unscopedMain, endSemi = false)).stripBreaks :: ( + imps.mkDocument(doc" # ") :/: + nonNestedScoped(p.main)(block(_, endSemi = false)).stripBreaks :: + locally: exprt match - case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" - case N => doc"" - ) + case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" + case N => doc"" def worksheet(p: Program)(using Raise, Scope): (Document, Document) = reserveNames(p) lazy val imps = p.imports.map: i => doc"""${getVar(i._1, N)} = await import("${i._2.toString}").then(m => m.default ?? m);""" p.main match - case Scoped(syms, body) => - blockPreamble(p.imports.map(_._1).toSeq ++ syms.toSeq) -> - (imps.mkDocument(doc" # ") :/: block(body, endSemi = false).stripBreaks) - case _ => - blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVarsNoScoped.toSeq) -> - (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) + case Scoped(syms, body) => + blockPreamble(p.imports.map(_._1).toSeq ++ syms.toSeq) -> + (imps.mkDocument(doc" # ") :/: block(body, endSemi = false).stripBreaks) + case _ => + blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVarsNoScoped.toSeq) -> + (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) def genLetDecls(vars: Iterator[(Symbol, Str)], isScoped: Bool): Document = if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme .toList.mkDocument(", ") - :: (if isScoped then doc"; /** scoped **/" else doc";") + :: doc";" def blockPreamble(ss: Iterable[Symbol])(using Raise, Scope): Document = // TODO document: mutable var assnts require the lookup @@ -641,7 +637,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: pre :: rest def body(t: Block, endSemi: Bool)(using Raise, Scope): Document = scope.nest givenIn: - nonNestedScoped(t)(bd => block(bd, endSemi)) + nonNestedScoped(t)(bd => block(bd, endSemi)) def defineProperty(target: Document, prop: Str, value: Document, enumerable: Bool = false): Document = doc"Object.defineProperty(${target}, ${prop.escaped}, ${ diff --git a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs index 478a8b0fd4..407eebfde8 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Predef.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Predef.mjs @@ -5,7 +5,7 @@ import Term from "./Term.mjs"; import RuntimeJS from "./RuntimeJS.mjs"; import Runtime from "./Runtime.mjs"; import Rendering from "./Rendering.mjs"; -let Predef1; /** scoped **/ +let Predef1; globalThis.Object.freeze(class Predef { static { Predef1 = this @@ -58,12 +58,12 @@ globalThis.Object.freeze(class Predef { return runtime.safeCall(f(x)) } static tap(x, f) { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(f(x)); return (tmp , x) } static pat(f, x) { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(f(x)); return (tmp , x) } @@ -72,14 +72,14 @@ globalThis.Object.freeze(class Predef { } static andThen(f, g) { return (x) => { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(f(x)); return runtime.safeCall(g(tmp)) } } static compose(f, g) { return (x) => { - let tmp; /** scoped **/ + let tmp; tmp = runtime.safeCall(g(x)); return runtime.safeCall(f(tmp)) } @@ -100,7 +100,7 @@ globalThis.Object.freeze(class Predef { } } static equals(a, b) { - let scrut, scrut1, scrut2, ac, scrut3, md, scrut4, scrut5, scrut6, scrut7, scrut8, scrut9, scrut10, scrut11, tmp, lambda, lambda1, tmp1, tmp2, tmp3; /** scoped **/ + let scrut, scrut1, scrut2, ac, scrut3, md, scrut4, scrut5, scrut6, scrut7, scrut8, scrut9, scrut10, scrut11, tmp, lambda, lambda1, tmp1, tmp2, tmp3; split_root$: { split_1$: { scrut = a === b; @@ -113,7 +113,7 @@ globalThis.Object.freeze(class Predef { scrut1 = a.length === b.length; if (scrut1 === true) { lambda = (undefined, function (a1, i) { - let tmp4; /** scoped **/ + let tmp4; tmp4 = runtime.safeCall(b.at(i)); return Predef.equals(a1, tmp4) }); @@ -153,7 +153,7 @@ globalThis.Object.freeze(class Predef { scrut4 = md !== undefined; if (scrut4 === true) { lambda1 = (undefined, function (field) { - let scrut12, scrut13, tmp4; /** scoped **/ + let scrut12, scrut13, tmp4; split_root$4: { split_1$4: { scrut12 = field !== null; @@ -229,12 +229,12 @@ globalThis.Object.freeze(class Predef { return tmp } static nequals(a, b) { - let tmp; /** scoped **/ + let tmp; tmp = Predef.equals(a, b); return ! tmp } static print(...xs) { - let tmp, tmp1; /** scoped **/ + let tmp, tmp1; tmp = runtime.safeCall(Predef.map(Predef.renderAsStr)); tmp1 = runtime.safeCall(tmp(...xs)); return runtime.safeCall(globalThis.console.log(...tmp1)) @@ -247,7 +247,7 @@ globalThis.Object.freeze(class Predef { } } static notImplemented(msg) { - let tmp; /** scoped **/ + let tmp; tmp = "Not implemented: " + msg; throw globalThis.Error(tmp) } @@ -259,7 +259,7 @@ globalThis.Object.freeze(class Predef { } static foldr(f) { return (first, ...rest) => { - let len, scrut, i, init, tmp; /** scoped **/ + let len, scrut, i, init, tmp; len = rest.length; scrut = len === 0; if (scrut === true) { @@ -268,7 +268,7 @@ globalThis.Object.freeze(class Predef { i = len - 1; init = runtime.safeCall(rest.at(i)); tmp1: while (true) { - let scrut1, tmp2, tmp3, tmp4; /** scoped **/ + let scrut1, tmp2, tmp3, tmp4; scrut1 = i > 0; if (scrut1 === true) { tmp2 = i - 1; @@ -288,9 +288,9 @@ globalThis.Object.freeze(class Predef { } } static mkStr(...xs) { - let lambda, tmp; /** scoped **/ + let lambda, tmp; lambda = (undefined, function (acc, x) { - let tmp1, tmp2, tmp3; /** scoped **/ + let tmp1, tmp2, tmp3; if (typeof x === 'string') { tmp1 = true; } else { diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index 65c5fdda1e..fd84b4265b 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -6,7 +6,7 @@ import RuntimeJS from "./RuntimeJS.mjs"; import Rendering from "./Rendering.mjs"; import LazyArray from "./LazyArray.mjs"; import Iter from "./Iter.mjs"; -let Runtime1; /** scoped **/ +let Runtime1; globalThis.Object.freeze(class Runtime { static { Runtime1 = this @@ -74,10 +74,10 @@ globalThis.Object.freeze(class Runtime { } #_reified; resumeWith(value) { - let lambda; /** scoped **/ + let lambda; const this$EffectHandle = this; lambda = (undefined, function () { - let tmp; /** scoped **/ + let tmp; tmp = Runtime.resume(this$EffectHandle.reified.contTrace); return runtime.safeCall(tmp(value)) }); @@ -127,12 +127,12 @@ globalThis.Object.freeze(class Runtime { this.split = LazyArray.__split; } static slice(xs, i, j) { - let tmp; /** scoped **/ + let tmp; tmp = xs.length - j; return xs.slice(i, tmp) } static lazySlice(xs, i, j) { - let tmp; /** scoped **/ + let tmp; tmp = LazyArray.dropLeftRight(i, j); return runtime.safeCall(tmp(xs)) } @@ -140,7 +140,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(LazyArray.__concat(...args)) } static get(xs, i) { - let scrut, scrut1, tmp, tmp1, tmp2; /** scoped **/ + let scrut, scrut1, tmp, tmp1, tmp2; scrut = i >= xs.length; if (scrut === true) { throw globalThis.RangeError("Tuple.get: index out of bounds") @@ -173,7 +173,7 @@ globalThis.Object.freeze(class Runtime { return runtime.safeCall(string.startsWith(prefix)) } static get(string, i) { - let scrut; /** scoped **/ + let scrut; scrut = i >= string.length; if (scrut === true) { throw globalThis.RangeError("Str.get: index out of bounds") @@ -209,7 +209,7 @@ globalThis.Object.freeze(class Runtime { this.indentLvl = 0; } static indent() { - let scrut, prev, tmp; /** scoped **/ + let scrut, prev, tmp; scrut = TraceLogger.enabled; if (scrut === true) { prev = TraceLogger.indentLvl; @@ -221,7 +221,7 @@ globalThis.Object.freeze(class Runtime { } } static resetIndent(n) { - let scrut; /** scoped **/ + let scrut; scrut = TraceLogger.enabled; if (scrut === true) { TraceLogger.indentLvl = n; @@ -231,7 +231,7 @@ globalThis.Object.freeze(class Runtime { } } static log(msg) { - let scrut, tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ + let scrut, tmp, tmp1, tmp2, tmp3, tmp4; scrut = TraceLogger.enabled; if (scrut === true) { tmp = runtime.safeCall("| ".repeat(TraceLogger.indentLvl)); @@ -387,7 +387,7 @@ globalThis.Object.freeze(class Runtime { }) } delay() { - let lambda; /** scoped **/ + let lambda; lambda = (undefined, function (k) { Runtime.stackResume = k; return runtime.Unit @@ -409,13 +409,13 @@ globalThis.Object.freeze(class Runtime { } #v; zext() { - let tmp, tmp1; /** scoped **/ + let tmp, tmp1; tmp = Runtime.shl(1, 31); tmp1 = runtime.safeCall(Runtime.bitnot(tmp)); return Runtime.bitand(this.#v, tmp1) } sext() { - let tmp; /** scoped **/ + let tmp; tmp = Runtime.shl(1, 31); return Runtime.bitor(this.#v, tmp) } @@ -427,7 +427,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, scrut1, scrut2, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + let scrut, name, scrut1, scrut2, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; tmp = got < expected; lambda = (undefined, function () { lambda1 = (undefined, function () { @@ -484,7 +484,7 @@ globalThis.Object.freeze(class Runtime { } } static deboundMethod(mtdName, clsName) { - let tmp, tmp1, tmp2, tmp3; /** scoped **/ + let tmp, tmp1, tmp2, tmp3; tmp = "[debinding error] Method '" + mtdName; tmp1 = tmp + "' of class '"; tmp2 = tmp1 + clsName; @@ -492,7 +492,7 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error(tmp3) } static try(f) { - let res; /** scoped **/ + let res; res = runtime.safeCall(f()); if (res instanceof Runtime.EffectSig.class) { return Runtime.EffectHandle(res) @@ -501,7 +501,7 @@ globalThis.Object.freeze(class Runtime { } } static printRaw(x) { - let rcd, tmp; /** scoped **/ + let rcd, tmp; rcd = globalThis.Object.freeze({ indent: 2, breakLength: 76 @@ -513,9 +513,9 @@ globalThis.Object.freeze(class Runtime { return Runtime.mkEffect(Runtime.PrintStackEffect, showLocals) } static topLevelEffect(tr, debug) { - let tmp, tmp1; /** scoped **/ + let tmp, tmp1; tmp2: while (true) { - let scrut, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + let scrut, tmp3, tmp4, tmp5, tmp6; scrut = tr.handler === Runtime.PrintStackEffect; if (scrut === true) { tmp3 = Runtime.showStackTrace("Stack Trace:", tr, debug, tr.handlerFun); @@ -538,18 +538,18 @@ globalThis.Object.freeze(class Runtime { } } static showStackTrace(header, tr, debug, showLocals) { - let msg, curHandler, atTail, tmp, tmp1, tmp2, tmp3; /** scoped **/ + let msg, curHandler, atTail, tmp, tmp1, tmp2, tmp3; msg = header; curHandler = tr.contTrace; atTail = true; if (debug === true) { tmp4: while (true) { - let scrut, cur, scrut1, tmp5, tmp6, tmp7, tmp8; /** scoped **/ + let scrut, cur, scrut1, tmp5, tmp6, tmp7, tmp8; scrut = curHandler !== null; if (scrut === true) { cur = curHandler.next; tmp9: while (true) { - let scrut2, locals, curLocals, loc, loc1, localsMsg, scrut3, tmp10, tmp11, lambda, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18, tmp19, tmp20; /** scoped **/ + let scrut2, locals, curLocals, loc, loc1, localsMsg, scrut3, tmp10, tmp11, lambda, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18, tmp19, tmp20; scrut2 = cur !== null; if (scrut2 === true) { locals = cur.getLocals; @@ -568,7 +568,7 @@ globalThis.Object.freeze(class Runtime { scrut3 = curLocals.locals.length > 0; if (scrut3 === true) { lambda = (undefined, function (l) { - let tmp21, tmp22; /** scoped **/ + let tmp21, tmp22; tmp21 = l.localName + "="; tmp22 = Rendering.render(l.value); return tmp21 + tmp22 @@ -636,12 +636,12 @@ globalThis.Object.freeze(class Runtime { return msg } static showFunctionContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ + let result, scrut, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; if (cont instanceof Runtime.FunctionContFrame.class) { tmp = cont.constructor.name + "(pc="; result = tmp + cont.pc; lambda = (undefined, function (m, marker) { - let scrut3, tmp8, tmp9; /** scoped **/ + let scrut3, tmp8, tmp9; scrut3 = runtime.safeCall(m.has(cont)); if (scrut3 === true) { tmp8 = ", " + marker; @@ -682,11 +682,11 @@ globalThis.Object.freeze(class Runtime { } } static showHandlerContChain(cont, hl, vis, reps) { - let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; /** scoped **/ + let result, scrut, scrut1, scrut2, lambda, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; if (cont instanceof Runtime.HandlerContFrame.class) { result = cont.handler.constructor.name; lambda = (undefined, function (m, marker) { - let scrut3, tmp7, tmp8; /** scoped **/ + let scrut3, tmp7, tmp8; scrut3 = runtime.safeCall(m.has(cont)); if (scrut3 === true) { tmp7 = ", " + marker; @@ -727,21 +727,21 @@ globalThis.Object.freeze(class Runtime { } } static debugCont(cont) { - let tmp, tmp1, tmp2; /** scoped **/ + let tmp, tmp1, tmp2; tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showFunctionContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugHandler(cont) { - let tmp, tmp1, tmp2; /** scoped **/ + let tmp, tmp1, tmp2; tmp = globalThis.Object.freeze(new globalThis.Map()); tmp1 = globalThis.Object.freeze(new globalThis.Set()); tmp2 = Runtime.showHandlerContChain(cont, tmp, tmp1, 0); return runtime.safeCall(globalThis.console.log(tmp2)) } static debugContTrace(contTrace) { - let scrut, scrut1, vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ + let scrut, scrut1, vis, hl, cur, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; if (contTrace instanceof Runtime.ContTrace.class) { tmp = globalThis.console.log("resumed: ", contTrace.resumed); scrut = contTrace.last === contTrace; @@ -772,7 +772,7 @@ globalThis.Object.freeze(class Runtime { tmp10 = runtime.safeCall(globalThis.console.log(tmp9)); cur = contTrace.nextHandler; tmp13: while (true) { - let scrut2, tmp14, tmp15; /** scoped **/ + let scrut2, tmp14, tmp15; scrut2 = cur !== null; if (scrut2 === true) { tmp14 = Runtime.showHandlerContChain(cur, hl, vis, 0); @@ -792,7 +792,7 @@ globalThis.Object.freeze(class Runtime { } } static debugEff(eff) { - let tmp, tmp1, tmp2, tmp3; /** scoped **/ + let tmp, tmp1, tmp2, tmp3; if (eff instanceof Runtime.EffectSig.class) { tmp = runtime.safeCall(globalThis.console.log("Debug EffectSig:")); tmp1 = globalThis.console.log("handler: ", eff.handler.constructor.name); @@ -804,7 +804,7 @@ globalThis.Object.freeze(class Runtime { } } static mkEffect(handler, handlerFun) { - let res, tmp; /** scoped **/ + let res, tmp; tmp = new Runtime.ContTrace.class(null, null, null, null, false); res = new Runtime.EffectSig.class(tmp, handler, handlerFun); res.contTrace.last = res.contTrace; @@ -812,7 +812,7 @@ globalThis.Object.freeze(class Runtime { return res } static handleBlockImpl(cur, handler) { - let handlerFrame; /** scoped **/ + let handlerFrame; handlerFrame = new Runtime.HandlerContFrame.class(null, null, handler); cur.contTrace.lastHandler.nextHandler = handlerFrame; cur.contTrace.lastHandler = handlerFrame; @@ -820,7 +820,7 @@ globalThis.Object.freeze(class Runtime { return Runtime.handleEffects(cur) } static enterHandleBlock(handler, body) { - let cur; /** scoped **/ + let cur; cur = runtime.safeCall(body()); if (cur instanceof Runtime.EffectSig.class) { return Runtime.handleBlockImpl(cur, handler) @@ -829,9 +829,9 @@ globalThis.Object.freeze(class Runtime { } } static handleEffects(cur) { - let tmp; /** scoped **/ + let tmp; tmp1: while (true) { - let nxt, scrut, tmp2; /** scoped **/ + let nxt, scrut, tmp2; if (cur instanceof Runtime.EffectSig.class) { nxt = Runtime.handleEffect(cur); scrut = cur === nxt; @@ -851,10 +851,10 @@ globalThis.Object.freeze(class Runtime { return tmp } static handleEffect(cur) { - let prevHandlerFrame, scrut, handlerFrame, saved, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + let prevHandlerFrame, scrut, handlerFrame, saved, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; prevHandlerFrame = cur.contTrace; tmp6: while (true) { - let scrut3, scrut4; /** scoped **/ + let scrut3, scrut4; split_root$: { split_1$: { scrut3 = prevHandlerFrame.nextHandler !== null; @@ -914,7 +914,7 @@ globalThis.Object.freeze(class Runtime { } static resume(contTrace) { return (value) => { - let scrut, tmp, tmp1; /** scoped **/ + let scrut, tmp, tmp1; scrut = contTrace.resumed; if (scrut === true) { throw globalThis.Error("Multiple resumption") @@ -927,12 +927,12 @@ globalThis.Object.freeze(class Runtime { } } static resumeContTrace(contTrace, value) { - let cont, handlerCont, curDepth, tmp; /** scoped **/ + let cont, handlerCont, curDepth, tmp; cont = contTrace.next; handlerCont = contTrace.nextHandler; curDepth = Runtime.stackDepth; tmp1: while (true) { - let scrut, scrut1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ + let scrut, scrut1, tmp2, tmp3, tmp4, tmp5; if (cont instanceof Runtime.FunctionContFrame.class) { tmp2 = runtime.safeCall(cont.resume(value)); value = tmp2; @@ -976,7 +976,7 @@ globalThis.Object.freeze(class Runtime { return tmp } static checkDepth() { - let scrut, tmp, lambda; /** scoped **/ + let scrut, tmp, lambda; tmp = Runtime.stackDepth >= Runtime.stackLimit; lambda = (undefined, function () { return Runtime.stackHandler !== null @@ -989,14 +989,14 @@ globalThis.Object.freeze(class Runtime { } } static runStackSafe(limit, f) { - let result, tmp; /** scoped **/ + let result, tmp; Runtime.stackLimit = limit; Runtime.stackDepth = 1; Runtime.stackHandler = Runtime.StackDelayHandler; result = Runtime.enterHandleBlock(Runtime.StackDelayHandler, f); Runtime.stackDepth = 1; tmp1: while (true) { - let scrut, saved, tmp2; /** scoped **/ + let scrut, saved, tmp2; scrut = Runtime.stackResume !== null; if (scrut === true) { saved = Runtime.stackResume; @@ -1017,7 +1017,7 @@ globalThis.Object.freeze(class Runtime { return result } static plus_impl(lhs, rhs) { - let tmp; /** scoped **/ + let tmp; split_root$: { split_1$: { if (lhs instanceof Runtime.Int31.class) { diff --git a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls index f75cc10893..69abb2d790 100644 --- a/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls +++ b/hkmc2/shared/src/test/mlscript/OverloadedModulesInSignatures.mls @@ -81,7 +81,7 @@ fun f: TrmMod = 42 :sjs fun f = TrmMod //│ JS (unsanitized): -//│ let f5; /** scoped **/ f5 = function f() { let tmp4; /** scoped **/ tmp4 = TrmMod1(); return tmp4 }; +//│ let f5; f5 = function f() { let tmp4; tmp4 = TrmMod1(); return tmp4 }; :fixme :expect 42 @@ -92,7 +92,7 @@ f :sjs fun f: module TrmMod = TrmMod //│ JS (unsanitized): -//│ let f6; /** scoped **/ f6 = function f() { return TrmMod1.class }; +//│ let f6; f6 = function f() { return TrmMod1.class }; fun assertModule(module m: ClsMod): module ClsMod = m diff --git a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls index 18ba41070d..630a7d1107 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/NonReturningStatements.mls @@ -31,7 +31,7 @@ fun foo = :sjs foo //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = foo2(); tmp2 +//│ let tmp2; tmp2 = foo2(); tmp2 //│ > 1 //│ > ... diff --git a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls index a57153fd3d..c347dc8224 100644 --- a/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls +++ b/hkmc2/shared/src/test/mlscript/backlog/ToTriage.mls @@ -52,7 +52,7 @@ Infinity :sjs val Infinity = 1 //│ JS (unsanitized): -//│ let Infinity; /** scoped **/ Infinity = 1; +//│ let Infinity; Infinity = 1; //│ ═══[COMPILATION ERROR] [Uncaught SyntaxError] Identifier 'Infinity' has already been declared //│ Infinity = Infinity @@ -93,7 +93,7 @@ fun main() = object Cls(val x) with fun huh = x //│ JS (unsanitized): -//│ let Cls1; /** scoped **/ +//│ let Cls1; //│ globalThis.Object.freeze(class Cls { //│ static { //│ Cls1.class = globalThis.Object.freeze(new this) @@ -190,7 +190,7 @@ class D extends id(C) //│ ║ ^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let D1; /** scoped **/ +//│ let D1; //│ globalThis.Object.freeze(class D extends Predef.id { //│ static { //│ D1 = this @@ -239,11 +239,9 @@ set (x += 1; ()) fun baz = 1 fun bar() = baz //│ JS (unsanitized): -//│ let bar, baz; /** scoped **/ -//│ baz = function baz() { -//│ return 1 -//│ }; -//│ bar = function bar() { let tmp4; /** scoped **/ tmp4 = baz(); return tmp4 }; +//│ let bar, baz; +//│ baz = function baz() { return 1 }; +//│ bar = function bar() { let tmp4; tmp4 = baz(); return tmp4 }; // ——— ——— ——— @@ -252,13 +250,13 @@ import "../../mlscript-compile/Stack.mls" // The parser rejects this, but it should be allowed. open Stack { ::, Nil } //│ ╔══[ERROR] Illegal 'open' statement shape. -//│ ║ l.253: open Stack { ::, Nil } +//│ ║ l.251: open Stack { ::, Nil } //│ ╙── ^^^^^^^^^^^^^^^ // Instead, if we parenthesize the operator, it is rejected by the elaborator. open Stack { (::), Nil } //│ ╔══[ERROR] Illegal 'open' statement element. -//│ ║ l.259: open Stack { (::), Nil } +//│ ║ l.257: open Stack { (::), Nil } //│ ╙── ^^^^ open Stack { Nil, :: } @@ -266,7 +264,7 @@ open Stack { Nil, :: } :sjs 1 :: Nil //│ ╔══[ERROR] Module 'Stack' does not contain member '::' -//│ ║ l.267: 1 :: Nil +//│ ║ l.265: 1 :: Nil //│ ╙── ^^ //│ JS (unsanitized): //│ Stack["::"](1, Stack.Nil) @@ -277,9 +275,9 @@ open Stack { Nil, :: } mkStr of ... // hello "oops" //│ ╔══[PARSE ERROR] Expected start of expression in this position; found new line instead -//│ ║ l.277: mkStr of ... // hello +//│ ║ l.275: mkStr of ... // hello //│ ║ ^ -//│ ║ l.278: "oops" +//│ ║ l.276: "oops" //│ ╙── //│ = "oops" @@ -302,25 +300,25 @@ Foo(1, 2, 3).args :todo if Foo(1, 2, 3) is Foo(...args) then args //│ ╔══[ERROR] Unrecognized pattern (spread). -//│ ║ l.303: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.301: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^^^^ //│ ╔══[ERROR] Name not found: args -//│ ║ l.303: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.301: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^^ //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.303: if Foo(1, 2, 3) is Foo(...args) then args +//│ ║ l.301: if Foo(1, 2, 3) is Foo(...args) then args //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╔══[ERROR] The constructor does not take any arguments but found three arguments. -//│ ║ l.315: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] +//│ ║ l.313: if Foo(1, 2, 3) is Foo(a, b, c) then [a, b, c] //│ ╙── ^^^^^^^ //│ ═══[RUNTIME ERROR] Error: match error if Foo(1, 2, 3) is Foo(arg) then arg //│ ╔══[ERROR] The constructor does not take any arguments but found one argument. -//│ ║ l.321: if Foo(1, 2, 3) is Foo(arg) then arg +//│ ║ l.319: if Foo(1, 2, 3) is Foo(arg) then arg //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] Error: match error @@ -342,7 +340,7 @@ fun w(txt) = fs.writeFileSync(outFilePath, txt) // () //│ JS (unsanitized): -//│ let w; /** scoped **/ w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; +//│ let w; w = function w(txt) { return globalThis.fs.writeFileSync(outFilePath, txt) }; w("whoops") //│ ═══[RUNTIME ERROR] Error: MLscript call unexpectedly returned `undefined`, the forbidden value. @@ -354,10 +352,10 @@ fun foo() = let bar(x: Str): Str = x + x bar("test") //│ ╔══[ERROR] Unsupported let binding shape -//│ ║ l.354: let bar(x: Str): Str = x + x +//│ ║ l.352: let bar(x: Str): Str = x + x //│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^ //│ ╔══[ERROR] Expected 0 arguments, got 1 -//│ ║ l.355: bar("test") +//│ ║ l.353: bar("test") //│ ╙── ^^^^^^^^ // ——— ——— ——— @@ -367,10 +365,10 @@ fun foo() = module Foo(x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.367: module Foo(x) +//│ ║ l.365: module Foo(x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -391,10 +389,10 @@ module Foo(x) module Foo(val x) //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' //│ ╟── which references the symbol introduced here -//│ ║ l.391: module Foo(val x) +//│ ║ l.389: module Foo(val x) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ globalThis.Object.freeze(class Foo6 { //│ static { //│ Foo7 = this @@ -414,7 +412,7 @@ module Foo(val x) // TODO support syntax? data class Foo(mut x) //│ ╔══[ERROR] Expected a valid parameter, found 'mut'-modified identifier -//│ ║ l.415: data class Foo(mut x) +//│ ║ l.413: data class Foo(mut x) //│ ╙── ^ // ——— ——— ——— @@ -431,10 +429,10 @@ set this.pc = 0 set (this).pc = 0 //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.430: set this.pc = 0 +//│ ║ l.428: set this.pc = 0 //│ ╙── ^^^^ //│ ╔══[ERROR] Cannot use 'this' outside of an object scope. -//│ ║ l.432: (this).pc = 0 +//│ ║ l.430: (this).pc = 0 //│ ╙── ^^^^ // But this doesn't... @@ -443,10 +441,10 @@ set set this.pc = 0 //│ ╔══[PARSE ERROR] Unexpected static selector here -//│ ║ l.444: this.pc = 0 +//│ ║ l.442: this.pc = 0 //│ ╙── ^^^ //│ ╔══[ERROR] Expected a right-hand side for this assignment -//│ ║ l.444: this.pc = 0 +//│ ║ l.442: this.pc = 0 //│ ╙── ^^^^ // ——— ——— ——— @@ -458,7 +456,7 @@ pattern A(pattern B) = B fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╔══[COMPILATION ERROR] No definition found in scope for member 'y' -//│ ║ l.459: fun foo(x) = if x is @annotations.compile A(0) as y then y +//│ ║ l.457: fun foo(x) = if x is @annotations.compile A(0) as y then y //│ ╙── ^ // ——— ——— ——— diff --git a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls index 8c99b7c926..037ca2f59d 100644 --- a/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls +++ b/hkmc2/shared/src/test/mlscript/basics/BadDefs.mls @@ -15,7 +15,7 @@ x :sjs val ++ = 0 //│ JS (unsanitized): -//│ let $_$_; /** scoped **/ $_$_ = 0; +//│ let $_$_; $_$_ = 0; //│ ++ = 0 :sjs @@ -51,7 +51,7 @@ fun ++ z = 0 :sjs ++ //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = $_$_2(); tmp +//│ let tmp; tmp = $_$_2(); tmp //│ = 0 diff --git a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls index 879780f51f..ced354b8b2 100644 --- a/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls +++ b/hkmc2/shared/src/test/mlscript/basics/CompanionModules_Classes.mls @@ -95,7 +95,7 @@ set state += 1 module Foo with val res = Foo().foo //│ JS (unsanitized): -//│ let Foo1, tmp1; /** scoped **/ +//│ let Foo1, tmp1; //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { @@ -107,7 +107,7 @@ module Foo with //│ } //│ constructor() {} //│ static { -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ tmp2 = Foo1(); //│ this.res = tmp2.foo; //│ } diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index df691692ad..0a8b33ff10 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/Drop.mls b/hkmc2/shared/src/test/mlscript/basics/Drop.mls index 1c0092c0d9..cce678bd28 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Drop.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Drop.mls @@ -4,13 +4,13 @@ :sjs drop id(2 + 2) //│ JS (unsanitized): -//│ let tmp, tmp1; /** scoped **/ tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit +//│ let tmp, tmp1; tmp = 2 + 2; tmp1 = Predef.id(tmp); runtime.Unit :sjs drop { a: 0, b: 1 } //│ JS (unsanitized): -//│ let a, b, tmp2, tmp3; /** scoped **/ +//│ let a, b, tmp2, tmp3; //│ a = 0; //│ tmp2 = globalThis.Object.freeze({ //│ a: a @@ -22,7 +22,7 @@ drop { a: 0, b: 1 } :sjs let discard = drop _ //│ JS (unsanitized): -//│ let discard; /** scoped **/ +//│ let discard; //│ let discard1; //│ discard1 = function discard(_0) { return runtime.Unit }; //│ discard = discard1; diff --git a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls index 708cfa4897..d91b3fba16 100644 --- a/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls +++ b/hkmc2/shared/src/test/mlscript/basics/FunnyRecordKeys.mls @@ -6,7 +6,7 @@ { a: 1 } //│ JS (unsanitized): -//│ let a; /** scoped **/ a = 1; globalThis.Object.freeze({ a: a }) +//│ let a; a = 1; globalThis.Object.freeze({ a: a }) //│ = {a: 1} { "a": 1 } @@ -35,7 +35,7 @@ :sjs { (id(0)): 1 } //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) +//│ let tmp; tmp = Predef.id(0); globalThis.Object.freeze({ [tmp]: 1 }) //│ = {"0": 1} diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index b0027fd8b2..e9e16402c8 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,7 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let Baz1, tmp; /** scoped **/ +//│ let Baz1, tmp; //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls index dd9ce4d58f..26fb4b1450 100644 --- a/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls +++ b/hkmc2/shared/src/test/mlscript/basics/LazySpreads.mls @@ -17,11 +17,11 @@ fun buildPalindrome = case 0 then [] n then [n, ..buildPalindrome(n - 1), n] //│ JS (unsanitized): -//│ let buildPalindrome; /** scoped **/ +//│ let buildPalindrome; //│ buildPalindrome = function buildPalindrome() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let n, tmp3, tmp4, tmp5; /** scoped **/ +//│ let n, tmp3, tmp4, tmp5; //│ if (caseScrut === 0) { //│ return globalThis.Object.freeze([]) //│ } else { @@ -48,11 +48,11 @@ fun sum2 = case [] then 0 [x, ..xs, y] then x + y + sum2(xs) //│ JS (unsanitized): -//│ let sum2; /** scoped **/ +//│ let sum2; //│ sum2 = function sum2() { //│ let lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; /** scoped **/ +//│ let x, xs, y, lastElement0$3, middleElements3, element0$, tmp5, tmp6, tmp7; //│ if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length === 0) { //│ return 0 //│ } else if (runtime.Tuple.isArrayLike(caseScrut) && caseScrut.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls index 2299883a06..39b9941e38 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MemberProjections.mls @@ -46,7 +46,7 @@ M.Foo:: n(foo, 2) :sjs let m = M.Foo::m //│ JS (unsanitized): -//│ let m; /** scoped **/ let m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; +//│ let m; let m1; m1 = function m(self, ...args) { return self.m(...args) }; m = m1; //│ m = fun m m(foo) @@ -132,7 +132,7 @@ Foo::n(foo, 2) //│ ╙── add a space before ‹identifier› to make it an operator application. //│ ═══[ERROR] Expected a statically known class; found ‹error›. //│ JS (unsanitized): -//│ let lambda9; /** scoped **/ +//│ let lambda9; //│ lambda9 = (undefined, function (self, ...args) { //│ return runtime.safeCall(self.n(...args)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls index 4cffa34572..2c3bbe2920 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MiscArrayTests.mls @@ -54,7 +54,7 @@ xs \ //│ ║ l.52: map(x => x * 2) //│ ╙── ^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let lambda5, tmp4; /** scoped **/ +//│ let lambda5, tmp4; //│ lambda5 = (undefined, function (x) { return x * 2 }); //│ tmp4 = map(lambda5); //│ Predef.passTo(xs1, tmp4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls index b0d56d3949..c76804e25b 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamListClasses.mls @@ -77,10 +77,10 @@ Foo(1, 2) :sjs let f = Foo // should compile to `(x, y) => Foo(x, y)(use[Int])` //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ let f1; //│ f1 = function f(x, y) { -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ tmp4 = Foo7(x, y); //│ return runtime.safeCall(tmp4(instance$Ident$_Int$_)) //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls index 9c2202fc04..178203f9e4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultiParamLists.mls @@ -6,7 +6,7 @@ fun f(n1: Int): Int = n1 //│ JS (unsanitized): -//│ let f; /** scoped **/ f = function f(n1) { return n1 }; +//│ let f; f = function f(n1) { return n1 }; f(42) //│ JS (unsanitized): @@ -19,24 +19,23 @@ f(42) fun f(n1: Int)(n2: Int): Int = (10 * n1 + n2) //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f(n1) { return (n2) => { let tmp; /** scoped **/ tmp = 10 * n1; return tmp + n2 } }; +//│ let f1; f1 = function f(n1) { return (n2) => { let tmp; tmp = 10 * n1; return tmp + n2 } }; // TODO compile this to // this.f$(4, 2) f(4)(2) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = f1(4); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = f1(4); runtime.safeCall(tmp(2)) //│ = 42 fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ f2 = function f(n1) { //│ return (n2) => { //│ return (n3) => { -//│ let tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp1, tmp2, tmp3; //│ tmp1 = 10 * n1; //│ tmp2 = tmp1 + n2; //│ tmp3 = 10 * tmp2; @@ -47,20 +46,17 @@ fun f(n1: Int)(n2: Int)(n3: Int): Int = 10 * (10 * n1 + n2) + n3 f(4)(2)(0) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ -//│ tmp1 = f2(4); -//│ tmp2 = runtime.safeCall(tmp1(2)); -//│ runtime.safeCall(tmp2(0)) +//│ let tmp1, tmp2; tmp1 = f2(4); tmp2 = runtime.safeCall(tmp1(2)); runtime.safeCall(tmp2(0)) //│ = 420 fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) + n4 //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(n1) { //│ return (n2) => { //│ return (n3) => { //│ return (n4) => { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7; /** scoped **/ +//│ let tmp3, tmp4, tmp5, tmp6, tmp7; //│ tmp3 = 10 * n1; //│ tmp4 = tmp3 + n2; //│ tmp5 = 10 * tmp4; @@ -74,7 +70,7 @@ fun f(n1: Int)(n2: Int)(n3: Int)(n4: Int): Int = 10 * (10 * (10 * n1 + n2) + n3) f(3)(0)(3)(1) //│ JS (unsanitized): -//│ let tmp3, tmp4, tmp5; /** scoped **/ +//│ let tmp3, tmp4, tmp5; //│ tmp3 = f3(3); //│ tmp4 = runtime.safeCall(tmp3(0)); //│ tmp5 = runtime.safeCall(tmp4(3)); diff --git a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls index aa73bdb0d9..5025320ce4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MultilineExpressions.mls @@ -80,7 +80,7 @@ if 1 + 2 //│ kw = Keywrd of keyword 'then' //│ rhs = IntLit of 0 //│ JS (unsanitized): -//│ let scrut, tmp17; /** scoped **/ +//│ let scrut, tmp17; //│ tmp17 = 2 * 3; //│ scrut = 1 + tmp17; //│ if (scrut === true) { diff --git a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls index d95bc500e7..58a398ea87 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutArr.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutArr.mls @@ -4,7 +4,7 @@ :sjs let t = [0, 1, 2] //│ JS (unsanitized): -//│ let t; /** scoped **/ t = globalThis.Object.freeze([ 0, 1, 2 ]); +//│ let t; t = globalThis.Object.freeze([ 0, 1, 2 ]); //│ t = [0, 1, 2] :re @@ -21,7 +21,7 @@ t :sjs let t = mut [0, 1, 2] //│ JS (unsanitized): -//│ let t1; /** scoped **/ t1 = [ 0, 1, 2 ]; +//│ let t1; t1 = [ 0, 1, 2 ]; //│ t = [0, 1, 2] t.push(4) diff --git a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls index 7efae81a91..0c8a48f3a4 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutRcd.mls @@ -46,7 +46,7 @@ r.foo :sjs let r = {} //│ JS (unsanitized): -//│ let r3; /** scoped **/ r3 = runtime.Unit; +//│ let r3; r3 = runtime.Unit; //│ r = () :re diff --git a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls index 655c4d0a45..408d7db105 100644 --- a/hkmc2/shared/src/test/mlscript/basics/MutVal.mls +++ b/hkmc2/shared/src/test/mlscript/basics/MutVal.mls @@ -33,7 +33,7 @@ O.gy :sjs class Foo(x, val y, mut val z) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x, y, z) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls index fb241cc248..b24d4380ee 100644 --- a/hkmc2/shared/src/test/mlscript/basics/NewMut.mls +++ b/hkmc2/shared/src/test/mlscript/basics/NewMut.mls @@ -10,7 +10,7 @@ class Foo with :sjs let f = new Foo //│ JS (unsanitized): -//│ let f; /** scoped **/ f = globalThis.Object.freeze(new Foo1()); +//│ let f; f = globalThis.Object.freeze(new Foo1()); //│ f = Foo { x: 0 } set @@ -38,7 +38,7 @@ f.y // :elt let f = new mut Foo //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = new Foo1(); +//│ let f1; f1 = new Foo1(); //│ f = Foo { x: 0 } set diff --git a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls index 100957af54..72c154d117 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PartialApps.mls @@ -109,19 +109,16 @@ passTo(1, add(., 1) @ _ + _)(2) :sjs let f = add(_, 1) //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ let f1; -//│ f1 = function f(_0) { let tmp15; /** scoped **/ tmp15 = add(); return tmp15(_0, 1) }; -//│ f = f1; +//│ let f; let f1; f1 = function f(_0) { let tmp15; tmp15 = add(); return tmp15(_0, 1) }; f = f1; //│ f = fun f :fixme let f = add(., 1) //│ ╔══[PARSE ERROR] Expected an expression; found period instead -//│ ║ l.119: let f = add(., 1) +//│ ║ l.116: let f = add(., 1) //│ ╙── ^ //│ ╔══[PARSE ERROR] Unexpected period here -//│ ║ l.119: let f = add(., 1) +//│ ║ l.116: let f = add(., 1) //│ ╙── ^ //│ ═══[RUNTIME ERROR] Error: Function expected 2 arguments but got 0 //│ f = undefined diff --git a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls index c9d079ed1c..36ad46feac 100644 --- a/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls +++ b/hkmc2/shared/src/test/mlscript/basics/PureTermStatements.mls @@ -56,7 +56,7 @@ fun foo() = :sjs 1; id(2) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.id(2); (1 , tmp) +//│ let tmp; tmp = Predef.id(2); (1 , tmp) //│ = 2 diff --git a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls index 02207fb129..df1eb55c0e 100644 --- a/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls +++ b/hkmc2/shared/src/test/mlscript/basics/ShortcircuitingOps.mls @@ -4,9 +4,7 @@ :sjs 1 && 2 //│ JS (unsanitized): -//│ let lambda; /** scoped **/ -//│ lambda = (undefined, function () { return 2 }); -//│ runtime.short_and(1, lambda) +//│ let lambda; lambda = (undefined, function () { return 2 }); runtime.short_and(1, lambda) //│ = 2 1 || 2 @@ -20,9 +18,7 @@ fun test(x) = :sjs 123 || test(42) //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ -//│ lambda2 = (undefined, function () { return test(42) }); -//│ runtime.short_or(123, lambda2) +//│ let lambda2; lambda2 = (undefined, function () { return test(42) }); runtime.short_or(123, lambda2) //│ = 123 0 || test(42) @@ -36,9 +32,9 @@ fun test(x) = :sjs fold(||)(0, false, 42, 123) //│ JS (unsanitized): -//│ let lambda5, tmp; /** scoped **/ +//│ let lambda5, tmp; //│ lambda5 = (undefined, function (arg1, arg2) { -//│ let lambda6; /** scoped **/ +//│ let lambda6; //│ lambda6 = (undefined, function () { //│ return arg2 //│ }); diff --git a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls index b9d1163e58..666d7e6f4c 100644 --- a/hkmc2/shared/src/test/mlscript/basics/StrTest.mls +++ b/hkmc2/shared/src/test/mlscript/basics/StrTest.mls @@ -8,7 +8,7 @@ open StrOps :sjs "a" is Str //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = "a"; if (typeof scrut === 'string') { true } else { false } +//│ let scrut; scrut = "a"; if (typeof scrut === 'string') { true } else { false } //│ = true diff --git a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls index 157da75cea..198b5ecd14 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Underscores.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Underscores.mls @@ -61,7 +61,7 @@ print(_) :sjs let test = _.f(0, _, 2) //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ let test1; //│ test1 = function test(_0, _1) { return runtime.safeCall(_0.f(0, _1, 2)) }; //│ test = test1; diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls index 4091bd3623..b05e9f394d 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbCodeGen.mls @@ -31,7 +31,7 @@ :sjs let x = 1 in x + 1 //│ JS (unsanitized): -//│ let x; /** scoped **/ x = 1; x + 1 +//│ let x; x = 1; x + 1 //│ = 2 //│ Type: Int @@ -61,7 +61,7 @@ false :sjs data class Foo(x: Int) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x1) { //│ return globalThis.Object.freeze(new Foo.class(x1)); //│ }; @@ -89,7 +89,7 @@ new Foo(42) :sjs let foo = new Foo(42) in foo.Foo#x //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = globalThis.Object.freeze(new Foo1.class(42)); foo.x +//│ let foo; foo = globalThis.Object.freeze(new Foo1.class(42)); foo.x //│ = 42 //│ Type: Int @@ -97,7 +97,7 @@ let foo = new Foo(42) in foo.Foo#x :sjs fun inc(x) = x + 1 //│ JS (unsanitized): -//│ let inc; /** scoped **/ inc = function inc(x1) { return x1 + 1 }; +//│ let inc; inc = function inc(x1) { return x1 + 1 }; //│ Type: ⊤ @@ -112,7 +112,7 @@ inc(41) :sjs if 1 == 2 then 0 else 42 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } +//│ let scrut; scrut = 1 == 2; if (scrut === true) { 0 } else { 42 } //│ = 42 //│ Type: Int @@ -120,7 +120,7 @@ if 1 == 2 then 0 else 42 :sjs if 1 is Int then 1 else 0 //│ JS (unsanitized): -//│ let scrut1; /** scoped **/ scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } +//│ let scrut1; scrut1 = 1; if (globalThis.Number.isInteger(scrut1)) { 1 } else { 0 } //│ = 1 //│ Type: Int @@ -134,7 +134,7 @@ data class Foo() let foo = new Foo() if foo is Foo then 1 else 0 //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ foo1 = globalThis.Object.freeze(new Foo3.class()); //│ if (foo1 instanceof Foo3.class) { 1 } else { 0 } //│ = 1 @@ -147,11 +147,11 @@ fun pow(x) = case 0 then 1 n then x * pow(x)(n-1) //│ JS (unsanitized): -//│ let pow; /** scoped **/ +//│ let pow; //│ pow = function pow(x1) { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp, tmp1, tmp2; /** scoped **/ +//│ let n, tmp, tmp1, tmp2; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -172,7 +172,7 @@ fun nott = case true then false false then true //│ JS (unsanitized): -//│ let nott; /** scoped **/ +//│ let nott; //│ nott = function nott() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { @@ -193,7 +193,7 @@ fun nott = case :sjs nott of false //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = nott(); tmp(false) +//│ let tmp; tmp = nott(); tmp(false) //│ = true //│ Type: Bool @@ -203,11 +203,11 @@ fun fact = case 0 then 1 n then n * fact(n - 1) //│ JS (unsanitized): -//│ let fact; /** scoped **/ +//│ let fact; //│ fact = function fact() { //│ let lambda1; //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp1, tmp2, tmp3; /** scoped **/ +//│ let n, tmp1, tmp2, tmp3; //│ if (caseScrut === 0) { //│ return 1 //│ } else { @@ -258,7 +258,7 @@ region x in x.ref 42 :sjs region x in let y = x.ref 42 in !(y) //│ JS (unsanitized): -//│ let y; /** scoped **/ +//│ let y; //│ let x4; //│ x4 = globalThis.Object.freeze(new globalThis.Region()); //│ y = globalThis.Object.freeze(new globalThis.Ref(x4, 42)); @@ -270,7 +270,7 @@ region x in let y = x.ref 42 in !(y) :sjs region x in let y = x.ref 42 in y := 0 //│ JS (unsanitized): -//│ let y1; /** scoped **/ +//│ let y1; //│ let x5; //│ x5 = globalThis.Object.freeze(new globalThis.Region()); //│ y1 = globalThis.Object.freeze(new globalThis.Ref(x5, 42)); diff --git a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls index 50986bddaa..48f541fb92 100644 --- a/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls +++ b/hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls @@ -87,11 +87,11 @@ fun test2() = //│ ║ l.83: case 0 then 0 //│ ╙── ^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let test22; /** scoped **/ +//│ let test22; //│ test22 = function test2() { -//│ let funny, tmp1; /** scoped **/ +//│ let funny, tmp1; //│ funny = function funny() { -//│ let lambda; /** scoped **/ +//│ let lambda; //│ let lambda1; //│ lambda = (undefined, function (caseScrut) { //│ if (caseScrut === 0) { @@ -101,7 +101,7 @@ fun test2() = //│ } //│ }); //│ lambda1 = (undefined, function (caseScrut) { -//│ let n, tmp2, tmp3, tmp4; /** scoped **/ +//│ let n, tmp2, tmp3, tmp4; //│ n = caseScrut; //│ tmp2 = funny(); //│ tmp3 = n - 1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls index d7ff1e8b8d..9cb5c5210c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Arrays.mls @@ -11,7 +11,7 @@ empty.0 :sjs let single = [1] //│ JS (unsanitized): -//│ let single; /** scoped **/ single = globalThis.Object.freeze([ 1 ]); +//│ let single; single = globalThis.Object.freeze([ 1 ]); //│ single = [1] single.0 @@ -21,7 +21,7 @@ single.0 :sjs val single = [1] //│ JS (unsanitized): -//│ let single1, tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; +//│ let single1, tmp; tmp = globalThis.Object.freeze([ 1 ]); single1 = tmp; //│ single = [1] single.0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls index 0d443fdd05..ce774c920f 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadInit.mls @@ -9,7 +9,7 @@ object Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar1; /** scoped **/ +//│ let Bar1; //│ globalThis.Object.freeze(class Bar { //│ static { //│ Bar1 = globalThis.Object.freeze(new this) @@ -49,7 +49,7 @@ module Bar with module Baz with val a = Bar.x //│ JS (unsanitized): -//│ let Bar3; /** scoped **/ +//│ let Bar3; //│ globalThis.Object.freeze(class Bar2 { //│ static { //│ Bar3 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls index 454e29826f..ed25930a28 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BadNew.mls @@ -29,7 +29,7 @@ new 2 + 2 :re new! 2 + 2 //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 +//│ let tmp1; tmp1 = globalThis.Object.freeze(new 2()); tmp1 + 2 //│ ═══[RUNTIME ERROR] TypeError: 2 is not a constructor :e diff --git a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls index db1624542a..7bf650836b 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BasicTerms.mls @@ -39,7 +39,7 @@ print("Hi") print("Hi") 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Predef.print("Hi"); 2 +//│ let tmp; tmp = Predef.print("Hi"); 2 //│ Lowered: //│ Program: //│ imports = Nil diff --git a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls index 70dc34b630..51390245ed 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/BuiltinOps.mls @@ -74,7 +74,7 @@ id(+)(1, 2) :re id(+)(1) //│ JS (unsanitized): -//│ let lambda4, tmp1; /** scoped **/ +//│ let lambda4, tmp1; //│ lambda4 = (undefined, function (arg1, arg2) { //│ return arg1 + arg2 //│ }); @@ -95,7 +95,7 @@ fun (+) lol(a, b) = [a, b] :sjs id(~)(2) //│ JS (unsanitized): -//│ let lambda5, tmp2; /** scoped **/ +//│ let lambda5, tmp2; //│ lambda5 = (undefined, function (arg) { //│ return ~ arg //│ }); diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls index 040882fb73..22009ea6bb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseOfCase.mls @@ -15,9 +15,9 @@ fun test(x) = Some(v) then print(v) None then print("none") //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test(x) { -//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; /** scoped **/ +//│ let v, scrut, v1, argument0$, argument0$1, tmp, tmp1; //│ if (x instanceof Some1.class) { //│ argument0$1 = x.value; //│ v = argument0$1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls index 6e09f1ea71..510dbe3eeb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/CaseShorthand.mls @@ -7,13 +7,7 @@ case x then x :sjs case { x then x } //│ JS (unsanitized): -//│ let lambda1; -//│ lambda1 = (undefined, function (caseScrut) { -//│ let x; /** scoped **/ -//│ x = caseScrut; -//│ return x -//│ }); -//│ lambda1 +//│ let lambda1; lambda1 = (undefined, function (caseScrut) { let x; x = caseScrut; return x }); lambda1 //│ = fun :sjs @@ -59,10 +53,10 @@ case 0 then true :todo // TODO: support this braceless syntax? case [x] then x, [] then 0 //│ ╔══[ERROR] Unexpected infix use of keyword 'then' here -//│ ║ l.60: case [x] then x, [] then 0 +//│ ║ l.54: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^ //│ ╔══[WARNING] Pure expression in statement position -//│ ║ l.60: case [x] then x, [] then 0 +//│ ║ l.54: case [x] then x, [] then 0 //│ ╙── ^^^^^^^^^^^^^^^ :sjs @@ -85,7 +79,7 @@ val isDefined = case Some then true None then false //│ JS (unsanitized): -//│ let isDefined, lambda12; /** scoped **/ +//│ let isDefined, lambda12; //│ lambda12 = (undefined, function (caseScrut) { //│ if (caseScrut instanceof Some1.class) { //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls index f99ade0950..195b55a49a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInClass.mls @@ -14,7 +14,7 @@ data class Outer(a, b) with print(i.c) print(i.i1(1)) //│ JS (unsanitized): -//│ let Outer1; /** scoped **/ +//│ let Outer1; //│ Outer1 = function Outer(a, b) { //│ return globalThis.Object.freeze(new Outer.class(a, b)); //│ }; @@ -23,7 +23,7 @@ data class Outer(a, b) with //│ Outer1.class = this //│ } //│ constructor(a, b) { -//│ let tmp, tmp1, tmp2; /** scoped **/ +//│ let tmp, tmp1, tmp2; //│ this.a = a; //│ this.b = b; //│ const this$Outer = this; @@ -35,7 +35,7 @@ data class Outer(a, b) with //│ this$Outer.Inner.class = this //│ } //│ constructor(c) { -//│ let tmp3, tmp4, tmp5; /** scoped **/ +//│ let tmp3, tmp4, tmp5; //│ this.c = c; //│ tmp3 = Predef.print(this$Outer.a); //│ tmp4 = Predef.print(this.c); @@ -62,7 +62,7 @@ data class Outer(a, b) with //│ return this.Inner(c) //│ } //│ o2(c, d) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = this.Inner(c); //│ return tmp.i1(d) //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls index 696a1e22bd..b5c1b26b34 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassInFun.mls @@ -6,9 +6,9 @@ fun test(a) = class C with { val x = a } new C //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test(a) { -//│ let C1; /** scoped **/ +//│ let C1; //│ globalThis.Object.freeze(class C { //│ static { //│ C1 = this @@ -37,9 +37,9 @@ fun test(x) = data class Foo(a, b) Foo(x, x + 1) //│ JS (unsanitized): -//│ let test2; /** scoped **/ +//│ let test2; //│ test2 = function test(x) { -//│ let Foo2, tmp; /** scoped **/ +//│ let Foo2, tmp; //│ Foo2 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls index 6c679f4917..55c80c83c3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ClassMatching.mls @@ -9,7 +9,7 @@ object None :sjs if Some(0) is Some(x) then x //│ JS (unsanitized): -//│ let scrut, x, argument0$; /** scoped **/ +//│ let scrut, x, argument0$; //│ scrut = Some1(0); //│ if (scrut instanceof Some1.class) { //│ argument0$ = scrut.value; @@ -26,7 +26,7 @@ let s = Some(0) if s is Some(x) then x //│ JS (unsanitized): -//│ let x1, argument0$1; /** scoped **/ +//│ let x1, argument0$1; //│ if (s instanceof Some1.class) { //│ argument0$1 = s.value; //│ x1 = argument0$1; @@ -55,7 +55,7 @@ x => if x is Some(x) then x //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x3) { -//│ let x4, argument0$4; /** scoped **/ +//│ let x4, argument0$4; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ x4 = argument0$4; @@ -118,9 +118,9 @@ fun f(x) = if x is None then "ok" else print("oops") //│ JS (unsanitized): -//│ let f4; /** scoped **/ +//│ let f4; //│ f4 = function f(x3) { -//│ let x4, scrut1, argument0$4, tmp5; /** scoped **/ +//│ let x4, scrut1, argument0$4, tmp5; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some1.class) { @@ -164,9 +164,9 @@ fun f(x) = if x is Some(u) then u Pair(a, b) then a + b //│ JS (unsanitized): -//│ let f5; /** scoped **/ +//│ let f5; //│ f5 = function f(x3) { -//│ let u, a, b, argument0$4, argument1$; /** scoped **/ +//│ let u, a, b, argument0$4, argument1$; //│ if (x3 instanceof Some1.class) { //│ argument0$4 = x3.value; //│ u = argument0$4; @@ -194,9 +194,9 @@ fun f(x) = print of if x is None then "ok" else "oops" //│ JS (unsanitized): -//│ let f6; /** scoped **/ +//│ let f6; //│ f6 = function f(x3) { -//│ let argument0$4, tmp9; /** scoped **/ +//│ let argument0$4, tmp9; //│ split_root$1: { //│ split_1$: { //│ if (x3 instanceof Some1.class) { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls index fc79ff4035..1c596f3fc1 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Classes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Classes.mls @@ -6,7 +6,7 @@ data class Foo(arguments) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(arguments1) { //│ return globalThis.Object.freeze(new Foo.class(arguments1)); //│ }; @@ -24,7 +24,7 @@ data class Foo(arguments) data class Foo(eval) //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ Foo3 = function Foo(eval1) { //│ return globalThis.Object.freeze(new Foo.class(eval1)); //│ }; @@ -42,7 +42,7 @@ data class Foo(eval) data class Foo(static) //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ Foo5 = function Foo(static1) { //│ return globalThis.Object.freeze(new Foo.class(static1)); //│ }; @@ -60,7 +60,7 @@ data class Foo(static) data class Foo(v') //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ Foo7 = function Foo(v$_) { //│ return globalThis.Object.freeze(new Foo.class(v$_)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls index af16010767..7ba95074d3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Comma.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Comma.mls @@ -7,27 +7,17 @@ fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f; /** scoped **/ -//│ f = function f() { -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 -//│ }; +//│ let f; +//│ f = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; fun f() = { console.log("ok"), 42 } //│ JS (unsanitized): -//│ let f1; /** scoped **/ -//│ f1 = function f() { -//│ let tmp; /** scoped **/ -//│ tmp = runtime.safeCall(globalThis.console.log("ok")); -//│ return 42 -//│ }; +//│ let f1; +//│ f1 = function f() { let tmp; tmp = runtime.safeCall(globalThis.console.log("ok")); return 42 }; fun f() = console.log("ok"), 42 //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; -//│ 42 +//│ let f2; f2 = function f() { return runtime.safeCall(globalThis.console.log("ok")) }; 42 //│ = 42 diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index 1770b21e64..4aa0be8931 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,14 +7,14 @@ :silent declare val console //│ JS (unsanitized): -//│ let console; /** scoped **/ +//│ let console; console.log("a") console.log("b") 123 //│ JS (unsanitized): -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ tmp = runtime.safeCall(globalThis.console.log("a")); //│ tmp1 = runtime.safeCall(globalThis.console.log("b")); //│ 123 @@ -25,7 +25,7 @@ console.log("b") let l = console.log l(123) //│ JS (unsanitized): -//│ let l; /** scoped **/ l = globalThis.console.log; runtime.safeCall(l(123)) +//│ let l; l = globalThis.console.log; runtime.safeCall(l(123)) //│ > 123 //│ l = fun log @@ -49,7 +49,7 @@ let y = x + 1 console.log("c") y * 2 //│ JS (unsanitized): -//│ let x, y, tmp2, tmp3, tmp4; /** scoped **/ +//│ let x, y, tmp2, tmp3, tmp4; //│ tmp2 = runtime.safeCall(globalThis.console.log("a")); //│ x = 123; //│ tmp3 = runtime.safeCall(globalThis.console.log("b")); diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index c2ef84645e..9d733318be 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; /** scoped **/ x = undefined; +//│ let x; x = undefined; //│ x = undefined x = 1 @@ -31,7 +31,7 @@ x let y = 1 //│ JS (unsanitized): -//│ let y; /** scoped **/ y = 1; +//│ let y; y = 1; //│ y = 1 :e @@ -45,7 +45,7 @@ z = 1 fun f() = 1 //│ JS (unsanitized): -//│ let f; /** scoped **/ f = function f() { return 1 }; +//│ let f; f = function f() { return 1 }; f //│ JS (unsanitized): @@ -56,7 +56,7 @@ f let f f(x) = x + 1 //│ JS (unsanitized): -//│ let f1; /** scoped **/ let f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; +//│ let f1; let f2; f2 = function f(x1) { return x1 + 1 }; f1 = f2; //│ f = fun f f(1) @@ -68,7 +68,7 @@ f(1) let foo foo = 0 //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = 0; +//│ let foo; foo = 0; //│ foo = 0 :fixme @@ -80,7 +80,7 @@ else foo = 1 //│ ║ l.77: then foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let foo1, scrut; /** scoped **/ +//│ let foo1, scrut; //│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { @@ -95,7 +95,7 @@ then else foo = 1 //│ JS (unsanitized): -//│ let foo2, scrut1; /** scoped **/ +//│ let foo2, scrut1; //│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } @@ -105,7 +105,7 @@ else fun f() = foo = 42 //│ JS (unsanitized): -//│ let f3; /** scoped **/ f3 = function f() { foo2 = 42; return runtime.Unit }; +//│ let f3; f3 = function f() { foo2 = 42; return runtime.Unit }; f() //│ JS (unsanitized): @@ -123,6 +123,6 @@ fun f() = foo = 0 //│ ║ l.121: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): -//│ let f4; /** scoped **/ f4 = function f() { return foo2 }; +//│ let f4; f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls index 1c439570a2..797b5c0c47 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/EarlyReturn.mls @@ -18,9 +18,9 @@ fun f(x) = return 0 x + 1 //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ f1 = function f(x) { -//│ let scrut, tmp, tmp1; /** scoped **/ +//│ let scrut, tmp, tmp1; //│ scrut = x < 0; //│ if (scrut === true) { //│ tmp = Predef.print("whoops"); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls index 0f1fbe2161..c23fc6c9b4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Formatting.mls @@ -7,7 +7,7 @@ let discard = drop _ //│ JS (unsanitized): -//│ let discard; /** scoped **/ +//│ let discard; //│ let discard1; //│ discard1 = function discard(_0) { return runtime.Unit }; //│ discard = discard1; @@ -18,7 +18,7 @@ let discard = drop _ discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a, b, tmp; /** scoped **/ +//│ let a, b, tmp; //│ a = 0; //│ b = 11111; //│ tmp = globalThis.Object.freeze({ a: a, b: b }); @@ -26,7 +26,7 @@ discard of { a: 0, b: 11111 } discard of { a: 0, b: 11111 } //│ JS (unsanitized): -//│ let a1, b1, tmp1; /** scoped **/ +//│ let a1, b1, tmp1; //│ a1 = 0; //│ b1 = 11111; //│ tmp1 = globalThis.Object.freeze({ a: a1, b: b1 }); @@ -35,14 +35,14 @@ discard of { a: 0, b: 11111 } discard of { aaaa: 1 } //│ JS (unsanitized): -//│ let aaaa, tmp2; /** scoped **/ +//│ let aaaa, tmp2; //│ aaaa = 1; //│ tmp2 = globalThis.Object.freeze({ aaaa: aaaa }); //│ runtime.safeCall(discard(tmp2)) discard of { aaaaaaaaa: 1, bbbb: 2 } //│ JS (unsanitized): -//│ let aaaaaaaaa, bbbb, tmp3; /** scoped **/ +//│ let aaaaaaaaa, bbbb, tmp3; //│ aaaaaaaaa = 1; //│ bbbb = 2; //│ tmp3 = globalThis.Object.freeze({ @@ -53,7 +53,7 @@ discard of { aaaaaaaaa: 1, bbbb: 2 } discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } //│ JS (unsanitized): -//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; /** scoped **/ +//│ let aaaaaaaaa1, bbbbbbbb, ccccccccc, dddddddddd, tmp4; //│ aaaaaaaaa1 = 1; //│ bbbbbbbb = 2; //│ ccccccccc = 3; @@ -69,7 +69,7 @@ discard of { aaaaaaaaa: 1, bbbbbbbb: 2, ccccccccc: 3, dddddddddd: 4 } discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ tmp5 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb" @@ -78,7 +78,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb" ] discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ] //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ tmp6 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", @@ -89,7 +89,7 @@ discard of [ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddd discard of [[ "aaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "ccccccccccccccccc", "dddddddddddddddddd" ]] //│ JS (unsanitized): -//│ let tmp7, tmp8; /** scoped **/ +//│ let tmp7, tmp8; //│ tmp7 = globalThis.Object.freeze([ //│ "aaaaaaaaaaaaaaa", //│ "bbbbbbbbbbbbbbbb", diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls index 09b2e2a3c5..6fef9160a3 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunInClass.mls @@ -19,9 +19,9 @@ fun test(a) = h(d) Inner(42) //│ JS (unsanitized): -//│ let test1; /** scoped **/ +//│ let test1; //│ test1 = function test(a) { -//│ let Inner1; /** scoped **/ +//│ let Inner1; //│ Inner1 = function Inner(b) { //│ return globalThis.Object.freeze(new Inner.class(b)); //│ }; @@ -30,7 +30,7 @@ fun test(a) = //│ Inner1.class = this //│ } //│ constructor(b) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ this.b = b; //│ tmp = Predef.print(a); //│ } @@ -42,7 +42,7 @@ fun test(a) = //│ ]) //│ } //│ g(d) { -//│ let h; /** scoped **/ +//│ let h; //│ const this$Inner = this; //│ h = function h(e) { //│ return globalThis.Object.freeze([ @@ -79,9 +79,9 @@ fun test(a) = print of [a, b] [C1(1), C2(2)] //│ JS (unsanitized): -//│ let test2; /** scoped **/ +//│ let test2; //│ test2 = function test(a) { -//│ let C11, C21, tmp, tmp1; /** scoped **/ +//│ let C11, C21, tmp, tmp1; //│ C11 = function C1(b) { //│ return globalThis.Object.freeze(new C1.class(b)); //│ }; @@ -90,7 +90,7 @@ fun test(a) = //│ C11.class = this //│ } //│ constructor(b) { -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ this.b = b; //│ tmp2 = globalThis.Object.freeze([ //│ a, @@ -109,7 +109,7 @@ fun test(a) = //│ C21.class = this //│ } //│ constructor(b) { -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ this.b = b; //│ tmp2 = globalThis.Object.freeze([ //│ a, @@ -141,7 +141,7 @@ data class Foo(a) with foo() Foo(123) //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -154,7 +154,7 @@ Foo(123) //│ this.foo(); //│ } //│ foo() { -//│ let bar, baz, tmp; /** scoped **/ +//│ let bar, baz, tmp; //│ const this$Foo = this; //│ bar = function bar() { //│ return this$Foo.a @@ -180,7 +180,7 @@ data class Bar(x) with foo()() Bar(1) //│ JS (unsanitized): -//│ let Bar1; /** scoped **/ +//│ let Bar1; //│ Bar1 = function Bar(x) { //│ return globalThis.Object.freeze(new Bar.class(x)); //│ }; @@ -189,14 +189,14 @@ Bar(1) //│ Bar1.class = this //│ } //│ constructor(x) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ this.x = x; //│ tmp = this.foo(); //│ runtime.safeCall(tmp()); //│ } //│ foo() { //│ return () => { -//│ let bar; /** scoped **/ +//│ let bar; //│ const this$Bar = this; //│ bar = function bar() { //│ return this$Bar.x diff --git a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls index 39096f59fa..540121fc08 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/FunctionsThis.mls @@ -6,20 +6,20 @@ val x = 2 fun foo() = x + 1 //│ JS (unsanitized): -//│ let foo, x; /** scoped **/ foo = function foo() { return x + 1 }; x = 2; +//│ let foo, x; foo = function foo() { return x + 1 }; x = 2; //│ x = 2 :sjs class Test with print(foo()) //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this //│ } //│ constructor() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = foo(); //│ Predef.print(tmp); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls index 48913d17dc..fa474f9d76 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Getters.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Getters.mls @@ -4,14 +4,14 @@ :sjs fun t = 42 //│ JS (unsanitized): -//│ let t; /** scoped **/ t = function t() { return 42 }; +//│ let t; t = function t() { return 42 }; :expect 42 :sjs t //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = t(); tmp +//│ let tmp; tmp = t(); tmp //│ = 42 @@ -37,11 +37,11 @@ fun test() = 42 whoops + whoops //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test() { -//│ let whoops, tmp1, tmp2; /** scoped **/ +//│ let whoops, tmp1, tmp2; //│ whoops = function whoops() { -//│ let tmp3; /** scoped **/ +//│ let tmp3; //│ tmp3 = Predef.print("ok"); //│ return 42 //│ }; @@ -65,7 +65,7 @@ module T with val c = p val d = this.p //│ JS (unsanitized): -//│ let T1; /** scoped **/ +//│ let T1; //│ globalThis.Object.freeze(class T { //│ static { //│ T1 = this @@ -107,7 +107,7 @@ T.d module M with fun t = 0 //│ JS (unsanitized): -//│ let M1; /** scoped **/ +//│ let M1; //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -135,9 +135,9 @@ fun test() = fun whoops = 42 whoops //│ JS (unsanitized): -//│ let test1; /** scoped **/ +//│ let test1; //│ test1 = function test() { -//│ let whoops, tmp1; /** scoped **/ +//│ let whoops, tmp1; //│ whoops = function whoops() { return 42 }; //│ tmp1 = whoops(); //│ return tmp1 @@ -163,8 +163,7 @@ fun bar() = fun baz() = 42 baz //│ JS (unsanitized): -//│ let bar; /** scoped **/ -//│ bar = function bar() { let baz; /** scoped **/ baz = function baz() { return 42 }; return baz }; +//│ let bar; bar = function bar() { let baz; baz = function baz() { return 42 }; return baz }; :sjs @@ -173,9 +172,9 @@ fun baz() = fun z = 2 (x, y) => x + y + w + z //│ JS (unsanitized): -//│ let baz; /** scoped **/ +//│ let baz; //│ baz = function baz() { -//│ let w, z; /** scoped **/ +//│ let w, z; //│ let lambda; //│ w = function w() { //│ return 1 @@ -184,7 +183,7 @@ fun baz() = //│ return 2 //│ }; //│ lambda = (undefined, function (x, y) { -//│ let tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ let tmp1, tmp2, tmp3, tmp4; //│ tmp1 = x + y; //│ tmp2 = w(); //│ tmp3 = tmp1 + tmp2; @@ -207,14 +206,14 @@ fun a() = b + d c //│ JS (unsanitized): -//│ let a; /** scoped **/ +//│ let a; //│ a = function a() { -//│ let b, c; /** scoped **/ +//│ let b, c; //│ b = function b() { //│ return 1 //│ }; //│ c = function c() { -//│ let d, tmp2, tmp3; /** scoped **/ +//│ let d, tmp2, tmp3; //│ d = function d() { return 2 }; //│ tmp2 = b(); //│ tmp3 = d(); @@ -236,14 +235,14 @@ fun b() = c d //│ JS (unsanitized): -//│ let b; /** scoped **/ +//│ let b; //│ b = function b() { -//│ let c, d; /** scoped **/ +//│ let c, d; //│ c = function c() { //│ return 1 //│ }; //│ d = function d() { -//│ let c1, tmp3; /** scoped **/ +//│ let c1, tmp3; //│ c1 = function c() { return 2 }; //│ tmp3 = c1(); //│ return tmp3 @@ -264,14 +263,14 @@ fun c() = e + f d //│ JS (unsanitized): -//│ let c; /** scoped **/ +//│ let c; //│ c = function c() { -//│ let d, f, tmp4; /** scoped **/ +//│ let d, f, tmp4; //│ f = function f() { //│ return 1 //│ }; //│ d = function d() { -//│ let e, tmp5, tmp6; /** scoped **/ +//│ let e, tmp5, tmp6; //│ e = function e() { //│ return 1 //│ }; @@ -292,7 +291,7 @@ c() data class Foo(x) with fun oops = x //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls index 7c40714263..883fdd4d3e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/GlobalThis.mls @@ -21,7 +21,7 @@ globalThis :re if false then 0 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = false; //│ if (scrut === true) { //│ 0 @@ -37,9 +37,9 @@ fun foo() = if false then 0 foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo() { -//│ let scrut1; /** scoped **/ +//│ let scrut1; //│ scrut1 = false; //│ if (scrut1 === true) { //│ return 0 diff --git a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls index ffe988ac02..5c656ea439 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Hygiene.mls @@ -10,7 +10,7 @@ object Test with print(Test) Test.x //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = globalThis.Object.freeze(new this) @@ -22,7 +22,7 @@ object Test with //│ }) //│ } //│ foo() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = Predef.print(Test1); //│ return Test1.x //│ } @@ -46,7 +46,7 @@ Test.foo() :sjs val Test = "oops" //│ JS (unsanitized): -//│ let Test2; /** scoped **/ Test2 = "oops"; +//│ let Test2; Test2 = "oops"; //│ Test = "oops" :re @@ -60,13 +60,7 @@ let f = () => x let x = 2 f() //│ JS (unsanitized): -//│ let x, f, x1; /** scoped **/ -//│ let f1; -//│ x = 1; -//│ f1 = function f() { return x }; -//│ f = f1; -//│ x1 = 2; -//│ runtime.safeCall(f()) +//│ let x, f, x1; let f1; x = 1; f1 = function f() { return x }; f = f1; x1 = 2; runtime.safeCall(f()) //│ = 1 //│ f = fun f //│ x = 2 @@ -78,10 +72,10 @@ module Test with val x = 1 let x = 2 //│ ╔══[ERROR] Name 'x' is already used -//│ ║ l.79: let x = 2 +//│ ║ l.73: let x = 2 //│ ║ ^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.78: val x = 1 +//│ ║ l.72: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS: //│ globalThis.Object.freeze(class Test3 { @@ -121,7 +115,7 @@ data class Cls(x) with fun bar = x print(this.x, x) //│ JS (unsanitized): -//│ let Cls1; /** scoped **/ +//│ let Cls1; //│ Cls1 = function Cls(x2) { //│ return globalThis.Object.freeze(new Cls.class(x2)); //│ }; @@ -185,7 +179,7 @@ module Whoops with val w: module Whoops = this fun g() = f() //│ JS (unsanitized): -//│ let Whoops2; /** scoped **/ +//│ let Whoops2; //│ globalThis.Object.freeze(class Whoops { //│ static { //│ Whoops2 = this @@ -232,7 +226,7 @@ Whoops.Whoops.g() :e Runtime //│ ╔══[ERROR] Name not found: Runtime -//│ ║ l.233: Runtime +//│ ║ l.227: Runtime //│ ╙── ^^^^^^^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls index e09edf1374..a440378432 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/IfThenElse.mls @@ -9,7 +9,7 @@ if true then 1 else 0 :sjs let f = x => if x then print("ok") else print("ko") //│ JS (unsanitized): -//│ let f, lambda; /** scoped **/ +//│ let f, lambda; //│ lambda = (undefined, function (x) { //│ if (x === true) { return Predef.print("ok") } else { return Predef.print("ko") } //│ }); @@ -26,9 +26,9 @@ f(false) :sjs let f = x => print((if x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f1, lambda1; /** scoped **/ +//│ let f1, lambda1; //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } @@ -41,9 +41,9 @@ let f = x => print((if x then "ok" else "ko") + "!") :sjs let f = x => print((if x and x then "ok" else "ko") + "!") //│ JS (unsanitized): -//│ let f2, lambda2; /** scoped **/ +//│ let f2, lambda2; //│ lambda2 = (undefined, function (x) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ if (x === true) { //│ tmp = "ok"; //│ } else { tmp = "ko"; } diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls index e2425e33b6..07b13f5dfb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportMLs.mls @@ -39,9 +39,7 @@ Some(1) :sjs (new Some(1)) isDefined() //│ JS (unsanitized): -//│ let tmp3; /** scoped **/ -//│ tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); -//│ Option.isDefined(tmp3) +//│ let tmp3; tmp3 = globalThis.Object.freeze(new Option.Some.class(1)); Option.isDefined(tmp3) //│ = true new Some(1) isDefined() @@ -68,14 +66,14 @@ None == Option.None :re Option.oops //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.69: Option.oops +//│ ║ l.67: Option.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' :e open Option { oops } //│ ╔══[ERROR] Module 'Option' does not contain member 'oops' -//│ ║ l.76: open Option { oops } +//│ ║ l.74: open Option { oops } //│ ╙── ^^^^ oops @@ -84,7 +82,7 @@ oops :re Option.None.oops //│ ╔══[ERROR] Object 'None' does not contain member 'oops' -//│ ║ l.85: Option.None.oops +//│ ║ l.83: Option.None.oops //│ ╙── ^^^^^ //│ ═══[RUNTIME ERROR] Error: Access to required field 'oops' yielded 'undefined' diff --git a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls index ca8e1d58ae..f328a019d5 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ImportedOps.mls @@ -11,12 +11,8 @@ fun foo() = "a" ~ "b" ~ "c" foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ -//│ foo = function foo() { -//│ let tmp; /** scoped **/ -//│ tmp = M1.concat("a", "b"); -//│ return M1.concat(tmp, "c") -//│ }; +//│ let foo; +//│ foo = function foo() { let tmp; tmp = M1.concat("a", "b"); return M1.concat(tmp, "c") }; //│ foo() //│ = "abc" diff --git a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls index eb040590d5..37ffed6216 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/InlineLambdas.mls @@ -3,9 +3,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda; /** scoped **/ +//│ let lambda; //│ lambda = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3; //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -18,9 +18,9 @@ :sjs (x => x + 1 + 1 + 1 + 1 + 1 + 1)(1) //│ JS (unsanitized): -//│ let lambda1; /** scoped **/ +//│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let tmp, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3, tmp4; //│ tmp = x + 1; //│ tmp1 = tmp + 1; //│ tmp2 = tmp1 + 1; @@ -34,13 +34,13 @@ :sjs (x => x) + 1 //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ lambda2 = (undefined, function (x) { return x }); lambda2 + 1 +//│ let lambda2; lambda2 = (undefined, function (x) { return x }); lambda2 + 1 //│ = "function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }1" :sjs 1 + (x => x) //│ JS (unsanitized): -//│ let lambda3; /** scoped **/ lambda3 = (undefined, function (x) { return x }); 1 + lambda3 +//│ let lambda3; lambda3 = (undefined, function (x) { return x }); 1 + lambda3 //│ = "1function (...args) { runtime.checkArgs(\"\", 1, true, args.length); let x = args[0]; return x }" diff --git a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls index 86a50c8394..cf8b2bf680 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Lambdas.mls @@ -8,7 +8,7 @@ x => let y = x y //│ JS (unsanitized): -//│ let lambda; lambda = (undefined, function (x) { let y; /** scoped **/ y = x; return y }); lambda +//│ let lambda; lambda = (undefined, function (x) { let y; y = x; return y }); lambda //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls index e157ace779..bc312d91f9 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/MergeMatchArms.mls @@ -58,7 +58,7 @@ if a is B then 2 C then 3 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ +//│ let tmp; //│ split_root$: { //│ split_default$: { //│ split_1$: { @@ -107,7 +107,7 @@ let x = if a is print("done") print(x) //│ JS (unsanitized): -//│ let x, tmp1, tmp2, tmp3, tmp4; /** scoped **/ +//│ let x, tmp1, tmp2, tmp3, tmp4; //│ tmp1 = 3; //│ if (a instanceof A1.class) { //│ tmp2 = 1; @@ -145,7 +145,7 @@ if a is let tmp = 2 B then 2 + tmp //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ tmp5 = 2; //│ if (a instanceof A1.class) { //│ 1 @@ -167,7 +167,7 @@ if a is let tmp = printAndId(3) B then 2 + tmp //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ if (a instanceof A1.class) { //│ 1 //│ } else { @@ -190,7 +190,7 @@ if a is C then 3 print(x) //│ JS (unsanitized): -//│ let x1, tmp7; /** scoped **/ +//│ let x1, tmp7; //│ if (a instanceof A1.class) { //│ 1 //│ } else if (a instanceof B1.class) { @@ -219,7 +219,7 @@ if a is print(x + 1) print(x + 2) //│ JS (unsanitized): -//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; /** scoped **/ +//│ let x2, tmp8, tmp9, tmp10, tmp11, tmp12; //│ if (a instanceof B1.class) { //│ 1 //│ } else { diff --git a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls index ddff7a89fe..0d43252465 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ModuleMethods.mls @@ -41,7 +41,7 @@ module Test with fun foo() = print(s) fun bar() = foo() //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -51,7 +51,7 @@ module Test with //│ } //│ static #s; //│ static { -//│ let tmp1; /** scoped **/ +//│ let tmp1; //│ Test.#s = 1; //│ tmp1 = Predef.print(Test.#s); //│ } diff --git a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls index bcc15a4dce..11e0e37234 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Modules.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Modules.mls @@ -4,7 +4,7 @@ :sjs module None //│ JS (unsanitized): -//│ let None1; /** scoped **/ +//│ let None1; //│ globalThis.Object.freeze(class None { //│ static { //│ None1 = this @@ -55,7 +55,7 @@ module M with val x = 1 val y = x + 1 //│ JS (unsanitized): -//│ let M1; /** scoped **/ +//│ let M1; //│ globalThis.Object.freeze(class M { //│ static { //│ M1 = this @@ -64,7 +64,7 @@ module M with //│ runtime.Unit; //│ } //│ static { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ globalThis.Object.freeze(class C { //│ static { //│ M.C = this @@ -123,7 +123,7 @@ M.oops module M with val m: module M = M //│ JS (unsanitized): -//│ let M3; /** scoped **/ +//│ let M3; //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -156,7 +156,7 @@ module AA with val y = 2 [x, y] //│ JS (unsanitized): -//│ let AA1; /** scoped **/ +//│ let AA1; //│ globalThis.Object.freeze(class AA { //│ static { //│ AA1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls index a249ca7f30..25562c62e6 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls @@ -22,7 +22,7 @@ scope.locally of 42 scope.locally of let x = 42 in x //│ JS (unsanitized): -//│ let x; /** scoped **/ x = 42; x +//│ let x; x = 42; x //│ Lowered: //│ Program: //│ imports = Nil @@ -46,7 +46,7 @@ scope.locally of ( x ) //│ JS (unsanitized): -//│ let x1; /** scoped **/ x1 = 42; x1 +//│ let x1; x1 = 42; x1 //│ Lowered: //│ Program: //│ imports = Nil @@ -70,7 +70,7 @@ scope.locally of scope.locally of ( x ) //│ JS (unsanitized): -//│ { let x2; /** scoped **/ x2 = 42; x2 } +//│ { let x2; x2 = 42; x2 } //│ Lowered: //│ Program: //│ imports = Nil @@ -106,13 +106,7 @@ fun foo(x) = //│ foo = function foo(...args) { //│ runtime.checkArgs("foo", 1, true, args.length); //│ let x3 = args[0]; -//│ let z; /** scoped **/{ -//│ let y, tmp; /** scoped **/ -//│ z = 1; -//│ y = 1; -//│ tmp = x3 + y; -//│ return tmp + z -//│ } +//│ let z;{ let y, tmp; z = 1; y = 1; tmp = x3 + y; return tmp + z } //│ }; //│ block$res6 = undefined; @@ -132,11 +126,11 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f() { -//│ let scrut, b; /** scoped **/ +//│ let scrut, b; //│ scrut = true; -//│ if (scrut === true) { let a; /** scoped **/ a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ if (scrut === true) { let a; a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -155,11 +149,11 @@ fun f() = b + 9 ) //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ f1 = function f() { -//│ let scrut, a; /** scoped **/ +//│ let scrut, a; //│ scrut = false; -//│ if (scrut === true) { a = 4; return a + 4 } else { let b; /** scoped **/ b = 5; return b + 9 } +//│ if (scrut === true) { a = 4; return a + 4 } else { let b; b = 5; return b + 9 } //│ }; @@ -178,9 +172,9 @@ fun g(x, y, z) = (_ + a) ) //│ JS (unsanitized): -//│ let g; /** scoped **/ +//│ let g; //│ g = function g(x3, y, z) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ let lambda; //│ split_root$: { //│ split_1$: { @@ -191,7 +185,7 @@ fun g(x, y, z) = //│ break split_1$ //│ } else { //│ if (z === true) { -//│ let a; /** scoped **/ +//│ let a; //│ a = 1; //│ lambda = (undefined, function (_0) { //│ return _0 + a @@ -225,10 +219,10 @@ fun foo(x, y) = print("ruarua") ) //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ foo1 = function foo(x3, y) { -//│ let z, scrut, m, tmp, tmp1; /** scoped **/{ -//│ let w1, w2, scrut1; /** scoped **/ +//│ let z, scrut, m, tmp, tmp1;{ +//│ let w1, w2, scrut1; //│ z = globalThis.Object.freeze([ //│ 1, //│ 2, @@ -257,12 +251,12 @@ scope.locally() :sjs scope.locally of (let x = 1 in x), (let y = 2 in y) //│ JS (unsanitized): -//│ let x3, y; /** scoped **/ x3 = 1; y = 2; y +//│ let x3, y; x3 = 1; y = 2; y //│ = 2 :e scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╔══[ERROR] Name not found: x -//│ ║ l.265: scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ║ l.259: scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╙── ^ diff --git a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls index 9544421bdd..59ec70080c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OpenWildcard.mls @@ -52,7 +52,7 @@ none() :sjs val Option = "Oops" //│ JS (unsanitized): -//│ let Option1; /** scoped **/ Option1 = "Oops"; +//│ let Option1; Option1 = "Oops"; //│ Option = "Oops" :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls index 9edf77ba6a..1cbc9194fb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/OptMatch.mls @@ -10,7 +10,7 @@ fun isDefined(x) = if x is Some then true None then false //│ JS (unsanitized): -//│ let isDefined; /** scoped **/ +//│ let isDefined; //│ isDefined = function isDefined(x) { //│ if (x instanceof Some1.class) { //│ return true @@ -31,9 +31,9 @@ val isDefined = case Some(_) then true None then false //│ JS (unsanitized): -//│ let isDefined1, lambda; /** scoped **/ +//│ let isDefined1, lambda; //│ lambda = (undefined, function (caseScrut) { -//│ let argument0$; /** scoped **/ +//│ let argument0$; //│ if (caseScrut instanceof Some1.class) { //│ argument0$ = caseScrut.value; //│ return true diff --git a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls index 2e93a38133..30be3cf8a8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ParamClasses.mls @@ -6,7 +6,7 @@ data class Foo() //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo() { //│ return globalThis.Object.freeze(new Foo.class()); //│ }; @@ -37,7 +37,7 @@ Foo.class data class Foo(a) //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ Foo3 = function Foo(a) { //│ return globalThis.Object.freeze(new Foo.class(a)); //│ }; @@ -64,19 +64,19 @@ Foo(1) Foo(1).a //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = Foo3(1); tmp.a +//│ let tmp; tmp = Foo3(1); tmp.a //│ = 1 fun foo(y) = Foo(y) foo(27) //│ JS (unsanitized): -//│ let foo; /** scoped **/ foo = function foo(y) { return Foo3(y) }; foo(27) +//│ let foo; foo = function foo(y) { return Foo3(y) }; foo(27) //│ = Foo(27) data class Foo(a, b) //│ JS (unsanitized): -//│ let Foo5; /** scoped **/ +//│ let Foo5; //│ Foo5 = function Foo(a, b) { //│ return globalThis.Object.freeze(new Foo.class(a, b)); //│ }; @@ -94,17 +94,17 @@ data class Foo(a, b) let foo = Foo //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = Foo5; +//│ let foo1; foo1 = Foo5; //│ foo = fun Foo { class: class Foo } let f = foo(1, 2) //│ JS (unsanitized): -//│ let f; /** scoped **/ f = runtime.safeCall(foo1(1, 2)); +//│ let f; f = runtime.safeCall(foo1(1, 2)); //│ f = Foo(1, 2) let f = new! foo(1, 2) //│ JS (unsanitized): -//│ let f1; /** scoped **/ f1 = globalThis.Object.freeze(new foo1(1, 2)); +//│ let f1; f1 = globalThis.Object.freeze(new foo1(1, 2)); //│ f = Foo(1, 2) f.a @@ -119,7 +119,7 @@ f.b let f = Foo(1, 2) //│ JS (unsanitized): -//│ let f2; /** scoped **/ f2 = Foo5(1, 2); +//│ let f2; f2 = Foo5(1, 2); //│ f = Foo(1, 2) f.a @@ -134,7 +134,7 @@ f.b Foo(print(1), print(2)) //│ JS (unsanitized): -//│ let tmp1, tmp2; /** scoped **/ tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo5(tmp1, tmp2) +//│ let tmp1, tmp2; tmp1 = Predef.print(1); tmp2 = Predef.print(2); Foo5(tmp1, tmp2) //│ > 1 //│ > 2 //│ = Foo((), ()) @@ -144,7 +144,7 @@ data class Inner(c) with fun i1(d) = c + d print(c) //│ JS (unsanitized): -//│ let Inner1; /** scoped **/ +//│ let Inner1; //│ Inner1 = function Inner(c) { //│ return globalThis.Object.freeze(new Inner.class(c)); //│ }; @@ -165,7 +165,7 @@ data class Inner(c) with let i = new Inner(100) //│ JS (unsanitized): -//│ let i; /** scoped **/ i = globalThis.Object.freeze(new Inner1.class(100)); +//│ let i; i = globalThis.Object.freeze(new Inner1.class(100)); //│ > 100 //│ i = Inner(100) @@ -185,7 +185,7 @@ class Foo(x, val y, z, val z, z) with print("z = " + z) print("this.z = " + this.z) //│ JS (unsanitized): -//│ let Foo7; /** scoped **/ +//│ let Foo7; //│ Foo7 = function Foo(x, y, z, z1, z2) { //│ return globalThis.Object.freeze(new Foo.class(x, y, z, z1, z2)); //│ }; @@ -194,7 +194,7 @@ class Foo(x, val y, z, val z, z) with //│ Foo7.class = this //│ } //│ constructor(x, y, z, z1, z2) { -//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ this.#x = x; //│ this.y = y; //│ this.#z = z; @@ -243,7 +243,7 @@ class Foo(val z, val z) //│ ║ l.237: class Foo(val z, val z) //│ ╙── ^ //│ JS (unsanitized): -//│ let Foo9; /** scoped **/ +//│ let Foo9; //│ Foo9 = function Foo(z, z1) { //│ return globalThis.Object.freeze(new Foo.class(z, z1)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls index c685072863..a299d263fb 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PartialApps.mls @@ -12,7 +12,7 @@ f(2) :sjs let f = foo(1, _, _) //│ JS (unsanitized): -//│ let f2; /** scoped **/ let f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; +//│ let f2; let f3; f3 = function f(_0, _1) { return foo(1, _0, _1) }; f2 = f3; //│ f = fun f f(2, 3) @@ -79,10 +79,7 @@ h(1) :sjs let i = _(0, 1, _) //│ JS (unsanitized): -//│ let i; /** scoped **/ -//│ let i1; -//│ i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; -//│ i = i1; +//│ let i; let i1; i1 = function i(_0, _1) { return runtime.safeCall(_0(0, 1, _1)) }; i = i1; //│ i = fun i i(print, 2) @@ -202,7 +199,7 @@ _ - _ of 1, 2 :pe |> 1 //│ ╔══[PARSE ERROR] Expected end of input; found literal instead -//│ ║ l.203: |> 1 +//│ ║ l.200: |> 1 //│ ╙── ^ //│ = fun pipeInto @@ -210,7 +207,7 @@ _ - _ of 1, 2 :re |> (1) //│ ╔══[ERROR] Expected 2 arguments, got 1 -//│ ║ l.211: |> (1) +//│ ║ l.208: |> (1) //│ ╙── ^^^ //│ ═══[RUNTIME ERROR] TypeError: f is not a function @@ -228,15 +225,13 @@ _ - _ of 1, 2 :sjs 1 \ (_ - 2) //│ JS (unsanitized): -//│ let lambda38; /** scoped **/ -//│ lambda38 = (undefined, function (_0) { return _0 - 2 }); -//│ Predef.passTo(1, lambda38) +//│ let lambda38; lambda38 = (undefined, function (_0) { return _0 - 2 }); Predef.passTo(1, lambda38) //│ = fun :sjs 1 \ (_ - 2)() //│ JS (unsanitized): -//│ let lambda39, tmp19; /** scoped **/ +//│ let lambda39, tmp19; //│ lambda39 = (undefined, function (_0) { //│ return _0 - 2 //│ }); @@ -253,13 +248,13 @@ _ - _ of 1, 2 :w let f = if _ then 1 else 0 //│ ╔══[WARNING] This catch-all clause makes the following branches unreachable. -//│ ║ l.254: let f = if _ then 1 else 0 +//│ ║ l.249: let f = if _ then 1 else 0 //│ ║ ^^^^^^ //│ ╟── This branch is unreachable. -//│ ║ l.254: let f = if _ then 1 else 0 +//│ ║ l.249: let f = if _ then 1 else 0 //│ ╙── ^^^^^^ //│ JS (unsanitized): -//│ let f15, tmp21; /** scoped **/ tmp21 = 1; f15 = tmp21; +//│ let f15, tmp21; tmp21 = 1; f15 = tmp21; //│ f = 1 :sjs @@ -268,9 +263,9 @@ fun f(x) = 0 then 1 _ then 2 //│ JS (unsanitized): -//│ let f16; /** scoped **/ +//│ let f16; //│ f16 = function f(x3) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = Predef.equals(x3, 0); //│ if (scrut === true) { return 1 } else { return 2 } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls index eb2618f1fd..acf7a35fca 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/PlainClasses.mls @@ -6,7 +6,7 @@ class Foo //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -23,7 +23,7 @@ Foo is Foo (new Foo) is Foo //│ JS (unsanitized): -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = globalThis.Object.freeze(new Foo1()); //│ if (scrut instanceof Foo1) { true } else { false } //│ = true @@ -53,7 +53,7 @@ Foo() data class Foo with { print("hi") } print("ok") //│ JS (unsanitized): -//│ let Foo3; /** scoped **/ +//│ let Foo3; //│ globalThis.Object.freeze(class Foo2 { //│ static { //│ Foo3 = this @@ -72,9 +72,9 @@ fun test() = print("ok") Foo //│ JS (unsanitized): -//│ let test; /** scoped **/ +//│ let test; //│ test = function test() { -//│ let Foo5, tmp; /** scoped **/ +//│ let Foo5, tmp; //│ globalThis.Object.freeze(class Foo4 { //│ static { //│ Foo5 = this @@ -91,7 +91,7 @@ fun test() = let t = test() //│ JS (unsanitized): -//│ let t; /** scoped **/ t = test(); +//│ let t; t = test(); //│ > ok //│ t = class Foo @@ -135,7 +135,7 @@ class Foo with let y = x + 1 fun z() = y + x //│ JS (unsanitized): -//│ let Foo6; /** scoped **/ +//│ let Foo6; //│ globalThis.Object.freeze(class Foo5 { //│ static { //│ Foo6 = this @@ -162,7 +162,7 @@ class Foo with fun z2() = 6 print("hello") //│ JS (unsanitized): -//│ let Foo8; /** scoped **/ +//│ let Foo8; //│ globalThis.Object.freeze(class Foo7 { //│ static { //│ Foo8 = this @@ -192,7 +192,7 @@ class Foo with fun foo(y) = x + y fun bar(z) = foo(z) + 1 //│ JS (unsanitized): -//│ let Foo10; /** scoped **/ +//│ let Foo10; //│ globalThis.Object.freeze(class Foo9 { //│ static { //│ Foo10 = this @@ -204,7 +204,7 @@ class Foo with //│ return this.x + y //│ } //│ bar(z) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = this.foo(z); //│ return tmp + 1 //│ } @@ -217,7 +217,7 @@ print(a.x) print(a.foo(1)) print(a.bar(1)) //│ JS (unsanitized): -//│ let a, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let a, tmp, tmp1, tmp2, tmp3; //│ a = globalThis.Object.freeze(new Foo10()); //│ tmp = Predef.print(a.x); //│ tmp1 = runtime.safeCall(a.foo(1)); @@ -243,7 +243,7 @@ class Foo with //│ ║ l.237: val x = 2 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo12; /** scoped **/ +//│ let Foo12; //│ globalThis.Object.freeze(class Foo11 { //│ static { //│ Foo12 = this @@ -267,7 +267,7 @@ class Foo with //│ ║ l.261: val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo14; /** scoped **/ +//│ let Foo14; //│ globalThis.Object.freeze(class Foo13 { //│ static { //│ Foo14 = this @@ -294,7 +294,7 @@ class Foo with val x = 1 //│ ║ l.292: class Foo with val x = 1 //│ ╙── ^^^^^^^^^ //│ JS (unsanitized): -//│ let Foo16; /** scoped **/ +//│ let Foo16; //│ globalThis.Object.freeze(class Foo15 { //│ static { //│ Foo16 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls index ea8a6fed34..8a571ad88e 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Primes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Primes.mls @@ -7,7 +7,7 @@ let x = 1 :sjs let x' = 2 //│ JS (unsanitized): -//│ let x$_; /** scoped **/ x$_ = 2; +//│ let x$_; x$_ = 2; //│ x' = 2 x' diff --git a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls index 3cd21a98c2..4defe108c8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Pwd.mls @@ -6,7 +6,7 @@ let folderName1 = process.env.PWD.split("/").pop() in let folderName2 = process.cwd().split("/").pop() in folderName2 === folderName1 || folderName2 === "shared" //│ JS (unsanitized): -//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; /** scoped **/ +//│ let folderName1, folderName2, tmp, tmp1, tmp2, tmp3, lambda; //│ tmp = runtime.safeCall(globalThis.process.env.PWD.split("/")); //│ folderName1 = runtime.safeCall(tmp.pop()); //│ tmp1 = runtime.safeCall(globalThis.process.cwd()); diff --git a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls index e2537d1ad9..3c1db6eeb8 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Quasiquotes.mls @@ -76,7 +76,7 @@ x `=> print(x) x //│ JS (unsanitized): -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ let x1, tmp7, tmp8, arr2; //│ tmp7 = globalThis.Object.freeze(new Term.Symbol("x")); //│ x1 = globalThis.Object.freeze(new Term.Ref(tmp7)); diff --git a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls index a252994119..10940ea078 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/RandomStuff.mls @@ -4,9 +4,9 @@ :sjs fun foo() = if false do foo() //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo() { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = false; //│ if (scrut === true) { return foo() } else { return runtime.Unit } //│ }; @@ -14,7 +14,7 @@ fun foo() = if false do foo() :sjs fun foo() = foo() //│ JS (unsanitized): -//│ let foo1; /** scoped **/ foo1 = function foo() { return foo1() }; +//│ let foo1; foo1 = function foo() { return foo1() }; :sjs @@ -22,7 +22,7 @@ data class Foo(x) with class Bar with val y = x //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ Foo1 = function Foo(x) { //│ return globalThis.Object.freeze(new Foo.class(x)); //│ }; @@ -54,12 +54,7 @@ fun foo() = fun bar() = bar() bar() //│ JS (unsanitized): -//│ let foo2; /** scoped **/ -//│ foo2 = function foo() { -//│ let bar; /** scoped **/ -//│ bar = function bar() { return bar() }; -//│ return bar() -//│ }; +//│ let foo2; foo2 = function foo() { let bar; bar = function bar() { return bar() }; return bar() }; :sjs @@ -68,10 +63,7 @@ do fun f = f () //│ JS (unsanitized): -//│ let f, f1; /** scoped **/ -//│ f1 = 1; -//│ f = function f() { let tmp; /** scoped **/ tmp = f(); return tmp }; -//│ runtime.Unit +//│ let f, f1; f1 = 1; f = function f() { let tmp; tmp = f(); return tmp }; runtime.Unit //│ f = 1 :sjs @@ -79,13 +71,13 @@ do let foo = 1 fun foo(x) = foo //│ ╔══[ERROR] Name 'foo' is already used -//│ ║ l.79: let foo = 1 +//│ ║ l.71: let foo = 1 //│ ║ ^^^^^^^ //│ ╟── by a member declared in the same block -//│ ║ l.80: fun foo(x) = foo +//│ ║ l.72: fun foo(x) = foo //│ ╙── ^^^^^^^^^^^^^^^^ //│ JS (unsanitized): -//│ let foo3, foo4; /** scoped **/ foo3 = function foo(x) { return foo4 }; foo4 = 1; +//│ let foo3, foo4; foo3 = function foo(x) { return foo4 }; foo4 = 1; //│ foo = 1 :sjs diff --git a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls index 679eaa4edf..f56f6ac6b4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SanityChecks.mls @@ -52,7 +52,7 @@ id(f)(3)(4) //│ runtime.checkArgs("", 2, true, args1.length); //│ let y = args1[0]; //│ let z = args1[1]; -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ tmp4 = x + y; //│ return tmp4 + z //│ } @@ -114,7 +114,7 @@ id(Cls(1, 2)).f(3) //│ this.y = y; //│ } //│ f(z, p) { -//│ let tmp6, tmp7; /** scoped **/ +//│ let tmp6, tmp7; //│ tmp6 = this.x + this.y; //│ tmp7 = tmp6 + z; //│ return tmp7 + p @@ -150,7 +150,7 @@ id(Cls(1, 2)).f(3) //│ runtime.checkArgs("f", 2, true, args.length); //│ let z = args[0]; //│ let p = args[1]; -//│ let tmp8, tmp9; /** scoped **/ +//│ let tmp8, tmp9; //│ tmp8 = this.x + this.y; //│ tmp9 = tmp8 + z; //│ return tmp9 + p @@ -191,7 +191,7 @@ id(Cls(1, 2)).f(3, 4)(5) //│ runtime.checkArgs("", 2, true, args1.length); //│ let q = args1[0]; //│ let s = args1[1]; -//│ let tmp11, tmp12, tmp13, tmp14; /** scoped **/ +//│ let tmp11, tmp12, tmp13, tmp14; //│ tmp11 = this.x + this.y; //│ tmp12 = tmp11 + z; //│ tmp13 = tmp12 + p; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls index faea7790d6..038ccfc0bf 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls @@ -6,7 +6,7 @@ let x = let y = 1 + 3 * 4 y + 4 //│ JS (unsanitized): -//│ let x, y, tmp; /** scoped **/ tmp = 3 * 4; y = 1 + tmp; x = y + 4; +//│ let x, y, tmp; tmp = 3 * 4; y = 1 + tmp; x = y + 4; //│ x = 17 @@ -19,9 +19,9 @@ fun f() = let b = 5 b + 9 //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f() { -//│ let scrut, a, b; /** scoped **/ +//│ let scrut, a, b; //│ scrut = true; //│ if (scrut === true) { a = 4; return a + 4 } else { b = 5; return b + 9 } //│ }; @@ -39,9 +39,9 @@ fun g(x, y, z) = let a = 1 (_ + a) //│ JS (unsanitized): -//│ let g; /** scoped **/ +//│ let g; //│ g = function g(x1, y1, z) { -//│ let a, tmp1, tmp2; /** scoped **/ +//│ let a, tmp1, tmp2; //│ let lambda; //│ split_root$: { //│ split_1$: { @@ -83,9 +83,9 @@ fun f() = if x is A(a, b) and a > b then true B(c, d) then false //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ f1 = function f() { -//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; /** scoped **/ +//│ let a, b, scrut, c, d, argument0$, argument1$, tmp1; //│ split_root$: { //│ split_default$: { //│ if (x instanceof A1.class) { @@ -121,9 +121,9 @@ fun f() = if x is fun f() = if A(1, Nil) is A(1, Nil) then 3 //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ f2 = function f() { -//│ let scrut, argument0$, argument1$, tmp1; /** scoped **/ +//│ let scrut, argument0$, argument1$, tmp1; //│ split_root$: { //│ split_default$: { //│ scrut = A1(1, Nil1); @@ -160,11 +160,11 @@ fun f(x) = x() x //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(x1) { -//│ let tmp1; /** scoped **/ +//│ let tmp1; //│ tmp2: while (true) { -//│ let a, a1, argument0$, argument1$; /** scoped **/ +//│ let a, a1, argument0$, argument1$; //│ if (x1 instanceof A1.class) { //│ argument0$ = x1.a; //│ argument1$ = x1.b; @@ -196,12 +196,12 @@ fun f() = else y = a //│ JS (unsanitized): -//│ let f4; /** scoped **/ +//│ let f4; //│ f4 = function f() { -//│ let y1, tmp1; /** scoped **/ +//│ let y1, tmp1; //│ y1 = x + 1; //│ tmp2: while (true) { -//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp3; /** scoped **/ +//│ let a, z, scrut, scrut1, argument0$, argument1$, tmp3; //│ split_root$: { //│ split_1$: { //│ if (y1 instanceof A1.class) { @@ -239,11 +239,11 @@ fun f(x, y) = while x && y do f(0, 0) + 4 //│ JS (unsanitized): -//│ let f5; /** scoped **/ +//│ let f5; //│ f5 = function f(x1, y1) { -//│ let tmp1; /** scoped **/ +//│ let tmp1; //│ tmp2: while (true) { -//│ let scrut, lambda, tmp3; /** scoped **/ +//│ let scrut, lambda, tmp3; //│ lambda = (undefined, function () { //│ return y1 //│ }); @@ -264,13 +264,13 @@ fun f(x, y) = class A with val x = 1 + 2 //│ JS (unsanitized): -//│ let A3; /** scoped **/ +//│ let A3; //│ globalThis.Object.freeze(class A2 { //│ static { //│ A3 = this //│ } //│ constructor() { -//│ let tmp1; /** scoped **/ +//│ let tmp1; //│ tmp1 = 1 + 2; //│ this.x = tmp1; //│ } @@ -289,7 +289,7 @@ set state += 1 module Foo with val res = Foo().foo() //│ JS (unsanitized): -//│ let Foo1, tmp1; /** scoped **/ +//│ let Foo1, tmp1; //│ tmp1 = state + 1; //│ state = tmp1; //│ Foo1 = function Foo() { @@ -301,7 +301,7 @@ module Foo with //│ } //│ constructor() {} //│ static { -//│ let tmp2, tmp3; /** scoped **/ +//│ let tmp2, tmp3; //│ tmp2 = Foo1(); //│ tmp3 = tmp2.foo(); //│ this.res = tmp3; @@ -326,7 +326,7 @@ type I = Int class A(x) with val y = x //│ JS (unsanitized): -//│ let A5; /** scoped **/ +//│ let A5; //│ A5 = function A(x1) { //│ return globalThis.Object.freeze(new A.class(x1)); //│ }; @@ -349,7 +349,7 @@ class A(x) with module M with data class A(x, y) //│ JS (unsanitized): -//│ let M3; /** scoped **/ +//│ let M3; //│ globalThis.Object.freeze(class M2 { //│ static { //│ M3 = this @@ -383,11 +383,11 @@ fun f(x) = let y = x x //│ JS (unsanitized): -//│ let f6; /** scoped **/ +//│ let f6; //│ f6 = function f(x1) { -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ tmp3: while (true) { -//│ let y1; /** scoped **/ +//│ let y1; //│ if (x1 === true) { //│ y1 = x1; //│ tmp2 = x1; @@ -406,12 +406,12 @@ fun f(x) = let y = x x //│ JS (unsanitized): -//│ let f7; /** scoped **/ +//│ let f7; //│ f7 = function f(x1) { -//│ let tmp2, while1, tmp3, tmp4; /** scoped **/ +//│ let tmp2, while1, tmp3, tmp4; //│ tmp2 = undefined; //│ while1 = (undefined, function () { -//│ let y1; /** scoped **/ +//│ let y1; //│ if (x1 === true) { //│ y1 = x1; //│ tmp2 = x1; diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls index 76ad2c3e21..fc4f6470d4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocksAndHandlers.mls @@ -18,9 +18,9 @@ fun f(x) = let z = 3 fun g() = z //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f(x) { -//│ let g, z; /** scoped **/ +//│ let g, z; //│ g = function g() { //│ return z //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls index ea5487f089..7682c56896 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SelfReferences.mls @@ -5,7 +5,7 @@ module Foo with val self: module Foo = Foo //│ JS (unsanitized): -//│ let Foo1; /** scoped **/ +//│ let Foo1; //│ globalThis.Object.freeze(class Foo { //│ static { //│ Foo1 = this @@ -61,13 +61,13 @@ module Foo with object Foo with val self = id(Foo) //│ JS (unsanitized): -//│ let Foo11; /** scoped **/ +//│ let Foo11; //│ globalThis.Object.freeze(class Foo10 { //│ static { //│ Foo11 = globalThis.Object.freeze(new this) //│ } //│ constructor() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = Predef.id(Foo11); //│ this.self = tmp; //│ Object.defineProperty(this, "class", { diff --git a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls index 7ac70bc6b4..77463ae9a7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/SetIn.mls @@ -7,7 +7,7 @@ let x = 0 :sjs let x += 1 //│ JS (unsanitized): -//│ let x1; /** scoped **/ x1 = x + 1; +//│ let x1; x1 = x + 1; //│ x = 1 x @@ -21,7 +21,7 @@ set x = 0 :sjs set x += 1 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = x1 + 1; x1 = tmp; runtime.Unit +//│ let tmp; tmp = x1 + 1; x1 = tmp; runtime.Unit x //│ = 1 @@ -30,7 +30,7 @@ x :sjs set x += 1 in print(x) //│ JS (unsanitized): -//│ let old, tmp1, tmp2, tmp3; /** scoped **/ +//│ let old, tmp1, tmp2, tmp3; //│ old = x1; //│ try { tmp2 = x1 + 1; x1 = tmp2; tmp3 = Predef.print(x1); tmp1 = tmp3; } finally { x1 = old; } //│ tmp1 @@ -69,9 +69,9 @@ fun example() = print(get_x()) example() //│ JS (unsanitized): -//│ let example2; /** scoped **/ +//│ let example2; //│ example2 = function example() { -//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; /** scoped **/ +//│ let x2, get_x, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11; //│ let get_x1; //│ x2 = 0; //│ get_x1 = function get_x() { @@ -111,9 +111,9 @@ fun example() = y example() //│ JS (unsanitized): -//│ let example3; /** scoped **/ +//│ let example3; //│ example3 = function example() { -//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; /** scoped **/ +//│ let x2, get_x, y, old1, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10; //│ let get_x1; //│ x2 = 0; //│ get_x1 = function get_x() { diff --git a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls index 228d0613df..9be3a3a5b7 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Spreads.mls @@ -19,7 +19,7 @@ foo(0, ...a) :sjs foo(1, ...[2, 3], 4) //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) +//│ let tmp; tmp = globalThis.Object.freeze([ 2, 3 ]); foo(1, ...tmp, 4) //│ = [1, 2, 3, 4] :re diff --git a/hkmc2/shared/src/test/mlscript/codegen/This.mls b/hkmc2/shared/src/test/mlscript/codegen/This.mls index 9d809eb9e6..64418595dd 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/This.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/This.mls @@ -10,14 +10,13 @@ fun test(x) = //│ ║ l.8: [this.a, x] //│ ╙── ^^^^ //│ JS (unsanitized): -//│ let test; /** scoped **/ test = function test(x) { return globalThis.Object.freeze([]) }; +//│ let test; test = function test(x) { return globalThis.Object.freeze([]) }; :sjs fun test(x) = [globalThis.a, x] //│ JS (unsanitized): -//│ let test1; /** scoped **/ -//│ test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; +//│ let test1; test1 = function test(x) { return globalThis.Object.freeze([ globalThis.a, x ]) }; :re test(123) @@ -43,7 +42,7 @@ module Test with fun test2(x) = [this.a, x] //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls index f68cf9062b..99166a71c4 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCallVariations.mls @@ -39,7 +39,7 @@ Example .!. oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = call(Example1, oops); runtime.safeCall(tmp(2)) +//│ let tmp; tmp = call(Example1, oops); runtime.safeCall(tmp(2)) //│ = [2, 1] Example.!. oops(2) @@ -106,7 +106,7 @@ Example .> oops(2) //│ rhs = Tup of Ls of //│ IntLit of 2 //│ JS (unsanitized): -//│ let tmp22; /** scoped **/ tmp22 = runtime.safeCall(oops(2)); call1(Example1, tmp22) +//│ let tmp22; tmp22 = runtime.safeCall(oops(2)); call1(Example1, tmp22) //│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'a') // * Note: diff --git a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls index b2171fa4c0..cad43e858a 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ThisCalls.mls @@ -19,7 +19,7 @@ s(123) :sjs ex |>. s(123) //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) +//│ let tmp1; tmp1 = Predef.call(ex, s); runtime.safeCall(tmp1(123)) //│ = [123, 456] diff --git a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls index fa1b38cc34..60ab9f2c5c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/Throw.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/Throw.mls @@ -31,9 +31,7 @@ fun f(x) = return y f(1) //│ JS (unsanitized): -//│ let f2; /** scoped **/ -//│ f2 = function f(x) { let y; /** scoped **/ throw globalThis.Error("e") }; -//│ f2(1) +//│ let f2; f2 = function f(x) { let y; throw globalThis.Error("e") }; f2(1) //│ ═══[RUNTIME ERROR] Error: e @@ -43,7 +41,7 @@ fun f(x) = throw (if x then Error("x") else Error("y")) f(false) //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ f3 = function f(x) { //│ if (x === true) { throw globalThis.Error("x") } else { throw globalThis.Error("y") } //│ }; diff --git a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls index 63ea5b79ad..1b3593632c 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls @@ -7,9 +7,9 @@ fun fib(a) = if a <= 1 then a else fib(a - 1) + fib(a - 2) //│ JS (unsanitized): -//│ let fib; /** scoped **/ +//│ let fib; //│ fib = function fib(a) { -//│ let scrut, tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let scrut, tmp, tmp1, tmp2, tmp3; //│ scrut = a <= 1; //│ if (scrut === true) { //│ return a diff --git a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls index 72170fa838..c0ec266ced 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/UnitValue.mls @@ -30,9 +30,7 @@ print of foo() :sjs print of globalThis.console.log("Hello, world!") //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ -//│ tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); -//│ Predef.print(tmp4) +//│ let tmp4; tmp4 = runtime.safeCall(globalThis.console.log("Hello, world!")); Predef.print(tmp4) //│ > Hello, world! //│ > () @@ -56,7 +54,7 @@ print of Box(foo()).value :sjs fun foo() = {} //│ JS (unsanitized): -//│ let foo5; /** scoped **/ foo5 = function foo() { return runtime.Unit }; +//│ let foo5; foo5 = function foo() { return runtime.Unit }; print of Box(foo()).value //│ > () @@ -66,7 +64,7 @@ print of Box(foo()).value fun foo(x) = set x = 1 //│ JS (unsanitized): -//│ let foo6; /** scoped **/ foo6 = function foo(x) { x = 1; return runtime.Unit }; +//│ let foo6; foo6 = function foo(x) { x = 1; return runtime.Unit }; print of Box(foo(1)).value //│ > () @@ -75,7 +73,7 @@ print of Box(foo(1)).value :e fun f = let x = 1 //│ ╔══[ERROR] Expected a body for let bindings in expression position -//│ ║ l.76: fun f = let x = 1 +//│ ║ l.74: fun f = let x = 1 //│ ╙── ^^^^^ print of f diff --git a/hkmc2/shared/src/test/mlscript/codegen/While.mls b/hkmc2/shared/src/test/mlscript/codegen/While.mls index 1a52c71596..65570a7f94 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/While.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/While.mls @@ -6,9 +6,9 @@ //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function () { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp1: while (true) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = true; //│ if (scrut === true) { //│ tmp = 0; @@ -54,9 +54,9 @@ while x set x = false else 42 //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ tmp5: while (true) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ if (x2 === true) { //│ tmp6 = Predef.print("Hello World"); //│ x2 = false; @@ -107,9 +107,9 @@ while //│ JS (unsanitized): //│ let lambda2; //│ lambda2 = (undefined, function () { -//│ let tmp19; /** scoped **/ +//│ let tmp19; //│ tmp20: while (true) { -//│ let i2, scrut3, tmp21; /** scoped **/ +//│ let i2, scrut3, tmp21; //│ i2 = 0; //│ scrut3 = i2 < 10; //│ if (scrut3 === true) { @@ -198,11 +198,11 @@ fun f(ls) = print(h) else print("Done!") //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f(ls) { -//│ let tmp28; /** scoped **/ +//│ let tmp28; //│ tmp29: while (true) { -//│ let tl, h, argument0$, argument1$; /** scoped **/ +//│ let tl, h, argument0$, argument1$; //│ if (ls instanceof Cons1.class) { //│ argument0$ = ls.hd; //│ argument1$ = ls.tl; @@ -252,7 +252,7 @@ let x = 1 :sjs while x is {} do() //│ JS (unsanitized): -//│ let tmp37; /** scoped **/ +//│ let tmp37; //│ tmp38: while (true) { //│ split_root$: { //│ split_1$: { diff --git a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls index 2ee44be02f..6fe4528dde 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/ClassCtxParams.mls @@ -101,7 +101,7 @@ class T class Foo(using T) with print(T) //│ JS (unsanitized): -//│ let Foo9; /** scoped **/ +//│ let Foo9; //│ Foo9 = function Foo(tmp5) { //│ return globalThis.Object.freeze(new Foo.class(tmp5)); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls index 18422737ba..005ed3b089 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/EtaExpansion.mls @@ -31,10 +31,10 @@ using Num = 0.42 :sjs let f1 = foo //│ JS (unsanitized): -//│ let f1; /** scoped **/ +//│ let f1; //│ let f11; //│ f11 = function f1() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = foo(); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; @@ -53,10 +53,10 @@ f1() :sjs let f2 = bar //│ JS (unsanitized): -//│ let f2; /** scoped **/ +//│ let f2; //│ let f21; //│ f21 = function f2(x) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = bar(x); //│ return runtime.safeCall(tmp(instance$Ident$_Int$_)) //│ }; @@ -75,12 +75,12 @@ f2(0.42) :sjs let f3 = baz //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ let f31; //│ f31 = function f3(x) { //│ let lambda1; //│ lambda1 = (undefined, function (y) { -//│ let tmp, tmp1, tmp2; /** scoped **/ +//│ let tmp, tmp1, tmp2; //│ tmp = baz(x); //│ tmp1 = runtime.safeCall(tmp(instance$Ident$_Int$_)); //│ tmp2 = runtime.safeCall(tmp1(y)); @@ -99,14 +99,14 @@ f3(43)(44) :sjs foo() //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) +//│ let tmp1; tmp1 = foo(); runtime.safeCall(tmp1(instance$Ident$_Int$_)) //│ = [42] // This should not expand :sjs bar(0.42) //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) +//│ let tmp2; tmp2 = bar(0.42); runtime.safeCall(tmp2(instance$Ident$_Int$_)) //│ = [0.42, 42] @@ -115,7 +115,7 @@ module Test with fun test(j)(using Int) = 0 fun main(using Int) = test //│ JS (unsanitized): -//│ let Test1; /** scoped **/ +//│ let Test1; //│ globalThis.Object.freeze(class Test { //│ static { //│ Test1 = this @@ -131,7 +131,7 @@ module Test with //│ static main(tmp3) { //│ let lambda1; //│ lambda1 = (undefined, function (j) { -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ tmp4 = Test.test(j); //│ return runtime.safeCall(tmp4(tmp3)) //│ }); diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index a58b71139f..482d1d6532 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -45,7 +45,7 @@ test(1, 2) :re 1 ++ 1 //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = test(); tmp2(1, 1) +//│ let tmp2; tmp2 = test(); tmp2(1, 1) //│ ═══[RUNTIME ERROR] TypeError: test is not a function diff --git a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls index f8c332aa31..ccf38c5495 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Debugging.mls @@ -17,7 +17,7 @@ fun f() = print of raiseUnhandledEffect() j / i //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ let getLocals2; //│ getLocals2 = function getLocals() { //│ let prev, thisInfo, arr, tmp; @@ -28,7 +28,7 @@ fun f() = //│ return prev //│ }; //│ f = function f() { -//│ let i, j, k, scrut, tmp, tmp1; /** scoped **/ +//│ let i, j, k, scrut, tmp, tmp1; //│ let getLocals3, Cont$func$f$1, doUnwind; //│ getLocals3 = function getLocals() { //│ let i1, j1, k1, prev, thisInfo, arr, tmp2; diff --git a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls index f677b2d3d1..55fecaedaf 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/Effects.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/Effects.mls @@ -156,7 +156,7 @@ if true do fun f() = 3 f() //│ JS (unsanitized): -//│ let f, scrut, tmp20; /** scoped **/ +//│ let f, scrut, tmp20; //│ let handleBlock$11; //│ handleBlock$11 = function handleBlock$() { //│ let h, res, Cont$handleBlock$h$11, doUnwind, Handler$h$12; diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls index 3b0ece8c6e..ee16b6ecbd 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsHygiene.mls @@ -13,9 +13,9 @@ fun foo(h): module M = module A A //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(h) { -//│ let A2, A3, scrut; /** scoped **/ +//│ let A2, A3, scrut; //│ globalThis.Object.freeze(class A { //│ static { //│ A2 = this diff --git a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls index 83c9524e92..b7c07f0546 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/EffectsInClasses.mls @@ -10,7 +10,7 @@ abstract class Effect with data class Lol(h) with print(h.perform("k")) //│ JS (unsanitized): -//│ let Lol1; /** scoped **/ +//│ let Lol1; //│ Lol1 = function Lol(h) { //│ return globalThis.Object.freeze(new Lol.class(h)); //│ }; @@ -19,7 +19,7 @@ data class Lol(h) with //│ Lol1.class = this //│ } //│ constructor(h) { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ let res, Cont$ctor$Lol$1, doUnwind; //│ const this$Lol = this; //│ globalThis.Object.freeze(class Cont$ctor$Lol$ extends runtime.FunctionContFrame.class { diff --git a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls index f4b53fdfcd..36a0c0416c 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/RecursiveHandlers.mls @@ -155,7 +155,7 @@ if true do h1.perform(()) str //│ JS (unsanitized): -//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; /** scoped **/ +//│ let str, scrut, tmp24, tmp25, tmp26, tmp27; //│ let handleBlock$7; //│ str = ""; //│ scrut = true; @@ -173,7 +173,7 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30; /** scoped **/ +//│ let tmp28, tmp29, tmp30; //│ let Cont$handler$h1$perform$2, doUnwind1; //│ globalThis.Object.freeze(class Cont$handler$h1$perform$1 extends runtime.FunctionContFrame.class { //│ static { @@ -261,7 +261,7 @@ str //│ perform(arg) { //│ let hdlrFun; //│ hdlrFun = function hdlrFun(k) { -//│ let tmp28, tmp29, tmp30, tmp31, tmp32; /** scoped **/ +//│ let tmp28, tmp29, tmp30, tmp31, tmp32; //│ let Cont$handler$h2$perform$2, doUnwind2; //│ globalThis.Object.freeze(class Cont$handler$h2$perform$1 extends runtime.FunctionContFrame.class { //│ static { diff --git a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls index 6dd7795543..722499592a 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/SetInHandlers.mls @@ -8,7 +8,7 @@ let x = 1 set x += 1 in print(x) x //│ JS (unsanitized): -//│ let x, old, tmp, tmp1, tmp2; /** scoped **/ +//│ let x, old, tmp, tmp1, tmp2; //│ x = 1; //│ old = x; //│ try { diff --git a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls index 2915ca9d40..6f285990d6 100644 --- a/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls +++ b/hkmc2/shared/src/test/mlscript/handlers/StackSafety.mls @@ -22,10 +22,10 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi; /** scoped **/ +//│ let hi; //│ let res, $_stack$_safe$_body$_; //│ hi = function hi(n) { -//│ let scrut, tmp; /** scoped **/ +//│ let scrut, tmp; //│ let Cont$func$hi$1, doUnwind, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$hi$ extends runtime.FunctionContFrame.class { //│ static { @@ -78,10 +78,10 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1; /** scoped **/ +//│ let sum1; //│ let res1, $_stack$_safe$_body$_1; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1; /** scoped **/ +//│ let scrut, tmp, tmp1; //│ let Cont$func$sum$1, doUnwind, curDepth, stackDelayRes; //│ globalThis.Object.freeze(class Cont$func$sum$ extends runtime.FunctionContFrame.class { //│ static { @@ -262,9 +262,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; /** scoped **/ +//│ let max; //│ max = function max(a, b) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -276,7 +276,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls index a1596055ef..76a8e46f8f 100644 --- a/hkmc2/shared/src/test/mlscript/interop/Arrays.mls +++ b/hkmc2/shared/src/test/mlscript/interop/Arrays.mls @@ -13,7 +13,7 @@ arr.map(_ === false) :sjs arr.map((e, ...) => e === false) //│ JS (unsanitized): -//│ let lambda1; /** scoped **/ +//│ let lambda1; //│ lambda1 = (undefined, function (e, ..._) { return e === false }); //│ runtime.safeCall(arr.map(lambda1)) //│ = [false, true] @@ -21,7 +21,7 @@ arr.map((e, ...) => e === false) :sjs arr.map((_, ...) => 1) //│ JS (unsanitized): -//│ let lambda2; /** scoped **/ +//│ let lambda2; //│ lambda2 = (undefined, function (_, ..._1) { return 1 }); //│ runtime.safeCall(arr.map(lambda2)) //│ = [1, 1] diff --git a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls index 8fc7c97d05..0985ecf1a0 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls @@ -88,7 +88,7 @@ fun f() = Good() f().foo() //│ JS (unsanitized): -//│ let f6, tmp9; /** scoped **/ +//│ let f6, tmp9; //│ let Bad1, Good1, Bad$, Good$, f$capture3; //│ Good$ = function Good$(isMut, x, y, z, f$capture4) { //│ let tmp10, tmp11; @@ -131,7 +131,7 @@ f().foo() //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ foo() { -//│ let tmp10, tmp11; /** scoped **/ +//│ let tmp10, tmp11; //│ this.z = 100; //│ tmp10 = this.x + this.y; //│ tmp11 = tmp10 + this.z; @@ -186,7 +186,7 @@ f().foo() //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f6 = function f() { -//│ let x, y, z, w, tmp10, tmp11; /** scoped **/ +//│ let x, y, z, w, tmp10, tmp11; //│ let capture; //│ capture = new f$capture3(null); //│ x = 1; diff --git a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls index 740ffcbb6f..9ae2b447c7 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/CompanionsInFun.mls @@ -11,13 +11,13 @@ fun f(x) = fun g = new A //│ ═══[WARNING] Modules are not yet lifted. //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ let g$; //│ g$ = function g$(A$member, A1, x) { //│ return globalThis.Object.freeze(new A$member()) //│ }; //│ f = function f(x) { -//│ let A1, g; /** scoped **/ +//│ let A1, g; //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls index 40b369eb47..9162254161 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/DefnsInClass.mls @@ -7,7 +7,7 @@ data class A(x) with fun getB() = x + y fun getA() = B(2).getB() //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ let B1, B$; //│ B$ = function B$(isMut, A$instance, y) { //│ let tmp, tmp1; @@ -55,7 +55,7 @@ data class A(x) with //│ this.x = x; //│ } //│ getA() { -//│ let tmp; /** scoped **/ +//│ let tmp; //│ tmp = B$(false, this, 2); //│ return tmp.getB() //│ } @@ -74,7 +74,7 @@ class A with g (new A).x() //│ JS (unsanitized): -//│ let A3, tmp1; /** scoped **/ +//│ let A3, tmp1; //│ let g, g$; //│ g$ = function g$(A$instance) { //│ return 2 diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index 358881b02a..eec1c56126 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -47,10 +47,10 @@ fun f(used1, unused1) = foo(used2) + unused2 f(1, 2) //│ JS (unsanitized): -//│ let f3; /** scoped **/ +//│ let f3; //│ let g1, g$3; //│ g$3 = function g$(used1, used2, g_arg) { -//│ let used3; /** scoped **/ +//│ let used3; //│ used3 = 2; //│ return used1 + used2 //│ }; @@ -60,7 +60,7 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let used2, unused2, foo, tmp; /** scoped **/ +//│ let used2, unused2, foo, tmp; //│ let g$here; //│ used2 = unused1; //│ unused2 = 2; @@ -79,10 +79,10 @@ fun f(a1, a2, a3, a4, a5, a6) = g f(1,2,3,4,5,6) //│ JS (unsanitized): -//│ let f4; /** scoped **/ +//│ let f4; //│ let g$4; //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; /** scoped **/ +//│ let tmp, tmp1, tmp2, tmp3; //│ tmp = a1 + a2; //│ tmp1 = tmp + a3; //│ tmp2 = tmp1 + a4; @@ -90,7 +90,7 @@ f(1,2,3,4,5,6) //│ return tmp3 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { -//│ let g2, tmp; /** scoped **/ +//│ let g2, tmp; //│ tmp = g$4(a1, a2, a3, a4, a5, a6); //│ return tmp //│ }; @@ -165,7 +165,7 @@ fun f(unused, immutable, mutated) = a + h() + unused f(1, 2, 1000) //│ JS (unsanitized): -//│ let f7; /** scoped **/ +//│ let f7; //│ let h$2, g$6, f$capture5; //│ g$6 = function g$(immutable, f$capture6) { //│ f$capture6.mutated$capture$0 = 2; @@ -185,7 +185,7 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let g3, h2, a1, tmp7, tmp8; /** scoped **/ +//│ let g3, h2, a1, tmp7, tmp8; //│ let capture; //│ capture = new f$capture5(mutated); //│ a1 = g$6(immutable, capture); @@ -256,7 +256,7 @@ fun g() = f g()(1) //│ JS (unsanitized): -//│ let g6, tmp7; /** scoped **/ +//│ let g6, tmp7; //│ let f14, f$1, h$4, g$capture1; //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; @@ -264,7 +264,7 @@ g()(1) //│ return x1 + g$capture2.y$capture$0 //│ }; //│ f$1 = function f$(g$capture2, x1) { -//│ let h3, k; /** scoped **/ +//│ let h3, k; //│ k = 4; //│ g$capture2.y$capture$0 = 2; //│ return x1 @@ -285,7 +285,7 @@ g()(1) //│ static [definitionMetadata] = ["class", "g$capture"]; //│ }); //│ g6 = function g() { -//│ let y1; /** scoped **/ +//│ let y1; //│ let capture, f$here; //│ capture = new g$capture1(null); //│ capture.y$capture$0 = 0; @@ -377,7 +377,7 @@ fun f(x) = set y = 2 [g, g] //│ JS (unsanitized): -//│ let f23; /** scoped **/ +//│ let f23; //│ let g12, g$14; //│ g$14 = function g$(y1) { //│ return y1 @@ -388,7 +388,7 @@ fun f(x) = //│ } //│ }; //│ f23 = function f(x1) { -//│ let y1, scrut; /** scoped **/ +//│ let y1, scrut; //│ let g$here; //│ y1 = undefined; //│ scrut = x1 < 0; @@ -413,7 +413,7 @@ fun f(x) = set x += 1 [a, g] //│ JS (unsanitized): -//│ let f24; /** scoped **/ +//│ let f24; //│ let g13, g$15, f$capture17; //│ g$15 = function g$(f$capture18) { //│ return f$capture18.x$capture$0 @@ -434,7 +434,7 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let a1, tmp10; /** scoped **/ +//│ let a1, tmp10; //│ let capture, g$here; //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); diff --git a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls index 581a224319..d5bc28e321 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Imports.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Imports.mls @@ -7,7 +7,7 @@ import "../../mlscript-compile/Option.mls" module A with fun f(x) = x is Option.Some //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ globalThis.Object.freeze(class A { //│ static { //│ A1 = this diff --git a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls index d6c1a0d75a..dcf2ad4d80 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Loops.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Loops.mls @@ -44,7 +44,7 @@ fun foo() = set x += 1 return () => x //│ JS (unsanitized): -//│ let foo2; /** scoped **/ +//│ let foo2; //│ let lambda2, lambda$2; //│ lambda$2 = function lambda$(x) { //│ return x @@ -55,11 +55,11 @@ fun foo() = //│ } //│ }); //│ foo2 = function foo() { -//│ let x, tmp2; /** scoped **/ +//│ let x, tmp2; //│ let lambda$here; //│ x = 1; //│ tmp3: while (true) { -//│ let scrut, tmp4; /** scoped **/ +//│ let scrut, tmp4; //│ scrut = true; //│ if (scrut === true) { //│ tmp4 = x + 1; diff --git a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls index 9a33020896..d8b5e7d8c6 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/ModulesObjects.mls @@ -27,7 +27,7 @@ fun foo(y) = (new M).foo() foo(10) //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ let M3, M$; //│ M$ = function M$(isMut, y) { //│ let tmp; @@ -55,7 +55,7 @@ foo(10) //│ toString() { return runtime.render(this); } //│ static [definitionMetadata] = ["class", "M"]; //│ }); -//│ foo1 = function foo(y) { let tmp; /** scoped **/ tmp = M$(false, y); return tmp.foo() }; +//│ foo1 = function foo(y) { let tmp; tmp = M$(false, y); return tmp.foo() }; //│ foo1(10) @@ -110,7 +110,7 @@ fun foo(x, y) = fun foo3 = M.foo2() foo3 //│ JS (unsanitized): -//│ let foo4; /** scoped **/ +//│ let foo4; //│ let M5, foo3$, foo$capture3; //│ foo3$ = function foo3$(M6, x, foo$capture4) { //│ return M6.foo2() @@ -147,7 +147,7 @@ fun foo(x, y) = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo4 = function foo(x, y) { -//│ let foo31, tmp; /** scoped **/ +//│ let foo31, tmp; //│ let M$1, capture; //│ capture = new foo$capture3(y); //│ M$1 = globalThis.Object.freeze(new M5(x, capture)); @@ -214,7 +214,7 @@ module M with val x = if A() is A then 2 else 3 M.A().get //│ JS (unsanitized): -//│ let M17, tmp3; /** scoped **/ +//│ let M17, tmp3; //│ globalThis.Object.freeze(class M16 { //│ static { //│ M17 = this @@ -223,7 +223,7 @@ M.A().get //│ runtime.Unit; //│ } //│ static { -//│ let scrut, tmp4; /** scoped **/ +//│ let scrut, tmp4; //│ this.A = function A() { //│ return globalThis.Object.freeze(new A.class()); //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 15a720556b..0e670441d0 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -12,7 +12,7 @@ fun foo() = xs.push(bar) set x = 2 //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ let bar, bar$, foo$capture1; //│ bar$ = function bar$(foo$capture2) { //│ return foo$capture2.x$capture$0 @@ -33,7 +33,7 @@ fun foo() = //│ static [definitionMetadata] = ["class", "foo$capture"]; //│ }); //│ foo = function foo() { -//│ let x, tmp; /** scoped **/ +//│ let x, tmp; //│ let capture, bar$here; //│ capture = new foo$capture1(null); //│ capture.x$capture$0 = 1; @@ -86,13 +86,9 @@ fun foo() = x bar //│ JS (unsanitized): -//│ let foo3; /** scoped **/ +//│ let foo3; //│ let bar3; -//│ bar3 = function bar() { -//│ let x; /** scoped **/ -//│ x = undefined; -//│ return x -//│ }; +//│ bar3 = function bar() { let x; x = undefined; return x }; //│ foo3 = function foo() { return bar3 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls index 98c99cce02..fe855ab7f8 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/StackSafetyLift.mls @@ -23,7 +23,7 @@ fun hi(n) = else hi(n - 1) hi(0) //│ JS (unsanitized): -//│ let hi; /** scoped **/ +//│ let hi; //│ let Cont$func$hi$1, res, $_stack$_safe$_body$_, doUnwind$, Cont$func$hi$$; //│ Cont$func$hi$$ = function Cont$func$hi$$(isMut, n, pc) { //│ let tmp, tmp1; @@ -63,7 +63,7 @@ hi(0) //│ return res1 //│ }; //│ hi = function hi(n) { -//│ let scrut, tmp; /** scoped **/ +//│ let scrut, tmp; //│ let stackDelayRes; //│ runtime.stackDepth = runtime.stackDepth + 1; //│ stackDelayRes = runtime.checkDepth(); @@ -96,7 +96,7 @@ fun sum(n) = n + sum(n - 1) sum(10000) //│ JS (unsanitized): -//│ let sum1; /** scoped **/ +//│ let sum1; //│ let Cont$func$sum$1, res1, $_stack$_safe$_body$_1, doUnwind$1, Cont$func$sum$$; //│ Cont$func$sum$$ = function Cont$func$sum$$(isMut, n, tmp, pc) { //│ let tmp1, tmp2; @@ -153,7 +153,7 @@ sum(10000) //│ return res2 //│ }; //│ sum1 = function sum(n) { -//│ let scrut, tmp, tmp1; /** scoped **/ +//│ let scrut, tmp, tmp1; //│ let curDepth, stackDelayRes; //│ curDepth = runtime.stackDepth; //│ runtime.stackDepth = runtime.stackDepth + 1; @@ -278,9 +278,9 @@ foo(h) :sjs fun max(a, b) = if a < b then b else a //│ JS (unsanitized): -//│ let max; /** scoped **/ +//│ let max; //│ max = function max(a, b) { -//│ let scrut; /** scoped **/ +//│ let scrut; //│ scrut = a < b; //│ if (scrut === true) { return b } else { return a } //│ }; @@ -292,7 +292,7 @@ fun hi(n) = n hi(0) //│ /!!!\ Option ':stackSafe' requires ':effectHandlers' to be set //│ JS (unsanitized): -//│ let hi1; /** scoped **/ hi1 = function hi(n) { return n }; hi1(0) +//│ let hi1; hi1 = function hi(n) { return n }; hi1(0) //│ = 0 :stackSafe 42 diff --git a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls index f9db8f56b2..3ebc7254e9 100644 --- a/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/objbuf/Mutation.mls @@ -14,7 +14,7 @@ class A(x) with set z += 2 x //│ JS (unsanitized): -//│ let A1; /** scoped **/ +//│ let A1; //│ A1 = function A(x) { //│ return globalThis.Object.freeze(new A.class(x)); //│ }; @@ -42,7 +42,7 @@ class A(x) with //│ } //│ static f(buf, idx) { //│ return (y) => { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ let idx1, idx2, idx3, idx4, idx5; //│ idx1 = idx + 0; //│ tmp = buf.buf.at(idx1) + 1; @@ -61,7 +61,7 @@ class A(x) with //│ get z() { return this.#z; } //│ set z(value) { this.#z = value; } //│ f(y) { -//│ let tmp, tmp1; /** scoped **/ +//│ let tmp, tmp1; //│ tmp = this.#x + 1; //│ this.#x = tmp; //│ tmp1 = this.z + 2; diff --git a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls index 226fd3ed63..41df93f70a 100644 --- a/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls +++ b/hkmc2/shared/src/test/mlscript/parser/PrefixOps.mls @@ -20,7 +20,7 @@ //│ ║ l.16: 1 //│ ╙── ^ //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ tmp2 = + 2; + 3 +//│ let tmp2; tmp2 = + 2; + 3 //│ ╔══[WARNING] Pure expression in statement position //│ ║ l.16: 1 //│ ╙── ^ @@ -31,7 +31,7 @@ + 2 + 3 //│ JS (unsanitized): -//│ let tmp3; /** scoped **/ tmp3 = 1 + 2; tmp3 + 3 +//│ let tmp3; tmp3 = 1 + 2; tmp3 + 3 //│ = 6 1 diff --git a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls index 87eb91c7eb..01db6e89df 100644 --- a/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls +++ b/hkmc2/shared/src/test/mlscript/std/FingerTreeListTest.mls @@ -99,7 +99,7 @@ let xs = FingerTreeList.mk(1, 2, 3) if xs is [a, ...ls] then [a, ls] //│ JS (unsanitized): -//│ let ls, a, middleElements, element0$; /** scoped **/ +//│ let ls, a, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs1) && xs1.length >= 1) { //│ element0$ = runtime.Tuple.get(xs1, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs1, 1, 0)); @@ -133,9 +133,9 @@ fun popByIndex(start, end, acc, lft) = if start >= end then acc else popByIndex(start + 1, end, [...acc, lft.at(start)], lft) //│ JS (unsanitized): -//│ let popByIndex; /** scoped **/ +//│ let popByIndex; //│ popByIndex = function popByIndex(start, end, acc, lft) { -//│ let scrut, tmp34, tmp35, tmp36; /** scoped **/ +//│ let scrut, tmp34, tmp35, tmp36; //│ scrut = start >= end; //│ if (scrut === true) { //│ return acc diff --git a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls index dbb5921435..3883902479 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/future/SymbolicClass.mls @@ -34,7 +34,7 @@ new 1 :: 2 //│ ║ ^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp; /** scoped **/ tmp = globalThis.Object.freeze(new 1()); Cons1(tmp, 2) +//│ let tmp; tmp = globalThis.Object.freeze(new 1()); Cons1(tmp, 2) //│ ═══[RUNTIME ERROR] TypeError: 1 is not a constructor // TODO? @@ -47,7 +47,7 @@ new (1 :: 2) //│ ║ ^^^^^^ //│ ╙── The 'new' keyword requires a statically known class; use the 'new!' operator for dynamic instantiation. //│ JS (unsanitized): -//│ let tmp1; /** scoped **/ tmp1 = Cons1(1, 2); globalThis.Object.freeze(new tmp1()) +//│ let tmp1; tmp1 = Cons1(1, 2); globalThis.Object.freeze(new tmp1()) //│ ═══[RUNTIME ERROR] TypeError: tmp1 is not a constructor new ::(1, 2) diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls index 1bc365e3c9..789353450b 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/JoinPoints.mls @@ -20,7 +20,7 @@ x => if x is [[0]] then 1 //│ JS (unsanitized): //│ let lambda1; //│ lambda1 = (undefined, function (x) { -//│ let element0$, element0$1, tmp; /** scoped **/ +//│ let element0$, element0$1, tmp; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(x) && x.length === 1) { @@ -57,9 +57,9 @@ fun crazy(v) = S(S(S(S(S(S(0)))))) then "bruh!" _ then S(S(S(S(S(S(0)))))) //│ JS (unsanitized): -//│ let crazy; /** scoped **/ +//│ let crazy; //│ crazy = function crazy(v) { -//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; /** scoped **/ +//│ let argument0$, argument0$1, argument0$2, argument0$3, argument0$4, argument0$5, tmp, tmp1, tmp2, tmp3, tmp4, tmp5; //│ split_root$: { //│ split_1$: { //│ if (v instanceof S1.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls index 5fdf8ed31f..7907a829f7 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/general/LogicalConnectives.mls @@ -25,7 +25,7 @@ fun test(x) = :sjs true and test(42) //│ JS (unsanitized): -//│ let scrut6, scrut7, tmp4; /** scoped **/ +//│ let scrut6, scrut7, tmp4; //│ split_root$3: { //│ split_1$3: { //│ scrut6 = true; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls index f08820694b..4bba48a888 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/Deduplication.mls @@ -10,22 +10,19 @@ let z = 0 :sjs if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut; /** scoped **/ scrut = x === 0; if (scrut === true) { 1 } else { 2 } +//│ let scrut; scrut = x === 0; if (scrut === true) { 1 } else { 2 } //│ = 1 :sjs let a = if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let a, scrut1, tmp; /** scoped **/ -//│ scrut1 = x === 0; -//│ if (scrut1 === true) { tmp = 1; } else { tmp = 2; } -//│ a = tmp; +//│ let a, scrut1, tmp; scrut1 = x === 0; if (scrut1 === true) { tmp = 1; } else { tmp = 2; } a = tmp; //│ a = 1 :sjs print of if x === 0 then 1 else 2 //│ JS (unsanitized): -//│ let scrut2, tmp1; /** scoped **/ +//│ let scrut2, tmp1; //│ scrut2 = x === 0; //│ if (scrut2 === true) { tmp1 = 1; } else { tmp1 = 2; } //│ Predef.print(tmp1) @@ -41,7 +38,7 @@ if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ split_root$: { //│ split_1$: { //│ if (x === 0) { @@ -81,7 +78,7 @@ let qqq = if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let qqq, tmp3; /** scoped **/ +//│ let qqq, tmp3; //│ split_root$1: { //│ split_1$1: { //│ if (x === 0) { @@ -121,7 +118,7 @@ print of if x is 1 then "1" else "" //│ JS (unsanitized): -//│ let tmp4; /** scoped **/ +//│ let tmp4; //│ split_root$2: { //│ split_1$2: { //│ if (x === 0) { @@ -162,9 +159,9 @@ fun foo(x, y, z) = 1 then "1" else "" //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(x1, y1, z1) { -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ split_root$3: { //│ split_1$3: { //│ if (x1 === 0) { @@ -208,7 +205,7 @@ print of if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let tmp5; /** scoped **/ +//│ let tmp5; //│ split_root$3: { //│ split_1$3: { //│ split_2$: { @@ -270,9 +267,9 @@ fun foo(x, y, z) = if y is 2 then "_2_" else "___" //│ JS (unsanitized): -//│ let foo1; /** scoped **/ +//│ let foo1; //│ foo1 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ split_root$4: { //│ split_1$4: { //│ split_2$1: { @@ -337,9 +334,9 @@ fun foo(x, y, z) = if x is let value = "hello" expensive_call(value) //│ JS (unsanitized): -//│ let foo2; /** scoped **/ +//│ let foo2; //│ foo2 = function foo(x1, y1, z1) { -//│ let value, tmp6; /** scoped **/ +//│ let value, tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 === 0) { @@ -380,7 +377,7 @@ fun foo(x, y, z) = if x is A then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo3; /** scoped **/ +//│ let foo3; //│ foo3 = function foo(x1, y1, z1) { //│ if (x1 instanceof A1.class) { return "Hello" } else { return "Goodbye" } //│ }; @@ -401,9 +398,9 @@ fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" else "Goodbye" //│ JS (unsanitized): -//│ let foo4; /** scoped **/ +//│ let foo4; //│ foo4 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ split_root$4: { //│ split_1$4: { //│ if (x1 instanceof A1.class) { @@ -427,9 +424,9 @@ fun foo(x, y, z) = fun foo(x, y, z) = if x is A and y is B and z is C then "Hello" //│ JS (unsanitized): -//│ let foo5; /** scoped **/ +//│ let foo5; //│ foo5 = function foo(x1, y1, z1) { -//│ let tmp6; /** scoped **/ +//│ let tmp6; //│ split_root$4: { //│ split_default$: { //│ if (x1 instanceof A1.class) { @@ -467,7 +464,7 @@ foo(A, B, B) :sjs let y = if true then 1 else 2 //│ JS (unsanitized): -//│ let y1, scrut3, tmp6; /** scoped **/ +//│ let y1, scrut3, tmp6; //│ scrut3 = true; //│ if (scrut3 === true) { tmp6 = 1; } else { tmp6 = 2; } //│ y1 = tmp6; diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls index db15ad49ed..18fb0d1700 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/ExcessiveDeduplication.mls @@ -17,7 +17,7 @@ if x then 0 y then 0 //│ JS (unsanitized): -//│ let tmp; /** scoped **/ +//│ let tmp; //│ split_root$: { //│ split_1$: { //│ if (x === true) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls index cfb98a2376..7a70cb4e1f 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/normalization/SimplePairMatches.mls @@ -11,7 +11,7 @@ x => if x is Pair(A, B) then 1 //│ JS (unsanitized): //│ let lambda; //│ lambda = (undefined, function (x) { -//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ let argument0$, argument1$, tmp; //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair1.class) { @@ -45,9 +45,9 @@ fun f(x) = if x is Pair(A, A) then 1 Pair(B, B) then 2 //│ JS (unsanitized): -//│ let f; /** scoped **/ +//│ let f; //│ f = function f(x) { -//│ let argument0$, argument1$, tmp; /** scoped **/ +//│ let argument0$, argument1$, tmp; //│ split_root$: { //│ split_default$: { //│ if (x instanceof Pair1.class) { diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls index 45662130ff..ae076f1267 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/ConjunctionPattern.mls @@ -36,9 +36,9 @@ fun foo(v) = A & B then 1 else 0 //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(v) { -//│ let tmp2; /** scoped **/ +//│ let tmp2; //│ split_root$2: { //│ split_1$2: { //│ if (v instanceof A1) { break split_1$2 } else { break split_1$2 } diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 408479468b..33339b6e3a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -6,9 +6,9 @@ fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ JS (unsanitized): -//│ let nonsense; /** scoped **/ +//│ let nonsense; //│ nonsense = function nonsense(xs) { -//│ let ys, middleElements; /** scoped **/ +//│ let ys, middleElements; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 0) { //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 0, 0)); //│ ys = middleElements; @@ -27,9 +27,9 @@ fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ JS (unsanitized): -//│ let lead_and_last; /** scoped **/ +//│ let lead_and_last; //│ lead_and_last = function lead_and_last(xs) { -//│ let x, ys, y, lastElement0$, middleElements, element0$; /** scoped **/ +//│ let x, ys, y, lastElement0$, middleElements, element0$; //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { //│ element0$ = runtime.Tuple.get(xs, 0); //│ middleElements = runtime.safeCall(runtime.Tuple.slice(xs, 1, 1)); @@ -61,9 +61,9 @@ fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 //│ JS (unsanitized): -//│ let nested_tuple_patterns; /** scoped **/ +//│ let nested_tuple_patterns; //│ nested_tuple_patterns = function nested_tuple_patterns(xs) { -//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; /** scoped **/ +//│ let x, y, w, z, lastElement0$, middleElements, element0$, element1$, element0$1, tmp6, tmp7, tmp8; //│ split_root$: { //│ split_default$: { //│ if (runtime.Tuple.isArrayLike(xs) && xs.length >= 2) { diff --git a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls index 3d8b177c07..f385992ea7 100644 --- a/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls +++ b/hkmc2/shared/src/test/mlscript/ups/MatchResult.mls @@ -33,9 +33,9 @@ Cross.unapply("0") :sjs fun foo(x) = x is Cross //│ JS (unsanitized): -//│ let foo; /** scoped **/ +//│ let foo; //│ foo = function foo(x2) { -//│ let unapplyResult, output, bindings; /** scoped **/ +//│ let unapplyResult, output, bindings; //│ unapplyResult = runtime.safeCall(Cross1.unapply(x2)); //│ if (unapplyResult instanceof runtime.MatchSuccess.class) { //│ output = unapplyResult.output; diff --git a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls index c1486578fd..c851a7bf7f 100644 --- a/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls +++ b/hkmc2/shared/src/test/mlscript/ups/SimpleTransform.mls @@ -11,14 +11,14 @@ data class Pair[A, B](first: A, second: B) :sjs pattern SumPair = Pair(a, b) => a + b //│ JS (unsanitized): -//│ let SumPair1; /** scoped **/ +//│ let SumPair1; //│ globalThis.Object.freeze(class SumPair { //│ static { //│ SumPair1 = globalThis.Object.freeze(new this) //│ } //│ constructor() {} //│ unapply(input) { -//│ let transform, argument0$, argument1$, transformResult, tmp; /** scoped **/ +//│ let transform, argument0$, argument1$, transformResult, tmp; //│ let lambda; //│ lambda = (undefined, function (a, b) { //│ return a + b From 7dc143d3648848eb6ad82522f3d6a3e4495b5d2e Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Fri, 5 Dec 2025 19:32:48 +0800 Subject: [PATCH 61/72] fixes - no declared vars in `Scoped` blocks - pCtor also gets a `Scoped` block, which is merged with ctor's `Scoped` block in jsbuilder because in the js backend, parent ctor and ctor are in the same js block - fix scoped blocks for lambdas for shortcut boolean ops --- .../main/scala/hkmc2/codegen/Lowering.scala | 11 +++++-- .../scala/hkmc2/codegen/js/JSBuilder.scala | 5 +-- .../src/test/mlscript-compile/Runtime.mjs | 3 +- .../src/test/mlscript/basics/Declare.mls | 2 +- .../src/test/mlscript/basics/Inheritance.mls | 9 ++--- .../src/test/mlscript/codegen/ConsoleLog.mls | 2 +- .../test/mlscript/codegen/ScopedBlocks.mls | 33 +++++++++++++++++++ .../test/mlscript/ctx/MissingDefinitions2.mls | 3 +- 8 files changed, 55 insertions(+), 13 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index cddbec09d6..cbe9b568cd 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -179,7 +179,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): d match case td: TermDefinition => reportAnnotations(td, td.extraAnnotations) - if td.owner.isEmpty then loweringCtx.collectScopedSym(td.sym) + if td.owner.isEmpty && td.hasDeclareModifier.isEmpty then + loweringCtx.collectScopedSym(td.sym) td.body match case N => // abstract declarations have no lowering blockImpl(stats, res)(k) @@ -304,7 +305,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): case S(ext) => assert(k isnt syntax.Mod) // modules can't extend things and can't have super calls subTerm(ext.cls): clsp => - val pctor = parentConstructor(ext.cls, ext.args) + val pctor = inScopedBlock(parentConstructor(ext.cls, ext.args)) Define( ClsLikeDefn( defn.owner, defn.sym, defn.bsym, defn.kind, defn.paramsOpt, defn.auxParams, S(clsp), @@ -483,7 +484,11 @@ class Lowering()(using Config, TL, Raise, State, Ctx): if isAnd || isOr then val lamSym = BlockMemberSymbol("lambda", Nil, false) loweringCtx.collectScopedSym(lamSym) - val lamDef = FunDefn.withFreshSymbol(N, lamSym, PlainParamList(Nil) :: Nil, returnedTerm(arg2))(isTailRec = false) + val lamDef = FunDefn.withFreshSymbol( + N, + lamSym, + PlainParamList(Nil) :: Nil, + inScopedBlock(returnedTerm(arg2)))(isTailRec = false) Define( lamDef, k(Call( diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 9b6b972a2c..1d1d56a4c0 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -303,8 +303,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: val privs = mkPrivs(pubFlds, privFlds, mtdPrefix, isym) - val preCtorCode = body(preCtor, true) - val ctorCode = doc"$preCtorCode${body(ctor, endSemi = true)}${ + // when both `preCtor` and `ctor` are `Scoped` blocks + // the `Begin` constructor can merge them + val ctorCode = doc"${body(Begin(preCtor, ctor), endSemi = true)}${ kind match case syntax.Obj => doc" # ${defineProperty(doc"this", "class", doc"${scope.lookup_!(isym, isym.toLoc)}")}" diff --git a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs index fd84b4265b..705ac36640 100644 --- a/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs +++ b/hkmc2/shared/src/test/mlscript-compile/Runtime.mjs @@ -427,9 +427,10 @@ globalThis.Object.freeze(class Runtime { throw globalThis.Error("unreachable"); } static checkArgs(functionName, expected, isUB, got) { - let scrut, name, scrut1, scrut2, tmp, lambda, lambda1, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; + let scrut, name, scrut1, scrut2, tmp, lambda, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12; tmp = got < expected; lambda = (undefined, function () { + let lambda1; lambda1 = (undefined, function () { return got > expected }); diff --git a/hkmc2/shared/src/test/mlscript/basics/Declare.mls b/hkmc2/shared/src/test/mlscript/basics/Declare.mls index 0a8b33ff10..d0425820bb 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Declare.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Declare.mls @@ -4,7 +4,7 @@ :sjs declare fun foo: Int //│ JS (unsanitized): -//│ let foo; +//│ :re :sjs diff --git a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls index e9e16402c8..db082741cb 100644 --- a/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls +++ b/hkmc2/shared/src/test/mlscript/basics/Inheritance.mls @@ -51,7 +51,7 @@ b is String data class Baz(z) extends Bar(z * 1) with fun baz = this.bar * 2 //│ JS (unsanitized): -//│ let Baz1, tmp; +//│ let Baz1; //│ Baz1 = function Baz(z) { //│ return globalThis.Object.freeze(new Baz.class(z)); //│ }; @@ -60,6 +60,7 @@ data class Baz(z) extends Bar(z * 1) with //│ Baz1.class = this //│ } //│ constructor(z) { +//│ let tmp; //│ tmp = z * 1; //│ super(tmp); //│ this.z = z; @@ -99,8 +100,8 @@ data class Barr(x: Int)(y: Int) with data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with fun g = z //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.99: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with -//│ ╙── ^^^^^^^^^^^^^^^^^^^ +//│ ║ l.100: data class Bazz(z: Int) extends Bar(z % 10)(z / 10) with +//│ ╙── ^^^^^^^^^^^^^^^^^^^ :todo Bazz(42).f @@ -110,7 +111,7 @@ Bazz(42).f new Barr(40)(2) with fun f = 43 //│ ╔══[COMPILATION ERROR] Extending a class with multiple parameter lists is not supported -//│ ║ l.110: new Barr(40)(2) with +//│ ║ l.111: new Barr(40)(2) with //│ ╙── ^^^^^^^^^^^ //│ = fun diff --git a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls index 4aa0be8931..be5f1aee43 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ConsoleLog.mls @@ -7,7 +7,7 @@ :silent declare val console //│ JS (unsanitized): -//│ let console; +//│ console.log("a") diff --git a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls index 038ccfc0bf..731e2881b2 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/ScopedBlocks.mls @@ -425,3 +425,36 @@ fun f(x) = //│ tmp4 = tmp3 !== runtime.LoopEnd; //│ if (tmp4 === true) { return tmp3 } else { return tmp2 } //│ }; + + + +data class Bar(x) + +:sjs +data class Baz(z) extends Bar(z * 1) with + val k = 1 + 2 + 3 + fun baz = this.bar * 2 +//│ JS (unsanitized): +//│ let Baz1; +//│ Baz1 = function Baz(z) { +//│ return globalThis.Object.freeze(new Baz.class(z)); +//│ }; +//│ globalThis.Object.freeze(class Baz extends Bar1.class { +//│ static { +//│ Baz1.class = this +//│ } +//│ constructor(z) { +//│ let tmp2, tmp3, tmp4; +//│ tmp4 = z * 1; +//│ super(tmp4); +//│ this.z = z; +//│ tmp2 = 1 + 2; +//│ tmp3 = tmp2 + 3; +//│ this.k = tmp3; +//│ } +//│ get baz() { +//│ return this.bar * 2; +//│ } +//│ toString() { return runtime.render(this); } +//│ static [definitionMetadata] = ["class", "Baz", ["z"]]; +//│ }); diff --git a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls index 482d1d6532..9ac3e9192e 100644 --- a/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls +++ b/hkmc2/shared/src/test/mlscript/ctx/MissingDefinitions2.mls @@ -16,13 +16,14 @@ x val x - +// with Scoped blocks, `x` is declared // :ge x val x: Int +// with Scoped blocks, `x` is declared // :ge x From 0227b70ff54e69a7a55220fd7e9a939dcfaabfd9 Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Sat, 6 Dec 2025 00:45:57 +0800 Subject: [PATCH 62/72] Make some improvements --- .../shared/src/main/scala/hkmc2/Config.scala | 8 +- .../src/main/scala/hkmc2/codegen/Block.scala | 10 +- .../main/scala/hkmc2/codegen/Lowering.scala | 103 +++++++++--------- .../scala/hkmc2/codegen/js/JSBuilder.scala | 14 +-- .../test/mlscript/codegen/DelayedLetInit.mls | 6 +- .../src/test/mlscript/lifter/FunInFun.mls | 69 ++++++++---- .../src/test/mlscript/lifter/Mutation.mls | 31 +++++- .../src/test/mlscript/llir/BasicCpp.mls | 2 +- 8 files changed, 142 insertions(+), 101 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/Config.scala b/hkmc2/shared/src/main/scala/hkmc2/Config.scala index 109a40726d..b580105872 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/Config.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/Config.scala @@ -27,9 +27,11 @@ case class Config( def stackSafety: Opt[StackSafety] = effectHandlers.flatMap(_.stackSafety) - // NOTE: with `Scoped` blocks inside loop bodies, currently this flag - // forces the rewriting of while loops when handler lowering is on to make sure that - // we do create programs with unbound variables + // NOTE: We force the rewriting of while loops to functions when handler lowering is on + // to prevent the "floating out" of definitions done by handler lowering, + // which currently does not respect scopes introduced by `Scoped` blocks. + // Currently, this is only a problem with while loops because we do not yet + // construct nested Scoped blocks in other places (but will in the future). // see https://github.com/hkust-taco/mlscript/pull/356#discussion_r2579529893 // and https://github.com/hkust-taco/mlscript/pull/356#discussion_r2585183902 def shouldRewriteWhile: Bool = diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index a21a6980b7..3ecbedca97 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -29,10 +29,10 @@ sealed abstract class Block extends Product: case _: End => true case _ => false - // this variation of `definedVars` excludes the `syms` in `Scoped` blocks - // so that `Scoped` blocks' vars can be seen in the output js code; - // otherwise `blockPreamble` allocates all the `definedVars` such that - // we cannot see and test vars generated because of the existence of the `Scoped` blocks + /** This variation of `definedVars` excludes the `syms` in `Scoped` blocks + * so that `Scoped` blocks' vars can be seen in the output js code; + * otherwise `blockPreamble` allocates all the `definedVars` such that + * we cannot see and test vars generated because of the existence of the `Scoped` blocks */ lazy val definedVarsNoScoped: Set[Local] = this match case _: Return | _: Throw => Set.empty case Begin(sub, rst) => sub.definedVarsNoScoped ++ rst.definedVarsNoScoped @@ -54,6 +54,8 @@ sealed abstract class Block extends Product: case Label(lbl, _, bod, rst) => bod.definedVarsNoScoped ++ rst.definedVarsNoScoped case Scoped(syms, body) => body.definedVarsNoScoped -- syms + // * Note: there is a good chance that historical users of `definedVars` do not properly respect Scoped blocks + // * and should adapt their logic to use `definedVarsNoScoped` instead. lazy val definedVars: Set[Local] = this match case _: Return | _: Throw => Set.empty case Begin(sub, rst) => sub.definedVars ++ rst.definedVars diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index cbe9b568cd..8a6a577f2c 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -158,14 +158,10 @@ class Lowering()(using Config, TL, Raise, State, Ctx): subTerm(lhs): l => subTerm_nonTail(rhs): r => blockImpl(stats, L((mut, RcdArg(S(l), r) :: flds)))(k) - case (decl @ LetDecl(sym, annotations)) :: (stats @ ((_: DefineVar) :: _)) => - reportAnnotations(decl, annotations) - if sym.asTrm.forall(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) - blockImpl(stats, res)(k) case (decl @ LetDecl(sym, annotations)) :: stats => reportAnnotations(decl, annotations) if sym.asTrm.forall(_.owner.isEmpty) then loweringCtx.collectScopedSym(sym) - blockImpl(DefineVar(sym, Term.Lit(Tree.UnitLit(false))) :: stats, res)(k) + blockImpl(stats, res)(k) case DefineVar(sym, rhs) :: stats => term(rhs): r => Assign(sym, r, blockImpl(stats, res)(k)) @@ -1172,55 +1168,54 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) case h :: t => go(t, Term.Lam(h, bod)) go(paramLists.reverse, bod) - def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = - inScopedBlock: - val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") - val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") - val resSym = TempSymbol(N, dbgNme = "traceLogRes") - val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg") - val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) - val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") - - loweringCtx.collectScopedSym( - enterMsgSym, - prevIndentLvlSym, - resSym, - retMsgSym, - resInspectedSym) - for (s, _) <- psInspectedSyms do loweringCtx.collectScopedSym(s) - - val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): - case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 - then Arg(N, Value.Ref(s)) :: acc - else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc - - val tmp1, tmp2, tmp3 = TempSymbol(N) - loweringCtx.collectScopedSym(tmp1, tmp2, tmp3) - - assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => - pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) - *) |>: - assignStmts( - enterMsgSym -> pureCall( - strConcatFn, - Arg(N, Value.Lit(Tree.StrLit(s"CALL ${name.getOrElse("[arrow function]")}("))) :: psSymArgs - ), - tmp1 -> pureCall(traceLogFn, Arg(N, Value.Ref(enterMsgSym)) :: Nil), - prevIndentLvlSym -> pureCall(traceLogIndentFn, Nil) - ) |>: - term(bod)(r => - assignStmts( - resSym -> r, - resInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(resSym)) :: Nil), - retMsgSym -> pureCall( - strConcatFn, - Arg(N, Value.Lit(Tree.StrLit("=> "))) :: Arg(N, Value.Ref(resInspectedSym)) :: Nil - ), - tmp2 -> pureCall(traceLogResetFn, Arg(N, Value.Ref(prevIndentLvlSym)) :: Nil), - tmp3 -> pureCall(traceLogFn, Arg(N, Value.Ref(retMsgSym)) :: Nil) - ) |>: - Ret(Value.Ref(resSym)) - ) + def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using LoweringCtx): Block = inScopedBlock: + val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg") + val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent") + val resSym = TempSymbol(N, dbgNme = "traceLogRes") + val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg") + val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) + val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") + + loweringCtx.collectScopedSym( + enterMsgSym, + prevIndentLvlSym, + resSym, + retMsgSym, + resInspectedSym) + for (s, _) <- psInspectedSyms do loweringCtx.collectScopedSym(s) + + val psSymArgs = psInspectedSyms.zipWithIndex.foldRight[Ls[Arg]](Arg(N, Value.Lit(Tree.StrLit(")"))) :: Nil): + case (((s, p), i), acc) => if i == psInspectedSyms.length - 1 + then Arg(N, Value.Ref(s)) :: acc + else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc + + val tmp1, tmp2, tmp3 = TempSymbol(N) + loweringCtx.collectScopedSym(tmp1, tmp2, tmp3) + + assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => + pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) + *) |>: + assignStmts( + enterMsgSym -> pureCall( + strConcatFn, + Arg(N, Value.Lit(Tree.StrLit(s"CALL ${name.getOrElse("[arrow function]")}("))) :: psSymArgs + ), + tmp1 -> pureCall(traceLogFn, Arg(N, Value.Ref(enterMsgSym)) :: Nil), + prevIndentLvlSym -> pureCall(traceLogIndentFn, Nil) + ) |>: + term(bod)(r => + assignStmts( + resSym -> r, + resInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(resSym)) :: Nil), + retMsgSym -> pureCall( + strConcatFn, + Arg(N, Value.Lit(Tree.StrLit("=> "))) :: Arg(N, Value.Ref(resInspectedSym)) :: Nil + ), + tmp2 -> pureCall(traceLogResetFn, Arg(N, Value.Ref(prevIndentLvlSym)) :: Nil), + tmp3 -> pureCall(traceLogFn, Arg(N, Value.Ref(retMsgSym)) :: Nil) + ) |>: + Ret(Value.Ref(resSym)) + ) object TrivialStatementsAndMatch: diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 1d1d56a4c0..c9d6b50d3a 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -514,7 +514,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case Scoped(syms, body) => scope.nest.givenIn: val vars = syms.toArray.sortBy(_.uid).iterator.flatMap: l => - if scope.lookup(l).isDefined then + if scope.lookup(l).isDefined then // FIXME: this logic is incorrect and should eventually be removed // NOTE: this warning is turned off because the lifter is not // yet updated to maintian the Scoped blocks, so when // something is lifted out, its symbol may be already declared in an outer level, @@ -591,9 +591,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: then "./" + os.Path(path).relativeTo(wd).toString else path doc"""import ${getVar(i._1, N)} from "${relPath}";""" - imps.mkDocument(doc" # ") :/: - nonNestedScoped(p.main)(block(_, endSemi = false)).stripBreaks :: - locally: + imps.mkDocument(doc" # ") + :/: nonNestedScoped(p.main)(block(_, endSemi = false)).stripBreaks + :: locally: exprt match case S(sym) => doc"\nlet ${sym.nme} = ${scope.lookup_!(sym, sym.toLoc)}; export default ${sym.nme};\n" case N => doc"" @@ -606,9 +606,9 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case Scoped(syms, body) => blockPreamble(p.imports.map(_._1).toSeq ++ syms.toSeq) -> (imps.mkDocument(doc" # ") :/: block(body, endSemi = false).stripBreaks) - case _ => - blockPreamble(p.imports.map(_._1).toSeq ++ p.main.definedVarsNoScoped.toSeq) -> - (imps.mkDocument(doc" # ") :/: returningTerm(p.main, endSemi = false).stripBreaks) + case body => + blockPreamble(p.imports.map(_._1).toSeq ++ body.definedVarsNoScoped.toSeq) -> + (imps.mkDocument(doc" # ") :/: returningTerm(body, endSemi = false).stripBreaks) def genLetDecls(vars: Iterator[(Symbol, Str)], isScoped: Bool): Document = if vars.isEmpty then doc"" else diff --git a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls index 9d733318be..1c42627d83 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/DelayedLetInit.mls @@ -6,7 +6,7 @@ let x //│ JS (unsanitized): -//│ let x; x = undefined; +//│ let x; //│ x = undefined x = 1 @@ -81,7 +81,6 @@ else foo = 1 //│ ╙── ^ //│ JS (unsanitized): //│ let foo1, scrut; -//│ foo1 = undefined; //│ scrut = true; //│ if (scrut === true) { //│ foo1 @@ -96,7 +95,6 @@ else foo = 1 //│ JS (unsanitized): //│ let foo2, scrut1; -//│ foo2 = undefined; //│ scrut1 = true; //│ if (scrut1 === true) { foo2 = 0; runtime.Unit } else { foo2 = 1; runtime.Unit } //│ foo = 0 @@ -120,7 +118,7 @@ foo :fixme fun f() = foo = 0 //│ ╔══[PARSE ERROR] Expected end of input; found '=' keyword instead -//│ ║ l.121: fun f() = foo = 0 +//│ ║ l.119: fun f() = foo = 0 //│ ╙── ^ //│ JS (unsanitized): //│ let f4; f4 = function f() { return foo2 }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls index eec1c56126..0fc05c2627 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/FunInFun.mls @@ -1,6 +1,28 @@ :lift :js + +:fixme +fun foo = + let x + fun bar = x +//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' +//│ ║ l.8: fun bar = x +//│ ║ ^ +//│ ╟── which references the symbol introduced here +//│ ║ l.7: let x +//│ ╙── ^ + +:expect 12 +fun foo = + let x + fun bar = x + x = 12 + bar +foo +//│ = 12 + + :expect 5 fun f(used1, unused1) = let used2 = unused1 @@ -60,14 +82,14 @@ f(1, 2) //│ } //│ }; //│ f3 = function f(used1, unused1) { -//│ let used2, unused2, foo, tmp; +//│ let used2, unused2, foo2, tmp1; //│ let g$here; //│ used2 = unused1; //│ unused2 = 2; //│ g$here = runtime.safeCall(g1(used1, used2)); -//│ foo = g$here; -//│ tmp = runtime.safeCall(foo(used2)); -//│ return tmp + unused2 +//│ foo2 = g$here; +//│ tmp1 = runtime.safeCall(foo2(used2)); +//│ return tmp1 + unused2 //│ }; //│ f3(1, 2) //│ = 5 @@ -82,17 +104,17 @@ f(1,2,3,4,5,6) //│ let f4; //│ let g$4; //│ g$4 = function g$(a1, a2, a3, a4, a5, a6) { -//│ let tmp, tmp1, tmp2, tmp3; -//│ tmp = a1 + a2; -//│ tmp1 = tmp + a3; -//│ tmp2 = tmp1 + a4; -//│ tmp3 = tmp2 + a5; -//│ return tmp3 + a6 +//│ let tmp1, tmp2, tmp3, tmp4; +//│ tmp1 = a1 + a2; +//│ tmp2 = tmp1 + a3; +//│ tmp3 = tmp2 + a4; +//│ tmp4 = tmp3 + a5; +//│ return tmp4 + a6 //│ }; //│ f4 = function f(a1, a2, a3, a4, a5, a6) { -//│ let g2, tmp; -//│ tmp = g$4(a1, a2, a3, a4, a5, a6); -//│ return tmp +//│ let g2, tmp1; +//│ tmp1 = g$4(a1, a2, a3, a4, a5, a6); +//│ return tmp1 //│ }; //│ f4(1, 2, 3, 4, 5, 6) //│ = 21 @@ -185,13 +207,13 @@ f(1, 2, 1000) //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f7 = function f(unused, immutable, mutated) { -//│ let g3, h2, a1, tmp7, tmp8; +//│ let g3, h2, a1, tmp8, tmp9; //│ let capture; //│ capture = new f$capture5(mutated); //│ a1 = g$6(immutable, capture); -//│ tmp7 = h$2(capture); -//│ tmp8 = a1 + tmp7; -//│ return tmp8 + unused +//│ tmp8 = h$2(capture); +//│ tmp9 = a1 + tmp8; +//│ return tmp9 + unused //│ }; //│ f7(1, 2, 1000) //│ = 7 @@ -256,7 +278,7 @@ fun g() = f g()(1) //│ JS (unsanitized): -//│ let g6, tmp7; +//│ let g6, tmp8; //│ let f14, f$1, h$4, g$capture1; //│ h$4 = function h$(x1, k, g$capture2) { //│ k = 5; @@ -292,8 +314,8 @@ g()(1) //│ f$here = runtime.safeCall(f14(capture)); //│ return f$here //│ }; -//│ tmp7 = g6(); -//│ runtime.safeCall(tmp7(1)) +//│ tmp8 = g6(); +//│ runtime.safeCall(tmp8(1)) //│ = 1 :expect 2 @@ -390,7 +412,6 @@ fun f(x) = //│ f23 = function f(x1) { //│ let y1, scrut; //│ let g$here; -//│ y1 = undefined; //│ scrut = x1 < 0; //│ if (scrut === true) { //│ y1 = 1; @@ -434,13 +455,13 @@ fun f(x) = //│ static [definitionMetadata] = ["class", "f$capture"]; //│ }); //│ f24 = function f(x1) { -//│ let a1, tmp10; +//│ let a1, tmp11; //│ let capture, g$here; //│ capture = new f$capture17(x1); //│ g$here = runtime.safeCall(g13(capture)); //│ a1 = g$here; -//│ tmp10 = capture.x$capture$0 + 1; -//│ capture.x$capture$0 = tmp10; +//│ tmp11 = capture.x$capture$0 + 1; +//│ capture.x$capture$0 = tmp11; //│ g$here = runtime.safeCall(g13(capture)); //│ return globalThis.Object.freeze([ a1, g$here ]) //│ }; diff --git a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls index 0e670441d0..811016166e 100644 --- a/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls +++ b/hkmc2/shared/src/test/mlscript/lifter/Mutation.mls @@ -65,11 +65,34 @@ xs.0() //│ = 2 +:fixme fun foo() = let x fun bar() = x = 2 [bar, () => x] +//│ ╔══[COMPILATION ERROR] No definition found in scope for member 'x' +//│ ║ l.73: [bar, () => x] +//│ ║ ^ +//│ ╟── which references the symbol introduced here +//│ ║ l.70: let x +//│ ╙── ^ + +:fixme +:expect 2 +let b_x = foo() +b_x.0() +b_x.1() +//│ ═══[RUNTIME ERROR] ReferenceError: x is not defined +//│ ═══[RUNTIME ERROR] Expected: '2', got: 'undefined' +//│ b_x = [fun bar, fun] + + +fun foo() = + let x = undefined + fun bar() = + x = 2 + [bar, () => x] :expect 2 let b_x = foo() @@ -86,9 +109,9 @@ fun foo() = x bar //│ JS (unsanitized): -//│ let foo3; -//│ let bar3; -//│ bar3 = function bar() { let x; x = undefined; return x }; -//│ foo3 = function foo() { return bar3 }; +//│ let foo4; +//│ let bar4; +//│ bar4 = function bar() { let x; return x }; +//│ foo4 = function foo() { return bar4 }; diff --git a/hkmc2/shared/src/test/mlscript/llir/BasicCpp.mls b/hkmc2/shared/src/test/mlscript/llir/BasicCpp.mls index 50b35ad37e..629dad4605 100644 --- a/hkmc2/shared/src/test/mlscript/llir/BasicCpp.mls +++ b/hkmc2/shared/src/test/mlscript/llir/BasicCpp.mls @@ -3,7 +3,7 @@ :cpp fun foo(a) = - let x + let x = undefined if a > 0 do x = 1 x + 1 From 5d992277b7794ccade32efec29d22d9b5365d50a Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Sat, 6 Dec 2025 10:18:25 +0800 Subject: [PATCH 63/72] Minor fix --- .../main/scala/hkmc2/codegen/Lowering.scala | 15 +-------------- .../scala/hkmc2/codegen/js/JSBuilder.scala | 10 ++++------ .../test/mlscript/codegen/NestedScoped.mls | 19 +++++++++++++------ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 8a6a577f2c..79df5ca305 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -548,20 +548,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): conclude(Value.Ref(ctx.builtins.debug.getLocals, N).withLocOf(f)) case t if instantiatedResolvedBms.exists(_ is ctx.builtins.scope.locally) => arg match - case Tup(flds) => - val body = (flds.foldRight[Term](UnitVal()) { - case (Fld(_, t, _), UnitVal()) => Blk(Nil, t) - case (Fld(_, t, _), Blk(stmts, res)) => Blk(t :: stmts, res) - case (_, _: UnitVal | Blk) => - ErrorReport( - msg"Unsupported form for scope.locally." -> - t.toLoc :: Nil, - source = Diagnostic.Source.Compilation) - Error - case _ => ??? // impossible - }) match - case Blk(stmts, res) => Blk(stmts.reverse, res) - case t => t + case Tup(Fld(_, body, _) :: Nil) => LoweringCtx.nestScoped.givenIn: val res = block(Nil, R(body))(k) val scopedSyms = loweringCtx.getCollectedSym diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index c9d6b50d3a..c33bd2fe38 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -526,7 +526,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: else Some(l -> scope.allocateName(l)) braced: - genLetDecls(vars, true) :: returningTerm(body, endSemi) + genLetDecls(vars) :: returningTerm(body, endSemi) // case _ => ??? @@ -610,7 +610,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: blockPreamble(p.imports.map(_._1).toSeq ++ body.definedVarsNoScoped.toSeq) -> (imps.mkDocument(doc" # ") :/: returningTerm(body, endSemi = false).stripBreaks) - def genLetDecls(vars: Iterator[(Symbol, Str)], isScoped: Bool): Document = + def genLetDecls(vars: Iterator[(Symbol, Str)]): Document = if vars.isEmpty then doc"" else doc" # let " :: vars.map: (_, nme) => nme @@ -621,14 +621,12 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: // TODO document: mutable var assnts require the lookup val vars = ss.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) - genLetDecls(vars, false) + genLetDecls(vars) // Only handle non-nested Scoped nodes: we output the bindings, but do not add another brace def nonNestedScoped(blk: Block)(k: Block => Document)(using Raise, Scope): Document = blk match case Scoped(syms, body) => - val vars = syms.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => - l -> scope.allocateName(l)) - genLetDecls(vars, true) :: k(body) + blockPreamble(syms) :: k(body) case _ => k(blk) diff --git a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls index 25562c62e6..e96149c98d 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls @@ -244,19 +244,26 @@ fun foo(x, y) = //│ }; -// does nothing +:ge scope.locally() +//│ ╔══[COMPILATION ERROR] Unsupported form for scope.locally. +//│ ║ l.248: scope.locally() +//│ ╙── ^^^^^^^^^^^^^ -:sjs +:ge scope.locally of (let x = 1 in x), (let y = 2 in y) -//│ JS (unsanitized): -//│ let x3, y; x3 = 1; y = 2; y -//│ = 2 +//│ ╔══[COMPILATION ERROR] Unsupported form for scope.locally. +//│ ║ l.255: scope.locally of (let x = 1 in x), (let y = 2 in y) +//│ ╙── ^^^^^^^^^^^^^ :e +:ge scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╔══[ERROR] Name not found: x -//│ ║ l.259: scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ║ l.263: scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╙── ^ +//│ ╔══[COMPILATION ERROR] Unsupported form for scope.locally. +//│ ║ l.263: scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ╙── ^^^^^^^^^^^^^ From a6b1adbb032ced38bb8d5a1e99fbbb6dc5b031ad Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Sat, 6 Dec 2025 14:12:30 +0800 Subject: [PATCH 64/72] Minor --- hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 79df5ca305..f2647a2d11 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -548,7 +548,7 @@ class Lowering()(using Config, TL, Raise, State, Ctx): conclude(Value.Ref(ctx.builtins.debug.getLocals, N).withLocOf(f)) case t if instantiatedResolvedBms.exists(_ is ctx.builtins.scope.locally) => arg match - case Tup(Fld(_, body, _) :: Nil) => + case Tup(Fld(FldFlags.benign(), body, N) :: Nil) => LoweringCtx.nestScoped.givenIn: val res = block(Nil, R(body))(k) val scopedSyms = loweringCtx.getCollectedSym From 9231b1b9ae25aae3bc44336eea4ade9f5a6202a3 Mon Sep 17 00:00:00 2001 From: ychenfo <43136427+ychenfo@users.noreply.github.com> Date: Sat, 6 Dec 2025 23:44:15 +0800 Subject: [PATCH 65/72] update comments for `definedVarsNoScoped` --- .../shared/src/main/scala/hkmc2/codegen/Block.scala | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index fff9a36db1..0c82b36143 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -29,10 +29,15 @@ sealed abstract class Block extends Product: case _: End => true case _ => false - /** This variation of `definedVars` excludes the `syms` in `Scoped` blocks - * so that `Scoped` blocks' vars can be seen in the output js code; - * otherwise `blockPreamble` allocates all the `definedVars` such that - * we cannot see and test vars generated because of the existence of the `Scoped` blocks */ + /** This variation of `definedVars` excludes the `syms` in `Scoped` blocks. + * It is used in JSBuilder now: names are allocated in JSBuilder for these `definedVarsNoScoped` symbols. + * This is needed now because + * - there are symbols that are not collected in the `Scoped` blocks (due to later passes), + * and they still need to be allocated a name in JSBuilder + * - if we don't exlude the `Scoped` symbols, they may be allocated a name + * prematurely and decalred in wrong places, e.g. symbols inside while bodies + * may be declared in an outer level + */ lazy val definedVarsNoScoped: Set[Local] = this match case _: Return | _: Throw => Set.empty case Begin(sub, rst) => sub.definedVarsNoScoped ++ rst.definedVarsNoScoped From 899ee24c95901738aaa106f4f4e5b06be2c0c1bf Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Sun, 7 Dec 2025 09:00:13 +0800 Subject: [PATCH 66/72] Add some comments --- hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index c33bd2fe38..6e3d0b0d4e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -606,7 +606,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: case Scoped(syms, body) => blockPreamble(p.imports.map(_._1).toSeq ++ syms.toSeq) -> (imps.mkDocument(doc" # ") :/: block(body, endSemi = false).stripBreaks) - case body => + case body => // TODO: remove body.definedVarsNoScoped after we can handle lambda lifting-related code correctly blockPreamble(p.imports.map(_._1).toSeq ++ body.definedVarsNoScoped.toSeq) -> (imps.mkDocument(doc" # ") :/: returningTerm(body, endSemi = false).stripBreaks) @@ -619,6 +619,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: def blockPreamble(ss: Iterable[Symbol])(using Raise, Scope): Document = // TODO document: mutable var assnts require the lookup + // TODO: remove the filter and lookup after we can handle lambda lifting-related code correctly val vars = ss.filter(scope.lookup(_).isEmpty).toArray.sortBy(_.uid).iterator.map(l => l -> scope.allocateName(l)) genLetDecls(vars) From 2482b1e00418d1d87277b85402c98e9e9ecb357b Mon Sep 17 00:00:00 2001 From: Yijia CHEN <43136427+ychenfo@users.noreply.github.com> Date: Mon, 8 Dec 2025 00:18:00 +0800 Subject: [PATCH 67/72] Update hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala Co-authored-by: Lionel Parreaux --- hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 0c82b36143..43c73f789b 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -32,7 +32,7 @@ sealed abstract class Block extends Product: /** This variation of `definedVars` excludes the `syms` in `Scoped` blocks. * It is used in JSBuilder now: names are allocated in JSBuilder for these `definedVarsNoScoped` symbols. * This is needed now because - * - there are symbols that are not collected in the `Scoped` blocks (due to later passes), + * - there are symbols that are not collected in the `Scoped` blocks (due to some passes that have not yet been adapted to Scoped), * and they still need to be allocated a name in JSBuilder * - if we don't exlude the `Scoped` symbols, they may be allocated a name * prematurely and decalred in wrong places, e.g. symbols inside while bodies From 06aa4953255663ede1d8a7c71df021b4e2b6c69a Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Mon, 8 Dec 2025 11:37:24 +0800 Subject: [PATCH 68/72] Minor --- .../main/scala/mlscript/compiler/optimizer/TailRecOpt.scala | 2 ++ hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala b/compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala index 3019421f8e..92f25617f7 100644 --- a/compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala +++ b/compiler/shared/main/scala/mlscript/compiler/optimizer/TailRecOpt.scala @@ -13,6 +13,8 @@ import compiler.ir.Node._ DOCUMENTATION OF SEMANTICS OF @tailcall and @tailrec +FIXME: This doc is a bit outdated, as we have not ported the "modulo-cons" optimization yet. + @tailcall: Used to annotate specific function calls. Calls annotated with @tailcall must be tail calls or tail modulo-cons calls. These calls must be optimized to not consume additional stack space. If such an optimization is not possible, then the diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 1784284556..629eed4581 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -44,7 +44,7 @@ class LoweringCtx( val map = initMap def collectScopedSym(s: Symbol) = definedSymsDuringLowering.add(s) - def collectScopedSym(s: Symbol*) = definedSymsDuringLowering.addAll(s) + def collectScopedSyms(s: Symbol*) = definedSymsDuringLowering.addAll(s) def getCollectedSym: collection.Set[Symbol] = definedSymsDuringLowering /* def +(kv: (Local, Value)): Subst = @@ -1215,7 +1215,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) val psInspectedSyms = params.params.map(p => TempSymbol(N, dbgNme = s"traceLogParam_${p.sym.nme}") -> p.sym) val resInspectedSym = TempSymbol(N, dbgNme = "traceLogResInspected") - loweringCtx.collectScopedSym( + loweringCtx.collectScopedSyms( enterMsgSym, prevIndentLvlSym, resSym, @@ -1229,7 +1229,7 @@ trait LoweringTraceLog(instrument: Bool)(using TL, Raise, State) else Arg(N, Value.Ref(s)) :: Arg(N, Value.Lit(Tree.StrLit(", "))) :: acc val tmp1, tmp2, tmp3 = TempSymbol(N) - loweringCtx.collectScopedSym(tmp1, tmp2, tmp3) + loweringCtx.collectScopedSyms(tmp1, tmp2, tmp3) assignStmts(psInspectedSyms.map: (pInspectedSym, pSym) => pInspectedSym -> pureCall(inspectFn, Arg(N, Value.Ref(pSym)) :: Nil) From 7962e0647335d2242e727a756c389fef78af0c8a Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Wed, 10 Dec 2025 13:46:00 +0800 Subject: [PATCH 69/72] Fix flatten for nested scopes --- .../src/main/scala/hkmc2/codegen/Block.scala | 9 +- .../main/scala/hkmc2/codegen/Lowering.scala | 3 +- .../test/mlscript/codegen/NestedScoped.mls | 87 ++++++++++++------- 3 files changed, 65 insertions(+), 34 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 43c73f789b..9a9afc1dc4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -220,6 +220,13 @@ sealed abstract class Block extends Product: if (newBody is body) && (newRest is rest) then this else Label(label, loop, newBody, newRest) + + // * Do not omit the `Begin`s that are used for nested scopes + case Begin(e: End, Scoped(syms, body)) => + val newBody = body.flatten(k) + if newBody is body + then this + else new Begin(e, new Scoped(syms, newBody)) case Begin(sub, rest) => sub.flatten(_ => rest.flatten(k)) @@ -288,7 +295,7 @@ sealed abstract class Block extends Product: val newBody = body.flatten(k) if newBody is body then this - else new Scoped(syms, newBody) + else Scoped(syms, newBody) case e: End => k(e) case t: BlockTail => this diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala index 629eed4581..e7a01327f4 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala @@ -600,7 +600,8 @@ class Lowering()(using Config, TL, Raise, State, Ctx): LoweringCtx.nestScoped.givenIn: val res = block(Nil, R(body))(k) val scopedSyms = loweringCtx.getCollectedSym - new Begin(new Scoped(scopedSyms, res), End()) + // Put the Scoped in the rest, so that the returned result can be found correctly + new Begin(End(), new Scoped(scopedSyms, res)) case _ => return fail: ErrorReport( diff --git a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls index e96149c98d..f85db38886 100644 --- a/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls +++ b/hkmc2/shared/src/test/mlscript/codegen/NestedScoped.mls @@ -5,11 +5,13 @@ :sjs scope.locally of 42 //│ JS (unsanitized): -//│ 42 +//│ { 42 } //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: +//│ main = Begin: +//│ sub = End of "" +//│ rest = Scoped: \ //│ syms = HashSet() //│ body = Return: //│ res = Lit of IntLit of 42 @@ -22,11 +24,13 @@ scope.locally of 42 scope.locally of let x = 42 in x //│ JS (unsanitized): -//│ let x; x = 42; x +//│ { let x; x = 42; x } //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: +//│ main = Begin: +//│ sub = End of "" +//│ rest = Scoped: \ //│ syms = HashSet(x) //│ body = Assign: //│ lhs = x @@ -46,11 +50,13 @@ scope.locally of ( x ) //│ JS (unsanitized): -//│ let x1; x1 = 42; x1 +//│ { let x1; x1 = 42; x1 } //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: +//│ main = Begin: +//│ sub = End of "" +//│ rest = Scoped: \ //│ syms = HashSet(x) //│ body = Assign: //│ lhs = x @@ -70,13 +76,17 @@ scope.locally of scope.locally of ( x ) //│ JS (unsanitized): -//│ { let x2; x2 = 42; x2 } +//│ {{ let x2; x2 = 42; x2 } } //│ Lowered: //│ Program: //│ imports = Nil -//│ main = Scoped: +//│ main = Begin: +//│ sub = End of "" +//│ rest = Scoped: \ //│ syms = HashSet() -//│ body = Scoped: +//│ body = Begin: +//│ sub = End of "" +//│ rest = Scoped: \ //│ syms = HashSet(x) //│ body = Assign: //│ lhs = x @@ -93,8 +103,8 @@ scope.locally of scope.locally of ( :e x //│ ╔══[ERROR] Name not found: x -//│ ║ l.94: x -//│ ╙── ^ +//│ ║ l.104: x +//│ ╙── ^ :ssjs @@ -106,7 +116,8 @@ fun foo(x) = //│ foo = function foo(...args) { //│ runtime.checkArgs("foo", 1, true, args.length); //│ let x3 = args[0]; -//│ let z;{ let y, tmp; z = 1; y = 1; tmp = x3 + y; return tmp + z } +//│ let z; +//│ z = 1;{ let y, tmp; y = 1; tmp = x3 + y; return tmp + z } //│ }; //│ block$res6 = undefined; @@ -130,7 +141,7 @@ fun f() = //│ f = function f() { //│ let scrut, b; //│ scrut = true; -//│ if (scrut === true) { let a; a = 4; return a + 4 } else { b = 5; return b + 9 } +//│ if (scrut === true) {{ let a; a = 4; return a + 4 } } else { b = 5; return b + 9 } //│ }; @@ -153,7 +164,7 @@ fun f() = //│ f1 = function f() { //│ let scrut, a; //│ scrut = false; -//│ if (scrut === true) { a = 4; return a + 4 } else { let b; b = 5; return b + 9 } +//│ if (scrut === true) { a = 4; return a + 4 } else {{ let b; b = 5; return b + 9 } } //│ }; @@ -221,21 +232,21 @@ fun foo(x, y) = //│ JS (unsanitized): //│ let foo1; //│ foo1 = function foo(x3, y) { -//│ let z, scrut, m, tmp, tmp1;{ +//│ let z, scrut, m, tmp, tmp1; +//│ z = globalThis.Object.freeze([ +//│ 1, +//│ 2, +//│ 3 +//│ ]); +//│ tmp = x3 > z[0]; +//│ scrut = ! tmp; +//│ if (scrut === true) { +//│ tmp1 = Predef.print("rua"); +//│ } else { +//│ tmp1 = runtime.Unit; +//│ } +//│ m = x3 + y;{ //│ let w1, w2, scrut1; -//│ z = globalThis.Object.freeze([ -//│ 1, -//│ 2, -//│ 3 -//│ ]); -//│ tmp = x3 > z[0]; -//│ scrut = ! tmp; -//│ if (scrut === true) { -//│ tmp1 = Predef.print("rua"); -//│ } else { -//│ tmp1 = runtime.Unit; -//│ } -//│ m = x3 + y; //│ w1 = z[1] + m; //│ w2 = z[2] - m; //│ scrut1 = Predef.equals(w1, w2); @@ -247,14 +258,14 @@ fun foo(x, y) = :ge scope.locally() //│ ╔══[COMPILATION ERROR] Unsupported form for scope.locally. -//│ ║ l.248: scope.locally() +//│ ║ l.259: scope.locally() //│ ╙── ^^^^^^^^^^^^^ :ge scope.locally of (let x = 1 in x), (let y = 2 in y) //│ ╔══[COMPILATION ERROR] Unsupported form for scope.locally. -//│ ║ l.255: scope.locally of (let x = 1 in x), (let y = 2 in y) +//│ ║ l.266: scope.locally of (let x = 1 in x), (let y = 2 in y) //│ ╙── ^^^^^^^^^^^^^ @@ -262,8 +273,20 @@ scope.locally of (let x = 1 in x), (let y = 2 in y) :ge scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╔══[ERROR] Name not found: x -//│ ║ l.263: scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ║ l.274: scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╙── ^ //│ ╔══[COMPILATION ERROR] Unsupported form for scope.locally. -//│ ║ l.263: scope.locally of (let x = 1 in x), (let y = 2 in x) +//│ ║ l.274: scope.locally of (let x = 1 in x), (let y = 2 in x) //│ ╙── ^^^^^^^^^^^^^ + + +:sjs +let z = 114 +scope.locally of ( + let y = 514 + y +) +//│ JS (unsanitized): +//│ let z; z = 114;{ let y; y = 514; y } +//│ = 514 +//│ z = 114 From 562f927d50bd46a6253f98752156a07b5742a87f Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Wed, 10 Dec 2025 17:30:25 +0800 Subject: [PATCH 70/72] Changes from meeting --- .../src/main/scala/hkmc2/codegen/Block.scala | 14 ++++++++++---- .../main/scala/hkmc2/codegen/HandlerLowering.scala | 2 +- .../src/main/scala/hkmc2/codegen/Lifter.scala | 8 +++++--- .../scala/hkmc2/codegen/StackSafeTransform.scala | 6 +++--- .../src/main/scala/hkmc2/codegen/TailRecOpt.scala | 2 +- .../main/scala/hkmc2/codegen/UsedVarAnalyzer.scala | 12 ++++++------ 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index 9a9afc1dc4..cb65ef2697 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -184,10 +184,10 @@ sealed abstract class Block extends Product: // Note that this returns the definitions in reverse order, with the bottommost definiton appearing // last. This is so that using defns.foldLeft later to add the definitions to the front of a block, // we don't need to reverse the list again to preserve the order of the definitions. - def floatOutDefns( - ignore: Defn => Bool = _ => false, - preserve: Defn => Bool = _ => false - ) = + def extractDefns( + ignore: Defn => Bool = _ => false, + preserve: Defn => Bool = _ => false + ): (Block, List[Defn]) = var defns: List[Defn] = Nil val transformer = new BlockTransformerShallow(SymbolSubst()): override def applyBlock(b: Block): Block = b match @@ -201,6 +201,12 @@ sealed abstract class Block extends Product: (transformer.applyBlock(this), defns) + def gatherDefns( + ignore: Defn => Bool = _ => false, + preserve: Defn => Bool = _ => false + ): List[Defn] = extractDefns(ignore, preserve)._2 // TODO: fix this very inefficient implementation + + lazy val flattened: Block = this.flatten(identity) private def flatten(k: End => Block): Block = this match diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala index 33b0591142..23707ade81 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala @@ -582,7 +582,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise, private def thirdPass(b: Block): Block = // to ensure the fun and class references in the continuation class are properly scoped, // we move all function defns to the top level of the handler block - val (blk, defns) = b.floatOutDefns() + val (blk, defns) = b.extractDefns() defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) private def locToStr(l: Loc): Str = diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lifter.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lifter.scala index 6d237681b7..99f5caec51 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Lifter.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Lifter.scala @@ -546,8 +546,10 @@ class Lifter(handlerPaths: Opt[HandlerPaths])(using State, Raise): extension (b: Block) private def floatOut(ctx: LifterCtx) = - b.floatOutDefns(preserve = defn => ctx.isModOrObj(defn.sym) || ctx.ignored(defn.sym)) - + b.extractDefns(preserve = defn => ctx.isModOrObj(defn.sym) || ctx.ignored(defn.sym)) + private def gather(ctx: LifterCtx) = + b.gatherDefns(preserve = defn => ctx.isModOrObj(defn.sym) || ctx.ignored(defn.sym)) + def createLiftInfoCont(d: Defn, parentCls: Opt[ClsLikeDefn], ctx: LifterCtx): Map[BlockMemberSymbol, LiftedInfo] = val AccessInfo(accessed, _, refdDefns) = ctx.getAccesses(d.sym) @@ -600,7 +602,7 @@ class Lifter(handlerPaths: Opt[HandlerPaths])(using State, Raise): defns.flatMap(createLiftInfoCont(_, N, ctx.addFnLocals(ctx.usedLocals(f.sym)))).toMap def createLiftInfoCls(c: ClsLikeDefn, ctx: LifterCtx): Map[BlockMemberSymbol, LiftedInfo] = - val defns = c.preCtor.floatOut(ctx)._2 ++ c.ctor.floatOut(ctx)._2 ++ c.companion.fold(Nil)(_.ctor.floatOut(ctx)._2) + val defns = c.preCtor.gather(ctx) ++ c.ctor.gather(ctx) ++ c.companion.fold(Nil)(_.ctor.gather(ctx)) val newCtx = if (c.companion.isDefined) && !ctx.ignored(c.sym) then ctx else ctx.addClsDefn(c) val staticMtdInfo = c.companion.fold(Map.empty): case value => value.methods.flatMap(f => createLiftInfoFn(f, newCtx)) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/StackSafeTransform.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/StackSafeTransform.scala index a98afafca9..977d240fc7 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/StackSafeTransform.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/StackSafeTransform.scala @@ -166,11 +166,11 @@ class StackSafeTransform(depthLimit: Int, paths: HandlerPaths, doUnwindMap: Map[ // However, due to how tightly coupled the stack safety and handler lowering are, it might be // better to simply merge the two passes in the future. val (blk, defns) = doUnwindPath.get match - case Value.Ref(sym, _) => rewritten.floatOutDefns() + case Value.Ref(sym, _) => rewritten.extractDefns() case _ => (rewritten, Nil) defns.foldLeft(blk)((acc, defn) => Define(defn, acc)) - - + + def rewriteFn(defn: FunDefn) = if doUnwindFns.contains(defn.sym) then defn else FunDefn(defn.owner, defn.sym, defn.dSym, defn.params, rewriteBlk(defn.body, L(defn.sym), 1))(defn.forceTailRec) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala index 3bb85d90d3..690da38c08 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala @@ -362,7 +362,7 @@ class TailRecOpt(using State, TL, Raise): c.copy(methods = mtds, companion = companion) def transform(b: Block) = - val (blk, defns) = b.floatOutDefns() + val defns = b.gatherDefns() val (funs, clses) = defns.partitionMap: case f: FunDefn => L(f) case c: ClsLikeDefn => R(c) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/UsedVarAnalyzer.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/UsedVarAnalyzer.scala index 622e5d8f0d..026610a470 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/UsedVarAnalyzer.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/UsedVarAnalyzer.scala @@ -48,9 +48,9 @@ class UsedVarAnalyzer(b: Block, handlerPaths: Opt[HandlerPaths])(using State): existingVars += (f.sym -> existing) val thisVars = Lifter.getVars(f) -- existing val newExisting = existing ++ thisVars - - val thisScopeDefns: List[Defn] = f.body.floatOutDefns()._2 - + + val thisScopeDefns: List[Defn] = f.body.gatherDefns() + nestedDefns += f.sym -> thisScopeDefns val newInScope = inScope ++ thisScopeDefns.map(_.sym) @@ -83,8 +83,8 @@ class UsedVarAnalyzer(b: Block, handlerPaths: Opt[HandlerPaths])(using State): val thisVars = Lifter.getVars(c) -- existing val newExisting = existing ++ thisVars - val thisScopeDefns: List[Defn] = c.methods ++ c.preCtor.floatOutDefns()._2 - ++ c.ctor.floatOutDefns()._2 ++ c.companion.fold(Nil)(comp => comp.ctor.floatOutDefns()._2 ++ comp.methods) + val thisScopeDefns: List[Defn] = c.methods ++ c.preCtor.gatherDefns() + ++ c.ctor.gatherDefns() ++ c.companion.fold(Nil)(comp => comp.ctor.gatherDefns() ++ comp.methods) nestedDefns += c.sym -> thisScopeDefns @@ -271,7 +271,7 @@ class UsedVarAnalyzer(b: Block, handlerPaths: Opt[HandlerPaths])(using State): // I'll fix it once it's fixed in the IR since we will have more tools to determine // what locals belong to what block. private def reqdCaptureLocals(f: FunDefn) = - var (_, defns) = f.body.floatOutDefns() + var defns = f.body.gatherDefns() val defnSyms = defns.collect: case f: FunDefn => f.sym -> f case c: ClsLikeDefn => c.sym -> c From a869edc757fa0f6bfdbd6cb46b0514fd51e9ffab Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 12 Dec 2025 22:45:37 +0800 Subject: [PATCH 71/72] Remove braces in switch --- .../scala/hkmc2/codegen/js/JSBuilder.scala | 2 +- .../src/test/mlscript/tailrec/TailRecOpt.mls | 58 +++++++++---------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala index 89750320a6..6ca873a496 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/js/JSBuilder.scala @@ -484,7 +484,7 @@ class JSBuilder(using TL, State, Ctx) extends CodeBuilder: if arms.sizeCompare(1) > 0 && arms.forall(_._1.isInstanceOf[Case.Lit]) => val l = arms.foldLeft(doc""): (acc, arm) => acc :: doc" # case ${arm._1.asInstanceOf[Case.Lit].lit.idStr}: #{ ${ - returningTerm(arm._2, endSemi = true) + nonNestedScoped(arm._2)(bd => returningTerm(bd, endSemi = true)) } # break; #} " val e = els match case S(el) => diff --git a/hkmc2/shared/src/test/mlscript/tailrec/TailRecOpt.mls b/hkmc2/shared/src/test/mlscript/tailrec/TailRecOpt.mls index 7378d825e6..b998753c3b 100644 --- a/hkmc2/shared/src/test/mlscript/tailrec/TailRecOpt.mls +++ b/hkmc2/shared/src/test/mlscript/tailrec/TailRecOpt.mls @@ -21,34 +21,32 @@ f(10000, 20000, 0) //│ g_f = function g_f(id, param0, param1, param2, param3) { //│ loopLabel: while (true) { //│ switch (id) { -//│ case 0:{ -//│ let tmp; -//│ tmp = param2 + param3; -//│ param2 = tmp; -//│ id = 1; -//│ continue loopLabel; -//│ } +//│ case 0: +//│ let tmp; +//│ tmp = param2 + param3; +//│ param2 = tmp; +//│ id = 1; +//│ continue loopLabel; //│ break; -//│ case 1:{ -//│ let scrut, scrut1, tmp, tmp1; -//│ scrut = param0 > 0; -//│ if (scrut === true) { -//│ tmp = param0 - 1; -//│ param0 = tmp; -//│ param3 = 1; +//│ case 1: +//│ let scrut, scrut1, tmp1, tmp2; +//│ scrut = param0 > 0; +//│ if (scrut === true) { +//│ tmp1 = param0 - 1; +//│ param0 = tmp1; +//│ param3 = 1; +//│ id = 0; +//│ continue loopLabel +//│ } else { +//│ scrut1 = param1 > 0; +//│ if (scrut1 === true) { +//│ tmp2 = param1 - 1; +//│ param1 = tmp2; +//│ param3 = 2; //│ id = 0; //│ continue loopLabel //│ } else { -//│ scrut1 = param1 > 0; -//│ if (scrut1 === true) { -//│ tmp1 = param1 - 1; -//│ param1 = tmp1; -//│ param3 = 2; -//│ id = 0; -//│ continue loopLabel -//│ } else { -//│ return param2 -//│ } +//│ return param2 //│ } //│ } //│ break; @@ -150,7 +148,7 @@ fun f(x) = @tailcall f(x) @tailcall g() //│ ╔══[ERROR] This call is not in tail position. -//│ ║ l.149: @tailcall f(x) +//│ ║ l.147: @tailcall f(x) //│ ╙── ^ :lift @@ -168,10 +166,10 @@ module A with @tailcall g(x - 1) A.f(10000) //│ ╔══[ERROR] This tail call exits the current scope is not optimized. -//│ ║ l.167: fun g(x) = if x < 0 then 0 else @tailcall f(x) +//│ ║ l.165: fun g(x) = if x < 0 then 0 else @tailcall f(x) //│ ╙── ^^^ //│ ╔══[ERROR] This tail call exits the current scope is not optimized. -//│ ║ l.168: @tailcall g(x - 1) +//│ ║ l.166: @tailcall g(x - 1) //│ ╙── ^^^^^^^^ //│ ═══[RUNTIME ERROR] RangeError: Maximum call stack size exceeded @@ -198,12 +196,12 @@ class A with fun g(x) = if x == 0 then 1 else @tailcall f(x - 1) (new A).f(10) //│ ╔══[ERROR] Calls from class methods cannot yet be marked @tailcall. -//│ ║ l.197: @tailrec fun f(x) = if x == 0 then 0 else @tailcall g(x - 1) +//│ ║ l.195: @tailrec fun f(x) = if x == 0 then 0 else @tailcall g(x - 1) //│ ╙── ^^^^^^^^ //│ ╔══[ERROR] Class methods may not yet be marked @tailrec. -//│ ║ l.197: @tailrec fun f(x) = if x == 0 then 0 else @tailcall g(x - 1) +//│ ║ l.195: @tailrec fun f(x) = if x == 0 then 0 else @tailcall g(x - 1) //│ ╙── ^ //│ ╔══[ERROR] Calls from class methods cannot yet be marked @tailcall. -//│ ║ l.198: fun g(x) = if x == 0 then 1 else @tailcall f(x - 1) +//│ ║ l.196: fun g(x) = if x == 0 then 1 else @tailcall f(x - 1) //│ ╙── ^^^^^^^^ //│ = 0 From 27241461d789d98eb9d72cd0767807dcf215e385 Mon Sep 17 00:00:00 2001 From: NeilKleistGao Date: Fri, 12 Dec 2025 22:55:51 +0800 Subject: [PATCH 72/72] Revert unnecessary changes --- hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala index cd73a40184..cf9e5b77a2 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala @@ -394,9 +394,7 @@ object Match: def apply(scrut: Path, arms: Ls[Case -> Block], dflt: Opt[Block], rest: Block): Block = dflt match case S(Match(`scrut`, arms2, dflt2, _: End)) => // TODO: also handle non-End rest (may require a join point) // * Currently, this branch does not seem used, because the UCS already does a good job at merging matches - rest match - case Scoped(syms, body) => Scoped(syms, Match(scrut, arms ::: arms2, dflt2, body)) - case _ => new Match(scrut, arms ::: arms2, dflt2, rest) + Match(scrut, arms ::: arms2, dflt2, rest) case _ => if !rest.isEmpty && arms.forall(_._2.isAbortive) && dflt.exists(_.isAbortive) then new Match(scrut, arms, dflt, End("unreachable"))