summaryrefslogtreecommitdiff
path: root/dhall_generator/src/dhall_expr.rs
blob: 41e558b70cf7337c1f1714f18bd577719ae62cd5 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
extern crate proc_macro;
use dhall_core::context::Context;
use dhall_core::*;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::BTreeMap;
use std::rc::Rc;

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

// Returns an expression of type Expr<_, _>. Expects interpolated variables
// to be of type Rc<Expr<_, _>>.
fn dhall_to_tokenstream(
    expr: &DhallExpr,
    ctx: &Context<Label, ()>,
) -> TokenStream {
    use dhall_core::Expr::*;
    match expr.as_ref() {
        Var(_) => {
            let v = dhall_to_tokenstream_bx(expr, 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! { dhall_core::Expr::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! { dhall_core::Expr::Lam(#x, #t, #b) }
        }
        App(f, a) => {
            let f = dhall_to_tokenstream_bx(f, ctx);
            let a = vec_to_tokenstream(a, ctx);
            quote! { dhall_core::Expr::App(#f, #a) }
        }
        Const(c) => {
            let c = const_to_tokenstream(*c);
            quote! { dhall_core::Expr::Const(#c) }
        }
        Builtin(b) => {
            let b = builtin_to_tokenstream(*b);
            quote! { dhall_core::Expr::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! { dhall_core::Expr::BinOp(#o, #a, #b) }
        }
        NaturalLit(n) => {
            quote! { dhall_core::Expr::NaturalLit(#n) }
        }
        BoolLit(b) => {
            quote! { dhall_core::Expr::BoolLit(#b) }
        }
        EmptyOptionalLit(x) => {
            let x = dhall_to_tokenstream_bx(x, ctx);
            quote! { dhall_core::Expr::EmptyOptionalLit(#x) }
        }
        NEOptionalLit(x) => {
            let x = dhall_to_tokenstream_bx(x, ctx);
            quote! { dhall_core::Expr::NEOptionalLit(#x) }
        }
        EmptyListLit(t) => {
            let t = dhall_to_tokenstream_bx(t, ctx);
            quote! { dhall_core::Expr::EmptyListLit(#t) }
        }
        NEListLit(es) => {
            let es = vec_to_tokenstream(es, ctx);
            quote! { dhall_core::Expr::NEListLit(#es) }
        }
        RecordType(m) => {
            let m = map_to_tokenstream(m, ctx);
            quote! { dhall_core::Expr::RecordType(#m) }
        }
        RecordLit(m) => {
            let m = map_to_tokenstream(m, ctx);
            quote! { dhall_core::Expr::RecordLit(#m) }
        }
        UnionType(m) => {
            let m = map_to_tokenstream(m, ctx);
            quote! { dhall_core::Expr::UnionType(#m) }
        }
        e => unimplemented!("{:?}", e),
    }
}

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

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

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

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

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

fn map_to_tokenstream(
    m: &BTreeMap<Label, Rc<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_bx(&*v, ctx))
        })
        .unzip();
    quote! { {
        use std::collections::BTreeMap;
        let mut m = BTreeMap::new();
        #( m.insert(#keys, #values); )*
        m
    } }
}

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

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