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
use etk_ops::Immediate;
use num_bigint::{BigInt, Sign};
use snafu::{Backtrace, Snafu};
use std::convert::TryFrom;
use std::fmt::{self, Debug};
use super::expression::{Expression, Terminal};
use super::macros::ExpressionMacroInvocation;
#[derive(Snafu, Debug)]
#[snafu(context(suffix(Context)))]
pub struct TryFromSliceError {
backtrace: Backtrace,
}
impl From<std::convert::Infallible> for TryFromSliceError {
fn from(e: std::convert::Infallible) -> Self {
match e {}
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Imm {
pub tree: Expression,
}
impl Imm {
pub fn with_label<S: Into<String>>(s: S) -> Self {
Terminal::Label(s.into()).into()
}
pub fn with_variable<S: Into<String>>(s: S) -> Self {
Terminal::Variable(s.into()).into()
}
pub fn with_expression(e: Expression) -> Self {
e.into()
}
pub fn with_macro(m: ExpressionMacroInvocation) -> Self {
Expression::Macro(m).into()
}
}
impl From<Vec<u8>> for Imm {
fn from(konst: Vec<u8>) -> Self {
Imm::from(Terminal::Number(BigInt::from_bytes_be(Sign::Plus, &konst)))
}
}
impl fmt::Display for Imm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.tree)
}
}
macro_rules! impl_from {
($ii:literal;) => {
impl From<[u8; $ii]> for Imm {
fn from(konst: [u8; $ii]) -> Self {
Imm::from(Terminal::Number(BigInt::from_bytes_be(
Sign::Plus,
&konst,
)))
}
}
};
($ii:literal; $ty:ty $(, $rest:ty)* $(,)*) => {
impl From<$ty> for Imm {
fn from(x: $ty) -> Self {
Imm::from(Terminal::Number(x.into()))
}
}
impl_from!($ii; $($rest,)*);
}
}
macro_rules! impl_try_from_slice {
($ii:literal) => {
impl TryFrom<&[u8]> for Imm {
type Error = TryFromSliceError;
fn try_from(x: &[u8]) -> Result<Self, Self::Error> {
if x.len() > $ii {
return TryFromSliceContext.fail();
}
Ok(Imm::from(Terminal::Number(BigInt::from_bytes_be(
Sign::Plus,
x,
))))
}
}
};
}
impl_from!(1;);
impl_from!(2;);
impl_from!(3;);
impl_from!(4;);
impl_from!(5;);
impl_from!(6;);
impl_from!(7;);
impl_from!(8;);
impl_from!(9;);
impl_from!(10;);
impl_from!(11;);
impl_from!(12;);
impl_from!(13;);
impl_from!(14;);
impl_from!(15;);
impl_from!(16;);
impl_from!(17;);
impl_from!(18;);
impl_from!(19;);
impl_from!(20;);
impl_from!(21;);
impl_from!(22;);
impl_from!(23;);
impl_from!(24;);
impl_from!(25;);
impl_from!(26;);
impl_from!(27;);
impl_from!(28;);
impl_from!(29;);
impl_from!(30;);
impl_from!(31;);
impl_from!(32; u8, u16, u32, u64, u128);
impl_try_from_slice!(32);
impl From<Expression> for Imm {
fn from(tree: Expression) -> Self {
Self { tree }
}
}
impl From<Terminal> for Imm {
fn from(terminal: Terminal) -> Self {
Expression::Terminal(terminal).into()
}
}
impl<const N: usize> Immediate<N> for Imm {}