VM-Builder

License: MIT

VM-Builder is a tool to easily create simple virtual machine with dynamic allocated memory by Vec<T> through implementing for operator a trait Op<T>. It does not provide a parser, so code for calling VM look like: ```rust fn main() { let program: Vec> = vec![ op!(...), ... ];

let vm: VM<any> = VM::init(program);
vm.run();

} ```

any is just Box<dyn std::any::Any> but you can put any type, of course.

Op<T> is rust pub trait Op<T> { fn exec(&mut self, pc: *mut usize, memory: *mut Vec<T>); }

E.g. you want to implement a operator jmp and log for printing. Log: rust struct Log {value: String} impl<T> Op<T> for Log { fn exec(&mut self, pc: *mut usize, memory: *mut Vec<T>) { println!("{}", self.value); } } Jmp: ```rust struct Jmp {counter: usize, jumped: bool} impl Jmp { pub fn new(counter: usize) -> Self { Self {counter, jumped: false} } }

impl Op for Jmp { fn exec(&mut self, pc: *mut usize, memory: *mut Vec) { unsafe { if !jumped { *pc = self.counter } } jumped = true; } } For avoiding non-expecting behavior the first operator has to be built-in `Init`. Self-repeating (2 times) "Hello, world!" in example language: rust fn main() { let program: Vec> = vec![ op!(Init), op!(Log {value: "Hello, world!".to_string()}), // 0 starts here op!(Jmp::new(0)) ];

let vm: VM<any> = VM::init(program);
vm.run();

}