Browse Source

Python Converter Enhancement

- Remove ref and out parameters from method declarations
pull/334/head
figment 12 years ago
parent
commit
e729a02051
  1. 37
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs

37
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/NRefactoryToPythonConverter.cs

@ -30,6 +30,7 @@ namespace ICSharpCode.PythonBinding
// references to fields or parameters. // references to fields or parameters.
List<ParameterDeclarationExpression> methodParameters = new List<ParameterDeclarationExpression>(); List<ParameterDeclarationExpression> methodParameters = new List<ParameterDeclarationExpression>();
MethodDeclaration currentMethod; MethodDeclaration currentMethod;
bool methodHasOutputParameters = false;
// Holds the names of any parameters defined for this class. // Holds the names of any parameters defined for this class.
List<string> propertyNames = new List<string>(); List<string> propertyNames = new List<string>();
@ -1234,6 +1235,10 @@ namespace ICSharpCode.PythonBinding
{ {
// Add method name. // Add method name.
currentMethod = methodDeclaration; currentMethod = methodDeclaration;
var oldMethodHasOutputParameters = methodHasOutputParameters;
methodHasOutputParameters = false;
string methodName = methodDeclaration.Name; string methodName = methodDeclaration.Name;
AppendIndented("def " + methodName); AppendIndented("def " + methodName);
@ -1263,6 +1268,7 @@ namespace ICSharpCode.PythonBinding
} }
methodParameters = oldMethodParameters; methodParameters = oldMethodParameters;
methodHasOutputParameters = oldMethodHasOutputParameters;
currentMethod = null; currentMethod = null;
return null; return null;
@ -1474,7 +1480,25 @@ namespace ICSharpCode.PythonBinding
returnStatement.Expression.AcceptVisitor(this, data); returnStatement.Expression.AcceptVisitor(this, data);
} else { } else {
AppendIndented("return "); AppendIndented("return ");
// python returns out and ref generally as a return tuple
if (methodHasOutputParameters)
Append("(");
returnStatement.Expression.AcceptVisitor(this, data); returnStatement.Expression.AcceptVisitor(this, data);
if (methodHasOutputParameters) {
if (currentMethod != null) {
foreach (var param in currentMethod.Parameters) {
if (param.ParamModifier == ParameterModifiers.Out
|| param.ParamModifier == ParameterModifiers.Ref) {
Append(", ");
Append(param.ParameterName);
}
}
}
Append(")");
}
AppendLine(); AppendLine();
} }
return null; return null;
@ -1753,7 +1777,7 @@ namespace ICSharpCode.PythonBinding
if (!skipInitializer) if (!skipInitializer)
usingStatement.ResourceAcquisition.AcceptVisitor(this, data); usingStatement.ResourceAcquisition.AcceptVisitor(this, data);
AppendIndentedLine("try:"); AppendIndentedLine("try:");
IncreaseIndent(); IncreaseIndent();
if (IsEmptyStatement(usingStatement.EmbeddedStatement)) if (IsEmptyStatement(usingStatement.EmbeddedStatement))
@ -2558,10 +2582,19 @@ namespace ICSharpCode.PythonBinding
Append("self, "); Append("self, ");
} }
for (int i = 0; i < parameters.Count; ++i) { for (int i = 0; i < parameters.Count; ++i) {
var param = parameters[i];
if (param.ParamModifier == ParameterModifiers.Out
|| param.ParamModifier == ParameterModifiers.Ref) {
// track both ref and output for return statement
methodHasOutputParameters = true;
// drop output parameters from declarations
if (param.ParamModifier == ParameterModifiers.Out)
continue;
}
if (i > 0) { if (i > 0) {
Append(", "); Append(", ");
} }
Append(parameters[i].ParameterName); Append(param.ParameterName);
} }
} else { } else {
if (!IsStatic(method)) { if (!IsStatic(method)) {

Loading…
Cancel
Save