Skip to content
This repository has been archived by the owner on Jul 4, 2022. It is now read-only.

feat: Allow parsing/compiling of content only data without files and include declaration files #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dist/bin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=bin.d.ts.map
1 change: 1 addition & 0 deletions dist/bin.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions dist/bin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var commander_1 = require("commander");
var _1 = require("./");
var _1 = __importDefault(require("./"));
var fs = require("fs");
// Compiler Version
var version = "2.0.0";
var version = "2.1.0";
// Define terminal commands.
commander_1.program.name("huffc");
commander_1.program.version(version);
Expand All @@ -14,15 +17,16 @@ commander_1.program
.option("--output-directory <output-dir>", "The output directory", "./")
.option("--bytecode", "Generate and log bytecode", false)
.option("-o, output", "The output file")
.option("-p, --paste", "Paste the output to the terminal");
.option("-p, --paste", "Paste the output to the terminal")
.option("-n, --no-linebreak", "Omit newline charater");
// Parse the terminal arguments.
commander_1.program.parse(process.argv);
var options = commander_1.program.opts();
var files = commander_1.program.args;
var destination = options.outputDir || ".";
var destination = options.outputDirectory || ".";
// Abort the program.
var abort = function (msg) {
console.error(msg || "Error occured");
process.stderr.write("".concat(msg, "\n") || "Error occured\n");
process.exit(1);
};
// Iterate the imported files.
Expand All @@ -32,16 +36,20 @@ files.forEach(function (file) {
abort("File extension must be .huff");
// Compile the file.
var result = (0, _1["default"])({
kind: "file",
filePath: file,
generateAbi: true
});
// If the user has specified an output file, write the output to the file.
var outputPath = "".concat(options.outputDirectory).concat(files[0].replace(".huff", ".json"));
var outputPath = "".concat(destination).concat(files[0].replace(".huff", ".json"));
if (options.output)
fs.writeFileSync(outputPath, result.abi);
// If the user has specified for us to log the bytecode, log it.
if (options.bytecode && !options.paste)
console.log(result.bytecode);
if (options.bytecode && !options.paste) {
process.stdout.write(result.bytecode);
if (options.linebreak)
process.stdout.write('\n');
}
if (options.paste)
console.log(result);
process.stdout.write("".concat(JSON.stringify(result), "\n"));
});
11 changes: 11 additions & 0 deletions dist/compiler/compiler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Definitions } from "../parser/utils/types";
/**
* Compile a macro into raw EVM bytecode.
* @param name The name of the macro.
* @param args An array of arguments passed into the macro.
* @param macros Maps all macros to their raw text and arguments.
* @param constants Maps all constants to their values.
* @param jumptables Maps all jump tables to their jump values.
*/
export declare const compileMacro: (name: string, args: string[], macros: Definitions["data"], constants: Definitions["data"], jumptables: Definitions["data"]) => any;
//# sourceMappingURL=compiler.d.ts.map
1 change: 1 addition & 0 deletions dist/compiler/compiler.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions dist/compiler/processor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Definitions } from "../parser/utils/types";
/**
* Process a macro, generating semi-complete EVM bytecode.
* @param name The name of the macro.
* @param bytecodeOffset The offset of the macro's bytecode.
* @param args The arguments passed into the array.
* @param macros Maps all macros to their raw text and arguments.
* @param constants Maps all constants to their values.
* @param jumptables Maps all jump tables to their jump values.
* @returns Semi-complete EVM bytecode.
*/
export declare const processMacro: (name: string, bytecodeOffset: number, args: string[], macros: Definitions["data"], constants: Definitions["data"], jumptables: Definitions["data"]) => any;
//# sourceMappingURL=processor.d.ts.map
1 change: 1 addition & 0 deletions dist/compiler/processor.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion dist/compiler/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.processMacro = void 0;
var opcodes_1 = require("../evm/opcodes");
var opcodes_1 = __importDefault(require("../evm/opcodes"));
var macros_1 = require("../parser/macros");
var types_1 = require("../parser/utils/types");
var bytes_1 = require("../utils/bytes");
Expand Down
147 changes: 147 additions & 0 deletions dist/evm/opcodes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
declare const _default: {
stop: string;
add: string;
mul: string;
sub: string;
div: string;
sdiv: string;
mod: string;
smod: string;
addmod: string;
mulmod: string;
exp: string;
signextend: string;
lt: string;
gt: string;
slt: string;
sgt: string;
eq: string;
iszero: string;
and: string;
or: string;
xor: string;
not: string;
byte: string;
sha3: string;
keccak: string;
address: string;
balance: string;
origin: string;
caller: string;
callvalue: string;
calldataload: string;
calldatasize: string;
calldatacopy: string;
codesize: string;
codecopy: string;
gasprice: string;
extcodesize: string;
extcodecopy: string;
returndatasize: string;
returndatacopy: string;
blockhash: string;
coinbase: string;
timestamp: string;
number: string;
difficulty: string;
gaslimit: string;
chainid: string;
selfbalance: string;
basefee: string;
pop: string;
mload: string;
mstore: string;
mstore8: string;
sload: string;
sstore: string;
jump: string;
jumpi: string;
getpc: string;
msize: string;
gas: string;
jumpdest: string;
push1: string;
push2: string;
push3: string;
push4: string;
push5: string;
push6: string;
push7: string;
push8: string;
push9: string;
push10: string;
push11: string;
push12: string;
push13: string;
push14: string;
push15: string;
push16: string;
push17: string;
push18: string;
push19: string;
push20: string;
push21: string;
push22: string;
push23: string;
push24: string;
push25: string;
push26: string;
push27: string;
push28: string;
push29: string;
push30: string;
push31: string;
push32: string;
dup1: string;
dup2: string;
dup3: string;
dup4: string;
dup5: string;
dup6: string;
dup7: string;
dup8: string;
dup9: string;
dup10: string;
dup11: string;
dup12: string;
dup13: string;
dup14: string;
dup15: string;
dup16: string;
swap1: string;
swap2: string;
swap3: string;
swap4: string;
swap5: string;
swap6: string;
swap7: string;
swap8: string;
swap9: string;
swap10: string;
swap11: string;
swap12: string;
swap13: string;
swap14: string;
swap15: string;
swap16: string;
shl: string;
shr: string;
sar: string;
log0: string;
log1: string;
log2: string;
log3: string;
log4: string;
create: string;
call: string;
callcode: string;
return: string;
delegatecall: string;
create2: string;
staticcall: string;
revert: string;
invalid: string;
selfdestruct: string;
};
export default _default;
//# sourceMappingURL=opcodes.d.ts.map
1 change: 1 addition & 0 deletions dist/evm/opcodes.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions dist/evm/opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ exports["default"] = {
difficulty: "44",
gaslimit: "45",
chainid: "46",
selfbalance: "47",
basefee: "48",
pop: "50",
mload: "51",
mstore: "52",
Expand Down Expand Up @@ -93,21 +95,6 @@ exports["default"] = {
push30: "7d",
push31: "7e",
push32: "7f",
log0: "a0",
log1: "a1",
log2: "a2",
log3: "a3",
log4: "a4",
create: "f0",
call: "f1",
callcode: "f2",
"return": "f3",
delegatecall: "f4",
staticcall: "fa",
create2: "fb",
revert: "fd",
invalid: "fe",
selfdestruct: "ff",
dup1: "80",
dup2: "81",
dup3: "82",
Expand Down Expand Up @@ -143,6 +130,19 @@ exports["default"] = {
shl: "1b",
shr: "1c",
sar: "1d",
rol: "1e",
ror: "1f"
log0: "a0",
log1: "a1",
log2: "a2",
log3: "a3",
log4: "a4",
create: "f0",
call: "f1",
callcode: "f2",
"return": "f3",
delegatecall: "f4",
create2: "f5",
staticcall: "fa",
revert: "fd",
invalid: "fe",
selfdestruct: "ff"
};
14 changes: 14 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { HuffCompilerArgs } from "./types";
/**
* Compile a Huff file.
* @param filePath The path to the file.
* @param args An array containing the arguments to the macro.
* @returns The compiled bytecode.
*/
declare const compile: (args: HuffCompilerArgs) => {
bytecode: string;
runtimeBytecode: any;
abi: string;
};
export default compile;
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
exports.__esModule = true;
var abi_1 = require("@ethersproject/abi");
var bytes_1 = require("./utils/bytes");
var compiler_1 = require("./compiler/compiler");
var high_level_1 = require("./parser/high-level");
var ethers_1 = require("ethers");
var output_1 = require("./output");
/**
* Compile a Huff file.
Expand All @@ -13,7 +13,7 @@ var output_1 = require("./output");
*/
var compile = function (args) {
// Parse the file and generate definitions.
var _a = (0, high_level_1.parseFile)(args.filePath), macros = _a.macros, constants = _a.constants, tables = _a.tables, functions = _a.functions, events = _a.events;
var _a = (0, high_level_1.parse)(args), macros = _a.macros, constants = _a.constants, tables = _a.tables, functions = _a.functions, events = _a.events;
// Generate the contract ABI.
var abi = args.generateAbi ? (0, output_1.generateAbi)(functions, events) : "";
// Set storage pointer constants.
Expand Down Expand Up @@ -48,7 +48,7 @@ var compile = function (args) {
pushContractSizeCode = "61".concat(contractSize);
}
// Compute pushX(offset to code)
if ((bootStrapCodeSize + constructorLength) < 256) {
if (bootStrapCodeSize + constructorLength < 256) {
// Convert the size and offset to bytes.
var contractCodeOffset = (0, bytes_1.padNBytes)((0, bytes_1.toHex)(bootStrapCodeSize + constructorLength), 1);
// push1(offset to code)
Expand All @@ -66,6 +66,7 @@ var compile = function (args) {
var bootstrapCode = "".concat(pushContractSizeCode, "80").concat(pushContractCodeOffset, "3d393df3");
var constructorCode = "".concat(constructorBytecode).concat(bootstrapCode);
var deployedBytecode = "".concat(constructorCode).concat(mainBytecode).concat(args.constructorArgs ? encodeArgs(args.constructorArgs) : "");
console.log(deployedBytecode);
// Return the bytecode.
return { bytecode: deployedBytecode, runtimeBytecode: mainBytecode, abi: abi };
};
Expand All @@ -84,8 +85,7 @@ function encodeArgs(args) {
values.push(arg.value);
});
// Encode and array the types and values.
var abiCoder = new ethers_1.ethers.utils.AbiCoder();
return abiCoder.encode(types, values).replace(/^(0x)/, "");
return abi_1.defaultAbiCoder.encode(types, values).replace(/^(0x)/, "");
}
// Export compiler function as default.
exports["default"] = compile;
10 changes: 10 additions & 0 deletions dist/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Definitions } from "../parser/utils/types";
/**
* Generate a contract ABI
* @param path The path to write the ABI to.
* @param functions Function definitions map.
* @param events Event definitions map.
* @returns Contract ABI.
*/
export declare const generateAbi: (functions: Definitions["data"], events: Definitions["data"]) => string;
//# sourceMappingURL=index.d.ts.map
Loading