summaryrefslogtreecommitdiff
path: root/dhall_generator/src/lib.rs
blob: 512df94c0d4c4cdbba46863f8318af0dfd53a9db (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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
extern crate proc_macro;
use dhall_core::context::Context;
use dhall_core::*;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::BTreeMap;

#[proc_macro]
pub fn dhall_expr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input_str = input.to_string();
    let expr: Box<Expr<X, Import>> = parser::parse_expr(&input_str).unwrap();
    let no_import =
        |_: &Import| -> X { panic!("Don't use import in dhall!()") };
    let expr = expr.map_embed(&no_import);
    let output = dhall_to_tokenstream(&expr, &Context::new());
    output.into()
}

// Returns an expression of type Expr<_, _>. Expects input variables
// to be of type Box<Expr<_, _>> (future-proof for structural sharing).
fn dhall_to_tokenstream(
    expr: &Expr<X, X>,
    ctx: &Context<Label, ()>,
) -> TokenStream {
    use dhall_core::Expr::*;
    match expr {
        e @ Var(_) => {
            let v = dhall_to_tokenstream_bx(e, ctx);
            quote! { *#v }
        }
        Pi(x, t, b) => {
            let t = dhall_to_tokenstream_bx(t, ctx);
            let b = dhall_to_tokenstream_bx(b, &ctx.insert(x.clone(), ()));
            let x = label_to_tokenstream(x);
            quote! { Pi(#x, #t, #b) }
        }
        Lam(x, t, b) => {
            let t = dhall_to_tokenstream_bx(t, ctx);
            let b = dhall_to_tokenstream_bx(b, &ctx.insert(x.clone(), ()));
            let x = label_to_tokenstream(x);
            quote! { Lam(#x, #t, #b) }
        }
        App(f, a) => {
            let f = dhall_to_tokenstream_bx(f, ctx);
            let a = dhall_to_tokenstream_bx(a, ctx);
            quote! { App(#f, #a) }
        }
        Const(c) => {
            let c = const_to_tokenstream(*c);
            quote! { Const(#c) }
        }
        Builtin(b) => {
            let b = builtin_to_tokenstream(*b);
            quote! { Builtin(#b) }
        }
        BinOp(o, a, b) => {
            let o = binop_to_tokenstream(*o);
            let a = dhall_to_tokenstream_bx(a, ctx);
            let b = dhall_to_tokenstream_bx(b, ctx);
            quote! { BinOp(#o, #a, #b) }
        }
        OptionalLit(t, es) => {
            let t = option_to_tokenstream(t, ctx);
            let es = vec_to_tokenstream(es, ctx);
            quote! { OptionalLit(#t, #es) }
        }
        ListLit(t, es) => {
            let t = option_to_tokenstream(t, ctx);
            let es = vec_to_tokenstream(es, ctx);
            quote! { ListLit(#t, #es) }
        }
        Record(m) => {
            let m = map_to_tokenstream(m, ctx);
            quote! { Record(#m) }
        }
        e => unimplemented!("{:?}", e),
    }
}

// Returns an expression of type Box<Expr<_, _>>
fn dhall_to_tokenstream_bx(
    expr: &Expr<X, X>,
    ctx: &Context<Label, ()>,
) -> TokenStream {
    use dhall_core::Expr::*;
    match expr {
        Var(V(s, n)) => {
            match ctx.lookup(&s, *n) {
                // Non-free variable; interpolates as itself
                Some(()) => {
                    let s: String = s.clone().into();
                    quote! { bx(Var(V(#s.into(), #n))) }
                }
                // Free variable; interpolates as a rust variable
                None => {
                    let s: String = s.clone().into();
                    // TODO: insert appropriate shifts ?
                    let v: TokenStream = s.parse().unwrap();
                    quote! { {
                        let x: Box<Expr<_, _>> = #v.clone();
                        x
                    } }
                }
            }
        }
        e => bx(dhall_to_tokenstream(e, ctx)),
    }
}

fn builtin_to_tokenstream(b: Builtin) -> TokenStream {
    format!("{:?}", b).parse().unwrap()
}

fn const_to_tokenstream(c: Const) -> TokenStream {
    format!("{:?}", c).parse().unwrap()
}

fn binop_to_tokenstream(b: BinOp) -> TokenStream {
    format!("{:?}", b).parse().unwrap()
}

fn label_to_tokenstream(l: &Label) -> TokenStream {
    let l = String::from(l.clone());
    quote! { #l.into() }
}

fn map_to_tokenstream(
    m: &BTreeMap<Label, Expr<X, X>>,
    ctx: &Context<Label, ()>,
) -> TokenStream {
    let (keys, values): (Vec<TokenStream>, Vec<TokenStream>) = m
        .iter()
        .map(|(k, v)| (label_to_tokenstream(k), dhall_to_tokenstream(v, ctx)))
        .unzip();
    quote! { {
        let mut m = BTreeMap::new();
        #( m.insert(#keys, #values); )*
        m
    } }
}

fn option_to_tokenstream(
    e: &Option<Box<Expr<X, X>>>,
    ctx: &Context<Label, ()>,
) -> TokenStream {
    let e = e.as_ref().map(|x| dhall_to_tokenstream_bx(x, ctx));
    match e {
        Some(x) => quote! { Some(#x) },
        None => quote! { None },
    }
}

fn vec_to_tokenstream(
    e: &Vec<Expr<X, X>>,
    ctx: &Context<Label, ()>,
) -> TokenStream {
    let e = e.iter().map(|x| dhall_to_tokenstream(x, ctx));
    quote! { vec![ #(#e),* ] }
}

fn bx(x: TokenStream) -> TokenStream {
    quote! { bx(#x) }
}