-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.js
360 lines (320 loc) · 10.7 KB
/
translate.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
*
* Translating ATLR abstract syntax into Scilla Syntax
*
*/
import SP from "./scillaParser.js"; //short for ScillaParser
import {
Cmodule,
Library,
ContractDef,
Field,
Component,
Clause,
Contract,
Pattern as Pat,
ComponentType as CT,
ScillaStmt as SS,
LibEntry as LE,
Lmodule,
} from "./syntax.js";
import * as SSS from "./syntax.js";
import SyntaxVisitor from "./syntaxVisitor.js";
import antlr4 from "antlr4";
import fs from "fs";
import ScillaLexer from "./scillaLexer.js";
import ScillaParser from "./scillaParser.js";
import { parse } from "path";
import * as ST from "./types.js";
export default class TranslateVisitor {
constructor() {
this.SV = new SyntaxVisitor();
}
printError(funcname, msg) {
console.log("[ERROR]" + funcname + ": " + msg);
}
translateStmt(ctx) {
if (ctx instanceof SP.LoadContext) {
const x = ctx.l.getText();
const r = ctx.r.getText();
return new SS.Load(x, r);
}
if (ctx instanceof SP.RemoteFetchContext) {
if (
ctx.r instanceof SP.RemoteLoadSidContext ||
ctx.r instanceof SP.RemoteLoadSpidContext
) {
const x = ctx.r.l.getText();
const adr = ctx.r.adr_id.getText();
const r = ctx.r.r.getText();
const res = new SS.RemoteLoad(x, adr, r);
return new SS.RemoteLoad(x, adr, r);
}
if (ctx.r instanceof SP.RemoteMapGetTrueContext) {
const x = ctx.r.l.getText();
const adr = ctx.r.adr_id.getText();
const m = ctx.r.r_id.getText();
const klist = ctx.r.keys.map((key) => key.i.getText());
return new SS.RemoteMapGet(x, adr, m, klist, true);
}
if (ctx.r instanceof SP.RemoteMapGetFalseContext) {
const x = ctx.r.l.getText();
const adr = ctx.r.adr_id.getText();
const m = ctx.r.r_id.getText();
const klist = ctx.r.keys.map((key) => key.i.getText());
return new SS.RemoteMapGet(x, adr, m, klist, false);
}
if (ctx.r instanceof SP.TypeCastContext) {
const x = ctx.r.l.getText();
const r = ctx.r.adr.getText();
const t = ST.resolveAddressTyp(ctx.r.t);
return new SS.TypeCast(x, r, t);
}
}
if (ctx instanceof SP.StoreContext) {
const x = ctx.l.getText();
const r = ctx.r.getText();
return new SS.Store(x, r);
}
if (ctx instanceof SP.BindContext) {
const x = ctx.l.getText();
const e = this.SV.translateExp(ctx.r);
return new SS.Bind(x, e);
}
if (ctx instanceof SP.MapUpdateContext) {
const m = ctx.l.getText();
const klist = ctx.keys.map((key) => key.i.getText());
const r = ctx.r.getText();
return new SS.MapUpdate(m, klist, r);
}
if (ctx instanceof SP.MapUpdateDeleteContext) {
const m = ctx.l.getText();
const klist = ctx.keys.map((key) => key.i.getText());
return new SS.MapUpdate(m, klist, undefined);
}
if (ctx instanceof SP.MapGetContext) {
const x = ctx.l.getText();
const m = ctx.r.getText();
const klist = ctx.keys.map((key) => key.i.getText());
return new SS.MapGet(x, m, klist, true);
}
if (ctx instanceof SP.MapGetExistsContext) {
const x = ctx.l.getText();
const m = ctx.r.getText();
const klist = ctx.keys.map((key) => key.i.getText());
return new SS.MapGet(x, m, klist, false);
}
if (ctx instanceof SP.ReadFromBCContext) {
const x = ctx.l.getText();
const bf = ctx.c.getText();
if (bf === "BLOCKNUMBER") {
return new SS.ReadFromBC(x, new SSS.CurBlockNum());
}
if (bf === "CHAINID") {
return new SS.ReadFromBC(x, new SSS.ChainID());
}
if (bf === "TIMESTAMP") {
const arg = ctx.args_opt.args[0].getText();
return new SS.ReadFromBC(x, new SSS.TimeStamp(arg)); //TODO: add iden to timestamp
}
this.printError("translateStmts", "Couldn't match ReadFromBC");
}
if (ctx instanceof SP.AcceptContext) {
return new SS.AcceptPayment();
}
if (ctx instanceof SP.SendContext) {
const ms = ctx.m.getText();
return new SS.SendMsgs(ms);
}
if (ctx instanceof SP.CreateEvntContext) {
const param = ctx.m.getText();
return new SS.CreateEvnt(param);
}
if (ctx instanceof SP.CallProcContext) {
const p = ctx.p.getText();
const actuals = ctx.args.map((arg) => arg.getText());
return new SS.CallProc(p, actuals);
}
if (ctx instanceof SP.IterateContext) {
const l = ctx.l.getText();
const p = ctx.p.getText();
return new SS.Iterate(l, p);
}
if (ctx instanceof SP.ThrowContext) {
const eopt = ctx.mopt === null ? undefined : ctx.mopt.getText();
return new SS.Throw(eopt);
}
if (ctx instanceof SP.MatchStmtContext) {
const x = ctx.x.getText();
const clauses = ctx.cs.map((c) => this.translateStmtPattern(c));
return new SS.MatchStmt(x, clauses);
}
}
translateStmtPattern(ctx) {
const pat = this.translatePattern(ctx.p);
const stmts = this.translateStmts(ctx);
return new Clause(pat, stmts);
}
translatePattern(ctx) {
if (ctx instanceof SP.WildcardContext) {
return new Pat.WildCard();
}
if (ctx instanceof SP.BinderContext) {
return new Pat.Binder(ctx.x.getText());
}
if (ctx instanceof SP.ConstructorContext) {
const c = ctx.c.getText();
const ps = ctx.ps.map((p) => this.translateArgPattern(p));
return new Pat.ConstructorPat(c, ps);
}
}
translateArgPattern(ctx) {
if (ctx instanceof SP.WildcardArgContext) {
return new Pat.WildCard();
}
if (ctx instanceof SP.BinderArgContext) {
return new Pat.Binder(ctx.x.getText());
}
if (ctx instanceof SP.ConstructorArgContext) {
return new Pat.ConstructorPat(ctx.c.getText(), []);
}
if (ctx instanceof SP.PatternArgContext) {
return this.translatePattern(ctx.p);
}
this.printError("translateArgPattern", "Couldn't match Context");
}
translateStmts(ctx) {
return ctx.ss.map((s) => this.translateStmt(s));
}
translateIdWithType(ctx) {
const param = ctx.n.getText();
const type = ST.generateSType(ctx.t.t);
return [param, type];
}
translateField(ctx) {
const parampair = this.translateIdWithType(ctx.iwt);
const e = this.SV.translateExp(ctx.rhs);
return new Field(parampair[0], parampair[1], e);
}
translateComponentParams(ctx) {
return ctx.params.map((param) => this.translateIdWithType(param.iwt));
}
translateComp(ctx) {
if (ctx instanceof SP.TransitionCompContext) {
const compType = new CT.CompTrans();
const compName = ctx.t.t.getText();
const compParams = this.translateComponentParams(ctx.t.params);
const compBody =
ctx.t.ss.ss === null ? [] : this.translateStmts(ctx.t.ss.ss);
return new Component(compType, compName, compParams, compBody);
}
if (ctx instanceof SP.ProcedureCompContext) {
const compType = new CT.CompProc();
const compName = ctx.p.t.getText();
const compParams = this.translateComponentParams(ctx.p.params);
const compBody =
ctx.p.ss.ss === null ? [] : this.translateStmts(ctx.p.ss.ss);
return new Component(compType, compName, compParams, compBody);
}
this.printError("translateComp", "Didn't not match Context");
}
translateContract(ctx) {
const cname = ctx.c.getText();
const cparams = ctx.params.map((param) =>
this.translateIdWithType(param.iwt)
);
const cconstraint =
ctx.ct === null ? undefined : this.SV.translateExp(ctx.ct.f);
const cfields = ctx.fs.map((field) => this.translateField(field));
const ccomps = ctx.comps.map((comp) => this.translateComp(comp));
return new Contract(cname, cparams, cconstraint, cfields, ccomps);
}
translateContractDef(ctx) {
const cname = ctx.cid().getText();
const cArgTypes = ctx.t.map((targ) => ST.resolveTArg(targ));
return new ContractDef(cname, cArgTypes);
}
translateLibEntry(ctx) {
if (ctx instanceof SP.LibVarContext) {
const x = ctx.ns.getText();
const tyopt = ctx.t === null ? undefined : ST.generateSType(ctx.t.t);
const e = this.SV.translateExp(ctx.e);
return new LE.LibVar(x, tyopt, e);
}
if (ctx instanceof SP.LibTypEmptContext) {
const x = ctx.tname.getText();
return new LE.LibType(x, []);
}
if (ctx instanceof SP.LibTypContext) {
const x = ctx.tname.getText();
const cntrDef = ctx.constrs.map((constr) =>
this.translateContractDef(constr)
);
return new LE.LibType(x, cntrDef);
}
this.printError("translateLibEntry", "Did not match Context");
}
translateLibrary(ctx) {
const lname = ctx.n.getText();
const lentries = ctx.ls.map((libentry) => this.translateLibEntry(libentry));
return new Library(lname, lentries);
}
translateELib(importname) {
//Import the external library by name
const filename = "stdlib/" + importname.c.getText() + ".scillib";
const input = fs.readFileSync(filename).toString();
const chars = new antlr4.InputStream(input);
const lexer = new ScillaLexer(chars);
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new ScillaParser(tokens);
const tree = parser.lmodule();
const lmodule = this.translateLModule(tree);
if (importname instanceof SP.NoShadowELibContext) {
return [importname.c.getText(), undefined, lmodule];
} else if (importname instanceof SP.ShadowELibContext) {
return [importname.c1.getText(), importname.c2.getText(), lmodule];
}
this.printError("translateElib", "Didn't match kind of elib");
}
translateELibraries(ctx) {
if (ctx === null) {
return undefined;
} else {
const elibs = ctx.els.map((importname) => this.translateELib(importname));
//elibs is a list of pairs (lists of 2 of String * lmodule)
return elibs;
}
}
translateCModule(ctx) {
const scillaVer = parseInt(ctx.NUMBER().getText());
var elibs = undefined;
var libs = undefined;
if (ctx.els !== null) {
elibs = this.translateELibraries(ctx.els);
}
if (ctx.ls !== null) {
libs = this.translateLibrary(ctx.ls);
}
const contract = this.translateContract(ctx.c);
return new Cmodule(scillaVer, libs, elibs, contract);
}
translateLModule(ctx) {
const scillaVer = parseInt(ctx.NUMBER().getText());
var elibs = undefined;
if (ctx.els !== null) {
elibs = this.translateELibraries(ctx.els); //TODO: look for loops?
}
var lib = this.translateLibrary(ctx.l);
return new Lmodule(scillaVer, lib, elibs);
}
visitChildren(ctx) {
if (!ctx) {
return;
}
return ctx instanceof SP.CmoduleContext
? this.translateCModule(ctx)
: ctx instanceof SP.LmoduleContext
? this.translateLModule(ctx)
: undefined;
}
}