Skip to content

Commit a620ce9

Browse files
committed
lang: mv Expression to a dedicated file
1 parent 900cbe1 commit a620ce9

File tree

4 files changed

+81
-75
lines changed

4 files changed

+81
-75
lines changed

crates/leanVm/src/lang/boolean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use crate::lang::Expression;
3+
use crate::lang::expression::Expression;
44

55
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
66
pub enum Boolean {

crates/leanVm/src/lang/const_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use p3_field::PrimeCharacteristicRing;
44

55
use crate::{
66
Label,
7-
lang::{ConstantValue, Expression, F, HighLevelOperation, SimpleExpr},
7+
lang::{ConstantValue, F, HighLevelOperation, SimpleExpr, expression::Expression},
88
};
99

1010
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::fmt;
2+
3+
use crate::lang::{F, HighLevelOperation, SimpleExpr, Var};
4+
5+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6+
pub enum Expression {
7+
Value(SimpleExpr),
8+
ArrayAccess {
9+
array: Var,
10+
index: Box<Expression>,
11+
},
12+
Binary {
13+
left: Box<Self>,
14+
operation: HighLevelOperation,
15+
right: Box<Self>,
16+
},
17+
}
18+
19+
impl Expression {
20+
#[must_use]
21+
pub fn naive_eval(&self) -> Option<F> {
22+
self.eval_with(
23+
&|value: &SimpleExpr| value.as_constant()?.naive_eval(),
24+
&|_, _| None,
25+
)
26+
}
27+
28+
pub fn eval_with<ValueFn, ArrayFn>(&self, value_fn: &ValueFn, array_fn: &ArrayFn) -> Option<F>
29+
where
30+
ValueFn: Fn(&SimpleExpr) -> Option<F>,
31+
ArrayFn: Fn(&Var, F) -> Option<F>,
32+
{
33+
match self {
34+
Self::Value(value) => value_fn(value),
35+
Self::ArrayAccess { array, index } => {
36+
array_fn(array, index.eval_with(value_fn, array_fn)?)
37+
}
38+
Self::Binary {
39+
left,
40+
operation,
41+
right,
42+
} => Some(operation.eval(
43+
left.eval_with(value_fn, array_fn)?,
44+
right.eval_with(value_fn, array_fn)?,
45+
)),
46+
}
47+
}
48+
}
49+
50+
impl From<SimpleExpr> for Expression {
51+
fn from(value: SimpleExpr) -> Self {
52+
Self::Value(value)
53+
}
54+
}
55+
56+
impl From<Var> for Expression {
57+
fn from(var: Var) -> Self {
58+
Self::Value(var.into())
59+
}
60+
}
61+
62+
impl fmt::Display for Expression {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
match self {
65+
Self::Value(val) => write!(f, "{val}"),
66+
Self::ArrayAccess { array, index } => write!(f, "{array}[{index}]"),
67+
Self::Binary {
68+
left,
69+
operation,
70+
right,
71+
} => write!(f, "({left} {operation} {right})"),
72+
}
73+
}
74+
}

crates/leanVm/src/lang/mod.rs

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,26 @@
11
use std::fmt;
22

3-
43
use crate::{
54
bytecode::precompiles::Precompile,
65
constant::F,
76
intermediate_bytecode::HighLevelOperation,
8-
lang::{boolean::Boolean, constant_value::ConstantValue, simple_expr::SimpleExpr},
7+
lang::{
8+
boolean::Boolean, constant_value::ConstantValue, expression::Expression,
9+
simple_expr::SimpleExpr,
10+
},
911
};
1012

1113
pub mod boolean;
1214
pub mod const_expr;
1315
pub mod constant_value;
16+
pub mod expression;
1417
pub mod function;
1518
pub mod program;
1619
pub mod simple_expr;
1720

1821
pub type Var = String;
1922
pub type ConstMallocLabel = usize;
2023

21-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22-
pub enum Expression {
23-
Value(SimpleExpr),
24-
ArrayAccess {
25-
array: Var,
26-
index: Box<Expression>,
27-
},
28-
Binary {
29-
left: Box<Self>,
30-
operation: HighLevelOperation,
31-
right: Box<Self>,
32-
},
33-
}
34-
35-
impl From<SimpleExpr> for Expression {
36-
fn from(value: SimpleExpr) -> Self {
37-
Self::Value(value)
38-
}
39-
}
40-
41-
impl From<Var> for Expression {
42-
fn from(var: Var) -> Self {
43-
Self::Value(var.into())
44-
}
45-
}
46-
47-
impl Expression {
48-
#[must_use]
49-
pub fn naive_eval(&self) -> Option<F> {
50-
self.eval_with(
51-
&|value: &SimpleExpr| value.as_constant()?.naive_eval(),
52-
&|_, _| None,
53-
)
54-
}
55-
56-
pub fn eval_with<ValueFn, ArrayFn>(&self, value_fn: &ValueFn, array_fn: &ArrayFn) -> Option<F>
57-
where
58-
ValueFn: Fn(&SimpleExpr) -> Option<F>,
59-
ArrayFn: Fn(&Var, F) -> Option<F>,
60-
{
61-
match self {
62-
Self::Value(value) => value_fn(value),
63-
Self::ArrayAccess { array, index } => {
64-
array_fn(array, index.eval_with(value_fn, array_fn)?)
65-
}
66-
Self::Binary {
67-
left,
68-
operation,
69-
right,
70-
} => Some(operation.eval(
71-
left.eval_with(value_fn, array_fn)?,
72-
right.eval_with(value_fn, array_fn)?,
73-
)),
74-
}
75-
}
76-
}
77-
7824
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7925
pub enum Line {
8026
Assignment {
@@ -131,20 +77,6 @@ pub enum Line {
13177
},
13278
}
13379

134-
impl fmt::Display for Expression {
135-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136-
match self {
137-
Self::Value(val) => write!(f, "{val}"),
138-
Self::ArrayAccess { array, index } => write!(f, "{array}[{index}]"),
139-
Self::Binary {
140-
left,
141-
operation,
142-
right,
143-
} => write!(f, "({left} {operation} {right})"),
144-
}
145-
}
146-
}
147-
14880
impl Line {
14981
#[allow(clippy::too_many_lines)]
15082
fn to_string_with_indent(&self, indent: usize) -> String {

0 commit comments

Comments
 (0)