Write an evaluator for a program (list of statements + return expression) that evaluates the program...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write an evaluator for a program (list of statements + return expression) that evaluates the program and returns the final value of the return expression after the statements in the program are run one by one. We explain how to evaluate a program step by step on an example program x15 //Stmtl yx 10 // Stmt 2 Z y5 // Stmt 3 x+y+ z // Return expression This program should return the value 30. The key idea is this: a) Start with an empty environment map to begin with. b) Execute each statement in the list of statements from beginning to end. When you execute the statement, do so with the current map and the return value is the next map. c) Evaluate the return expression on the final map after you have executed all the statements. Complete the code below. You can use loops and var for now. We will show you how to avoid it later. Here is how you iterate through each element of a list in scala: for (item <1st) { // Do stuff with item } (): def evalProgram (p: Program): Double = p match { case CalcProgram (1stOfStatement, returnExpr) => { // YOUR CODE HERE } ??? } ]://BEGIN TEST val v1 val v2 Assignment ("x", Const(2.0)) Assignment("y", Plus (Var("x"), Const(4.0))) val v3 = Assignment ("z", Var("y")) val pl = CalcProgram (List (v1, v2, v3), Var("z")) print (evalProgram (pl)) assert (evalProgram (pl) == 6) passed (5) //END TEST ]://BEGIN TEST val vvl Assignment("x", Const(2.0)) val vv2 = Assignment ("y", Star (Var("x"), Var("x"))) val vv3 = Assignment ("z", Star (Var("y"), Var("y"))) Assignment ("w", Star (Var("z"), Var("z"))) val vv4 val p2 CalcProgram (List (vvl, vv2, vv3, vv4), Var("w")) print (evalProgram (p2)) assert (evalProgram (p2) passed(5) //END TEST == 256) Write an evaluator for a program (list of statements + return expression) that evaluates the program and returns the final value of the return expression after the statements in the program are run one by one. We explain how to evaluate a program step by step on an example program x15 //Stmtl yx 10 // Stmt 2 Z y5 // Stmt 3 x+y+ z // Return expression This program should return the value 30. The key idea is this: a) Start with an empty environment map to begin with. b) Execute each statement in the list of statements from beginning to end. When you execute the statement, do so with the current map and the return value is the next map. c) Evaluate the return expression on the final map after you have executed all the statements. Complete the code below. You can use loops and var for now. We will show you how to avoid it later. Here is how you iterate through each element of a list in scala: for (item <1st) { // Do stuff with item } (): def evalProgram (p: Program): Double = p match { case CalcProgram (1stOfStatement, returnExpr) => { // YOUR CODE HERE } ??? } ]://BEGIN TEST val v1 val v2 Assignment ("x", Const(2.0)) Assignment("y", Plus (Var("x"), Const(4.0))) val v3 = Assignment ("z", Var("y")) val pl = CalcProgram (List (v1, v2, v3), Var("z")) print (evalProgram (pl)) assert (evalProgram (pl) == 6) passed (5) //END TEST ]://BEGIN TEST val vvl Assignment("x", Const(2.0)) val vv2 = Assignment ("y", Star (Var("x"), Var("x"))) val vv3 = Assignment ("z", Star (Var("y"), Var("y"))) Assignment ("w", Star (Var("z"), Var("z"))) val vv4 val p2 CalcProgram (List (vvl, vv2, vv3, vv4), Var("w")) print (evalProgram (p2)) assert (evalProgram (p2) passed(5) //END TEST == 256)
Expert Answer:
Answer rating: 100% (QA)
scala def evaluateProgramstatements ListString returnExpression String Int var environme... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
A physics student is studying the effect of temperature on the resistance of a current carrying wire. She applies a voltage to a iron wire at a temperature of 60.0C and notes that it produces a...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
The reconciling item in a bank reconciliation that will result in an adjusting entry by the depositor is: (a) outstanding checks. (b) deposit in transit. (c) a bank error. (d) bank service charges.
-
A typical white-dwarf star, which once was an average star like our Sun but is now in the last stage of its evolution, is the size of our Moon but has the mass of our Sun what is the surface gravity...
-
Assets (ROA) and price to earnings (PE) ratio for a company you are familiar with. What do these metrics tell you about the financial health of the company? What is the economic and market factors...
-
Consider a European Call option with strike \(K\) and time to expiration \(T\). Denote the price of the call for \(C(S, T)\) and let \(B(T)\) the price of one unit of a zero coupon bond maturing at...
-
Rework in job costing, journal entry (continuation of 18-35). Assume that the 5 spoiled units of Whitefish Machine Shops Job #10 can be reworked for a total cost $1,800. A total cost of $5,000...
-
6. The terms of an infinite geometric series are given by a = 5(0.2) 3*-*. Show all work to find: a) the first term of the series. b) the common ratio of the series, rounded to four decimal places if...
-
Apollo Shoes is an audit case designed to introduce you to the entire audit process, from planning the engagement to drafting the final report. You are asked to assume the role of a veteran of...
-
3. Find the area of the region enclosed by the graphs y = 2 cos x and y = x2 1. 4. Find the area of the region between y = sec? x and y = sin x from x = 0 to x = T/4.
-
A particle carrying \(3.00 \mathrm{nC}\) of positive charge is at the origin of a rectangular coordinate system, and a particle carrying \(3.00 \mathrm{nC}\) of negative charge is on the \(x\) axis...
-
Determine the center of mass of a pool cue whose diameter decreases smoothly from \(40 \mathrm{~mm}\) to \(10 \mathrm{~mm}\) over its \(1.40-\mathrm{m}\) length (Figure P6.44). Assume that the cue is...
-
Although consumption of carbonated beverages has been decreasing, it is still an $81 billion industry in North America, which far exceeds alternatives such as water ($23 billion) and sports drinks...
-
Create chronological and functional rsums to highlight your key selling points.
-
Describe a firm that still uses a lot of television/print advertising. a. Is its marketing spending ineffective? b. Why do you think the firm still allocates resource to this area? c. How would you...
-
Methane, ethane, propane, and butane are gases at room temperature, while pentane, hexane, and heptane are liquids. In terms of ldf, why does boiling point of hydrocarbons increase as more carbons...
-
A Firm intends to invest some capital for a period of 15 years; the Firm's Management considers three Options, each consisting of purchasing a machinery of a specific brand, different for each...
-
After closing time, the store manager would like to know how much business was transacted during the day. Modify the CashRegister class to enable this functionality. Supply methods getSalesTotal and...
-
What is the value of each variable after the if statement? a. int n = 1; int k = 2; int r = n; if (k < n) { r = k; } b. int n = 1; int k = 2; int r; if (n < k) { r = k; } else { r = k + n; } c. int n...
-
What happens if an exception does not have a matching catch clause?
-
Universities are interested in increasing donations from their alumni. Donations from former students help fund important building projects, upgrade existing facilities, support student...
-
Figure P16.36 shows the two lowest resonances recorded in the vocal tract of the eastern towhee, a small songbird. a. Is this birds vocal tract better modeled as an open-open tube or an open-closed...
-
Two loudspeakers, 1.0 m apart, emit sound waves with the same frequency along the positive x-axis. Victor, standing on the axis to the right of the speakers, hears no sound. As the frequency is...
Study smarter with the SolutionInn App