Currently, I am developing an assembler and simulator for a simplistic assembly language that my computer science students use during their classes. The project is being written in JavaScript with the intention of creating a user-friendly interface in the web browser to help students visualize how each instruction impacts the state of the machine.
One particular challenge I am facing is determining the most effective method for conveying error messages from the assembler when it encounters invalid assembly code. At this stage, the assembler's API is quite basic:
var assembler = ... // Obtain the assembler object
var valid_source = "0 mov r1 r2\n1 halt";
var valid_binary = assembler.assemble(valid_source); // Returns string of binary data (0's and 1's)
var invalid_source = "foo bar baz!";
var invalid_binary = assembler.assemble(invalid_source); // What should be the response in this case?
I have considered several approaches for addressing this issue:
- Create and throw a new JavaScript Error object. However, this seems like excessive handling since users may not necessarily need or want intricate details like a stack trace.
- Return a string or object containing specific error information. This way, the assembler's user can decide how to handle any errors that arise.
Modify the assembler's API to utilize a callback function instead:
assembler.assemble(source, function(binary, error) { if (error) { // Handle the error } // Process the binary otherwise });
- Or perhaps explore a different approach altogether?
Any suggestions, insights, or feedback would be highly valued.