Skip to content

Commit

Permalink
make scalajs tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Oct 2, 2023
1 parent bfa2b37 commit e90e487
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
22 changes: 13 additions & 9 deletions core/src/main/scala/org/bykn/bosatsu/codegen/python/Code.scala
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,21 @@ object Code {
case exprS => Parens(exprS)
}
}
case class SelectItem(arg: Expression, position: Int) extends Expression {
case class SelectItem(arg: Expression, position: Expression) extends Expression {
def simplify: Expression =
arg.simplify match {
case MakeTuple(items) if items.lengthCompare(position) > 0 =>
items(position)
case MakeList(items) if items.lengthCompare(position) > 0 =>
items(position)
case simp =>
SelectItem(simp, position)
(arg.simplify, position.simplify) match {
case (MakeTuple(items), PyInt(bi)) if items.lengthCompare(bi.intValue()) > 0 =>
items(bi.intValue())
case (MakeList(items), PyInt(bi)) if items.lengthCompare(bi.intValue()) > 0 =>
items(bi.intValue())
case (simp, spos) =>
SelectItem(simp, spos)
}
}
object SelectItem {
def apply(arg: Expression, position: Int): SelectItem =
SelectItem(arg, Code.fromInt(position))
}
// foo[a:b]
case class SelectRange(arg: Expression, start: Option[Expression], end: Option[Expression]) extends Expression {
def simplify = SelectRange(arg, start.map(_.simplify), end.map(_.simplify))
Expand Down Expand Up @@ -610,7 +614,7 @@ object Code {
lit match {
case Lit.Str(s) => PyString(s)
case Lit.Integer(bi) => PyInt(bi)
case Lit.Chr(_) => ???
case Lit.Chr(s) => PyString(s)
}

def fromInt(i: Int): Expression =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,21 @@ object PythonGen {

Env.andCode(regionMatches, rest)
}
case (c: CharPart) :: tail => ???
case (c: CharPart) :: tail =>
val n1 = if (c.capture) (next + 1) else next
for {
tailRes <- loop(offsetIdent, tail, n1)
matches = Code.Op(offsetIdent, Code.Const.Lt, strEx.dot(Code.Ident("__len__"))())
stmt =
if (c.capture) {
// b = str[offset:]
(bindArray(next) := Code.SelectItem(strEx, offsetIdent))
.withValue(Code.Const.True)
}
else Code.Const.True
and2 <- Env.andCode(stmt, tailRes)
and1 <- Env.andCode(matches, and2)
} yield and1
case (h: Glob) :: tail =>
tail match {
case Nil =>
Expand Down Expand Up @@ -1274,19 +1288,20 @@ object PythonGen {
.withValue(result))
}
.flatten
case (c: CharPart) :: tail2 => ???
case (c: CharPart) :: tail2 =>
// TODO convert the MatchlessToValue code to python like above
???
// $COVERAGE-OFF$
case (_: Glob) :: _ =>
// $COVERAGE-OFF$
throw new IllegalArgumentException(s"pattern: $pat should have been prevented: adjacent globs are not permitted (one is always empty)")
// $COVERAGE-ON$
// $COVERAGE-ON$
}
}

Env.newAssignableVar
.flatMap { offsetIdent =>
loop(offsetIdent, pat, 0)
.map { res => (offsetIdent := Code.fromInt(0)).withValue(res) }
}
for {
offsetIdent <- Env.newAssignableVar
res <- loop(offsetIdent, pat, 0)
} yield (offsetIdent := Code.fromInt(0)).withValue(res)
}

def searchList(locMut: LocalAnonMut, initVL: ValueLike, checkVL: ValueLike, optLeft: Option[LocalAnonMut]): Env[ValueLike] = {
Expand Down
10 changes: 8 additions & 2 deletions core/src/test/scala/org/bykn/bosatsu/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,14 @@ class ParserTest extends ParserTestBase {
val str = strbuilder.toString
val hex = cps.map(_.toHexString)

assert(p.parseAll(str).map(_.codePoints.toArray.toList) == Right(cps),
s"hex = $hex, str = ${str.codePoints.toArray.toList} utf16 = ${str.toCharArray().toList.map(_.toInt.toHexString)}")
val parsed = p.parseAll(str)
assert(parsed == Right(str))

if (Platform.isScalaJvm) {
// codePoints isn't available in scalajs
assert(parsed.map(_.codePoints.toArray.toList) == Right(cps),
s"hex = $hex, str = ${str.codePoints.toArray.toList} utf16 = ${str.toCharArray().toList.map(_.toInt.toHexString)}")
}
}
}

Expand Down

0 comments on commit e90e487

Please sign in to comment.