Hello and welcome, everyone.
In this lecture we're going to implement a special function that allows people to create tokens outside
of the Lexa.
Okay, so all I want to do is only just scroll all the way down.
And just under the LAX file, we're going to say struct lax process tokens, build for string, we're
going to say compile process, compiler, constant char string.
So essentially anyone outside of Alexa can call this function.
They can pass in a string such as init, it x equals 50 and the function will then create a series of
tokens in a new lex process instance and it will return that Lex process.
So then we'll have tokens for in it x equals 50 semicolon.
So this is a good function because it allows us to use lexical analysis functionality without having
to restrict ourselves only to the input file.
Right.
We can pass in actual strings.
So we're going to start writing this function now.
We're going to say struct buffer, buffer equals buffer, create buffer, printf, buffer, string,
and then we're going to go struct lacks process, lacks process equals lacks process create compiler.
Ampersand lacks a string buffer functions which we haven't created yet, and then our private data will
just be the buffer.
So let's now create this Lex Lexa string buffer function's variable.
So just go up here and we're going to go struct lacks process functions lacks a string buffer functions
equals so then, so then we're going to have to fill in the next char peak charm posture.
Okay, so we're just going to make those functions now.
So just go char a string buffer next char struct lacks process process struct buffer buff equals lex
process private process.
And then we just do return buffer read buff.
So what we do here is we get the private data from our LEX process, which was set here on Lex process
create.
We pass the buffer here as, as the private pointer.
Right.
So we get the private pointer stored as a buffer pointer here and then we call the buffer read function
and we pass in that buffer and buffer re will read one bite from the actual buffer.
So that's how that works guys.
So we're going to do the same for peaking.
Lex string buffer peek char struct lex process process struct buffer buff equals lex process private
process return buffer peek buff just like that.
Now finally we do the push char so we go void lex the string buffer plus char struck lex process process
char c So we just go straight buffer buff equals lex process private process buffer writes buffer.
See?
Okay, so there are three functions.
Let's now set the prototypes.
So we're going to say next char equals lex a string buffer next char peak char equals lex a string buffer
peak char and finally push char equals lex string buffer push char.
Perfect.
So that now sets up the Lex process correctly and any peak or next operations.
Well, of course, call our special functions here, which will read from the buffer.
So what we now do is we check if the Lex process is null.
If it is, we failed, so we just return null and we now need to call the lex function to cause lex
analysis to happen for this lex process.
And if it doesn't return an okay response, then there was an error.
So we return null.
So after calling the lex function, we've successfully perform LEX analysis.
We now have tokens, so we just return the Lex process and in the caller of this function can access
all of those tokens which are a direct output of the input string they provided here.
So just put this in the header file of compiler dot hedge just so it can be accessed outside of the
Lexa.
So we're just going to put it by there and it wouldn't hurt us.
But a comment builds tokens for the input string.
Perfect.
So we've now successfully implemented that.
Let's see if we can build this and it builds absolutely fine.
Perfect.