47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Neo.IronLua;
|
|
using NLog;
|
|
|
|
namespace ChaosBot.Services.ProgrammingLanguageInterpreter
|
|
{
|
|
internal class LuaProgrammingLanguageInterpreter : IProgrammingLanguageInterpreter
|
|
{
|
|
private static readonly ILogger Logger = Program.GetLogger();
|
|
private readonly StringBuilder _outputBuilder = new StringBuilder();
|
|
|
|
public string Interpret(string content, string command)
|
|
{
|
|
using (Lua lua = new Lua())
|
|
{
|
|
// This needs to be dynamic if we want to call
|
|
// functions from within the lua environment
|
|
// This is a runtime type check
|
|
dynamic env = lua.CreateEnvironment();
|
|
|
|
ParamsDelegate printDel = Print;
|
|
env.print = printDel;
|
|
|
|
env.dochunk(content, $"{command}.lua");
|
|
|
|
return _outputBuilder.ToString();
|
|
}
|
|
}
|
|
|
|
private delegate void ParamsDelegate(params object[] parameters);
|
|
|
|
private void Print(params object[] parameters)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
foreach (object parameter in parameters)
|
|
{
|
|
sb.Append(parameter);
|
|
}
|
|
|
|
_outputBuilder.Append(sb);
|
|
}
|
|
}
|
|
}
|