Skip to content

Commit

Permalink
LF: add Arithmetic builtin exceptions
Browse files Browse the repository at this point in the history
part of #8020

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da committed May 12, 2021
1 parent e61e6b2 commit 05e1f7c
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private[engine] final class ValueTranslator(compiledPackages: CompiledPackages)
case (BTNumeric, ValueNumeric(d)) =>
typeArg0 match {
case TNat(s) =>
Numeric.fromBigDecimal(s, d).fold(fail, SValue.SNumeric)
Numeric.fromBigDecimal(s, d).fold(fail, SValue.SNumeric(_))
case _ =>
typeMismatch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,10 @@ private[lf] object Anf {
val body: SExpr = flattenExp(depth, env, body0)(anf => Land(anf.wrapped)).bounce
Bounce(() => transform(depth, SEScopeExercise(body), k))

case x: SEAbs => throw CompilationError(s"flatten: unexpected: $x")
case x: SEDamlException => throw CompilationError(s"flatten: unexpected: $x")
case x: SEAppAtomicFun => throw CompilationError(s"flatten: unexpected: $x")
case x: SEAppAtomicGeneral => throw CompilationError(s"flatten: unexpected: $x")
case x: SEAppAtomicSaturatedBuiltin => throw CompilationError(s"flatten: unexpected: $x")
case x: SELet1Builtin => throw CompilationError(s"flatten: unexpected: $x")
case x: SECaseAtomic => throw CompilationError(s"flatten: unexpected: $x")
case _: SEAbs | _: SEDamlException | _: SEAppAtomicFun | _: SEAppAtomicGeneral |
_: SEAppAtomicSaturatedBuiltin | _: SELet1Builtin | _: SELet1BuiltinArithmetic |
_: SECaseAtomic =>
throw CompilationError(s"flatten: unexpected: $exp")
}

private[this] def atomizeExps[A](depth: DepthA, env: Env, exps: List[SExpr], k: K[AExpr, A])(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1144,12 +1144,6 @@ private[lf] final class Compiler(
val newBody = closureConvert(newRemapsF ++ newRemapsA, body)
SEMakeClo(fvs.map(remap).toArray, arity, newBody)

case x: SELoc =>
throw CompilationError(s"closureConvert: unexpected SELoc: $x")

case x: SEMakeClo =>
throw CompilationError(s"closureConvert: unexpected SEMakeClo: $x")

case SEAppGeneral(fun, args) =>
val newFun = closureConvert(remaps, fun)
val newArgs = args.map(closureConvert(remaps, _))
Expand Down Expand Up @@ -1197,26 +1191,13 @@ private[lf] final class Compiler(
case SELabelClosure(label, expr) =>
SELabelClosure(label, closureConvert(remaps, expr))

case x: SEDamlException =>
throw CompilationError(s"unexpected SEDamlException: $x")

case x: SEImportValue =>
throw CompilationError(s"unexpected SEImportValue: $x")

case x: SEAppAtomicGeneral =>
throw CompilationError(s"closureConvert: unexpected: $x")

case x: SEAppAtomicSaturatedBuiltin =>
throw CompilationError(s"closureConvert: unexpected: $x")

case SELet1General(bound, body) =>
SELet1General(closureConvert(remaps, bound), closureConvert(shift(remaps, 1), body))

case x: SELet1Builtin =>
throw CompilationError(s"closureConvert: unexpected: $x")

case x: SECaseAtomic =>
throw CompilationError(s"closureConvert: unexpected: $x")
case _: SELoc | _: SEMakeClo | _: SELet1Builtin | _: SELet1BuiltinArithmetic |
_: SEDamlException | _: SEImportValue | _: SEAppAtomicGeneral |
_: SEAppAtomicSaturatedBuiltin | _: SECaseAtomic =>
throw CompilationError(s"closureConvert: unexpected $expr")
}
}

Expand Down Expand Up @@ -1264,10 +1245,6 @@ private[lf] final class Compiler(
args.foldLeft(go(fun, bound, free))((acc, arg) => go(arg, bound, acc))
case SEAbs(n, body) =>
go(body, bound + n, free)
case x: SELoc =>
throw CompilationError(s"freeVars: unexpected SELoc: $x")
case x: SEMakeClo =>
throw CompilationError(s"freeVars: unexpected SEMakeClo: $x")
case SECase(scrut, alts) =>
alts.foldLeft(go(scrut, bound, free)) { case (acc, SCaseAlt(pat, body)) =>
go(body, bound + patternNArgs(pat), acc)
Expand All @@ -1285,16 +1262,10 @@ private[lf] final class Compiler(
case SEScopeExercise(body) =>
go(body, bound, free)

case x: SEDamlException =>
throw CompilationError(s"unexpected SEDamlException: $x")
case x: SEImportValue =>
throw CompilationError(s"unexpected SEImportValue: $x")

case x: SEAppAtomicGeneral => throw CompilationError(s"freeVars: unexpected: $x")
case x: SEAppAtomicSaturatedBuiltin => throw CompilationError(s"freeVars: unexpected: $x")
case x: SELet1General => throw CompilationError(s"freeVars: unexpected: $x")
case x: SELet1Builtin => throw CompilationError(s"freeVars: unexpected: $x")
case x: SECaseAtomic => throw CompilationError(s"freeVars: unexpected: $x")
case _: SELoc | _: SEMakeClo | _: SEDamlException | _: SEImportValue |
_: SEAppAtomicGeneral | _: SEAppAtomicSaturatedBuiltin | _: SELet1General |
_: SELet1Builtin | _: SELet1BuiltinArithmetic | _: SECaseAtomic =>
throw CompilationError(s"freeVars: unexpected $expr")
}

go(expr, initiallyBound, Set.empty)
Expand Down Expand Up @@ -1356,10 +1327,6 @@ private[lf] final class Compiler(
case SEAppAtomicFun(fun, args) =>
go(fun)
args.foreach(go)
case x: SEVar =>
throw CompilationError(s"validate: SEVar encountered: $x")
case abs: SEAbs =>
throw CompilationError(s"validate: SEAbs encountered: $abs")
case SEMakeClo(fvs, n, body) =>
fvs.foreach(goLoc)
goBody(0, n, fvs.length)(body)
Expand All @@ -1377,6 +1344,7 @@ private[lf] final class Compiler(
goBody(maxS + bounds.length, maxA, maxF)(body)
case _: SELet1General => goLets(maxS)(expr)
case _: SELet1Builtin => goLets(maxS)(expr)
case _: SELet1BuiltinArithmetic => goLets(maxS)(expr)
case SECatchSubmitMustFail(body) =>
go(body)
case SELocation(_, body) =>
Expand All @@ -1389,10 +1357,8 @@ private[lf] final class Compiler(
case SEScopeExercise(body) =>
go(body)

case x: SEDamlException =>
throw CompilationError(s"unexpected SEDamlException: $x")
case x: SEImportValue =>
throw CompilationError(s"unexpected SEImportValue: $x")
case _: SEVar | _: SEAbs | _: SEDamlException | _: SEImportValue =>
throw CompilationError(s"validate: unexpected $expr")
}
@tailrec
def goLets(maxS: Int)(expr: SExpr): Unit = {
Expand All @@ -1404,6 +1370,9 @@ private[lf] final class Compiler(
case SELet1Builtin(_, args, body) =>
args.foreach(go)
goLets(maxS + 1)(body)
case SELet1BuiltinArithmetic(_, args, body) =>
args.foreach(go)
goLets(maxS + 1)(body)
case expr =>
go(expr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ private[lf] object Pretty {
ex match {
case DamlEFailedAuthorization(nid, fa) =>
text(prettyFailedAuthorization(nid, fa))
case DamlEArithmeticError(message) =>
text(message)
case DamlEUnhandledException(exc) =>
text(s"unhandled exception:") & {
exc match {
Expand Down Expand Up @@ -590,7 +588,8 @@ private[lf] object Pretty {
prettySExpr(index)(SELet(List(rhs), body))
case SELet1Builtin(builtin, args, body) =>
prettySExpr(index)(SELet1General(SEAppAtomicSaturatedBuiltin(builtin, args), body))

case SELet1BuiltinArithmetic(builtin, args, body) =>
prettySExpr(index)(SELet1General(SEAppAtomicSaturatedBuiltin(builtin, args), body))
case SETryCatch(body, handler) =>
text("try-catch") + char('(') + prettySExpr(index)(body) + text(", ") +
prettySExpr(index)(handler) + char(')')
Expand Down
Loading

0 comments on commit 05e1f7c

Please sign in to comment.