OCaml Code: The goal of this project is to understand and build an interpreter for a small, OCaml-like, stackbased bytecode language. Make sure that the code compiles correctly and provide the code with the screenshot of the output. Make sure to have the following methods below: -Push integers, strings, and names on the stack -Push booleans -Pushing an error literal or unit literal will push :error: or :unit: onto the stack, respectively -Command pop removes the top value from the stack -The command add refers to integer addition. Since this is a binary operator, it consumes the top two values in the stack, calculates the sum and pushes the result back to the stack - Command sub refers to integer subtraction -Command mul refers to integer multiplication -Command div refers to integer division -Command rem refers to the remainder of integer division -Command neg is to calculate the negation of an integer -Command swap interchanges the top two elements in the stack, meaning that the first element becomes the second and the second becomes the first -Command toString removes the top element of the stack and converts it to a string -println command pops a string off the top of the stack and writes it, followed by a newline, to the output file that is specified as the second argument to the interpreter function -Command quit causes the interpreter to stop Below are the test cases: input1.txt: push 1 toString println quit input2.txt: push 5 push :true: push :false: push "str1" push str1 push :error: push :unit: toString println toString println toString println toString println toString println toString println toString println quit input3.txt: push 10 push 2 push 8 pop add toString println quit input4.txt: push 6 push 2 push 3 sub add toString println quit input5.txt: push :true: push 7 push 8 push :false: pop mul toString println toString println quit output1.txt: 1 output2.txt: :unit: :error: str1 str1 :false: :true: 5 output3.txt: 12
OCaml Code: The goal of this project is to understand and build an interpreter for a small, OCaml-like, stackbased bytecode language. Make sure that the code compiles correctly and provide the code with the screenshot of the output. Make sure to have the following methods below:
-Push integers, strings, and names on the stack
-Push booleans
-Pushing an error literal or unit literal will push :error: or :unit:
onto the stack, respectively
-Command pop removes the top value from the stack
-The command add refers to integer addition. Since this is a binary operator, it consumes the top
two values in the stack, calculates the sum and pushes the result back to the stack
- Command sub refers to integer subtraction
-Command mul refers to integer multiplication
-Command div refers to integer division
-Command rem refers to the remainder of integer division
-Command neg is to calculate the negation of an integer
-Command swap interchanges the top two elements in the stack, meaning that the first element
becomes the second and the second becomes the first
-Command toString removes the top element of the stack and converts it to a string
-println command pops a string off the top of the stack and writes it, followed by a newline,
to the output file that is specified as the second argument to the interpreter function
-Command quit causes the interpreter to stop
Below are the test cases:
input1.txt:
push 1
toString
println
quit
input2.txt:
push 5
push :true:
push :false:
push "str1"
push str1
push :error:
push :unit:
toString
println
toString
println
toString
println
toString
println
toString
println
toString
println
toString
println
quit
input3.txt:
push 10
push 2
push 8
pop
add
toString
println
quit
input4.txt:
push 6
push 2
push 3
sub
add
toString
println
quit
input5.txt:
push :true:
push 7
push 8
push :false:
pop
mul
toString
println
toString
println
quit
output1.txt:
1
output2.txt:
:unit:
:error:
str1
str1
:false:
:true:
5
output3.txt:
12
output4.txt:
5
output5.txt:
56
:true:
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 12 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)