2023-10-18 05:14:24 -05:00
|
|
|
mod utils;
|
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
use {
|
2024-09-16 08:27:38 -05:00
|
|
|
crate::utils::IterExt,
|
|
|
|
std::{
|
|
|
|
fs::File,
|
|
|
|
io::{self, BufRead, BufReader, BufWriter, Seek, Write},
|
|
|
|
path::Path,
|
|
|
|
},
|
2024-07-08 00:22:53 -05:00
|
|
|
};
|
2023-10-18 05:14:24 -05:00
|
|
|
|
2024-07-08 00:27:40 -05:00
|
|
|
fn root() -> &'static Path {
|
|
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap()
|
|
|
|
}
|
2023-10-18 05:14:24 -05:00
|
|
|
|
2024-10-12 06:07:49 -05:00
|
|
|
fn build_cmd(cmd: impl AsRef<str>) -> std::process::Command {
|
2024-10-10 08:48:08 -05:00
|
|
|
let mut args = cmd.as_ref().split_whitespace();
|
|
|
|
let mut c = std::process::Command::new(args.next().unwrap());
|
|
|
|
for arg in args {
|
|
|
|
c.arg(arg);
|
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
c
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec(mut cmd: std::process::Command) -> io::Result<()> {
|
|
|
|
if !cmd.status()?.success() {
|
|
|
|
return Err(io::Error::other(format!("command failed: {:?}", cmd)));
|
2024-10-10 08:48:08 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-12 06:07:49 -05:00
|
|
|
fn build_wasm_blob(name: &str, debug: bool) -> io::Result<()> {
|
|
|
|
let mut c = build_cmd(if debug { "cargo wasm-build-debug" } else { "cargo wasm-build" });
|
|
|
|
c.arg(format!("wasm-{name}"));
|
|
|
|
exec(c)?;
|
2024-10-12 08:04:58 -05:00
|
|
|
let profile = if debug { "small-dev" } else { "small" };
|
|
|
|
let out_path = format!("target/wasm32-unknown-unknown/{profile}/wasm_{name}.wasm");
|
2024-10-12 06:07:49 -05:00
|
|
|
if !debug {
|
|
|
|
exec(build_cmd(format!("wasm-opt -Oz {out_path} -o {out_path}")))?;
|
|
|
|
}
|
2024-10-12 08:04:58 -05:00
|
|
|
exec(build_cmd(format!("cp {out_path} depell/src/{name}.wasm")))?;
|
|
|
|
exec(build_cmd(format!("gzip -f depell/src/{name}.wasm")))?;
|
|
|
|
Ok(())
|
2024-10-12 06:07:49 -05:00
|
|
|
}
|
|
|
|
|
2024-09-16 08:27:38 -05:00
|
|
|
fn main() -> io::Result<()> {
|
2024-10-10 08:48:08 -05:00
|
|
|
let args = std::env::args().skip(1).collect::<Vec<_>>();
|
2024-09-16 08:27:38 -05:00
|
|
|
match args[0].as_str() {
|
|
|
|
"fmt" => fmt(args[1] == "-r" || args[1] == "--renumber"),
|
2024-10-12 06:07:49 -05:00
|
|
|
"build-depell-debug" => {
|
|
|
|
build_wasm_blob("hbfmt", true)?;
|
|
|
|
build_wasm_blob("hbc", true)?;
|
2024-10-12 08:04:58 -05:00
|
|
|
exec(build_cmd("gzip -k -f depell/src/index.js"))?;
|
|
|
|
exec(build_cmd("gzip -k -f depell/src/index.css"))?;
|
|
|
|
exec(build_cmd("cargo run -p depell --features gzip"))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"build-depell" => {
|
|
|
|
build_wasm_blob("hbfmt", false)?;
|
|
|
|
build_wasm_blob("hbc", false)?;
|
|
|
|
exec(build_cmd("gzip -k -f depell/src/index.js"))?;
|
|
|
|
exec(build_cmd("gzip -k -f depell/src/index.css"))?;
|
|
|
|
exec(build_cmd("cargo run -p depell --features gzip --release"))?;
|
2024-10-10 08:48:08 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-09-16 08:27:38 -05:00
|
|
|
_ => Ok(()),
|
|
|
|
}
|
2023-10-18 05:14:24 -05:00
|
|
|
}
|
|
|
|
|
2024-09-16 08:27:38 -05:00
|
|
|
pub fn fmt(renumber: bool) -> io::Result<()> {
|
|
|
|
let mut file = File::options()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
2024-10-10 08:48:08 -05:00
|
|
|
.open(crate::root().join("bytecode/instructions.in"))?;
|
2023-10-18 05:14:24 -05:00
|
|
|
|
2024-09-16 08:27:38 -05:00
|
|
|
// Extract records
|
|
|
|
let reader = BufReader::new(&file);
|
|
|
|
let mut recs = vec![];
|
|
|
|
let mut lens = [0_usize; 4];
|
|
|
|
|
|
|
|
for rec in reader.split(b';').filter_map(|r| {
|
|
|
|
r.map(|ln| {
|
|
|
|
let s = String::from_utf8_lossy(&ln);
|
|
|
|
let s = s.trim_matches('\n');
|
|
|
|
if s.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.split(',').map(|s| Box::<str>::from(s.trim())).collect_array::<4>().map(Ok::<_, ()>)
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}) {
|
|
|
|
let rec = rec?.expect("Valid record format");
|
|
|
|
for (current, next) in lens.iter_mut().zip(rec.iter()) {
|
|
|
|
*current = (*current).max(next.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
recs.push(rec);
|
2023-10-18 05:14:24 -05:00
|
|
|
}
|
2024-09-16 08:27:38 -05:00
|
|
|
|
|
|
|
// Clear file!
|
|
|
|
file.set_len(0)?;
|
|
|
|
file.seek(std::io::SeekFrom::Start(0))?;
|
|
|
|
|
|
|
|
let mut writer = BufWriter::new(file);
|
|
|
|
|
|
|
|
let ord_opco_len = digit_count(recs.len()) as usize;
|
|
|
|
for (n, rec) in recs.iter().enumerate() {
|
|
|
|
// Write opcode number
|
|
|
|
if renumber {
|
|
|
|
let n = format!("{n:#04X}");
|
|
|
|
write!(writer, "{n}, {}", padding(ord_opco_len, &n))?;
|
|
|
|
} else {
|
|
|
|
write!(writer, "{}, {}", rec[0], padding(lens[0], &rec[0]))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write other fields
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"{}, {}{},{} {}{};",
|
|
|
|
rec[1],
|
|
|
|
padding(lens[1], &rec[1]),
|
|
|
|
rec[2],
|
|
|
|
padding(lens[2], &rec[2]),
|
|
|
|
rec[3],
|
|
|
|
padding(lens[3], &rec[3]),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn padding(req: usize, s: &str) -> Box<str> {
|
|
|
|
" ".repeat(req.saturating_sub(s.len())).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn digit_count(n: usize) -> u32 {
|
|
|
|
n.checked_ilog10().unwrap_or(0) + 1
|
2023-10-18 05:14:24 -05:00
|
|
|
}
|