-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (27 loc) · 1.05 KB
/
index.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
(async () => {
// Imports
var fm = require("formality-lang");
var VM = require('ethereumjs-vm').default;
var vm = new VM();
var BN = require('bn.js');
var fs = require("fs");
// An example Formality program
var {defs} = await fm.parse(fs.readFileSync("./main.fm", "utf8"));
// Compiles term to EVM bytecode
var bytes = fm.evm.compile("main/main", defs);
var code = Buffer.from(bytes, "hex");
// Reduces term on EVM
var evmResult = await vm.runCode({code, gasLimit: new BN(0xFFFFFFFF)});
// Gets result from the Ethereum memory
var evmNorm = fm.evm.decompile(evmResult.runState.memory._store);
// Also reduces on Formality's optimal-mode, for comparison purposes
var optResult = fm.optimal.normal("main/main", defs);
// Prints Ethereum gas cost
console.log("evmNorm : " + fm.stringify(evmNorm));
console.log("evmCost : " + evmResult.gasUsed.toString() + " gas");
console.log("");
// Prints INet graph-rewrite cost
console.log("optNorm : " + fm.stringify(optResult.term));
console.log("optCost : " + optResult.stats.rwts + " graph-rewrites");
console.log("");
})();