@givelotus/compiler 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +331 -2
- package/artifact-v2.schema.json +58 -0
- package/artifact.schema.json +19 -0
- package/dist/ast.d.ts +292 -0
- package/dist/ast.js +150 -0
- package/dist/bindings.d.ts +6 -0
- package/dist/bindings.js +134 -0
- package/dist/call-runtime.d.ts +84 -0
- package/dist/call-runtime.js +249 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +22 -0
- package/dist/compiler.d.ts +79 -0
- package/dist/compiler.js +125 -0
- package/dist/diagnostics.d.ts +163 -0
- package/dist/diagnostics.js +498 -0
- package/dist/ecash.d.ts +41 -0
- package/dist/ecash.js +190 -0
- package/dist/fees.d.ts +164 -0
- package/dist/fees.js +208 -0
- package/dist/fixture-cli.d.ts +2 -0
- package/dist/fixture-cli.js +41 -0
- package/dist/fixtures.d.ts +68 -0
- package/dist/fixtures.js +46 -0
- package/dist/forth-ir.d.ts +73 -0
- package/dist/forth-ir.js +147 -0
- package/dist/generic-cli.d.ts +2 -0
- package/dist/generic-cli.js +140 -0
- package/dist/generic.d.ts +48 -0
- package/dist/generic.js +2566 -0
- package/dist/lints.d.ts +8 -0
- package/dist/lints.js +204 -0
- package/dist/names.d.ts +1 -0
- package/dist/names.js +7 -0
- package/dist/opcodes.d.ts +154 -0
- package/dist/opcodes.js +162 -0
- package/dist/optimizer.d.ts +58 -0
- package/dist/optimizer.js +786 -0
- package/dist/package-fetch-cli.d.ts +2 -0
- package/dist/package-fetch-cli.js +17 -0
- package/dist/package-lock-cli.d.ts +2 -0
- package/dist/package-lock-cli.js +28 -0
- package/dist/package-sources.d.ts +82 -0
- package/dist/package-sources.js +785 -0
- package/dist/package-update-cli.d.ts +2 -0
- package/dist/package-update-cli.js +20 -0
- package/dist/packages.d.ts +231 -0
- package/dist/packages.js +1324 -0
- package/dist/parser.d.ts +32 -0
- package/dist/parser.js +575 -0
- package/dist/policy.d.ts +83 -0
- package/dist/policy.js +207 -0
- package/dist/runtime.d.ts +58 -0
- package/dist/runtime.js +126 -0
- package/dist/scheduler.d.ts +38 -0
- package/dist/scheduler.js +76 -0
- package/dist/semantic-ir.d.ts +76 -0
- package/dist/semantic-ir.js +231 -0
- package/dist/std.d.ts +27 -0
- package/dist/std.js +282 -0
- package/dist/target-compiler.d.ts +9 -0
- package/dist/target-compiler.js +139 -0
- package/dist/targets.d.ts +53 -0
- package/dist/targets.js +67 -0
- package/dist/token-cli.d.ts +2 -0
- package/dist/token-cli.js +82 -0
- package/dist/token-metadata.d.ts +43 -0
- package/dist/token-metadata.js +166 -0
- package/dist/toml-subset.d.ts +19 -0
- package/dist/toml-subset.js +81 -0
- package/dist/transaction-runtime.d.ts +182 -0
- package/dist/transaction-runtime.js +646 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +3 -0
- package/dist/vm-cost.d.ts +11 -0
- package/dist/vm-cost.js +541 -0
- package/package.json +74 -4
- package/policy-artifact.schema.json +20 -0
- package/std/custody/arbiter-escrow.lotus +20 -0
- package/std/custody/htlc.lotus +15 -0
- package/std/custody/multisig-2of3.lotus +13 -0
- package/std/custody/single-owner-wallet.lotus +9 -0
- package/std/custody/vault-with-recovery.lotus +14 -0
- package/std/defi/amm-pool-v2.lotus +97 -0
- package/std/defi/indexed-limit-order.lotus +37 -0
- package/std/nft/nft-collection.lotus +51 -0
- package/std/nft/royalty-transfer-rules.lotus +38 -0
- package/std/nft/unique-nft.lotus +30 -0
- package/std/tokens/capped-supply-token.lotus +76 -0
- package/std/tokens/fixed-supply-token.lotus +29 -0
- package/std/tokens/lrc20.lotus +26 -0
- package/std/tokens/standard-transfer-rules.lotus +15 -0
- package/std/tokens/stateful-token-merge.lotus +132 -0
- package/std/tokens/upgradable-token-rules.lotus +70 -0
- package/std/tokens/voucher-mint-token.lotus +31 -0
- package/std-pkg/lotus.toml +9 -0
- package/std-pkg/src/lrc20.lotus +38 -0
- package/std-pkg/src/stateful_token.lotus +131 -0
- package/std-pkg/src/upgradable.lotus +68 -0
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DiagnosticBag } from './diagnostics.js';
|
|
2
|
+
import { type LanguageVersion, type Program, type Token } from './ast.js';
|
|
3
|
+
export declare function lex(source: string, file: string, bag: DiagnosticBag): Token[];
|
|
4
|
+
export declare class Parser {
|
|
5
|
+
private readonly tokens;
|
|
6
|
+
private readonly bag;
|
|
7
|
+
private readonly languageVersion;
|
|
8
|
+
private i;
|
|
9
|
+
constructor(tokens: Token[], bag: DiagnosticBag, languageVersion?: LanguageVersion);
|
|
10
|
+
private peek;
|
|
11
|
+
private fatal;
|
|
12
|
+
private take;
|
|
13
|
+
private ident;
|
|
14
|
+
private type;
|
|
15
|
+
private params;
|
|
16
|
+
/** Pratt `nud` (null denotation): prefix operators, grouping, literals, and primary names/calls. */
|
|
17
|
+
private nud;
|
|
18
|
+
/** Left binding power for Pratt `led` (infix and postfix tokens). */
|
|
19
|
+
private ledBindingPower;
|
|
20
|
+
/** Pratt `led` (left denotation): postfix indexing/member access and infix binary operators. */
|
|
21
|
+
private led;
|
|
22
|
+
private expression;
|
|
23
|
+
private statements;
|
|
24
|
+
/**
|
|
25
|
+
* Helper statements. `for` is rejected: inlining it into a method loop would
|
|
26
|
+
* nest past the consensus cap of two without a second loop implementation.
|
|
27
|
+
* `return` is only the last statement of the function body, not of an `if`.
|
|
28
|
+
*/
|
|
29
|
+
private helperBody;
|
|
30
|
+
private importDecl;
|
|
31
|
+
parse(requireContract?: boolean): Program;
|
|
32
|
+
}
|
package/dist/parser.js
ADDED
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import { assertPortableName } from './names.js';
|
|
2
|
+
import { SUPPORTED_COMPILER_EDITIONS, SUPPORTED_OPTIMIZER_VERSIONS, isSupportedCompilerEdition, isSupportedOptimizerVersion, } from './optimizer.js';
|
|
3
|
+
import { MAX_METHODS, MAX_SOURCE, } from './ast.js';
|
|
4
|
+
function bindName(name, span, bag) {
|
|
5
|
+
try {
|
|
6
|
+
assertPortableName(name);
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
bag.raise('E0115', error.message, span);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function utf8Offset(source, utf16) {
|
|
13
|
+
return new TextEncoder().encode(source.slice(0, utf16)).length;
|
|
14
|
+
}
|
|
15
|
+
function spanAt(file, start, end) {
|
|
16
|
+
return { start, end, file };
|
|
17
|
+
}
|
|
18
|
+
function cover(start, end) {
|
|
19
|
+
return {
|
|
20
|
+
start: start.start,
|
|
21
|
+
end: end.end,
|
|
22
|
+
...(start.file !== undefined ? { file: start.file } : {}),
|
|
23
|
+
...(start.module !== undefined ? { module: start.module } : {}),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function lex(source, file, bag) {
|
|
27
|
+
const encoded = new TextEncoder().encode(source);
|
|
28
|
+
if (encoded.length > MAX_SOURCE)
|
|
29
|
+
bag.raise('E0002', 'source exceeds 65536 UTF-8 bytes', spanAt(file, 0, encoded.length));
|
|
30
|
+
const re = /\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/|==|!=|>=|<=|\+=|-=|&&|\|\||::|"[^"\\\n]*"|[A-Za-z_][A-Za-z_0-9]*|[0-9]+|[<>{}()\[\]:;,.!+*\-=/%]/gy;
|
|
31
|
+
const out = [];
|
|
32
|
+
let pos = 0;
|
|
33
|
+
while (pos < source.length) {
|
|
34
|
+
re.lastIndex = pos;
|
|
35
|
+
const m = re.exec(source);
|
|
36
|
+
if (!m) {
|
|
37
|
+
const cp = source.codePointAt(pos) ?? 0, width = cp > 0xffff ? 2 : 1;
|
|
38
|
+
const start = utf8Offset(source, pos);
|
|
39
|
+
if (source.startsWith('/*', pos))
|
|
40
|
+
bag.raise('E0003', 'unterminated block comment', spanAt(file, start, encoded.length));
|
|
41
|
+
if (source.startsWith('"', pos) || source.startsWith("'", pos))
|
|
42
|
+
bag.raise('E0004', 'unterminated string literal', spanAt(file, start, encoded.length));
|
|
43
|
+
bag.raise('E0001', 'unsupported character', spanAt(file, start, utf8Offset(source, pos + width)));
|
|
44
|
+
}
|
|
45
|
+
if (!/^\s|^\/\/|^\/\*/.test(m[0]))
|
|
46
|
+
out.push({ text: m[0], span: spanAt(file, utf8Offset(source, pos), utf8Offset(source, re.lastIndex)) });
|
|
47
|
+
pos = re.lastIndex;
|
|
48
|
+
}
|
|
49
|
+
out.push({ text: '<eof>', span: spanAt(file, utf8Offset(source, pos), utf8Offset(source, pos)) });
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
export class Parser {
|
|
53
|
+
tokens;
|
|
54
|
+
bag;
|
|
55
|
+
languageVersion;
|
|
56
|
+
i = 0;
|
|
57
|
+
constructor(tokens, bag, languageVersion = 0) {
|
|
58
|
+
this.tokens = tokens;
|
|
59
|
+
this.bag = bag;
|
|
60
|
+
this.languageVersion = languageVersion;
|
|
61
|
+
}
|
|
62
|
+
peek() { return this.tokens[this.i]; }
|
|
63
|
+
fatal(code, message, span) { this.bag.raise(code, message, span); }
|
|
64
|
+
take(expected) {
|
|
65
|
+
const t = this.peek();
|
|
66
|
+
if (expected && t.text !== expected)
|
|
67
|
+
this.fatal('E0101', `expected ${expected}`, t.span);
|
|
68
|
+
this.i++;
|
|
69
|
+
return t;
|
|
70
|
+
}
|
|
71
|
+
ident() {
|
|
72
|
+
const t = this.take();
|
|
73
|
+
if (!/^[A-Za-z_][A-Za-z_0-9]*$/.test(t.text))
|
|
74
|
+
this.fatal('E0102', 'expected identifier', t.span);
|
|
75
|
+
return t;
|
|
76
|
+
}
|
|
77
|
+
type() {
|
|
78
|
+
const t = this.ident();
|
|
79
|
+
if (t.text === 'Bytes') {
|
|
80
|
+
this.take('(');
|
|
81
|
+
const n = this.take();
|
|
82
|
+
this.take(')');
|
|
83
|
+
if (!/^[0-9]+$/.test(n.text) || Number(n.text) < 1 || Number(n.text) > 8192)
|
|
84
|
+
this.fatal('E0104', 'Bytes bound 1..8192', n.span);
|
|
85
|
+
return `Bytes(${Number(n.text)})`;
|
|
86
|
+
}
|
|
87
|
+
if (['PublicKey', 'Signature', 'Bytes32', 'Int', 'Amount', 'TokenId', 'TxId', 'StateRoot', 'ChainId', 'SigHash', 'BranchRoot', 'Bool', 'MethodLeafHash', 'ContractRootHash'].includes(t.text)) {
|
|
88
|
+
return t.text;
|
|
89
|
+
}
|
|
90
|
+
this.fatal('E0103', `unsupported type ${t.text}`, t.span);
|
|
91
|
+
}
|
|
92
|
+
params(close) {
|
|
93
|
+
const out = [];
|
|
94
|
+
if (this.peek().text !== close)
|
|
95
|
+
while (true) {
|
|
96
|
+
const name = this.ident();
|
|
97
|
+
this.take(':');
|
|
98
|
+
const type = this.type();
|
|
99
|
+
bindName(name.text, name.span, this.bag);
|
|
100
|
+
if (out.some(x => x.name === name.text))
|
|
101
|
+
this.fatal('E0201', `duplicate parameter ${name.text}`, name.span);
|
|
102
|
+
out.push({ name: name.text, type, span: name.span });
|
|
103
|
+
if (this.peek().text !== ',')
|
|
104
|
+
break;
|
|
105
|
+
this.take(',');
|
|
106
|
+
}
|
|
107
|
+
this.take(close);
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
/** Pratt `nud` (null denotation): prefix operators, grouping, literals, and primary names/calls. */
|
|
111
|
+
nud(depth) {
|
|
112
|
+
const start = this.peek();
|
|
113
|
+
if (start.text === '!' || start.text === '-') {
|
|
114
|
+
this.take();
|
|
115
|
+
const inner = this.expression(70, depth + 1);
|
|
116
|
+
return { kind: 'unary', value: start.text, right: inner, span: cover(start.span, inner.span) };
|
|
117
|
+
}
|
|
118
|
+
if (start.text === '(') {
|
|
119
|
+
this.take();
|
|
120
|
+
const inner = this.expression(0, depth + 1);
|
|
121
|
+
const end = this.take(')');
|
|
122
|
+
return { ...inner, span: cover(start.span, end.span) };
|
|
123
|
+
}
|
|
124
|
+
if (/^[0-9]+$/.test(start.text)) {
|
|
125
|
+
this.take();
|
|
126
|
+
return { kind: 'number', value: start.text, span: start.span };
|
|
127
|
+
}
|
|
128
|
+
if (start.text === 'true' || start.text === 'false') {
|
|
129
|
+
this.take();
|
|
130
|
+
return { kind: 'bool', value: start.text, span: start.span };
|
|
131
|
+
}
|
|
132
|
+
const name = this.ident();
|
|
133
|
+
let value = name.text;
|
|
134
|
+
if (this.peek().text === '::') {
|
|
135
|
+
// `alias::helper(...)` or a contract reference `alias::Contract` / `alias::Contract.method`.
|
|
136
|
+
this.take('::');
|
|
137
|
+
value += '::' + this.ident().text;
|
|
138
|
+
}
|
|
139
|
+
while (this.peek().text === '.') {
|
|
140
|
+
this.take('.');
|
|
141
|
+
value += '.' + this.ident().text;
|
|
142
|
+
}
|
|
143
|
+
if (this.peek().text === '(') {
|
|
144
|
+
this.take('(');
|
|
145
|
+
const args = [];
|
|
146
|
+
if (this.peek().text !== ')')
|
|
147
|
+
while (true) {
|
|
148
|
+
args.push(this.expression(0, depth + 1));
|
|
149
|
+
if (this.peek().text !== ',')
|
|
150
|
+
break;
|
|
151
|
+
this.take(',');
|
|
152
|
+
}
|
|
153
|
+
const end = this.take(')');
|
|
154
|
+
return { kind: 'call', value, args, span: cover(name.span, end.span) };
|
|
155
|
+
}
|
|
156
|
+
return { kind: 'name', value, span: cover(name.span, this.tokens[this.i - 1].span) };
|
|
157
|
+
}
|
|
158
|
+
/** Left binding power for Pratt `led` (infix and postfix tokens). */
|
|
159
|
+
ledBindingPower(token) {
|
|
160
|
+
switch (token) {
|
|
161
|
+
case '||': return 10;
|
|
162
|
+
case '&&': return 20;
|
|
163
|
+
case '==':
|
|
164
|
+
case '!=': return 30;
|
|
165
|
+
case '<':
|
|
166
|
+
case '>':
|
|
167
|
+
case '<=':
|
|
168
|
+
case '>=': return 40;
|
|
169
|
+
case '+':
|
|
170
|
+
case '-': return 50;
|
|
171
|
+
case '*':
|
|
172
|
+
case '/':
|
|
173
|
+
case '%': return 60;
|
|
174
|
+
case '[':
|
|
175
|
+
case '.': return 80;
|
|
176
|
+
default: return 0;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** Pratt `led` (left denotation): postfix indexing/member access and infix binary operators. */
|
|
180
|
+
led(left, bp, depth) {
|
|
181
|
+
const op = this.take();
|
|
182
|
+
if (op.text === '[') {
|
|
183
|
+
const index = this.expression(0, depth + 1);
|
|
184
|
+
const end = this.take(']');
|
|
185
|
+
return { kind: 'index', left, right: index, span: cover(left.span, end.span) };
|
|
186
|
+
}
|
|
187
|
+
if (op.text === '.') {
|
|
188
|
+
const field = this.ident();
|
|
189
|
+
if (this.peek().text === '(') {
|
|
190
|
+
this.take('(');
|
|
191
|
+
const args = [];
|
|
192
|
+
if (this.peek().text !== ')')
|
|
193
|
+
while (true) {
|
|
194
|
+
args.push(this.expression(0, depth + 1));
|
|
195
|
+
if (this.peek().text !== ',')
|
|
196
|
+
break;
|
|
197
|
+
this.take(',');
|
|
198
|
+
}
|
|
199
|
+
const end = this.take(')');
|
|
200
|
+
return { kind: 'member_call', left, value: field.text, args, span: cover(left.span, end.span) };
|
|
201
|
+
}
|
|
202
|
+
return { kind: 'member', left, value: field.text, span: cover(left.span, field.span) };
|
|
203
|
+
}
|
|
204
|
+
const right = this.expression(bp + 1, depth + 1);
|
|
205
|
+
return { kind: 'binary', value: op.text, left, right, span: cover(left.span, right.span) };
|
|
206
|
+
}
|
|
207
|
+
expression(minBp = 0, depth = 0) {
|
|
208
|
+
if (depth > 64)
|
|
209
|
+
this.fatal('E0105', 'expression depth exceeded', this.peek().span);
|
|
210
|
+
let left = this.nud(depth);
|
|
211
|
+
let steps = 0;
|
|
212
|
+
while (true) {
|
|
213
|
+
const bp = this.ledBindingPower(this.peek().text);
|
|
214
|
+
if (bp === 0 || bp < minBp)
|
|
215
|
+
break;
|
|
216
|
+
if (++steps > 1024)
|
|
217
|
+
this.fatal('E0105', 'expression depth exceeded', this.peek().span);
|
|
218
|
+
left = this.led(left, bp, depth);
|
|
219
|
+
}
|
|
220
|
+
return left;
|
|
221
|
+
}
|
|
222
|
+
statements(depth = 0, loops = 0) {
|
|
223
|
+
if (depth > 16)
|
|
224
|
+
this.fatal('E0106', 'control nesting depth exceeded', this.peek().span);
|
|
225
|
+
this.take('{');
|
|
226
|
+
const out = [];
|
|
227
|
+
while (this.peek().text !== '}') {
|
|
228
|
+
const start = this.take();
|
|
229
|
+
if (start.text === 'require') {
|
|
230
|
+
this.take('(');
|
|
231
|
+
const expr = this.expression();
|
|
232
|
+
this.take(')');
|
|
233
|
+
const end = this.take(';');
|
|
234
|
+
out.push({ kind: 'require', expr, span: cover(start.span, end.span) });
|
|
235
|
+
}
|
|
236
|
+
else if (start.text === 'if') {
|
|
237
|
+
const expr = this.expression();
|
|
238
|
+
const then = this.statements(depth + 1, loops);
|
|
239
|
+
const otherwise = this.peek().text === 'else' ? (this.take('else'), this.statements(depth + 1, loops)) : [];
|
|
240
|
+
out.push({ kind: 'if', expr, then, otherwise, span: cover(start.span, this.tokens[this.i - 1].span) });
|
|
241
|
+
}
|
|
242
|
+
else if (start.text === 'for') {
|
|
243
|
+
if (loops >= 2)
|
|
244
|
+
this.fatal('E0108', 'loop nesting exceeds two', start.span);
|
|
245
|
+
const variable = this.ident();
|
|
246
|
+
this.take('in');
|
|
247
|
+
const collection = this.ident().text + '.' + (this.take('.'), this.ident().text);
|
|
248
|
+
if (collection !== 'tx.inputs' && collection !== 'tx.outputs')
|
|
249
|
+
this.fatal('E0109', `unsupported collection ${collection}`, start.span);
|
|
250
|
+
const then = this.statements(depth + 1, loops + 1);
|
|
251
|
+
out.push({ kind: 'for', variable: variable.text, collection, then, span: cover(start.span, this.tokens[this.i - 1].span) });
|
|
252
|
+
}
|
|
253
|
+
else if (start.text === 'break') {
|
|
254
|
+
if (loops === 0)
|
|
255
|
+
this.fatal('E0124', 'break is only allowed inside a for loop', start.span);
|
|
256
|
+
const end = this.take(';');
|
|
257
|
+
out.push({ kind: 'break', span: cover(start.span, end.span) });
|
|
258
|
+
}
|
|
259
|
+
else if (start.text === 'let') {
|
|
260
|
+
this.take('(');
|
|
261
|
+
const a = this.ident();
|
|
262
|
+
bindName(a.text, a.span, this.bag);
|
|
263
|
+
this.take(',');
|
|
264
|
+
const b = this.ident();
|
|
265
|
+
bindName(b.text, b.span, this.bag);
|
|
266
|
+
if (a.text === b.text)
|
|
267
|
+
this.fatal('E0201', `duplicate tuple binding ${b.text}`, b.span);
|
|
268
|
+
this.take(')');
|
|
269
|
+
this.take('=');
|
|
270
|
+
const expr = this.expression();
|
|
271
|
+
const end = this.take(';');
|
|
272
|
+
out.push({ kind: 'let_tuple', variables: [{ name: a.text, span: a.span }, { name: b.text, span: b.span }], expr, span: cover(start.span, end.span) });
|
|
273
|
+
}
|
|
274
|
+
else if (/^[A-Za-z_][A-Za-z_0-9]*$/.test(start.text)) {
|
|
275
|
+
const op = this.take();
|
|
276
|
+
if (!['=', '+=', '-='].includes(op.text))
|
|
277
|
+
this.fatal('E0110', `unsupported assignment ${op.text}`, op.span);
|
|
278
|
+
const expr = this.expression();
|
|
279
|
+
const end = this.take(';');
|
|
280
|
+
out.push({ kind: 'assign', variable: start.text, assignOp: op.text, expr, span: cover(start.span, end.span) });
|
|
281
|
+
}
|
|
282
|
+
else
|
|
283
|
+
this.fatal('E0111', `unsupported statement ${start.text}`, start.span);
|
|
284
|
+
if (out.length > 256)
|
|
285
|
+
this.fatal('E0107', 'statement budget exceeded', start.span);
|
|
286
|
+
}
|
|
287
|
+
this.take('}');
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Helper statements. `for` is rejected: inlining it into a method loop would
|
|
292
|
+
* nest past the consensus cap of two without a second loop implementation.
|
|
293
|
+
* `return` is only the last statement of the function body, not of an `if`.
|
|
294
|
+
*/
|
|
295
|
+
helperBody(depth = 0, nested = false) {
|
|
296
|
+
if (depth > 16)
|
|
297
|
+
this.fatal('E0106', 'control nesting depth exceeded', this.peek().span);
|
|
298
|
+
this.take('{');
|
|
299
|
+
const out = [];
|
|
300
|
+
while (this.peek().text !== '}') {
|
|
301
|
+
if (out.at(-1)?.kind === 'return')
|
|
302
|
+
this.fatal('E0123', 'statement after return', this.peek().span);
|
|
303
|
+
const start = this.peek();
|
|
304
|
+
if (start.text === 'let') {
|
|
305
|
+
this.take();
|
|
306
|
+
if (this.peek().text === '(') {
|
|
307
|
+
this.take('(');
|
|
308
|
+
const a = this.ident();
|
|
309
|
+
bindName(a.text, a.span, this.bag);
|
|
310
|
+
this.take(',');
|
|
311
|
+
const b = this.ident();
|
|
312
|
+
bindName(b.text, b.span, this.bag);
|
|
313
|
+
if (a.text === b.text)
|
|
314
|
+
this.fatal('E0201', `duplicate tuple binding ${b.text}`, b.span);
|
|
315
|
+
this.take(')');
|
|
316
|
+
this.take('=');
|
|
317
|
+
const expr = this.expression();
|
|
318
|
+
const end = this.take(';');
|
|
319
|
+
out.push({ kind: 'let_tuple', variables: [{ name: a.text, span: a.span }, { name: b.text, span: b.span }], expr, span: cover(start.span, end.span) });
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
const name = this.ident();
|
|
323
|
+
bindName(name.text, name.span, this.bag);
|
|
324
|
+
this.take(':');
|
|
325
|
+
const type = this.type();
|
|
326
|
+
this.take('=');
|
|
327
|
+
const expr = this.expression();
|
|
328
|
+
const end = this.take(';');
|
|
329
|
+
out.push({ kind: 'let', name: name.text, type, expr, nameSpan: name.span, span: cover(start.span, end.span) });
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
else if (start.text === 'require') {
|
|
333
|
+
this.take();
|
|
334
|
+
this.take('(');
|
|
335
|
+
const expr = this.expression();
|
|
336
|
+
this.take(')');
|
|
337
|
+
const end = this.take(';');
|
|
338
|
+
out.push({ kind: 'require', expr, span: cover(start.span, end.span) });
|
|
339
|
+
}
|
|
340
|
+
else if (start.text === 'if') {
|
|
341
|
+
this.take();
|
|
342
|
+
const expr = this.expression();
|
|
343
|
+
const then = this.helperBody(depth + 1, true);
|
|
344
|
+
const otherwise = this.peek().text === 'else' ? (this.take('else'), this.helperBody(depth + 1, true)) : [];
|
|
345
|
+
out.push({ kind: 'if', expr, then, otherwise, span: cover(start.span, this.tokens[this.i - 1].span) });
|
|
346
|
+
}
|
|
347
|
+
else if (start.text === 'for')
|
|
348
|
+
this.fatal('E0122', 'for is not allowed inside a helper', start.span);
|
|
349
|
+
else if (start.text === 'return') {
|
|
350
|
+
if (nested)
|
|
351
|
+
this.fatal('E0123', 'return must be the final statement of the helper', start.span);
|
|
352
|
+
this.take();
|
|
353
|
+
const expr = this.expression();
|
|
354
|
+
const end = this.take(';');
|
|
355
|
+
out.push({ kind: 'return', expr, span: cover(start.span, end.span) });
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
this.fatal('E0111', `unsupported statement ${this.take().text}`, start.span);
|
|
359
|
+
if (out.length > 256)
|
|
360
|
+
this.fatal('E0107', 'statement budget exceeded', start.span);
|
|
361
|
+
}
|
|
362
|
+
const end = this.take('}');
|
|
363
|
+
if (!nested && out.at(-1)?.kind !== 'return')
|
|
364
|
+
this.fatal('E0123', 'helper must end with return', end.span);
|
|
365
|
+
return out;
|
|
366
|
+
}
|
|
367
|
+
importDecl(start) {
|
|
368
|
+
const target = this.take();
|
|
369
|
+
let targetText = target.text;
|
|
370
|
+
if (targetText.startsWith('"')) {
|
|
371
|
+
if (targetText.length < 2 || !targetText.endsWith('"'))
|
|
372
|
+
this.fatal('E0004', 'unterminated string literal', target.span);
|
|
373
|
+
targetText = targetText.slice(1, -1);
|
|
374
|
+
if (!targetText)
|
|
375
|
+
this.fatal('E0112', 'empty import path', target.span);
|
|
376
|
+
}
|
|
377
|
+
else if (targetText !== 'package' && !/^[A-Za-z_][A-Za-z_0-9]*$/.test(targetText)) {
|
|
378
|
+
this.fatal('E0112', 'expected dependency alias, package, or quoted module path', target.span);
|
|
379
|
+
}
|
|
380
|
+
if (targetText !== 'package' && !targetText.includes('/'))
|
|
381
|
+
bindName(targetText, target.span, this.bag);
|
|
382
|
+
const path = [];
|
|
383
|
+
let items;
|
|
384
|
+
let pathEnd = target.span;
|
|
385
|
+
while (this.peek().text === '::') {
|
|
386
|
+
this.take('::');
|
|
387
|
+
if (this.peek().text === '{') {
|
|
388
|
+
this.take('{');
|
|
389
|
+
items = [];
|
|
390
|
+
while (this.peek().text !== '}') {
|
|
391
|
+
const name = this.ident();
|
|
392
|
+
bindName(name.text, name.span, this.bag);
|
|
393
|
+
let local = name;
|
|
394
|
+
if (this.peek().text === 'as') {
|
|
395
|
+
this.take('as');
|
|
396
|
+
local = this.ident();
|
|
397
|
+
bindName(local.text, local.span, this.bag);
|
|
398
|
+
}
|
|
399
|
+
items.push({ name: name.text, local: local.text, span: cover(name.span, local.span) });
|
|
400
|
+
if (this.peek().text !== ',')
|
|
401
|
+
break;
|
|
402
|
+
this.take(',');
|
|
403
|
+
}
|
|
404
|
+
this.take('}');
|
|
405
|
+
if (!items.length)
|
|
406
|
+
this.fatal('E0112', 'empty import list', start.span);
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
const segment = this.ident();
|
|
410
|
+
path.push(segment.text);
|
|
411
|
+
pathEnd = segment.span;
|
|
412
|
+
}
|
|
413
|
+
if (!path.length)
|
|
414
|
+
this.fatal('E0112', 'import requires a module path', start.span);
|
|
415
|
+
let moduleAlias;
|
|
416
|
+
if (!items) {
|
|
417
|
+
if (this.peek().text !== 'as')
|
|
418
|
+
this.fatal('E0112', "import requires ::{names} or 'as alias'", this.peek().span);
|
|
419
|
+
this.take('as');
|
|
420
|
+
const alias = this.ident();
|
|
421
|
+
bindName(alias.text, alias.span, this.bag);
|
|
422
|
+
moduleAlias = { name: alias.text, span: alias.span };
|
|
423
|
+
}
|
|
424
|
+
const end = this.take(';');
|
|
425
|
+
return {
|
|
426
|
+
target: targetText, targetSpan: target.span, path, pathSpan: cover(target.span, pathEnd),
|
|
427
|
+
span: cover(start.span, end.span), ...(items ? { items } : {}), ...(moduleAlias ? { moduleAlias } : {}),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
parse(requireContract = true) {
|
|
431
|
+
const states = new Map();
|
|
432
|
+
const helpers = new Map();
|
|
433
|
+
const contracts = new Map();
|
|
434
|
+
const imports = [];
|
|
435
|
+
let edition;
|
|
436
|
+
let optimizerVersion;
|
|
437
|
+
while (this.peek().text !== '<eof>') {
|
|
438
|
+
let start = this.take();
|
|
439
|
+
if (start.text === 'pragma') {
|
|
440
|
+
if (imports.length || states.size || helpers.size || contracts.size)
|
|
441
|
+
this.fatal('E0135', 'pragma must precede imports and declarations', start.span);
|
|
442
|
+
const key = this.ident();
|
|
443
|
+
const raw = this.take();
|
|
444
|
+
if (!raw.text.startsWith('"') || raw.text.length < 2 || !raw.text.endsWith('"')) {
|
|
445
|
+
this.fatal('E0135', `pragma ${key.text} requires a quoted string value`, raw.span);
|
|
446
|
+
}
|
|
447
|
+
const value = raw.text.slice(1, -1);
|
|
448
|
+
this.take(';');
|
|
449
|
+
if (key.text === 'edition') {
|
|
450
|
+
if (edition !== undefined)
|
|
451
|
+
this.fatal('E0136', 'duplicate pragma edition', key.span);
|
|
452
|
+
if (!isSupportedCompilerEdition(value)) {
|
|
453
|
+
this.fatal('E0133', `unsupported compiler edition "${value}"; expected one of: ${SUPPORTED_COMPILER_EDITIONS.join(', ')}`, raw.span);
|
|
454
|
+
}
|
|
455
|
+
edition = value;
|
|
456
|
+
}
|
|
457
|
+
else if (key.text === 'optimizer') {
|
|
458
|
+
if (optimizerVersion !== undefined)
|
|
459
|
+
this.fatal('E0136', 'duplicate pragma optimizer', key.span);
|
|
460
|
+
if (!isSupportedOptimizerVersion(value)) {
|
|
461
|
+
this.fatal('E0134', `unsupported optimizerVersion "${value}"; expected one of: ${SUPPORTED_OPTIMIZER_VERSIONS.join(', ')}`, raw.span);
|
|
462
|
+
}
|
|
463
|
+
optimizerVersion = value;
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
this.fatal('E0135', `unsupported pragma ${key.text}; expected edition or optimizer`, key.span);
|
|
467
|
+
}
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (start.text === 'import') {
|
|
471
|
+
if (states.size || helpers.size || contracts.size)
|
|
472
|
+
this.fatal('E0113', 'import must precede declarations', start.span);
|
|
473
|
+
imports.push(this.importDecl(start));
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
let visibility = 'private';
|
|
477
|
+
if (start.text === 'export' || start.text === 'package' || (this.languageVersion === 1 && start.text === 'private')) {
|
|
478
|
+
visibility = start.text;
|
|
479
|
+
if (!['state', 'function', 'contract', 'token'].includes(this.peek().text))
|
|
480
|
+
this.fatal('E0114', `${start.text} applies only to a state, function, contract or token`, start.span);
|
|
481
|
+
const modifier = start;
|
|
482
|
+
start = this.take();
|
|
483
|
+
start = { text: start.text, span: cover(modifier.span, start.span) };
|
|
484
|
+
}
|
|
485
|
+
if (start.text === 'state') {
|
|
486
|
+
let mode = 'separate';
|
|
487
|
+
if ((this.peek().text === 'separate' || this.peek().text === 'inline') && this.tokens[this.i + 1]?.text !== '{') {
|
|
488
|
+
mode = this.take().text;
|
|
489
|
+
}
|
|
490
|
+
const name = this.ident();
|
|
491
|
+
bindName(name.text, name.span, this.bag);
|
|
492
|
+
if (states.has(name.text))
|
|
493
|
+
this.fatal('E0209', `duplicate state ${name.text}`, name.span);
|
|
494
|
+
this.take('{');
|
|
495
|
+
const fields = [];
|
|
496
|
+
while (this.peek().text !== '}') {
|
|
497
|
+
const fname = this.ident();
|
|
498
|
+
bindName(fname.text, fname.span, this.bag);
|
|
499
|
+
this.take(':');
|
|
500
|
+
const ftype = this.type();
|
|
501
|
+
if (fields.some(f => f.name === fname.text))
|
|
502
|
+
this.fatal('E0201', `duplicate state field ${fname.text}`, fname.span);
|
|
503
|
+
fields.push({ name: fname.text, type: ftype, span: fname.span });
|
|
504
|
+
if (this.peek().text === ',')
|
|
505
|
+
this.take(',');
|
|
506
|
+
else
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
const end = this.take('}');
|
|
510
|
+
if (!fields.length || fields.length > 64)
|
|
511
|
+
this.fatal('E0107', 'state declaration must have 1..64 fields', name.span);
|
|
512
|
+
states.set(name.text, { name: name.text, mode, fields, span: cover(start.span, end.span), visibility });
|
|
513
|
+
}
|
|
514
|
+
else if (start.text === 'function') {
|
|
515
|
+
const name = this.ident();
|
|
516
|
+
this.take('(');
|
|
517
|
+
const params = this.params(')');
|
|
518
|
+
this.take(':');
|
|
519
|
+
const result = this.type();
|
|
520
|
+
const body = this.helperBody();
|
|
521
|
+
bindName(name.text, name.span, this.bag);
|
|
522
|
+
if (helpers.has(name.text))
|
|
523
|
+
this.fatal('E0202', `duplicate helper ${name.text}`, name.span);
|
|
524
|
+
helpers.set(name.text, { name: name.text, params, result, body, span: cover(start.span, this.tokens[this.i - 1].span), visibility });
|
|
525
|
+
}
|
|
526
|
+
else if (start.text === 'contract' || start.text === 'token') {
|
|
527
|
+
const name = this.ident();
|
|
528
|
+
bindName(name.text, name.span, this.bag);
|
|
529
|
+
const params = this.peek().text === '<' ? (this.take('<'), this.params('>')) : [];
|
|
530
|
+
this.take('{');
|
|
531
|
+
const methods = [];
|
|
532
|
+
while (this.peek().text !== '}') {
|
|
533
|
+
const marker = this.take();
|
|
534
|
+
const invocation = start.text === 'token' ? 'mint' : 'spend';
|
|
535
|
+
if (this.languageVersion === 1) {
|
|
536
|
+
const expected = invocation === 'mint' ? 'mint' : 'entry';
|
|
537
|
+
if (marker.text !== expected)
|
|
538
|
+
this.fatal('E0130', `LC-1 ${start.text} methods require ${expected}; move utilities to a free helper`, marker.span);
|
|
539
|
+
}
|
|
540
|
+
else if (marker.text !== 'function')
|
|
541
|
+
this.fatal('E0101', 'expected function (entry/mint requires languageVersion 1)', marker.span);
|
|
542
|
+
const method = this.ident();
|
|
543
|
+
bindName(method.text, method.span, this.bag);
|
|
544
|
+
this.take('(');
|
|
545
|
+
const args = this.params(')');
|
|
546
|
+
const body = this.statements();
|
|
547
|
+
if (!body.length || methods.some(x => x.name.toLowerCase() === method.text.toLowerCase()))
|
|
548
|
+
this.fatal('E0203', `empty or duplicate method ${method.text}`, method.span);
|
|
549
|
+
if (start.text === 'token' && (args[0]?.type !== 'Amount'))
|
|
550
|
+
this.fatal('E0118', `token method ${method.text} first parameter must be Amount`, args[0]?.span ?? method.span);
|
|
551
|
+
methods.push({ ...(this.languageVersion === 1 ? { invocation } : {}), name: method.text, params: args, body, span: cover(method.span, this.tokens[this.i - 1].span) });
|
|
552
|
+
if (methods.length > MAX_METHODS)
|
|
553
|
+
this.fatal('E0117', 'more than 16 methods', method.span);
|
|
554
|
+
}
|
|
555
|
+
const end = this.take('}');
|
|
556
|
+
if (!methods.length || contracts.has(name.text))
|
|
557
|
+
this.fatal('E0204', `empty or duplicate contract ${name.text}`, name.span);
|
|
558
|
+
contracts.set(name.text, { name: name.text, kind: start.text, params, methods, span: cover(start.span, end.span), visibility });
|
|
559
|
+
}
|
|
560
|
+
else
|
|
561
|
+
this.fatal('E0101', 'expected contract or helper', start.span);
|
|
562
|
+
}
|
|
563
|
+
if (requireContract && !contracts.size)
|
|
564
|
+
this.fatal('E0116', 'source has no contract', this.peek().span);
|
|
565
|
+
return {
|
|
566
|
+
...(this.languageVersion === 1 ? { languageVersion: 1 } : {}),
|
|
567
|
+
...(edition !== undefined ? { edition } : {}),
|
|
568
|
+
...(optimizerVersion !== undefined ? { optimizerVersion } : {}),
|
|
569
|
+
...(states.size > 0 ? { states } : {}),
|
|
570
|
+
helpers,
|
|
571
|
+
contracts,
|
|
572
|
+
imports,
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
}
|