Skip to content
/ monty Public

A Repository contains stack and queues operations to create a Monty language interpreter

Notifications You must be signed in to change notification settings

Moniaar/monty

Repository files navigation

monty

But wait a minute...what is a Monty lanugauge?
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
Interpeter
A program that directly executes the instructions in a high-level language, without converting it into machine code. In programming, we can execute a program in two ways. Firstly, through compilation and secondly, through an interpreter. And the common way is to use a compiler

what we are supposed to create is an intepreter for that language as mostly scripts of monty have a .m extension. For example let us say there is a script file called new.m, then that file will have a list of commands in it line by line . as an example for that script is the following: push 4 push 3 push 2 pall All in different seperate lines, and no more than 2 arguments passed into it. This code will give you a stack with the following output: 2 3 4 All printed in seperate lines using the pall command which is shortend to "print all" to print the stack content. And that is basically what a monty script is.

How it works?:

  1. To have your monty interpreter, you open new.m with functions like fopen and close it with fclose when you are done with it. Its implementation is ./monty new.m Which where the ./monty is the exe. you get after executing the whole source files when you install it.
  2. So if there are more than 2 command line arguments, (the file name and the monty excutable command), you print out the error found in the files that handles them.
  3. In addition, if the file can't be opened for some reason, you print out the given error.
  4. After you open the file, you basically go through the script line by line. you parse it in to an array of char, you use get line or fgets for the line like we used in a command line interpreter.
  5. Then you tokenize it based on delimiters. So for examlple in new.m push 2 becomes two tokens. push is the first token, as 2 is the second token.
  6. After that token[0] which is push becomes the opcode. stack_t will be the doubly linked list where you implement these op codes.
  7. For pushing opcode you are supposed to write a function, push. You comapre the token[0] with the name which is push and select the respective function. It will be based on the structure provided named instruction_t. Your push function for example will look like: void push(stack_t **stack, unsigned int line_number)
  8. For the rest of the functions, you compare your first token with the name, if the function strcmp returns 0 when you compare them, you select the function that is to be used.
  9. That script for example has a list of commands, known in our project as opcodes that implement a stack. As an like example push, pop are opcodes. You open the file then print out errors accordingly if the argument is more than 2 in the file. If it doesnt open with fopen then you print out the error accordingly.
  10. token[0] will be the opcode, token[1] will be the argument for the op code.

How to install it?:

compile with the following: gcc -Wall -Werror -Wextra -pedantic *.c -o monty

Concpets you need to make yourself familier with before diving here

  1. DMA (Dynamic Memory Allocation)
  2. Stacks and Queues
  3. Data structures and algorithems basics
  4. File handling
  5. Singly and doubly linked lists

References

  1. How to use extern to share variable between cource files?
  2. google
  3. Stacks and queues in C
  4. Stack operation
  5. Queue operation
  6. Toppr website

Authors

  1. Solomon Chidera
  2. Omnia Ahmed (Moniaar)

About

A Repository contains stack and queues operations to create a Monty language interpreter

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages