static void Main(string[] args) { var obj = new CSharpFunction("Test"); string output = obj.TransformText(); Console.WriteLine(output); Console.ReadLine(); }
public void PushCSharpClosure(CSharpFunction f, int n) { var closure = new LuaClosure(f, n); for (int i = n - 1; i >= 0; i--) { var val = LuaStack.Pop(); closure.Upvals[i] = LuaUpValue.CreateClosed(val); } LuaStack.Push(closure); }
public void Execute(CSharpFunction function, IEnumerable <DataCell> input_data, DataCell output, CommandContext command_context = null) { if (_availableExecutionServices.ContainsKey(typeof(BasicExecutionService))) { _availableExecutionServices[typeof(CSharpExecutionService)].Execute(function, input_data, output, command_context); } else { throw new Exception(String.Format("Исполнитель функций C# не доступен.")); } }
public static void Load(Interpreter interpreter) { var basicOperators = new BasicOperators(interpreter); var basicFunctions = new BasicFunctions(interpreter); var methods = new object[] { basicFunctions, basicOperators } .Select(t => new Tuple<object, MethodInfo[]>(t, t.GetType().GetMethods())) .Select(t => t.Item2.Select(m => new Tuple<MethodInfo, object>(m, t.Item1))) .Aggregate((a, m) => a.Concat(m).ToArray()) .Where(m => m.Item1.GetCustomAttributes(typeof(FunctionAttributes), false).Length == 1) .OrderBy(m => (m.Item1.GetCustomAttributes(typeof(FunctionAttributes), false)[0] as FunctionAttributes).Priority) .ToArray(); foreach (var methodPair in methods) { var method = methodPair.Item1; var obj = methodPair.Item2; var attribs = method.GetCustomAttributes(typeof(FunctionAttributes), false)[0] as FunctionAttributes; var arguments = method.GetParameters().Select(p => p.ParameterType).ToArray(); var returnType = method.ReturnType; Func<List<Value>, Value> del = delegate(List<Value> args) { return (Value)method.Invoke(obj, args.ToArray()); }; var func = new CSharpFunction(del, attribs.Fixity, arguments.Length); func.Signature.InputSignature.Clear(); foreach (var type in arguments) { if (type == typeof(Value)) func.Signature.InputSignature.Add(ValueType.Uncertain); else func.Signature.InputSignature.Add(ValueType.GetType(type.Name)); } func.Signature.OutputSignature = ValueType.GetType(returnType == typeof(Value) ? "Uncertain" : returnType.Name); Value val; if ((val = interpreter.ScopeTreeRoot.LookupNoRef(attribs.Identifier)) is PartialApplication) { interpreter.ScopeTreeRoot.AddToScope(attribs.Identifier, (val as PartialApplication).Merge(func)); } else { interpreter.ScopeTreeRoot.AddToScope(attribs.Identifier, func); } } foreach (var type in ValueType.Types.Values) { interpreter.ScopeTreeRoot.AddToScope(type.TypeName, new CastFunction(type)); } }
private MethodDeclarationSyntax FunctionExtern(CSharpFunction function) { var parameterStrings = function.Parameters.Select( x => $@"{x.Type.Name} {x.Name}"); var parameters = string.Join(',', parameterStrings); var code = $@" {function.CodeLocationComment} [DllImport(LibraryName)] public static extern {function.ReturnType.Name} {function.Name}({parameters}); "; var member = ParseMemberCode <MethodDeclarationSyntax>(code); return(member); }
public void PushCSharpFunction(CSharpFunction cs) => LuaStack.Push(new LuaClosure(cs, 0));
public void Register(string name, CSharpFunction cs) { PushCSharpFunction(cs); SetGlobal(name); }
public Function Function(List args, CodeBlock block) { var functionsScope = new ScopeTreeNode(Permanency.NonPermanent, Interpreter.CurrentScopeNode); Func<List<Value>, Value> code = delegate(List<Value> list) { var tempScope = new ScopeTreeNode(Permanency.NonPermanent, functionsScope.Parent); Interpreter.EnterScope(tempScope); for (int i = 0; i < list.Count; ++i) { var name = args.InnerList[i].Var.OriginalName; Interpreter.CurrentScopeNode.AddToScope(name, list[i]); } var ret = block.Execute(Interpreter); if (ret.Type <= ValueType.ReturnValue) ret = (ret as ReturnValue).InnerValue; Interpreter.ExitScope(); return ret; }; var fun = new CSharpFunction(code, Fixity.Prefix, args.InnerList.Count) { FunctionScope = functionsScope }; fun.Signature.InputSignature.Clear(); args.Select(v => v.Type).ToList().ForEach(t => fun.Signature.InputSignature.Add(t == ValueType.BlankIdentifier ? ValueType.Uncertain : t)); return fun; }