circom/src/iterateast.js
src/iterateast.jsRaw
1
2const assert = require("assert");
3
4module.exports = iterateAST;
5
6
7function iterateAST(ast, fn, _pfx) {
8 if (!ast) return;
9
10 const pfx = _pfx || "";
11 let itPfx = 0;
12
13 function getPfx() {
14 res = pfx+"."+itPfx;
15 itPfx ++;
16 return res;
17 }
18
19
20 let res = fn(ast, pfx);
21 if (res) return res;
22 function iterate(arr) {
23 if (arr) {
24 for (let i=0; i<arr.length; i++) {
25 res = iterateAST(arr[i], fn, getPfx());
26 if (res) return res;
27 }
28 }
29 }
30
31 if ((ast.type == "NUMBER")) {
32 //
33 } else if (ast.type == "VARIABLE") {
34 iterate(ast.selectors);
35 } else if (ast.type == "PIN") {
36 iterate(ast.component.selectors);
37 iterate(ast.pin.selectors);
38 } else if (ast.type == "OP") {
39 iterate(ast.values);
40 } else if (ast.type == "DECLARE") {
41 iterate(ast.name.selectors);
42 } else if (ast.type == "FUNCTIONCALL") {
43 iterate(ast.params);
44 } else if (ast.type == "BLOCK") {
45 iterate(ast.statements);
46 } else if (ast.type == "COMPUTE") {
47 iterateAST(ast.body, fn, getPfx());
48 } else if (ast.type == "FOR") {
49 iterateAST(ast.init, fn, getPfx());
50 iterateAST(ast.condition, fn, getPfx());
51 iterateAST(ast.step, fn, getPfx());
52 iterateAST(ast.body, fn, getPfx());
53 } else if (ast.type == "WHILE") {
54 iterateAST(ast.condition, fn, getPfx());
55 iterateAST(ast.body, fn, getPfx());
56 } else if (ast.type == "IF") {
57 iterateAST(ast.condition, fn, getPfx());
58 iterateAST(ast.then, fn, getPfx());
59 iterateAST(ast.else, fn, getPfx());
60 } else if (ast.type == "RETURN") {
61 iterateAST(ast.value, fn, getPfx());
62 } else if (ast.type == "ARRAY") {
63 iterate(ast.values);
64 } else if ((ast.type == "TEMPLATEDEF")) {
65 //
66 } else if ((ast.type == "FUNCTIONDEF")) {
67 //
68 } else if ((ast.type == "INCLUDE")) {
69 //
70 } else {
71 assert(false, "GEN -> Invalid AST iteration: " + ast.type);
72 }
73
74
75}