1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
import {_, nil, Code, Name} from "./code"
interface NameGroup {
prefix: string
index: number
}
export interface NameValue {
ref: ValueReference // this is the reference to any value that can be referred to from generated code via `globals` var in the closure
key?: unknown // any key to identify a global to avoid duplicates, if not passed ref is used
code?: Code // this is the code creating the value needed for standalone code wit_out closure - can be a primitive value, function or import (`require`)
}
export type ValueReference = unknown // possibly make CodeGen parameterized type on this type
class ValueError extends Error {
readonly value?: NameValue
constructor(name: ValueScopeName) {
super(`CodeGen: "code" for ${name} not defined`)
this.value = name.value
}
}
interface ScopeOptions {
prefixes?: Set<string>
parent?: Scope
}
interface ValueScopeOptions extends ScopeOptions {
scope: ScopeStore
es5?: boolean
lines?: boolean
}
export type ScopeStore = Record<string, ValueReference[] | undefined>
type ScopeValues = {
[Prefix in string]?: Map<unknown, ValueScopeName>
}
export type ScopeValueSets = {
[Prefix in string]?: Set<ValueScopeName>
}
export enum UsedValueState {
Started,
Completed,
}
export type UsedScopeValues = {
[Prefix in string]?: Map<ValueScopeName, UsedValueState | undefined>
}
export const varKinds = {
const: new Name("const"),
let: new Name("let"),
var: new Name("var"),
}
export class Scope {
protected readonly _names: {[Prefix in string]?: NameGroup} = {}
protected readonly _prefixes?: Set<string>
protected readonly _parent?: Scope
constructor({prefixes, parent}: ScopeOptions = {}) {
this._prefixes = prefixes
this._parent = parent
}
toName(nameOrPrefix: Name | string): Name {
return nameOrPrefix instanceof Name ? nameOrPrefix : this.name(nameOrPrefix)
}
name(prefix: string): Name {
return new Name(this._newName(prefix))
}
protected _newName(prefix: string): string {
const ng = this._names[prefix] || this._nameGroup(prefix)
return `${prefix}${ng.index++}`
}
private _nameGroup(prefix: string): NameGroup {
if (this._parent?._prefixes?.has(prefix) || (this._prefixes && !this._prefixes.has(prefix))) {
throw new Error(`CodeGen: prefix "${prefix}" is not allowed in this scope`)
}
return (this._names[prefix] = {prefix, index: 0})
}
}
interface ScopePath {
property: string
itemIndex: number
}
export class ValueScopeName extends Name {
readonly prefix: string
value?: NameValue
scopePath?: Code
constructor(prefix: string, nameStr: string) {
super(nameStr)
this.prefix = prefix
}
setValue(value: NameValue, {property, itemIndex}: ScopePath): void {
this.value = value
this.scopePath = _`.${new Name(property)}[${itemIndex}]`
}
}
interface VSOptions extends ValueScopeOptions {
_n: Code
}
const line = _`\n`
export class ValueScope extends Scope {
protected readonly _values: ScopeValues = {}
protected readonly _scope: ScopeStore
readonly opts: VSOptions
constructor(opts: ValueScopeOptions) {
super(opts)
this._scope = opts.scope
this.opts = {...opts, _n: opts.lines ? line : nil}
}
get(): ScopeStore {
return this._scope
}
name(prefix: string): ValueScopeName {
return new ValueScopeName(prefix, this._newName(prefix))
}
value(nameOrPrefix: ValueScopeName | string, value: NameValue): ValueScopeName {
if (value.ref === undefined) throw new Error("CodeGen: ref must be passed in value")
const name = this.toName(nameOrPrefix) as ValueScopeName
const {prefix} = name
const valueKey = value.key ?? value.ref
let vs = this._values[prefix]
if (vs) {
const _name = vs.get(valueKey)
if (_name) return _name
} else {
vs = this._values[prefix] = new Map()
}
vs.set(valueKey, name)
const s = this._scope[prefix] || (this._scope[prefix] = [])
const itemIndex = s.length
s[itemIndex] = value.ref
name.setValue(value, {property: prefix, itemIndex})
return name
}
getValue(prefix: string, keyOrRef: unknown): ValueScopeName | undefined {
const vs = this._values[prefix]
if (!vs) return
return vs.get(keyOrRef)
}
scopeRefs(scopeName: Name, values: ScopeValues | ScopeValueSets = this._values): Code {
return this._reduceValues(values, (name: ValueScopeName) => {
if (name.scopePath === undefined) throw new Error(`CodeGen: name "${name}" has no value`)
return _`${scopeName}${name.scopePath}`
})
}
scopeCode(
values: ScopeValues | ScopeValueSets = this._values,
usedValues?: UsedScopeValues,
getCode?: (n: ValueScopeName) => Code | undefined
): Code {
return this._reduceValues(
values,
(name: ValueScopeName) => {
if (name.value === undefined) throw new Error(`CodeGen: name "${name}" has no value`)
return name.value.code
},
usedValues,
getCode
)
}
private _reduceValues(
values: ScopeValues | ScopeValueSets,
valueCode: (n: ValueScopeName) => Code | undefined,
usedValues: UsedScopeValues = {},
getCode?: (n: ValueScopeName) => Code | undefined
): Code {
let code: Code = nil
for (const prefix in values) {
const vs = values[prefix]
if (!vs) continue
const nameSet = (usedValues[prefix] = usedValues[prefix] || new Map())
vs.forEach((name: ValueScopeName) => {
if (nameSet.has(name)) return
nameSet.set(name, UsedValueState.Started)
let c = valueCode(name)
if (c) {
const def = this.opts.es5 ? varKinds.var : varKinds.const
code = _`${code}${def} ${name} = ${c};${this.opts._n}`
} else if ((c = getCode?.(name))) {
code = _`${code}${c}${this.opts._n}`
} else {
throw new ValueError(name)
}
nameSet.set(name, UsedValueState.Completed)
})
}
return code
}
}
|