@@ -2,7 +2,8 @@ use crate::byte_set::ByteSet;
22use crate :: error:: { ParseError , ParseResult } ;
33use crate :: read:: ReadCtxt ;
44use crate :: {
5- DynFormat , Expr , Format , FormatModule , MatchTree , Next , Pattern , TypeScope , ValueType ,
5+ Arith , DynFormat , Expr , Format , FormatModule , IntRel , MatchTree , Next , Pattern , TypeScope ,
6+ ValueType ,
67} ;
78use crate :: { IntoLabel , Label } ;
89use serde:: Serialize ;
@@ -179,104 +180,134 @@ impl Expr {
179180 }
180181 Expr :: Lambda ( _, _) => panic ! ( "cannot eval lambda" ) ,
181182
182- Expr :: BitAnd ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
183- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( x & y) ,
184- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( x & y) ,
185- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( x & y) ,
186- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
187- } ) ,
188- Expr :: BitOr ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
189- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( x | y) ,
190- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( x | y) ,
191- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( x | y) ,
192- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
193- } ) ,
194- Expr :: Eq ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
195- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x == y) ,
196- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x == y) ,
197- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x == y) ,
198- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
199- } ) ,
200- Expr :: Ne ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
201- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x != y) ,
202- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x != y) ,
203- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x != y) ,
204- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
205- } ) ,
206- Expr :: Lt ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
207- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x < y) ,
208- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x < y) ,
209- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x < y) ,
210- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
211- } ) ,
212- Expr :: Gt ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
213- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x > y) ,
214- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x > y) ,
215- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x > y) ,
216- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
217- } ) ,
218- Expr :: Lte ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
219- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x <= y) ,
220- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x <= y) ,
221- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x <= y) ,
222- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
223- } ) ,
224- Expr :: Gte ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
225- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x >= y) ,
226- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x >= y) ,
227- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x >= y) ,
228- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
229- } ) ,
230- Expr :: Mul ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
231- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_mul ( x, y) . unwrap ( ) ) ,
232- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_mul ( x, y) . unwrap ( ) ) ,
233- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_mul ( x, y) . unwrap ( ) ) ,
234- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
235- } ) ,
236- Expr :: Div ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
237- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_div ( x, y) . unwrap ( ) ) ,
238- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_div ( x, y) . unwrap ( ) ) ,
239- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_div ( x, y) . unwrap ( ) ) ,
240- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
241- } ) ,
242- Expr :: Rem ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
243- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_rem ( x, y) . unwrap ( ) ) ,
244- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_rem ( x, y) . unwrap ( ) ) ,
245- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_rem ( x, y) . unwrap ( ) ) ,
246- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
247- } ) ,
248- Expr :: Shl ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
249- ( Value :: U8 ( x) , Value :: U8 ( y) ) => {
250- Value :: U8 ( u8:: checked_shl ( x, u32:: from ( y) ) . unwrap ( ) )
251- }
252- ( Value :: U16 ( x) , Value :: U16 ( y) ) => {
253- Value :: U16 ( u16:: checked_shl ( x, u32:: from ( y) ) . unwrap ( ) )
254- }
255- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_shl ( x, y) . unwrap ( ) ) ,
256- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
257- } ) ,
258- Expr :: Shr ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
259- ( Value :: U8 ( x) , Value :: U8 ( y) ) => {
260- Value :: U8 ( u8:: checked_shr ( x, u32:: from ( y) ) . unwrap ( ) )
261- }
262- ( Value :: U16 ( x) , Value :: U16 ( y) ) => {
263- Value :: U16 ( u16:: checked_shr ( x, u32:: from ( y) ) . unwrap ( ) )
264- }
265- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_shr ( x, y) . unwrap ( ) ) ,
266- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
267- } ) ,
268- Expr :: Add ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
269- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_add ( x, y) . unwrap ( ) ) ,
270- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_add ( x, y) . unwrap ( ) ) ,
271- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_add ( x, y) . unwrap ( ) ) ,
272- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
273- } ) ,
274- Expr :: Sub ( x, y) => Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
275- ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_sub ( x, y) . unwrap ( ) ) ,
276- ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_sub ( x, y) . unwrap ( ) ) ,
277- ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_sub ( x, y) . unwrap ( ) ) ,
278- ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
279- } ) ,
183+ Expr :: IntRel ( IntRel :: Eq , x, y) => {
184+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
185+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x == y) ,
186+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x == y) ,
187+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x == y) ,
188+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
189+ } )
190+ }
191+ Expr :: IntRel ( IntRel :: Ne , x, y) => {
192+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
193+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x != y) ,
194+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x != y) ,
195+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x != y) ,
196+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
197+ } )
198+ }
199+ Expr :: IntRel ( IntRel :: Lt , x, y) => {
200+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
201+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x < y) ,
202+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x < y) ,
203+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x < y) ,
204+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
205+ } )
206+ }
207+ Expr :: IntRel ( IntRel :: Gt , x, y) => {
208+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
209+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x > y) ,
210+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x > y) ,
211+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x > y) ,
212+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
213+ } )
214+ }
215+ Expr :: IntRel ( IntRel :: Lte , x, y) => {
216+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
217+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x <= y) ,
218+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x <= y) ,
219+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x <= y) ,
220+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
221+ } )
222+ }
223+ Expr :: IntRel ( IntRel :: Gte , x, y) => {
224+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
225+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: Bool ( x >= y) ,
226+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: Bool ( x >= y) ,
227+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: Bool ( x >= y) ,
228+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
229+ } )
230+ }
231+ Expr :: Arith ( Arith :: Add , x, y) => {
232+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
233+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_add ( x, y) . unwrap ( ) ) ,
234+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_add ( x, y) . unwrap ( ) ) ,
235+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_add ( x, y) . unwrap ( ) ) ,
236+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
237+ } )
238+ }
239+ Expr :: Arith ( Arith :: Sub , x, y) => {
240+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
241+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_sub ( x, y) . unwrap ( ) ) ,
242+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_sub ( x, y) . unwrap ( ) ) ,
243+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_sub ( x, y) . unwrap ( ) ) ,
244+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
245+ } )
246+ }
247+ Expr :: Arith ( Arith :: Mul , x, y) => {
248+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
249+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_mul ( x, y) . unwrap ( ) ) ,
250+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_mul ( x, y) . unwrap ( ) ) ,
251+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_mul ( x, y) . unwrap ( ) ) ,
252+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
253+ } )
254+ }
255+ Expr :: Arith ( Arith :: Div , x, y) => {
256+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
257+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_div ( x, y) . unwrap ( ) ) ,
258+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_div ( x, y) . unwrap ( ) ) ,
259+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_div ( x, y) . unwrap ( ) ) ,
260+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
261+ } )
262+ }
263+ Expr :: Arith ( Arith :: Rem , x, y) => {
264+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
265+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( u8:: checked_rem ( x, y) . unwrap ( ) ) ,
266+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( u16:: checked_rem ( x, y) . unwrap ( ) ) ,
267+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_rem ( x, y) . unwrap ( ) ) ,
268+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
269+ } )
270+ }
271+ Expr :: Arith ( Arith :: BitAnd , x, y) => {
272+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
273+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( x & y) ,
274+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( x & y) ,
275+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( x & y) ,
276+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
277+ } )
278+ }
279+ Expr :: Arith ( Arith :: BitOr , x, y) => {
280+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
281+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => Value :: U8 ( x | y) ,
282+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => Value :: U16 ( x | y) ,
283+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( x | y) ,
284+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
285+ } )
286+ }
287+ Expr :: Arith ( Arith :: Shl , x, y) => {
288+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
289+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => {
290+ Value :: U8 ( u8:: checked_shl ( x, u32:: from ( y) ) . unwrap ( ) )
291+ }
292+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => {
293+ Value :: U16 ( u16:: checked_shl ( x, u32:: from ( y) ) . unwrap ( ) )
294+ }
295+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_shl ( x, y) . unwrap ( ) ) ,
296+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
297+ } )
298+ }
299+ Expr :: Arith ( Arith :: Shr , x, y) => {
300+ Cow :: Owned ( match ( x. eval_value ( scope) , y. eval_value ( scope) ) {
301+ ( Value :: U8 ( x) , Value :: U8 ( y) ) => {
302+ Value :: U8 ( u8:: checked_shr ( x, u32:: from ( y) ) . unwrap ( ) )
303+ }
304+ ( Value :: U16 ( x) , Value :: U16 ( y) ) => {
305+ Value :: U16 ( u16:: checked_shr ( x, u32:: from ( y) ) . unwrap ( ) )
306+ }
307+ ( Value :: U32 ( x) , Value :: U32 ( y) ) => Value :: U32 ( u32:: checked_shr ( x, y) . unwrap ( ) ) ,
308+ ( x, y) => panic ! ( "mismatched operands {x:?}, {y:?}" ) ,
309+ } )
310+ }
280311
281312 Expr :: AsU8 ( x) => Cow :: Owned ( match x. eval_value ( scope) {
282313 Value :: U8 ( x) => Value :: U8 ( x) ,
0 commit comments