Monty project
"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.
"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."
0x19. C - Stacks, Queues - LIFO, FIFO
Monty is a scripting language often used as a simple bytecode interpreter.
What is the Monty Bytecode Interpreter. This is a C program that will take a file as an argument, open the file, and read the instructions in the file, line by line. For each line read, the interpreter is supposed to execute the instructions on that line.
The aim of the project is to build an interpreter that interprets byte codes given it to from the command line.
Here the byte code are just instructions like push 1, pop 1 etc, that performs different operations on top of stack like add an element(push), remove(pop) or on the queue
...
Nb1: If you want to use dprintf method or any function from the stdio.h header file like getline, you'll have to include the
#define_POSIX_C_SOURCE 200809
Add this to your source file where you have the getline function
This is because, dprintf was not standardized until POSIX.1-2008, but the header file stdio.h has being there for like forever, so, stdio.h needs to continue working as intended without them breaking it.
POSIX 200809L is like a macro that allows stdio.h the access to some new methods/functions like dprintf and getline
Also, if ALX allowed us to compile the program with gcc -wall -Werror -Wextra -Pedantic -std=gnu11 instead of gcc -wall -Werror -Wextra -Pedantic -std=c89, the program won't break and there would be no need to define the POSIX thing.
You'd want to place it at the top of your file before adding any header files ie "#include etc.h"
Nb2: When you pass a file to the getline function in C, it reads from the file using the file stream (FILE) provided.
The getline function reads characters from the file stream until it encounters a newline character ('\n') or reaches the end of the file. It dynamically allocates memory to store the read line and returns a pointer to that memory. The function also updates a variable (typically passed as a reference) to store the size of the allocated buffer.
In the code snippet below, Have you wondered how getline() remembers where it left off in a file while getting input from it?
while (read_line > 0)
{
content = NULL;
read_line = getline(&content, &size, file);
bus.content = content;
counter++;
if (read_line > 0)
{
execute(content, &stack, counter, file);
}
free(content);
}
The getline() function, in conjunction with the file pointer, automatically manages the position within the file and remembers where it left off after each call.
When you open a file using the fopen() function and obtain a file pointer, the file pointer keeps track of the current position within the file. As you read lines using getline(), the file pointer advances to the next line after each successful read.
Internally, the getline() function uses the file pointer to determine the current position and reads characters from that point onward until it reaches the end of the line or encounters the end of the file. After reading a line, it updates the file pointer's position to the start of the next line so that subsequent calls to getline() continue from the correct location.
This behavior is a fundamental feature of the file I/O functionality provided by the C standard library. The file pointer maintains the state of the file stream and ensures that subsequent read operations start from the appropriate position.
Therefore, you don't need to explicitly handle the file position; getline() takes care of it automatically, allowing you to read lines consecutively until the end of the file is reached.
getline returns -1 when it reaches end of file



