Skip to content

Commit 2d15fee

Browse files
authored
compiler: lang reorg (#15)
* compiler: lang reorg * clippy * fmt
1 parent 0277b91 commit 2d15fee

File tree

15 files changed

+726
-677
lines changed

15 files changed

+726
-677
lines changed

crates/compiler/src/a_simplify_lang.rs

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,10 +1132,7 @@ fn handle_const_arguments_helper(
11321132
for (arg_expr, (arg_var, is_constant)) in args.iter().zip(&func.arguments) {
11331133
if *is_constant {
11341134
let const_eval = arg_expr.naive_eval().unwrap_or_else(|| {
1135-
panic!(
1136-
"Failed to evaluate constant argument: {}",
1137-
arg_expr.to_string()
1138-
)
1135+
panic!("Failed to evaluate constant argument: {arg_expr}")
11391136
});
11401137
const_evals.push((arg_var.clone(), const_eval));
11411138
}
@@ -1332,11 +1329,7 @@ impl ToString for VarOrConstMallocAccess {
13321329
malloc_label,
13331330
offset,
13341331
} => {
1335-
format!(
1336-
"ConstMallocAccess({}, {})",
1337-
malloc_label,
1338-
offset.to_string()
1339-
)
1332+
format!("ConstMallocAccess({malloc_label}, {offset})")
13401333
}
13411334
}
13421335
}
@@ -1352,28 +1345,17 @@ impl SimpleLine {
13521345
arg0,
13531346
arg1,
13541347
} => {
1355-
format!(
1356-
"{} = {} {} {}",
1357-
var.to_string(),
1358-
arg0.to_string(),
1359-
operation.to_string(),
1360-
arg1.to_string()
1361-
)
1348+
format!("{} = {} {} {}", var.to_string(), arg0, operation, arg1)
13621349
}
13631350
Self::DecomposeBits {
13641351
var: result,
13651352
to_decompose,
13661353
label: _,
13671354
} => {
1368-
format!("{} = decompose_bits({})", result, to_decompose.to_string())
1355+
format!("{result} = decompose_bits({to_decompose})")
13691356
}
13701357
Self::RawAccess { res, index, shift } => {
1371-
format!(
1372-
"{} = memory[{} + {}]",
1373-
res.to_string(),
1374-
index,
1375-
shift.to_string()
1376-
)
1358+
format!("{res} = memory[{index} + {shift}]")
13771359
}
13781360
Self::IfNotZero {
13791361
condition,
@@ -1393,20 +1375,10 @@ impl SimpleLine {
13931375
.join("\n");
13941376

13951377
if else_branch.is_empty() {
1396-
format!(
1397-
"if {} != 0 {{\n{}\n{}}}",
1398-
condition.to_string(),
1399-
then_str,
1400-
spaces
1401-
)
1378+
format!("if {condition} != 0 {{\n{then_str}\n{spaces}}}")
14021379
} else {
14031380
format!(
1404-
"if {} != 0 {{\n{}\n{}}} else {{\n{}\n{}}}",
1405-
condition.to_string(),
1406-
then_str,
1407-
spaces,
1408-
else_str,
1409-
spaces
1381+
"if {condition} != 0 {{\n{then_str}\n{spaces}}} else {{\n{else_str}\n{spaces}}}"
14101382
)
14111383
}
14121384
}
@@ -1476,14 +1448,14 @@ impl SimpleLine {
14761448
} else {
14771449
"malloc"
14781450
};
1479-
format!("{} = {}({})", var, alloc_type, size.to_string())
1451+
format!("{var} = {alloc_type}({size})")
14801452
}
14811453
Self::ConstMalloc {
14821454
var,
14831455
size,
14841456
label: _,
14851457
} => {
1486-
format!("{} = malloc({})", var, size.to_string())
1458+
format!("{var} = malloc({size})")
14871459
}
14881460
Self::Panic => "panic".to_string(),
14891461
};

crates/compiler/src/intermediate_bytecode.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::BTreeMap;
1+
use std::{collections::BTreeMap, fmt};
22

33
use p3_field::{PrimeCharacteristicRing, PrimeField64};
44
use vm::{Label, Operation};
@@ -73,6 +73,18 @@ impl HighLevelOperation {
7373
}
7474
}
7575

76+
impl fmt::Display for HighLevelOperation {
77+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78+
match self {
79+
Self::Add => write!(f, "+"),
80+
Self::Mul => write!(f, "*"),
81+
Self::Sub => write!(f, "-"),
82+
Self::Div => write!(f, "/"),
83+
Self::Exp => write!(f, "**"),
84+
}
85+
}
86+
}
87+
7688
#[derive(Debug, Clone)]
7789
pub(crate) enum IntermediateInstruction {
7890
Computation {
@@ -191,7 +203,7 @@ impl ToString for IntermediateValue {
191203
Self::Constant(value) => value.to_string(),
192204
Self::Fp => "fp".to_string(),
193205
Self::MemoryAfterFp { offset } => {
194-
format!("m[fp + {}]", offset.to_string())
206+
format!("m[fp + {offset}]")
195207
}
196208
}
197209
}
@@ -200,7 +212,7 @@ impl ToString for IntermediateValue {
200212
impl ToString for IntermediaryMemOrFpOrConstant {
201213
fn to_string(&self) -> String {
202214
match self {
203-
Self::MemoryAfterFp { offset } => format!("m[fp + {}]", offset.to_string()),
215+
Self::MemoryAfterFp { offset } => format!("m[fp + {offset}]"),
204216
Self::Fp => "fp".to_string(),
205217
Self::Constant(c) => c.to_string(),
206218
}
@@ -214,12 +226,7 @@ impl ToString for IntermediateInstruction {
214226
shift_0,
215227
shift_1,
216228
res,
217-
} => format!(
218-
"{} = m[m[fp + {}] + {}]",
219-
res.to_string(),
220-
shift_0.to_string(),
221-
shift_1.to_string()
222-
),
229+
} => format!("{} = m[m[fp + {}] + {}]", res.to_string(), shift_0, shift_1),
223230
Self::DotProduct {
224231
arg0,
225232
arg1,
@@ -230,7 +237,7 @@ impl ToString for IntermediateInstruction {
230237
arg0.to_string(),
231238
arg1.to_string(),
232239
res.to_string(),
233-
size.to_string()
240+
size
234241
),
235242
Self::MultilinearEval {
236243
coeffs,
@@ -242,7 +249,7 @@ impl ToString for IntermediateInstruction {
242249
coeffs.to_string(),
243250
point.to_string(),
244251
res.to_string(),
245-
n_vars.to_string()
252+
n_vars
246253
),
247254
Self::DecomposeBits {
248255
res_offset,
@@ -318,7 +325,7 @@ impl ToString for IntermediateInstruction {
318325
vectorized,
319326
} => format!(
320327
"m[fp + {}] = {}({})",
321-
offset.to_string(),
328+
offset,
322329
if *vectorized { "malloc_vec" } else { "malloc" },
323330
size.to_string(),
324331
),
@@ -335,18 +342,6 @@ impl ToString for IntermediateInstruction {
335342
}
336343
}
337344

338-
impl ToString for HighLevelOperation {
339-
fn to_string(&self) -> String {
340-
match self {
341-
Self::Add => "+".to_string(),
342-
Self::Mul => "*".to_string(),
343-
Self::Sub => "-".to_string(),
344-
Self::Div => "/".to_string(),
345-
Self::Exp => "**".to_string(),
346-
}
347-
}
348-
}
349-
350345
impl ToString for IntermediateBytecode {
351346
fn to_string(&self) -> String {
352347
let mut res = String::new();

0 commit comments

Comments
 (0)