• Scala
  • 50 lines
  • 19 views
  • Pasted by anonymous on August 21, 2012
sealed abstract class ArithExpr {
  def eval(env: Env): Double
  def exprString: String
}

case class Const(value: Double) extends ArithExpr {
  override def eval(env: Env) = value
  override def exprString = value.toString
}

case class Var(name: String) extends ArithExpr {
  override def eval(env: Env) =  env.lookup(name) match {
    case Some(d) => d
    case None => throw new IllegalArgumentException("Env " + env + " does not contain a binding for " + name)
  }
  override def exprString = name
// } // <-- MISSING THIS BRACE

sealed abstract class Env {
  def lookup(name: String): Option[Double]
  def cons(vn: String, v: Double) = ConsEnv(vn, v, this)
}

case object EmptyEnv extends Env {
  override def lookup(name: String) = None
}

case class ConsEnv(varName: String, value: Double, rest: Env) extends Env {

  override def lookup(name: String) = 
    if (name == varName) Some(value)
    else rest.lookup(name);
}

/* SCALAC OUTPUT

solution2.scala:7: error: Missing closing brace `}' assumed here
  override def eval(env: Env) = value
  ^
solution2.scala:7: error: expected start of definition
  override def eval(env: Env) = value
           ^
solution2.scala:8: error: expected start of definition
  override def exprString = value.toString
           ^
solution2.scala:9: error: eof expected but '}' found.
}
^

*/

Did you know? CLOSE

  • There are keyboard shortcuts!
    • When Creating A Paste
      • ALT+P Toggle Private
      • CTRL+Enter Create Paste
      • ALT+W Toggle word wrap
    • When Viewing A Paste
      • ALT+G Go to a line
      • ALT+CTRL+E Edit the paste
      • ALT+R Show the raw code
  • There are URL options!
    • When Creating A Paste
      • ?lang=Javascript to default to javascript
    • When Viewing A Paste
      • #L-N Jump to line number N
?