Skip to content

martinmulwa/monty

Repository files navigation

C - Simple Monty ByteCode Interpreter

Description

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:

martin@ubuntu:~/monty$ cat -e bytecodes/000.m
push 0$
push 1$
push 2$
  push 3$
                   pall    $
push 4$
    push 5    $
      push    6        $
pall$
martin@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:

martin@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!$
martin@ubuntu:~/monty$

Compilation

The program is compiled using the following command:

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

Usage

./monty file

where file is the path to the file containing Monty byte code

Supported Operations

The Simple Monty ByteCode Interpreter supports the following operations:

Operation Description
push <n> Pushes an integer n to the stack
pall Prints all the values in the stack
pint Prints the value at the top of 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
nop Does nothing
sub Subtracts the top element of the stack from the second top element of the stack
div Divides the second top element of the stack by the top element of the stack
mul Multiplies the second top element of the stack with the top element of the stack
mod Computes the remainder of the division of the second top element of the stack by the top element of the stack
pchar Prints the char at the top of the stack
pstr Prints the the string starting at the top of the stack

Examples

martin@ubuntu:~/monty$ cat bytecodes/07.m
push 1
push 2
push 3
pall
pop
pall
pop
pall
pop
pall
martin@ubuntu:~/monty$ ./monty bytecodes/07.m
3
2
1
2
1
1
martin@ubuntu:~/monty$
martin@ubuntu:~/monty$ cat bytecodes/31.m
push 1
push 2
push 3
push 4
push 0
push 110
push 0
push 108
push 111
push 111
push 104
push 99
push 83
pstr
martin@ubuntu:~/monty$ ./monty bytecodes/31.m
School
martin@ubuntu:~/monty$
martin@ubuntu:~/monty$ cat bytecodes/12.m
push 1
push 2
push 3
pall
add
pall

martin@ubuntu:~/monty$ ./monty bytecodes/12.m
3
2
1
5
1
martin@ubuntu:~/monty$

Credits

Written by

About

A Simple Interpreter for Monty ByteCode Files

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published