Building Your Own Programming Language Running Files from the CLI
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.
[00:00:19] 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.
[00:00:37] 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.
[00:00:53] 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.
[00:01:15] 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.
[00:01:36] 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?
[00:01:54] 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?
[00:02:17] 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.
[00:02:43] 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?
[00:03:02] 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?
[00:03:16] 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.
[00:03:33] 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.
[00:03:50] 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?
[00:04:23] 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?
[00:04:41] 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?
[00:05:01] 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.
[00:05:17] 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.
[00:05:32] 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.
[00:05:59] 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.
[00:06:21] 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?
[00:06:40] 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?