OCaml Code: I need help with writing the push and pop statements in order to create an interpreter for OCaml. I need help with writing the print statement as well. The 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. In the case that the top element on the stack is not a string, it should be returned to the stack and an :error: pushed. If the stack is empty, an :error: shall be pushed. Below is the unfinished code. Make sure to use the test cases as well and show the screenshots of the code passing the test cases. Attached is info. for push and pop. interpreter.ml type stackValue = BOOL of bool | INT of int | ERROR | STRING of string | NAME of string | UNIT type command = ADD | SUB | MUL | DIV | PUSH of stackValue | POP of stackValue | REM | NEG | TOSTRING | SWAP | PRINTLN | QUIT let interpreter (input, output) = let ic = open_in input in let oc = open_out output in let rec loop_read acc = try let l = String.trim(input_line ic) in loop_read (l::acc) with | End_of_file -> List.rev acc in let strList = loop_read [] in let str2sv s = match s with | ":true:" -> BOOL true | ":false:" -> BOOL false | ":error:" -> ERROR | ":unit:" -> UNIT | _-> let rec try_parse_int str = try INT (int_of_string str) with | Failure _ -> try_parse_name str and try_parse_name str= NAME str in try_parse_int s in letstr2com s = match s with | "Add" -> ADD | "Sub" -> SUB | "Mul" -> MUL | "Div" -> DIV | "Swap" -> SWAP | "Neg" -> NEG | "Rem" -> REM | "ToString" -> TOSTRING | "Println" -> PRINTLN | "Quit" -> QUIT | _ -> PUSH (str2sv s) in let com2str c = match c with | "Add" -> ADD | "Sub" -> SUB | "Mul" -> MUL | "Div" -> DIV | "Swap" -> SWAP | "Neg" -> NEG | "Rem" -> REM | "ToString" -> TOSTRING | "Println" -> PRINTLN | "Quit" -> QUIT | _ -> PUSH (str2sv c) in let sv2str sv = match sv with | BOOL b -> string_of_bool b | INT i -> string_of_int i | ERROR -> ":error:" | STRING s -> s | NAME n -> n in let comList = List.map str2com strList in let processor cl stack = match (cl, stack) with | (ADD::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a+b)::restOfStack) | (ADD::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (SUB::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a-b)::restOfStack) | (SUB::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (MUL::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a*b)::restOfStack) | (MUL::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (DIV::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a/b)::restOfStack) | (DIV::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (REM::restOfCommands, INT(a)::INT(b)::restOfStack) -> processor restOfCommands ( INT(a%b)::restOfStack) | (REM::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (SWAP::restOfCommands, x::y::restOfStack) -> processor restOfCommands y::x::restOfStack | (SWAP::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (NEG::restOfCommands, INT(a)::restOfStack) -> processor restOfCommands ( INT(-a)::restOfStack) | (NEG::restOfCommands, s) -> processor restOfCommands (ERROR::stack) | (TOSTRING::restOfCommands, x::restOfStack) -> processor restOfCommands ( (sv2str x)::restOfStack) | (TOSTRING::restOfCommands, s) -> processor restOfCommands (ERROR::stack) in processor comList [] Test cases: input1.txt: push 1 toString println quit output1.txt: 1
OCaml Code: I need help with writing the push and pop statements in order to create an interpreter for OCaml. I need help with writing the print statement as well. The 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. In the case that the top element on the stack is not a string, it should be returned to the stack and an :error: pushed. If the stack is empty, an :error: shall be pushed.
Below is the unfinished code. Make sure to use the test cases as well and show the screenshots of the code passing the test cases. Attached is info. for push and pop.
interpreter.ml
input1.txt:
push 1
toString
println
quit
output1.txt:
1
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 12 images
I ran the code and used the test cases and this is the error I got when I compiled my code. Attached is an image of the error. Don't reject the question. This is NOT a graded work. Please fix the code and show the screenshots of the code passing the test cases.