private static object ConvertAndRunCode( EngineWrapper engine, Microsoft.CodeAnalysis.SyntaxNode csharpAstNode, string[] requiredImports = null) { var rewritten = MultiLineLambdaRewriter.RewriteMultiLineLambdas(csharpAstNode); var pythonAst = new CSharpToPythonConvert().Visit(rewritten); var convertedCode = PythonAstPrinter.PrintPythonAst(pythonAst); var extraImports = requiredImports is null ? "" : string.Join("\r\n", requiredImports.Select(i => "import " + i)); convertedCode = "import clr\r\n" + extraImports + "\r\n" + convertedCode; if (pythonAst is PyAst.SuiteStatement suiteStmt) { var pythonStatements = suiteStmt.Statements .Where(s => !(s is PyAst.FromImportStatement || s is PyAst.ImportStatement)).ToList(); // If the AST contained only a function definition, run it if (pythonStatements.Count == 1 && pythonStatements.Single() is PyAst.FunctionDefinition funcDef) { convertedCode += $"\r\n{funcDef.Name}()"; } if (pythonStatements.Count >= 1 && pythonStatements.All(s => s is PyAst.ClassDefinition)) { var lastClassDef = (PyAst.ClassDefinition)pythonStatements.Last(); convertedCode += $"\r\n{lastClassDef.Name}()"; } } var scope = engine.Engine.CreateScope(); var source = engine.Engine.CreateScriptSourceFromString(convertedCode, Microsoft.Scripting.SourceCodeKind.AutoDetect); return(source.Execute(scope)); }
private void AssertGeneratedFileDesc(GeneratedFileDescription fileDesc, string fileName, string namespaceName, string className, string[] methodNames) { Assert.True(fileDesc.Name == fileName); SyntaxNode root = CSharpSyntaxTree.ParseText(fileDesc.Text).GetRoot(); ClassDeclarationSyntax[] classes = root.DescendantNodes().OfType <ClassDeclarationSyntax>().ToArray(); Assert.True(classes.Length == 1); ClassDeclarationSyntax clazz = classes[0]; Assert.True(clazz.Identifier.ValueText == className); Assert.True((clazz.Parent as NamespaceDeclarationSyntax)?.Name?.ToString() == namespaceName); MethodDeclarationSyntax[] methods = clazz.DescendantNodes().OfType <MethodDeclarationSyntax>().ToArray(); Assert.True(methods.Length == methodNames.Length); foreach (MethodDeclarationSyntax method in methods) { Assert.Contains(method.Identifier.ValueText, methodNames); Assert.True(method.AttributeLists.Count == 1); Assert.True(method.AttributeLists.First().Attributes.First().Name.ToString() == "Fact"); } }
public void Construct(Microsoft.CodeAnalysis.SyntaxNode node) { _buildingSpans = true; Visit(node); _buildingSpans = false; Visit(node); }
private static string ConvertCsharpAST(Microsoft.CodeAnalysis.SyntaxNode csharpAst) { var rewritten = MultiLineLambdaRewriter.RewriteMultiLineLambdas(csharpAst); var pythonAst = new CSharpToPythonConvert().Visit(rewritten); return(PythonAstPrinter.PrintPythonAst(pythonAst)); }
private int GetConditionComplexity(Microsoft.CodeAnalysis.SyntaxNode node) { var conditionsVisitor = CreateConditionsVisitor(); conditionsVisitor.Visit(node); return(conditionsVisitor.Count); }
public static Neo.ASML.Node.ASMProject Compile(Microsoft.CodeAnalysis.SyntaxNode node) { var proj = new Neo.ASML.Node.ASMProject(); CompileContext context = new CompileContext(); CompileNode(context, proj, node); return(proj); }
private string GetNodePath(Microsoft.CodeAnalysis.SyntaxNode node) { if (node == null) { return(""); } var parent = GetNodePath(node.Parent); return((string.IsNullOrEmpty(parent) ? "" : $"{parent} → ") + node.Kind()); }
public static void SelectNode(this IWpfTextView view, Microsoft.CodeAnalysis.SyntaxNode node, bool includeTrivia) { if (includeTrivia) { view.Selection.Select(new SnapshotSpan(view.TextSnapshot, node.FullSpan.Start, node.FullSpan.Length), false); } else { view.Selection.Select(new SnapshotSpan(view.TextSnapshot, node.Span.Start, node.Span.Length), false); } }
public void TestAllConstructs() { string csharpConstructs = TestFilesHelper.GetFile("AllConstructs.cs"); Microsoft.CodeAnalysis.SyntaxNode vbActualConstructs = Converter.Convert(SyntaxFactory.ParseSyntaxTree(csharpConstructs)); string vbActual = vbActualConstructs.ToFullString(); string vbExpected = TestFilesHelper.GetFile("AllConstructs.txt"); Assert.Equal(vbExpected, vbActual); }
public override void Visit(Microsoft.CodeAnalysis.SyntaxNode node) { if (_targetPatternRegEx.Match(node.ToString()).Success) { Messages.AppendLine(String.Format("{0} >{1}<", GetNodeContext((CSharpSyntaxNode)node), node.ToString())); } base.Visit(node); }
static void evaluate(Microsoft.CodeAnalysis.SyntaxNode n) { if (n is Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax) { } else if (n is Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax) { evaluate(((Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax)n).SyntaxTree.GetRoot()); } else if (n is Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax) { } }
public CodeInfo InsertActiveNodeToExternal(string externalFullPath, string serverMethodFolderPath, string methodName, Microsoft.CodeAnalysis.SyntaxNode syntaxNode) { throw new NotImplementedException(); }
public CodeInfo InsertActiveNodeToMainMethod(string mainMethodFullPath, string serverMethodFolderPath, Microsoft.CodeAnalysis.SyntaxNode activeSyntaxNode, string activeDocumentPath) { throw new NotImplementedException(); }
public CodeInfo RemoveActiveNodeFromActiveDocument(Microsoft.CodeAnalysis.Document activeDocument, Microsoft.CodeAnalysis.SyntaxNode activeSyntaxNode, string serverMethodFolderPath) { throw new NotImplementedException(); }
private ConditionalAccessExpressionSyntax GetConditionalAccessExpressionSyntax(Microsoft.CodeAnalysis.SyntaxNode parent) { if (parent == null) { return(null); } if (parent.Parent is ConditionalAccessExpressionSyntax conditionalAccess) { if (!conditionalAccess.ToString().StartsWith("." + memberBindingExpression.Name, StringComparison.Ordinal)) { return(conditionalAccess); } } return(GetConditionalAccessExpressionSyntax(parent.Parent)); }
private static bool IsInterfaceDeclarationSyntax(Microsoft.CodeAnalysis.SyntaxNode declarationSyntax) { return(declarationSyntax.Parent is InterfaceDeclarationSyntax); }
private static bool HasNoLiteralExpression(Microsoft.CodeAnalysis.SyntaxNode propertyDeclarationSyntax) { return(!propertyDeclarationSyntax.DescendantNodes().OfType <LiteralExpressionSyntax>().Any()); }
static void CompileNode(CompileContext context, Neo.ASML.Node.ASMProject project, Microsoft.CodeAnalysis.SyntaxNode node) { if (node is Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax) { var func = new Neo.ASML.Node.ASMFunction(); var srcmethod = node as Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax; func.Name = srcmethod.Identifier.ValueText; project.nodes.Add(func); context.variables = new List <string>(); foreach (var op in srcmethod.Body.Statements) { if (op is Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax) { var localvar = op as Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax; var vars = localvar.Declaration.Variables; foreach (var _var in vars) { context.variables.Add(_var.Identifier.ValueText); var index = context.variables.IndexOf(_var.Identifier.ValueText); if (_var.Initializer != null) { var v = _var.Initializer.Value.ToString(); func.nodes.Add(new Neo.ASML.Node.ASMComment() { text = "//" + _var.ToString() }); //push setitem func.nodes.Add(new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.DUPFROMALTSTACK), commentRight = "//variables array" }); func.nodes.Add(new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.CreatePush(), valuetext = index.ToString(), commentRight = "//index" }); //push value CompileExpression(context, func, _var.Initializer.Value, "//value"); func.nodes.Add(new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.SETITEM) }); } } //define a local value } if (op is Microsoft.CodeAnalysis.CSharp.Syntax.ReturnStatementSyntax) { var ret = op as Microsoft.CodeAnalysis.CSharp.Syntax.ReturnStatementSyntax; if (ret.Expression != null) { CompileExpression(context, func, ret.Expression, "//" + ret.ToString()); } func.nodes.Add(new Neo.ASML.Node.ASMComment() { text = "//clear and return" }); func.nodes.Add(new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.FROMALTSTACK) }); func.nodes.Add(new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.DROP) }); func.nodes.Add(new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.RET) }); } } var variablecount = context.variables.Count; func.nodes.Insert(0, new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.CreatePush(), valuetext = variablecount.ToString(), commentRight = "//insert varlist code" }); func.nodes.Insert(1, new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.NEWARRAY) }); func.nodes.Insert(2, new Neo.ASML.Node.ASMInstruction() { opcode = Neo.ASML.Node.ASMOpCode.Create(Neo.VM.OpCode.TOALTSTACK) }); } else { var subnodes = node.ChildNodes(); foreach (var sn in subnodes) { CompileNode(context, project, sn); } } }
private static bool HasAccessorDeclarationSyntaxAsDescendantNodes(Microsoft.CodeAnalysis.SyntaxNode propertyDeclarationSyntax) { return(propertyDeclarationSyntax.DescendantNodes().OfType <AccessorDeclarationSyntax>().Any()); }
static void DumpAstNode(Microsoft.CodeAnalysis.SyntaxNode node, string space) { if (node is Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax) { } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax) { var cl = node as Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax; var outtxt = space + "<class define>:" + cl.Identifier.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.PredefinedTypeSyntax) { var pt = node as Microsoft.CodeAnalysis.CSharp.Syntax.PredefinedTypeSyntax; var outtxt = space + "<type>:" + pt.Keyword.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclaratorSyntax) { var vd = node as Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclaratorSyntax; var outtxt = space + "<variable>:" + vd.Identifier.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.EqualsValueClauseSyntax) { var eq = node as Microsoft.CodeAnalysis.CSharp.Syntax.EqualsValueClauseSyntax; var outtxt = space + "<Equals>:" + eq.EqualsToken.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.LiteralExpressionSyntax) { var lt = node as Microsoft.CodeAnalysis.CSharp.Syntax.LiteralExpressionSyntax; var outtxt = space + "<value>:" + lt.Token.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax) { var define = (node as Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax).Declaration; var subv = define.Variables; Console.WriteLine(space + "<local value>"); foreach (var sv in subv) { DumpAstNode(sv, " " + space); } return; } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax) { var bin = node as Microsoft.CodeAnalysis.CSharp.Syntax.BinaryExpressionSyntax; var outtxt = space + "<binary>:" + bin.OperatorToken.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax) { var id = node as Microsoft.CodeAnalysis.CSharp.Syntax.IdentifierNameSyntax; var outtxt = space + "<id>:" + id.Identifier.ValueText; Console.WriteLine(outtxt); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.ReturnStatementSyntax) { Console.WriteLine(space + "<return>"); } else if (node is Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax) { var me = node as Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax; var outtxt = space + "<method define>:" + me.Identifier.ValueText; Console.WriteLine(outtxt); var subbody = me.Body.ChildNodes(); foreach (var sn in subbody) { DumpAstNode(sn, " " + space); } return; } else { var outtxt = space + "node=" + node.GetType().ToString(); Console.WriteLine(outtxt); } var subnode = node.ChildNodes(); foreach (var sn in subnode) { DumpAstNode(sn, " " + space); } }
static IEnumerable <object> ImmutableClasses(Microsoft.CodeAnalysis.SyntaxNode node) { foreach (var child in node.ChildNodes()) { if (child is UsingDirectiveSyntax) { continue; } if (child is AttributeListSyntax) { continue; } if (child is EnumDeclarationSyntax) { continue; } if (child is QualifiedNameSyntax) { continue; } if (child is BaseListSyntax || child is FieldDeclarationSyntax || child is PropertyDeclarationSyntax || child is ConstructorDeclarationSyntax || child is MethodDeclarationSyntax) { continue; } if (child is InterfaceDeclarationSyntax) { continue; } if (child is TypeParameterListSyntax) { continue; } var isDrill = child is NamespaceDeclarationSyntax || child is ClassDeclarationSyntax; var isView = !isDrill; if (child is ClassDeclarationSyntax) { isView = true; } if (isDrill) { foreach (var cc in ImmutableClasses(child)) { yield return(cc); } } //if (child is ClassDeclarationSyntax) //{ // foreach (var member in ) // foreach (var cc in ImmutableClasses(child)) // yield return cc; //} if (isView) { var @class = child as ClassDeclarationSyntax; if (@class != null) { yield return(@class); } else { yield return(child.GetType().FullName); } } } }
private static bool IsAbstractMethodDefinition(Microsoft.CodeAnalysis.SyntaxNode declarationSyntax) { var childTokens = declarationSyntax.ChildTokens(); return(childTokens.Any(t => t.Kind() == SyntaxKind.AbstractKeyword)); }
public void Construct(Microsoft.CodeAnalysis.SyntaxNode node) { Visit(node); }
static void DumpAst(Microsoft.CodeAnalysis.SyntaxNode node) { Console.WriteLine("==Dump AST:"); DumpAstNode(node, ""); }