-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
870884d
commit 8c8e4b0
Showing
12 changed files
with
141 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/melvic/dry/interpreter/natives/Exceptions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.melvic.dry.interpreter.natives | ||
|
||
import com.melvic.dry.interpreter.Env | ||
import com.melvic.dry.interpreter.Env.Register | ||
import com.melvic.dry.interpreter.values.DException._ | ||
import com.melvic.dry.interpreter.values.Value.{Types, typeOf} | ||
import com.melvic.dry.interpreter.values.{Callable, DException, DInstance, Value} | ||
import com.melvic.dry.result.Failure.RuntimeError | ||
import com.melvic.dry.result.Result.Result | ||
|
||
object Exceptions { | ||
def register: Register = | ||
_.defineWith("raise", raise) | ||
.defineWith(DivisionByZero.name, DException(DivisionByZero, _)) | ||
|
||
private def raise(env: Env): Callable = Callable.withLineNo(1, env) { line => | ||
def invalidArgument(got: Value): Result[Value] = | ||
RuntimeError.invalidArgument(Types.Exception, typeOf(got), line).fail | ||
|
||
{ | ||
case (exception: DInstance) :: _ => | ||
def message: String = | ||
DException.messageOf(exception).getOrElse("An exception occurred") | ||
|
||
DException.Kind.of(exception).fold(invalidArgument(exception)) { case DivisionByZero.name => | ||
RuntimeError.divisionByZero(message, line).fail | ||
} | ||
case arg :: _ => invalidArgument(arg) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ala/com/melvic/dry/interpreter/Keys.scala → ...melvic/dry/interpreter/natives/Keys.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/scala/com/melvic/dry/interpreter/values/DException.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.melvic.dry.interpreter.values | ||
|
||
import com.melvic.dry.interpreter.Env | ||
import com.melvic.dry.interpreter.values.DException.Kind | ||
import com.melvic.dry.interpreter.values.Value.{Types, typeOf} | ||
import com.melvic.dry.result.Failure.RuntimeError | ||
import com.melvic.dry.result.Result.implicits.ToResult | ||
|
||
class DException(val kind: Kind, val env: Env) extends DClass(kind.name, Map.empty, env) { | ||
override def arity = 1 | ||
|
||
override def call = { | ||
case args @ ((message: Value.Str) :: _) => | ||
super.call(args).flatMap { case instance: DInstance => | ||
instance.addField("exception_type", Value.Str(kind.name)).addField("message", message).ok | ||
} | ||
case arg :: _ => RuntimeError.invalidArgument(s"${Types.String}", typeOf(arg), lineNumber).fail | ||
} | ||
} | ||
|
||
object DException { | ||
sealed trait Kind { | ||
val name: String = this.toString | ||
} | ||
|
||
object Kind { | ||
def of(instance: DInstance): Option[String] = | ||
asString(instance.fields.get("exception_type")) | ||
} | ||
|
||
case object DivisionByZero extends Kind | ||
|
||
def apply(kind: Kind, env: Env): DException = | ||
new DException(kind, env) | ||
|
||
def unapply(exception: DException): Option[(Kind, Env)] = | ||
Some(exception.kind, exception.env) | ||
|
||
def messageOf(exception: DInstance): Option[String] = | ||
asString(exception.fields.get("message")) | ||
|
||
private def asString(value: Option[Value]): Option[String] = | ||
value.flatMap { | ||
case Value.Str(value) => Some(value) | ||
case _ => None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters