Beispiel #1
0
        /// <summary>
        /// Executes the translation process.
        /// </summary>
        /// <param name="context">The compilation context.</param>
        public void Execute(CompilationContext context)
        {
            if (context == null || context.Model == null)
                return;

            if (context.OutputStream == null || !context.OutputStream.CanWrite)
                throw new CompilationException("The translation output stream is null or not writable.");

            var translationCtx = new TranslationContext();
            translationCtx.OutputStream = context.OutputStream;

            // write enum declarations
            var enumTranslator = new EnumTranslator();
            foreach (var item in context.Model.Enums)
                enumTranslator.Translate(item, translationCtx);

            // write class declarations
            var classTranslator = new ClassTranslator();
            foreach (var item in ClassSorter.Sort(context.Model.Classes))
                classTranslator.Translate(item, translationCtx);

            // write global statements
            if (context.Model.GlobalStatements.Any())
            {
                translationCtx.WriteLine();
                foreach (var item in context.Model.GlobalStatements)
                    translationCtx.WriteModel(item);
            }
        }
Beispiel #2
0
        public void TestGenerics()
        {
            // Create the open class<T> which contains a List<T>

            GenericType genericClassArg = new GenericType("T");

            Class openClass = new Class();

            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;

            Method openMethod = new Method("OpenMethod");

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

            openClass.Methods.Add(openMethod);

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

            Class closedClass = new Class();

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

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

            Method closedMethod = new Method("ClosedMethod");

            closedMethod.ArgumentTypes.Add(closedListType);
            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);
        }
Beispiel #3
0
        public IEnumerable <string> GetGeneratedCode()
        {
            ClassTranslator classTranslator = new ClassTranslator();

            foreach (var project in dte.Solution.Projects.OfType <EnvDTE.Project>())
            {
                foreach (var projectItem in project.ProjectItems.OfType <EnvDTE.ProjectItem>())
                {
                    string fullPath = projectItem.Properties?.Item("FullPath")?.Value as string;
                    if (fullPath != null && fullPath.EndsWith(".netpc"))
                    {
                        ClassGraph classGraph = NetPrints.Serialization.SerializationHelper.LoadClass(fullPath);
                        yield return(classTranslator.TranslateClass(classGraph));
                    }
                }
            }
        }
        public void Setup()
        {
            classTranslator = new ClassTranslator();

            cls = new ClassGraph()
            {
                Name      = "TestClass",
                Namespace = "TestNamespace",
            };

            CreateStringLengthMethod();
            CreateMainMethod();

            cls.Variables.Add(new Variable(cls, "testVariable", TypeSpecifier.FromType <string>(), null, null, VariableModifiers.None));
            cls.Methods.Add(stringLengthMethod);
            cls.Methods.Add(mainMethod);
        }
Beispiel #5
0
        public void Setup()
        {
            classTranslator = new ClassTranslator();

            cls = new Class()
            {
                Name      = "TestClass",
                Namespace = "TestNamespace",
                SuperType = TypeSpecifier.FromType <object>()
            };

            CreateStringLengthMethod();
            CreateMainMethod();

            cls.Attributes.Add(new Variable("testVariable", TypeSpecifier.FromType <string>()));
            cls.Methods.Add(stringLengthMethod);
            cls.Methods.Add(mainMethod);
        }
        public void Setup()
        {
            classTranslator = new ClassTranslator();

            cls = new Class()
            {
                Name      = "TestClass",
                Namespace = "TestNamespace",
                SuperType = typeof(object)
            };

            CreateStringLengthMethod();
            CreateMainMethod();

            cls.Attributes.Add(new Variable("testVariable", typeof(string)));
            cls.Methods.Add(stringLengthMethod);
            cls.Methods.Add(mainMethod);
        }
Beispiel #7
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);
        }