Faster generated decoders

This commit is contained in:
Tony Garnock-Jones 2021-03-14 21:10:47 +01:00
parent e078a71b30
commit 5afb1469f3
3 changed files with 680 additions and 594 deletions

View File

@ -44,7 +44,10 @@ export interface TypedDecoder<T> {
closeCompound(): boolean; closeCompound(): boolean;
} }
export function checkIs<T, E extends Value<T>>(actual: Value<T>, expected: E): E | undefined { export function asLiteral<T, E extends Exclude<Value<T>, Annotated<T>>>(
actual: Value<T>,
expected: E): E | undefined
{
return is(actual, expected) ? expected : void 0; return is(actual, expected) ? expected : void 0;
} }
@ -78,16 +81,7 @@ export class Decoder<T = never> implements TypedDecoder<T> {
nextbyte(): number { nextbyte(): number {
if (this.atEnd()) throw new ShortPacket("Short packet"); if (this.atEnd()) throw new ShortPacket("Short packet");
return this.packet[this.advance()]; return this.packet[this.index++];
}
peekbyte(): number {
if (this.atEnd()) throw new ShortPacket("Short packet");
return this.packet[this.index];
}
advance(): number {
return this.index++;
} }
nextbytes(n: number): DataView { nextbytes(n: number): DataView {
@ -106,7 +100,7 @@ export class Decoder<T = never> implements TypedDecoder<T> {
} }
peekend(): boolean { peekend(): boolean {
return (this.peekbyte() === Tag.End) && (this.advance(), true); return (this.nextbyte() === Tag.End) || (this.index--, false);
} }
nextvalues(): Value<T>[] { nextvalues(): Value<T>[] {
@ -259,55 +253,42 @@ export class Decoder<T = never> implements TypedDecoder<T> {
} }
skipAnnotations(): void { skipAnnotations(): void {
while (this.peekbyte() === Tag.Annotation) { if (!this.atEnd() && this.packet[this.index] === Tag.Annotation) {
this.advance(); this.index++;
this.skip(); this.skip();
} }
} }
nextBoolean(): boolean | undefined { nextBoolean(): boolean | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.False: this.advance(); return false; case Tag.False: return false;
case Tag.True: this.advance(); return true; case Tag.True: return true;
default: return void 0; default: return void 0;
} }
} }
nextFloat(): SingleFloat | undefined { nextFloat(): SingleFloat | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.Float: case Tag.Float: return new SingleFloat(this.nextbytes(4).getFloat32(0, false));
this.advance(); default: return void 0;
return new SingleFloat(this.nextbytes(4).getFloat32(0, false));
default:
return void 0;
} }
} }
nextDouble(): DoubleFloat | undefined { nextDouble(): DoubleFloat | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.Double: case Tag.Double: return new DoubleFloat(this.nextbytes(8).getFloat64(0, false));
this.advance(); default: return void 0;
return new DoubleFloat(this.nextbytes(8).getFloat64(0, false));
default:
return void 0;
} }
} }
nextPointer(): T | undefined { nextPointer(): T | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.Pointer: { case Tag.Pointer: return this.decodePointer(this);
this.advance(); default: return void 0;
const M = this.mark();
const v = this.decodePointer(this);
if (v === void 0) this.restoreMark(M);
return v;
}
default:
return void 0;
} }
} }
@ -315,43 +296,31 @@ export class Decoder<T = never> implements TypedDecoder<T> {
this.skipAnnotations(); this.skipAnnotations();
const b = this.nextbyte(); const b = this.nextbyte();
switch (b) { switch (b) {
case Tag.SignedInteger: case Tag.SignedInteger: return this.nextint(this.varint());
return this.nextint(this.varint()); default: return this.nextSmallOrMediumInteger(b);
default: {
const v = this.nextSmallOrMediumInteger(b);
if (v === void 0) this.index--; // ugh
return v;
}
} }
} }
nextString(): string | undefined { nextString(): string | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.String: case Tag.String: return Bytes.from(this.nextbytes(this.varint())).fromUtf8();
this.advance(); default: return void 0;
return Bytes.from(this.nextbytes(this.varint())).fromUtf8();
default:
return void 0;
} }
} }
nextByteString(): Bytes | undefined { nextByteString(): Bytes | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.ByteString: case Tag.ByteString: return Bytes.from(this.nextbytes(this.varint()));
this.advance(); default: return void 0;
return Bytes.from(this.nextbytes(this.varint()));
default:
return void 0;
} }
} }
nextSymbol(): symbol | undefined { nextSymbol(): symbol | undefined {
this.skipAnnotations(); this.skipAnnotations();
switch (this.peekbyte()) { switch (this.nextbyte()) {
case Tag.Symbol: case Tag.Symbol:
this.advance();
return Symbol.for(Bytes.from(this.nextbytes(this.varint())).fromUtf8()); return Symbol.for(Bytes.from(this.nextbytes(this.varint())).fromUtf8());
default: default:
return void 0; return void 0;
@ -360,22 +329,22 @@ export class Decoder<T = never> implements TypedDecoder<T> {
openRecord(): boolean { openRecord(): boolean {
this.skipAnnotations(); this.skipAnnotations();
return (this.peekbyte() === Tag.Record) && (this.advance(), true); return (this.nextbyte() === Tag.Record) || (this.index--, false);
} }
openSequence(): boolean { openSequence(): boolean {
this.skipAnnotations(); this.skipAnnotations();
return (this.peekbyte() === Tag.Sequence) && (this.advance(), true); return (this.nextbyte() === Tag.Sequence) || (this.index--, false);
} }
openSet(): boolean { openSet(): boolean {
this.skipAnnotations(); this.skipAnnotations();
return (this.peekbyte() === Tag.Set) && (this.advance(), true); return (this.nextbyte() === Tag.Set) || (this.index--, false);
} }
openDictionary(): boolean { openDictionary(): boolean {
this.skipAnnotations(); this.skipAnnotations();
return (this.peekbyte() === Tag.Dictionary) && (this.advance(), true); return (this.nextbyte() === Tag.Dictionary) || (this.index--, false);
} }
closeCompound(): boolean { closeCompound(): boolean {

View File

@ -20,10 +20,12 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
const functions: Array<Item> = []; const functions: Array<Item> = [];
const imports = new KeyedSet<[string, string]>(); const imports = new KeyedSet<[string, string]>();
let temps: Array<string> = []; let temps: Array<string> = [];
let body: Array<Item> = [];
let tempCounter = 0;
const pointerName = Schema._.details(schema).get(M.$pointer); const pointerName = Schema._.details(schema).get(M.$pointer);
function gentemp(): string { function gentemp(): string {
const varname = '_tmp' + temps.length; const varname = '_tmp' + tempCounter++;
temps.push(varname); temps.push(varname);
return varname; return varname;
} }
@ -40,113 +42,245 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
return varname; return varname;
} }
function decoderFor(p: Pattern, recordFields = false): Item { function emit(item: Item): void {
body.push(item);
}
function collectBody(f: () => void): Item {
const oldTemps = temps;
const oldBody = body;
temps = []
body = [];
f();
const ts = temps;
const result = body;
temps = oldTemps;
body = oldBody;
return block(
... ts.length > 0 ? [seq('let ', commas(... ts), ': any')] : [],
... result);
}
function accumulateCompound(p: Pattern,
kFail: () => Item[],
kAcc: (temp: string) => Item[]): Item
{
const t = gentemp();
return seq(`while (!d.closeCompound()) `, collectBody(() => {
emit(seq(`${t} = void 0`));
decoderFor(p, t);
emit(seq(`if (${t} === void 0) `, block(
... kFail(),
seq(`break`))));
kAcc(t).forEach(emit);
}));
}
function derefPattern(p: Pattern): Pattern {
if (p.label === M.$ref) {
return lookup(refPosition(p), p, env,
(p) => p,
(p) => p,
(_mod, _modPath, pp) => pp ?? p);
} else {
return p;
}
}
function decoderForTuple(ps: Pattern[],
dest: string,
recordFields: boolean,
variablePattern: Pattern | undefined): void {
const temps = ps.map(gentemp);
function loop(i: number) {
if (i < ps.length) {
decoderFor(ps[i], temps[i]);
emit(seq(`if (${temps[i]} !== void 0) `,
collectBody(() => loop(i + 1))));
} else {
if (variablePattern === void 0) {
emit(seq(`if (d.closeCompound()) ${dest} = `, brackets(... temps)));
} else {
emit(block(
seq(`let vN: Array<`, typeFor(variablePattern),
`> | undefined = `, brackets(... temps)),
accumulateCompound(variablePattern,
() => [`vN = void 0`],
(t) => [`vN.push(${t})`]),
seq(`${dest} = vN`)));
}
}
}
if (recordFields) {
loop(0);
} else {
emit(seq(`if (d.openSequence()) `, collectBody(() => loop(0))));
}
}
function decoderFor(p: Pattern, dest: string, recordFields = false): void {
switch (p.label) { switch (p.label) {
case M.$atom: case M.$atom:
switch (p[0]) { switch (p[0]) {
case M.$Boolean: return `d.nextBoolean()`; case M.$Boolean: emit(`${dest} = d.nextBoolean()`); break;
case M.$Float: return `d.nextFloat()`; case M.$Float: emit(`${dest} = d.nextFloat()`); break;
case M.$Double: return `d.nextDouble()`; case M.$Double: emit(`${dest} = d.nextDouble()`); break;
case M.$SignedInteger: return `d.nextSignedInteger()`; case M.$SignedInteger: emit(`${dest} = d.nextSignedInteger()`); break;
case M.$String: return `d.nextString()`; case M.$String: emit(`${dest} = d.nextString()`); break;
case M.$ByteString: return `d.nextByteString()`; case M.$ByteString: emit(`${dest} = d.nextByteString()`); break;
case M.$Symbol: return `d.nextSymbol()`; case M.$Symbol: emit(`${dest} = d.nextSymbol()`); break;
} }
case M.$lit: break;
case M.$lit: {
let n: string;
switch (typeof p[0]) { switch (typeof p[0]) {
case 'boolean': return `_.checkIs(d.nextBoolean(), ${literal(p[0])})`; case 'boolean': n = `d.nextBoolean()`; break;
case 'string': return `_.checkIs(d.nextString(), ${literal(p[0])})`; case 'string': n = `d.nextString()`; break;
case 'number': return `_.checkIs(d.nextSignedInteger(), ${literal(p[0])})`; case 'number': n = `d.nextSignedInteger()`; break;
case 'symbol': return `_.checkIs(d.nextSymbol(), ${literal(p[0])})`; case 'symbol': n = `d.nextSymbol()`; break;
default: return `_.checkIs(d.next(), ${literal(p[0])})`; default: n = `d.next()`; break;
} }
emit(`${dest} = _.asLiteral(${n}, ${literal(p[0])})`);
break;
}
case M.$ref: case M.$ref:
return lookup(refPosition(p), p, env, lookup(refPosition(p), p, env,
(_p) => `decode${p[1].description!}(d)`, (_p) => emit(`${dest} = decode${p[1].description!}(d)`),
(p) => decoderFor(p), (p) => decoderFor(p, dest),
(mod, modPath,_p) => { (mod, modPath,_p) => {
imports.add([mod, modPath]); imports.add([mod, modPath]);
return `${mod}.decode${p[1].description!}(d)`; emit(`${dest} = ${mod}.decode${p[1].description!}(d)`);
}); });
case M.$or: break;
return opseq('void 0', ' ?? ', ... p[0].map(pp => decoderFor(pp))); case M.$or: {
case M.$and: const alts = p[0];
switch (p[0].length) { const recs = alts.map(derefPattern);
case 0: return `d.next()`; if (recs.length > 1 && recs.every(pp => pp.label === M.$rec)) {
case 1: return decoderFor(p[0][0]); // Hoist the record check up.
default: { // This is pretty hacky. If we lift the level of
const tmp = gentemp(); // discourse a little, we can do this
const [pp0, ... ppN] = p[0]; // automatically and generically...
const otherChecks = emit(seq(`if (d.openRecord()) `, collectBody(() => {
opseq('false', ' && ', ... ppN.map(pp => predicateFor(tmp, pp))); const label = gentemp();
return seq(`((${tmp} = `, decoderFor(pp0), `) != void 0) && `, emit(seq(`${label} = d.next()`));
otherChecks, ` ? ${tmp} : void 0`); const mark = gentemp();
emit(seq(`${mark} = d.mark()`));
function loop(i: number) {
const alt = recs[i];
if (alt.label !== M.$rec) return; // avoid a cast
emit(seq(`if (`, predicateFor(label, alt[0]), `) `, collectBody(() => {
const fs = gentemp();
decoderFor(alt[1], fs, true);
emit(seq(`if (${fs} !== void 0) ${dest} = _.Record`,
anglebrackets(typeFor(alt[0]), typeFor(alt[1])),
parens(seq(label, ` as any`),
seq(fs, ` as any`))));
})));
if (i < recs.length - 1) {
emit(seq(`if (${dest} === void 0) `, collectBody(() => {
emit(`d.restoreMark(${mark})`);
loop(i + 1);
})));
}
}
loop(0);
})));
} else {
switch (alts.length) {
case 0: break; // assume dest is already void 0
case 1: decoderFor(alts[0], dest); break;
default: {
const mark = gentemp();
emit(`${mark} = d.mark()`);
function loop(i: number) {
decoderFor(alts[i], dest);
if (i < alts.length - 1) {
emit(seq(`if (${dest} === void 0) `, collectBody(() => {
emit(`d.restoreMark(${mark})`);
loop(i + 1);
})));
}
}
loop(0);
break;
}
} }
} }
break;
}
case M.$and:
switch (p[0].length) {
case 0: emit(`${dest} = d.next()`); break;
case 1: decoderFor(p[0][0], dest); break;
default: {
const [pp0, ... ppN] = p[0];
decoderFor(pp0, dest);
const otherChecks =
opseq('true', ' && ', ... ppN.map(pp => predicateFor(dest, pp)));
emit(seq(`if (!`, otherChecks, `) ${dest} = void 0`));
break;
}
}
break;
case M.$pointer: case M.$pointer:
return `_decodePtr(d)`; emit(`${dest} = _decodePtr(d)`);
break;
case M.$rec: case M.$rec:
return fnblock( // assume dest is already void 0
seq(`const M = d.mark()`), emit(seq(`if (d.openRecord()) `, collectBody(() => {
seq(`if (!d.openRecord()) return void 0`), const label = gentemp();
seq(`const L = `, decoderFor(p[0])), decoderFor(p[0], label);
seq(`if (L === void 0) { d.restoreMark(M); return void 0; }`), emit(seq(`if (${label} !== void 0) `,
seq(`const Fs = (`, decoderFor(p[1], true), `) as any`), collectBody(() => {
seq(`if (Fs === void 0) { d.restoreMark(M); return void 0; }`), const fs = gentemp();
seq(`return _.Record`, decoderFor(p[1], fs, true);
anglebrackets(typeFor(p[0]), typeFor(p[1])), emit(seq(
parens(`L`, `Fs`))); `if (${fs} !== void 0) ${dest} = _.Record`,
anglebrackets(typeFor(p[0]), typeFor(p[1])),
parens(seq(label, ` as any`),
seq(fs, ` as any`))));
})));
})));
break;
case M.$tuple: case M.$tuple:
return fnblock( // assume dest is already void 0
seq(`const M = d.mark()`), decoderForTuple(p[0].map(unname), dest, recordFields, void 0);
... recordFields ? [] : [seq(`if (!d.openSequence()) return void 0`)], break;
... p[0].map((pp, i) =>
seq(`const v${i} = `, decoderFor(unname(pp)), `; `,
`if (v${i} === void 0) { d.restoreMark(M); return void 0; }`)),
seq(`if (!d.closeCompound()) { d.restoreMark(M); return void 0; }`),
seq(`return [${p[0].map((_pp, i) => `v${i}`).join(', ')}] as `, typeFor(p)));
case M.$tuple_STAR_: case M.$tuple_STAR_:
return fnblock( // assume dest is already void 0
seq(`const M = d.mark()`), decoderForTuple(p[0].map(unname), dest, recordFields, unname(p[1]));
... recordFields ? [] : [seq(`if (!d.openSequence()) return void 0`)], break;
... p[0].map((pp, i) =>
seq(`const v${i} = `, decoderFor(unname(pp)), `; `,
`if (v${i} === void 0) { d.restoreMark(M); return void 0; }`)),
seq(`const vN: Array<`, typeFor(unname(p[1])), `> = []`),
seq(`let tmp: undefined | `, typeFor(unname(p[1]))),
seq(`while ((tmp = `, decoderFor(unname(p[1])), `) !== void 0) vN.push(tmp)`),
seq(`if (!d.closeCompound()) { d.restoreMark(M); return void 0; }`),
(p[0].length === 0
? seq(`return vN`)
: seq(`return [${p[0].map((_pp, i) => `v${i}`).join(', ')}, ... vN] as `,
typeFor(p))));
case M.$setof: case M.$setof:
return fnblock( // assume dest is already void 0
seq(`const M = d.mark()`), emit(seq(`if (d.openSet()) `, collectBody(() => {
seq(`if (!d.openSet()) return void 0`), emit(seq(`let r: `, typeFor(p), ` | undefined = new _.KeyedSet()`));
seq(`const r: `, typeFor(p), ` = new _.KeyedSet()`), emit(accumulateCompound(p[0],
seq(`let tmp: undefined | `, typeFor(p[0])), () => [`r = void 0`],
seq(`while ((tmp = `, decoderFor(p[0]), `) !== void 0) r.add(tmp)`), (t) => [`r.add(${t})`]));
seq(`if (!d.closeCompound()) { d.restoreMark(M); return void 0; }`), emit(`${dest} = r`);
seq(`return r`)); })));
break;
case M.$dictof: case M.$dictof:
return fnblock( // assume dest is already void 0
seq(`const M = d.mark()`), emit(seq(`if (d.openDictionary()) `, collectBody(() => {
seq(`if (!d.openDictionary()) return void 0`), emit(seq(`let r: `, typeFor(p), ` | undefined = new _.KeyedDictionary()`));
seq(`const r: `, typeFor(p), ` = new _.KeyedDictionary()`), emit(seq(`while (!d.closeCompound()) `, collectBody(() => {
seq(`let K: undefined | `, typeFor(p[0])), emit(seq(`let K: undefined | `, typeFor(p[0]), ` = void 0`));
seq(`while ((K = `, decoderFor(p[0]), `) !== void 0) `, block( decoderFor(p[0], 'K');
seq(`const V = `, decoderFor(p[1])), emit(seq(`if (K === void 0) { r = void 0; break; }`));
seq(`if (V === void 0) { d.restoreMark(M); return void 0; }`), emit(seq(`let V: undefined | `, typeFor(p[1]), ` = void 0`));
seq(`r.set(K, V)`))), decoderFor(p[1], 'V');
seq(`if (!d.closeCompound()) { d.restoreMark(M); return void 0; }`), emit(seq(`if (V === void 0) { r = void 0; break; }`));
seq(`return r`)); emit(seq(`r.set(K, V)`));
})));
emit(seq(`${dest} = r`));
})));
break;
case M.$dict: case M.$dict:
return fnblock( emit(seq(`${dest} = d.next()`));
seq(`const M = d.mark()`), emit(seq(
seq(`const r = d.next()`), `if (${dest} !== void 0 && !(`, predicateFor(dest, p), `)) ${dest} = void 0`));
seq(`if (!(`, predicateFor('r', p), `)) { d.restoreMark(M); return void 0; }`), break;
seq(`return r`));
default: default:
((_p: never) => {})(p); ((_p: never) => {})(p);
throw new Error("Unreachable"); throw new Error("Unreachable");
@ -223,7 +357,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
case M.$Symbol: return `typeof ${v} === 'symbol'`; case M.$Symbol: return `typeof ${v} === 'symbol'`;
} }
case M.$lit: case M.$lit:
return `_.is(${v}, ${literal(p[0])})`; return `_.asLiteral(${v}, ${literal(p[0])}) !== void 0`;
case M.$ref: case M.$ref:
return lookup(refPosition(p), p, env, return lookup(refPosition(p), p, env,
(_p) => `is${Ref._.name(p).description!}(${v})`, (_p) => `is${Ref._.name(p).description!}(${v})`,
@ -232,8 +366,22 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
imports.add([mod, modPath]); imports.add([mod, modPath]);
return `${mod}.is${Ref._.name(p).description!}(${v})`; return `${mod}.is${Ref._.name(p).description!}(${v})`;
}); });
case M.$or: case M.$or: {
return opseq('false', ' || ', ... p[0].map(pp => predicateFor(v, pp))); const alts = p[0];
const recs = alts.map(derefPattern);
if (recs.length > 1 && recs.every(pp => pp.label === M.$rec)) {
return seq(
`_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(${v}) && `,
parens(opseq('false', ' || ',
... recs.map(r =>
(r.label !== M.$rec) ? '' : parens(seq(
predicateFor(`${v}.label`, r[0]),
' && ',
predicateFor(v, r[1], true)))))));
} else {
return opseq('false', ' || ', ... p[0].map(pp => predicateFor(v, pp)));
}
}
case M.$and: case M.$and:
return opseq('true', ' && ', ... p[0].map(pp => predicateFor(v, pp))); return opseq('true', ' && ', ... p[0].map(pp => predicateFor(v, pp)));
case M.$pointer: case M.$pointer:
@ -254,7 +402,7 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
... (recordOkAsTuple ? [] ... (recordOkAsTuple ? []
: [`_.Array.isArray(${v})`, `!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(${v})`]), : [`_.Array.isArray(${v})`, `!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(${v})`]),
`(${v}.length >= ${p[0].length})`, `(${v}.length >= ${p[0].length})`,
seq(`${v}.slice(${p[0].length})`, seq(p[0].length > 0 ? `${v}.slice(${p[0].length})` : v,
`.every(v => `, `.every(v => `,
parens(predicateFor('v', unname(p[1]))), parens(predicateFor('v', unname(p[1]))),
`)`), `)`),
@ -332,14 +480,14 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
' else ', ' else ',
block(`return v`))))); block(`return v`)))));
temps = [];
const decoder = decoderFor(pattern);
functions.push( functions.push(
seq(`export function decode${name.description!}`, seq(`export function decode${name.description!}`,
`(d: _.TypedDecoder<_ptr>): ${name.description!} | undefined `, `(d: _.TypedDecoder<_ptr>): ${name.description!} | undefined `,
block( collectBody(() => {
... temps.length > 0 ? [seq('let ', commas(... temps), ': any')] : [], emit(seq(`let result`));
seq(`return `, decoder)))); decoderFor(pattern, 'result');
emit(seq(`return result`));
})));
} }
types.push(seq('export type _ptr = ', pointerName === false ? 'never' : typeFor(pointerName), `;`)); types.push(seq('export type _ptr = ', pointerName === false ? 'never' : typeFor(pointerName), `;`));
@ -347,7 +495,12 @@ export function compile(env: Environment, schema: Schema, options: CompilerOptio
functions.push(seq(`export const _decodePtr = `, functions.push(seq(`export const _decodePtr = `,
(pointerName === false (pointerName === false
? '() => { throw new _.DecodeError("Pointers forbidden"); }' ? '() => { throw new _.DecodeError("Pointers forbidden"); }'
: seq(`(d: _.TypedDecoder<_ptr>) => `, decoderFor(pointerName))), : seq(`(d: _.TypedDecoder<_ptr>) => `,
collectBody(() => {
emit(seq(`let result`));
decoderFor(pointerName, 'result');
emit(seq(`return result`));
}))),
`;`)); `;`));
const f = new Formatter(); const f = new Formatter();

View File

@ -109,7 +109,7 @@ export function isSchema(v: any): v is Schema {
let _tmp0, _tmp1, _tmp2: any; let _tmp0, _tmp1, _tmp2: any;
return ( return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $schema) && _.asLiteral(v.label, $schema) !== void 0 &&
( (
(v.length === 1) && (v.length === 1) &&
( (
@ -138,193 +138,160 @@ export function asSchema(v: any): Schema {
} }
export function decodeSchema(d: _.TypedDecoder<_ptr>): Schema | undefined { export function decodeSchema(d: _.TypedDecoder<_ptr>): Schema | undefined {
let _tmp0, _tmp1, _tmp2: any; let result;
return ((() => { if (d.openRecord()) {
const M = d.mark(); let _tmp3: any;
if (!d.openRecord()) return void 0; _tmp3 = _.asLiteral(d.nextSymbol(), $schema);
const L = _.checkIs(d.nextSymbol(), $schema); if (_tmp3 !== void 0) {
if (L === void 0) { d.restoreMark(M); return void 0; }; let _tmp4, _tmp5, _tmp6, _tmp7, _tmp8: any;
const Fs = (((() => { _tmp5 = d.next();
const M = d.mark(); if (_tmp5 !== void 0 && !((
const v0 = ((() => { _.Dictionary.isDictionary<_val, _ptr>(_tmp5) &&
const M = d.mark(); ((_tmp6 = _tmp5.get($version)) !== void 0 && isVersion(_tmp6)) &&
const r = d.next(); ((_tmp7 = _tmp5.get($pointer)) !== void 0 && isPointerName(_tmp7)) &&
if (!(( (
_.Dictionary.isDictionary<_val, _ptr>(r) && (_tmp8 = _tmp5.get($definitions)) !== void 0 && (
((_tmp0 = r.get($version)) !== void 0 && isVersion(_tmp0)) && _.Dictionary.isDictionary<_val, _ptr>(_tmp8) &&
((_tmp1 = r.get($pointer)) !== void 0 && isPointerName(_tmp1)) && ((() => {
( for (const e of _tmp8) {
(_tmp2 = r.get($definitions)) !== void 0 && ( if (!(typeof e[0] === 'symbol')) return false;
_.Dictionary.isDictionary<_val, _ptr>(_tmp2) && if (!(isPattern(e[1]))) return false;
((() => { };
for (const e of _tmp2) { return true;
if (!(typeof e[0] === 'symbol')) return false; })())
if (!(isPattern(e[1]))) return false;
};
return true;
})())
)
) )
))) { d.restoreMark(M); return void 0; };
return r;
})()); if (v0 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0] as [
(
{
get(k: typeof $version): Version;
get(k: typeof $pointer): PointerName;
get(k: typeof $definitions): _.KeyedDictionary<symbol, Pattern, _ptr>;
has(k: typeof $version): true;
has(k: typeof $pointer): true;
has(k: typeof $definitions): true;
} & _.Dictionary<_val, _ptr>
) )
]; ))) _tmp5 = void 0;
})())) as any; if (_tmp5 !== void 0) {if (d.closeCompound()) _tmp4 = [_tmp5];};
if (Fs === void 0) { d.restoreMark(M); return void 0; }; if (_tmp4 !== void 0) result = _.Record<
return _.Record< (typeof $schema),
(typeof $schema), [
[ (
( {
{ get(k: typeof $version): Version;
get(k: typeof $version): Version; get(k: typeof $pointer): PointerName;
get(k: typeof $pointer): PointerName; get(k: typeof $definitions): _.KeyedDictionary<symbol, Pattern, _ptr>;
get(k: typeof $definitions): _.KeyedDictionary<symbol, Pattern, _ptr>; has(k: typeof $version): true;
has(k: typeof $version): true; has(k: typeof $pointer): true;
has(k: typeof $pointer): true; has(k: typeof $definitions): true;
has(k: typeof $definitions): true; } & _.Dictionary<_val, _ptr>
} & _.Dictionary<_val, _ptr> )
) ]
] >(_tmp3 as any, _tmp4 as any);
>(L, Fs); };
})()); };
return result;
} }
export function isVersion(v: any): v is Version {return _.is(v, $1);} export function isVersion(v: any): v is Version {return _.asLiteral(v, $1) !== void 0;}
export function asVersion(v: any): Version { export function asVersion(v: any): Version {
if (!isVersion(v)) {throw new TypeError(`Invalid Version: ${_.stringify(v)}`);} else {return v;}; if (!isVersion(v)) {throw new TypeError(`Invalid Version: ${_.stringify(v)}`);} else {return v;};
} }
export function decodeVersion(d: _.TypedDecoder<_ptr>): Version | undefined {return _.checkIs(d.nextSignedInteger(), $1);} export function decodeVersion(d: _.TypedDecoder<_ptr>): Version | undefined {let result; result = _.asLiteral(d.nextSignedInteger(), $1); return result;}
export function isPointerName(v: any): v is PointerName {return (isRef(v) || _.is(v, __lit5));} export function isPointerName(v: any): v is PointerName {return (isRef(v) || _.asLiteral(v, __lit5) !== void 0);}
export function asPointerName(v: any): PointerName { export function asPointerName(v: any): PointerName {
if (!isPointerName(v)) {throw new TypeError(`Invalid PointerName: ${_.stringify(v)}`);} else {return v;}; if (!isPointerName(v)) {throw new TypeError(`Invalid PointerName: ${_.stringify(v)}`);} else {return v;};
} }
export function decodePointerName(d: _.TypedDecoder<_ptr>): PointerName | undefined {return (decodeRef(d) ?? _.checkIs(d.nextBoolean(), __lit5));} export function decodePointerName(d: _.TypedDecoder<_ptr>): PointerName | undefined {
let _tmp9: any;
let result;
_tmp9 = d.mark();
result = decodeRef(d);
if (result === void 0) {d.restoreMark(_tmp9); result = _.asLiteral(d.nextBoolean(), __lit5);};
return result;
}
export function isPattern(v: any): v is Pattern { export function isPattern(v: any): v is Pattern {
return ( return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && (
( (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $atom) &&
( (
(v.length === 1) && _.asLiteral(v.label, $atom) !== void 0 && (
( (v.length === 1) &&
_.is(v[0], $Boolean) || (
_.is(v[0], $Float) || _.asLiteral(v[0], $Boolean) !== void 0 ||
_.is(v[0], $Double) || _.asLiteral(v[0], $Float) !== void 0 ||
_.is(v[0], $SignedInteger) || _.asLiteral(v[0], $Double) !== void 0 ||
_.is(v[0], $String) || _.asLiteral(v[0], $SignedInteger) !== void 0 ||
_.is(v[0], $ByteString) || _.asLiteral(v[0], $String) !== void 0 ||
_.is(v[0], $Symbol) _.asLiteral(v[0], $ByteString) !== void 0 ||
_.asLiteral(v[0], $Symbol) !== void 0
)
) )
) ) ||
) || (_.asLiteral(v.label, $pointer) !== void 0 && ((v.length === 0))) ||
( (_.asLiteral(v.label, $lit) !== void 0 && ((v.length === 1) && true)) ||
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $pointer) &&
((v.length === 0))
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $lit) &&
((v.length === 1) && true)
) ||
isRef(v) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $or) &&
( (
(v.length === 1) && _.asLiteral(v.label, $ref) !== void 0 && ((v.length === 2) && isModuleRef(v[0]) && typeof v[1] === 'symbol')
( ) ||
_.Array.isArray(v[0]) && (
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && _.asLiteral(v.label, $or) !== void 0 && (
(v[0].length >= 0) && (v.length === 1) &&
v[0].slice(0).every(v => (isPattern(v))) (
_.Array.isArray(v[0]) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
(v[0].length >= 0) &&
v[0].every(v => (isPattern(v)))
)
) )
) ) ||
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $and) &&
( (
(v.length === 1) && _.asLiteral(v.label, $and) !== void 0 && (
( (v.length === 1) &&
_.Array.isArray(v[0]) && (
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && _.Array.isArray(v[0]) &&
(v[0].length >= 0) && !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
v[0].slice(0).every(v => (isPattern(v))) (v[0].length >= 0) &&
v[0].every(v => (isPattern(v)))
)
) )
) ) ||
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $rec) &&
((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $tuple) &&
( (
(v.length === 1) && _.asLiteral(v.label, $rec) !== void 0 && ((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
( ) ||
_.Array.isArray(v[0]) && (
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && _.asLiteral(v.label, $tuple) !== void 0 && (
(v[0].length >= 0) && (v.length === 1) &&
v[0].slice(0).every(v => (isNamedPattern(v))) (
_.Array.isArray(v[0]) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
(v[0].length >= 0) &&
v[0].every(v => (isNamedPattern(v)))
)
) )
) ) ||
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $tuple_STAR_) &&
( (
(v.length === 2) && _.asLiteral(v.label, $tuple_STAR_) !== void 0 && (
( (v.length === 2) &&
_.Array.isArray(v[0]) && (
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && _.Array.isArray(v[0]) &&
(v[0].length >= 0) && !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
v[0].slice(0).every(v => (isNamedPattern(v))) (v[0].length >= 0) &&
) && v[0].every(v => (isNamedPattern(v)))
isNamedPattern(v[1]) ) &&
) isNamedPattern(v[1])
) || )
( ) ||
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $setof) &&
((v.length === 1) && isPattern(v[0]))
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $dictof) &&
((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $dict) &&
( (
(v.length === 1) && _.asLiteral(v.label, $setof) !== void 0 && ((v.length === 1) && isPattern(v[0]))
( ) ||
_.Dictionary.isDictionary<_val, _ptr>(v[0]) && (
((() => { _.asLiteral(v.label, $dictof) !== void 0 && ((v.length === 2) && isPattern(v[0]) && isPattern(v[1]))
for (const e of v[0]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;}; ) ||
return true; (
})()) _.asLiteral(v.label, $dict) !== void 0 && (
(v.length === 1) &&
(
_.Dictionary.isDictionary<_val, _ptr>(v[0]) &&
((() => {
for (const e of v[0]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;};
return true;
})())
)
) )
) )
) )
@ -336,38 +303,38 @@ export function asPattern(v: any): Pattern {
} }
export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined { export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined {
return ( let result;
((() => { if (d.openRecord()) {
const M = d.mark(); let _tmp10, _tmp11: any;
if (!d.openRecord()) return void 0; _tmp10 = d.next();
const L = _.checkIs(d.nextSymbol(), $atom); _tmp11 = d.mark();
if (L === void 0) { d.restoreMark(M); return void 0; }; if (_.asLiteral(_tmp10, $atom) !== void 0) {
const Fs = (((() => { let _tmp12, _tmp13, _tmp14: any;
const M = d.mark(); _tmp14 = d.mark();
const v0 = ( _tmp13 = _.asLiteral(d.nextSymbol(), $Boolean);
_.checkIs(d.nextSymbol(), $Boolean) ?? if (_tmp13 === void 0) {
_.checkIs(d.nextSymbol(), $Float) ?? d.restoreMark(_tmp14);
_.checkIs(d.nextSymbol(), $Double) ?? _tmp13 = _.asLiteral(d.nextSymbol(), $Float);
_.checkIs(d.nextSymbol(), $SignedInteger) ?? if (_tmp13 === void 0) {
_.checkIs(d.nextSymbol(), $String) ?? d.restoreMark(_tmp14);
_.checkIs(d.nextSymbol(), $ByteString) ?? _tmp13 = _.asLiteral(d.nextSymbol(), $Double);
_.checkIs(d.nextSymbol(), $Symbol) if (_tmp13 === void 0) {
); if (v0 === void 0) { d.restoreMark(M); return void 0; }; d.restoreMark(_tmp14);
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; _tmp13 = _.asLiteral(d.nextSymbol(), $SignedInteger);
return [v0] as [ if (_tmp13 === void 0) {
( d.restoreMark(_tmp14);
(typeof $Boolean) | _tmp13 = _.asLiteral(d.nextSymbol(), $String);
(typeof $Float) | if (_tmp13 === void 0) {
(typeof $Double) | d.restoreMark(_tmp14);
(typeof $SignedInteger) | _tmp13 = _.asLiteral(d.nextSymbol(), $ByteString);
(typeof $String) | if (_tmp13 === void 0) {d.restoreMark(_tmp14); _tmp13 = _.asLiteral(d.nextSymbol(), $Symbol);};
(typeof $ByteString) | };
(typeof $Symbol) };
) };
]; };
})())) as any; };
if (Fs === void 0) { d.restoreMark(M); return void 0; }; if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp12 = [_tmp13];};
return _.Record< if (_tmp12 !== void 0) result = _.Record<
(typeof $atom), (typeof $atom),
[ [
( (
@ -380,203 +347,187 @@ export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined {
(typeof $Symbol) (typeof $Symbol)
) )
] ]
>(L, Fs); >(_tmp10 as any, _tmp12 as any);
})()) ?? };
((() => { if (result === void 0) {
const M = d.mark(); d.restoreMark(_tmp11);
if (!d.openRecord()) return void 0; if (_.asLiteral(_tmp10, $pointer) !== void 0) {
const L = _.checkIs(d.nextSymbol(), $pointer); let _tmp15: any;
if (L === void 0) { d.restoreMark(M); return void 0; }; if (d.closeCompound()) _tmp15 = [];
const Fs = (((() => { if (_tmp15 !== void 0) result = _.Record<(typeof $pointer), []>(_tmp10 as any, _tmp15 as any);
const M = d.mark(); };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; if (result === void 0) {
return [] as []; d.restoreMark(_tmp11);
})())) as any; if (_.asLiteral(_tmp10, $lit) !== void 0) {
if (Fs === void 0) { d.restoreMark(M); return void 0; }; let _tmp16, _tmp17: any;
return _.Record<(typeof $pointer), []>(L, Fs); _tmp17 = d.next();
})()) ?? if (_tmp17 !== void 0) {if (d.closeCompound()) _tmp16 = [_tmp17];};
((() => { if (_tmp16 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp10 as any, _tmp16 as any);
const M = d.mark(); };
if (!d.openRecord()) return void 0; if (result === void 0) {
const L = _.checkIs(d.nextSymbol(), $lit); d.restoreMark(_tmp11);
if (L === void 0) { d.restoreMark(M); return void 0; }; if (_.asLiteral(_tmp10, $ref) !== void 0) {
const Fs = (((() => { let _tmp18, _tmp19, _tmp20: any;
const M = d.mark(); _tmp19 = decodeModuleRef(d);
const v0 = d.next(); if (v0 === void 0) { d.restoreMark(M); return void 0; }; if (_tmp19 !== void 0) {
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; _tmp20 = d.nextSymbol();
return [v0] as [_val]; if (_tmp20 !== void 0) {if (d.closeCompound()) _tmp18 = [_tmp19, _tmp20];};
})())) as any; };
if (Fs === void 0) { d.restoreMark(M); return void 0; }; if (_tmp18 !== void 0) result = _.Record<(typeof $ref), [ModuleRef, symbol]>(_tmp10 as any, _tmp18 as any);
return _.Record<(typeof $lit), [_val]>(L, Fs);
})()) ??
decodeRef(d) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $or);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = ((() => {
const M = d.mark();
if (!d.openSequence()) return void 0;
const vN: Array<Pattern> = [];
let tmp: undefined | Pattern;
while ((tmp = decodePattern(d)) !== void 0) vN.push(tmp);
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return vN;
})()); if (v0 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0] as [Array<Pattern>];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $or), [Array<Pattern>]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $and);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = ((() => {
const M = d.mark();
if (!d.openSequence()) return void 0;
const vN: Array<Pattern> = [];
let tmp: undefined | Pattern;
while ((tmp = decodePattern(d)) !== void 0) vN.push(tmp);
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return vN;
})()); if (v0 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0] as [Array<Pattern>];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $and), [Array<Pattern>]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $rec);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = decodePattern(d); if (v0 === void 0) { d.restoreMark(M); return void 0; };
const v1 = decodePattern(d); if (v1 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0, v1] as [Pattern, Pattern];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $rec), [Pattern, Pattern]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $tuple);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = ((() => {
const M = d.mark();
if (!d.openSequence()) return void 0;
const vN: Array<NamedPattern> = [];
let tmp: undefined | NamedPattern;
while ((tmp = decodeNamedPattern(d)) !== void 0) vN.push(tmp);
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return vN;
})()); if (v0 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0] as [Array<NamedPattern>];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $tuple), [Array<NamedPattern>]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $tuple_STAR_);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = ((() => {
const M = d.mark();
if (!d.openSequence()) return void 0;
const vN: Array<NamedPattern> = [];
let tmp: undefined | NamedPattern;
while ((tmp = decodeNamedPattern(d)) !== void 0) vN.push(tmp);
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return vN;
})()); if (v0 === void 0) { d.restoreMark(M); return void 0; };
const v1 = decodeNamedPattern(d); if (v1 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0, v1] as [Array<NamedPattern>, NamedPattern];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $setof);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = decodePattern(d); if (v0 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0] as [Pattern];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $setof), [Pattern]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $dictof);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = decodePattern(d); if (v0 === void 0) { d.restoreMark(M); return void 0; };
const v1 = decodePattern(d); if (v1 === void 0) { d.restoreMark(M); return void 0; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; };
return [v0, v1] as [Pattern, Pattern];
})())) as any;
if (Fs === void 0) { d.restoreMark(M); return void 0; };
return _.Record<(typeof $dictof), [Pattern, Pattern]>(L, Fs);
})()) ??
((() => {
const M = d.mark();
if (!d.openRecord()) return void 0;
const L = _.checkIs(d.nextSymbol(), $dict);
if (L === void 0) { d.restoreMark(M); return void 0; };
const Fs = (((() => {
const M = d.mark();
const v0 = ((() => {
const M = d.mark();
if (!d.openDictionary()) return void 0;
const r: _.KeyedDictionary<_val, Pattern, _ptr> = new _.KeyedDictionary();
let K: undefined | _val;
while ((K = d.next()) !== void 0) {
const V = decodePattern(d);
if (V === void 0) { d.restoreMark(M); return void 0; };
r.set(K, V);
}; };
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; if (result === void 0) {
return r; d.restoreMark(_tmp11);
})()); if (v0 === void 0) { d.restoreMark(M); return void 0; }; if (_.asLiteral(_tmp10, $or) !== void 0) {
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; let _tmp21, _tmp22: any;
return [v0] as [_.KeyedDictionary<_val, Pattern, _ptr>]; if (d.openSequence()) {
})())) as any; let _tmp23: any;
if (Fs === void 0) { d.restoreMark(M); return void 0; }; {
return _.Record<(typeof $dict), [_.KeyedDictionary<_val, Pattern, _ptr>]>(L, Fs); let vN: Array<Pattern> | undefined = [];
})()) while (!d.closeCompound()) {
); _tmp23 = void 0;
_tmp23 = decodePattern(d);
if (_tmp23 === void 0) {vN = void 0; break;};
vN.push(_tmp23);
};
_tmp22 = vN;
};
};
if (_tmp22 !== void 0) {if (d.closeCompound()) _tmp21 = [_tmp22];};
if (_tmp21 !== void 0) result = _.Record<(typeof $or), [Array<Pattern>]>(_tmp10 as any, _tmp21 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $and) !== void 0) {
let _tmp24, _tmp25: any;
if (d.openSequence()) {
let _tmp26: any;
{
let vN: Array<Pattern> | undefined = [];
while (!d.closeCompound()) {
_tmp26 = void 0;
_tmp26 = decodePattern(d);
if (_tmp26 === void 0) {vN = void 0; break;};
vN.push(_tmp26);
};
_tmp25 = vN;
};
};
if (_tmp25 !== void 0) {if (d.closeCompound()) _tmp24 = [_tmp25];};
if (_tmp24 !== void 0) result = _.Record<(typeof $and), [Array<Pattern>]>(_tmp10 as any, _tmp24 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $rec) !== void 0) {
let _tmp27, _tmp28, _tmp29: any;
_tmp28 = decodePattern(d);
if (_tmp28 !== void 0) {
_tmp29 = decodePattern(d);
if (_tmp29 !== void 0) {if (d.closeCompound()) _tmp27 = [_tmp28, _tmp29];};
};
if (_tmp27 !== void 0) result = _.Record<(typeof $rec), [Pattern, Pattern]>(_tmp10 as any, _tmp27 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $tuple) !== void 0) {
let _tmp30, _tmp31: any;
if (d.openSequence()) {
let _tmp32: any;
{
let vN: Array<NamedPattern> | undefined = [];
while (!d.closeCompound()) {
_tmp32 = void 0;
_tmp32 = decodeNamedPattern(d);
if (_tmp32 === void 0) {vN = void 0; break;};
vN.push(_tmp32);
};
_tmp31 = vN;
};
};
if (_tmp31 !== void 0) {if (d.closeCompound()) _tmp30 = [_tmp31];};
if (_tmp30 !== void 0) result = _.Record<(typeof $tuple), [Array<NamedPattern>]>(_tmp10 as any, _tmp30 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $tuple_STAR_) !== void 0) {
let _tmp33, _tmp34, _tmp35: any;
if (d.openSequence()) {
let _tmp36: any;
{
let vN: Array<NamedPattern> | undefined = [];
while (!d.closeCompound()) {
_tmp36 = void 0;
_tmp36 = decodeNamedPattern(d);
if (_tmp36 === void 0) {vN = void 0; break;};
vN.push(_tmp36);
};
_tmp34 = vN;
};
};
if (_tmp34 !== void 0) {
_tmp35 = decodeNamedPattern(d);
if (_tmp35 !== void 0) {if (d.closeCompound()) _tmp33 = [_tmp34, _tmp35];};
};
if (_tmp33 !== void 0) result = _.Record<(typeof $tuple_STAR_), [Array<NamedPattern>, NamedPattern]>(_tmp10 as any, _tmp33 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $setof) !== void 0) {
let _tmp37, _tmp38: any;
_tmp38 = decodePattern(d);
if (_tmp38 !== void 0) {if (d.closeCompound()) _tmp37 = [_tmp38];};
if (_tmp37 !== void 0) result = _.Record<(typeof $setof), [Pattern]>(_tmp10 as any, _tmp37 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $dictof) !== void 0) {
let _tmp39, _tmp40, _tmp41: any;
_tmp40 = decodePattern(d);
if (_tmp40 !== void 0) {
_tmp41 = decodePattern(d);
if (_tmp41 !== void 0) {if (d.closeCompound()) _tmp39 = [_tmp40, _tmp41];};
};
if (_tmp39 !== void 0) result = _.Record<(typeof $dictof), [Pattern, Pattern]>(_tmp10 as any, _tmp39 as any);
};
if (result === void 0) {
d.restoreMark(_tmp11);
if (_.asLiteral(_tmp10, $dict) !== void 0) {
let _tmp42, _tmp43: any;
if (d.openDictionary()) {
let r: _.KeyedDictionary<_val, Pattern, _ptr> | undefined = new _.KeyedDictionary();
while (!d.closeCompound()) {
let K: undefined | _val = void 0;
K = d.next();
if (K === void 0) { r = void 0; break; };
let V: undefined | Pattern = void 0;
V = decodePattern(d);
if (V === void 0) { r = void 0; break; };
r.set(K, V);
};
_tmp43 = r;
};
if (_tmp43 !== void 0) {if (d.closeCompound()) _tmp42 = [_tmp43];};
if (_tmp42 !== void 0) result = _.Record<(typeof $dict), [_.KeyedDictionary<_val, Pattern, _ptr>]>(_tmp10 as any, _tmp42 as any);
};
};
};
};
};
};
};
};
};
};
};
};
};
return result;
} }
export function isNamedPattern(v: any): v is NamedPattern { export function isNamedPattern(v: any): v is NamedPattern {
return ( return (
( (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $named) && _.asLiteral(v.label, $named) !== void 0 &&
((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1])) ((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1]))
) || ) ||
isPattern(v) isPattern(v)
@ -588,30 +539,30 @@ export function asNamedPattern(v: any): NamedPattern {
} }
export function decodeNamedPattern(d: _.TypedDecoder<_ptr>): NamedPattern | undefined { export function decodeNamedPattern(d: _.TypedDecoder<_ptr>): NamedPattern | undefined {
return ( let _tmp44: any;
((() => { let result;
const M = d.mark(); _tmp44 = d.mark();
if (!d.openRecord()) return void 0; if (d.openRecord()) {
const L = _.checkIs(d.nextSymbol(), $named); let _tmp45: any;
if (L === void 0) { d.restoreMark(M); return void 0; }; _tmp45 = _.asLiteral(d.nextSymbol(), $named);
const Fs = (((() => { if (_tmp45 !== void 0) {
const M = d.mark(); let _tmp46, _tmp47, _tmp48: any;
const v0 = d.nextSymbol(); if (v0 === void 0) { d.restoreMark(M); return void 0; }; _tmp47 = d.nextSymbol();
const v1 = decodePattern(d); if (v1 === void 0) { d.restoreMark(M); return void 0; }; if (_tmp47 !== void 0) {
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; _tmp48 = decodePattern(d);
return [v0, v1] as [symbol, Pattern]; if (_tmp48 !== void 0) {if (d.closeCompound()) _tmp46 = [_tmp47, _tmp48];};
})())) as any; };
if (Fs === void 0) { d.restoreMark(M); return void 0; }; if (_tmp46 !== void 0) result = _.Record<(typeof $named), [symbol, Pattern]>(_tmp45 as any, _tmp46 as any);
return _.Record<(typeof $named), [symbol, Pattern]>(L, Fs); };
})()) ?? };
decodePattern(d) if (result === void 0) {d.restoreMark(_tmp44); result = decodePattern(d);};
); return result;
} }
export function isRef(v: any): v is Ref { export function isRef(v: any): v is Ref {
return ( return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $ref) && _.asLiteral(v.label, $ref) !== void 0 &&
((v.length === 2) && isModuleRef(v[0]) && typeof v[1] === 'symbol') ((v.length === 2) && isModuleRef(v[0]) && typeof v[1] === 'symbol')
); );
} }
@ -621,37 +572,44 @@ export function asRef(v: any): Ref {
} }
export function decodeRef(d: _.TypedDecoder<_ptr>): Ref | undefined { export function decodeRef(d: _.TypedDecoder<_ptr>): Ref | undefined {
return ((() => { let result;
const M = d.mark(); if (d.openRecord()) {
if (!d.openRecord()) return void 0; let _tmp49: any;
const L = _.checkIs(d.nextSymbol(), $ref); _tmp49 = _.asLiteral(d.nextSymbol(), $ref);
if (L === void 0) { d.restoreMark(M); return void 0; }; if (_tmp49 !== void 0) {
const Fs = (((() => { let _tmp50, _tmp51, _tmp52: any;
const M = d.mark(); _tmp51 = decodeModuleRef(d);
const v0 = decodeModuleRef(d); if (v0 === void 0) { d.restoreMark(M); return void 0; }; if (_tmp51 !== void 0) {
const v1 = d.nextSymbol(); if (v1 === void 0) { d.restoreMark(M); return void 0; }; _tmp52 = d.nextSymbol();
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; if (_tmp52 !== void 0) {if (d.closeCompound()) _tmp50 = [_tmp51, _tmp52];};
return [v0, v1] as [ModuleRef, symbol]; };
})())) as any; if (_tmp50 !== void 0) result = _.Record<(typeof $ref), [ModuleRef, symbol]>(_tmp49 as any, _tmp50 as any);
if (Fs === void 0) { d.restoreMark(M); return void 0; }; };
return _.Record<(typeof $ref), [ModuleRef, symbol]>(L, Fs); };
})()); return result;
} }
export function isModuleRef(v: any): v is ModuleRef {return (_.is(v, $thisModule) || isModulePath(v));} export function isModuleRef(v: any): v is ModuleRef {return (_.asLiteral(v, $thisModule) !== void 0 || isModulePath(v));}
export function asModuleRef(v: any): ModuleRef { export function asModuleRef(v: any): ModuleRef {
if (!isModuleRef(v)) {throw new TypeError(`Invalid ModuleRef: ${_.stringify(v)}`);} else {return v;}; if (!isModuleRef(v)) {throw new TypeError(`Invalid ModuleRef: ${_.stringify(v)}`);} else {return v;};
} }
export function decodeModuleRef(d: _.TypedDecoder<_ptr>): ModuleRef | undefined {return (_.checkIs(d.nextSymbol(), $thisModule) ?? decodeModulePath(d));} export function decodeModuleRef(d: _.TypedDecoder<_ptr>): ModuleRef | undefined {
let _tmp53: any;
let result;
_tmp53 = d.mark();
result = _.asLiteral(d.nextSymbol(), $thisModule);
if (result === void 0) {d.restoreMark(_tmp53); result = decodeModulePath(d);};
return result;
}
export function isModulePath(v: any): v is ModulePath { export function isModulePath(v: any): v is ModulePath {
return ( return (
_.Array.isArray(v) && _.Array.isArray(v) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
(v.length >= 0) && (v.length >= 0) &&
v.slice(0).every(v => (typeof v === 'symbol')) v.every(v => (typeof v === 'symbol'))
); );
} }
@ -660,15 +618,21 @@ export function asModulePath(v: any): ModulePath {
} }
export function decodeModulePath(d: _.TypedDecoder<_ptr>): ModulePath | undefined { export function decodeModulePath(d: _.TypedDecoder<_ptr>): ModulePath | undefined {
return ((() => { let result;
const M = d.mark(); if (d.openSequence()) {
if (!d.openSequence()) return void 0; let _tmp54: any;
const vN: Array<symbol> = []; {
let tmp: undefined | symbol; let vN: Array<symbol> | undefined = [];
while ((tmp = d.nextSymbol()) !== void 0) vN.push(tmp); while (!d.closeCompound()) {
if (!d.closeCompound()) { d.restoreMark(M); return void 0; }; _tmp54 = void 0;
return vN; _tmp54 = d.nextSymbol();
})()); if (_tmp54 === void 0) {vN = void 0; break;};
vN.push(_tmp54);
};
result = vN;
};
};
return result;
} }
export const _decodePtr = () => { throw new _.DecodeError("Pointers forbidden"); }; export const _decodePtr = () => { throw new _.DecodeError("Pointers forbidden"); };