Implement a Compiler struct in Rust that converts a series of assembly-like instructions into a vector of Opcode enums. The compile method of the Compiler should parse the input string and produce the appropriate opcodes according to the following specifications:
Supported Instructions:
No Argument Instructions: The following instructions do not require arguments and should be directly converted to their corresponding Opcode:
add → Opcode::Add
sub → Opcode::Subtract
mul → Opcode::Multiply
drop → Opcode::Drop
swp → Opcode::Swap
halt → Opcode::Halt
Instructions Requiring Arguments: The following instructions require a numeric or label argument:
The value for push and dup should be extracted from the argument, while for bnz, the label should be stored as unresolved initially.
Label Support: The compiler should support labels defined with a colon (:). It should store the address of each label as it is encountered. When a bnz instruction is compiled, it should check if the label exists:
If it exists, convert Reference::Unresolved(label) to Reference::Resolved(address).
If it does not exist by the end of the compilation process, it should panic with an error indicating the undefined reference.
Whitespace Handling: The compile method should handle leading, trailing, and multiple whitespaces. Whitespace should be ignored when parsing tokens.
Error Handling: Implement error handling using anyhow::Result for unknown instructions or if parsing fails.
Testing: Ensure that the implementation passes the provided tests, which cover:
Instructions without arguments.
Instructions with numeric arguments.
Handling of whitespace.
Compilation of multiple instructions in sequence.
Proper handling of labels with bnz.
Place the implementation of the Compiler struct and its compile method in the /app/ directory.