Check out a free preview of the full Building Your Own Programming Language course:
The "Building the REPL CLI" 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 demonstrates how to build a REPL, and tries a few commands, such as add and subtract in the new created dropbear shell.

Get Unlimited Access Now

Transcript from the "Building the REPL CLI" Lesson

[00:00:00]
>> Steve Kinney: So now that we have a language that we can run, we should run it, right? It'd be cool to actually be able to use our small but mighty language that we can kind of grow and extend over time. So one things that we'll build is, as we mentioned before, a repl, the read eval, print loop.

[00:00:21] And all it needs to do is take a statement be able to basically tokenize it, parse it, evaluate it, get to that final result and then print it back. We can kind of use some node libraries to make that easier for ourselves. We're gonna have this reple function here, that we'll call.

[00:00:46] One of the things we are able to do is we'll use this require.main, just like put a little welcome message and kick it off. So this means we can require it and it won't kick off the repl, which we'll use to add it to the command line interface in a little bit.

[00:01:03] But we can also like so we can both require it and call it from the command line tool that we're going to make. We can also just call it as it is right now. We're going to use some libraries for this, but they're pretty simple. One of them is called chalk.

[00:01:16] All chalk does is it adds colors to your terminal, which is incredibly important. And then we'll use prompt to be able to get some feedback, so on and so forth. And we're gonna use very little of these libraries. Prompt will let you have a series of questions that you can ask.

[00:01:33] We really just want to show them a command prompt, get whatever the last command was, run it through everything. I did make this parse and evaluate library which has got a few just pipes here. So parse and evaluate, we'll go ahead and I'll run it through tokenize, parse, transform and evaluate.

[00:01:52] Transform rather is currently just a node app. Right, we should actually have it.
>> Steve Kinney: Return node. Let's actually take that out for now.
>> Steve Kinney: I don't need to add entropy in there. All right, so we'll tokenize, we'll parcel evaluate, we'll add transform back in when we actually need it.

[00:02:19] And so this will kind of run through is just to basically have, we could call those three functions together, I don't want to. So we have that as a little helper for us. Great, so really what we need to do is set up asking for the command which we can do with this inquire library which will be our command prompt.

[00:02:41] And we'll say const ask questions.
>> Steve Kinney: All right, so once we have that, it takes an array of questions that we pass to it. And it will return a promise that will resolve when the questions are answered, right? And for us it'll be relatively quickly, since we're gonna ask one question.

[00:03:03] Give us something to do. We're not going to collect all that many. So make our questions array and the array check some objects. And one of the ones we'll add is, I'll take a name which is just some key that we convert to it and so I'm going to just name it command in all caps as kind of like a constant.

[00:03:26] What type is it? For us, it's an input. And then what is the message that it asks? We're just gonna have like a little prompt in this case. So we'll say message and we'll use chalk to make it, anyone have a favorite color, otherwise it's gonna be blue

[00:03:47]
>> Steve Kinney: Background yellow leaves blue. We'll just say, give us a little command prompt there. All right, great. So we have that in place, and then we'll just return,
>> Steve Kinney: The prompt with the questions. Now, I mentioned that we also have to spell return correctly. We want everything to work.

[00:04:12] I mentioned that it returns a promise. And so you have two options here. You can go the promise chain route, which a lot of cases I actually prefer but we'll make this an async await function. And we want to do is if we're not careful, a JavaScript error here will like blow up the entire stack and kick us out of the repl, right?

[00:04:31] And our syntax we know isn't perfect yet. We didn't check to make sure all the braces were balanced and stuff along those lines. So, there are still things that can go wrong so we'll make sure we do is are catching any errors to make sure we don't blow everything up because it's kind of annoying to get kicked out all the time.

[00:04:47] So we've got this async function, and we'll put it in a try catch block. And if some error does blow up, we'll just console error. So we'll see it,
>> Steve Kinney: But we won't actually get kicked out with it, awesome. So first of all, we need to go and get, this is just a function we need to go and request whatever their command is.

[00:05:25]
>> Steve Kinney: And then we'll pull of that command. Basically we'll get back an object of all the questions we asked just cuz we only asked one. So pull that off.
>> Steve Kinney: We could theoretically do it there as well. One thing we wanna do is make sure that there is a command, right, or something to display.

[00:05:56]
>> Steve Kinney: There's not just a bunch of white space. We'll console, log it.
>> Steve Kinney: Go with the yellow, seems good. And so we've got that command. What we'll do is run with that parse and evaluate function.
>> Steve Kinney: And then remember we said it's a read, eval, print, and then was the last part?

[00:06:27] It's a loop, right? So right now we're reading. We're parsing any [INAUDIBLE], we're printing it, but then we need to just call it.
>> Steve Kinney: And obviously, you can make this more complicated. This is mostly just to kind of get something quick and dirty up and running. We're just kind of doing the easiest possible version of this for now.

[00:06:49] So we have that in place. Again, this part here, just to explain it, if we're calling this file directly, it will show you this fun welcome prompt and kick off the repl. If you require the file, it wont immediately like, because this console will stop the world and ask you for a thing to do, right?

[00:07:04] So if we want to use it anywhere else and be able to call it programatically, that is going to be somewhat difficult unless we wrap it like this. We effectively have everything in place. It is, if I'm not mistaken, let's just double check. It's in our package JSON as the kind of MPM start.

[00:07:25] So let's take it for a spin.
>> Steve Kinney: Cool, and you can see Welcome to the Dropbear Programming Language, right? You now have an interactive prompt that you can use your programming language with, right? So we can try it out, we can say, add 2 and 2. We get 4 back, our programming language works.

[00:07:46] It does a thing, right? I know, it's like oddly satisfying, right? Yeah, sol lets say add 2 and 4 but then we'll also like subtract.
>> Steve Kinney: We don't have stuff like auto complete and stuff along those lines. There's definitely ways to expand this. I will type out the 3 and 2.

[00:08:11]
>> Steve Kinney: Cool.
>> Steve Kinney: (add 2 4 and then ((subtract 3 2)). Let me see that.
>> Steve Kinney: All right, that works.
>> Steve Kinney: Yes, add two and four and one. My programming language works, it's just my math that doesn't work. [LAUGH] That doesn't seem right. Great, that was really, I hope I inspired confidence in all of you with that.

[00:08:42] Yeah, so 2 plus 4 plus 1 does in fact equal 7. Everything works, the world is okay. So yeah, now we have a repl for our language