arnaucube
arnaucube/circomPublic Self-hosted repository
circom/src/genOptCode.js
src/genOptCode.jsRaw
 1
 2
 3module.exports = genOpt;
 4
 5
 6function genOpt(ctx, ast) {
 7    if (ast.type == "OP") {
 8        if (ast.op == "=") {
 9            return genOptVarAssignement(ctx, ast);
10        } else {
11            error(ctx, ast, "GENOPT -> Invalid operation: " + ast.op);
12        }
13    } else if (ast.type == "TEMPLATEDEF") {
14        return genOptTemplateDef(ctx, ast);
15    } else {
16        error(ctx, ast, "GENOPT -> Invalid AST node type: " + ast.type);
17    }
18}
19
20function error(ctx, ast, errStr) {
21    ctx.error = {
22        pos:   {
23            first_line: ast.first_line,
24            first_column: ast.first_column,
25            last_line: ast.last_line,
26            last_column: ast.last_column
27        },
28        errStr: errStr,
29        errFile: ctx.fileName,
30        ast: ast
31    };
32}
33
34
35function genOptTemplateDef(ctx, ast) {
36    if (ctx.templates[ast.name]) {
37        return error(ctx, ast, "Template name already exists: "+ast.name);
38    }
39    ctx.templates[ast.name] = {
40        type: "TEMPLATE",
41        params: ast.params,
42        block: ast.block,
43        fileName: ctx.fileName,
44        filePath: ctx.filePath
45    };
46}
47
48function genOptVarAssignement(ctx, ast) {
49    let varName;
50    if (ast.values[0].type == "DECLARE") {
51        varName = genOptCode(ctx, ast.values[0]);
52        if (ctx.error) return;
53    } else {
54        varName = ast.values[0];
55    }
56    const varContent = getScope(ctx, varName.name, varName.selectors);
57    if (ctx.error) return;
58
59    if ((typeof(varContent) != "object")||(varContent == null)) return  error(ctx, ast, "Variable not defined");
60
61    if (varContent.type == "COMPONENT") return genOptInstantiateComponet(ctx, varName, ast.values[1]);
62    if (varContent.type == "SIGNAL") return error(ctx, ast, "Cannot assig to a signal with `=` use <-- or <== ops");
63
64    const res = genOpt(ctx, ast.values[1]);
65    if (ctx.error) return;
66
67    setScope(ctx, varName.name, varName.selectors, res);
68
69    return v;
70}