lemme learn ya a thing or two.

still not dead yet, eh?

Congrats for making it this far! I know it's a bit of a tedious series, but props for following it! Today we're going to go into more of the structure of things, and making our interpreter able to further process things. If you haven't already, please check out the start of this series, or you'll be a little lost here.


beginning our evaluation...

So you'll notice from the last part what happens if we try to run an RPN equation. It executes with no error, but it doesn't really solve anything - it just shows us what we already know.



This is cool and all, but how do we get it to actually do shit?


Our first step would be to create a function where the program uses an operator string to see what operation to use. We've already defined the operators, but before that, we need to tell the computer (or rather, compiler) what to do with them. For this, we'll use long. Let's make this before our main function.



Nice. Now, using if() statements, we'll make something that somewhat resembles an array. It'll allow the interpreter to pick out certain operators, and process what to do with them.



This is pretty easy to read, I'd say.


If the compiler detects an equation with a "+", then it'll return with whatever x & y values added together would be. Same with "-", and so on. We should also add another return statement, to tell the compiler what to do if it runs correctly - to go straight to the main function.



Solid. Now if you run it as I've typed it, you'll get an error, primarily due to the formatting on  the return(x<operator>y;) part. Before continuing, try and figure out why it's not working. Hell, continue. But it won't work until you figure it out.


recursion: the act of recursion.

We're going to continue our structuring by using mpc, but also by using strcmp to check what operator to use, while strstr checks if a tag has a substring. This is called a recursive evaluation function. Place it after the long function we made.



Before we run anything, we should also tell the interpreter to print the evaluation, rather than the process of finding such. This is pretty easy. We'll just use some stuff from mpc to get us there. You can do this without mpc, but this way is soooo much easier. Plus, we're already using it.



I'll go ahead and spoil the solution to the previous problem while we're at it.



Alright, let's compile & run it.



Looks pretty alright. Go ahead and test it with an RPN equation.



Sweet. Try dividing by zero.



Shit, it crashed. No worries, though! Come back tomorrow for the next part of this tutorial series, and we'll figure out how to handle this.




tutorials on whatever, daily. doesn't have to be tech-related, but it mostly is.