Interp - an Interpreted Programming Language Pt.3 - Setting up the environment

1 minute read

In Pt. 3 we start coding the project and define some command-line arguments.


We will use the Rust language. Why? It’s my favorite language. Rust can be installed with rustup. Start a new Rust project:

cargo new Interp # Will make a directory called 'Interp'


We use StructOpt (found here) for handling our command-line arguments. Include StructOpt to your dependencies.


structopt = "0.3.26"

Create a source file to src/ to define your arguments. I’ll call it


use structopt::StructOpt;

pub struct Opt {

    /// Source filename, None for stdin
    pub source_file: Option<String>,


Add this to your main:


mod opt;

use structopt::StructOpt;

fn main() {
    let opt = opt::Opt::from_args();
    println!("{:?}", opt.source_file)

Let’s run the program: (inside the ‘Interp’ directory)

$ cargo run --help        # Displays help text
$ cargo run source.interp
$ cargo run


Let’s read our source code line by line.


mod opt;

use std::io::{self, BufRead};
use std::fs;
use structopt::StructOpt;

fn main() {
    let opt = opt::Opt::from_args();

    let reader: Box<dyn BufRead>;
    match opt.source_file {
        Some(_filename) => {
            reader = Box::new(io::BufReader::new(fs::File::open(&_filename).unwrap()));
        None => {
            reader = Box::new(io::BufReader::new(io::stdin()));

    for line_str in reader.lines() {
        let line_str = line_str.unwrap();
        println!("{}", line_str);

The above program works like the ‘cat’ unix command. In the next post we will implement hello world.

Source Code

Source code for this part can be found here.

Next Up

Click for Pt. 4 or click here for all parts.