id
int64
0
3.78k
code
stringlengths
13
37.9k
declarations
stringlengths
16
64.6k
2,600
function parsePropertyValue(cxt: ParseCxt, key: Name, schema: SchemaObject): void { parseCode({...cxt, schema, data: _`${cxt.data}[${key}]`}) }
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,601
function parseType(cxt: ParseCxt): void { const {gen, schema, data, self} = cxt switch (schema.type) { case "boolean": parseBoolean(cxt) break case "string": parseString(cxt) break case "timestamp": { parseString(cxt) const vts = useFunc(gen, validTimestamp) con...
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,602
function parseString(cxt: ParseCxt): void { parseToken(cxt, '"') parseWith(cxt, parseJsonString) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,603
function parseEnum(cxt: ParseCxt): void { const {gen, data, schema} = cxt const enumSch = schema.enum parseToken(cxt, '"') // TODO loopEnum gen.if(false) for (const value of enumSch) { const valueStr = JSON.stringify(value).slice(1) // remove starting quote gen.elseIf(_`${jsonSlice(valueStr.length)}...
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,604
function parseNumber(cxt: ParseCxt, maxDigits?: number): void { const {gen} = cxt skipWhitespace(cxt) gen.if( _`"-0123456789".indexOf(${jsonSlice(1)}) < 0`, () => jsonSyntaxError(cxt), () => parseWith(cxt, parseJsonNumber, maxDigits) ) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,605
function parseRef(cxt: ParseCxt): void { const {gen, self, definitions, schema, schemaEnv} = cxt const {ref} = schema const refSchema = definitions[ref] if (!refSchema) throw new MissingRefError(self.opts.uriResolver, "", ref, `No definition ${ref}`) if (!hasRef(refSchema)) return parseCode({...cxt, schema: r...
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,606
function getParser(gen: CodeGen, sch: SchemaEnv): Code { return sch.parse ? gen.scopeValue("parse", {ref: sch.parse}) : _`${gen.scopeValue("wrapper", {ref: sch})}.parse` }
class SchemaEnv implements SchemaEnvArgs { readonly schema: AnySchema readonly schemaId?: "$id" | "id" readonly root: SchemaEnv baseId: string // TODO possibly, it should be readonly schemaPath?: string localRefs?: LocalRefs readonly meta?: boolean readonly $async?: boolean // true if the current schema...
2,607
function getParser(gen: CodeGen, sch: SchemaEnv): Code { return sch.parse ? gen.scopeValue("parse", {ref: sch.parse}) : _`${gen.scopeValue("wrapper", {ref: sch})}.parse` }
class CodeGen { readonly _scope: Scope readonly _extScope: ValueScope readonly _values: ScopeValueSets = {} private readonly _nodes: ParentNode[] private readonly _blockStarts: number[] = [] private readonly _constants: Constants = {} private readonly opts: CGOptions constructor(extScope: ValueScope, o...
2,608
function parseEmpty(cxt: ParseCxt): void { parseWith(cxt, parseJson) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,609
function parseWith(cxt: ParseCxt, parseFunc: {code: string}, args?: SafeExpr): void { partialParse(cxt, useFunc(cxt.gen, parseFunc), args) }
type SafeExpr = Code | number | boolean | null
2,610
function parseWith(cxt: ParseCxt, parseFunc: {code: string}, args?: SafeExpr): void { partialParse(cxt, useFunc(cxt.gen, parseFunc), args) }
type SafeExpr = Code | number | boolean | null
2,611
function parseWith(cxt: ParseCxt, parseFunc: {code: string}, args?: SafeExpr): void { partialParse(cxt, useFunc(cxt.gen, parseFunc), args) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,612
function partialParse(cxt: ParseCxt, parseFunc: Name, args?: SafeExpr): void { const {gen, data} = cxt gen.assign(data, _`${parseFunc}(${N.json}, ${N.jsonPos}${args ? _`, ${args}` : nil})`) gen.assign(N.jsonPos, _`${parseFunc}.position`) gen.if(_`${data} === undefined`, () => parsingError(cxt, _`${parseFunc}.me...
type SafeExpr = Code | number | boolean | null
2,613
function partialParse(cxt: ParseCxt, parseFunc: Name, args?: SafeExpr): void { const {gen, data} = cxt gen.assign(data, _`${parseFunc}(${N.json}, ${N.jsonPos}${args ? _`, ${args}` : nil})`) gen.assign(N.jsonPos, _`${parseFunc}.position`) gen.if(_`${data} === undefined`, () => parsingError(cxt, _`${parseFunc}.me...
type SafeExpr = Code | number | boolean | null
2,614
function partialParse(cxt: ParseCxt, parseFunc: Name, args?: SafeExpr): void { const {gen, data} = cxt gen.assign(data, _`${parseFunc}(${N.json}, ${N.jsonPos}${args ? _`, ${args}` : nil})`) gen.assign(N.jsonPos, _`${parseFunc}.position`) gen.if(_`${data} === undefined`, () => parsingError(cxt, _`${parseFunc}.me...
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,615
function partialParse(cxt: ParseCxt, parseFunc: Name, args?: SafeExpr): void { const {gen, data} = cxt gen.assign(data, _`${parseFunc}(${N.json}, ${N.jsonPos}${args ? _`, ${args}` : nil})`) gen.assign(N.jsonPos, _`${parseFunc}.position`) gen.if(_`${data} === undefined`, () => parsingError(cxt, _`${parseFunc}.me...
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,616
function parseToken(cxt: ParseCxt, tok: string): void { tryParseToken(cxt, tok, jsonSyntaxError) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,617
function tryParseToken(cxt: ParseCxt, tok: string, fail: GenParse, success?: GenParse): void { const {gen} = cxt const n = tok.length skipWhitespace(cxt) gen.if( _`${jsonSlice(n)} === ${tok}`, () => { gen.add(N.jsonPos, n) success?.(cxt) }, () => fail(cxt) ) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,618
function tryParseToken(cxt: ParseCxt, tok: string, fail: GenParse, success?: GenParse): void { const {gen} = cxt const n = tok.length skipWhitespace(cxt) gen.if( _`${jsonSlice(n)} === ${tok}`, () => { gen.add(N.jsonPos, n) success?.(cxt) }, () => fail(cxt) ) }
type GenParse = (cxt: ParseCxt) => void
2,619
function skipWhitespace({gen, char: c}: ParseCxt): void { gen.code( _`while((${c}=${N.json}[${N.jsonPos}],${c}===" "||${c}==="\\n"||${c}==="\\r"||${c}==="\\t"))${N.jsonPos}++;` ) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,620
function jsonSyntaxError(cxt: ParseCxt): void { parsingError(cxt, _`"unexpected token " + ${N.json}[${N.jsonPos}]`) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,621
function parsingError({gen, parseName}: ParseCxt, msg: Code): void { gen.assign(_`${parseName}.message`, msg) gen.assign(_`${parseName}.position`, N.jsonPos) gen.return(undef) }
interface ParseCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code parseName: Name char: Name }
2,622
function parsingError({gen, parseName}: ParseCxt, msg: Code): void { gen.assign(_`${parseName}.message`, msg) gen.assign(_`${parseName}.position`, N.jsonPos) gen.return(undef) }
type Code = _Code | Name
2,623
(cxt: SerializeCxt) => void
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,624
function compileSerializer( this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap ): SchemaEnv { const _sch = getCompilingSchema.call(this, sch) if (_sch) return _sch const {es5, lines} = this.opts.code const {ownProperties} = this.opts const gen = new CodeGen(this.scope, {es5, lines, ownProperties}) ...
class SchemaEnv implements SchemaEnvArgs { readonly schema: AnySchema readonly schemaId?: "$id" | "id" readonly root: SchemaEnv baseId: string // TODO possibly, it should be readonly schemaPath?: string localRefs?: LocalRefs readonly meta?: boolean readonly $async?: boolean // true if the current schema...
2,625
function compileSerializer( this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap ): SchemaEnv { const _sch = getCompilingSchema.call(this, sch) if (_sch) return _sch const {es5, lines} = this.opts.code const {ownProperties} = this.opts const gen = new CodeGen(this.scope, {es5, lines, ownProperties}) ...
class Ajv { opts: InstanceOptions errors?: ErrorObject[] | null // errors from the last validation logger: Logger // shared external scope values for compiled functions readonly scope: ValueScope readonly schemas: {[Key in string]?: SchemaEnv} = {} readonly refs: {[Ref in string]?: SchemaEnv | string} = {...
2,626
function compileSerializer( this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap ): SchemaEnv { const _sch = getCompilingSchema.call(this, sch) if (_sch) return _sch const {es5, lines} = this.opts.code const {ownProperties} = this.opts const gen = new CodeGen(this.scope, {es5, lines, ownProperties}) ...
class Ajv extends AjvCore { constructor(opts: JTDOptions = {}) { super({ ...opts, jtd: true, }) } _addVocabularies(): void { super._addVocabularies() this.addVocabulary(jtdVocabulary) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) ret...
2,627
function compileSerializer( this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap ): SchemaEnv { const _sch = getCompilingSchema.call(this, sch) if (_sch) return _sch const {es5, lines} = this.opts.code const {ownProperties} = this.opts const gen = new CodeGen(this.scope, {es5, lines, ownProperties}) ...
class Ajv extends AjvCore { _addVocabularies(): void { super._addVocabularies() draft7Vocabularies.forEach((v) => this.addVocabulary(v)) if (this.opts.discriminator) this.addKeyword(discriminator) } _addDefaultMetaSchema(): void { super._addDefaultMetaSchema() if (!this.opts.meta) return ...
2,628
function compileSerializer( this: Ajv, sch: SchemaEnv, definitions: SchemaObjectMap ): SchemaEnv { const _sch = getCompilingSchema.call(this, sch) if (_sch) return _sch const {es5, lines} = this.opts.code const {ownProperties} = this.opts const gen = new CodeGen(this.scope, {es5, lines, ownProperties}) ...
type SchemaObjectMap = {[Ref in string]?: SchemaObject}
2,629
function serializeCode(cxt: SerializeCxt): void { let form: JTDForm | undefined for (const key of jtdForms) { if (key in cxt.schema) { form = key break } } serializeNullable(cxt, form ? genSerialize[form] : serializeEmpty) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,630
function serializeNullable(cxt: SerializeCxt, serializeForm: (_cxt: SerializeCxt) => void): void { const {gen, schema, data} = cxt if (!schema.nullable) return serializeForm(cxt) gen.if( _`${data} === undefined || ${data} === null`, () => gen.add(N.json, _`"null"`), () => serializeForm(cxt) ) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,631
(_cxt: SerializeCxt) => void
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,632
function serializeElements(cxt: SerializeCxt): void { const {gen, schema, data} = cxt gen.add(N.json, str`[`) const first = gen.let("first", true) gen.forOf("el", data, (el) => { addComma(cxt, first) serializeCode({...cxt, schema: schema.elements, data: el}) }) gen.add(N.json, str`]`) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,633
function serializeValues(cxt: SerializeCxt): void { const {gen, schema, data} = cxt gen.add(N.json, str`{`) const first = gen.let("first", true) gen.forIn("key", data, (key) => serializeKeyValue(cxt, key, schema.values, first)) gen.add(N.json, str`}`) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,634
function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first?: Name): void { const {gen, data} = cxt addComma(cxt, first) serializeString({...cxt, data: key}) gen.add(N.json, str`:`) const value = gen.const("value", _`${data}${getProperty(key)}`) serializeCode({...cxt, schema, data: ...
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,635
function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first?: Name): void { const {gen, data} = cxt addComma(cxt, first) serializeString({...cxt, data: key}) gen.add(N.json, str`:`) const value = gen.const("value", _`${data}${getProperty(key)}`) serializeCode({...cxt, schema, data: ...
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,636
function serializeKeyValue(cxt: SerializeCxt, key: Name, schema: SchemaObject, first?: Name): void { const {gen, data} = cxt addComma(cxt, first) serializeString({...cxt, data: key}) gen.add(N.json, str`:`) const value = gen.const("value", _`${data}${getProperty(key)}`) serializeCode({...cxt, schema, data: ...
interface SchemaObject extends _SchemaObject { id?: string $id?: string $schema?: string $async?: false [x: string]: any // TODO }
2,637
function serializeDiscriminator(cxt: SerializeCxt): void { const {gen, schema, data} = cxt const {discriminator} = schema gen.add(N.json, str`{${JSON.stringify(discriminator)}:`) const tag = gen.const("tag", _`${data}${getProperty(discriminator)}`) serializeString({...cxt, data: tag}) gen.if(false) for (c...
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,638
function serializeProperties(cxt: SerializeCxt): void { const {gen} = cxt gen.add(N.json, str`{`) serializeSchemaProperties(cxt) gen.add(N.json, str`}`) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,639
function serializeSchemaProperties(cxt: SerializeCxt, discriminator?: string): void { const {gen, schema, data} = cxt const {properties, optionalProperties} = schema const props = keys(properties) const optProps = keys(optionalProperties) const allProps = allProperties(props.concat(optProps)) let first = !d...
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,640
function keys(ps?: SchemaObjectMap): string[] { return ps ? Object.keys(ps) : [] }
type SchemaObjectMap = {[Ref in string]?: SchemaObject}
2,641
function isAdditional(key: Name, ps: string[]): Code | true { return ps.length ? and(...ps.map((p) => _`${key} !== ${p}`)) : true }
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,642
function serializeType(cxt: SerializeCxt): void { const {gen, schema, data} = cxt switch (schema.type) { case "boolean": gen.add(N.json, _`${data} ? "true" : "false"`) break case "string": serializeString(cxt) break case "timestamp": gen.if( _`${data} instanceof Dat...
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,643
function serializeString({gen, data}: SerializeCxt): void { gen.add(N.json, _`${useFunc(gen, quote)}(${data})`) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,644
function serializeNumber({gen, data}: SerializeCxt): void { gen.add(N.json, _`"" + ${data}`) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,645
function serializeRef(cxt: SerializeCxt): void { const {gen, self, data, definitions, schema, schemaEnv} = cxt const {ref} = schema const refSchema = definitions[ref] if (!refSchema) throw new MissingRefError(self.opts.uriResolver, "", ref, `No definition ${ref}`) if (!hasRef(refSchema)) return serializeCode(...
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,646
function getSerialize(gen: CodeGen, sch: SchemaEnv): Code { return sch.serialize ? gen.scopeValue("serialize", {ref: sch.serialize}) : _`${gen.scopeValue("wrapper", {ref: sch})}.serialize` }
class SchemaEnv implements SchemaEnvArgs { readonly schema: AnySchema readonly schemaId?: "$id" | "id" readonly root: SchemaEnv baseId: string // TODO possibly, it should be readonly schemaPath?: string localRefs?: LocalRefs readonly meta?: boolean readonly $async?: boolean // true if the current schema...
2,647
function getSerialize(gen: CodeGen, sch: SchemaEnv): Code { return sch.serialize ? gen.scopeValue("serialize", {ref: sch.serialize}) : _`${gen.scopeValue("wrapper", {ref: sch})}.serialize` }
class CodeGen { readonly _scope: Scope readonly _extScope: ValueScope readonly _values: ScopeValueSets = {} private readonly _nodes: ParentNode[] private readonly _blockStarts: number[] = [] private readonly _constants: Constants = {} private readonly opts: CGOptions constructor(extScope: ValueScope, o...
2,648
function serializeEmpty({gen, data}: SerializeCxt): void { gen.add(N.json, _`JSON.stringify(${data})`) }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,649
function addComma({gen}: SerializeCxt, first?: Name): void { if (first) { gen.if( first, () => gen.assign(first, false), () => gen.add(N.json, str`,`) ) } else { gen.add(N.json, str`,`) } }
interface SerializeCxt { readonly gen: CodeGen readonly self: Ajv // current Ajv instance readonly schemaEnv: SchemaEnv readonly definitions: SchemaObjectMap schema: SchemaObject data: Code }
2,650
function addComma({gen}: SerializeCxt, first?: Name): void { if (first) { gen.if( first, () => gen.assign(first, false), () => gen.add(N.json, str`,`) ) } else { gen.add(N.json, str`,`) } }
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,651
(names: UsedNames, c) => { if (c instanceof Name) names[c.str] = (names[c.str] || 0) + 1 return names }
type UsedNames = Record<string, number | undefined>
2,652
function mergeExprItems(a: CodeItem, b: CodeItem): CodeItem | undefined { if (b === '""') return a if (a === '""') return b if (typeof a == "string") { if (b instanceof Name || a[a.length - 1] !== '"') return if (typeof b != "string") return `${a.slice(0, -1)}${b}"` if (b[0] === '"') return a.slice(0,...
type CodeItem = Name | string | number | boolean | null
2,653
function strConcat(c1: Code, c2: Code): Code { return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str`${c1}${c2}` }
type Code = _Code | Name
2,654
optimizeNames(_names: UsedNames, _constants: Constants): this | undefined { return this }
type Constants = Record<string, SafeExpr | undefined>
2,655
optimizeNames(_names: UsedNames, _constants: Constants): this | undefined { return this }
type UsedNames = Record<string, number | undefined>
2,656
constructor(private readonly varKind: Name, private readonly name: Name, private rhs?: SafeExpr) { super() }
type SafeExpr = Code | number | boolean | null
2,657
constructor(private readonly varKind: Name, private readonly name: Name, private rhs?: SafeExpr) { super() }
type SafeExpr = Code | number | boolean | null
2,658
constructor(private readonly varKind: Name, private readonly name: Name, private rhs?: SafeExpr) { super() }
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,659
render({es5, _n}: CGOptions): string { const varKind = es5 ? varKinds.var : this.varKind const rhs = this.rhs === undefined ? "" : ` = ${this.rhs}` return `${varKind} ${this.name}${rhs};` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,660
optimizeNames(names: UsedNames, constants: Constants): this | undefined { if (!names[this.name.str]) return if (this.rhs) this.rhs = optimizeExpr(this.rhs, names, constants) return this }
type Constants = Record<string, SafeExpr | undefined>
2,661
optimizeNames(names: UsedNames, constants: Constants): this | undefined { if (!names[this.name.str]) return if (this.rhs) this.rhs = optimizeExpr(this.rhs, names, constants) return this }
type UsedNames = Record<string, number | undefined>
2,662
constructor(readonly lhs: Code, public rhs: SafeExpr, private readonly sideEffects?: boolean) { super() }
type SafeExpr = Code | number | boolean | null
2,663
constructor(readonly lhs: Code, public rhs: SafeExpr, private readonly sideEffects?: boolean) { super() }
type SafeExpr = Code | number | boolean | null
2,664
constructor(readonly lhs: Code, public rhs: SafeExpr, private readonly sideEffects?: boolean) { super() }
type Code = _Code | Name
2,665
render({_n}: CGOptions): string { return `${this.lhs} = ${this.rhs};` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,666
optimizeNames(names: UsedNames, constants: Constants): this | undefined { if (this.lhs instanceof Name && !names[this.lhs.str] && !this.sideEffects) return this.rhs = optimizeExpr(this.rhs, names, constants) return this }
type Constants = Record<string, SafeExpr | undefined>
2,667
optimizeNames(names: UsedNames, constants: Constants): this | undefined { if (this.lhs instanceof Name && !names[this.lhs.str] && !this.sideEffects) return this.rhs = optimizeExpr(this.rhs, names, constants) return this }
type UsedNames = Record<string, number | undefined>
2,668
constructor(lhs: Code, private readonly op: Code, rhs: SafeExpr, sideEffects?: boolean) { super(lhs, rhs, sideEffects) }
type SafeExpr = Code | number | boolean | null
2,669
constructor(lhs: Code, private readonly op: Code, rhs: SafeExpr, sideEffects?: boolean) { super(lhs, rhs, sideEffects) }
type SafeExpr = Code | number | boolean | null
2,670
constructor(lhs: Code, private readonly op: Code, rhs: SafeExpr, sideEffects?: boolean) { super(lhs, rhs, sideEffects) }
type Code = _Code | Name
2,671
render({_n}: CGOptions): string { return `${this.lhs} ${this.op}= ${this.rhs};` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,672
constructor(readonly label: Name) { super() }
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,673
render({_n}: CGOptions): string { return `${this.label}:` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,674
constructor(readonly label?: Code) { super() }
type Code = _Code | Name
2,675
render({_n}: CGOptions): string { const label = this.label ? ` ${this.label}` : "" return `break${label};` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,676
constructor(readonly error: Code) { super() }
type Code = _Code | Name
2,677
render({_n}: CGOptions): string { return `throw ${this.error};` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,678
constructor(private code: SafeExpr) { super() }
type SafeExpr = Code | number | boolean | null
2,679
constructor(private code: SafeExpr) { super() }
type SafeExpr = Code | number | boolean | null
2,680
render({_n}: CGOptions): string { return `${this.code};` + _n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,681
optimizeNames(names: UsedNames, constants: Constants): this { this.code = optimizeExpr(this.code, names, constants) return this }
type Constants = Record<string, SafeExpr | undefined>
2,682
optimizeNames(names: UsedNames, constants: Constants): this { this.code = optimizeExpr(this.code, names, constants) return this }
type UsedNames = Record<string, number | undefined>
2,683
render(opts: CGOptions): string { return this.nodes.reduce((code, n) => code + n.render(opts), "") }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,684
optimizeNames(names: UsedNames, constants: Constants): this | undefined { const {nodes} = this let i = nodes.length while (i--) { // iterating backwards improves 1-pass optimization const n = nodes[i] if (n.optimizeNames(names, constants)) continue subtractNames(names, n.names) ...
type Constants = Record<string, SafeExpr | undefined>
2,685
optimizeNames(names: UsedNames, constants: Constants): this | undefined { const {nodes} = this let i = nodes.length while (i--) { // iterating backwards improves 1-pass optimization const n = nodes[i] if (n.optimizeNames(names, constants)) continue subtractNames(names, n.names) ...
type UsedNames = Record<string, number | undefined>
2,686
(names: UsedNames, n) => addNames(names, n.names)
type UsedNames = Record<string, number | undefined>
2,687
render(opts: CGOptions): string { return "{" + opts._n + super.render(opts) + "}" + opts._n }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,688
render(opts: CGOptions): string { let code = `if(${this.condition})` + super.render(opts) if (this.else) code += "else " + this.else.render(opts) return code }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,689
optimizeNames(names: UsedNames, constants: Constants): this | undefined { this.else = this.else?.optimizeNames(names, constants) if (!(super.optimizeNames(names, constants) || this.else)) return this.condition = optimizeExpr(this.condition, names, constants) return this }
type Constants = Record<string, SafeExpr | undefined>
2,690
optimizeNames(names: UsedNames, constants: Constants): this | undefined { this.else = this.else?.optimizeNames(names, constants) if (!(super.optimizeNames(names, constants) || this.else)) return this.condition = optimizeExpr(this.condition, names, constants) return this }
type UsedNames = Record<string, number | undefined>
2,691
constructor(private iteration: Code) { super() }
type Code = _Code | Name
2,692
render(opts: CGOptions): string { return `for(${this.iteration})` + super.render(opts) }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,693
optimizeNames(names: UsedNames, constants: Constants): this | undefined { if (!super.optimizeNames(names, constants)) return this.iteration = optimizeExpr(this.iteration, names, constants) return this }
type Constants = Record<string, SafeExpr | undefined>
2,694
optimizeNames(names: UsedNames, constants: Constants): this | undefined { if (!super.optimizeNames(names, constants)) return this.iteration = optimizeExpr(this.iteration, names, constants) return this }
type UsedNames = Record<string, number | undefined>
2,695
constructor( private readonly varKind: Name, private readonly name: Name, private readonly from: SafeExpr, private readonly to: SafeExpr ) { super() }
type SafeExpr = Code | number | boolean | null
2,696
constructor( private readonly varKind: Name, private readonly name: Name, private readonly from: SafeExpr, private readonly to: SafeExpr ) { super() }
type SafeExpr = Code | number | boolean | null
2,697
constructor( private readonly varKind: Name, private readonly name: Name, private readonly from: SafeExpr, private readonly to: SafeExpr ) { super() }
class Name extends _CodeOrName { readonly str: string constructor(s: string) { super() if (!IDENTIFIER.test(s)) throw new Error("CodeGen: name must be a valid identifier") this.str = s } toString(): string { return this.str } emptyStr(): boolean { return false } get names(): UsedN...
2,698
render(opts: CGOptions): string { const varKind = opts.es5 ? varKinds.var : this.varKind const {name, from, to} = this return `for(${varKind} ${name}=${from}; ${name}<${to}; ${name}++)` + super.render(opts) }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }
2,699
render(opts: CGOptions): string { return `for(${this.varKind} ${this.name} ${this.loop} ${this.iterable})` + super.render(opts) }
interface CGOptions extends CodeGenOptions { _n: "\n" | "" }