From 0a3ffaf63698aab9c2f4d016f808cd83af6fe391 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:00:04 +0100 Subject: [PATCH] small refactor in the vm (#40) --- crates/brainfuck_vm/src/compiler.rs | 9 ++++----- crates/brainfuck_vm/src/registers.rs | 19 ++----------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/crates/brainfuck_vm/src/compiler.rs b/crates/brainfuck_vm/src/compiler.rs index 90381d4..a05c6b0 100644 --- a/crates/brainfuck_vm/src/compiler.rs +++ b/crates/brainfuck_vm/src/compiler.rs @@ -2,6 +2,7 @@ use stwo_prover::core::fields::m31::BaseField; +#[derive(Debug, Clone, Default)] pub struct Compiler { code: Vec, instructions: Vec, @@ -9,8 +10,7 @@ pub struct Compiler { impl Compiler { pub fn new(code: &str) -> Self { - let trimmed_code = code.chars().filter(|c| !c.is_whitespace()).collect(); - Self { code: trimmed_code, instructions: vec![] } + Self { code: code.chars().filter(|c| !c.is_whitespace()).collect(), ..Default::default() } } pub fn compile(&mut self) -> Vec { @@ -25,9 +25,8 @@ impl Compiler { } ']' => { let start_pos = loop_stack.pop().unwrap(); - let loop_end_pos = self.instructions.len() + 1; - self.instructions[start_pos] = BaseField::from((loop_end_pos - 1) as u32); - self.instructions.push(BaseField::from((start_pos + 1) as u32)); + self.instructions[start_pos] = BaseField::from(self.instructions.len()); + self.instructions.push(BaseField::from(start_pos + 1)); } _ => (), } diff --git a/crates/brainfuck_vm/src/registers.rs b/crates/brainfuck_vm/src/registers.rs index 346ade9..9b2676a 100644 --- a/crates/brainfuck_vm/src/registers.rs +++ b/crates/brainfuck_vm/src/registers.rs @@ -1,9 +1,8 @@ // Adapted from rkdud007 brainfuck-zkvm https://github.com/rkdud007/brainfuck-zkvm/blob/main/src/registers.rs -use num_traits::identities::Zero; use stwo_prover::core::fields::m31::BaseField; -#[derive(PartialEq, Eq, Clone)] +#[derive(PartialEq, Eq, Clone, Default)] pub struct Registers { /// Clock Cycle Counter pub clk: BaseField, @@ -21,23 +20,9 @@ pub struct Registers { pub mvi: BaseField, } -impl Default for Registers { - fn default() -> Self { - Self::new() - } -} - impl Registers { pub fn new() -> Self { - Self { - clk: BaseField::zero(), - ip: BaseField::zero(), - ci: BaseField::zero(), - ni: BaseField::zero(), - mp: BaseField::zero(), - mv: BaseField::zero(), - mvi: BaseField::zero(), - } + Self::default() } }