Check out a free preview of the full Building Your Own Programming Language course
The "Identity Helpers & Lexer" Lesson is part of the full, Building Your Own Programming Language course featured in this preview video. Here's what you'd learn in this lesson:
Steve explains why helpers are useful when thinking about building a programming language, and demonstrates how to loop over an array of tokens with a helper.
Transcript from the "Identity Helpers & Lexer" Lesson
[00:00:00]
>> Steve Kinney: We're gonna need some helpers, right? Because you don't actually need to use any helpers here, right? If you wanna sit there and write by hand, equals, equals, equals, open parentheses, have at it, right? If you wanna write that Red Jax repeatedly to see if it's a letter, right?
[00:00:16]
Okay, that's fine. You do you, I'm not going to do that. I'm enjoying my youth and I would like to maintain some of it. So we'll figure out both the tokens, we need to identify white space, so on and so forth, right? Once we have that in place, what we'll do is effectively, like I said before, we'll have this cursor, we'll have an array of tokens, let's just start moving through it, right?
[00:00:44]
Especially in the lexing and parsing parts of the show, there will be a non-zero amount of recursion, right? Which is always one of those things where recursion, when it's working, this is clear as day. I understand how this code works, it's great. And when it doesn't work, you feel like you don't actually know how to use a computer anymore, right?
[00:01:09]
So there is that, right? And what I wanted to kind of open up as well especially in the room right now, which is like, I would love this to be like a conversation, feel free to ask any questions or anything you want to know more about or talk about, absolutely let's do that.
[00:01:23]
Cuz some of this stuff is a little bit tricky to wrap your head around. Yeah.
>> Speaker 2: [COUGH] Just a quick one. Inside this block where you've got the while loop, would you also be able to use a reduce for this?
>> Steve Kinney: Yeah, I mean, you could use the reduce.
[00:01:37]
The problem is that you kind of need to, you wanna be able to iterate the cursor inside of the loop. So you're gonna likely have a loop inside of the loop. Right now when we do the actual parser, definitely like this is a string, so my thought process on it like having actually considered that question beforehand was, did I want to do a, I didn't like an indeterminately large.
[00:02:03]
We're always gonna be working pretty small chunks of code today, right? But in determining the large file, do I really want to go ahead and break it into a giant array? Right, strings reversal and string manipulation JavaScript is really fast, right? When we get to the parcel part, well then it is an array of tokens, and so in that case I'll be popping them off the front of the array in a lot of cases.
[00:02:25]
But not to spoil some of the surprises, but since if we come across a string, we need to go to the end of that string before we go and take another turn on this loop, we need to have control of where that cursor is inside of a loop, right?
[00:02:40]
Yeah, if you've made a giant or I could, you do that as well, absolutely and we will when we get to the part b, syntactic parser.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops