CS代写 CS 320: Language Interpreter Design Part 1

CS 320: Language Interpreter Design Part 1

Part 1 Due: November 11th 11:59pm EST

Copyright By PowCoder代写 加微信 powcoder

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:

interpreter : string -> string -> unit

2 Functionality

The function will take a program as an input string, consisting of the program lines, and will take in the output
file path as the second parameter, and will return a unit, as your evaluated output will be logged into a file.

Because the function evaluates a stack-based programming language, a stack is used internally throughout
the interpreter to keep track of intermediate evaluation results. Note that the stack will not be checked, but
rather what is written in the output file — the stack and the output file are two different things.

The autograder will check your output file’s content, and more information on the output file will be described

3 Part 1: Basic Computation
Due Date: November 11th 11:59pm EST

3.1 Grammar

For part 1 you will need to support the following grammar

3.1.1 Constants

::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

::= [−] {}

::= a | b | c | … | y | z | A | B | C | … | Y | Z (aka the alphabet)

::= “{}”

::= |

3.1.2 Programs

::= Quit | Push | Pop | Add | Sub | Mul | Div | Swap | Neg | Concat

::= |

3.2 Errors

In part 1, when an error occurs during interpretation, evaluation must stop immediately and output the exact
log “Error” into the provided output file (see 3.3.1 for more information about the output file).

3.3 Commands

Your interpreter should be able to handle the following commands:

3.3.1 Quit

This command causes the interpreter to stop and does not have to be on the last line of the program. When
called, the created stack should be written out to an output file that is specified as the second argument to the
main function, and no commands after the Quit call get evaluated.

should result in the following stack and the following contents in the output file:

Push “candy”

should result in the following stack and the following contents in the output file:

Push “candy”

should result in the stack

and a file with nothing inside (this will be checked)

3.3.2 Push

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 “abc”

should result in the following stack and the following contents in the output file:

The command Pop removes the top element from the stack. If the stack contains less than 1 element, terminate
evaluation with error.

should result in the following stack and the following contents in the output file:

should result in the following stack and the following contents in the output file:

Add consumes the top 2 values in the stack, and pushes their sum to the stack. If there are fewer than 2 values
on the stack, terminate with error. If not all of the top 2 values on the stack are not integers, terminate with error.

should result in the following stack and the following contents in the output file:

Push “hello”

should result in the following stack and the following contents in the output file:

Push “bottle”
Push “water”

should result in the following stack and the following contents in the output file:

Sub consumes the top 2 values on the stack, and pushes the difference between the top value and the second
top value to the stack.

If there are fewer than 2 values on the stack, terminate with error. If the top 2 values on the stack are
not integers, terminate with error.

Push “test”

should result in the following stack and the following contents in the output file:

Push “test”

should result in the following stack and the following contents in the output file:

Push “choco”

should result in the following stack and the following contents in the output file:

3.3.6 consumes the top 2 values in the stack, and pushes their product to the stack.

If there are fewer than 2 values on the stack, terminate with error. If the top 2 values on the stack are
not integers, terminate with error.

should result in the following stack and the following contents in the output file:

Push “laddoo”

should result in the following stack and the following contents in the output file:

Push “coffee”
Push “cream”

should result in the following stack and the following contents in the output file:

Div consumes the top 2 values on the stack, and pushes the quotient between the top value and the second top
value onto the stack.

If there are fewer than 2 values on the stack, terminate with error. If the second value is 0, terminate with
error. If the top 2 values on the stack are not integers, terminate with error.

should result in the following stack and the following contents in the output file:

Push “samosa”

should result in the following stack and the following contents in the output file:

should result in the following stack and the following contents in the output file:

3.3.8 Swap

Swap takes the top 2 elements in the stack and swaps their order. If there are less than 2 items in the stack,
terminate with error

Push “hello”
Push “world”

should result in the following stack and the following contents in the output file:

Push “320”
Push “cas”

should result in the following stack and the following contents in the output file:

should result in the following stack and the following contents in the output file:

Neg would negate the top element on the stack. If the top element in the stack is not an integer, terminate with
error. If the stack is empty, terminate with error.

should result in the following stack and the following contents in the output file:

Push “kenny”

should result in the following stack and the following contents in the output file:

should result in the following stack and the following contents in the output file:

3.3.10 consumes the top 2 values on the stack, and pushes the concatenation between the top value and the
second top value onto the stack.

If there are fewer than 2 values in the stack, terminate with error. If the top 2 elements in the stack are
not of string type, then terminate with error.

Push “lemon”
Push “laddoo”

should result in the following stack and the following contents in the output file:

“laddoolemon”

Push “peanut”

should result in the following stack and the following contents in the output file:

Push “chocolate chip”
Push “cookie”

should result in the following stack and the following contents in the output file:

Functionality
Part 1: Basic Computation Due Date: November 11th 11:59pm EST

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com