Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started