-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.js
216 lines (197 loc) · 5 KB
/
Loader.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
const { SubtrackParser } = require('./Track')
const { TmSetting, TmObject } = require('./Object')
const methodTypes = [
'proGlobal',
'proMerge',
'epiNote',
'epiTrack'
]
class TmLoader {
/**
* Tm Library Loader
* @param syntax - Thulium Syntax Object
*/
constructor(syntax) {
this.Package = new TmPackage(syntax.Code, syntax.Dict)
this.loadTypes(syntax.Types)
this.loadChord(syntax.Chord)
this.loadPlugin(syntax.Class)
this.loadMeta(syntax.Meta)
this.Track = {}
}
loadMeta(meta) {
const preserved = [], initial = {}
for (const attr in meta) {
if (meta[attr].preserve) preserved.push(attr)
if (meta[attr].initial) {
initial[attr] = meta[attr].initial
}
}
this.Meta = {
Preserved: preserved,
Initial: initial
}
}
loadTypes(types) {
const result = {}
for (const type in types) {
result[type] = {
preserve: types[type].preserve,
class: types[type].class
}
}
this.Types = result
}
loadChord(dict) {
const result = {}
dict.forEach(chord => {
result[chord.Notation] = chord.Pitches
})
this.Chord = result
}
loadPlugin(plugins) {
this.Notation = function() {
const result = {}
for (const plugin of plugins) {
result[plugin.Name] = []
}
return result
}
methodTypes.forEach(method => {
const candidates = []
plugins.forEach(plugin => {
if (method in plugin) {
candidates.push(plugin[method])
}
})
this[method] = function(thisArg, ...rest) {
candidates.forEach(func => func.call(thisArg, ...rest))
}
})
}
}
class TmPackage {
constructor(source, dict) {
/* eslint-disable-next-line no-new-func */
this.Dict = new Function(`${source}
return {${dict.map(func => func.Name).join(',')}};
`)()
}
applyFunction(parser, token) {
const API = new TmAPI(parser, token, this.Dict)
return this.Dict[token.Name].apply(API, TmPackage.getArguments(token.Args))
}
static getArguments(args) {
return args.map(arg => {
switch (arg.Type) {
case 'Number':
case 'String':
case 'Array':
return arg.Content
case 'Expression':
// FIXME: using expression parser
/* eslint-disable-next-line no-eval */
return eval(arg.Content.replace(/Log2/g, 'Math.log2'))
default:
return {
Type: 'Subtrack',
Content: [arg]
}
}
})
}
}
const Protocols = {
Default: {
Read: ['PitchQueue'],
Write: ['PitchQueue']
}
}
const NativeMethods = [
'mergeMeta',
'isLegalBar',
'pushError'
]
class TmAPI {
/**
* Thulium API
* @param {TmParser} parser - Thulium Parser Object
* @param {TmToken} token - Function Token
* @param {TmPackageDict} dict - Map of Functions
*/
constructor(parser, token, dict) {
Object.assign(this, parser)
this.Token = token
this.Function = new Proxy({}, {
get: (_, name) => dict[name]
})
for (const method of NativeMethods) {
this[method] = parser[method]
}
}
newSettings(settings = {}) {
return new TmSetting(settings)
}
ParsePlainTrack(track, { Protocol = 'Default', Settings = null } = {}) {
const data = this.ParseTrack(track, { Protocol, Settings })
const result = data[0]
result.Meta.Duration = Math.max(...data.map(track => track.Meta.Duration))
// FIXME: error handling
result.Content.concat(...data.slice(1).map(track => track.Content))
return result
}
ParseTrack(track, { Protocol = 'Default', Settings = null } = {}) {
if (track === undefined) {
track = { Type: 'Subtrack', Content: [] }
}
return new SubtrackParser(
track,
Settings === null ? this.Settings : this.Settings.extend(Settings),
this.Library,
TmAPI.wrap(this.Meta, Protocol)
).parse()
}
ReportError(name, args) {
if (!name.includes('::')) {
name = 'Func::' + this.Token.Name + '::' + name
}
this.pushError(name, args)
}
JoinTrack(src1, ...rest) {
const result = {
Meta: Object.assign(src1.Meta),
Content: src1.Content.slice(),
Warnings: src1.Warnings.slice(),
Settings: this.Settings,
pushError: this.pushError,
isLegalBar: this.isLegalBar
}
for (let src of rest) {
result.Content.push(...src.Content.map(note => {
return Object.assign({}, note, {
StartTime: note.StartTime + result.Meta.Duration
})
}))
this.Meta.Duration += src.Meta.Duration
this.mergeMeta(result, src)
}
return result
}
static wrap(meta, protocol) {
const protocolList = Protocols[protocol]
return new Proxy(meta, {
get(obj, prop) {
if (protocolList.Read.includes(prop)) {
return obj[prop]
}
return null
},
set(obj, prop, val) {
if (protocolList.Write.includes(prop)) {
obj[prop] = val
}
}
})
}
}
module.exports = TmLoader