|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + bytecode::precompiles::Precompile, |
| 5 | + lang::{Var, boolean::Boolean, expression::Expression}, |
| 6 | +}; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 9 | +pub enum Line { |
| 10 | + Assignment { |
| 11 | + var: Var, |
| 12 | + value: Expression, |
| 13 | + }, |
| 14 | + ArrayAssign { |
| 15 | + // array[index] = value |
| 16 | + array: Var, |
| 17 | + index: Expression, |
| 18 | + value: Expression, |
| 19 | + }, |
| 20 | + Assert(Boolean), |
| 21 | + IfCondition { |
| 22 | + condition: Boolean, |
| 23 | + then_branch: Vec<Self>, |
| 24 | + else_branch: Vec<Self>, |
| 25 | + }, |
| 26 | + ForLoop { |
| 27 | + iterator: Var, |
| 28 | + start: Expression, |
| 29 | + end: Expression, |
| 30 | + body: Vec<Self>, |
| 31 | + unroll: bool, |
| 32 | + }, |
| 33 | + FunctionCall { |
| 34 | + function_name: String, |
| 35 | + args: Vec<Expression>, |
| 36 | + return_data: Vec<Var>, |
| 37 | + }, |
| 38 | + FunctionRet { |
| 39 | + return_data: Vec<Expression>, |
| 40 | + }, |
| 41 | + Precompile { |
| 42 | + precompile: Precompile, |
| 43 | + args: Vec<Expression>, |
| 44 | + res: Vec<Var>, |
| 45 | + }, |
| 46 | + Break, |
| 47 | + Panic, |
| 48 | + // Hints: |
| 49 | + Print { |
| 50 | + line_info: String, |
| 51 | + content: Vec<Expression>, |
| 52 | + }, |
| 53 | + MAlloc { |
| 54 | + var: Var, |
| 55 | + size: Expression, |
| 56 | + vectorized: bool, |
| 57 | + }, |
| 58 | + DecomposeBits { |
| 59 | + var: Var, // a pointer to 31 field elements, containing the bits of "to_decompose" |
| 60 | + to_decompose: Expression, |
| 61 | + }, |
| 62 | +} |
| 63 | + |
| 64 | +impl Line { |
| 65 | + #[allow(clippy::too_many_lines)] |
| 66 | + pub(crate) fn to_string_with_indent(&self, indent: usize) -> String { |
| 67 | + let spaces = " ".repeat(indent); |
| 68 | + let line_str = match self { |
| 69 | + Self::Assignment { var, value } => { |
| 70 | + format!("{var} = {value}") |
| 71 | + } |
| 72 | + Self::ArrayAssign { |
| 73 | + array, |
| 74 | + index, |
| 75 | + value, |
| 76 | + } => { |
| 77 | + format!("{array}[{index}] = {value}") |
| 78 | + } |
| 79 | + Self::Assert(condition) => format!("assert {condition}"), |
| 80 | + Self::IfCondition { |
| 81 | + condition, |
| 82 | + then_branch, |
| 83 | + else_branch, |
| 84 | + } => { |
| 85 | + let then_str = then_branch |
| 86 | + .iter() |
| 87 | + .map(|line| line.to_string_with_indent(indent + 1)) |
| 88 | + .collect::<Vec<_>>() |
| 89 | + .join("\n"); |
| 90 | + |
| 91 | + let else_str = else_branch |
| 92 | + .iter() |
| 93 | + .map(|line| line.to_string_with_indent(indent + 1)) |
| 94 | + .collect::<Vec<_>>() |
| 95 | + .join("\n"); |
| 96 | + |
| 97 | + if else_branch.is_empty() { |
| 98 | + format!("if {condition} {{\n{then_str}\n{spaces}}}") |
| 99 | + } else { |
| 100 | + format!( |
| 101 | + "if {condition} {{\n{then_str}\n{spaces}}} else {{\n{else_str}\n{spaces}}}" |
| 102 | + ) |
| 103 | + } |
| 104 | + } |
| 105 | + Self::ForLoop { |
| 106 | + iterator, |
| 107 | + start, |
| 108 | + end, |
| 109 | + body, |
| 110 | + unroll, |
| 111 | + } => { |
| 112 | + let body_str = body |
| 113 | + .iter() |
| 114 | + .map(|line| line.to_string_with_indent(indent + 1)) |
| 115 | + .collect::<Vec<_>>() |
| 116 | + .join("\n"); |
| 117 | + format!( |
| 118 | + "for {} in {}..{} {}{{\n{}\n{}}}", |
| 119 | + iterator, |
| 120 | + start, |
| 121 | + end, |
| 122 | + if *unroll { "unroll " } else { "" }, |
| 123 | + body_str, |
| 124 | + spaces |
| 125 | + ) |
| 126 | + } |
| 127 | + Self::FunctionCall { |
| 128 | + function_name, |
| 129 | + args, |
| 130 | + return_data, |
| 131 | + } => { |
| 132 | + let args_str = args |
| 133 | + .iter() |
| 134 | + .map(std::string::ToString::to_string) |
| 135 | + .collect::<Vec<_>>() |
| 136 | + .join(", "); |
| 137 | + let return_data_str = return_data |
| 138 | + .iter() |
| 139 | + .map(std::string::ToString::to_string) |
| 140 | + .collect::<Vec<_>>() |
| 141 | + .join(", "); |
| 142 | + |
| 143 | + if return_data.is_empty() { |
| 144 | + format!("{function_name}({args_str})") |
| 145 | + } else { |
| 146 | + format!("{return_data_str} = {function_name}({args_str})") |
| 147 | + } |
| 148 | + } |
| 149 | + Self::FunctionRet { return_data } => { |
| 150 | + let return_data_str = return_data |
| 151 | + .iter() |
| 152 | + .map(std::string::ToString::to_string) |
| 153 | + .collect::<Vec<_>>() |
| 154 | + .join(", "); |
| 155 | + format!("return {return_data_str}") |
| 156 | + } |
| 157 | + Self::Precompile { |
| 158 | + precompile, |
| 159 | + args, |
| 160 | + res: return_data, |
| 161 | + } => { |
| 162 | + format!( |
| 163 | + "{} = {}({})", |
| 164 | + return_data |
| 165 | + .iter() |
| 166 | + .map(std::string::ToString::to_string) |
| 167 | + .collect::<Vec<_>>() |
| 168 | + .join(", "), |
| 169 | + precompile.name, |
| 170 | + args.iter() |
| 171 | + .map(std::string::ToString::to_string) |
| 172 | + .collect::<Vec<_>>() |
| 173 | + .join(", ") |
| 174 | + ) |
| 175 | + } |
| 176 | + Self::Print { |
| 177 | + line_info: _, |
| 178 | + content, |
| 179 | + } => { |
| 180 | + let content_str = content |
| 181 | + .iter() |
| 182 | + .map(std::string::ToString::to_string) |
| 183 | + .collect::<Vec<_>>() |
| 184 | + .join(", "); |
| 185 | + format!("print({content_str})") |
| 186 | + } |
| 187 | + Self::MAlloc { |
| 188 | + var, |
| 189 | + size, |
| 190 | + vectorized, |
| 191 | + } => { |
| 192 | + if *vectorized { |
| 193 | + format!("{var} = malloc_vectorized({size})") |
| 194 | + } else { |
| 195 | + format!("{var} = malloc({size})") |
| 196 | + } |
| 197 | + } |
| 198 | + Self::DecomposeBits { var, to_decompose } => { |
| 199 | + format!("{var} = decompose_bits({to_decompose})") |
| 200 | + } |
| 201 | + Self::Break => "break".to_string(), |
| 202 | + Self::Panic => "panic".to_string(), |
| 203 | + }; |
| 204 | + format!("{spaces}{line_str}") |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +impl fmt::Display for Line { |
| 209 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 210 | + write!(f, "{}", self.to_string_with_indent(0)) |
| 211 | + } |
| 212 | +} |
0 commit comments