aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2a8b2744698af50e231227b7e79d9d5a374f85fb (plain)
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
mod ast;
mod checker;
mod checker_set;
mod checker_signature;
mod checker_state;
mod parser;

use std::env;
use std::fs;
use std::process;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use tracing_tree::HierarchicalLayer;

fn main() {
    let args: Vec<String> = env::args().skip(1).collect();

    if args.is_empty() {
        eprintln!("Usage: makkai [--debug] <file.makkai> [file.makkai ...]");
        process::exit(1);
    }

    let debug_mode = args.first().map_or(false, |arg| arg == "--debug");

    if debug_mode {
        tracing_subscriber::registry()
            .with(
                HierarchicalLayer::new(2)
                    .with_targets(false)
                    .with_bracketed_fields(true),
            )
            .init();
    }

    let files: Vec<String> = if debug_mode {
        args.into_iter().skip(1).collect()
    } else {
        args
    };

    if files.is_empty() {
        eprintln!("Usage: makkai [--debug] <file.makkai> [file.makkai ...]");
        process::exit(1);
    }

    for file in files {
        let src = match fs::read_to_string(&file) {
            Ok(content) => content,
            Err(e) => {
                let msg = match e.kind() {
                    std::io::ErrorKind::NotFound => "file not found".to_string(),
                    _ => format!("io error: {}", e),
                };
                eprintln!("{}: {}", file, msg);
                process::exit(1);
            }
        };

        let programme = match parser::parse_result(&src) {
            Ok(p) => p,
            Err(parse_err) => {
                eprintln!("{}: Error\n\t{}", file, parse_err);
                process::exit(1);
            }
        };

        if let Err(check_err) = programme.check() {
            eprintln!("{} Error\n\t{}", file, check_err);
            process::exit(1);
        } else {
            println!("{} Ok", file);
        }
    }
}