From c95b474a5e6a78c95e5d604a0456844bded97b3e Mon Sep 17 00:00:00 2001 From: ktokto313 Date: Sat, 1 Jun 2024 10:11:12 +0700 Subject: [PATCH] Fix parser: add integer larger than 9 power support --- plonk/src/parser.rs | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/plonk/src/parser.rs b/plonk/src/parser.rs index 6ac8393..8e14ea9 100644 --- a/plonk/src/parser.rs +++ b/plonk/src/parser.rs @@ -351,11 +351,12 @@ impl Parser { None => { let constant = value.parse::().unwrap(); let wire = if is_negative { - Wire::new("-".to_string() + constant.to_string().as_str(), - Fr::from(constant).neg()) + Wire::new( + "-".to_string() + constant.to_string().as_str(), + Fr::from(constant).neg(), + ) } else { - Wire::new(constant.to_string(), - Fr::from(constant)) + Wire::new(constant.to_string(), Fr::from(constant)) }; println!("{:?} {}", wire, is_negative); self.generate_constant_gate(gate_list, gate_set, position_map, wire.clone()); @@ -398,6 +399,7 @@ impl Parser { let string = string.to_lowercase(); let mut result = String::new(); let mut last_char = ' '; + let mut number_buffer = String::new(); let mut flag = false; for char in string.chars() { if char == ' ' { @@ -405,21 +407,32 @@ impl Parser { } if char == '^' { flag = true; - } else if flag { - if char.is_numeric() { - for _ in 0..char.to_string().parse::().unwrap() - 1 { - result.push('*'); - result.push(last_char); + } else if !char.is_numeric() { + if flag { + if !number_buffer.is_empty() { + for _ in 0..number_buffer.parse::().unwrap() - 1 { + result.push('*'); + result.push(last_char); + } + flag = false; + number_buffer = String::new(); + } else { + panic!("can't parse polynomial") } - flag = false; - } else { - panic!("can't parse polynomial") } - } else { last_char = char; result.push(char); + } else { + number_buffer.push(char); } } + if flag && !number_buffer.is_empty() { + for _ in 0..number_buffer.parse::().unwrap() - 1 { + result.push('*'); + result.push(last_char); + } + } + println!("{}", result); result } } @@ -609,4 +622,9 @@ mod tests { fn parse_string_panic_test() { let _result = Parser::parse_string("x * y + 3 * x ^ x + x * y * z=0"); } + + #[test] + fn parse_string_high_degree_test() { + let _result = Parser::parse_string("x^ 100 + x^10 = x^2"); + } }