1. firefly
  2. Eldis4

Commits

firefly  committed b8d0331

Adding support for '~hs' command, for haskell evaluation using mueval.

  • Participants
  • Parent commits 691bcb6
  • Branches default

Comments (0)

Files changed (3)

File modules/05-interpreters.js

View file
   }
 })
 
-function getExecResponder(self, data) {
+// Helper that collects the output
+function getExecResponder(options, self, data) {
+  if (!options) {
+    // no `options`--shift arguments to the left
+    data = arguments[1]
+    self = arguments[0]
+    options = {}
+  }
+
   return function(err, stdout, stderr) {
+    if (options.firstLineIsInput) {
+      if (stdout.indexOf(options.input + "\n") == 0) {
+        stdout = stdout.slice(options.input.length + 1)
+      }
+    }
+
     if (err) {
       if (err.signal == 'SIGKILL') {
         self.send(data.target, data.nick + ": Timed out.")
           trailing = " [stdout: " + escapeString(stdout) + "]"
         }
 
+        console.warn("-- interpreter failed --")
+        console.warn("stdout:", stdout)
+        console.warn("stderr:", stderr)
+        console.warn("  ---")
         self.send(data.target, util.format("%s: (returned %s): %s%s",
                   data.nick, err.code, escapeString(stderr), trailing))
       }
     } else {
+      console.log("stdout:", stdout)
+      console.log("stderr:", stderr)
       self.send(data.target, util.format("%s: %s", data.nick,
                 escapeString(stdout)))
     }
 }
 
 // Assumes stuff/migol.jar exists and is a proper executable Migol interpreter.
+/*
 bot.triggers.command.add('migol', {
   help: "Evaluates a Migol program.",
   exec: function(data) {
     })
   }
 })
+*/
 
 // Assumes stuff/z.jar exists and is a proper executable Z interpreter.
 bot.triggers.command.add('z', {
       , options = {timeout:8000, killSignal:'SIGKILL'}
       , suffix  = Math.random()
       , tmpfile = '/tmp/tmp-eldis4-z-' + suffix
-    //  , command = "java -jar stuff/Z3.jar -w '[' ']' '" + tmpfile + "'"
-      , command = "stuff/z/runner.sh '" + tmpfile + "' '" + suffix + "'"
+
+   // , command = "java -jar stuff/Z3.jar -w '[' ']' '" + tmpfile + "'"
+   // , command = "stuff/z/runner.sh '" + tmpfile + "' '" + suffix + "'"
+
+      , runner  = 'stuff/z/runner.sh'
+      , args    = [tmpfile, suffix]
+
 
     fs.writeFile(tmpfile, data.line, function(err) {
       if (err) {
         return
       }
 
-      child_process.exec(command, options, getExecResponder(self, data))
+      child_process.execFile(runner, args, options, getExecResponder({}, self, data))
+   // child_process.exec(command, options, getExecResponder(self, data))
     })
   }
 })
+
+
+//-- '~hs' for haskell eval -------------------------------
+bot.triggers.command.add('hs', {
+  help: "Evaluates a Haskell snippet.",
+  exec: function(data) {
+    var self    = this
+      , options = {timeout:8000, killSignal:'SIGKILL'}
+      , suffix  = Math.random()
+   // , escaped = shellEscape(shellEscape(data.line)) // note double-escape!
+   // , command = "stuff/hs/runner.sh '" + escaped + "'"
+
+      , input   = data.line
+
+      , runner  = 'stuff/hs/mueval'
+      , args    = [ '--inferred-type', '--time-limit=3', '--no-imports'
+                  , '--load-file=stuff/hs/Environment.hs', '-e', input ]
+
+//${mueval} --inferred-type --time-limit="$timeout" --no-imports \
+//          --load-file="$library" -e "${irc_input}"
+
+      , responderOpts = { input:            input
+                        , firstLineIsInput: true }
+
+ // child_process.exec(command, options, getExecResponder(responderOpts, self, data))
+    child_process.execFile(runner, args, options,
+                           getExecResponder(responderOpts, self, data))
+
+    function shellEscape(str) {
+      // foo"bar  => foo"\""bar, which is useful if embedded in a double-quoted
+      // string in shell.
+      return str.replace(/"/g,     '"\\""')
+             // .replace(/[{}$]/g, '\\$&')
+    }
+  }
+})
+

File stuff/hs/Environment.hs

View file
+module Environment where
+
+import           Prelude
+import           Control.Applicative
+import           Control.Arrow
+import qualified Control.Exception as E
+import           Control.Monad
+import           Control.Monad.Fix
+import           Control.Monad.Reader
+import           Control.Monad.State
+import           Control.Monad.Trans
+import           Control.Monad.Writer
+--import           Control.Parallel
+import           Data.Bits
+import qualified Data.ByteString.Lazy as B
+import           Data.Char
+import           Data.Complex
+import           Data.Function
+import           Data.Functor
+import           Data.List
+import           Data.Map (Map)
+import qualified Data.Map as Map
+import           Data.Maybe
+import           Data.Monoid
+import           Data.Ratio
+import           Data.Set (Set)
+import qualified Data.Set as Set
+import           Data.Typeable
+import           Data.Word
+import           Numeric
+--import           System
+--import           System.IO
+--import           System.Process
+--import           System.Random
+import           Text.Printf
+--import           Text.Regex
+import           Text.Show.Functions
+
+instance Typeable a => Show (IO a) where
+  show io = '<' : show (typeOf io) ++ ">"
+
+data PutStr = PutStr String
+
+instance Show PutStr where
+  show (PutStr s) = s

File stuff/hs/runner.sh

View file
+#!/bin/sh
+
+# config vars
+#basedir="/tmp/eldis4/z-eval/"
+library="stuff/hs/Environment.hs"
+mueval="stuff/hs/mueval"
+#whitelist="import unpack cast random math stdout inspect"
+timeout=3
+
+
+# Begin program..
+irc_input="$1"
+
+# Run it!
+${mueval} --inferred-type --time-limit="$timeout" --no-imports \
+          --load-file="$library" -e "${irc_input}"