Hello and welcome, everybody.
And in this lesson we are going to implement parentheses.
So if we go here and we did something like 50 plus 20, you know, in between parentheses and we did
make and then main can see that it loops forever.
Obviously we don't want that.
We need to be dealing with it.
So we're going to implement parenthesis in this lecture.
So back to positive C So just above pause expression, we're going to say void, pause for parenthesis
and it's going to accept a history.
Now in our past expression, we're going to say F as equals, token peak next se file is equal to a
left bracket.
Then we're going to say pause for parenthesis.
We're going to pass in the history and then we're going to return zero.
Or as a matter of fact, we don't need the return zero here just to an else pause expression normal.
So if there's a left bracket, we call pause for parenthesis, which handles that parsing of that expression,
hope that makes sense.
So here we're going to say expect op left bracket.
We're going to say struct node left node equals null struck node temp node equals node PQ or null.
And then we're going to say F temp node and node is value type temp node.
Then left node equals temp node.
No, Pop.
I'll explain this in a minute.
We do need to implement this node as value type function.
So let's head over to node C and we're going to do that.
So scroll to the bottom and we're going to start by creating a function called node is expression or
parenthesis.
That's going to accept a node.
And we're just going to say return no type.
Equals node type expression, parenthesis or node type.
Equals node type expression.
So that will return true if this node is a parenthesis node or an expression node.
So now we're going to say bool node is value type struct node node.
So we're going to return node is expression or parenthesis or node type equals.
No type identifier or node type equals.
No type number
or no type is equal to no type.
Unity or no type is equal to no type.
Ternary or no type is equal to no type string.
So they represent all of our value, no types.
So essentially what can be passed into a variable?
That's what a value is, right?
What can we pass into a variable?
So we allow for expressions or parentheses to be passed into a variable.
We allow identifiers to be passed into a variable.
We allow numbers to be passed into a variable.
Uni generates ten trees and strings.
They all return a value that can go into a variable string, returns a pointer, for example.
So that's what that is.
Let's put these in the header file now.
So we're just going to search for nodes and we're going to put it.
By here.
All right, remove the bodies, and now it's in the header file so we can access it.
Perfect.
So now we can go back to parse the dot C and we can continue implementing parse for parentheses.
So you're probably wondering what this does.
So let me explain.
Let's imagine we have test 50 plus 24 pass of parentheses whereby here by this left bracket, right.
So we peak on the stack and we get the test identifier here.
So then what we're basically saying is if the the left node we've peaked is a value type, which it
is, is an identifier.
Then the left node should equal this left node, the temp node right, which is test.
And then we pop off that test identifier because we've just stored it in the left node here.
So that's what that code does.
And don't worry about that too much right now.
We'll get to that as we finish this function.
So what I now want us to do, I want us to scroll to the top of this file.
And just by here, we're going to say struck node, pause the blank node.
And this is basically going to be no type blank.
And it just represents a node that is just blank.
It means nothing.
Right?
I'll just scroll down to our parse function and just by here we're going to say has a blank node equals
no create ampersand struck node dot type equals node type blank.
Is that just initializes blank node essentially.
So now let's just head back to our paths expression for parenthesis.
And just by here we're going to say struck node expression node equals parse blank node.
And then we're going to say if the token next is not symbol right bracket, then we're not finished
with the expression and we're going to say pause expression of all route history begin
just like that.
And then we're going to say expression node equals no pop.
So if you imagine something like 50 plus 20, we come in there, we expect the operator, which gives
us something like that, 50 plus 20 right bracket.
We can see that it isn't the end of the expression.
So we go in here and we call parse expression root, which parses the 50 plus 20.
That's what's going on there.
So now finally, when the expression is parsed, then we just need to expect a right bracket symbol
to end the expression.
And now we're going to say make expression, parenthesis, node, and we don't have this function yet,
expression node.
So that makes the parenthesis node.
So we're going to create this now.
So go to nodes C and just search for my expression node.
And just under there we're going to say make, we're just going to copy and paste what we wrote and
we're going to accept an expression node.
So now we're now going to do we're going to go to compiler H.
And in our struck nodes, which is here just under the expression, we're going to say struct parenthesis
and then we're going to say parentheses here as well.
And then we're just going to go struc node expression, the expression inside the parenthesis node.
Very nice.
So let's head back to Node C and we're going to say know, create ampersand struct node dot type equals
node type expression parenthesis dot parenthesis dot expression equals expression node.
So that creates that.
Let's put that in the header file now.
So we're going to search for make expression node.
We're just going to put it underneath there like that.
Now head back to parse dot C and we can see that now resolves correctly.
Okay.
So now we're going to deal with our little left node thing we did here.
Okay.
So are we're going to say we're going to say if there's a left node,
then we're going to pop off the parenthesis node that we just created.
And then we're going to say, make expression node.
Left node, parenthesis, node and the operator will be these left and right brackets, essentially.
So this here test, 50 plus 20, essentially, has this become an expression?
Parenthesis node.
So we'll call it epi OC and the left node here is the identifier.
So this is what your resulted with your resulted with a expression node with a left node being an identifier
test and a right now being a expression parentheses node.
So that's basically why we do that here, because we know there's something valid to the left that needs
to be merged into a new expression such as this function call, for example.
So that is why we do that here.
So we basically merge it.
So the left node here becomes identifier test and then the the parenthesis node, we just parsed 50
plus 20.
That becomes the right node of that expression node with the operator being a left and right bracket
to signify that this is kind of like a joint expression, right?
So test is the left node, right is the parenthesis node and obviously the operator is these two brackets
to signify what is going on to signify this is a function call.
So the final thing to do is to check for additional parentheses.
So what we're going to say is here, we're going to say void plaza, deal with additional expression.
So now with additional parenthesis, additional expressions, I meant.
So what we're going to say here is we're going to say if token peak next type is equal token type operator,
then pause expression of all history begin zero.
So yeah, basically if there's an additional operator, then we might have something like this, you
see.
So there's more to parse.
So then just down here we're going to say pause to deal with additional expression.
So we're nearly ready to test this.
This is one thing that we need to change head to parse function.
Remember, we did this partial blank node a few minutes ago.
Just put it under the set vector because we haven't yet set that actual vector.
Right.
So if we have that above, it's going to cause a segmentation fault.
Okay.
So if we now go to the terminal and we did a make clean and a make and we run main can see that works
absolutely fine.
So you can see here now we support expressions.
So yeah, we can test that in the debugger so that you can see, so I can explain it a bit more.
So JDB Main.
And we're going to do pause for parenthesis.
We're going to copy and paste that I want to show you guys there so you're able to debug things yourself.
So we do practice with the debugger quite a bit.
We're going to break, we're going to run.
See now we can go through.
It means it expects the left operator.
We can see it at par.
It pauses the expression for root.
And then we got the expression node.
So if we did print asterisk expression node, we can see that that is an operator of plus it has a left
node and a right node.
Nice and simple.
We're going to do next.
And we make the impressionable note.
And if we do print no peek, we can see that there's our expression will note.
If we print the parentheses, you'll need to get rid of the ask us for that.
We can see there's our expression inside it.
So we're going to go next pause.
Pause to deal with additional expression.
And yeah, we can see that that worked perfectly fine.
So if we keep pressing next that'll keep taking us down for one where I'm positive 299 print next.
And we can see if we print up now we have our equals.
So yeah, that is x equals 50 plus 20.
So I left node.
No left.
That's our X variable.
And our right node is our parenthesis node.
And obviously we reorder it then based on the body mass rules and there we go gets pushed to the stack.
So that's how it works, you know, and I advise using the debugger whenever you're confused because
it really makes things easier and more understandable for people.
So I hope you enjoy this lecture.
Thank you for watching.