示例#1
0
        public static string GenerateFunction(FnDesc desc)
        {
            string args = GenerateFunctionArgs(desc.Arguments);
            string body = string.IsNullOrEmpty(desc.Body) ? "pass" : desc.Body;

            body = body.Replace("\n", "\n\t");
            string rtrn   = desc.IsReturn ? "return " : "";
            string tabbed = string.Format("\t{0}{1}", rtrn, body);

            return(string.Format("def {0}({1}):\n{2}", desc.Name, args, tabbed));
        }
示例#2
0
        public object AddFunction(FnDesc desc)
        {
            if (desc.Arguments == null)
            {
                desc.Arguments = new List <FnArgDesc>();
            }
            desc.Arguments.Insert(0, new FnArgDesc("self"));

            var function = CodeGenerator.GenerateFunction(desc);

            _pi.Execute(function);
            var fn = _pi.GetVariable(desc.Name);

            _pi.RemoveVariable(desc.Name);

            var method = new Method(fn, _object, DynamicHelpers.GetPythonType(_object));

            SetMember(desc.Name, method);
            return(method);
        }
 public CachedFunction(FnDesc desc)
 {
     _fnDesc = desc;
 }