private static string GetComment(Specification S, bool declOnly, G25.fgs FGS, G25.Operator op, G25.FloatType FT, bool assign) { StringBuilder SB = new StringBuilder(); if ((S.OutputCpp()) && op.IsUnaryInPlace()) { if (op.IsPrefix) { SB.Append("returns (" + FGS.ArgumentVariableNames[0] + " = " + FGS.OutputName + "(" + FGS.ArgumentVariableNames[0] + "))"); } else { SB.Append("returns input value of " + FGS.ArgumentVariableNames[0] + ", but sets " + FGS.ArgumentVariableNames[0] + " to " + FGS.OutputName + "(" + FGS.ArgumentVariableNames[0] + ")"); } } else if (assign) { SB.Append("returns (" + FGS.ArgumentVariableNames[0] + " = " + FGS.OutputName + "(" + FGS.ArgumentVariableNames[0]); SB.Append(", " + FGS.ArgumentVariableNames[1]); SB.Append("))"); } else { SB.Append("returns " + FGS.OutputName + "(" + FGS.ArgumentVariableNames[0]); if (op.IsBinary()) { SB.Append(", " + FGS.ArgumentVariableNames[1]); } SB.Append(")"); } return(SB.ToString()); }
private static void WriteOperatorBody(StringBuilder SB, Specification S, G25.CG.Shared.CGdata cgd, G25.fgs FGS, G25.Operator op, string funcName) { bool returnTypeEqualsFirstArgument = FGS.ReturnTypeName == FGS.ArgumentTypeNames[0]; if ((S.OutputCpp()) && op.IsUnaryInPlace() && returnTypeEqualsFirstArgument) // special unary case for C++ { if (op.IsPrefix) { SB.AppendLine("\t" + FGS.ArgumentVariableNames[0] + " = " + funcName + "(" + FGS.ArgumentVariableNames[0] + ");"); SB.AppendLine("\treturn " + FGS.ArgumentVariableNames[0] + ";"); } else { SB.AppendLine("\t" + FGS.ReturnTypeName + " retVal(" + FGS.ArgumentVariableNames[0] + ");"); SB.AppendLine("\t" + FGS.ArgumentVariableNames[0] + " = " + funcName + "(" + FGS.ArgumentVariableNames[0] + ");"); SB.AppendLine("\treturn retVal;"); } } else // regular operator function { SB.Append("\treturn " + funcName + "(" + FGS.ArgumentVariableNames[0]); if (op.IsBinary()) { SB.Append(", " + FGS.ArgumentVariableNames[1]); } SB.AppendLine(");"); } }
private static void WriteOperator(StringBuilder SB, Specification S, G25.CG.Shared.CGdata cgd, bool declOnly, G25.fgs FGS, G25.Operator op) { bool comment = declOnly || S.m_inlineOperators; bool returnTypeEqualsFirstArgument = FGS.ReturnTypeName == FGS.ArgumentTypeNames[0]; foreach (string floatName in FGS.FloatNames) { G25.FloatType FT = S.GetFloatType(floatName); string funcName = FGS.OutputName; { // regular operator bool assign = false; bool returnByReference = returnTypeEqualsFirstArgument && op.IsPrefixUnary() && op.IsUnaryInPlace(); // for unary prefix ++ and --, needs to return by reference (&) bool constVal = !(op.IsUnaryInPlace() && returnTypeEqualsFirstArgument); string funcDecl = GetFuncDecl(S, declOnly, FGS, op, FT, assign, constVal, returnByReference); if (comment) // comment? { SB.AppendLine("/// " + GetComment(S, declOnly, FGS, op, FT, assign)); } SB.Append(funcDecl); if (declOnly) { SB.AppendLine(";"); } else { SB.AppendLine(" {"); WriteOperatorBody(SB, S, cgd, FGS, op, funcName); SB.AppendLine("}"); } } // add an extra operator with assignment (like +=) ? if (op.IsBinary() && returnTypeEqualsFirstArgument) { bool assign = true; bool constVal = false; bool returnByReference = true; // todo: for unary prefix ++ needs to return by reference (&) string funcDecl = GetFuncDecl(S, declOnly, FGS, op, FT, assign, constVal, returnByReference); if (comment) // comment? { SB.AppendLine("/// " + GetComment(S, declOnly, FGS, op, FT, assign)); } SB.Append(funcDecl); if (declOnly) { SB.AppendLine(";"); } else { SB.AppendLine(" {"); SB.Append("\treturn (" + FGS.ArgumentVariableNames[0] + " = " + funcName + "(" + FGS.ArgumentVariableNames[0]); SB.AppendLine(", " + FGS.ArgumentVariableNames[1] + "));"); SB.AppendLine("}"); } } } }