Question: Help with Scala. Note: Can't use var or while/for loops. Will rate, thanks. The scala definitions are given below. In [ ] : 1 2

Help with Scala. Note: Can't use var or while/for loops. Will rate, thanks.

Help with Scala. Note: Can't use var or while/for loops. Will rate,thanks. The scala definitions are given below. In [ ] : 1

2 3 4 5 6 7 sealed trait Expr case class Const(d:

The scala definitions are given below. In [ ] : 1 2 3 4 5 6 7 sealed trait Expr case class Const(d: Double) extends Expr case class Ident(s: String) extends Expr case class Plus (el: Expr, e2: Expr) extends Expr case class Mult(el: Expr, e2: Expr) extends Expr case class Let(id: String, el: Expr, e2: Expr) extends Expr case class MultiLet(id: List[String], eList: List[Expr], eBody: Expr) extends Expr Semantics for MultiLet Let us write down the semantic rules for a multilet statement: n> 1, eval(e1, env) = V1, V1 # error, newenv = enu o {x1 H 01} eval(MultiLet([x1...,xn), [e1,...,en), eBody), env) = eval(MultiLet([x2,..., xn], [e2, en], eBody), newenv) (multilet-non-empty) Interpreter for MultiLet Statements Implement an interpreter for the lettuce language with multi-let statements. Your interpreter does not need to "propagate" error: instead you should throw an IllegalArgumentException whenever an error is encountered. Style Guide Use of var/while/for loops in your solution below is disallowed. 1 sealed trait Value 2 case class NumValue(f: Double) extends Value 3 case object Error extends Value /* -- Do not return Error -- simply throw an new IllegalArgumentException whenever 5 type Environment = Map[ String, value] 6 7 def evalExpr(e: Expr, env: Environment): Value = { 8 9 e match 10 case Const(f) => NumValue(f) 11 case Ident(x) => { 12 if (env.contains (x)) { 13 env(x) 14 } else { 15 throw new IllegalArgumentException ("Not found identifier") 16 } 17 } 18 case Plus(el, e2) => { 19 val vl = evalExpr(el, env) 20 val v2 = evalExpr(e2, env) 21 (vi, v2) match { 22 case (NumValue(f1), NumValue(f2)) => NumValue(f1 + f2) 23 case - => throw new IllegalArgumentException("plus failed") 24 } 25 } case Mult(el, e2) => { val vl = evalExpr(el, env) val v2 = evalExpr(e2, env) (vl, v2) match { case (NumValue(fl), Numvalue(f2)) => NumValue(f1 + f2) case => throw new IllegalArgumentException ("mult failed") } } case Let(x, el, e2) => { // YOUR CODE HERE ??? } case MultiLet(xList, eList, eBody) => { // YOUR CODE HERE ??? } } } //BEGIN TEST let (x, y) = (10, 20) in let x = y in x + x *y val x = Ident("x") val y = Ident("y") val let1 = Let("x", y, Plus (x, Mult(x, y)) ) val mletl = MultiLet( List("x", "Y"), List(Const(10.0), Const (20.0)), leti) val v = evalExpr (mleti, Map.empty) assert (v == NumValue ( 420.0), s"Test I failed expected: NumValue ( 420.0), obtained $v") passed (6) //END TEST

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!