diff --git a/hm-asm-web/README.md b/hm-asm-web/README.md index 0624c27..955b4dd 100644 --- a/hm-asm-web/README.md +++ b/hm-asm-web/README.md @@ -5,6 +5,9 @@ two functions to javascript. - `simulate(code: &str, cycles: usize)`, its return value is equivalent to the one of `hm_asm_simulator::simulate::simulate` - `assemble(code: &str)`, its return vlaue is equivalent to the one of `hm_asm_simulator::generate::generate_binary` +If either of the functions should fail they will return their Rust error as a String. In the case of a grammar based +error, this would be a nice pest.rs error. + ## Demo In `demo/` You'll find a demo app that compiles and simulates a simple program inside of the console. You can test it with diff --git a/hm-asm-web/demo/index.js b/hm-asm-web/demo/index.js index 2882d26..dd9409b 100644 --- a/hm-asm-web/demo/index.js +++ b/hm-asm-web/demo/index.js @@ -2,8 +2,13 @@ import * as wasm from "hm-asm-web"; console.log("Welcome to hm-asm-web"); -const code = "LDA #1\nADD #3\nSTA (8)" -console.log("Compiling:\n" + code) +const code = "LDA #1\nADD #3\nSTA (8)"; +console.log("Compiling:\n" + code); console.log(wasm.assemble(code)); -console.log("Simulating for four clock cycles:\n" + code ) +console.log("Simulating for four clock cycles:\n" + code); console.log(wasm.simulate(code, 4)); + +const invalid = "LDA dkfljlsdkfjsdf"; +console.log("Compiling invalid code:\n" + invalid); +console.log(wasm.assemble(invalid)); +console.log("Hurray a nice error msg"); diff --git a/hm-asm-web/src/lib.rs b/hm-asm-web/src/lib.rs index d865634..56f949d 100644 --- a/hm-asm-web/src/lib.rs +++ b/hm-asm-web/src/lib.rs @@ -9,9 +9,10 @@ use hm_asm_simulator::{ #[wasm_bindgen] pub fn simulate(code: &str, cycles: usize) -> JsValue { - let instructions = parse_asm( - AsmParser::parse(Rule::program, &code).unwrap_or_else(|e| panic!("{}", e)), - ); + let instructions = parse_asm(match AsmParser::parse(Rule::program, &code) { + Ok(instructions) => instructions, + Err(e) => return JsValue::from_str(&format!("{}", e)) + }); let states = hm_asm_simulator::simulate::simulate(instructions, cycles); JsValue::from_serde(&states).unwrap() @@ -20,9 +21,12 @@ pub fn simulate(code: &str, cycles: usize) -> JsValue { #[wasm_bindgen] pub fn assemble(code: &str) -> JsValue { - let instructions = parse_asm( - AsmParser::parse(Rule::program, &code).unwrap_or_else(|e| panic!("{}", e)), - ); + let instructions = parse_asm(match AsmParser::parse(Rule::program, &code) { + Ok(instructions) => instructions, + Err(e) => return JsValue::from_str(&format!("{}", e)) + }); + + let binary = generate_binary(instructions); JsValue::from_serde(&binary).unwrap() }