Interp - an Interpreted Programming Language Pt.3 - Setting up the environment
In Pt. 3 we start coding the project and define some command-line arguments.
Environment
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'
StructOpt
We use StructOpt (found here) for handling our command-line arguments. Include StructOpt to your dependencies.
Cargo.toml:
[dependencies]
structopt = "0.3.26"
Create a source file to src/ to define your arguments. I’ll call it opt.rs.
src/opt.rs:
use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt()]
pub struct Opt {
/// Source filename, None for stdin
pub source_file: Option<String>,
}
Add this to your main:
src/main.rs:
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
Some("source.interp")
$ cargo run
None
Input
Let’s read our source code line by line.
src/main.rs:
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.