summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: c180302d8bd20a2980aa3b4ea97cb0c1bdf35029 (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
use chomp::{
    ast::{
        convert::{Context, Convert},
        typed::{FlastContext, Type},
    },
    nibble::Expression,
};
use proc_macro2::Span;
use std::process::exit;
use std::{
    error::Error,
    io::{self, Read, Write},
};
use syn::Ident;

fn main() {
    let mut input = String::new();
    let res = io::stdin()
        .read_to_string(&mut input)
        .map_err(|e| Box::new(e) as Box<dyn Error>)
        .and_then(|_| syn::parse_str(&input).map_err(|e| Box::new(e) as Box<dyn Error>))
        .and_then(|nibble: Expression| {
            nibble
                .convert(&mut Context::new())
                .well_typed(&mut FlastContext::new())
                .map_err(|e| Box::new(e) as Box<dyn Error>)
        })
        .map(|(typed, _)| typed.emit_code(Ident::new("Ast", Span::call_site())))
        .and_then(|code| {
            write!(io::stdout(), "{:#}", code).map_err(|e| Box::new(e) as Box<dyn Error>)
        });

    if let Err(e) = res {
        eprintln!("{}", e);
        drop(e);
        exit(1)
    }
}