-
Notifications
You must be signed in to change notification settings - Fork 2
/
write-lua-ops.js
42 lines (38 loc) · 1.27 KB
/
write-lua-ops.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
// Utility to write out the opcode mapping from `bytecode-table.js`
// as a Lua file
//
// Run it under `node` with the CLI in `bin/write-lua-ops.js`
define(['./bytecode-table'], function(bytecode_table) {
var bops = [];
while(true) {
var bc = bytecode_table.for_num(bops.length);
if (!bc) { break; }
bops.push(bc);
}
var comma = function(i) { return (i < (bops.length-1)) ? ',' : ''; };
var luaName = function(bc) {
var name = bc.name.toUpperCase();
if (name==='2DUP') { return 'DUP2'; }
return name;
};
console.log('-- generated by TurtleScript write-lua-ops.js');
console.log('');
console.log('local ops = {}');
console.log('');
// ## Emit `Op` enumeration.
console.log('ops.byname = {');
bops.forEach(function(bc, i) {
console.log(' ["' + luaName(bc) + '"] = ' + i + comma(i));
});
console.log('}');
console.log('');
console.log('ops.bynum = {}');
console.log('');
console.log('-- invert byname into bynum, and combine both into root export');
console.log('for name,val in pairs(ops.byname) do');
console.log(' ops.bynum[val] = name');
console.log(' ops[name] = val');
console.log('end');
console.log('');
console.log('return ops');
});