0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その647

0
Posted at

概要

cscの作法、調べてみた。
Neo.IronLua、見つけたので、やってみた。
luaからc#の関数使ってみた。

サンプルコード


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Neo.IronLua;

namespace ExtTest
{
	class StartEx {
		public const string ProgramSource = 
			"local a, b = tonumber(read(\"a\")), tonumber(read(\"b\"));\n" +
			"local PrintResult = function(o, op)\n" +
			"    print(o.. ' = ' .. a..op..b);\n" +
			"end;\n" +
			"PrintResult(a + b, \" + \");\n" +
			"PrintResult(a - b, \" - \");\n" +
			"PrintResult(a * b, \" * \");\n" +
			"PrintResult(a / b, \" / \");\n" +
			"PrintResult(a // b, \" // \");\n";
		private static void Print(object[] texts) {
			foreach (object o in texts)
				Console.Write(o);
			Console.WriteLine();
		} 
		private static string Read(string sLabel) {
			Console.Write(sLabel);
			Console.Write(": ");
			return Console.ReadLine();
		} 
		public static void Main(string[] args) {
			Console.WriteLine(ProgramSource);
			using (var l = new Lua())
			{
				dynamic g = l.CreateEnvironment<LuaGlobal>();
				g.print = new Action<object[]>(Print);
				g.read = new Func<string, string>(Read);
				var chunk = l.CompileChunk(ProgramSource, "test.lua", new LuaCompileOptions() {
					DebugEngine = LuaStackTraceDebugger.Default
				});
				try
				{
					g.dochunk(chunk);
				}
				catch (Exception e)
				{
					Console.WriteLine("Expception: {0}", e.Message);
					var d = LuaExceptionData.GetData(e);
					Console.WriteLine("StackTrace: {0}", d.FormatStackTrace(0, false));
				}
			}
		}
	}
}




実行結果

>neo0
local a, b = tonumber(read("a")), tonumber(read("b"));
local PrintResult = function(o, op)
    print(o.. ' = ' .. a..op..b);
end;
PrintResult(a + b, " + ");
PrintResult(a - b, " - ");
PrintResult(a * b, " * ");
PrintResult(a / b, " / ");
PrintResult(a // b, " // ");

a: 2
b: 3
5 = 2 + 3
-1 = 2 - 3
6 = 2 * 3
0.666666666666667 = 2 / 3
0 = 2 // 3

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?