From 61253031b49d5490f4c3f238198afa44e0870fdb Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Thu, 22 Jul 2021 22:17:33 -0700 Subject: [PATCH] countdown loops --- compiler/src/statement/iteration/iteration.rs | 8 ++++++- tests/compiler/statements/reverse_loop.leo | 19 +++++++++++++++++ .../compiler/statements/reverse_loop.leo.out | 21 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/compiler/statements/reverse_loop.leo create mode 100644 tests/expectations/compiler/compiler/statements/reverse_loop.leo.out diff --git a/compiler/src/statement/iteration/iteration.rs b/compiler/src/statement/iteration/iteration.rs index fb3750c451..d9bb4184d0 100644 --- a/compiler/src/statement/iteration/iteration.rs +++ b/compiler/src/statement/iteration/iteration.rs @@ -53,7 +53,13 @@ impl<'a, F: PrimeField, G: GroupType> ConstrainedProgram<'a, F, G> { .to_usize() .ok_or_else(|| StatementError::loop_index_const(&span))?; - for i in from..to { + let iter: Box> = if from < to { + Box::new(from..to) + } else { + Box::new((to..from).rev()) + }; + + for i in iter { // Store index in current function scope. // For loop scope is not implemented. let variable = statement.variable.borrow(); diff --git a/tests/compiler/statements/reverse_loop.leo b/tests/compiler/statements/reverse_loop.leo new file mode 100644 index 0000000000..52b24c0bf4 --- /dev/null +++ b/tests/compiler/statements/reverse_loop.leo @@ -0,0 +1,19 @@ +/* +namespace: Compile +expectation: Pass +input_file: inputs/dummy.in +*/ + +function main(k: bool) -> bool { + let reverse: u32 = 0; + for i in 10..0 { + reverse += i; + } + + let forward: u32 = 0; + for x in 0..10 { + forward += x; + } + + return reverse == forward && k; +} \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/statements/reverse_loop.leo.out b/tests/expectations/compiler/compiler/statements/reverse_loop.leo.out new file mode 100644 index 0000000000..42b439b56d --- /dev/null +++ b/tests/expectations/compiler/compiler/statements/reverse_loop.leo.out @@ -0,0 +1,21 @@ +--- +namespace: Compile +expectation: Pass +outputs: + - circuit: + num_public_variables: 0 + num_private_variables: 1 + num_constraints: 1 + at: 042610d0fd1fe6d6ac112138f8755752f44c7d2a00f1b5960574d6da5cda393f + bt: e97756698880ab7555a959a5fb5c6b4e15bd64612aa677adbfe2d0bd91f0a83c + ct: cf1cbb66a638b4860a516671fb74850e6ccf787fe6c4c8d29e9c04efe880bd05 + output: + - input_file: inputs/dummy.in + output: + registers: + r0: + type: bool + value: "true" + initial_ast: 2de2209f674d98d4c079ab820b1e154ab47cf45a0413a8b0643b322afb836518 + canonicalized_ast: 780fd70bae5c39651481e3966332f3759fd4b80a03cabc812682aa0e5aebb0b7 + type_inferenced_ast: 4256bce9f7fa10cd6265340993d77abc18eff6f71cb6eb13a0e064c310b75658