protospec-build 0.3.0

One binary format language to rule them all, One binary format language to find them, One binary format language to bring them all and in the darkness bind them.
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::{
    cmp::{Ordering, PartialOrd},
    ops::{Add, Div, Mul, Neg, Rem, Sub},
};

use proc_macro2::Literal;

use super::*;

#[derive(PartialEq)]
pub enum ConstValue {
    Int(ConstInt),
    Bool(bool),
    String(Vec<u8>),
    F32(f32),
    F64(f64),
}

impl PartialOrd for ConstValue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (ConstValue::Int(i1), ConstValue::Int(i2)) => i1.partial_cmp(i2),
            (ConstValue::F32(i1), ConstValue::F32(i2)) => i1.partial_cmp(i2),
            (ConstValue::F64(i1), ConstValue::F64(i2)) => i1.partial_cmp(i2),
            _ => None,
        }
    }
}

impl Add for ConstValue {
    type Output = Option<Self>;

    fn add(self, other: Self) -> Self::Output {
        match (self, other) {
            (ConstValue::Int(i1), ConstValue::Int(i2)) => Some(ConstValue::Int(i1.add(i2)?)),
            (ConstValue::F32(i1), ConstValue::F32(i2)) => Some(ConstValue::F32(i1.add(i2))),
            (ConstValue::F64(i1), ConstValue::F64(i2)) => Some(ConstValue::F64(i1.add(i2))),
            (ConstValue::String(mut i1), ConstValue::String(i2)) => {
                i1.extend(i2);
                Some(ConstValue::String(i1))
            }
            _ => None,
        }
    }
}

impl Sub for ConstValue {
    type Output = Option<Self>;

    fn sub(self, other: Self) -> Self::Output {
        match (self, other) {
            (ConstValue::Int(i1), ConstValue::Int(i2)) => Some(ConstValue::Int(i1.sub(i2)?)),
            (ConstValue::F32(i1), ConstValue::F32(i2)) => Some(ConstValue::F32(i1.sub(i2))),
            (ConstValue::F64(i1), ConstValue::F64(i2)) => Some(ConstValue::F64(i1.sub(i2))),
            _ => None,
        }
    }
}

impl Mul for ConstValue {
    type Output = Option<Self>;

    fn mul(self, other: Self) -> Self::Output {
        match (self, other) {
            (ConstValue::Int(i1), ConstValue::Int(i2)) => Some(ConstValue::Int(i1.mul(i2)?)),
            (ConstValue::F32(i1), ConstValue::F32(i2)) => Some(ConstValue::F32(i1.mul(i2))),
            (ConstValue::F64(i1), ConstValue::F64(i2)) => Some(ConstValue::F64(i1.mul(i2))),
            _ => None,
        }
    }
}

impl Div for ConstValue {
    type Output = Option<Self>;

    fn div(self, other: Self) -> Self::Output {
        match (self, other) {
            (ConstValue::Int(i1), ConstValue::Int(i2)) => Some(ConstValue::Int(i1.div(i2)?)),
            (ConstValue::F32(i1), ConstValue::F32(i2)) => Some(ConstValue::F32(i1.div(i2))),
            (ConstValue::F64(i1), ConstValue::F64(i2)) => Some(ConstValue::F64(i1.div(i2))),
            _ => None,
        }
    }
}

impl Rem for ConstValue {
    type Output = Option<Self>;

    fn rem(self, other: Self) -> Self::Output {
        match (self, other) {
            (ConstValue::Int(i1), ConstValue::Int(i2)) => Some(ConstValue::Int(i1.rem(i2)?)),
            (ConstValue::F32(i1), ConstValue::F32(i2)) => Some(ConstValue::F32(i1.rem(i2))),
            (ConstValue::F64(i1), ConstValue::F64(i2)) => Some(ConstValue::F64(i1.rem(i2))),
            _ => None,
        }
    }
}

impl Neg for ConstValue {
    type Output = Option<Self>;

    #[allow(unreachable_code)]
    fn neg(self) -> Self::Output {
        match self {
            ConstValue::Int(i1) => Some(ConstValue::Int(i1.neg()?)),
            ConstValue::F32(i1) => Some(ConstValue::F32(i1.neg())),
            ConstValue::F64(i1) => Some(ConstValue::F64(i1.neg())),
            _ => None,
        }
    }
}

impl ConstValue {
    pub fn emit(&self) -> TokenStream {
        use ConstInt::*;
        match self {
            ConstValue::Int(i) => match i {
                I8(x) => quote! { #x },
                I16(x) => quote! { #x },
                I32(x) => quote! { #x },
                I64(x) => quote! { #x },
                I128(x) => quote! { #x },
                U8(x) => quote! { #x },
                U16(x) => quote! { #x },
                U32(x) => quote! { #x },
                U64(x) => quote! { #x },
                U128(x) => quote! { #x },
            },
            ConstValue::Bool(b) => quote! { #b },
            ConstValue::String(c) => {
                let c = Literal::byte_string(&c[..]);
                quote! {
                    #c
                }
            }
            ConstValue::F32(s) => quote! { #s },
            ConstValue::F64(s) => quote! { #s },
        }
    }

    pub fn cast_to(&self, target: &Type) -> Option<Self> {
        match (self, target) {
            (ConstValue::Int(i1), Type::Scalar(s)) => Some(ConstValue::Int(i1.cast_to(*s))),
            (ConstValue::F32(i1), Type::F32) => Some(ConstValue::F32(*i1 as f32)),
            (ConstValue::F64(i1), Type::F32) => Some(ConstValue::F32(*i1 as f32)),
            (ConstValue::F32(i1), Type::F64) => Some(ConstValue::F64(*i1 as f64)),
            (ConstValue::F64(i1), Type::F64) => Some(ConstValue::F64(*i1 as f64)),
            //todo: int <-> float casting
            _ => None,
        }
    }

    fn to_bool(&self) -> Option<bool> {
        match self {
            ConstValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    fn to_int(&self) -> Option<ConstInt> {
        match self {
            ConstValue::Int(b) => Some(*b),
            _ => None,
        }
    }
}

pub fn eval_const_expression(expr: &Expression) -> Option<ConstValue> {
    use Expression::*;
    match expr {
        Binary(c) => {
            let left = eval_const_expression(&c.left)?;
            let right = eval_const_expression(&c.right)?;
            Some(match c.op {
                BinaryOp::Lt => ConstValue::Bool(left < right),
                BinaryOp::Gt => ConstValue::Bool(left > right),
                BinaryOp::Lte => ConstValue::Bool(left <= right),
                BinaryOp::Gte => ConstValue::Bool(left >= right),
                BinaryOp::Eq => ConstValue::Bool(left == right),
                BinaryOp::Ne => ConstValue::Bool(left != right),
                BinaryOp::Or => ConstValue::Bool(left.to_bool()? || right.to_bool()?),
                BinaryOp::And => ConstValue::Bool(left.to_bool()? && right.to_bool()?),
                BinaryOp::BitOr => ConstValue::Int((left.to_int()? | right.to_int()?)?),
                BinaryOp::BitAnd => ConstValue::Int((left.to_int()? & right.to_int()?)?),
                BinaryOp::BitXor => ConstValue::Int((left.to_int()? ^ right.to_int()?)?),
                BinaryOp::Shr => ConstValue::Int((left.to_int()? >> right.to_int()?)?), // todo: cast signedness to ensure proper shr is used in rust
                BinaryOp::Shl => ConstValue::Int((left.to_int()? << right.to_int()?)?),
                BinaryOp::ShrSigned => ConstValue::Int((left.to_int()? >> right.to_int()?)?),
                BinaryOp::Add => ConstValue::Int((left.to_int()? + right.to_int()?)?),
                BinaryOp::Sub => ConstValue::Int((left.to_int()? - right.to_int()?)?),
                BinaryOp::Mul => ConstValue::Int((left.to_int()? * right.to_int()?)?),
                BinaryOp::Div => ConstValue::Int((left.to_int()? / right.to_int()?)?),
                BinaryOp::Mod => ConstValue::Int((left.to_int()? % right.to_int()?)?),
                BinaryOp::Elvis => {
                    unimplemented!("cannot use elvis operator (?:) in const context")
                }
            })
        }
        Member(c) => {
            let target = eval_const_expression(&c.target)?;
            let member = eval_const_expression(&c.member.value)?;
            match (target, member) {
                (ConstValue::Int(target), ConstValue::Int(member)) => {
                    // todo: find better way to get same-typed zero
                    Some(ConstValue::Bool((target & member)? != (member - member)?))
                },
                _ => unimplemented!("can not use member exprs on nonint targets")
            }
        }
        Unary(c) => {
            let inner = eval_const_expression(&c.inner)?;
            Some(match c.op {
                UnaryOp::Negate => ConstValue::Int((-inner.to_int()?)?),
                UnaryOp::Not => ConstValue::Bool(!inner.to_bool()?),
                UnaryOp::BitNot => ConstValue::Int((!inner.to_int()?)?),
            })
        }
        Cast(c) => {
            let inner = eval_const_expression(&c.inner)?;
            inner.cast_to(&c.type_)
        }
        ArrayIndex(_) => {
            unimplemented!("can not use array indexing in const")
        }
        EnumAccess(c) => eval_const_expression(&c.variant.value),
        Int(c) => Some(ConstValue::Int(c.value)),
        ConstRef(c) => eval_const_expression(&c.value),
        InputRef(_) => {
            unimplemented!("cannot access input in constant");
        }
        FieldRef(_) => {
            unimplemented!("cannot access field in constant");
        }
        Str(c) => Some(ConstValue::String(c.content.clone())),
        Ternary(c) => {
            let condition = eval_const_expression(&c.condition)?;
            let if_true = eval_const_expression(&c.if_true)?;
            let if_false = eval_const_expression(&c.if_false)?;

            match condition {
                ConstValue::Bool(true) => Some(if_true),
                ConstValue::Bool(false) => Some(if_false),
                _ => None,
            }
        }
        Bool(c) => Some(ConstValue::Bool(*c)),
        Call(_) => {
            unimplemented!("cannot call ffi in constant");
        }
    }
}

pub fn emit_expression<F: Fn(&Arc<Field>) -> TokenStream>(
    expr: &Expression,
    ref_resolver: &F,
) -> TokenStream {
    use Expression::*;
    match expr {
        Binary(c) => {
            let left = emit_expression(&c.left, ref_resolver);
            let right = emit_expression(&c.right, ref_resolver);
            if c.op == BinaryOp::Elvis {
                quote! {
                    (#left).unwrap_or(#right)
                }
            } else {
                let op = match c.op {
                    BinaryOp::Lt => quote! { < },
                    BinaryOp::Gt => quote! { > },
                    BinaryOp::Lte => quote! { <= },
                    BinaryOp::Gte => quote! { >= },
                    BinaryOp::Eq => quote! { == },
                    BinaryOp::Ne => quote! { != },
                    BinaryOp::Or => quote! { || },
                    BinaryOp::And => quote! { && },
                    BinaryOp::BitOr => quote! { | },
                    BinaryOp::BitAnd => quote! { & },
                    BinaryOp::BitXor => quote! { ^ },
                    BinaryOp::Shr => quote! { >> },
                    BinaryOp::Shl => quote! { << },
                    BinaryOp::ShrSigned => quote! { >>> },
                    BinaryOp::Add => quote! { + },
                    BinaryOp::Sub => quote! { - },
                    BinaryOp::Mul => quote! { * },
                    BinaryOp::Div => quote! { / },
                    BinaryOp::Mod => quote! { % },
                    BinaryOp::Elvis => unimplemented!(),
                };
                quote! {
                    ((#left) #op (#right))
                }
            }
        }
        Member(c) => {
            let target = emit_expression(&c.target, ref_resolver);
            let get_member = format_ident!("{}", c.member.name.to_snake());
            quote! {
                #target.#get_member()
            }
        }
        Unary(c) => {
            let inner = emit_expression(&c.inner, ref_resolver);
            let op = match c.op {
                UnaryOp::Negate => quote! { - },
                UnaryOp::Not => quote! { ! },
                UnaryOp::BitNot => quote! { ~ },
            };
            quote! {
                (#op #inner)
            }
        }
        Cast(c) => {
            let inner = emit_expression(&c.inner, ref_resolver);
            let target = emit_type_ref(&c.type_);
            quote! {
                (#inner) as #target
            }
        }
        ArrayIndex(c) => {
            let array = emit_expression(&c.array, ref_resolver);
            let index = emit_expression(&c.index, ref_resolver);

            quote! {
                (#array)[#index]
            }
        }
        EnumAccess(c) => {
            let enum_name = emit_ident(&c.enum_field.name);
            let enum_variant_name = emit_ident(&c.variant.name);
            let enum_type = emit_type_ref(&c.variant.type_);
            quote! {
                #enum_name::#enum_variant_name as #enum_type
            }
        }
        Int(c) => {
            use ConstInt::*;
            match c.value {
                I8(x) => quote! { #x },
                I16(x) => quote! { #x },
                I32(x) => quote! { #x },
                I64(x) => quote! { #x },
                I128(x) => quote! { #x },
                U8(x) => quote! { #x },
                U16(x) => quote! { #x },
                U32(x) => quote! { #x },
                U64(x) => quote! { #x },
                U128(x) => quote! { #x },
            }
        }
        ConstRef(c) => {
            let c = format_ident!("{}", c.name);
            quote! {
                #c
            }
        }
        InputRef(c) => {
            let c = format_ident!("{}", c.name);
            quote! {
                #c
            }
        }
        FieldRef(c) => ref_resolver(c),
        Str(c) => {
            let c = Literal::byte_string(&c.content[..]);
            quote! {
                #c
            }
        }
        Ternary(c) => {
            let condition = emit_expression(&c.condition, ref_resolver);
            let if_true = emit_expression(&c.if_true, ref_resolver);
            let if_false = emit_expression(&c.if_false, ref_resolver);

            quote! {
                if #condition {
                    #if_true
                } else {
                    #if_false
                }
            }
        }
        Bool(c) => {
            quote! {
                #c
            }
        }
        Call(c) => {
            let mut arguments = vec![];
            for argument in &c.arguments {
                let expression = emit_expression(argument, ref_resolver);
                arguments.push(FFIArgumentValue {
                    type_: argument.get_type().expect("missing type in ffi argument"),
                    present: true,
                    value: expression,
                });
            }
            for argument in c.function.arguments[c.arguments.len()..].iter() {
                arguments.push(FFIArgumentValue {
                    type_: argument.type_.clone().unwrap_or(Type::Bool),
                    present: false,
                    value: quote! {},
                });
            }
            c.function.inner.call(&arguments[..])
        }
    }
}