Nkechi-Christabel / monty

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stacks, Queues - LIFO, FIFO

stackqueue

What is Stack in C?

A stack in C is nothing but a linear data structure that follows the LIFO rule (Last In First Out). In a stack, both insertion and deletion take place from just one end, that is, from the top.

What is a Queue in C?

In contrast to a stack, a queue in C is nothing but a linear data structure that follows the FIFO rule (First In First Out). Insertion is done from the back (the rear end) and deletion is done from the front.

More Info

The Monty language

Monty 0.98 is a scripting language that is first compiled into Monty byte codes (Just like Python). It relies on a unique stack, with specific instructions to manipulate it. The goal of this project is to create an interpreter for Monty ByteCodes files.

Monty byte code files

Files containing Monty byte codes usually have the .m extension. Most of the industry uses this standard but it is not required by the specification of the language. There is not more than one instruction per line. There can be any number of spaces before or after the opcode and its argument:

julien@ubuntu:~/monty$ cat -e bytecodes/000.m
push 0$
push 1$
push 2$
  push 3$
                   pall    $
push 4$
    push 5    $
      push    6        $
pall$
julien@ubuntu:~/monty$

Monty byte code files can contain blank lines (empty or made of spaces only, and any additional text after the opcode or its required argument is not taken into account:

julien@ubuntu:~/monty$ cat -e bytecodes/001.m
push 0 Push 0 onto the stack$
push 1 Push 1 onto the stack$
$
push 2$
  push 3$
                   pall    $
$
$
                           $
push 4$
$
    push 5    $
      push    6        $
$
pall This is the end of our program. Monty is awesome!$
julien@ubuntu:~/monty$

Compilation & Output

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

Supported Opcodes

The following opcodes are supported by the interpreter:

  • push:Pushes an integer onto the stack
  • pop:Removes the top element of the stack
  • swap:Swaps the top two elements of the stack
  • add:Adds the top two elements of the stack.
  • sub:Subtracts the top element of the stack from the second top element.
  • div:Divides the second top element of the stack by the top element.
  • mul:Multiplies the second top element of the stack with the top element.
  • mod:Computes the modulus of the second top element of the stack with the top element of the stack.
  • pall:Prints all the values on the stack.
  • pint:Prints the value at the top of the stack.
  • pchar:Prints the character at the top of the stack.
  • pstr:Prints the string starting at the top of the stack.
  • nop:Does nothing.
  • comments:The capability of commenting.
  • rotl:Moves element at the top to the bottom of the stack.
  • rotr:The bottom of the stack becomes the top.
  • queue, stack:Toggles the doubly link list implementation style
  • Example

    
    julien@ubuntu:~/monty$ cat bytecodes/35.m
    push 1
    push 2
    push 3
    push 4
    push 5
    push 6
    push 7
    push 8
    push 9
    push 0
    pall
    rotl
    pall
    julien@ubuntu:~/monty$ ./monty bytecodes/35.m
    0
    9
    8
    7
    6
    5
    4
    3
    2
    1
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0
    julien@ubuntu:~/monty$
    
    

Authors

This program was written by:

Nkechi Christabel Udenkwor

Bamiwo Adebayo

About


Languages

Language:C 91.5%Language:Brainfuck 3.1%Language:M 3.1%Language:MATLAB 1.7%Language:Forth 0.6%