Hello and welcome, everybody.
And in this lecture we are going to implement the array bracket part of variables.
So for example, by the end of this lecture, we should be able to do things like that in an x 5020.
We will not be able to use the arrays right now, but we will be able to declare them after this lecture.
So the first thing we're going to do is we're going to create a new file called Array C and we're going
to include compiler H.
We're going to include helpers vector.
We're going to include a certain age.
Now we're going to go to computer age.
And just above data type, we're going to say struct array brackets.
And this new structure we've declared will contain a vector of end brackets.
And essentially this vector will contain each part of the array.
So here we have an array of 50 and 20.
So therefore our array brackets would be a count of two.
This array back of vector and I should point out this is a vector of struct nodes.
Struct, no pointers.
Okay.
Third time lucky.
So okay, when you've created that, we're going to scroll down to data type because we need to make
an option here for arrays in the data type because what we're doing here in it x 5020, we're essentially
making an array declaration.
So therefore the data type must be able to support that.
So just in our struct data type, we're going to say struct array
and that's going to take an array brackets pointer and we're going to call it brackets.
And just under our array brackets here, we're going to say size, tw size.
So this size is essentially equal to the size of the data type by here multiplied by the total array
indexes.
Okay.
So just to keep it simple, if we did in it fall, fall and there'd be four times, four times four,
and that would be what this size would be, because we're multiplying the size of in it, which is four
by this array index and this array index.
And that gives us the total array size.
The total array size equation equals data type size multiplied by each index.
So obviously when we access a index in this array later on, such as if we look at it for here and I
went something like in it Y equals x one.
Obviously the size will then be four because we've gone in one step into the array, right?
So the size would be for the size of in it rather than four times four.
So the size you see here is the size of the individual data type.
In our case here, it'll be integer.
And the size you see in this array structure is the size when you take the array brackets into account.
Okay.
Hope that makes sense.
Okay.
So now that we've implemented the array bracket structure and the array structure, we can head back
to array c.
So just in here, we're going to say struct array, brackets, array, brackets, new.
And we're just going to say struct array, brackets.
Brackets equals color one size of struct array brackets.
And then we're going to say brackets and brackets equals vector create size of struct note.
So our array brackets basically contains a vector of nodes that represents those brackets.
So in this situation for three, we'll have an array brackets structure here that has an in brackets
vector that contains two elements, the no type number which contains value for and another node type
number which contains value three.
So now we're just going to return the brackets pointer.
Now we're going to say void array brackets free.
And I know we're not actively dealing with freeing data, but we'll just get the functions there anyway
in case you guys decide to implement that later down the line.
And we're just going to go free brackets.
So as you know, the reason we're not dealing with freeing memory right now is because our compiler
designs hard enough.
We don't want to be dealing with double free errors and stuff like that as well.
And it's not such a big deal because the compiler will run from step one to step six or whatever that
step will be, and then it'll unload the program.
Right.
So it's not a big deal, really.
It is a big deal if you want to keep the compiler in memory or something like that.
But usually compilers, you know, the compiler file and they finish.
So, so here we're just going to avoid array brackets.
And so this will allow us to add an array at a node to the array brackets essentially.
So we're going to say struct array, brackets, brackets, struc node, bracket node.
So the first thing we're going to do, we're going to assert that the bracket node type is equal to
node type bracket because we don't want them passing a function node here trying to get it to be added
to an array brackets vector, you know what I mean?
So we're just going to go back to push brackets and brackets, ampersand, bracket node, nice and simple.
So that will ensure that we can add a bracket node to a to a brackets structure.
However, if it's not a bracket node you've passed, it will cause an assertion error.
So now we're just going to stay stuck vector array, brackets, node vector, and we're just going to
say struct, array, brackets, brackets, and we're going to say return brackets and brackets and we're
going to make another function size t array brackets calculate size from index struct data type dx type
struct array brackets brackets and finally in it index.
So for now, we're just going to return zero here, because I want to explain to you about how we calculate
array, bracket, size and a bit more detail before we go and implement stuff like this.
But simply put, while this function will do, it will calculate the array bracket size from a given
index.
So if we were to pass zero here, then it would calculate from the first index here.
We would say if we were to pass one, it would calculate the size from index one here, but we'll explain
more on that later.
We're not going to implement that here because I think it will be too much for you to understand at
this point in time.
So we're going to go size t array brackets, calculate size struct data type, dx type, struct array,
brackets, brackets and we're just going to return array brackets, calculate size from index and we're
basically just going to pass index zero just like that.
Now the final thing we're going to say is, is in it array, total indexes, struct data type, de type
and we're going to assert that the de type flags has a data type flag is array.
In other words, it's this data type, an array, data type.
And then we're going to go straight to brackets.
Brackets equals de type array dot brackets.
And then we're just going to do return vector count brackets and brackets.
So that's a nice simple function.
And we'll we're just going to put a warning right here, guys.
You know, always put the warnings because then we know to come back to it because the project does
get quite big.
And if you're not giving yourself guides that you haven't implemented things, then it's going to bite
you, you know, not yet implemented, calculating size of array brackets.
So that just shows we get a warning every time we compile so that we can stay on track of things and
not forget things.
Right.
Okay.
So we're going to put these in the header file now.
So just go to compile H.
And we're just going to paste it in.
And just get rid of all the bodies so that the prototypes are showing.
Nice and easy.
Okay.
So we're ready to go back to parse the dot.
See?
And in our past variable function, we're now going to check for array brackets.
But like this.
So we can get rid of this warning now because we're now checking for a rate bracket.
So we're just going to go struct array brackets brackets equals null if token next is operator and we're
going to look for a left array bracket, then brackets equals parse array brackets and we haven't created
that function yet.
We will create it and we're going to pass it in the history and then we're going to say D type array.
Dot brackets equals brackets.
So after we've parsed the array brackets, we're going to set the array brackets here in the actual
data type that we created earlier.
And then we're going to say D type array, size equals array brackets calculate size.
We're going to pass in the D type and the brackets, and then we're going to say D type flags or equals
data type flag is array.
So we're basically just ordering on that flag.
So anyone who has this data type knows that they're dealing with an array declaration such as an X for
three OC.
Nice.
So we're now ready to create the parse array brackets function.
And on 560 here, obviously we calculate the size of the array brackets, the ones that are not yet
implemented.
Okay.
So yeah, just by here, we're going to say void, parse array brackets.
We're going to say struct history.
History, and it shouldn't be void.
It should be array brackets.
My bad.
So we're just going to say struct array brackets brackets equals array brackets new
and we're going to say while token next is operator v array bracket expect op.
We haven't actually created that yet.
So if we scroll up to expect same that we made last lecture or the lecture before that and we're just
going to create another one here static void expect up and operates take a const char pointer as you
know
and we're just going to go struct token next token equals token next if next token is null or next token
type does not equal token type operator or the next token file.
Does not equal the operator provided.
Make sure you do an explanation Mark that goes out because we're noting that compiler error current
process expecting the operator percent se but something else was provided.
And we just do next token se file.
Nice.
That's nice and easy.
So now we can search for expect OP to get back to where we were lying.
Five, six, five.
So as you know, that'll basically expect that the next token is a left bracket, left or right bracket.
And if it isn't, then it will show us that error.
If it is, then it'll just pop it off essentially with the token next.
So now we're going to say if token is symbol token peak next and we're going to look for the array to
a bracket.
And that's a that's a char that goes, not a string.
So as I already explained to you in previous lecture, our left brackets count as operators and our
right brackets count as symbols.
So as I already explained to you in a previous lecture in this course, our left brackets count as operators
and our right brackets count as symbols.
And this is the same for even these parenthesis operators as well.
So nothing between the brackets, then we're just can expect the symbol of the right array bracket and
we're going to break.
So yeah, this just ensures that if someone provides like an array bracket with nothing between it,
we just kind of just deal with it.
And that's that because in certain situations you can do things like this, you know what I mean?
And then we don't really have a size in there, so we need to be able to account for something like
that.
Right?
Which is why we check here.
For that situation.
So now we're going to say pause, expression of all routes.
We're going to pass in the history and then we're going to expect the symbol for the right array bracket.
So now we're just going to pop off that expression that we've parsed.
So essentially, we've parsed an expression of whatever was between those brackets.
So in this case, it's for.
So in that situation, our expression out here would be no type number and it would contain four.
So that's what we've done here.
We parsed that expression after we've popped off the left array bracket, and then we'll obviously we
expect the right array bracket to close that by the hope that makes sense.
So we're going to turn this into a bracket node.
So we're going to say make bracket node, and we haven't yet created that function.
And then we're going to pop that bracket node off the stack and we're just going to do array brackets,
add brackets, bracket node, simple as that.
And then here we're going to return the brackets.
So let's just implement the make bracket.
No function.
So just scroll up.
And you know, you can just search for make and then just find the make and just do control left click.
Okay, cool.
So it's in A.C. and we're gonna say void make bracket node struck node node and we're just going to
basically say no, create
node type bracket.
And we need to change our struct node so that we can account for brackets as well.
So let's just head over to compose H and just instruct node and divide list.
We're going to say struct bracket bracket and we're going to say struct node in a so just as an example
and it x 5050 would be our bracket node and the actual inner would be no type number with value of 50.
So I hope that makes sense.
Right.
So in a situation where we say in X 50, the actual bracket 50 is our bracket node, no type bracket
and the inner node here is the 50 number, which is no type number hope that makes sense in our situation
it would be four and for the next bracket it would be the end, it would be three hope.
That makes sense.
So back to note, let's see, we're going to say dot bracket, dot inner equals node.
Okay.
And we're going to copy and paste that prototype
just by there as well.
So the make bracket node goes there.
So back to the dot C, we're going to scroll down to parse the parse variable just just by their parse
variable and everything looks okay.
We just need to go to the Makefile now to add a readout c in there.
So just copy and paste this in resolver and change that to array, dot array, dot C and just up here.
After the SIM was over, I'm going to say belt.
All right, so then we're just going to go make clean make.
You see that composite?
Okay, we're going to run Main.
We can see that.
That works absolutely fine.
So when I was just going to quickly test this so in positive see in parse next.
We're going to make a break point.
So we're going to say JTB main break paths a dot C 748 it's 748 for me might be different for you as
you know I'm breaking in that loop just by the vector push.
We're going to say run print as coarse node.
And we can see that that that works.
Okay.
So if we went no node, var type, we get the data type of the variable and we can see brackets is set.
So we're going to look into brackets.
And we're going to just do a vector count on there.
And remember, we need to do the count on the actual in brackets, not the brackets itself.
And we see a count of two, which is correct, because we do have two array brackets on our variable.
So that works perfectly.
We now have the brackets pointer and we know exactly what those brackets are for that variable declaration.
You can see that the flag is set to say that this is an array data type.
We can see our size is zero because we haven't yet implemented the calculation feature, which we'll
do in a few lectures time.
So yeah, thanks for watching.
We now have primitive variables that can be array declarations.