Skip to content

Commit

Permalink
Fix two codegen problems: selections on naked new & binary operator…
Browse files Browse the repository at this point in the history
…s in web demo

Also add some missing self types in two tests
  • Loading branch information
LPTK committed Apr 2, 2024
1 parent 2424980 commit 69392b3
Show file tree
Hide file tree
Showing 8 changed files with 3,216 additions and 3,039 deletions.
6,150 changes: 3,125 additions & 3,025 deletions bin/mlscript-opt.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ object Main {
|""".stripMargin

val backend = new JSWebBackend()
val (lines, resNames) = backend(pgrm, true)
val (lines, resNames) = backend(pgrm, newDefs = true)
val code = lines.mkString("\n")

// TODO: add a toggle button to show js code
Expand Down
11 changes: 4 additions & 7 deletions shared/src/main/scala/mlscript/JSBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.collection.mutable.{Set => MutSet}
import scala.util.control.NonFatal
import scala.util.chaining._

abstract class JSBackend(allowUnresolvedSymbols: Bool) {
abstract class JSBackend {
def oldDefs: Bool

protected implicit class TermOps(term: Term) {
Expand Down Expand Up @@ -146,10 +146,7 @@ abstract class JSBackend(allowUnresolvedSymbols: Bool) {
return Left(CodeGenError(s"type alias ${name} is not a valid expression"))
case S(_) => lastWords("register mismatch in scope")
case N =>
if (allowUnresolvedSymbols)
JSIdent(name)
else
return Left(CodeGenError(s"unresolved symbol ${name}"))
return Left(CodeGenError(s"unresolved symbol ${name}"))
}
})

Expand Down Expand Up @@ -1317,7 +1314,7 @@ abstract class JSBackend(allowUnresolvedSymbols: Bool) {

}

class JSWebBackend extends JSBackend(allowUnresolvedSymbols = true) {
class JSWebBackend extends JSBackend {
def oldDefs = false

// Name of the array that contains execution results
Expand Down Expand Up @@ -1448,7 +1445,7 @@ class JSWebBackend extends JSBackend(allowUnresolvedSymbols = true) {
if (newDefs) generateNewDef(pgrm) else generate(pgrm)
}

abstract class JSTestBackend extends JSBackend(allowUnresolvedSymbols = false) {
abstract class JSTestBackend extends JSBackend {

private val lastResultSymbol = topLevelScope.declareValue("res", Some(false), false, N)
private val resultIdent = JSIdent(lastResultSymbol.runtimeName)
Expand Down
4 changes: 2 additions & 2 deletions shared/src/main/scala/mlscript/codegen/Codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ class JSMember(`object`: JSExpr, property: JSExpr) extends JSExpr {
override def precedence: Int = 20
override def toSourceCode: SourceCode =
`object`.toSourceCode.parenthesized(
`object`.precedence < precedence || `object`.isInstanceOf[JSRecord]
`object`.precedence < precedence || `object`.isInstanceOf[JSRecord] || `object`.isInstanceOf[JSNew]
) ++ SourceCode("[") ++ property.toSourceCode ++ SourceCode("]")

override def isSimple: Bool = `object`.isSimple
Expand All @@ -602,7 +602,7 @@ object JSMember {
class JSField(`object`: JSExpr, val property: JSIdent) extends JSMember(`object`, property) {
override def toSourceCode: SourceCode =
`object`.toSourceCode.parenthesized(
`object`.precedence < precedence || `object`.isInstanceOf[JSRecord]
`object`.precedence < precedence || `object`.isInstanceOf[JSRecord] || `object`.isInstanceOf[JSNew]
) ++ SourceCode(
if (JSField.isValidFieldName(property.name)) {
s".${property.name}"
Expand Down
1 change: 1 addition & 0 deletions shared/src/main/scala/mlscript/codegen/Scope.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Scope(val name: Str, enclosing: Opt[Scope]) {
"Wildcard",
"NoCases",
"discard",
"window",
) foreach { name =>
register(BuiltinSymbol(name, name))
}
Expand Down
79 changes: 79 additions & 0 deletions shared/src/test/diff/codegen/New.mls
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,82 @@ c()
//│ = C {}


:js
class X(val a: Int)
new X(1).a
//│ class X(a: Int)
//│ Int
//│ // Prelude
//│ class TypingUnit10 {
//│ #X;
//│ constructor() {
//│ }
//│ get X() {
//│ const qualifier = this;
//│ if (this.#X === undefined) {
//│ class X {
//│ #a;
//│ get a() { return this.#a; }
//│ constructor(a) {
//│ this.#a = a;
//│ }
//│ static
//│ unapply(x) {
//│ return [x.#a];
//│ }
//│ };
//│ this.#X = ((a) => Object.freeze(new X(a)));
//│ this.#X.class = X;
//│ this.#X.unapply = X.unapply;
//│ }
//│ return this.#X;
//│ }
//│ }
//│ const typing_unit10 = new TypingUnit10;
//│ globalThis.X = typing_unit10.X;
//│ // Query 1
//│ res = new X.class(1).a;
//│ // End of generated code
//│ res
//│ = 1

:js
class X {
val a = 1
}
(new X).a
//│ class X {
//│ constructor()
//│ val a: 1
//│ }
//│ 1
//│ // Prelude
//│ class TypingUnit11 {
//│ #X;
//│ constructor() {
//│ }
//│ get X() {
//│ const qualifier = this;
//│ if (this.#X === undefined) {
//│ class X {
//│ #a;
//│ get a() { return this.#a; }
//│ constructor() {
//│ this.#a = 1;
//│ const a = this.#a;
//│ }
//│ };
//│ this.#X = X;
//│ }
//│ return this.#X;
//│ }
//│ }
//│ const typing_unit11 = new TypingUnit11;
//│ globalThis.X = typing_unit11.X;
//│ // Query 1
//│ res = (new X).a;
//│ // End of generated code
//│ res
//│ = 1


4 changes: 2 additions & 2 deletions shared/src/test/diff/nu/CaseExpr.mls
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ fun foo = case



abstract class Option[out A]
abstract class Option[out A]: Some[A] | None
class Some[out A](val value: A) extends Option[A]
module None extends Option[nothing]
//│ abstract class Option[A]
//│ abstract class Option[A]: None | Some[A]
//│ class Some[A](value: A) extends Option
//│ module None extends Option

Expand Down
4 changes: 2 additions & 2 deletions shared/src/test/diff/nu/Eval.mls
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ test
//│ 1


abstract class Option[out A]
abstract class Option[out A]: Some[A] | None
class Some[out A](val value: A) extends Option[A]
module None extends Option[nothing]
//│ abstract class Option[A]
//│ abstract class Option[A]: None | Some[A]
//│ class Some[A](value: A) extends Option
//│ module None extends Option

Expand Down

0 comments on commit 69392b3

Please sign in to comment.