-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource2evm.js
104 lines (93 loc) · 2.23 KB
/
source2evm.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
#! /usr/local/bin/node
const fs = require('fs')
const process = require('process');
const { exit } = require("process");
const execSync = require('child_process').execSync;
// check if compiled
if (!fs.existsSync("output/evm_alt.js")) {
console.log("Compiler file not found, compiling from source...");
execSync("yarn tsc");
console.log("Succesfully compiled! ");
}
const arch_type = require('os').arch();
let evm = "";
if (arch_type === 'x64') {
evm = "bin/evm";
} else if (arch_type === 'arm') {
evm = "bin/evm_arm"
} else{
console.log(arch_type + " not supported");
exit(1);
}
const compiler = require("./output/evm_alt");
const args = process.argv.slice(2);
let input_file = null;
let output_file = null;
let execute = false;
let debug = false;
const USAGE = 'node source2evm -i file [options]';
const HELP_MSG = USAGE + `
\n
Options: \n
-o Path to write compiled EVM bytecode to \n
-r Execute the compiled bytecode on EVM \n
-h Show this message \n
`;
let i = 0;
while (i < args.length) {
switch (args[i]) {
case '-i':
input_file = args[i + 1];
i = i + 2;
break;
case '-o':
output_file = args[i + 1];
i = i + 2;
break;
case '-r':
execute = true;
i = i + 1;
break;
case '-h':
console.log(HELP_MSG);
exit();
default:
console.log("Unknown input: " + args[i]);
console.log(USAGE);
exit();
}
}
if (input_file === null) {
console.log('No input provided');
console.log(USAGE);
exit(1);
}
try {
const data = fs.readFileSync(input_file, 'utf8')
console.log('Compiling...');
compiled = compiler.parse_and_compile(data);
if (output_file !== null) {
fs.writeFile(output_file, compiler, err => {
if (err) {
console.error(err)
return
}
console.log('Compiled bytecode written to ' + output_file);
});
} else if (!execute) {
console.log(compiled);
}
if (execute) {
let res = execSync(evm + (debug ? " --debug" : "") + " --code " + compiled + " run");
const output = Number(res);
if (isNaN(output)) {
console.log("EVM error!");
console.log(String(res));
} else {
console.log(output);
}
}
} catch (err) {
console.error(err)
exit(1);
}