#Holberton School Project - interpetor for the Monty language
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
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
op codes | Description and Examples |
push |
The opcode push pushes an element to the stack. Example:
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
# |
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
|
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
|
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
|
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
|
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