Homework 5
- Reimplement Homework 1 in Scala.
Expr becomes an abstract class with an undefined method def value: T
- Use case classes for
Const, Sum, Product
Rand and Read are objects
- Use
Seq[T] => T instead of the Function interface
- Use varargs for the
Sum, Product, and Op.
- Put all your classes into an object
hw5part1 in a file src/main/scala/hw5part1.scala.
- Here is a test suite.
- Make an object
hw5part2 and copy your definitions of Expr, Const, Op, Sum, Product. (We don't need Rand and Read.)
- Add a case class
Var, like Variable in the lab of lecture 8, but make it generic.
- Rename the
value method in Expr[T] to eval, with a parameter symbols: Map[String, T], like in the lab. If a variable doesn't exist in the symbol table, it's ok to throw an exception.
- Define
Def like Definition in the lab, but make it generic. Define eval[T](d: Def[T], symbols: Map[String, T])
- Here is a test suite.
- Make a file
hw5part3.scala. Include the SimpleLanguageParser and eval function from the Lecture 9 lab. In the grammar, allow variables (which you can parse with ident from JavaTokenParsers). Add a parameter symbols: Map[String, Int] to eval for evaluating such variables.
- Here is a test suite.