-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3as.js
81 lines (68 loc) · 1.91 KB
/
p3as.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
require('@babel/register')({
presets: ['@babel/preset-env'],
});
let fs = require('fs');
let path = require('path');
let minimist = require('minimist');
let p3js = require('./src/p3js/');
let argv = minimist(process.argv.slice(2), {
string: ['o'],
boolean: ['show-refs'],
unknown(opt) {
if (opt[0] === '-') {
console.log("Unknown option '" + opt + "'.");
process.exit(1);
return false;
}
},
});
if (typeof argv._[0] === 'undefined') {
console.log('No input file.');
process.exit(2);
}
let input_file = path.normalize(argv._[0]);
let path_parts = path.parse(input_file);
let output_file = path.join(path_parts.dir, path_parts.name + '.exe');
if (typeof argv.o === 'string') {
output_file = path.normalize(argv.o);
}
let data;
let result;
let buffer;
try {
data = fs.readFileSync(input_file, 'utf8');
} catch (e) {
console.error(e.message);
process.exit(3);
}
console.log("Assembling '" + input_file + "'...");
try {
result = p3js.assembly.assembleWithDefaultValidator(data);
buffer = result.buildProgramCode();
} catch (e) {
if (e instanceof p3js.assembly.AssemblerError) {
console.error('Assembler Error: ' + e.getFullMessage());
} else {
console.error(e.message);
}
process.exit(4);
}
console.log('Statistics:');
console.log(' References: ' + result.labelCount);
console.log(' Pseudo Instructions: ' + result.pseudoCount);
console.log(' Instructions: ' + result.instructionCount);
console.log(' Memory: ' + result.getMemoryUsageString());
if (argv['show-refs']) {
console.log('References:');
Object.keys(result.labels).forEach((name) => {
console.log(' ' + name + ': ' + ('0000' + result.labels[name].toString(16)).substr(-4));
});
}
console.log("Writing program to '" + output_file + "'...");
try {
fs.writeFileSync(output_file, Buffer.from(new Uint8Array(buffer)));
} catch (e) {
console.error(e.message);
process.exit(5);
}
console.log('Done.');