public override string GetCode(CSharpSyntaxNode Node) { if (Node.Kind() == SyntaxKind.MethodDeclaration) { string name = Node.ChildTokens().ToList()[Node.ChildTokens().Count() - 1].ToString(); FullName += "." + name + "."; FullName = string.Join(".", FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)); if (name == "Main") { MainPath = FullName; } string[] arr = Node.ChildTokens().ToList().Select(t => t.ToString()).ToArray(); if (Node.HasChildKind(SyntaxKind.PredefinedType)) { string T = Node.Kind2Child(SyntaxKind.PredefinedType).ChildTokens().ToList()[0].ToString(); return((arr.Contains("public") ? "public: " : "") + (arr.Contains("private") ? "private: " : "") + (arr.Contains("static") ? "static " : "") + T + " " + name + Environment.NewLine); } } foreach (CSharpSyntaxNode node in Node.ChildNodes()) { string code = GetCode(node); if (code != null) { return(code); } } return(null); }
private static void AST2CPP(CSharpSyntaxNode ROOT, ref string CPP_SRC) { CodeBlock b = null; if (debug) { for (int j = 0; j <= level; j++) { CPP_SRC += "\t"; } CPP_SRC += ROOT.Kind().ToString() + " = [" + string.Join(", ", ROOT.ChildTokens().ToList()) + "]" + Environment.NewLine; } else { List <CodeBlock> codeBlocks = new List <CodeBlock>(); foreach (Type type in Assembly.GetExecutingAssembly(). GetTypes().Where(t => t != typeof(CodeBlock) && t.IsSubclassOf(typeof(CodeBlock)))) { codeBlocks.Add((CodeBlock)Activator.CreateInstance(type, false)); } foreach (CodeBlock codeBlock in codeBlocks) { if (codeBlock.IsMatch(ROOT)) { CPP_SRC += codeBlock.GetCode(ROOT); b = codeBlock; } } } if (!debug && b != null) { CPP_SRC += b.GetBody().Split(new string[] { "%BODY%" }, StringSplitOptions.None)[0]; } level++; foreach (CSharpSyntaxNode node in ROOT.ChildNodes()) { AST2CPP(node, ref CPP_SRC); } level--; if (!debug && b != null) { CPP_SRC += b.GetBody().Split(new string[] { "%BODY%" }, StringSplitOptions.None)[1]; } if (b != null && b.GetBody().ToCharArray().Contains('{') && b.GetBody().ToCharArray().Contains('}')) { List <string> lst = FullName.Split('.').ToList(); if (lst.Any()) { lst.RemoveAt(lst.Count - 1); } if (!debug) { FullName = string.Join(".", lst); } } }
public override string GetCode(CSharpSyntaxNode Node) { if (Node.Kind() == SyntaxKind.ClassDeclaration) { string name = Node.ChildTokens().ToList()[Node.ChildTokens().Count() - 3].ToString(); FullName += "." + name + "."; FullName = string.Join(".", FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)); string[] arr = Node.ChildTokens().ToList().Select(t => t.ToString()).ToArray(); return((arr.Contains("static") ? "static " : "") + "class " + name + Environment.NewLine); } foreach (CSharpSyntaxNode node in Node.ChildNodes()) { string code = GetCode(node); if (code != null) { return(code); } } return(null); }
public void Initialize(string defaultName) { string name = (from c in nodeReference.ChildTokens() where c.Kind() == SyntaxKind.IdentifierToken select c.Text).FirstOrDefault <string>(); if (String.IsNullOrEmpty(name)) { this.Name = defaultName; } else { this.Name = name; } }
public override string GetCode(CSharpSyntaxNode Node) { if (Node.Kind() == SyntaxKind.StringLiteralExpression) { foreach (SyntaxToken token in Node.ChildTokens()) { if (token.ToString().StartsWith("\"#")) { return(token.ToString().Substring(1, token.ToString().Length - 2) + Environment.NewLine); } } } foreach (CSharpSyntaxNode node in Node.ChildNodes()) { string code = GetCode(node); if (code != null) { return(code); } } return(null); }