Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this Program in Scala: First, you learn how to configure and run your Scala project with the code that I wrote in

Please help with this Program in Scala:

First, you learn how to configure and run your Scala project with the code that I wrote in class. You will add the logic for assigning results of arithmetic computations to variables, which will later be used in the same or other expressions. Next, you will create an implementation of scopes, named and anonymous with scoping rules for obscuring and shadowing that you define to resolve the values of variables that have the same names in expressions. Fourth, you will create macros to substitute macro definitions for the used macro names in expressions. Finally, you will create Junit or Flatspec tests to verify the correctness of your implementation. Finally, explain your implementation and the semantics of your language.

code:

object Main extends App {
// (3+2)*(4*7)
type Environment = Map[String,Int]
type EvaluateEnvironmentFunction = Environment ?=> Int
given env as Environment = Map("peter"->3, "zach"->7)
enum Expression:
case Value(v:Int)
case Variable(name:String)
case Add(o1:Expression, o2:Expression)
case Multiply(o1:Expression, o2:Expression)
import Main.Expression._
val result:Expression = Multiply(Add(Variable("zach"),Value(2)), Multiply(Value(4),Value(7)))
def eval(expression: Expression):EvaluateEnvironmentFunction = expression match {
case Value(v) => v
case Variable(someName) => summon[Environment].getOrElse(someName, throw Exception(s"undefined variable $someName"))
case Add(o1, o2) => eval(o1) + eval(o2)
case Multiply(o1, o2) => eval(o1) * eval(o2)
}
println(result)
println(eval(result))
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

=+b) What is the probability that he does turn off his phone?

Answered: 1 week ago

Question

Do you currently have a team agreement?

Answered: 1 week ago

Question

How will the members be held accountable?

Answered: 1 week ago