hm-asm-web should not panic if it receives invalid asm code

This commit is contained in:
Henrik Böving
2021-03-05 19:59:16 +01:00
parent 3b49e98d97
commit f2a0966b17
3 changed files with 21 additions and 9 deletions

View File

@@ -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` - `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` - `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 ## Demo
In `demo/` You'll find a demo app that compiles and simulates a simple program inside of the console. In `demo/` You'll find a demo app that compiles and simulates a simple program inside of the console.
You can test it with You can test it with

View File

@@ -2,8 +2,13 @@ import * as wasm from "hm-asm-web";
console.log("Welcome to hm-asm-web"); console.log("Welcome to hm-asm-web");
const code = "LDA #1\nADD #3\nSTA (8)" const code = "LDA #1\nADD #3\nSTA (8)";
console.log("Compiling:\n" + code) console.log("Compiling:\n" + code);
console.log(wasm.assemble(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)); 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");

View File

@@ -9,9 +9,10 @@ use hm_asm_simulator::{
#[wasm_bindgen] #[wasm_bindgen]
pub fn simulate(code: &str, cycles: usize) -> JsValue { pub fn simulate(code: &str, cycles: usize) -> JsValue {
let instructions = parse_asm( let instructions = parse_asm(match AsmParser::parse(Rule::program, &code) {
AsmParser::parse(Rule::program, &code).unwrap_or_else(|e| panic!("{}", e)), Ok(instructions) => instructions,
); Err(e) => return JsValue::from_str(&format!("{}", e))
});
let states = hm_asm_simulator::simulate::simulate(instructions, cycles); let states = hm_asm_simulator::simulate::simulate(instructions, cycles);
JsValue::from_serde(&states).unwrap() JsValue::from_serde(&states).unwrap()
@@ -20,9 +21,12 @@ pub fn simulate(code: &str, cycles: usize) -> JsValue {
#[wasm_bindgen] #[wasm_bindgen]
pub fn assemble(code: &str) -> JsValue { pub fn assemble(code: &str) -> JsValue {
let instructions = parse_asm( let instructions = parse_asm(match AsmParser::parse(Rule::program, &code) {
AsmParser::parse(Rule::program, &code).unwrap_or_else(|e| panic!("{}", e)), Ok(instructions) => instructions,
); Err(e) => return JsValue::from_str(&format!("{}", e))
});
let binary = generate_binary(instructions); let binary = generate_binary(instructions);
JsValue::from_serde(&binary).unwrap() JsValue::from_serde(&binary).unwrap()
} }