public void MakeMethod(SamFileMethod method, StringBuilder builder) { foreach (var item in method.Arguments) { builder.AppendLine($"// {item.Type} {item.Name}"); } builder.AppendLine($"// Return: {method.Type} "); builder.Append("\t"); if (method.IsStatic) { builder.Append("static "); } builder.Append($"{method.Name}("); foreach (var item in method.Arguments) { builder.Append($"{item.Name}, "); } if (method.Arguments.Count > 0) { builder.Length -= 2; // remove ", " } builder.Append(")"); builder.AppendLine(); builder.AppendLine("\t{"); builder.Append("\t\t"); // build body. MakeMethodBody(method, builder); builder.AppendLine("\t}"); }
public void MakeMethod(SamFileMethod method, StringBuilder builder) { builder.Append("\tpublic "); if (method.IsStatic) { builder.Append("static "); } if (method.Name == "Main") { method.Name = "main"; } builder.Append($"{ConvertNetTypeToJava(method.Type)} {method.Name}("); foreach (var item in method.Arguments) { builder.Append($"{ConvertNetTypeToJava(item.Type)} {item.Name}, "); } if (method.Arguments.Count > 0) { builder.Length -= 2; // remove ", " } builder.Append(") {"); builder.AppendLine(); builder.Append("\t\t"); // build body. MakeMethodBody(method, builder); builder.AppendLine("\t}"); }
public void MakeMethod(SamFileMethod method, StringBuilder builder) { builder.Append("\tPublic "); if (method.IsStatic) { builder.Append("Shared "); } string methodType = (method.Type == "void" ? "Sub" : "Function"); builder.Append($"{methodType} {method.Name}("); foreach (var item in method.Arguments) { builder.Append($"{item.Name} As {item.Type}, "); } if (method.Arguments.Count > 0) { builder.Length -= 2; // remove ", " } builder.Append(")"); if (method.Type != "void") { builder.Append($" As {method.Type}"); } //builder.AppendLine(); builder.AppendLine(); builder.Append("\t\t"); // build body. MakeMethodBody(method, builder); builder.AppendLine($"\tEnd {methodType}"); }
public void MakeMethodBody(SamFileMethod method, StringBuilder builder) { string tabs = "\t\t"; var tokens = method.Tokens; var lastToken = tokens.LastOrDefault(); foreach (var item in tokens) { if (item is CallFuncToken func) { if (func.FuncName == "Write") { builder.Append("System.out.print("); } else if (func.FuncName == "WriteLine") { builder.Append("System.out.println("); } else if (func.FuncName == "Equals") { builder.Append("equals("); } else { builder.Append(func.FuncName); } } else if (item is EndCallFuncToken) { builder.Append(")"); } else if (item is StringLiteralToken stringLiteral) { builder.Append(stringLiteral.Value); } else if (item is EndOfLineToken) { builder.Append(";"); builder.AppendLine(); if (item != lastToken) { builder.Append(tabs); } } else if (item is LoadVariableToken varToken) { builder.Append(varToken.Name); } else if (item is EqualToken) { builder.Append("=="); } } }