1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use super::ast::*;
use super::expression_builders::*;
use super::statement_builders::*;
use crate::ast::{Access, Expression, VariableType};
use num_bigint::BigInt;

#[derive(Clone)]
pub struct Symbol {
    pub name: String,
    pub is_array: Vec<Expression>,
    pub init: Option<Expression>,
}

pub struct TupleInit {
    pub tuple_init: (AssignOp, Expression),
}

pub fn assign_with_op_shortcut(
    op: ExpressionInfixOpcode,
    meta: Meta,
    variable: (String, Vec<Access>),
    rhe: Expression,
) -> Statement {
    let (var, access) = variable;
    let variable = build_variable(meta.clone(), var.clone(), access.clone());
    let infix = build_infix(meta.clone(), variable, op, rhe);
    build_substitution(meta, var, access, AssignOp::AssignVar, infix)
}

pub fn plusplus(meta: Meta, variable: (String, Vec<Access>)) -> Statement {
    let one = build_number(meta.clone(), BigInt::from(1));
    assign_with_op_shortcut(ExpressionInfixOpcode::Add, meta, variable, one)
}

pub fn subsub(meta: Meta, variable: (String, Vec<Access>)) -> Statement {
    let one = build_number(meta.clone(), BigInt::from(1));
    assign_with_op_shortcut(ExpressionInfixOpcode::Sub, meta, variable, one)
}

pub fn for_into_while(
    meta: Meta,
    init: Statement,
    cond: Expression,
    step: Statement,
    body: Statement,
) -> Statement {
    let while_body = build_block(body.get_meta().clone(), vec![body, step]);
    let while_statement = build_while_block(meta.clone(), cond, while_body);
    build_block(meta, vec![init, while_statement])
}

pub fn split_declaration_into_single_nodes(
    meta: Meta,
    xtype: VariableType,
    symbols: Vec<Symbol>,
    op: AssignOp,
) -> Statement {
    // use crate::ast_shortcuts::VariableType::Var;
    let mut initializations = Vec::new();

    for symbol in symbols {
        let with_meta = meta.clone();
        let has_type = xtype.clone();
        let name = symbol.name.clone();
        let dimensions = symbol.is_array;
        let possible_init = symbol.init;
        let single_declaration = build_declaration(with_meta, has_type, name, dimensions.clone());
        initializations.push(single_declaration);

        if let Option::Some(init) = possible_init {
            let substitution = build_substitution(meta.clone(), symbol.name, vec![], op, init);
            initializations.push(substitution);
        }
        // If the variable is not initialized it is default initialized to 0 by
        // Circom.  We remove this because we don't want this assignment to be
        // flagged as an unused assignment by the side-effect analysis.
        // else if xtype == Var {
        //     let mut value = Expression::Number(meta.clone(), BigInt::from(0));
        //     for dim_expr in dimensions.iter().rev() {
        //         value = build_uniform_array(meta.clone(), value, dim_expr.clone());
        //     }

        //     let substitution = build_substitution(meta.clone(), symbol.name, vec![], op, value);
        //     initializations.push(substitution);
        // }
    }
    build_initialization_block(meta, xtype, initializations)
}

pub fn split_declaration_into_single_nodes_and_multi_substitution(
    meta: Meta,
    xtype: VariableType,
    symbols: Vec<Symbol>,
    init: Option<TupleInit>,
) -> Statement {
    let mut initializations = Vec::new();
    let mut values = Vec::new();
    for symbol in symbols {
        let with_meta = meta.clone();
        let has_type = xtype.clone();
        let name = symbol.name.clone();
        let dimensions = symbol.is_array;
        debug_assert!(symbol.init.is_none());
        let single_declaration =
            build_declaration(with_meta.clone(), has_type, name.clone(), dimensions.clone());
        initializations.push(single_declaration);
        // Circom default initializes local arrays to 0. We remove this because
        // we don't want these assignments to be flagged as unused assignments
        // by the side-effect analysis.
        // if xtype == Var && init.is_none() {
        //     let mut value = Expression::Number(meta.clone(), BigInt::from(0));
        //     for dim_expr in dimensions.iter().rev() {
        //         value = build_uniform_array(meta.clone(), value, dim_expr.clone());
        //     }

        //     let substitution =
        //         build_substitution(meta.clone(), symbol.name, vec![], AssignOp::AssignVar, value);
        //     initializations.push(substitution);
        // }
        values.push(Expression::Variable { meta: with_meta.clone(), name, access: Vec::new() })
    }
    if let Some(tuple) = init {
        let (op, expression) = tuple.tuple_init;
        let multi_sub = build_multi_substitution(
            meta.clone(),
            build_tuple(meta.clone(), values),
            op,
            expression,
        );
        initializations.push(multi_sub);
    }
    build_initialization_block(meta, xtype, initializations)
}