diff --git a/Cargo.toml b/Cargo.toml index f8b26b6..34db540 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/kadenzipfel/bytepeep" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -strum = "0.24" +strum = { version = "0.24", features = ["derive"] } strum_macros = "0.24" colored = "2.0.0" -clap = { version = "4.0", features = ["derive"] } \ No newline at end of file +clap = { version = "4.0", features = ["derive"] } diff --git a/README.md b/README.md index 9232f4d..befb3c4 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Contributions welcome! - [ ] Reassign jumps - [ ] Handle different inputs - [ ] Mnemonics - - [ ] Huff + - [x] Huff - [ ] Return tips along with optimized bytecode ### Disclaimer diff --git a/src/main.rs b/src/main.rs index 1c1702e..c22958a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,15 @@ use colored::Colorize; -use std::{iter}; +use std::iter; use clap::Parser; -use crate::{assembler::*, checks::contains_jumps, disassembler::*, peephole::*, types::*}; +use crate::{ + assembler::*, + checks::contains_jumps, + disassembler::*, + peephole::*, + types::*, + utils::{Source, compile_huff}, +}; mod assembler; mod checks; @@ -15,16 +22,28 @@ mod utils; #[derive(Parser)] pub struct Cli { - bytecode: String + bytecode: String, + + #[clap(long, help = "Source type (raw/huff)", default_value = "raw")] + source: Source, } fn main() { let args: Cli = Cli::parse(); - let bytecode = &args.bytecode; + let bytecode = match args.source { + Source::Raw => args.bytecode.clone(), + Source::Huff => match compile_huff(&args.bytecode) { + Ok(code) => code, + Err(e) => { + eprintln!("Error: {}", e.red()); + std::process::exit(1); + } + } + }; println!("Bytecode: {}", bytecode); - let bytes: Bytecode = disassemble(bytecode); + let bytes: Bytecode = disassemble(&bytecode); let output_bytes = output(&bytes); let jump_warning: bool = contains_jumps(&bytes); diff --git a/src/utils.rs b/src/utils.rs index b5b4078..45044e5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,41 @@ use crate::evm::*; +use std::{path::Path, process::Command}; + +#[derive(Debug, Clone, strum::EnumString)] +#[strum(ascii_case_insensitive)] +pub enum Source { + Raw, + Huff, +} + +pub fn compile_huff(path: &str) -> Result { + if !Path::new(path).exists() { + return Err(format!("File not found: {}", path)); + } + + let output = Command::new("huffc") + .args(["-b", path]) + .output() + .map_err(|e| format!("Failed to compile the huff code: {}", e))?; + + if !output.status.success() { + return Err(format!("huffc failed: {}", + String::from_utf8_lossy(&output.stderr))); + } + + let output_str = String::from_utf8_lossy(&output.stdout); + + // Extract the bytecode from last line of the o/p + let bytecode = output_str + .lines() + .last() + .ok_or_else(|| "No output".to_string())? + .trim() + .trim_end_matches('%') + .to_string(); + + Ok(bytecode) +} // Find minimum viable length for pushdata pub fn min_pushdata_len(string: &String) -> (usize, String) {