aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: ed8596196a13e2183eda001b6bf6e61e1ecf41d0 (plain) (blame)
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
use std::path::{Path, PathBuf};

mod tokenizer;
mod vm;
mod compiler;

fn main() {
    let file = file_from_args().unwrap_or_else(|| Path::new("tests/simple.tdy").to_owned());
    if let Err(err) = run_file(&file) {
        println!("{}", err);
    }
}

fn file_from_args() -> Option<PathBuf> {
    std::env::args().skip(1).map(|s| Path::new(&s).to_owned()).find(|p| p.is_file())
}

fn run_file(path: &Path) -> Result<(), vm::Error> {
    let tokens = tokenizer::file_to_tokens(path);
    let block = compiler::compile("main", path, tokens);  // path -> str might fail
    vm::run_block(block)
}

#[cfg(test)]
mod tests {
    use super::run_file;
    use std::path::Path;

    #[test]
    fn order_of_operations() {
        let file = Path::new("tests/order-of-operations.tdy");
        assert!(run_file(&file).is_ok());
    }
}