From 6da4c3db11d64b85edd71ec61c754a4bb4053f1b Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 1 May 2016 16:21:36 +0200 Subject: [PATCH] it sorta works, still need to actually use interpreter --- .../Programming/Commands/HaskellRepl.cs | 68 ++++++++++++++----- NadekoBot/NadekoBot.cs | 2 + NadekoBot/NadekoBot.csproj | 2 + 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/NadekoBot/Modules/Programming/Commands/HaskellRepl.cs b/NadekoBot/Modules/Programming/Commands/HaskellRepl.cs index 567d9b84..f8c1f78d 100644 --- a/NadekoBot/Modules/Programming/Commands/HaskellRepl.cs +++ b/NadekoBot/Modules/Programming/Commands/HaskellRepl.cs @@ -1,8 +1,10 @@ using Discord; using Discord.Commands; using NadekoBot.Classes; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +using System.Text; using System.Threading; using System.Threading.Tasks; @@ -13,7 +15,7 @@ namespace NadekoBot.Modules.Programming.Commands { class HaskellRepl : DiscordCommand { - Queue> commandQueue = new Queue>(); + ConcurrentQueue> commandQueue = new ConcurrentQueue>(); Thread haskellThread; @@ -23,27 +25,51 @@ namespace NadekoBot.Modules.Programming.Commands haskellThread = new Thread(new ThreadStart(() => { - Process.Start(new ProcessStartInfo + var p = Process.Start(new ProcessStartInfo { FileName = "stack", //shouldn't use repl, but a Language.Haskell.Interpreter somehow - Arguments = "repl" + Arguments = "repl", + UseShellExecute = false, + RedirectStandardInput = true, + RedirectStandardOutput = true, + CreateNoWindow = true, + }); + + Task.Run(async () => + { + while (true) + { + while (commandQueue.Count == 0) + await Task.Delay(100); + + //read from queue + KeyValuePair com; + if (!commandQueue.TryDequeue(out com)) + { + await Task.Delay(100); + continue; + } + //var bytes = Encoding.ASCII.GetBytes(com.Key); + + //send the command to the process + p.StandardInput.WriteLine(com.Key); + + //wait 50 ms for execution + await Task.Delay(50); + + //read everything from the output + var outBuffer = new byte[1500]; + + p.StandardOutput.BaseStream.Read(outBuffer, 0, 1500); + + var outStr = Encoding.ASCII.GetString(outBuffer); + //send to channel + await com.Value.SendMessage($"```hs\nPrelude> {com.Key}\n" + outStr + "\n```"); + } }); })); - - - Task.Run(() => - { - //read from queue - - //send the command to the process - - //wait 50 ms for execution - - //read everything from the output - - //send to chanenl - }); + haskellThread.Start(); } @@ -51,9 +77,15 @@ namespace NadekoBot.Modules.Programming.Commands { cgb.CreateCommand(Module.Prefix + "hs") .Description("Executes a haskell express with LAMBDABOT") - .Do(async e => + .Parameter("command", ParameterType.Unparsed) + .Do(e => { + var com = e.GetArg("command")?.Trim(); + if (string.IsNullOrWhiteSpace(com)) + return; + //send a command and a channel to the queue + commandQueue.Enqueue(new KeyValuePair(com, e.Channel)); }); } } diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 9414f21b..2e7ffe7f 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -16,6 +16,7 @@ using NadekoBot.Modules.NSFW; using NadekoBot.Modules.Permissions; using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Modules.Pokemon; +using NadekoBot.Modules.Programming; using NadekoBot.Modules.Searches; using NadekoBot.Modules.Translator; using NadekoBot.Modules.Trello; @@ -183,6 +184,7 @@ namespace NadekoBot modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None); modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None); modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None); + modules.Add(new ProgrammingModule(), "Programming", ModuleFilter.None); if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey)) modules.Add(new TrelloModule(), "Trello", ModuleFilter.None); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 8660d194..f8d39b81 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -123,6 +123,8 @@ + +