-
Notifications
You must be signed in to change notification settings - Fork 20
/
text2json.js
56 lines (46 loc) · 1.12 KB
/
text2json.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
const immediates = require('./immediates.json')
module.exports = (text) => {
const json = []
const textArray = text.split(/\s|\n/)
while (textArray.length) {
const textOp = textArray.shift()
const jsonOp = {}
let [type, name] = textOp.split('.')
if (name === undefined) {
name = type
} else {
jsonOp.return_type = type
}
jsonOp.name = name
const immediate = immediates[jsonOp.name === 'const' ? jsonOp.return_type : jsonOp.name]
if (immediate) {
jsonOp.immediates = immediataryParser(immediate, textArray)
}
json.push(jsonOp)
}
return json
}
function immediataryParser (type, txt) {
const json = {}
switch (type) {
case 'br_table':
const dests = []
while (1) {
let dest = txt[0]
if (isNaN(dest)) break
txt.shift()
dests.push(dest)
}
return dests
case 'call_indirect':
json.index = txt.shift()
json.reserved = 0
return json
case 'memory_immediate':
json.flags = txt.shift()
json.offset = txt.shift()
return json
default:
return txt.shift()
}
}