Let's build a little Haskell interpreter that we can use as a safe sandbox on the Web.
We'll use Scotty for our Web binding, so we'll want to enable
OverloadedStrings and import some functions:
hint to interpret and run client code:
We need functions to take arbitrary source code as a string, try to evaluate it, and handle compilation errors:
We also need a way to expose it to the Web:
Finally, we need a way to launch it:
main function defined, we're ready to fire it up. We can use Nix to import the
scotty packages for us:
$ nix-shell -p 'haskellPackages.ghcWithPackages (pkgs: [ pkgs.hint pkgs.scotty ])'
From our Nix shell, we can use Codedown to extract the Haskell code above, and
runhaskell to launch it:
$ curl -s https://earldouglas.com/posts/toys/haskeval.md | codedown haskell | HASKEVAL_PORT=3000 runhaskell Setting phasers to stun... (port 3000) (ctrl-c to quit)
Let's try it out! Enter a line of Haskell (with the type
Show a => a) below, and hit