circomspect_program_structure/abstract_syntax_tree/
statement_builders.rs1use super::ast::*;
2use Statement::*;
3
4pub fn build_conditional_block(
5 meta: Meta,
6 cond: Expression,
7 if_case: Statement,
8 else_case: Option<Statement>,
9) -> Statement {
10 IfThenElse { meta, cond, else_case: else_case.map(Box::new), if_case: Box::new(if_case) }
11}
12
13pub fn build_while_block(meta: Meta, cond: Expression, stmt: Statement) -> Statement {
14 While { meta, cond, stmt: Box::new(stmt) }
15}
16
17pub fn build_initialization_block(
18 meta: Meta,
19 xtype: VariableType,
20 initializations: Vec<Statement>,
21) -> Statement {
22 InitializationBlock { meta, xtype, initializations }
23}
24pub fn build_block(meta: Meta, stmts: Vec<Statement>) -> Statement {
25 Block { meta, stmts }
26}
27
28pub fn build_return(meta: Meta, value: Expression) -> Statement {
29 Return { meta, value }
30}
31
32pub fn build_declaration(
33 meta: Meta,
34 xtype: VariableType,
35 name: String,
36 dimensions: Vec<Expression>,
37) -> Statement {
38 let is_constant = true;
39 Declaration { meta, xtype, name, dimensions, is_constant }
40}
41
42pub fn build_substitution(
43 meta: Meta,
44 var: String,
45 access: Vec<Access>,
46 op: AssignOp,
47 rhe: Expression,
48) -> Statement {
49 Substitution { meta, var, access, op, rhe }
50}
51
52pub fn build_constraint_equality(meta: Meta, lhe: Expression, rhe: Expression) -> Statement {
53 ConstraintEquality { meta, lhe, rhe }
54}
55
56pub fn build_log_call(meta: Meta, args: Vec<LogArgument>) -> Statement {
57 let mut new_args = Vec::new();
58 for arg in args {
59 match arg {
60 LogArgument::LogExp(..) => {
61 new_args.push(arg);
62 }
63 LogArgument::LogStr(str) => {
64 new_args.append(&mut split_string(str));
65 }
66 }
67 }
68 LogCall { meta, args: new_args }
69}
70
71fn split_string(str: String) -> Vec<LogArgument> {
72 let mut v = vec![];
73 let sub_len = 230;
74 let mut cur = str;
75 while !cur.is_empty() {
76 let (chunk, rest) = cur.split_at(std::cmp::min(sub_len, cur.len()));
77 v.push(LogArgument::LogStr(chunk.to_string()));
78 cur = rest.to_string();
79 }
80 v
81}
82
83pub fn build_assert(meta: Meta, arg: Expression) -> Statement {
84 Assert { meta, arg }
85}
86
87pub fn build_multi_substitution(
88 meta: Meta,
89 lhe: Expression,
90 op: AssignOp,
91 rhe: Expression,
92) -> Statement {
93 MultiSubstitution { meta, lhe, op, rhe }
94}
95
96pub fn build_anonymous_component_statement(meta: Meta, arg: Expression) -> Statement {
97 MultiSubstitution {
98 meta: meta.clone(),
99 lhe: crate::expression_builders::build_tuple(meta, Vec::new()),
100 op: AssignOp::AssignConstraintSignal,
101 rhe: arg,
102 }
103}