Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.53 KB | None | 0 0
  1.  
  2. object Expressions {
  3.   import Exceptions._
  4.  
  5.   sealed abstract class ExprNodeLabel
  6.   case class INT() extends ExprNodeLabel
  7.   case class BOOL() extends ExprNodeLabel
  8.  
  9.   sealed abstract class ExprEdgeLabel
  10.   case object add extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, INT]
  11.   case object sub extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, INT]
  12.   case object mul extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, INT]
  13.   case object div extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, INT]
  14.   case object lt extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, BOOL]
  15.   case object gt extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, BOOL]
  16.   case object equ extends ExprEdgeLabel with EdgeLabel.Binary[INT, INT, BOOL]
  17.   case object and extends ExprEdgeLabel with EdgeLabel.Binary[BOOL, BOOL, BOOL]
  18.   case object or extends ExprEdgeLabel with EdgeLabel.Binary[BOOL, BOOL, BOOL]
  19.  
  20.   type ExpressionCodeGraph = CodeGraph[ExprNodeLabel, ExprEdgeLabel]
  21.  
  22.   def evaluate(cg: ExpressionCodeGraph, inputs: Seq[Any]): Seq[Any] = {
  23.     import CodeGraphOps._
  24.     var env: Map[CodeGraph.NodeKey, Any] = cg.inputs.zip(inputs).toMap
  25.     for (edgeKey <- cg.depthFirstOrder()) {
  26.       val edge = cg.edge(edgeKey).head
  27.       def args = edge.args.map(x => env.getOrElse(x, throw InvalidNodeKeyException(x)))
  28.       def results = edge.label match {
  29.         case `add` => Seq(args(0).asInstanceOf[Integer] + args(1).asInstanceOf[Integer])
  30.         case `sub` => Seq(args(0).asInstanceOf[Integer] - args(1).asInstanceOf[Integer])
  31.         case `mul` => Seq(args(0).asInstanceOf[Integer] * args(1).asInstanceOf[Integer])
  32.         case `div` => Seq(args(0).asInstanceOf[Integer] / args(1).asInstanceOf[Integer])
  33.         case `lt` => Seq(args(0).asInstanceOf[Integer] < args(1).asInstanceOf[Integer])
  34.         case `gt` => Seq(args(0).asInstanceOf[Integer] > args(1).asInstanceOf[Integer])
  35.         case `equ` => Seq(args(0).asInstanceOf[Integer] == args(1).asInstanceOf[Integer])
  36.         case `and` => Seq(args(0).asInstanceOf[Boolean] && args(1).asInstanceOf[Boolean])
  37.         case `or` => Seq(args(0).asInstanceOf[Boolean] || args(1).asInstanceOf[Boolean])
  38.       }
  39.       env = env ++ edge.results.zip(results).toMap
  40.     }
  41.     cg.outputs.map(x => env.getOrElse(x, throw InvalidNodeKeyException(x)))
  42.   }
  43.  
  44.   object simpleExprCodeGraph extends CodeGraphBuilder[ExprNodeLabel, ExprEdgeLabel] {
  45.     val (x, y) = input(INT, INT)
  46.  
  47.     val sum = add(x, y)
  48.     val prod = mul(x, y)
  49.     val prodMinusSum = sub(prod, sum)
  50.  
  51.     output(prodMinusSum)
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement