Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to parse var declarations #43

Merged
merged 7 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
KyrylR marked this conversation as resolved.
Show resolved Hide resolved
return;
}

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

return;
}
};

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

Expand Down
Loading