This project is about creating an intepreter that interprets and compiles Monty bytecode files.
Our Monty interpreter has been tested on Ubuntu 14.05.5 LTS
Tests done in VirtualBox on Ubuntu via Vagrant(1.9.1)
Once cloned over, the repository will contain the following files:
File | Decription |
---|---|
monty.h | header file with prototypes |
main.c | contains main function to Monty |
find_opcode.c | function pointer to find correct function |
manipulate_stack_opcodes.c | contains functions that manipulate the stack (pop, swap, nop, rotl, rotr) |
math_stack_opcodes.c | contains functions that perform math operations on node data (add, sub, mul, div, mod) |
print_stack_opcodes.c | contains functions that print elements of the list (pall, pint, pchar, pstr, prev) |
push_stack_opcodes.c | contains functions related to push node (push to beginning or end, check mode, switch mode) |
stack_helper_funcs.c | contains helper functions (tokenize, list length, free list, is_int, is_empty) |
brainfuck | subdirectory with brainfuck programs |
- monty.h - Header file that includes prototypes and structs
- main.c - File includes the main monty function
main()
- initializes global variable, opens file for reading, uses a loop to read file line by line
- find_opcode.c - Function to find correct function pointer depending on opcode
find_opcode()
- uses array of instruction_t structs to find appropriate function pointer depending on opcode
- manipulate_stack_opcodes.c - Functions that manipulate the stack:
pop()
- removes the top element of the stackswap()
- swaps the top two elements of the stacknop()
- does nothingrotl()
- rotates the stack to the top, top element becomes the last one, and second element becomes the first onerotr()
- rotates the stack to the bottom, last element becomes the top element of the stack
- math_stack_opcodes.c - Functions that perform math operations on node data:
add()
- adds the top two elements of the stack, stores result in second element, and top element is removedsub()
- subtracts the top element from the second element, stores result in second element, and top element is removed_div()
- divides second element by the top element, stores result in second element, and top element is removedmul()
- multiplies the top two elements of the stack, stores result in second element, and top element is removedmod()
- computes the rest of the division of the second top element of the stack by the top element of the stack, stores result in second element, and top element is removed
- print_stack_opcodes.c - Functions that print elements of the list:
pall()
- prints all the values on the stack, starting from the top of the stackpint()
- prints the value at the top of the stack, followed by a new linepchar()
- prints the char at the top of the stack, followed by a new linepstr()
- prints the string starting at the top of the stack, followed by a new lineprev()
- prints all the elements of a list in reverse
- push_stack_opcodes.c - Functions related to push node:
push_node()
- adds new node to the beginning of doubly linked list (stack mode)push_node_mode()
- checks what mode we are using (stack or queues) and calls the appropriate push functionpush_node_end()
- function that adds new node to the end of doubly linked list (queue mode)stack()
- sets the format of the data to a stack (LIFO)queue()
- sets the format of the data to a queue (FIFO)
- stack_helper_funcs.c - Helper functions:
tokenize()
- tokenizes the line from Monty bytecode fileis_empty()
- checks if line needs to be tokenizedis_int()
- checks if string contains all digitslist_len()
- returns the number of elements in a listfree_list()
- frees a list
First step is to clone the repository into your directory
$ git clone https://github.com/kjowong/monty.git
Compile all the .c
files in monty
gcc -Wall -Werror -Wextra -pedantic *.c -o monty
After compiling, run the executable ./monty
on your bytecode file.
$ ./monty 00.m
3
2
1
$
Other functionalities are currently in development.
Currently the interpreter relies on the atoi function from the C standard library and overflows are not handled.