Skip to content

Commit

Permalink
Added visitTemplateStmt to handle if stamens
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Oct 17, 2024
1 parent 4a8b48b commit e864214
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/core/dependencies/parser/CircomTemplateInputsVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
VarDeclarationContext,
VarDefinitionContext,
RhsValueContext,
TemplateStmtContext,
} from "@distributedlab/circom-parser";

import { InputData } from "../../../types/core";
Expand Down Expand Up @@ -60,6 +61,53 @@ export class CircomTemplateInputsVisitor extends CircomVisitor<void> {
}
};

visitTemplateStmt = (ctx: TemplateStmtContext) => {
if (ctx.identifier() && ctx.ASSIGNMENT() && ctx.expression(0)) {
const id = ctx.identifier().ID(0).getText();
const value = new CircomExpressionVisitor(true, this.vars).visitExpression(ctx.expression(0));

if (Array.isArray(value)) {
throw new HardhatZKitError(`Currently, only single value assignment is supported - ${value}`);
}

this.vars[id] = {
value: value,
};

return;
}

if (!ctx.IF()) {
this.visitChildren(ctx);

return;
}

const result = new CircomExpressionVisitor(true, this.vars).visitExpression(ctx.parExpression().expression());

if (Array.isArray(result)) {
throw new HardhatZKitError(
`Currently, only single value assignment is supported as a result inside if statement - ${result}`,
);
}

if (result === 1n) {
this.visitTemplateStmt(ctx.templateStmt(0));

return;
}

if (result === 0n && !ctx.ELSE()) {
return;
}

if (result === 0n && ctx.ELSE()) {
this.visitTemplateStmt(ctx.templateStmt(1));

return;
}
};

visitVarDeclaration = (ctx: VarDeclarationContext) => {
const vars = this._parseVarDefinition(ctx.varDefinition());

Expand Down

0 comments on commit e864214

Please sign in to comment.