Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The verifier script is shown below. Please make sure it works with the verifier and the test cases shown in the second screenshot. The expected

The verifier script is shown below. Please make sure it works with the verifier and the test cases shown in the second screenshot. The expected results are shown at the bottom. If you do, I will be sure to upvote :)

import Stacklang2 {- Test Cases for Part 1 Stack Language 2 Test by loading hw4p2verifier.hs Call function runAllTests Results Test 1: Just [Right 36] Test 2: Just [Right 1,Left True,Left True,Right 3] Test 3: Nothing Test 4: Nothing Test 5: Just [Right 9,Right 5,Right 7,Right 9] Test 6: Just [Right 20] Test 7: Just [Right 63,Right 63,Right 9] Test 8: Just [Right 4,Right 5,Right 7,Right 9] Test 9: Just [Right 10] Test 10: Just [Right 100,Right 100] Test 11: Just [Right 15] -}

s1 :: Stack s1 = [Right 1,Right 3, Right 5, Right 7, Right 9] s2 :: Stack s2 = [Left True, Right 3] t1 = [LDI 3,DUP,ADD,DUP,MULT] t2 = [LDB True, DUP, IFELSE [LDI 1][LDI 0]] t3 = [LEQ] t4 = [ADD, ADD, MULT, DUP] t5 = [LEQ, IFELSE [] [], LDI 9] t6 = [LDI 5, LDI 2, LEQ, IFELSE [LDI 10, DUP] [], ADD] t7 = [LDI 5, LDI 7, LEQ, IFELSE [LDI 10, DUP] [], ADD] t8 = [LDI 5, LDI 1, LEQ, IFELSE [LDB True, IFELSE [LDI 10] [LDI 100]] [LDI 20, DUP]] t9 = [LDI 5, LDI 1, LEQ, IFELSE [LDB False, IFELSE [LDI 10] [LDI 100]] [LDI 20, DUP], DUP] t10 = [LDI 1, LDI 2, LDI 3, LDI 4, LDI 5, ADD, ADD, ADD, ADD]

tList = [t1, t2, t3, t4, t5, t6, t4, t7, t8, t9, t10] sList = [[], s2, s2, [], s1, [], s1, s1, [], [], []]

runAll :: [Stack] -> [Prog]-> Int -> String runAll [] _ _= " " runAll _ [] _ = " " runAll (s:ss) (p:ps) n = "Test "++show(n)++": "++show(run p s) ++ " " ++ (runAll ss ps (n+1)) runAllTests :: IO () runAllTests = putStrLn (runAll sList tList 1)

image text in transcribed

image text in transcribed

Part 2: Stack Language 2 A stack language with two types (integers and booleans), comparisons and if-else commands. To the Stack language in Part 1, add/modify the following commands/operations - LDI Int - loads its integer parameter onto the stack (replaces LD) - LDB Bool-loads its boolean parameter onto the stack. - ADD - same as with Stack Language 1 only adds integers - MULT - same as with Stack Language 1 only multiplies integers - DUP - places a copy of the stack's topmost element on the stack. If the stack is empty then DUP produces an error. Works with both integer and Boolean values. - LEQ - removes the top integer from the stack, removes the second integer from the stack. If top second the True is pushed on the stack else False is pushed onto the stack. - IFELSE Prog Prog - removes the top of the stack, if the value is true, then run the first program, else run the second program. If the values id not a Boolean then this is an error. Here is a definition of the abstract syntax and type definitions that you should use. type Prog =[Cmd] type Stack =[ Either Bool Int ] data Cmd = LDI Int | LDB Bool | LEQ | ADD | MULT | DUP | IFELSE Prog Prog deriving Show run :: Prog Stack Maybe Stack Since the stack can contain two types use the Either with Bool on the "Left" and Int on the "Right". A stack containing the integers 1,2 and 3 would be defines as [Right 1, Right 2, Right 3] and a stack with a true and the integer 6 would be [Left True, Right 6]. Again the command run will return a Maybe stack. Submit a file named Stacklang2.hs. The first line of the code should be module Stacklang2 where. You can use the following S2-programs to test Stack Lang 2. stack1 :: Stack stack1 = [Right 1, Right 3, Right 5, Right 7, Right 9] stack 2 :: Stack stack 2=[ Left True, Right 3] test1 =[LDI 3, DUP, ADD, DUP, MULT] test2 =[LDB True, DUP, IFELSE [LDI 1 ][ LDI 0]] test3 =[LEQ] test 4=[ADD,ADD, MULT, DUP ] test5 = [LEQ, IFELSE [] [], LDI 9] test6 =[ LDI 5, LDI 2, LEQ, IFELSE [LDI 10, DUP] [], ADD] test7 =[ LDI 5, LDI 7, LEQ, IFELSE [LDI 10, DUP ] [LDI 20, DUP], ADD] test8=[LDI 1, LDI 2, LDI 3, LDI 4, LDI 5, ADD, ADD, ADD, ADD] *Main> run test1 [] Just [Right 36] *Main> run test2 [] Just [Right 1, Left True] *Main> run test3 stack1 Just [Left True, Right 5, Right 7, Right 9] *Main> run test4 stack1 Just [Right 63,Right 63, Right 9] *Main> run test4 stack2 Nothing *Main> run test 5 stack1 Just [Right 9, Right 5, Right 7, Right 9] Main> run test6 [] Just [Right 20] Main> run test7 [] Just [Right 40] Main> run test8 [] Just [Right 15]

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

More Books

Students also viewed these Databases questions

Question

LO3 Discuss the steps of a typical selection process.

Answered: 1 week ago