diff --git a/Cargo.lock b/Cargo.lock index 70333d6..c2f3772 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,147 @@ version = 4 [[package]] name = "automata" version = "0.1.0" + +[[package]] +name = "automata-web" +version = "0.1.0" +dependencies = [ + "automata", + "console_error_panic_hook", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "bumpalo" +version = "3.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "js-sys" +version = "0.3.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "proc-macro2" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "syn" +version = "2.0.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "wasm-bindgen" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b32828d774c412041098d182a8b38b16ea816958e07cf40eec2bc080ae137ac" +dependencies = [ + "js-sys", + "wasm-bindgen", +] diff --git a/Cargo.toml b/Cargo.toml index b7008b4..2f19bd1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] + + +[workspace] +members = ["web"] \ No newline at end of file diff --git a/default.nix b/default.nix index 579ba87..50ae5c0 100644 --- a/default.nix +++ b/default.nix @@ -5,6 +5,9 @@ # Replace llvmPackages with llvmPackages_X, where X is the latest LLVM version (at the time of writing, 16) llvmPackages.bintools rustup + wasm-bindgen-cli_0_2_100 + wasm-pack + binaryen ]; RUSTC_VERSION = "nightly"; # https://github.com/rust-lang/rust-bindgen#environment-variables diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 2b4b0e8..0000000 --- a/src/main.rs +++ /dev/null @@ -1,37 +0,0 @@ -use automata::automata::npda; - -fn main() { - let input = include_str!("../example.npda"); - - let table = match npda::TransitionTable::load_table(input) { - Ok((ok, logs)) => { - for log in logs.displayable() { - println!("{log}") - } - ok - } - Err(logs) => { - for log in logs.displayable() { - println!("{log}") - } - return; - } - }; - - let input = "aababaab"; - println!("running on: '{input}'"); - let mut simulator = npda::Simulator::begin(input, table); - loop { - match simulator.step(){ - npda::SimulatorResult::Pending => {}, - npda::SimulatorResult::Reject => { - println!("REJECTED"); - break; - }, - npda::SimulatorResult::Accept(npda) => { - println!("ACCEPT: {npda:?}"); - break; - }, - } - } -} diff --git a/web/Cargo.toml b/web/Cargo.toml new file mode 100644 index 0000000..a2c1580 --- /dev/null +++ b/web/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "automata-web" +version = "0.1.0" +edition = "2024" + +[dependencies] +automata = {path=".."} +console_error_panic_hook = "0.1.7" +wasm-bindgen = "*" +web-sys = { version = "0.3.83", features = ["Window", "Document", "HtmlElement", "Text"] } diff --git a/web/build.sh b/web/build.sh new file mode 100755 index 0000000..14256f9 --- /dev/null +++ b/web/build.sh @@ -0,0 +1,15 @@ + + +CRATE_NAME="automata-web" +TARGET_NAME="automata-web" +OUT_FILE_NAME="./root/automata.wasm" +mkdir root +TARGET="../target" + + +cargo build --package automata-web --target wasm32-unknown-unknown --release +TARGET_NAME="${CRATE_NAME}.wasm" +WASM_PATH="${TARGET}/wasm32-unknown-unknown/release/$TARGET_NAME" + +wasm-bindgen ${WASM_PATH} --out-dir root --out-name ${OUT_FILE_NAME} --no-modules --no-typescript +wasm-opt ${OUT_FILE_NAME} -O2 --fast-math -g -o ${OUT_FILE_NAME} \ No newline at end of file diff --git a/web/init.sh b/web/init.sh new file mode 100755 index 0000000..fd27bb4 --- /dev/null +++ b/web/init.sh @@ -0,0 +1,2 @@ +cargo install wasm-bindgen-cli +rustup target add wasm32-unknown-unknown \ No newline at end of file diff --git a/web/src/main.rs b/web/src/main.rs new file mode 100644 index 0000000..1b849a3 --- /dev/null +++ b/web/src/main.rs @@ -0,0 +1,51 @@ +use automata::automata::npda; + +use web_sys::window; + +fn main() { + console_error_panic_hook::set_once(); + + let document = window() + .and_then(|win| win.document()) + .expect("Could not access the document"); + let body = document.body().expect("Could not access document.body"); + let text_node = document.create_text_node("Hello, world from Vanilla Rust!"); + body.append_child(text_node.as_ref()) + .expect("Failed to append text"); +} + +// pub fn main() { +// let input = include_str!("../../example.npda"); + +// let table = match npda::TransitionTable::load_table(input) { +// Ok((ok, logs)) => { +// for log in logs.displayable() { +// println!("{log}") +// } +// ok +// } +// Err(logs) => { +// for log in logs.displayable() { +// println!("{log}") +// } +// return; +// } +// }; + +// let input = "aababaab"; +// println!("running on: '{input}'"); +// let mut simulator = npda::Simulator::begin(input, table); +// loop { +// match simulator.step(){ +// npda::SimulatorResult::Pending => {}, +// npda::SimulatorResult::Reject => { +// println!("REJECTED"); +// break; +// }, +// npda::SimulatorResult::Accept(npda) => { +// println!("ACCEPT: {npda:?}"); +// break; +// }, +// } +// } +// }