Skip to content

Commit

Permalink
Fix parser: add integer larger than 9 power support
Browse files Browse the repository at this point in the history
  • Loading branch information
ktokto313 committed Jun 1, 2024
1 parent 9a90bef commit c95b474
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions plonk/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,12 @@ impl Parser {
None => {
let constant = value.parse::<i32>().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());
Expand Down Expand Up @@ -398,28 +399,40 @@ 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 == ' ' {
continue;
}
if char == '^' {
flag = true;
} else if flag {
if char.is_numeric() {
for _ in 0..char.to_string().parse::<i32>().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::<i32>().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::<i32>().unwrap() - 1 {
result.push('*');
result.push(last_char);
}
}
println!("{}", result);
result
}
}
Expand Down Expand Up @@ -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");
}
}

0 comments on commit c95b474

Please sign in to comment.