Building Your Own Programming Language

Running Files from the CLI

Steve Kinney

Steve Kinney

Building Your Own Programming Language

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

The "Running Files from the 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 CLI, create a command that allows developers to access the dropbear shell, and create a dropbear file extension.


Transcript from the "Running Files from the CLI" Lesson

>> Steve Kinney: feels like the other thing that it would be nice to be able to do is to call it from the command line. Right? Like to be able to say, hey, read and evaluate this file, right? A thing that we could also choose to do is add another command where we for instance evaluate a file and had an output file where we put it out to another file.

There's a lot of ways that you can extend this. A lot of it is ancillary to the actual. It is definitely probably key that you have somewhere that you're able to use your language, otherwise what are you doing? But we're not gonna go all the way into that.

To get out of it it's just Ctrl+C to click the prompt and you're back on your normal command line, right? So, again, oddly satisfying, when we take a break if you need a few minutes, if you're watching the video, you need to pause and just marvel at your own creation.

I get it. I understand. I did it. [LAUGH] Cool. So let's hop back over. Let's look at the CLI code. CLI code, we put in a bin directory is kind of the node convention. And you can see it's just called drop bear. And I still wrote most of it but I will kind of take you through it.

I think it deserves some explaining and talk about how you could theoretically expand upon it. So the important part here is we have this shebang, right? Hash bang, shebang, that basically says okay even though it doesn't have the .js, you're not calling it with node, we can in fact, it will run in a node environment.

I mean theory this could be bash too if you wanted to. We'll pull in that parse and evaluate. We want to do two things. We want to be able to call the repl from this command line tool. And we wanna be able to read a file and log the output, all right?

And so we pulled in the ability to read from the file system, we pulled in the ability to parse and evaluate. What's going on over here? So, you get this process at argv, argv is all of the arguments past the command line. And we have to slice two cuz the first one is node, the second one is the command itself, right?

Got it, noted. Don't need those, right? So this will actually give us the rest of it, right? So if there's no command if they just type in, drop bear. Cool, like shoot them into the REPL, right? Whatever they type they can type in capital whatever. They can type run, will also then grab another argument and be able to read the file.

We'll grab the rest of the args, effectively the third argue and forward. So like, we can add some additional error checking here as well, but basically, they can say, drop bear run and then a file name and we'll send it to the same parse and evaluator, right? You can do flags through output but you'd also do a lot of that in bash, right?

If you just use the angle bracket, you can actually output to another file as well, so I didn't feel like that was the best use of our time, but we can take it for a spin. But then, how do you use it? Well, you have a few options, right?

Like, with the bin directory and node modules, if it was a dependency, for instance, you could do like either from your package JSON. If it was a script, you could run it, or with MPX, you can run it. But also, if you globally installed it, you now have the Dropbear programming language in your path.

But you're like, I don't really need to publish it to MPM yet, totally cool. You can still use it from anywhere in your system by running MPM link. So let's actually take that for a spin.
>> Steve Kinney: That's why I make sure I'm not in the actual Dropbear prompt now.

You can run npm link.
>> Steve Kinney: This is effectively installing it globally. All right, and you can see that I can type it in dropbear now. And I'm in the command line for dropbear.
>> Steve Kinney: Right now our language is predominately math like we now have the power to add whatever else we want to it right?

And so if you think about it, like we talked about the fact that like languages have a lot of different avenues. They can be a programming language per se. But they can also do other commands, like a lot of this like being able to parse and understand the syntactic structure of thing could be useful for writing your own command line tools right?

Like we use it like I mentioned earlier for like SQL and stuff along those lines. There's a ton of uses that a lot of these seals like on top of the fun like I'm still enamored by this and I've been doing it for a while. There's a lot of like just generally practical ways that like, otherwise you'd be sitting there probably like regxing stuff, so on and so forth, right?

Like, and one of the things that got me into this was like, basically trying to resurrect HTML, which went really poorly. So we can do that. And the other thing is a run if you look in, there's an Examples folder. If you look in there, there's this example.dbr.

I spent a lot of time trying to come up with a file extension that wasn't taken by anything else on my file system. And it wasn't a full .dropbear, which I thought about for a while, but I decided that I didn't want to do that. So we can do a dropbear and we can say run.

Do examples
>> Steve Kinney: Right? I can now like read a file and execute it, right? So we've got some of the kind of like hallmarks of a tool that we can build on our system. Be able to do interesting things right, like our language is still pretty simple but it's got a lot of the moving pieces of a real language and if you NPM publish this it will work like I said, we can just type dropbear now from anywhere.

We can also run and execute files. If you want to do the output somewhere I can say like I can just do the carrot command right? TXD, how about that, like catoutput.txt.
>> Steve Kinney: There's my six. So now I can theoretically run programs as I add more things to this language.

There is a few libraries. Like Minimist is actually in the package json here, which will like allow you to parse flags and do additional other things if you wanted to additional features to your command line tool. That is a thing that you can and arguably should do as well, right?

This library right here, Minimist, which is incredibly difficult to say out loud in front of people, great. So we have everything in place there. Now, what if we wanted to transpire this language? Right or do things with that ASD? What if we wanted to add additional features? Sometimes it makes sense to do that at parse time, right?

Sometimes you do want to transform the language over time. I mean, as I said before, most of us when we're using JavaScript are not using JavaScript isolation. We are also using JavaScript as we get it to the point where we can transform it into other versions of JavaScript that we can use.

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