Check out a free preview of the full Building Your Own Programming Language course

The "REPLs" 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 defines what a REPL is, gives different examples of existing REPLs, and introduces the next sections.

Preview
Close

Transcript from the "REPLs" Lesson

[00:00:00]
>> Steve Kinney: We can figure out the structure of our language now. We know what a number is. We know what an identifier is. We know what a expression is. We basically have the very simple structure to our language. We do have some problems that we kind of need to address however.

[00:00:20]
We can say, yeah, cool, add two numbers. What does it mean to add two numbers, right? And where are we adding these numbers, right? We're gonna need a few things here. We're gonna need a way to actually use this language. That'd be cool. And we're also gonna need some amount of a standard library, right?

[00:00:40]
Some amount of things that are available, right? JavaScript language itself or any language that we've used, even some languages more than JavaScript, right, have a set of built in objects, and built in data structures, and built in functions that we can use. Even if it's the plus symbol or something along those lines, or Ruby's innumerable, or the Python standard library.

[00:01:00]
We can do a lot of stuff with just the language even before the third-party libraries. It's basically the foundation for all of that. So we're gonna do a few things, we're going to build a REPL. I'll talk about what that is. We're gonna build a little CLI tool.

[00:01:14]
We're also going to build the kind of base built in functions that you can totally expand, right? You could actually if you wanted this language to have all the powers of low dash, you could have that as built ins in the language, right? And but you have to put that into the standard library using kind of JavaScript underneath it.

[00:01:34]
So what is a REPL? A REPL is a read-evaluate-print loop. REPL, [LAUGH] it's an acronym, and it does what it says. Basically, you give it some expression, it reads it, it evaluates it, right, which is the part we have not implemented yet. We can parse but we don't and once we have the structure, we need to actually then execute it, right?

[00:02:02]
And then once you've executed it, it's effectively printing something to the screen saying something happened. You can run a function but if you don't tell anyone did it really happen? And then once we have read, evaluated and printed the result then we give him another prompt, right? And we allow them to go ahead and play the whole game over.

[00:02:24]
You have used a REPL at some point of your life likely. Node has a REPL built in. If you just type node, you'll get one where you can start typing things. Python, Ruby, almost every language I can think of has one. So we are gonna need a few things in place.

[00:02:41]
I kind of alluded to in the ramp up to this. We've just been taking for granted that there's a add function in the language, right, or a subtract function in the language, right? There can be [LAUGH]. We're the ones making this language and it means that we got to put them in there, right?

[00:03:01]
And we're going to stand on the shoulders of our JavaScript giants so that we're not again shifting bets for the next week and a half, right? But we need to actually add them in so that when someone says add two and two or subtract or multiply or modular and like I said, you can add anything you want in there.

[00:03:17]
Right, we'll do obviously just a kind of basic set because there's not much more learning that happens from doing the 17th one. And so we'll kind of use some of those basic things that are kind of built in there as well. And so we're going to use JavaScript for this so effectively we'll create an add function.

[00:03:35]
In we have a little bit differently cuz we don't necessarily have operators in our language. Everything is a function expression currently in a list. And we know that our add or subtract can take an unlimited series of arguments. So we'll have to do a little bit of work.

[00:03:50]
Now, some of that I've done for you. It's what I care more about is the wiring up. I'll take you on a tour. And then once I've taken you on a tour of some of the kind of structure that I've built, we'll talk a little bit about, okay, now how do we actually go ahead and evaluate this language.

[00:04:04]
So let's actually look at some of the code that we have in the quote-unquote standard library. And you'll be able to add stuff to the standard library in.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now