#Holberton School Project - interpetor for the Monty language

Description

This is a Holberton School partner project with @jayjay823 and @rdsim8589. This is an interpetor for the monty byte code.The monty byte code are strored .m files that excute a command line by line. The purpuse of this is project to understand the concepts of FIFO (First in First Out) and LIFO (Last In First Out).

##How To Use

$ git clone git@github.com:jayjay823/monty.git

compile everything within the directory that ends with .c

$ gcc -Wall -Werror -Wextra -pedantic *.c -o monty

run the binary file monty with the .m file (only accepts one file at a time)

$ ./monty test.m

##Completed Features

Proper usage

Must pass one file that that exist. if non-existant file passed

$ ./monty no_file.m
>>> Error: Can't open file no_file

if no files passed or more than one file passed

$ ./monty test_1.m test_2.m
>>> USAGE: monty file

Files must contain valid opcode If invalid given

$cat test_1.m
>>> bad_fun
>>> push 2
>>> pall
$ ./monty test_1.m
>>> L1: unknown instruction bad_fun

The default method of storing will be the stack

Avaliable opcodes

op codes Description and Examples
push The opcode push pushes an element to the stack.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pall
output
$ ./monty test_1.m
>>> 1
>>> 2
if push is not followed a negative or postive int, output will be:</br>
<code>L(line_number): usage: push integer<code></br>
</td>
pall The opcode pall prints all the values on the stack, starting from the top of the stack.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pall
output
$ ./monty test_1.m
>>> 1
>>> 2
pint The opcode pint prints the value at the top of the stack, followed by a new line.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pint
output
$ ./monty test_1.m
>>> 2
If stack empty, output will be:</br>
<code>L(line_number): can't pint, stack empty</code>
</td>
pop The opcode pop removes the top element of the stack.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pall
>>> pop
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 1
>>> 1
If stack is empty, output will be</br>
<code>L(line_number): can't pop an empty stack</code></br>
</td>
swap The opcode swap swaps the top two elements of the stack.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pall
>>> swap
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 1
>>> 1
>>> 2
If the stack is less than two elements long, output will be:</br>
<code>L(line_number): can't swap, stack too short</code></br>
</td>
add The opcode add adds the top two elements of the stack. The two elements will be replaced with one element that is the sum of the two elements. The stack is now one element shorter.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pall
>>> add
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 1
>>> 3
If the stack is less than two elements long, output will be: </br>
<code>L(line_number): can't add, stack too short</code></br>
</td>
nop The opcode nop doesn't do anything

Example:
$cat test_1.m
>>> nop
output
$ ./monty test_1.m
sub The opcode sub subtracts the top elements from second top element of the stack. The two elements will be replaced with one element that is the subtraction of the two elements. The stack is now one element shorter.

Example:
$cat test_1.m
>>> push 1
>>> push 2
>>> pall
>>> sub
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 1
>>> -1
If the stack is less than two elements long, output will be: </br>
<code>L(line_number): can't sub, stack too short</code></br>
div The opcode div divides the seconds top element by the top element of the stack. The two elements will be replaced with one element that is the division of the two elements. The stack is now one element shorter.

Example:
$cat test_1.m
>>> push 6
>>> push 2
>>> pall
>>> div
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 6
>>> 3
If the stack is less than two elements long, output will be: </br>
<code>L(line_number): can't div, stack too short</code></br>

If the the top element of the stack is 0, output will be:</br>
<code>L(line_number): division by zero</code></br>
</td>
mul The opcode mul multiplies the second top element of the stack with the top element of the stack. The two elements will be replaced with one element that is the division of the two elements. The stack is now one element shorter.

Example:
$cat test_1.m
>>> push 6
>>> push 2
>>> pall
>>> mul
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 6
>>> 12
If the stack is less than two elements long, output will be:
L(line_number): can't mul, stack too short
</td>
mod The opcode mod computes the rest of the division of the second top element of the stack by the top element of the stack. The two elements will be replaced with one element that is the division of the two elements. The stack is now one element shorter.

Example:
$cat test_1.m
>>> push 6
>>> push 2
>>> pall
>>> mod
>>> pall
output
$ ./monty test_1.m
>>> 2
>>> 6
>>> 0
If the stack is less than two elements long, output will be:</br>
<code>L(line_number): can't mod, stack too short</code></br>

If the the top element of the stack is 0, output will be:</br>
<code>L(line_number): division by zero</code></br>
</td>
# When the first non-space character of a line is #, this line is treated a comment (don't do anything)

Example:
$cat test_1.m
>>> # Nothing will print
output
$ ./monty test_1.m
</td>
pchar The opcode pchar prints the ascii character of element at the top of the stack, followed by a new line.

Example:
$cat test_1.m
>>> push 72
>>> pchar
output
$ ./monty test_1.m
>>> H
If stack is empty, output will be:</br>
<code>L(line_number): can't pchar an empty stack</code></br>

If the value is not in the ascii table (man ascii), output will be:</br>
<code>L(line_number): can't pchar, value out of range</code></br>
</td>
pstr The opcode pstr prints the string starting at the top of the stack. The int values will be treated as ascii value characters. The string will stop when the stack is over, the values of the element is 0 or not in the ascii table.

Example:
$cat test_1.m
>>> push 0
>>> push 65
>>> push 72
>>> pstr
output
$ ./monty test_1.m
>>> HA
If the stack is empty, output will be:</br>
<code> >>> </code></br>

</td>
rotl The opcode rotl rotates the stack to the top. The top element of the stack becomes the last one, and the second top element of the stack becomes the first one.

Example:
$cat test_1.m
>>> push 0
>>> push 1
>>> push 2
>>> push 3
>>> pall
>>> rotl
>>> pall
output
$ ./monty test_1.m
>>> 3
>>> 2
>>> 1
>>> 0
>>> 2
>>> 1
>>> 0
>>> 3
rotr The opcode rotr rotates the stack to the bottom. The last element of the stack becomes the top element of the stack.

Example:
$cat test_1.m
>>> push 0
>>> push 1
>>> push 2
>>> push 3
>>> pall
>>> rotr
>>> pall
output
$ ./monty test_1.m
>>> 3
>>> 2
>>> 1
>>> 0
>>> 0
>>> 3
>>> 2
>>> 1
queue The opcode queue sets the format of the data to a queue (FIFO).

Example:
$cat test_1.m
>>> queue
>>> push 0
>>> push 1
>>> push 2
>>> push 3
>>> pall
output</br>
	<code> $ ./monty test_1.m</code></br>
	<code> >>> 0</code></br>
	<code> >>> 1</code></br>
	<code> >>> 2</code></br>
	<code> >>> 3</code></br>
</td>
stack The opcode stack sets the format of the data to a stack (LIFO). This is the default behavior of the program.

Example:
$cat test_1.m
>>> stack
>>> push 0
>>> push 1
>>> push 2
>>> push 3
>>> pall
output
$ ./monty test_1.m
>>> 3
>>> 2
>>> 1
>>> 0

##Contributors Jay Wang - Github || Twitter || email

Richard Sim - Github || Twitter || email

##Want to be contributor? reach out to any of the Contributors