-
Notifications
You must be signed in to change notification settings - Fork 25
/
inline.js
228 lines (213 loc) · 8.2 KB
/
inline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Node.js API.
const util = require('util')
// Format source code maintaining indentation.
const $ = require('programmatic')
// Join an array of strings separated by an empty line.
const join = require('./join')
module.exports = function ({ variables, packet, direction, accumulators, parameters }) {
const accumulated = {}
const buffered = []
const stack = []
let $$ = -1
function accumulations (functions, seen = {}) {
const invocations = buffered.filter(accumulator => {
return accumulator.properties.some(property => {
return accumulated[property] != null && !seen[property]
})
})
for (const invocation of invocations) {
for (const property of invocation.properties) {
seen[property] = true
}
}
return invocations.length != 0
? invocations.map(invocation => {
return $(`
`, invocation.source, `
$starts[${invocation.start}] = $start
`)
}).join('\n')
: null
return invocations.length != 0 ? invocations.join('\n') : null
}
function accumulator (field, filter = () => true) {
const filtered = field.accumulators.filter(accumulator => filter(accumulator.name))
return filtered.length != 0 ? filtered.map(accumulator => {
const { name, source } = accumulator
accumulated[name] = []
if (accumulator.type == 'function' && parameters[name] == null) {
return `$accumulator[${util.inspect(name)}] = (${accumulators[name]})()`
} else {
return `$accumulator[${util.inspect(name)}] = ${accumulators[name]}`
}
}).join('\n') : null
}
function _properties (path, source, $_) {
// Collection of function properties.
const is = {
transform: false,
buffered: false,
assertion: false
}
const properties = {}, name = path.split('.').pop()
let type = 'transform'
const vargs = source.vargs.slice()
for (const property of source.properties) {
if (property == '$_' || property == name) {
if (source.defaulted.includes(property)) {
is.assertion = true
} else {
is.transform = true
}
properties[property] = $_
} else if (property == '$direction') {
properties[property] = util.inspect(direction)
} else if (property == '$i') {
properties[property] = variables.i ? property : '[]'
} else if (property == '$path') {
properties[property] = util.inspect(path.split('.'))
} else if (property == packet.name || property == '$') {
properties[property] = packet.name
} else if (accumulated[property]) {
properties[property] = `$accumulator[${util.inspect(property)}]`
} else if (property == '$buffer') {
is.buffered = true
properties[property] = property
} else if (property == '$start') {
is.buffered = true
properties[property] = `$starts[${buffered.length}]`
} else if (property == '$end') {
is.buffered = true
properties[property] = '$start'
} else {
properties[property] = vargs.shift()
}
}
return {
is: is,
properties: $(`
{
`, Object.keys(properties).map(property => {
return `${property}: ${properties[property]}`
}).join(',\n'), `
}
`)
}
}
function test (path, test, signature = []) {
if (test.properties.length == 0) {
const sliced = signature.concat([ packet.name ]).slice(0, test.arity)
return $(`
(`, test.source, `)(${sliced.join(', ')})
`)
}
const { properties } = _properties(path, test, null)
return $(`
(`, test.source, `)(`, properties, `)
`)
}
//
// Generate one or more inline user defined functions. We need to generate
// functions with both positional and named arguments, lookup up those
// arguments in the context supplied by the generator.
//
function inline (path, inlines) {
const registers = direction == 'serialize'
? [ path, `$$[${++$$}]` ]
: [ path ]
if (inlines.length == 0) {
stack.push({ offset: 0, length: 0 })
return { path: path, inlined: null, starts: null }
}
// Array of functions that operate on the underlying buffer.
const offset = buffered.length
const assignee = registers[registers.length - 1]
const inlined = []
// For each function defined by an inline.
for (const inline of inlines) {
// Read the initial register definition.
const $_ = registers[0]
// Positional arguments are simplified, less analysis because special
// features are indicated by named functions.
if (inline.properties.length == 0) {
const args = inline.vargs.concat([
$_, packet.name, variables.i ? '$i' : '[]', util.inspect(path.split('.')), util.inspect(direction)
]).slice(0, inline.arity).join(', ')
if (~inline.defaulted.indexOf(inline.vargs.length)) {
inlined.push($(`
; (`, inline.source, `)(${$_})
`))
} else {
if (registers.length != 1) {
registers.shift()
}
inlined.push($(`
${assignee} = (`, inline.source, `)(${args})
`))
}
} else {
const { properties, is } = _properties(path, inline, $_)
if (is.buffered) {
if (is.transform) {
throw new Error
} else {
buffered.push({
start: buffered.length,
properties: inline.properties,
source: $(`
; (`, inline.source, `)(`, properties, `)
`)
})
}
} else {
if (! is.transform) {
inlined.push($(`
; (`, inline.source, `)(`, properties, `)
`))
} else {
if (registers.length != 1) {
registers.shift()
}
inlined.push($(`
${assignee} = (`, inline.source, `)(`, properties, `)
`))
}
}
}
}
const entry = { offset: offset, length: buffered.length - offset }
const starts = []
for (let i = entry.offset, I = entry.offset + entry.length; i < I; i++) {
starts.push(`$starts[${i}] = $start`)
}
stack.push(entry)
return {
path: registers[0],
inlined: inlined.length != 0 ? join(inlined) : null,
starts: starts.length != 0 ? starts.join('\n') : null,
}
}
function exit () {
return buffered.length != 0
? buffered.map(buffered => buffered.source).join('\n')
: null
}
function pop () {
const popped = stack.pop()
if (direction == 'serialize') {
$$--
}
const spliced = buffered
.splice(popped.offset, popped.length)
.map(buffered => buffered.source)
return spliced.length != 0 ? spliced.join('\n') : null
}
return {
accumulations: accumulations,
accumulator: accumulator,
test: test,
inline: inline,
exit: exit,
pop: pop
}
}