I'm playing around with different designs to implement VM's (in a functional style). Think for parsing, FSM etc.
One of the simpler I've come up with uses normal clojure funcs for each instruction to the vm. They all take the state as a map as input and then additional arguments. They all return the delta for the state. The delta is then merged into the main state in a reduce. A program for the VM is a collection of instructions where a keyword names the func. In code: (defn adda [st i] {:a (+ (:a st) i)}) (defn suba [st i] {:a (- (:a st) i)}) (defn vm-exec [program] (let [runi (fn [st instr] (apply (eval (symbol (name (first instr)))) st (rest instr)))] (reduce #(merge %1 (runi %1 %2)) {:a 0 :b 0} program))) so that user=> (vm-exec [[:adda 1] [:adda 3] [:suba 2]]) {:b 0, :a 2} Any comments on this? Are there better/standard Lisp/Clojure ways of implementing VMs? Feels clunky to have to use map accessors all over and to return delta state. Most of my alternatives use atoms or refs but then you are restricted in where the functions can be defined (so the state vars are in scope etc). Thanks for any input. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---