mitrakov

Nihirash Interpreter

Mar 30th, 2021
455
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // by Nihirash
  2. object Interpreter {
  3.   type Result[T] = Either[Throwable, T]
  4.  
  5.   sealed trait Operations
  6.   final case class Log(message: String) extends Operations
  7.   final case class DoSomething(what: String) extends Operations
  8.  
  9.   sealed trait Expression[T] {
  10.     def eval(state: Seq[Operations])(implicit env: Map[String, Long]): T
  11.     def print(): String
  12.   }
  13.  
  14.   sealed trait Operation extends Expression[Seq[Operations]]
  15.   sealed trait NumbericExpression extends Expression[Long]
  16.   sealed trait BooleanExpression extends Expression[Boolean]
  17.  
  18.  
  19.   final case class Do(block: Seq[Operation]) extends Operation {
  20.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Seq[Operations] = {
  21.       @scala.annotation.tailrec
  22.       def intEval(intState: Seq[Operations], acc: Seq[Operation]) : Seq[Operations] = acc match {
  23.         case h :: t =>
  24.           intEval(h.eval(intState), t)
  25.         case Nil => intState
  26.       }
  27.  
  28.       intEval(state, block)
  29.     }
  30.  
  31.     override def print(): String =
  32.       s"{ \n${block.map(_.print()).mkString("\n")}\n}"
  33.   }
  34.  
  35.   final case class Condition(cond: BooleanExpression, body: Operation, alter: Operation) extends Operation {
  36.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Seq[Operations] =
  37.       if (cond.eval(state))
  38.         body.eval(state)
  39.     else
  40.         alter.eval(state)
  41.  
  42.     override def print(): String =
  43.       s"If (${cond.print()})\nТО: ${body.print()}\nelse: ${alter.print()}"
  44.   }
  45.  
  46.   final case class OperationLog(message: String) extends Operation {
  47.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Seq[Operations] =
  48.       state :+ Log(message)
  49.  
  50.     override def print(): String =
  51.       s"""Log: "${message}""""
  52.   }
  53.  
  54.   final case class OperationDoSomething(what: String) extends Operation {
  55.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Seq[Operations] =
  56.       state :+ DoSomething(what)
  57.  
  58.     override def print(): String =
  59.       s"Do ${what}"
  60.   }
  61.  
  62.   final case class Constant(value: Long) extends NumbericExpression {
  63.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Long =
  64.       value
  65.  
  66.     override def print(): String = value.toString
  67.   }
  68.  
  69.   final case class Sum(lvalue: NumbericExpression, rvalue: NumbericExpression) extends NumbericExpression {
  70.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Long =
  71.       lvalue.eval(state) + rvalue.eval(state)
  72.  
  73.     override def print(): String =
  74.       s" ${lvalue.print()} + ${rvalue.print()} "
  75.   }
  76.  
  77.   final case class GetEnvValue(key: String) extends NumbericExpression {
  78.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Long =
  79.       env.getOrElse(key, 0L)
  80.  
  81.     override def print(): String = key
  82.   }
  83.  
  84.   final case class NumbericEquals(lexpr: NumbericExpression, rexpr: NumbericExpression) extends BooleanExpression {
  85.     override def eval(state: Seq[Operations])(implicit env: Map[String, Long]): Boolean =
  86.       lexpr.eval(state) == rexpr.eval(state)
  87.  
  88.     override def print(): String =
  89.       s" ${lexpr.print()} = ${rexpr.print()} "
  90.   }
  91.  
  92. }
  93.  
  94. object Hello extends  App {
  95.   import Interpreter._
  96.   val program = Do(Seq(
  97.     OperationLog("Hello, world"),
  98.     Condition(NumbericEquals(Constant(1), Sum(GetEnvValue("first"), Constant(2))),
  99.       Do(Seq(
  100.         OperationLog("Success"),
  101.         OperationDoSomething("Make changes")
  102.       )),
  103.       OperationDoSomething("ConditionFailed")
  104.   )))
  105.  
  106.   implicit val env = Map[String, Long]("first" -> -1)
  107.  
  108.   println(s"Operations: ${program.eval(Seq.empty)}")
  109.   println(s"Algorithm: \n${program.print()}")
  110. }
  111.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×