@@ -5,6 +5,7 @@ use std::collections::{BTreeMap, BTreeSet};
55use std:: fmt:: { Display , Formatter } ;
66use utils:: ToUsize ;
77
8+ use crate :: a_simplify_lang:: VarOrConstMallocAccess ;
89use crate :: { F , parser:: ConstArrayValue } ;
910pub use lean_vm:: { FileId , FunctionName , SourceLocation } ;
1011
@@ -39,12 +40,8 @@ pub type ConstMallocLabel = usize;
3940
4041#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
4142pub enum SimpleExpr {
42- Var ( Var ) ,
43+ Memory ( VarOrConstMallocAccess ) ,
4344 Constant ( ConstExpression ) ,
44- ConstMallocAccess {
45- malloc_label : ConstMallocLabel ,
46- offset : ConstExpression ,
47- } ,
4845}
4946
5047impl SimpleExpr {
@@ -63,13 +60,6 @@ impl SimpleExpr {
6360 pub const fn is_constant ( & self ) -> bool {
6461 matches ! ( self , Self :: Constant ( _) )
6562 }
66-
67- pub fn simplify_if_const ( & self ) -> Self {
68- if let Self :: Constant ( constant) = self {
69- return constant. clone ( ) . into ( ) ;
70- }
71- self . clone ( )
72- }
7363}
7464
7565impl From < ConstantValue > for SimpleExpr {
@@ -86,16 +76,15 @@ impl From<ConstExpression> for SimpleExpr {
8676
8777impl From < Var > for SimpleExpr {
8878 fn from ( var : Var ) -> Self {
89- Self :: Var ( var)
79+ VarOrConstMallocAccess :: Var ( var) . into ( )
9080 }
9181}
9282
9383impl SimpleExpr {
9484 pub fn as_constant ( & self ) -> Option < ConstExpression > {
9585 match self {
96- Self :: Var ( _) => None ,
9786 Self :: Constant ( constant) => Some ( constant. clone ( ) ) ,
98- Self :: ConstMallocAccess { .. } => None ,
87+ Self :: Memory ( _ ) => None ,
9988 }
10089 }
10190
@@ -226,7 +215,7 @@ impl Display for Condition {
226215pub enum Expression {
227216 Value ( SimpleExpr ) ,
228217 ArrayAccess {
229- array : SimpleExpr ,
218+ array : Var ,
230219 index : Vec < Self > , // multi-dimensional array access
231220 } ,
232221 MathExpr ( MathOperation , Vec < Self > ) ,
@@ -356,10 +345,7 @@ impl Expression {
356345 self . eval_with (
357346 & |value : & SimpleExpr | value. as_constant ( ) ?. naive_eval ( ) ,
358347 & |arr, indexes| {
359- let SimpleExpr :: Var ( name) = arr else {
360- return None ;
361- } ;
362- let array = const_arrays. get ( name) ?;
348+ let array = const_arrays. get ( arr) ?;
363349 assert_eq ! ( indexes. len( ) , array. depth( ) ) ;
364350 let idx = indexes. iter ( ) . map ( |e| e. to_usize ( ) ) . collect :: < Vec < _ > > ( ) ;
365351 array. navigate ( & idx) ?. as_scalar ( ) . map ( F :: from_usize)
@@ -370,7 +356,7 @@ impl Expression {
370356 pub fn eval_with < ValueFn , ArrayFn > ( & self , value_fn : & ValueFn , array_fn : & ArrayFn ) -> Option < F >
371357 where
372358 ValueFn : Fn ( & SimpleExpr ) -> Option < F > ,
373- ArrayFn : Fn ( & SimpleExpr , Vec < F > ) -> Option < F > ,
359+ ArrayFn : Fn ( & Var , Vec < F > ) -> Option < F > ,
374360 {
375361 match self {
376362 Self :: Value ( value) => value_fn ( value) ,
@@ -405,7 +391,7 @@ impl Expression {
405391#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
406392pub enum AssignmentTarget {
407393 Var ( Var ) ,
408- ArrayAccess { array : SimpleExpr , index : Box < Expression > } ,
394+ ArrayAccess { array : Var , index : Box < Expression > } ,
409395}
410396
411397impl Display for AssignmentTarget {
@@ -713,11 +699,8 @@ impl Display for ConstantValue {
713699impl Display for SimpleExpr {
714700 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
715701 match self {
716- Self :: Var ( var) => write ! ( f, "{var}" ) ,
717702 Self :: Constant ( constant) => write ! ( f, "{constant}" ) ,
718- Self :: ConstMallocAccess { malloc_label, offset } => {
719- write ! ( f, "malloc_access({malloc_label}, {offset})" )
720- }
703+ Self :: Memory ( var_or_const_malloc_access) => write ! ( f, "{var_or_const_malloc_access}" ) ,
721704 }
722705 }
723706}
0 commit comments