Transcript of RSM Episode 7

RSM project page

00:05 so welcome to part seven this is going to be very brief because if you would
00:10 like to learn more about what's been going on since part six
00:14 i was doing a live stream it's quite long but there's a link in the description here on the if you're
00:20 watching this on the youtube and it will be a link on the
00:24 web page too to the recording of the live stream
00:30 during that stream we were implementing the parser for the assembler or at least the beginning of it and we got it working you can see it opening here on the side i'll just summarize that and show you in
00:42 just a minute and to make any change to anything else i don't think so no we did not uh so we spent most of the
00:51 time in here writing this out
00:56 what we've introduced is uh a couple of things we have a pratt style
01:03 parser that uses the dispatch table with prefix and infix parslets
01:07 for handling uh precedence and stuff like that we have introduced a new structure called a node or describing an ast or
01:16 maybe an you know ssa thing later who knows but the thing is that we parse as
01:20 a node it's really simple right now it has a
01:25 type where we can reuse just the token for from the scanner that we looked at in the previous part we have the source location
01:33 for it and as well as some data fields
01:40 depending on the type of dna node and the actual the actual parser itself
01:45 ended up being fairly small we gave we get the uh the scanner here a
01:50 better name for advanced the parser since we ended up just kind of cutting it down it's sort of logically more what it does rather than like scan talk which is
01:59 technically i guess more correct but advanced advance um makes more sense
02:06 and i here start some of the the the parse code that we wrote or i wrote
02:11 is it oh yeah we i don't know i want to feel like we're doing this together but so we implemented two parslets
02:23 one that's a prefix parselet for the uh for the register type
02:28 uh a token and it's really straightforward we create one of these nodes that we that we defined and we set
02:35 its value to the current numeric value that's been
02:39 parsed right so if we reduce the one over here it would be one
02:43 and then we advance departure to the next token
02:50 and then we wrote an infix parslet for the equality operator
02:55 this one takes a thing on the left side in the example here it'll get a register
03:00 so receive it here and then it will parse an expression on the right hand side which in our example will be is not
03:05 a register and it builds up a yet another node with
03:12 operands instead of an integer value this type of node will have two operands which is these are the
03:18 the left-hand side and the right-hand then we have the then we have the
03:29 parser dispatch table this map a token to uh a
03:36 prefix parslet and or an infix parslet so we see that for registered tokens we have our register prefix parsley up here
03:44 and for the uh the equal sign we have our in equal in fixed parser for the
03:53 and comments we just ignore and all the other tokens we'll have just zero here
03:57 and we're we're sort of saying that you know to do essentially for those things
04:01 and we'll keep adding to this table as we add more parse functions for more
04:06 types of things and finally we have the the sort of the
04:11 um the the less specific the the root parse
04:16 function whatever that parses like any statement
04:21 and expects a semicolon explicit or implicit at the end
04:26 and this one is the sort of the the thing that does the lookup in the
04:30 table so it takes the current token that's been scanned which we've primed
04:34 the person out before we call this the first time so it takes the it looks for previous parts that it's an error if it doesn't find one and at this point we will find a lot because we're still working on it
04:46 the next thing it does is that it calls the prefix parselet and so what happens
04:49 here is really just like calling one of these functions right so we'll call the the partial distance function
04:58 and the result from that will be in node uh and we have some code here to catch some errors some programming errors as we're working on this and the next thing that it does is that
05:09 it looks for an infix parser parslet matching the token that comes after here
05:15 so in the case of you know we have a you know register what's it
05:19 um register one will be he will be here
05:35 right um at this point and then sorry at this point we'll be there
05:42 and after we parse that the parser is now here and there is a infix parslet for the equal sign
05:51 so we'll look that up and we'll pass along the thing that we just parsed into
05:54 there the infix function and we keep doing that as as long as we have infix
06:02 parslets available for the next token that we got and as long as the the precedence there is the same or higher
06:08 we just keep wrapping essentially um tokens sorry nodes that were parsing sd notes
06:16 and we can see that wrapping effect over here so this is the result of parsing the
06:26 so we sample program here let me scroll it up oh it's in a different file of
06:29 course so here's a sample program these two we
06:37 will uh we'll have this wrapping effect first we parse the r1 right and then the infix
06:43 thing comes into play and we pass that kind of throw that in there and that parses another one and that that
06:51 wraps those two uh into one and that's how we got the the tree structure from
06:56 this type of uh parser this type of parser and yeah so uh so that was the success
07:03 we ended up uh parsing a very simple but yet valid
07:10 uh tiny chunk of our assembly language here and in the next
07:16 in the next part let me save that changes that i made in the next part
07:20 we'll be looking at expanding this partially to cover the entire example factorial function
07:27 and that includes all of the syntax of of this which looks like a lot that is probably
07:33 going to be pretty pure little code to add to our already existing parser most
07:38 of the infrastructure is there after that will be and let me switch back to
07:43 uh assembly assembler file here
07:47 so after that uh we'll be working on the
07:53 analysis pass which also i suspect will be very minimal we might actually not even need it for to complete you know
07:59 our example function so this will do things like making sure that you know um if you the types match and stuff like that
08:09 and then what we'll do next is the code generation to generate these virtual
08:12 machine instructions which we can then pass on to our
08:17 interpreter and run all programs from the command line and stuff like that and it's got to be a big milestone
08:23 so that's what we've been up to since the since the past part six uh and i
08:29 hope uh that you enjoy these little videos and the progress and uh if you're
08:33 interested in stuff i hope you've also found the code on github and maybe
08:38 following along maybe you're even making your own version of this that'd be cool to see and so bye bye for now see you later