What Is Inflation?

Inflation is a rise in prices that exceeds the rate of general price increases. Prices are changing all the time for various reasons. When prices rise, it means that there’s more money chasing after…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating an Interpreter for Monty Bytecodes Using C Programming Language

PART SEVEN

using stack data structure
stack.c
updated to cater for the queue implementation
push.c

I finally made changes to the push function to cater for the case of the queue. In the push, after the stack has been created, I check if the head is NULL, meaning if the linked list is empty. What I do is just to assign the created stack to the head because it is the only node. When the head is not NULL, I then check whether we are to use the stack data structure. If we are, then I insert the new stack to the beginning of the linked list by assigning the current head to the next of the new stack node and setting the prev member of the current head to the new stack node. Once this is done, I then set the head to the new stack node. We have seen this earlier. The main change to this code is the case where we are not using stack. With the help of a temporary variable (tmp), I loop through the linked list starting from the head till last node, which has its next member to be NULL. Once I have the last node in tmp, I set its next member variable to the new stack node and set the prev member of the new stack node to tmp. This is all that is needed to implement the queue opcode.

Simply compile your code with the given gcc command and run the program with the bytecodes we examined earlier. Below is an image that shows the content of the Monty file and the output of the program.

standard output

The queue command pushes 1 to the end. Stack 2 is pushed to end and so is stack 3. The pall function then prints from the beginning hence prints 1 to 3. The stack function sets the stack member of arguments to 1. Stack 4 is now pushed to the beginning of the linked list, and so are stacks 5 and 6. We now have a stack looking like 6, 5, 4, 1, 2, and 3. The add function will then add 6 to 5, and assigned the value to stack 5 while removing stack 6. We will now have 11, 4, 1, 2, 3. These are printed by pall. The next queue function sets the stack member of our arguments to 0. Therefore, the next push instruction adds stack 1111 to the end of the linked list giving us 11, 4, 1, 2, 3, 1111. The next add function will also add 11 to 4 and set stack 4 to the result while removing stack 11. Our final linked list will therefore be 15, 1, 2, 3, 1111.

We are done implementing all the opcode functions associated with Monty interpreter. I have a few things to say, though, in order for you to get all scores in the project. Make sure you use the betty linter to check all your C files and header files. Also, check if there are any memory leaks or memory access errors by using valgrind command. One important thing to note when using valgrind to track memory issues is the usage of “-g” flag with the gcc compilation commands. For example: “gcc -g -Wall -Werror -Wextra -pedantic -std=c89 *.c -o monty”. What this does is it allows valgrind to give you the file names and line numbers where the memory issues occur. It is really helpful. You can check it out.

Lastly, I ran the program in valgrind mode so you see that I actually deal with freeing up memory allocations.

standard output

Drawing the curtains, tell me one thing you learnt from these series I have written, as well as one thing you disliked. Thank you for reading. Thank you for letting me know what your thoughts are.

👋👋.

Add a comment

Related posts:

How To Build A Targeted Audience And Grow Your Email List.

Do you know that email marketing is one of the most trusted and reliable ways to build your audience, generate leads, and convert paying customers? But most times, we are faced with the question…

Europe Frozen Potato Market 2021 Best Workable Strategy That Will Help to Boost your Revenue Till 2

Europe Frozen Potato Market was valued at US$ 18,699.80 million in 2021 and is projected to reach US$ 24,705.33 million by 2028 with a CAGR of 4.1% from 2021 to 2028. The “ Europe Frozen Potato…

Graffiti cleaning services in uk

Cleaning graffiti from the walls or surfaces is challenging for property owners. Not only it reduces the look of the property, but it also decreases its resale value. Therefore, it becomes crucial to…