sl_compiler/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::env;
use std::ffi::OsString;

pub struct Config {
    pub script: String,
    pub args: Vec<String>,
    pub dump: bool,
    pub run: bool,
    pub globals_pre: bool,
    pub globals_post: bool,
}

pub const VERSION_STRING: &str = env!("VERSION_STRING");

const HELP: &str = r#"sl-compiler - Simple Lisp Compiler
Run sl-compiler.

USAGE:
    sl-compiler [FLAGS] [OPTIONS] [args]

FLAGS:
    -v, --version  Print the version, platform and revision of sl-compiler then exit.
    -h, --help     Print help (this) and exit.

OPTIONS:
    -r, --run          Compile and run the script.
    -d, --dump         Compile and dump the bytecode for the script.
    -g1, --global_pre  Compile and dump the globals before running.
    -g2, --global_post Compile and dump the globals before running.

ARGS:
    <args>...      Script to run with arguments."#;

fn help(_name: &str) {
    println!("{HELP}");
}

fn version() {
    println!("{VERSION_STRING}");
}

fn get_arg(exe_name: &str, args: &mut Vec<OsString>) -> Option<String> {
    if let Some(argument) = args.pop() {
        if let Ok(arg) = argument.into_string() {
            return Some(arg);
        }
    }
    help(exe_name);
    None
}

pub fn get_config() -> Option<Config> {
    let mut script: Option<String> = None;
    let mut command_args: Vec<String> = Vec::new();

    let mut args: Vec<OsString> = env::args_os().collect();

    args.reverse();
    let exe_name = get_arg("unknown", &mut args)?; // Pop off the executable name.

    let mut run = true;
    let mut dump = false;
    let mut globals_pre = false;
    let mut globals_post = false;
    while !args.is_empty() {
        if let Some(argument) = args.pop() {
            if let Ok(arg) = argument.into_string() {
                match &arg[..] {
                    "-r" | "--run" => run = true,
                    "-d" | "--dump" => {
                        run = false;
                        dump = true;
                    }
                    "-g1" | "--globals_pre" => globals_pre = true,
                    "-g2" | "--globals_post" => {
                        globals_post = true;
                        run = true;
                    }
                    "-v" | "--version" => {
                        version();
                        return None;
                    }
                    "-h" | "--help" => {
                        help(&exe_name);
                        return None;
                    }
                    _ => {
                        if script.is_none() {
                            script = Some(arg);
                        } else {
                            command_args.push(arg);
                        }
                    }
                }
            } else {
                help(&exe_name);
                return None;
            }
        }
    }
    if script.is_none() {
        println!("Must provide a script to compile.");
        help(&exe_name);
        return None;
    }
    Some(Config {
        run,
        dump,
        globals_pre,
        globals_post,
        script: script.unwrap(),
        args: command_args,
    })
}