Code Monkey home page Code Monkey logo

monty_language's Introduction

The Monty Language Interpreter



Generic badge Generic badge Generic badge Generic badge

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. The monty interpreter is designed to execute instructions in a Monty ByteCode file.

Installation and uninstalling: How to install the program

1. First, clone this repository:
```
vagrant@ubuntu:~$ git clone https://github.com/Ebuube/Monty_language.git
```



2. Change into this newly created directory:

```
vagrant@ubuntu:~$ cd Monty_language
vagrant@ubuntu:~/Monty_language$
```
  1. Then run the Makefile

    #Note: Ensure you have gcc and make installed on your system before running the Makefile.

    vagrant@ubuntu:~/Monty_language$ make
    

    If you don't have make utility installed, check out how to install it. For Ubuntu, you can look at this documentation How to install make on Ubuntu



5. Confirm installation by running the monty command on a sample bytecode file and expect the result shown below. Example:

```
vagrant@ubuntu:~$ cat bytecode.m
push 1
push 2
push 3
add
pint
push 4
pall
vagrant@ubuntu:~$ monty bytecode.m
5
4
5
1
vagrant@ubuntu:~$
```

#Note: Once installed, you can use monty anywhere in your machine :).



To Uninstall

Change into the repository you cloned and run the clean target of make thus:

```
vagrant@ubuntu:~/Monty_language$ make clean
```



If the repo has been deleted or is no where to be found, you can easily unisintall 'monty' by removing the binary file from the /usr/bin directory thus:

```
vagrant@ubuntu:~$ sudo rm /usr/bin/monty
```





Execution: How to use the monty program



The monty program



  • Usage: monty file
    • where file is the path to the file containing Monty byte code



  • If the user does not give any file or more than one argument to your program, print the error message USAGE: monty file, followed by a new line, and exit with the status EXIT_FAILURE



  • If, for any reason, it’s not possible to open the file, print the error message Error: Can't open file <file>, followed by a new line, and exit with the status EXIT_FAILURE
    • where <file> is the name of the file



  • If the file contains an invalid instruction, print the error message L<line_number>: unknown instruction <opcode>, followed by a new line, and exit with the status EXIT_FAILURE
    • where line_number is the line number where the instruction appears.
    • Line numbers always start at 1



  • The monty program runs the byte codes line by line and stop if either:
    • it executed properly every line of the file
    • it finds an error in the file
    • an error occurred



  • If you can’t malloc anymore, print the error message Error: malloc failed, followed by a new line, and exit with status EXIT_FAILURE.



  • You have to use malloc and free and are not allowed to use any other function from man malloc (realloc, calloc, …)



Example: Given a monty byte code file named 00.m in the bytecodes directory, we can execute it thus...

vagrant@ubuntu:~/monty$ cat -e bytecodes/00.m
push 1$
push 2$
push 3$
pall$
vagrant@ubuntu:~/monty$ monty bytecodes/00.m
3
2
1
vagrant@ubuntu:~/monty$





opcodes: Available opcodes

  • push: Usage: push <int>
    • This pushes an (integer) element to the stack.



  • pall: Usage: pall
    • This prints all the values on the stack, starting from the top of the stack.



  • pint: Usage: pint
    • This prints the value at the top of the stack, followed by a new line.



  • pop: Usage: pop
    • This removes the top element of the stack, but doesn't print or return it.



  • swap: Usage: swap
    • This swaps the top two elements of the stack.



  • add: Usage: add
    • This adds the top two element of the stack. The result is stored in the second top element of the stack, and the top element is removed, so that at the end:
      • The top element of the stack contains the result
      • The stack is one element shorter



  • nop: Usage: nop
    • This opcode does not do anything.



  • sub: Usage: sub
    • This subtracts the top element of the stack from the second top element. Then removes the top element of the stack so that the stack becomes one element shorter. The result is store in the currently top element of the stack.



  • div: Usage: div
    • This divides the top element of the stack by the second top element. Then removes the top element of the stack so that the stack becomes one element shorter. The result is store in the currently top element of the stack.



  • mul: Usage: mul
    • This multiply the second top element of the stack with the top element. Then removes the top element of the stack so that the stack becomes one element shorter. The result is store in the currently top element of the stack.



  • mod: Usage: mod
    • This computes the rest of the division of the second top element of the stack by the top element of the stack. Then removes the top element of the stack so that the stack becomes one element shorter. The result is store in the currently top element of the stack.



  • pchar: Usage: pchar
    • This prints the character at the top of the stack, followed by a new line. The value at the top of the stack must be a valid decimal representation of an ASCII character. In other words, it should be in the range 0 - 127 inclusive.



  • pstr: Usage: pstr
    • This prints the string starting at the top of the stack, followed by a new line. The string stops when either:
      • the stack is over
      • the value of the element is 0
      • the value of the element is not in the ASCII table If the stack is empty, print only new line.



  • rotl: Usage: rotl
    • This 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.



  • rotr: Usage: rotr
    • This rotates the stack to the bottom. The last element of the stack becomes the top element of the stack.



  • stack: Usage: stack
    • Set the format of the data to a stack (LIFO). This is the default behaviour of the program :).



  • queue: Usage: queue
    • Set the format of the data to a queue (FIFO).



Note: When switching mode:

* The top of the stack becomes the front of the queue.
* The front of the queue becomes the top of the stack.



Comments

You can create a comment in a bytecode file by starting the line with an hash #. You can as well have blank lines in your file. Example: In a file named bytecode.m

```
vagrant@ubuntu:~$ cat bytecode.m
# Load new elements
push 4
push 3

# Print all elements
pall

#I may not give space immediately after the comment
# I may decide to do so, equally :)
```



Warning: Values outside INT_MIN - INT_MAX (both exclusive) can't be used as data values for the stack





File format: Monty instruction file format



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:

Given a directory named bytecodes that contains Monty byte codes.

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

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



Dependencies

  • You must have the make utility installed on your system
  • You must have gcc compiler on your system



Further Reading

Creating an Interpreter for Monty Byte codes Using C Programming Language
Monty Language: Implementing Stacks & Queues



Acknowledgements

Git GITHUB Generic badge

monty_language's People

Contributors

ebuube avatar

Stargazers

 avatar

Watchers

 avatar  avatar

monty_language's Issues

Implement basic opcodes

Overview

Create source code to implement the basic opcodes for monty the interpreter.

Intended outcome

A user of the program can execute files that contains one instruction per line with opcodes such as push, pall, pint, etc.

Features

Implement these opcodes.

  • Implement the push and pall opcodes
    • The push opcode: It pushes an element to the stack. Usage: push <int>
    • where <int> is an integer
    • The pall opcode: It prints all the values on the stack, starting from the top of the stack. Usage: pall
  • Implement the pint opcode
    • The opcode pint prints the value at the top of the stack, followed by a new line. Usage: pint
  • Implement the pop opcode
    • The opcode pop removes the top element of the stack. Usage: pop
  • #10
    • The opcode swap swaps the top two elements of the stack. Usage: swap
  • #11
    • The opcode add adds the top two elements of the stack. The stack becomes one element shorter. Usage: add
  • #12
    • The opcode nop doesn't do anything. Usage: nop

Relevant documentation

https://data-flair.training/blogs/stacks-and-queues-in-c/
https://www.digitalocean.com/community/tutorials/stack-in-c
https://www.edureka.co/blog/queue-in-c/
https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files

Estimated length of task

The task could take three-four days to accomplish. Thursday 22 - Sunday 25 June, 2023.

Timeline for task

This issue must be completed before the end of this week

Dependencies on other work

It doesn't affect other works done.

Example of similar work

Linked list
More linked list
Doubly linked list
Dynamic libraries

Update README.md

Summary

  • Reformat some sections of README.md
  • Include appropriate links to the shields at the top section of the README.md

Implement new opcodes

Summary

New opcodes to be implemented:

  • sub : subtract top element of the stack from the second top element.
  • div : divide 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 : Compute the rest of the division of the second top element of the stack by the top element of the stack
  • Implement commenting when # starts a line
  • pchar : print the char at the top of the stack, followed by a new line
  • pstr : print the string starting at the top of the stack, followed by a new line.
  • rotl : rotates the stack to the top
  • rotr : rotates the stack to the bottom
  • stack : sets the format of the data to a stack (LIFO)
  • queue : sets the format of the data to a queue (FIFO)

Initial Documentation

Overview

Create initial documentation for this repository.

Intended outcome

This will guide the developer as well as future collaborators.

Features

  • Create a well explanatory README.md
  • Create an AUTHORS.md file and include the name of the current authors.
  • Describe the system of workflow for this repository
  • Include a .gitignore file in the repository and ignore *.bak files.

Relevant documentation

Workflow

Estimated length of task

It could take a day to set out all the necessary documentations and craft a well explanatory README.md

Timeline for task

From Wed 21 June, 2023 - Thurs 22 June, 2023. Leaving it longer will lead to disorganization of this program.

Dependencies on other work

Not really as the repo was just opened recently and no work has been done on this repo, yet.

Example of similar work

Setting up README.md for the AirBnB clone

Create configuration file

Summary

Create a configuration file that moves the executable program to the directory where it will be accessible through out the program.

Update README.md

Summary

Include information on how to uninstall the Monty interpreter.

Also update the documentation on how to install it. Include information on why it is important to change into the directory cloned.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.