예제 #1
0
        public void CreateIfElseMethod()
        {
            List <TypeSpecifier> argumentTypes = new List <TypeSpecifier>()
            {
                typeof(int),
                typeof(bool),
            };

            List <TypeSpecifier> returnTypes = new List <TypeSpecifier>()
            {
                typeof(int),
            };

            // Create method
            ifElseMethod = new Method("IfElse")
            {
                Modifiers = MethodModifiers.Public
            };
            ifElseMethod.ArgumentTypes.AddRange(argumentTypes);
            ifElseMethod.ReturnTypes.AddRange(returnTypes);

            // Create nodes
            IfElseNode  ifElseNode  = new IfElseNode(ifElseMethod);
            LiteralNode literalNode = new LiteralNode(ifElseMethod, typeof(int), 123);

            // Connect exec nodes
            GraphUtil.ConnectExecPins(ifElseMethod.EntryNode.InitialExecutionPin, ifElseNode.ExecutionPin);
            GraphUtil.ConnectExecPins(ifElseNode.TruePin, ifElseMethod.ReturnNode.ReturnPin);
            GraphUtil.ConnectExecPins(ifElseNode.FalsePin, ifElseMethod.ReturnNode.ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[1], ifElseNode.ConditionPin);
            GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[0], ifElseMethod.ReturnNode.InputDataPins[0]);
            GraphUtil.ConnectDataPins(literalNode.ValuePin, ifElseMethod.ReturnNode.InputDataPins[0]);
        }
예제 #2
0
        public void CreateStringLengthMethod()
        {
            List <TypeSpecifier> returnTypes = new List <TypeSpecifier>()
            {
                TypeSpecifier.FromType <int>(),
            };

            // Create method
            stringLengthMethod = new Method("StringLength")
            {
                Class     = cls,
                Modifiers = MethodModifiers.Public
            };

            stringLengthMethod.ReturnTypes.AddRange(returnTypes);

            // Create nodes
            VariableGetterNode getStringNode = new VariableGetterNode(stringLengthMethod, cls.Type, new Variable("testVariable", TypeSpecifier.FromType <string>()));
            VariableGetterNode getLengthNode = new VariableGetterNode(stringLengthMethod, cls.Type, new Variable("Length", TypeSpecifier.FromType <int>()));

            // Connect node execs
            GraphUtil.ConnectExecPins(stringLengthMethod.EntryNode.InitialExecutionPin, stringLengthMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(getStringNode.ValuePin, getLengthNode.TargetPin);
            GraphUtil.ConnectDataPins(getLengthNode.ValuePin, stringLengthMethod.ReturnNodes.First().InputDataPins[0]);
        }
예제 #3
0
        public void CreateStringLengthMethod()
        {
            List <TypeSpecifier> argumentTypes = new List <TypeSpecifier>()
            {
                typeof(string),
            };

            List <TypeSpecifier> returnTypes = new List <TypeSpecifier>()
            {
                typeof(int),
            };

            // Create method
            stringLengthMethod = new Method("StringLength")
            {
                Modifiers = MethodModifiers.Public
            };

            stringLengthMethod.ArgumentTypes.AddRange(argumentTypes);
            stringLengthMethod.ReturnTypes.AddRange(returnTypes);

            // Create nodes
            VariableGetterNode getLengthNode = new VariableGetterNode(stringLengthMethod, typeof(string), "Length", typeof(int));

            // Connect node execs
            GraphUtil.ConnectExecPins(stringLengthMethod.EntryNode.InitialExecutionPin, stringLengthMethod.ReturnNode.ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(stringLengthMethod.EntryNode.OutputDataPins[0], getLengthNode.InputDataPins[0]);
            GraphUtil.ConnectDataPins(getLengthNode.OutputDataPins[0], stringLengthMethod.ReturnNode.InputDataPins[0]);
        }
예제 #4
0
        public void CreateMainMethod()
        {
            mainMethod = new Method("Main")
            {
                Class     = cls,
                Modifiers = MethodModifiers.Static
            };

            MethodSpecifier stringLengthSpecifier = new MethodSpecifier("StringLength", new Named <BaseType> [0], new List <TypeSpecifier>()
            {
                TypeSpecifier.FromType <int>()
            }, MethodModifiers.Public, TypeSpecifier.FromType <string>(), Array.Empty <BaseType>());
            //MethodSpecifier writeConsoleSpecifier = typeof(Console).GetMethods().Single(m => m.Name == "WriteLine" && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(string));
            TypeSpecifier   stringType            = TypeSpecifier.FromType <string>();
            MethodSpecifier writeConsoleSpecifier = new MethodSpecifier("WriteLine", new Named <BaseType>[] { new Named <BaseType>("argName", stringType) }, new BaseType[0], MethodModifiers.Public, TypeSpecifier.FromType(typeof(Console)), new BaseType[0]);

            // Create nodes
            LiteralNode        stringLiteralNode   = LiteralNode.WithValue(mainMethod, "Hello World");
            VariableSetterNode setStringNode       = new VariableSetterNode(mainMethod, cls.Type, new Variable("testVariable", TypeSpecifier.FromType <string>()));
            CallMethodNode     getStringLengthNode = new CallMethodNode(mainMethod, stringLengthSpecifier);
            CallMethodNode     writeConsoleNode    = new CallMethodNode(mainMethod, writeConsoleSpecifier);

            // Connect node execs
            GraphUtil.ConnectExecPins(mainMethod.EntryNode.InitialExecutionPin, setStringNode.InputExecPins[0]);
            GraphUtil.ConnectExecPins(setStringNode.OutputExecPins[0], getStringLengthNode.InputExecPins[0]);
            GraphUtil.ConnectExecPins(getStringLengthNode.OutputExecPins[0], writeConsoleNode.InputExecPins[0]);
            GraphUtil.ConnectExecPins(writeConsoleNode.OutputExecPins[0], mainMethod.ReturnNodes.First().InputExecPins[0]);

            // Connect node data
            GraphUtil.ConnectDataPins(stringLiteralNode.ValuePin, setStringNode.NewValuePin);
            GraphUtil.ConnectDataPins(getStringLengthNode.OutputDataPins[0], writeConsoleNode.ArgumentPins[0]);
        }
예제 #5
0
        public void CreateStringLengthMethod()
        {
            // Create method
            stringLengthMethod = new MethodGraph("StringLength")
            {
                Class     = cls,
                Modifiers = MethodModifiers.None
            };

            List <TypeNode> returnTypeNodes = new List <TypeNode>()
            {
                new TypeNode(stringLengthMethod, TypeSpecifier.FromType <int>()),
            };

            for (int i = 0; i < returnTypeNodes.Count; i++)
            {
                stringLengthMethod.MainReturnNode.AddReturnType();
                GraphUtil.ConnectTypePins(returnTypeNodes[i].OutputTypePins[0], stringLengthMethod.MainReturnNode.InputTypePins[i]);
            }

            TypeSpecifier stringType = TypeSpecifier.FromType <string>();
            TypeSpecifier intType    = TypeSpecifier.FromType <int>();

            // Create nodes
            VariableGetterNode getStringNode = new VariableGetterNode(stringLengthMethod, new VariableSpecifier("testVariable", stringType, MemberVisibility.Public, MemberVisibility.Public, stringType, VariableModifiers.None));
            VariableGetterNode getLengthNode = new VariableGetterNode(stringLengthMethod, new VariableSpecifier("Length", intType, MemberVisibility.Public, MemberVisibility.Public, stringType, VariableModifiers.None));

            // Connect node execs
            GraphUtil.ConnectExecPins(stringLengthMethod.EntryNode.InitialExecutionPin, stringLengthMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(getStringNode.ValuePin, getLengthNode.TargetPin);
            GraphUtil.ConnectDataPins(getLengthNode.ValuePin, stringLengthMethod.ReturnNodes.First().InputDataPins[0]);
        }
예제 #6
0
        public void CreateStringLengthMethod()
        {
            List <TypeSpecifier> argumentTypes = new List <TypeSpecifier>()
            {
                TypeSpecifier.FromType <string>(),
            };

            // Create method
            stringLengthMethod = new Method("StringLength")
            {
                Visibility = MemberVisibility.Public
            };

            // Add arguments
            List <TypeNode> argTypeNodes = new List <TypeNode>()
            {
                new TypeNode(stringLengthMethod, TypeSpecifier.FromType <string>()),
            };

            for (int i = 0; i < argTypeNodes.Count; i++)
            {
                stringLengthMethod.EntryNode.AddArgument();
                GraphUtil.ConnectTypePins(argTypeNodes[i].OutputTypePins[0], stringLengthMethod.EntryNode.InputTypePins[i]);
            }

            // Add return types
            List <TypeNode> returnTypeNodes = new List <TypeNode>()
            {
                new TypeNode(stringLengthMethod, TypeSpecifier.FromType <int>()),
            };

            for (int i = 0; i < returnTypeNodes.Count; i++)
            {
                stringLengthMethod.MainReturnNode.AddReturnType();
                GraphUtil.ConnectTypePins(returnTypeNodes[i].OutputTypePins[0], stringLengthMethod.MainReturnNode.InputTypePins[i]);
            }

            // Create nodes
            var getLengthNode = new VariableGetterNode(stringLengthMethod, new VariableSpecifier("Length", TypeSpecifier.FromType <int>(),
                                                                                                 MemberVisibility.Public, MemberVisibility.Public, TypeSpecifier.FromType <string>(), VariableModifiers.None));

            // Connect node execs
            GraphUtil.ConnectExecPins(stringLengthMethod.EntryNode.InitialExecutionPin, stringLengthMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(stringLengthMethod.EntryNode.OutputDataPins[0], getLengthNode.InputDataPins[0]);
            GraphUtil.ConnectDataPins(getLengthNode.OutputDataPins[0], stringLengthMethod.ReturnNodes.First().InputDataPins[0]);
        }
예제 #7
0
        public void CreateIfElseMethod()
        {
            // Create method
            ifElseMethod = new Method("IfElse")
            {
                Visibility = MemberVisibility.Public
            };

            // Add arguments
            List <TypeNode> argTypeNodes = new List <TypeNode>()
            {
                new TypeNode(ifElseMethod, TypeSpecifier.FromType <int>()),
                new TypeNode(ifElseMethod, TypeSpecifier.FromType <bool>()),
            };

            for (int i = 0; i < argTypeNodes.Count; i++)
            {
                ifElseMethod.EntryNode.AddArgument();
                GraphUtil.ConnectTypePins(argTypeNodes[i].OutputTypePins[0], ifElseMethod.EntryNode.InputTypePins[i]);
            }

            // Add return types
            List <TypeNode> returnTypeNodes = new List <TypeNode>()
            {
                new TypeNode(ifElseMethod, TypeSpecifier.FromType <int>()),
            };

            for (int i = 0; i < returnTypeNodes.Count; i++)
            {
                ifElseMethod.MainReturnNode.AddReturnType();
                GraphUtil.ConnectTypePins(returnTypeNodes[i].OutputTypePins[0], ifElseMethod.MainReturnNode.InputTypePins[i]);
            }

            // Create nodes
            IfElseNode  ifElseNode  = new IfElseNode(ifElseMethod);
            LiteralNode literalNode = LiteralNode.WithValue(ifElseMethod, 123);

            // Connect exec nodes
            GraphUtil.ConnectExecPins(ifElseMethod.EntryNode.InitialExecutionPin, ifElseNode.ExecutionPin);
            GraphUtil.ConnectExecPins(ifElseNode.TruePin, ifElseMethod.ReturnNodes.First().ReturnPin);
            GraphUtil.ConnectExecPins(ifElseNode.FalsePin, ifElseMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[1], ifElseNode.ConditionPin);
            GraphUtil.ConnectDataPins(ifElseMethod.EntryNode.OutputDataPins[0], ifElseMethod.ReturnNodes.First().InputDataPins[0]);
            GraphUtil.ConnectDataPins(literalNode.ValuePin, ifElseMethod.ReturnNodes.First().InputDataPins[0]);
        }
예제 #8
0
        public void CreateForLoopMethod()
        {
            // Create method
            forLoopMethod = new Method("ForLoop")
            {
                Modifiers = MethodModifiers.Public
            };

            // Create nodes
            LiteralNode maxIndexLiteralNode = new LiteralNode(forLoopMethod, typeof(int), 10);
            ForLoopNode forLoopNode         = new ForLoopNode(forLoopMethod);

            // Connect exec nodes
            GraphUtil.ConnectExecPins(forLoopMethod.EntryNode.InitialExecutionPin, forLoopNode.ExecutionPin);
            GraphUtil.ConnectExecPins(forLoopNode.CompletedPin, forLoopMethod.ReturnNode.ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(maxIndexLiteralNode.ValuePin, forLoopNode.MaxIndexPin);
        }
예제 #9
0
        public void CreateForLoopMethod()
        {
            // Create method
            forLoopMethod = new Method("ForLoop")
            {
                Visibility = MemberVisibility.Public
            };

            // Create nodes
            LiteralNode maxIndexLiteralNode = LiteralNode.WithValue(forLoopMethod, 10);
            ForLoopNode forLoopNode         = new ForLoopNode(forLoopMethod);

            // Connect exec nodes
            GraphUtil.ConnectExecPins(forLoopMethod.EntryNode.InitialExecutionPin, forLoopNode.ExecutionPin);
            GraphUtil.ConnectExecPins(forLoopNode.CompletedPin, forLoopMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(maxIndexLiteralNode.ValuePin, forLoopNode.MaxIndexPin);
        }
        public void TestDelegate()
        {
            // Create method
            Method delegateMethod = new Method("DelegateMethod")
            {
                Visibility = MemberVisibility.Public
            };

            List <TypeNode> returnTypeNodes = new List <TypeNode>()
            {
                new TypeNode(delegateMethod, TypeSpecifier.FromType <Func <int, string, float> >()),
            };

            for (int i = 0; i < returnTypeNodes.Count; i++)
            {
                delegateMethod.MainReturnNode.AddReturnType();
                GraphUtil.ConnectTypePins(returnTypeNodes[i].OutputTypePins[0], delegateMethod.MainReturnNode.InputTypePins[i]);
            }

            MethodSpecifier delegateMethodSpecifier = new MethodSpecifier("TestMethod",
                                                                          new MethodParameter[]
            {
                new MethodParameter("arg1", TypeSpecifier.FromType <int>(), MethodParameterPassType.Default),
                new MethodParameter("arg2", TypeSpecifier.FromType <string>(), MethodParameterPassType.Default)
            },
                                                                          new BaseType[] { TypeSpecifier.FromType <float>() },
                                                                          MethodModifiers.Static, MemberVisibility.Public,
                                                                          TypeSpecifier.FromType <double>(), Array.Empty <BaseType>());

            // Create nodes
            MakeDelegateNode makeDelegateNode = new MakeDelegateNode(delegateMethod, delegateMethodSpecifier);

            // Connect node execs
            GraphUtil.ConnectExecPins(delegateMethod.EntryNode.InitialExecutionPin, delegateMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(makeDelegateNode.OutputDataPins[0], delegateMethod.ReturnNodes.First().InputDataPins[0]);

            string translated = methodTranslator.Translate(delegateMethod, true);
        }
예제 #11
0
        public void TestDelegate()
        {
            List <TypeSpecifier> argumentTypes = new List <TypeSpecifier>()
            {
            };

            List <TypeSpecifier> returnTypes = new List <TypeSpecifier>()
            {
                typeof(Func <int, string, float>)
            };

            // Create method
            Method delegateMethod = new Method("DelegateMethod")
            {
                Modifiers = MethodModifiers.Public
            };

            delegateMethod.ArgumentTypes.AddRange(argumentTypes);
            delegateMethod.ReturnTypes.AddRange(returnTypes);

            MethodSpecifier delegateMethodSpecifier = new MethodSpecifier("TestMethod",
                                                                          new BaseType[] { typeof(int), typeof(string) },
                                                                          new BaseType[] { typeof(float) },
                                                                          MethodModifiers.Static,
                                                                          typeof(double),
                                                                          Array.Empty <BaseType>());

            // Create nodes
            MakeDelegateNode makeDelegateNode = new MakeDelegateNode(delegateMethod, delegateMethodSpecifier);

            // Connect node execs
            GraphUtil.ConnectExecPins(delegateMethod.EntryNode.InitialExecutionPin, delegateMethod.ReturnNode.ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(makeDelegateNode.OutputDataPins[0], delegateMethod.ReturnNode.InputDataPins[0]);

            string translated = methodTranslator.Translate(delegateMethod);
        }
예제 #12
0
        public void TestDelegate()
        {
            List <TypeSpecifier> argumentTypes = new List <TypeSpecifier>()
            {
            };

            List <TypeSpecifier> returnTypes = new List <TypeSpecifier>()
            {
                TypeSpecifier.FromType <Func <int, string, float> >(),
            };

            // Create method
            Method delegateMethod = new Method("DelegateMethod")
            {
                Modifiers = MethodModifiers.Public
            };

            delegateMethod.ArgumentTypes.AddRange(argumentTypes);
            delegateMethod.ReturnTypes.AddRange(returnTypes);

            MethodSpecifier delegateMethodSpecifier = new MethodSpecifier("TestMethod",
                                                                          new Named <BaseType>[] { new Named <BaseType>("arg1", TypeSpecifier.FromType <int>()), new Named <BaseType>("arg2", TypeSpecifier.FromType <string>()) },
                                                                          new BaseType[] { TypeSpecifier.FromType <float>() },
                                                                          MethodModifiers.Static,
                                                                          TypeSpecifier.FromType <double>(),
                                                                          Array.Empty <BaseType>());

            // Create nodes
            MakeDelegateNode makeDelegateNode = new MakeDelegateNode(delegateMethod, delegateMethodSpecifier);

            // Connect node execs
            GraphUtil.ConnectExecPins(delegateMethod.EntryNode.InitialExecutionPin, delegateMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(makeDelegateNode.OutputDataPins[0], delegateMethod.ReturnNodes.First().InputDataPins[0]);

            string translated = methodTranslator.Translate(delegateMethod);
        }
예제 #13
0
        public void TestGenerics()
        {
            // Create the open class<T> which contains a List<T>

            GenericType genericClassArg = new GenericType("T");

            ClassGraph openClass = new ClassGraph();

            openClass.Name      = "OpenClass";
            openClass.Namespace = "Namespace";
            openClass.DeclaredGenericArguments.Add(genericClassArg);

            TypeSpecifier listType = TypeSpecifier.FromType(typeof(List <>));

            Assert.AreEqual(listType.GenericArguments.Count, 1);

            listType.GenericArguments[0] = genericClassArg;

            MethodGraph openMethod = new MethodGraph("OpenMethod");

            // Add open list parameter
            TypeNode listTypeNode = new TypeNode(openMethod, listType);

            openMethod.MainReturnNode.AddReturnType();
            GraphUtil.ConnectTypePins(listTypeNode.OutputTypePins[0], openMethod.MainReturnNode.InputTypePins[0]);

            DefaultNode defaultNode = new DefaultNode(openMethod);

            GraphUtil.ConnectTypePins(listTypeNode.OutputTypePins[0], defaultNode.TypePin);
            GraphUtil.ConnectDataPins(defaultNode.DefaultValuePin, openMethod.MainReturnNode.InputDataPins[0]);

            GraphUtil.ConnectExecPins(openMethod.EntryNode.InitialExecutionPin, openMethod.ReturnNodes.First().ReturnPin);

            openClass.Methods.Add(openMethod);

            // Create the closed class which contains a List<string>

            ClassGraph closedClass = new ClassGraph();

            closedClass.Name      = "ClosedClass";
            closedClass.Namespace = "Namespace";

            TypeSpecifier closedListType = TypeSpecifier.FromType <string>();

            MethodGraph closedMethod = new MethodGraph("ClosedMethod");

            // Add closed list parameter
            TypeNode closedListTypeNode = new TypeNode(closedMethod, closedListType);

            closedMethod.MainReturnNode.AddReturnType();
            GraphUtil.ConnectTypePins(closedListTypeNode.OutputTypePins[0], closedMethod.MainReturnNode.InputTypePins[0]);

            DefaultNode closedDefaultNode = new DefaultNode(closedMethod);

            GraphUtil.ConnectTypePins(closedListTypeNode.OutputTypePins[0], closedDefaultNode.TypePin);
            GraphUtil.ConnectDataPins(closedDefaultNode.DefaultValuePin, closedMethod.MainReturnNode.InputDataPins[0]);

            GraphUtil.ConnectExecPins(closedMethod.EntryNode.InitialExecutionPin, closedMethod.ReturnNodes.First().ReturnPin);

            closedClass.Methods.Add(closedMethod);

            // Translate the classes

            ClassTranslator translator = new ClassTranslator();

            string openClassTranslated = translator.TranslateClass(openClass);

            string closedClassTranslated = translator.TranslateClass(closedClass);
        }