Skip to content

Commit 7270b6a

Browse files
committed
zkDSL: fix bug
1 parent bb3806e commit 7270b6a

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,28 @@ fn simplify_lines(
651651
Expression::Binary { left, operation, right } => {
652652
let left = simplify_expr(left, &mut res, counters, array_manager, const_malloc, const_arrays);
653653
let right = simplify_expr(right, &mut res, counters, array_manager, const_malloc, const_arrays);
654-
res.push(SimpleLine::Assignment {
655-
var: var.clone().into(),
656-
operation: *operation,
657-
arg0: left,
658-
arg1: right,
659-
});
654+
// If both operands are constants, evaluate at compile time and assign the result
655+
if let (SimpleExpr::Constant(left_cst), SimpleExpr::Constant(right_cst)) = (&left, &right) {
656+
let result = ConstExpression::Binary {
657+
left: Box::new(left_cst.clone()),
658+
operation: *operation,
659+
right: Box::new(right_cst.clone()),
660+
}
661+
.try_naive_simplification();
662+
res.push(SimpleLine::Assignment {
663+
var: var.clone().into(),
664+
operation: HighLevelOperation::Add,
665+
arg0: SimpleExpr::Constant(result),
666+
arg1: SimpleExpr::zero(),
667+
});
668+
} else {
669+
res.push(SimpleLine::Assignment {
670+
var: var.clone().into(),
671+
operation: *operation,
672+
arg0: left,
673+
arg1: right,
674+
});
675+
}
660676
}
661677
Expression::MathExpr(_, _) => unreachable!(),
662678
},

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,15 +1305,20 @@ fn test_2d_nested_array_with_expressions() {
13051305
);
13061306
}
13071307

1308-
// #[test]
1309-
// fn bug() {
1310-
// let program = r#"
1311-
// const ARR = [1];
1312-
// fn main() {
1313-
// x = ARR[0]**2;
1314-
// print(x);
1315-
// return;
1316-
// }
1317-
// "#;
1318-
// compile_and_run(&ProgramSource::Raw(program.to_string()), (&[], &[]), false);
1319-
// }
1308+
#[test]
1309+
fn test_const_array_element_exponentiation() {
1310+
let program = r#"
1311+
const ARR = [[5]];
1312+
fn main() {
1313+
x = ARR[0][0]**2;
1314+
assert x == 25;
1315+
return;
1316+
}
1317+
"#;
1318+
compile_and_run(
1319+
&ProgramSource::Raw(program.to_string()),
1320+
(&[], &[]),
1321+
DEFAULT_NO_VEC_RUNTIME_MEMORY,
1322+
false,
1323+
);
1324+
}

0 commit comments

Comments
 (0)