
Justin Bailey wrote:
As part of the Ruby Quiz in Haskell solutions appearing on the wiki recently, I just a solution to Ruby Quiz #100 - create a bytecode interpreter for a simple expression language.
Like I said, the code below parses simple integer arithmetic expressions and generates byte codes for a hypothetical stack-based intepreter that would evaluate the expressions. To run it, save it as a literate haskell file and run "interpret_tests". That just shows correctness, though. Other output can be obtained by running "compile_tests" (shows bytes for all tests), "generate_tests" (symbolic bytecodes for all tests), and "eval_tests" (evaluate ASTs for all tests).
To see the AST generated for a example expression, try something like 'parse "2-2-2"'.
I'm just learning Haskell (about a month in) and if anyone has time and desire to critique the code below, I'd love to hear it. I come from an OOP (primarily C# & Ruby) background, so I'm really interested in getting a handle on the functional/Haskell "way" of coding. Thanks for any feedback!
I haven't had time to look over your code, but this reminds me of a fun paper, "A type-correct, stack-safe, provably correct expression compiler in Epigram" The code consists of an expression interpreter, a stack machine emulator, a compiler, and a proof that forall expr, evaluate exper == execute (compile expr). (proofs are functions - go Curry-Howard) You can find the paper at http://www.cs.nott.ac.uk/~jjw/ Brandon