Esempio n. 1
0
        public void Print(Tree.Nodes.Program treeProgram)
        {
            Console.WriteLine("program");

            List <ClassDeclaration> printableClassNodes = treeProgram.ClassNodes.Where(c => c.IsPrintable).ToList();

            for (int i = 0; i < printableClassNodes.Count; i++)
            {
                string ind = ((i + 1) == printableClassNodes.Count) ?
                             indentEndBar : indentBranchBar;
                string nextInd = (i + 1 == printableClassNodes.Count) ?
                                 indent : indentBar;

                var staticModifier = printableClassNodes[i].IsStatic ? "static" : "";
                Console.WriteLine($"{indent}" +
                                  $"{ind}{staticModifier} class {printableClassNodes[i].Name}");

                for (int k = 0; k < printableClassNodes[i].VarDeclarationNodes.Count; k++)
                {
                    bool last = (k + 1 == printableClassNodes[i].VarDeclarationNodes.Count) &&
                                (printableClassNodes[i].FunctionDeclarationNodes.Count == 0);

                    PrintStatement(indent + nextInd,
                                   printableClassNodes[i].VarDeclarationNodes[k],
                                   last);
                }

                for (int b = 0; b < printableClassNodes[i].FunctionDeclarationNodes.Count; b++)
                {
                    PrintFuncDeclaration(indent + nextInd,
                                         printableClassNodes[i].FunctionDeclarationNodes[b],
                                         b + 1 == printableClassNodes[i].FunctionDeclarationNodes.Count);
                }
            }
        }
Esempio n. 2
0
 public void GenerateProgram(Tree.Nodes.Program programNode)
 {
     //create all class and method builders
     foreach (var classNode in programNode.ClassNodes)
     {
         DeclareClass(classNode, module);
     }
     //generate code for all declared classes
     foreach (var classNode in programNode.ClassNodes)
     {
         GenerateClass(classNode, module);
     }
 }
Esempio n. 3
0
        public CodeGenerator(string assName, Tree.Nodes.Program programNode)
        {
            // create a dynamic assembly and module
            assemblyName      = new AssemblyName();
            assemblyName.Name = assName;

            assemblyBuilder = Thread.GetDomain()
                              .DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);

            module = assemblyBuilder.DefineDynamicModule(assName + ".exe", true);

            GenerateProgram(programNode);

            assemblyBuilder.SetEntryPoint(mainMethodbuilder, PEFileKinds.ConsoleApplication);

            assemblyBuilder.Save(assName + ".exe");
        }
Esempio n. 4
0
 public void GenerateCode(Tree.Nodes.Program program)
 {
     program.ClassNodes.ForEach(GenerateClassCode);
 }