circomspect_program_structure/abstract_syntax_tree/
ast_shortcuts.rs1use super::ast::*;
2use super::expression_builders::*;
3use super::statement_builders::*;
4use crate::ast::{Access, Expression, VariableType};
5use num_bigint::BigInt;
6
7#[derive(Clone)]
8pub struct Symbol {
9 pub name: String,
10 pub is_array: Vec<Expression>,
11 pub init: Option<Expression>,
12}
13
14pub struct TupleInit {
15 pub tuple_init: (AssignOp, Expression),
16}
17
18pub fn assign_with_op_shortcut(
19 op: ExpressionInfixOpcode,
20 meta: Meta,
21 variable: (String, Vec<Access>),
22 rhe: Expression,
23) -> Statement {
24 let (var, access) = variable;
25 let variable = build_variable(meta.clone(), var.clone(), access.clone());
26 let infix = build_infix(meta.clone(), variable, op, rhe);
27 build_substitution(meta, var, access, AssignOp::AssignVar, infix)
28}
29
30pub fn plusplus(meta: Meta, variable: (String, Vec<Access>)) -> Statement {
31 let one = build_number(meta.clone(), BigInt::from(1));
32 assign_with_op_shortcut(ExpressionInfixOpcode::Add, meta, variable, one)
33}
34
35pub fn subsub(meta: Meta, variable: (String, Vec<Access>)) -> Statement {
36 let one = build_number(meta.clone(), BigInt::from(1));
37 assign_with_op_shortcut(ExpressionInfixOpcode::Sub, meta, variable, one)
38}
39
40pub fn for_into_while(
41 meta: Meta,
42 init: Statement,
43 cond: Expression,
44 step: Statement,
45 body: Statement,
46) -> Statement {
47 let while_body = build_block(body.get_meta().clone(), vec![body, step]);
48 let while_statement = build_while_block(meta.clone(), cond, while_body);
49 build_block(meta, vec![init, while_statement])
50}
51
52pub fn split_declaration_into_single_nodes(
53 meta: Meta,
54 xtype: VariableType,
55 symbols: Vec<Symbol>,
56 op: AssignOp,
57) -> Statement {
58 let mut initializations = Vec::new();
60
61 for symbol in symbols {
62 let with_meta = meta.clone();
63 let has_type = xtype.clone();
64 let name = symbol.name.clone();
65 let dimensions = symbol.is_array;
66 let possible_init = symbol.init;
67 let single_declaration = build_declaration(with_meta, has_type, name, dimensions.clone());
68 initializations.push(single_declaration);
69
70 if let Option::Some(init) = possible_init {
71 let substitution = build_substitution(meta.clone(), symbol.name, vec![], op, init);
72 initializations.push(substitution);
73 }
74 }
87 build_initialization_block(meta, xtype, initializations)
88}
89
90pub fn split_declaration_into_single_nodes_and_multi_substitution(
91 meta: Meta,
92 xtype: VariableType,
93 symbols: Vec<Symbol>,
94 init: Option<TupleInit>,
95) -> Statement {
96 let mut initializations = Vec::new();
97 let mut values = Vec::new();
98 for symbol in symbols {
99 let with_meta = meta.clone();
100 let has_type = xtype.clone();
101 let name = symbol.name.clone();
102 let dimensions = symbol.is_array;
103 debug_assert!(symbol.init.is_none());
104 let single_declaration =
105 build_declaration(with_meta.clone(), has_type, name.clone(), dimensions.clone());
106 initializations.push(single_declaration);
107 values.push(Expression::Variable { meta: with_meta.clone(), name, access: Vec::new() })
121 }
122 if let Some(tuple) = init {
123 let (op, expression) = tuple.tuple_init;
124 let multi_sub = build_multi_substitution(
125 meta.clone(),
126 build_tuple(meta.clone(), values),
127 op,
128 expression,
129 );
130 initializations.push(multi_sub);
131 }
132 build_initialization_block(meta, xtype, initializations)
133}