这是一篇美国的ocaml解释器cs代写
1 Overview
The project is broken down into three parts. Each part is worth 100 points.
You will submit a file named interpreter.ml which contains a function, interpreter, with the following type signature:
interp : string -> string list
2 Functionality
the function will take a program as an input string, and will return list of strings “logged” by the program. A stack is used internally to keep track intermediate evaluation results. Only the string log will be tested during grading, the stack will not be tested.
3 Part 1: Basic Computation
3.1 Grammar
For part 1 you will need to support the following grammar
3.1.1 Constants
digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
int ::= [−] digit {digit}
bool ::= True | False
const ::= int | bool | ()
3.1.2 Programs
prog ::= coms
com ::= Push const | Pop int | Trace int
| Add int | Sub int | Mul int | Div int
coms ::= com {com}
3.1.3 Values
val ::= int | bool | ()
3.2 Errors
In part 1, when an error occurs during interpretation, evaluation must stop immediately and output the exact
log [“Error”].
3.3 Commands
Your interpreter should be able to handle the following commands:
3.3.1 Push
Push const
All kinds of const are pushed to the stack in the same way. Resolve the constant to the appropriate value and add it to the stack.
The program
Push 9
Push True
Push False
Push ()
should result in the stack
()
False
True
9
3.3.2 Pop
The command Pop n removes the top n values from the stack. If n is negative or the stack contains less than n values, terminate evaluation with error.
Example 1
Push True
Push False
Push ()
Pop 1
should result in the stack
False
True
Example 2
Push True
Push False
Push ()
Pop 2
should result in the stack
True
Example 3
Push True
Push False
Push ()
Pop 4
should terminate with the following list of string.
[“Error”]
3.3.3 Trace
The Trace n command consumes the top n values on the stack and adds their string representations to the output list. New entries to the log should be added to the head position.
If n is negative or the stack contains less than n values, terminate with error. For a Trace n command where n > 1, the produced log should be equivalent to n executions of Trace 1 command.
Example 1
Push ()
Push 5
Push 1
Push 2
Trace 1
Trace 1
should result in the stack
5
()
and the output log
[“1”; “2”]
Example 2
Push ()
Push 5
Push 1
Push 2
Trace 2
should result in the stack
5
()
and the output log
[“1”; “2”]
Example 3
Push ()
Push 5
Push 1
Push 2
Trace 2
Trace 2
should result in an empty stack and the output log
[“()”; “5”; “1”; “2”]
3.3.4 Add
Add n consumes the top n values in the stack, and pushes their sum to the stack.
If n is negative or there are fewer than n values on the stack, terminate with error. If the top n values on the stack are not integers, terminate with error. If n is zero, push 0 onto the stack without consuming anything on the stack.
Example 1
Push 5
Push 7
Add 2
Push 3
Add 2
should result in the stack
15
Example 2
Push 5
Add 0
should result in the stack
0
5
Example 3
Push 5
Push 4
Add 1
should result in the stack
4
5