Skip to content

Commit eda9734

Browse files
committed
Make the php & lua startup bytecode a bit prettier
1 parent d04a885 commit eda9734

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎write-lua-bytecode.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escap
101101
}) + '"';
102102
};
103103

104+
var luaName = function(bc) {
105+
var name = bc.name.toUpperCase();
106+
if (name==='2DUP') { return 'DUP2'; }
107+
return name;
108+
};
104109
var pad = function(s, width) {
105110
if (width===undefined) { width = 10; }
106111
while (s.length < width) { s += ' '; }
@@ -111,6 +116,7 @@ define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escap
111116
console.log('-- generated by TurtleScript write-lua-bytecode.js');
112117
console.log('local jsval = require("luaturtle.jsval")');
113118
console.log('local ifunc = require("luaturtle.ifunc")');
119+
console.log('local ops = require("luaturtle.ops")');
114120
console.log('');
115121
console.log('local startup = {}');
116122
console.log('');
@@ -137,14 +143,15 @@ define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escap
137143
var pc = j;
138144
var bc = bytecode_table.for_num(f.bytecode[j]);
139145
var a = f.bytecode.slice(j, j+=bc.args+1);
140-
a = a.map(function(b) {
146+
a = a.map(function(b, i) {
147+
if (i===0) { return 'ops.' + luaName(bc); }
141148
if (typeof(b) !== 'number') { b = b.label; }
142149
return ''+b;
143150
});
144151
var b = a.slice(1);
145152
a = a.join(', ') + ((j < f.bytecode.length) ? ',' : '');
146153
b = bc.name + ( b.length ? ( '(' + b.join(',') + ')' ) : '' );
147-
console.log(' ' + pad(a) + '-- ' + pc + ': ' + b);
154+
console.log(' ' + pad(a, 25) + '-- ' + pc + ': ' + b);
148155
}
149156
console.log(' }');
150157
console.log(' }' + comma(i));

‎write-php-bytecode.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escap
7777
}) + '"';
7878
};
7979

80+
var phpName = function(bc) {
81+
var name = bc.name.toUpperCase();
82+
if (name==='2DUP') { return 'DUP2'; }
83+
return name;
84+
};
8085
var pad = function(s, width) {
8186
if (width===undefined) { width = 10; }
8287
while (s.length < width) { s += ' '; }
@@ -115,14 +120,15 @@ define(['./parse', './bcompile', './bytecode-table', './top-level', './str-escap
115120
var pc = j;
116121
var bc = bytecode_table.for_num(f.bytecode[j]);
117122
var a = f.bytecode.slice(j, j+=bc.args+1);
118-
a = a.map(function(b) {
123+
a = a.map(function(b, i) {
124+
if (i===0) { return 'Op::' + phpName(bc); }
119125
if (typeof(b) !== 'number') { b = b.label; }
120126
return ''+b;
121127
});
122128
var b = a.slice(1);
123129
a = a.join(', ') + ((j < f.bytecode.length) ? ',' : '');
124130
b = bc.name + ( b.length ? ( '(' + b.join(',') + ')' ) : '' );
125-
console.log('\t\t\t\t' + pad(a) + '// ' + pc + ': ' + b);
131+
console.log('\t\t\t\t' + pad(a, 25) + '// ' + pc + ': ' + b);
126132
}
127133
console.log('\t\t\t] );');
128134
});

0 commit comments

Comments
 (0)