Skip to content

Commit 900cbe1

Browse files
committed
lang: mv ConstExpression to a dedicated file
1 parent 917e0fb commit 900cbe1

File tree

4 files changed

+120
-111
lines changed

4 files changed

+120
-111
lines changed

crates/leanVm/src/intermediate_bytecode.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::{collections::BTreeMap, fmt};
22

33
use p3_field::{PrimeCharacteristicRing, PrimeField64};
44

5-
use crate::{Label, bytecode::operation::Operation, constant::F, lang::ConstExpression};
5+
use crate::{
6+
Label, bytecode::operation::Operation, constant::F, lang::const_expr::ConstExpression,
7+
};
68

79
#[derive(Debug, Clone)]
810
pub struct IntermediateBytecode {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::fmt;
2+
3+
use p3_field::PrimeCharacteristicRing;
4+
5+
use crate::{
6+
Label,
7+
lang::{ConstantValue, Expression, F, HighLevelOperation, SimpleExpr},
8+
};
9+
10+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11+
pub enum ConstExpression {
12+
Value(ConstantValue),
13+
Binary {
14+
left: Box<Self>,
15+
operation: HighLevelOperation,
16+
right: Box<Self>,
17+
},
18+
}
19+
20+
impl ConstExpression {
21+
#[must_use]
22+
pub const fn zero() -> Self {
23+
Self::scalar(0)
24+
}
25+
26+
#[must_use]
27+
pub const fn one() -> Self {
28+
Self::scalar(1)
29+
}
30+
31+
#[must_use]
32+
pub const fn label(label: Label) -> Self {
33+
Self::Value(ConstantValue::Label(label))
34+
}
35+
36+
#[must_use]
37+
pub const fn scalar(scalar: usize) -> Self {
38+
Self::Value(ConstantValue::Scalar(scalar))
39+
}
40+
41+
#[must_use]
42+
pub const fn function_size(function_name: Label) -> Self {
43+
Self::Value(ConstantValue::FunctionSize { function_name })
44+
}
45+
pub fn eval_with<EvalFn>(&self, func: &EvalFn) -> Option<F>
46+
where
47+
EvalFn: Fn(&ConstantValue) -> Option<F>,
48+
{
49+
match self {
50+
Self::Value(value) => func(value),
51+
Self::Binary {
52+
left,
53+
operation,
54+
right,
55+
} => Some(operation.eval(left.eval_with(func)?, right.eval_with(func)?)),
56+
}
57+
}
58+
59+
#[must_use]
60+
pub fn naive_eval(&self) -> Option<F> {
61+
self.eval_with(&|value| match value {
62+
ConstantValue::Scalar(scalar) => Some(F::from_usize(*scalar)),
63+
_ => None,
64+
})
65+
}
66+
}
67+
68+
impl From<usize> for ConstExpression {
69+
fn from(value: usize) -> Self {
70+
Self::Value(ConstantValue::Scalar(value))
71+
}
72+
}
73+
74+
impl TryFrom<Expression> for ConstExpression {
75+
type Error = ();
76+
77+
fn try_from(value: Expression) -> Result<Self, Self::Error> {
78+
match value {
79+
Expression::Value(SimpleExpr::Constant(const_expr)) => Ok(const_expr),
80+
Expression::Value(_) | Expression::ArrayAccess { .. } => Err(()),
81+
Expression::Binary {
82+
left,
83+
operation,
84+
right,
85+
} => {
86+
let left_expr = Self::try_from(*left)?;
87+
let right_expr = Self::try_from(*right)?;
88+
Ok(Self::Binary {
89+
left: Box::new(left_expr),
90+
operation,
91+
right: Box::new(right_expr),
92+
})
93+
}
94+
}
95+
}
96+
}
97+
98+
impl From<ConstantValue> for ConstExpression {
99+
fn from(value: ConstantValue) -> Self {
100+
Self::Value(value)
101+
}
102+
}
103+
104+
impl fmt::Display for ConstExpression {
105+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106+
match self {
107+
Self::Value(value) => write!(f, "{value}"),
108+
Self::Binary {
109+
left,
110+
operation,
111+
right,
112+
} => write!(f, "({left} {operation} {right})"),
113+
}
114+
}
115+
}

crates/leanVm/src/lang/mod.rs

Lines changed: 1 addition & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use std::fmt;
22

3-
use p3_field::PrimeCharacteristicRing;
43

54
use crate::{
6-
Label,
75
bytecode::precompiles::Precompile,
86
constant::F,
97
intermediate_bytecode::HighLevelOperation,
108
lang::{boolean::Boolean, constant_value::ConstantValue, simple_expr::SimpleExpr},
119
};
1210

1311
pub mod boolean;
12+
pub mod const_expr;
1413
pub mod constant_value;
1514
pub mod function;
1615
pub mod program;
@@ -19,100 +18,6 @@ pub mod simple_expr;
1918
pub type Var = String;
2019
pub type ConstMallocLabel = usize;
2120

22-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23-
pub enum ConstExpression {
24-
Value(ConstantValue),
25-
Binary {
26-
left: Box<Self>,
27-
operation: HighLevelOperation,
28-
right: Box<Self>,
29-
},
30-
}
31-
32-
impl From<usize> for ConstExpression {
33-
fn from(value: usize) -> Self {
34-
Self::Value(ConstantValue::Scalar(value))
35-
}
36-
}
37-
38-
impl TryFrom<Expression> for ConstExpression {
39-
type Error = ();
40-
41-
fn try_from(value: Expression) -> Result<Self, Self::Error> {
42-
match value {
43-
Expression::Value(SimpleExpr::Constant(const_expr)) => Ok(const_expr),
44-
Expression::Value(_) | Expression::ArrayAccess { .. } => Err(()),
45-
Expression::Binary {
46-
left,
47-
operation,
48-
right,
49-
} => {
50-
let left_expr = Self::try_from(*left)?;
51-
let right_expr = Self::try_from(*right)?;
52-
Ok(Self::Binary {
53-
left: Box::new(left_expr),
54-
operation,
55-
right: Box::new(right_expr),
56-
})
57-
}
58-
}
59-
}
60-
}
61-
62-
impl ConstExpression {
63-
#[must_use]
64-
pub const fn zero() -> Self {
65-
Self::scalar(0)
66-
}
67-
68-
#[must_use]
69-
pub const fn one() -> Self {
70-
Self::scalar(1)
71-
}
72-
73-
#[must_use]
74-
pub const fn label(label: Label) -> Self {
75-
Self::Value(ConstantValue::Label(label))
76-
}
77-
78-
#[must_use]
79-
pub const fn scalar(scalar: usize) -> Self {
80-
Self::Value(ConstantValue::Scalar(scalar))
81-
}
82-
83-
#[must_use]
84-
pub const fn function_size(function_name: Label) -> Self {
85-
Self::Value(ConstantValue::FunctionSize { function_name })
86-
}
87-
pub fn eval_with<EvalFn>(&self, func: &EvalFn) -> Option<F>
88-
where
89-
EvalFn: Fn(&ConstantValue) -> Option<F>,
90-
{
91-
match self {
92-
Self::Value(value) => func(value),
93-
Self::Binary {
94-
left,
95-
operation,
96-
right,
97-
} => Some(operation.eval(left.eval_with(func)?, right.eval_with(func)?)),
98-
}
99-
}
100-
101-
#[must_use]
102-
pub fn naive_eval(&self) -> Option<F> {
103-
self.eval_with(&|value| match value {
104-
ConstantValue::Scalar(scalar) => Some(F::from_usize(*scalar)),
105-
_ => None,
106-
})
107-
}
108-
}
109-
110-
impl From<ConstantValue> for ConstExpression {
111-
fn from(value: ConstantValue) -> Self {
112-
Self::Value(value)
113-
}
114-
}
115-
11621
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11722
pub enum Expression {
11823
Value(SimpleExpr),
@@ -384,19 +289,6 @@ impl Line {
384289
}
385290
}
386291

387-
impl fmt::Display for ConstExpression {
388-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
389-
match self {
390-
Self::Value(value) => write!(f, "{value}"),
391-
Self::Binary {
392-
left,
393-
operation,
394-
right,
395-
} => write!(f, "({left} {operation} {right})"),
396-
}
397-
}
398-
}
399-
400292
impl fmt::Display for Line {
401293
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
402294
write!(f, "{}", self.to_string_with_indent(0))

crates/leanVm/src/lang/simple_expr.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::{ConstExpression, ConstMallocLabel, ConstantValue, Var};
3+
use crate::lang::{ConstMallocLabel, ConstantValue, Var, const_expr::ConstExpression};
44

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

0 commit comments

Comments
 (0)