public string fixForC(CallNode callNode) { string output = ""; if (callNode is FunctionCallNode) { var fixedNode = (FunctionCallNode)callNode; List <COOPClass> inputs = new List <COOPClass>(); foreach (CallNode parameter in fixedNode.parameters) { inputs.Add(parameter.type); } string parameters = "("; if (callNode is ObjectFunctionCallNode) { ObjectFunctionCallNode node = callNode as ObjectFunctionCallNode; inputs.Insert(0, node.parentObject.type); NameInputTypePair tempPair = new NameInputTypePair(fixedNode.symbol, inputs); while (getMangeledName(tempPair) == null) { inputs[0] = hierarchy.getParent(inputs[0]); if (inputs[0] == null) { return(null); } tempPair = new NameInputTypePair(fixedNode.symbol, inputs); } parameters += fixForC(node.parentObject); if (inputs.Count > 1) { parameters += ","; } } NameInputTypePair pair = new NameInputTypePair(fixedNode.symbol, inputs); for (var i = 0; i < fixedNode.parameters.Count; i++) { parameters += fixForC(fixedNode.parameters[i]); if (i < fixedNode.parameters.Count - 1) { parameters += ","; } } parameters += ")"; string mangled = originalNameAndInputTypesToMangledName[pair]; output = mangled + parameters; } else if (callNode is SymbolNode) { output = callNode.ToString(); } return(output); }
public Collection <FileConvertedInformation> convert(COOPClass coopObject, ClassHierarchy hierarchy) { var output = new Collection <FileConvertedInformation>(); COOPClass parent = hierarchy.getParent(coopObject); bool createProtected, createPrivate; string publicStructure = generatePublicStructure(coopObject, parent, out createProtected, out createPrivate); string cFile = "", protectedHeader = $"#ifndef SRC_PROTECTED_FILE_{coopObject.Name}\n#define SRC_PROTECTED_FILE_{coopObject.Name}\n", publicHeader = $"#ifndef SRC_PUBLIC_FILE_{coopObject.Name}\n#define SRC_PUBLIC_FILE_{coopObject.Name}\n"; foreach (COOPClass coopObjectImport in coopObject.imports) { cFile += $"#include \"{coopObjectImport.Name}.h\"\n"; } if (coopObject.Parent != null) { cFile += $"#include \"{coopObject.Parent.Name}_protected.h\"\n"; } cFile += $"#include \"{coopObject.Name}_protected.h\"\n"; if (createPrivate) { cFile += generatePrivateStructure(coopObject); } protectedHeader += $"#include \"{coopObject.Name}.h\"\n"; if (createProtected) { protectedHeader += generateProtectedStructure(coopObject); } publicHeader += publicStructure; COOPFunctionConverter functionConverter = new COOPFunctionConverter(hierarchy, coopObject); List <FunctionConvertedInformation> functionConvertedInformations = new List <FunctionConvertedInformation>(); foreach (var coopObjectFunction in coopObject.getFunctions()) { var f = functionConverter.convert(coopObjectFunction); functionConvertedInformations.AddRange(f); } foreach (FunctionConvertedInformation functionConvertedInformation in from f in functionConvertedInformations where f.accessLevel == AccessLevel.Private select f) { cFile += functionConvertedInformation.signature + ";\n"; } foreach (FunctionConvertedInformation functionConvertedInformation in from f in functionConvertedInformations where f.accessLevel == AccessLevel.Protected select f) { protectedHeader += functionConvertedInformation.signature + ";\n"; } bool hasMain = false; string sig = ""; foreach (FunctionConvertedInformation functionConvertedInformation in from f in functionConvertedInformations where f.accessLevel == AccessLevel.Public select f) { publicHeader += functionConvertedInformation.signature + ";\n"; //Console.WriteLine(functionConvertedInformation.signature); if (functionConvertedInformation.OriginalName == "main") { Console.WriteLine("Main Method Found: " + functionConvertedInformation.signature); hasMain = true; sig = functionConvertedInformation.signature; } } foreach (FunctionConvertedInformation functionConvertedInformation in functionConvertedInformations) { cFile += functionConvertedInformation.signature + "{\n"; cFile += functionConvertedInformation.body; cFile += "}\n"; } publicHeader += "#endif"; protectedHeader += "#endif"; if (humanReadable) { cFile = toHumanReadable(cFile); publicHeader = toHumanReadable(publicHeader); protectedHeader = toHumanReadable(protectedHeader); } output.Add(new FileConvertedInformation(coopObject.Name + ".c", cFile)); output.Add(new FileConvertedInformation(coopObject.Name + "_protected.h", protectedHeader)); output.Add(new FileConvertedInformation(coopObject.Name + ".h", publicHeader)); if (hasMain) { output[2].hasMainMethod = true; output[2].mainMethod = sig; Console.WriteLine("Main Method Confirmed: " + sig); } return(output); }