Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
clap = { version = "4.0", features = ["derive"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Contributions welcome!
- [ ] Reassign jumps
- [ ] Handle different inputs
- [ ] Mnemonics
- [ ] Huff
- [x] Huff
- [ ] Return tips along with optimized bytecode

### Disclaimer
Expand Down
29 changes: 24 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<String, String> {
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) {
Expand Down
Loading