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; } }
public PyClassDefinition FindOrCreateClass(PyQualifiedName pyClassName, PyQualifiedName baseClass) { var c = Classes.FirstOrDefault(i => pyClassName == i.Name); if (c != null) { return(c); } c = new PyClassDefinition(pyClassName, baseClass); Classes.Add(c); return(c); }
private void TranslateMethod(PyCodeModule pyModule, [CanBeNull] PyClassDefinition pyClass, MethodDeclaration md) { if (pyClass == null) { if (!md.Info.IsStatic) { throw new Exception( $"Unable to translate {md.Info} method: Module class can contain only static methods"); } } Tranlate_MethodOrProperty(pyModule, pyClass, md.Info, md.Body, 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; } }
private void TranslateProperty(PyCodeModule pyModule, [CanBeNull] PyClassDefinition pyClassDefinition, CsharpPropertyDeclaration propertyDeclaration) { var pi = _state.Principles.CurrentType.GetProperty(propertyDeclaration.PropertyName); var pti = PropertyTranslationInfo.FromPropertyInfo(pi); if (pti.GetSetByMethod) { CsharpPropertyDeclarationAccessor accessor; if (!string.IsNullOrEmpty(pti.GetMethodName)) { accessor = propertyDeclaration.Accessors.FirstOrDefault(u => u.Name == "get"); if (accessor != null) { Tranlate_MethodOrProperty(pyModule, pyClassDefinition, pi.GetGetMethod(), accessor.Statement, pti.GetMethodName); } } if (string.IsNullOrEmpty(pti.SetMethodName)) { return; } accessor = propertyDeclaration.Accessors.FirstOrDefault(u => u.Name == "set"); if (accessor != null) { Tranlate_MethodOrProperty(pyModule, pyClassDefinition, pi.GetSetMethod(), accessor.Statement, pti.SetMethodName); } } else { pyClassDefinition.Fields.Add(new PyClassFieldDefinition(pti.FieldScriptName, propertyDeclaration.Type.DotnetType) { IsStatic = pti.IsStatic }); } }
private void TranslateField(PyCodeModule module, PyClassDefinition pyClass, FieldDeclaration field) { PyValueTranslator pyValueTranslator = null; foreach (var item in field.Items) { if (item.OptionalFieldInfo == null) { continue; } var fti = Info.GetOrMakeTranslationInfo(item.OptionalFieldInfo); switch (fti.Destination) { case FieldTranslationDestionations.DefinedConst: if (item.Value == null) { throw new NotSupportedException(); } if (pyValueTranslator == null) { pyValueTranslator = new PyValueTranslator(_state); } var definedValue = pyValueTranslator.TransValue(item.Value); { if (fti.IncludeModule != module.ModuleName) { module = GetOrMakeModuleByName(fti.IncludeModule); } } module.DefinedConsts.Add(new KeyValuePair <string, IPyValue>(fti.ScriptName, definedValue)); break; case FieldTranslationDestionations.GlobalVariable: if (item.Value != null) { IPyValue value; // muszę na chwilę wyłączyć current type, bo to jes poza klasą generowane { var saveCurrentType = _state.Principles.CurrentType; _state.Principles.CurrentType = null; try { if (pyValueTranslator == null) { pyValueTranslator = new PyValueTranslator(_state); } value = pyValueTranslator.TransValue(item.Value); } finally { _state.Principles.CurrentType = saveCurrentType; } } var assign = new PyAssignExpression(PyVariableExpression.MakeGlobal(fti.ScriptName), value); module.TopCode.Statements.Add(new PyExpressionStatement(assign)); } break; case FieldTranslationDestionations.JustValue: continue; // don't define case FieldTranslationDestionations.NormalField: case FieldTranslationDestionations.ClassConst: { var def = new PyClassFieldDefinition(fti.ScriptName, field.Type.DotnetType); var cti = _state.Principles.GetTi(_state.Principles.CurrentType, true); if (cti.IsArray) { continue; } if (field.Modifiers.Has("const") ^ (fti.Destination == FieldTranslationDestionations.ClassConst)) { throw new Exception("beige lion"); } def.IsConst = fti.Destination == FieldTranslationDestionations.ClassConst; // field.Modifiers.Has("const"); def.IsStatic = def.IsConst || field.Modifiers.Has("static"); if (field.Modifiers.Has("public")) { def.Visibility = Visibility.Public; } else if (field.Modifiers.Has("protected")) { def.Visibility = Visibility.Protected; } else { def.Visibility = Visibility.Private; } if (item.Value != null) { if (pyValueTranslator == null) { pyValueTranslator = new PyValueTranslator(_state); } var value = pyValueTranslator.TransValue(item.Value); /* * if (!(value is PyConstValue)) * { * // converts to value * value = ExpressionEvaluator.Evaluate(value); * // dificult to translate-move values to additional class * // var t = new RefactorByMovingToAnotherClass(); * //value = t.ConvertAndRefactor(value); * } * */ def.ConstValue = value; } pyClass.Fields.Add(def); break; } default: throw new NotSupportedException(); } } }