Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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);
                    }
                }
            }
        }
Exemplo n.º 3
0
        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);
            }
        }