circom_lsp_program_structure/abstract_syntax_tree/
statement_builders.rs

1use 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(|s| Box::new(s)), 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(..) => { new_args.push(arg);}
61            LogArgument::LogStr(str) => { new_args.append(&mut split_string(str));}
62        }
63    }
64    LogCall { meta, args: new_args }
65}
66
67fn split_string(str: String) -> Vec<LogArgument> {
68    let mut v = vec![];
69    let sub_len = 230;
70    let mut cur = str;
71    while !cur.is_empty() {
72        let (chunk, rest) = cur.split_at(std::cmp::min(sub_len, cur.len()));
73        v.push(LogArgument::LogStr(chunk.to_string()));
74        cur = rest.to_string();
75    }
76    v
77}
78
79pub fn build_assert(meta: Meta, arg: Expression) -> Statement {
80    Assert { meta, arg }
81}
82
83pub fn build_mult_substitution(meta: Meta, lhe: Expression, op : AssignOp, rhe: Expression) -> Statement {
84    MultSubstitution { meta: meta.clone(), lhe, op, rhe }
85}
86
87pub fn build_anonymous_component_statement(meta: Meta, arg: Expression) -> Statement {
88    MultSubstitution { meta: meta.clone(), lhe: crate::expression_builders::build_tuple(meta, Vec::new()), op: AssignOp::AssignConstraintSignal, rhe: arg }
89}