private void OutputConstructor(string name, Definition.ConstructorDef c) { //Summary(c.Brief); WriteLine("{0} {1}({2})", GetAccessLevel(c.AccessLevel), name, GetParamStr(c.Parameters)); WriteLine("{{"); IndentDepth++; if (c.Initializer != null) { MakeIndent(); var tob = c.Initializer.ThisOrBase; if (c.Initializer.ThisOrBase == "base") { tob = "super"; } Res.AppendFormat("{0}({1});\r\n", tob, string.Join(", ", c.Initializer.Arguments.ConvertAll(GetExpression))); } OutputStatements(c.Body); IndentDepth--; WriteLine("}}"); }
private void OutputField(Definition.FieldDef f) { //Summary(f.Brief); MakeIndent(); if (f.Argument == null) { Res.AppendFormat("{2} {3}{0} {1}", GetTypeSpecifier(f.Type), f.Name, GetAccessLevel(f.AccessLevel), f.IsStatic ? "static " : ""); if (f.Initializer != null) { Res.AppendFormat(" = {0};\r\n", GetExpression(f.Initializer)); } else { Res.AppendLine(";"); } } else { // 無理やりfixedArrayを再現 var atype = (Definition.ArrayType)f.Type; Res.AppendFormat("{0} {1}{2} {3} = new {4}[{5}];\r\n", GetAccessLevel(f.AccessLevel), f.IsStatic ? "static " : "", GetTypeSpecifier(f.Type), f.Name, GetTypeSpecifier(atype.BaseType), f.Argument); } }
private void OutputEnum(Definition.EnumDef es) { Summary(es.Summary); WriteLine("public enum {0} {{", es.Name); IndentDepth++; int count = 0; foreach (var e in es.Members) { Summary(e.Summary); MakeIndent(); Res.Append(e.Name); if (e.Value != null) { var expression = GetExpression(e.Value); var suffix = string.Empty; if (expression.Contains("swig")) { suffix = ".swigValue()"; } if (count != es.Members.Count - 1) { Res.AppendFormat("({0}{1}),\r\n", expression, suffix); } else { Res.AppendFormat("({0}{1});\r\n", expression, suffix); } } else { Res.AppendLine(","); } count++; } // 定型文 var idText = @" private final int id; private {0}(final int id) { this.id = id; } public int swigValue() { return id; } public static {0} swigToEnum(int id) { for ({0} e : values() ) { if (e.swigValue() == id) { return e; } } throw new IllegalArgumentException(""Not found : "" + id); } "; idText = idText.Replace("{0}", es.Name); Res.AppendLine(""); Res.Append(idText); Res.AppendLine(""); IndentDepth--; WriteLine("}}"); }
private void OutputFieldInInterface(Definition.FieldDef f) { //Summary(f.Brief); MakeIndent(); Res.AppendFormat("{0} {1};", GetTypeSpecifier(f.Type), f.Name); }
private void OutputStatement(Definition.Statement s) { if (s == null) { WriteLine("/* debug: null statement */"); } else if (s is Definition.BlockStatement) { var s2 = (Definition.BlockStatement)s; OutputStatements(s2.Statements); } else if (s is Definition.ContinueStatement) { WriteLine("continue;"); } else if (s is Definition.BreakStatement) { WriteLine("break;"); } else if (s is Definition.ExpressionStatement) { var s2 = (Definition.ExpressionStatement)s; WriteLine("{0};", GetExpression(s2.Expression)); } else if (s is Definition.ForeachStatement) { var s2 = (Definition.ForeachStatement)s; WriteLine("for({0} {1}: {2})", GetTypeSpecifier(s2.Type), s2.Name, GetExpression(s2.Value)); WriteLine("{{"); IndentDepth++; OutputStatement(s2.Statement); IndentDepth--; WriteLine("}}"); } else if (s is Definition.ForStatement) { var s2 = (Definition.ForStatement)s; WriteLine("for({0} {1} = {2}; {3}; {4})", GetTypeSpecifier(s2.Declaration.Type), s2.Declaration.Name, GetExpression(s2.Declaration.Value), GetExpression(s2.Condition), GetExpression(s2.Incrementor)); WriteLine("{{"); IndentDepth++; OutputStatement(s2.Statement); IndentDepth--; WriteLine("}}"); } else if (s is Definition.WhileStatement) { var s2 = (Definition.WhileStatement)s; WriteLine("while({0})", GetExpression(s2.Condition)); WriteLine("{{"); IndentDepth++; OutputStatement(s2.Statement); IndentDepth--; WriteLine("}}"); WriteLine(""); } else if (s is Definition.IfStatement) { var s2 = (Definition.IfStatement)s; WriteLine("if({0})", GetExpression(s2.Condition)); WriteLine("{{"); IndentDepth++; OutputStatement(s2.TrueStatement); IndentDepth--; if (s2.FalseStatement != null) { WriteLine("}}"); WriteLine("else"); WriteLine("{{"); IndentDepth++; OutputStatement(s2.FalseStatement); IndentDepth--; WriteLine("}}"); } else { WriteLine("}}"); } } else if (s is Definition.ReturnStatement) { var s2 = (Definition.ReturnStatement)s; WriteLine("return {0};", GetExpression(s2.Return)); } else if (s is Definition.VariableDeclarationStatement) { MakeIndent(); var s2 = (Definition.VariableDeclarationStatement)s; Res.AppendFormat("{0} {1}", GetTypeSpecifier(s2.Type), s2.Name); if (s2.Value != null) { Res.AppendFormat(" = {0};\r\n", GetExpression(s2.Value)); } else { Res.AppendLine(";"); } } else if (s is Definition.LockStatement) { var s2 = (Definition.LockStatement)s; WriteLine("synchronized({0})", GetExpression(s2.Expression)); WriteLine("{{"); IndentDepth++; OutputStatement(s2.Statement); IndentDepth--; WriteLine("}}"); } else if (s is Definition.CommentStatement) { var s2 = (Definition.CommentStatement)s; WriteLine("// {0}", s2.Text); } else { throw new NotImplementedException("unknown statement " + s.GetType().ToString()); } }
public override void Translate(string targetDir, Definition.Definitions definisions) { var sep = Path.DirectorySeparatorChar.ToString(); foreach (Definition.EnumDef o in definisions.Enums) { IndentDepth = 0; if (o.IsDefinedBySWIG) { continue; } var subDir = targetDir + string.Join(sep, o.Namespace.Split('.')); System.IO.Directory.CreateDirectory(subDir); var of = System.IO.File.CreateText(subDir + sep + o.Name + ".java"); if (o.Namespace != string.Empty) { Res.AppendFormat("package {0};\r\n\r\n", o.Namespace); } OutputEnum(o); of.Write(Res.ToString()); of.Close(); Res.Clear(); } foreach (var o in definisions.Classes) { IndentDepth = 0; if (o.IsDefinedBySWIG) { continue; } if (o.IsDefinedDefault) { continue; } if (!o.IsExported) { continue; } var subDir = targetDir + string.Join(sep, o.Namespace.Split('.')); System.IO.Directory.CreateDirectory(subDir); var of = System.IO.File.CreateText(subDir + sep + o.Name + ".java"); if (o.Namespace != string.Empty) { Res.AppendFormat("package {0};\r\n\r\n", o.Namespace); } OutputClass(o); of.Write(Res.ToString()); of.Close(); Res.Clear(); } foreach (var o in definisions.Structs) { if (o.IsDefinedDefault) { continue; } IndentDepth = 0; var subDir = targetDir + string.Join(sep, o.Namespace.Split('.')); System.IO.Directory.CreateDirectory(subDir); var of = System.IO.File.CreateText(subDir + sep + o.Name + ".java"); if (o.Namespace != string.Empty) { Res.AppendFormat("package {0};\r\n\r\n", o.Namespace); } OutputStruct(o); of.Write(Res.ToString()); of.Close(); Res.Clear(); } foreach (var o in definisions.Interfaces) { IndentDepth = 0; var subDir = targetDir + string.Join(sep, o.Namespace.Split('.')); System.IO.Directory.CreateDirectory(subDir); var of = System.IO.File.CreateText(subDir + sep + o.Name + ".java"); if (o.Namespace != string.Empty) { Res.AppendFormat("package {0};\r\n\r\n", o.Namespace); } OutputInterface(o); of.Write(Res.ToString()); of.Close(); Res.Clear(); } }