Hello and welcome, everybody.
And in this lecture, we're going to start creating functions.
And this particular lecture will be making the functions, structures and some of the enumerations as
well.
Okay.
So what I want you to do, just under this enumeration, we're going to create another enumeration.
And we're going to create an attribute in here.
Function node flag is native.
All right.
And that's just going to have a binary one at the end there.
So as I already explained in the last presentation, this flag will be set for native functions.
The flag is set for native functions.
Okay, so now we've created that flag.
Let's just scroll up to struct node.
And there we go.
We found struck no grade, just under the actual body sub struct.
We're going to create another substrate called function
and we're just going to call that func just to keep things simple.
So we're going to have init flags.
These are the special flags I spoke about in the last presentation.
We're going to have our return data type, return type a void, init long, etc. We're going to have
our function name i.e. function name main.
We're going to have another sub struct called function arguments which is going to call our args.
This will obviously contain our function arguments.
So it's going to be a vector for those
vector of struct node.
So it holds a vector struct.
No pointers must be type no type variable.
Now we're going to have our stack addition
how much to add to the base pointer to find the first argument.
Okay.
And remember, I did make a presentation where we did briefly discuss the stack and scopes, so you
should watch that lecture if you're a bit confused about why we would be adding to the base pointer
to access a stack variable.
Right?
So next we have our body node
pointer to the function body node null if this is a function prototype.
Finally we have our stack size, the stack size for all variables inside this function.
So as I said, we need to know the stack size because when we enter a function during code generation,
we have to subtract from the stack pointer to make enough room for local variables and stuff like that.
And obviously when we're done with the function, we add to the stack pointer to restore the stack pointer
back to where it was.
And we can only do that successfully if we know what the stack size is.
So that is our substructure function.
And this this little structure here will represent all functions in C and as you can clearly see, it
contains all the main parts.
Functions have return types.
We have that functions have names, we have that functions have arguments.
We have that functions have bodies.
We have that.
And finally functions have stacks.
And we also take that into account.
So this is all the data structure you need to represent an entire function.
And C we will probably add to this sub structure function later on in the course when we actually go
to generate the code because there are some things I would still like to be able to keep track of,
but I'm not going to burden you with that right now because we're in the parsing stage, not the code
generation stage.
Okay, let's see if this compiles.
So we're going to do a make clean and make.
See that compiles without problems.
So that concludes this lecture.
We've now implemented the basic structure types for representing functions and our compiler.
Thanks for watching.