private void Tranlate_MethodOrProperty(PyCodeModule pyModule, [CanBeNull] PyClassDefinition pyClass, MethodInfo info, IStatement body, string overrideName) { _state.Principles.CurrentMethod = info; try { var mti = _state.Principles.GetOrMakeTranslationInfo(info); var pyMethod = new PyClassMethodDefinition(string.IsNullOrEmpty(overrideName) ? mti.ScriptName : overrideName); if (pyClass == null) { pyModule.Methods.Add(pyMethod); pyMethod.Kind = PyMethodKind.OutOfClass; } else { pyClass.Methods.Add(pyMethod); pyMethod.Kind = info.IsStatic ? PyMethodKind.ClassStatic : PyMethodKind.ClassInstance; } if (info.IsPublic) { pyMethod.Visibility = Visibility.Public; } else if (info.IsPrivate) { pyMethod.Visibility = Visibility.Private; } else { pyMethod.Visibility = Visibility.Protected; } { var declaredParameters = info.GetParameters(); foreach (var parameter in declaredParameters) { var pyParameter = new PyMethodArgument { Name = parameter.Name }; pyMethod.Arguments.Add(pyParameter); if (parameter.HasDefaultValue) { pyParameter.DefaultValue = new PyConstValue(parameter.DefaultValue); } } } if (body != null) { pyMethod.Statements.AddRange(TranslateStatement(body)); } } finally { _state.Principles.CurrentMethod = null; } }
private void TranslateConstructor(PyClassDefinition pyClass, ConstructorDeclaration md) { // state.Principles.CurrentMethod = md.Info; try { // MethodTranslationInfo mti = MethodTranslationInfo.FromMethodInfo(md.Info); // state.Principles.CurrentMethod = var pyMethod = new PyClassMethodDefinition("__init__"); pyClass.Methods.Add(pyMethod); if (md.Info.IsPublic) { pyMethod.Visibility = Visibility.Public; } else if (md.Info.IsPrivate) { pyMethod.Visibility = Visibility.Private; } else { pyMethod.Visibility = Visibility.Protected; } pyMethod.Kind = md.Info.IsStatic ? PyMethodKind.ClassStatic : PyMethodKind.ClassInstance; { var declaredParameters = md.Info.GetParameters(); foreach (var parameter in declaredParameters) { var pyParameter = new PyMethodArgument(); pyParameter.Name = parameter.Name; pyMethod.Arguments.Add(pyParameter); if (parameter.HasDefaultValue) { pyParameter.DefaultValue = new PyConstValue(parameter.DefaultValue); } } } if (md.Body != null) { pyMethod.Statements.AddRange(TranslateStatement(md.Body)); } } finally { _state.Principles.CurrentMethod = null; } }
public void T51_Should_emit_instance_class_method() { var method = new PyClassMethodDefinition("Foo") { Kind = PyMethodKind.ClassInstance, Visibility = Visibility.Public }; method.Statements.Add(new PyReturnStatement(new PyConstValue(123))); method.Arguments.Add(new PyMethodArgument { Name = "x" }); WriteCode(method, @" def Foo(self, x): return 123"); }