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
|
use std::{
env,
error::Error,
fmt::Display,
fs,
io::{Read, Write},
path::Path,
process::Command,
};
use chomp::{
chomp::{
check::{InlineCall, TypeCheck},
context::Context,
visit::Visitable,
},
lower::{rust::RustBackend, Backend, GenerateCode},
nibble::cst::File,
};
const PATH: &str = "src/nibble.nb";
#[derive(Debug)]
struct UndecVar;
impl Display for UndecVar {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Undeclared variable somewhere.")
}
}
impl Error for UndecVar {}
fn main() {
println!("cargo:rerun-if-changed={}", PATH);
let out_dir = env::var("OUT_DIR").unwrap();
let mut input = String::new();
fs::File::open(PATH)
.map_err(|e| Box::new(e) as Box<dyn Error>)
.and_then(|mut file| {
file.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: File| nibble.convert().ok_or(Box::new(UndecVar) as Box<dyn Error>))
.and_then(|(funs, goal)| {
funs.into_iter()
.try_rfold(goal, |goal, function| {
goal.fold(&mut InlineCall::new(function))
})
.map_err(|e| Box::new(e) as Box<dyn Error>)
})
.and_then(|term| {
let mut context = Context::default();
term.fold(&mut TypeCheck {
context: &mut context,
})
.map_err(|e| Box::new(e) as Box<dyn Error>)
})
.map(|typed| {
let mut backend = RustBackend::default();
let id = typed.gen(&mut backend);
backend.emit_code(id)
})
.and_then(|code| {
fs::File::create(Path::new(&out_dir).join("nibble.rs"))
.and_then(|mut f| write!(f, "{}", code))
.map_err(|e| Box::new(e) as Box<dyn Error>)
})
.unwrap();
Command::new("rustfmt")
.arg(&format!("{}/nibble.rs", out_dir))
.status()
.unwrap();
}
|