-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircomTemplateVisitor.ts
80 lines (65 loc) · 1.91 KB
/
CircomTemplateVisitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { parseIdentifier } from "./utils";
import {
CircomVisitor,
SignalDeclarationContext,
TemplateDeclarationContext,
} from "../generated";
import { Templates } from "../types";
import { ParserError } from "../errors/ParserError";
export class CircomTemplateVisitor extends CircomVisitor<void> {
templates: Templates;
currentTemplate: string | null;
constructor() {
super();
this.templates = {};
this.currentTemplate = null;
}
visitTemplateDeclaration = (ctx: TemplateDeclarationContext) => {
if (ctx.ID().getText() in this.templates) {
throw new ParserError({
message: `Template name ${ctx.ID().getText()} is already in use`,
line: ctx.start.line,
column: ctx.start.column,
});
}
this.currentTemplate = ctx.ID().getText();
const parameters: string[] = [];
if (ctx.args() && ctx.args().ID_list()) {
ctx
.args()
.ID_list()
.forEach((arg) => {
parameters.push(arg.getText());
});
}
this.templates[this.currentTemplate] = {
inputs: [],
parameters: parameters,
isCustom: !!ctx.CUSTOM(),
};
ctx
.templateBlock()
.templateStmt_list()
.forEach((stmt) => {
this.visitChildren(stmt);
});
this.currentTemplate = null;
};
visitSignalDeclaration = (ctx: SignalDeclarationContext) => {
if (this.currentTemplate) {
const signalDefinition = ctx.signalDefinition();
const identifier = signalDefinition.identifier();
const signalType = signalDefinition.SIGNAL_TYPE().getText();
this.templates[this.currentTemplate].inputs.push({
...parseIdentifier(identifier),
type: signalType,
});
ctx.identifier_list().forEach((identifier) => {
this.templates[this.currentTemplate!].inputs.push({
...parseIdentifier(identifier),
type: signalType,
});
});
}
};
}