Пример #1
0
 public Tuple <int, CompileErrors> GenerateCode(string ModuleName)
 {
     try
     {
         //We check errors in source code
         CompileErrors errors = new CompileErrors();
         errors.Criticals    = CompileErrors.CheckFatalErrors(MainSource, MethodsSource);
         errors.NonCriticals = CompileErrors.CheckRecursions(MainSource, MethodsSource);
         if (errors.Criticals.Count > 0)
         {
             return(new Tuple <int, CompileErrors>(1, errors));
         }
         if (!Directory.Exists(Path.GetDirectoryName(ModuleName)))
         {
             Directory.CreateDirectory(Path.GetDirectoryName(ModuleName));
         }
         Directory.SetCurrentDirectory(Path.GetDirectoryName(ModuleName));
         AssemblyName    name = new AssemblyName(Path.GetFileName(ModuleName));
         AssemblyBuilder AB   = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save);
         ModuleBuilder   MB   = AB.DefineDynamicModule(Path.GetFileNameWithoutExtension(ModuleName) + ".exe");
         prog  = MB.DefineType("Program");
         charT = prog.DefineField("CharTable", typeof(char).MakeArrayType(2), FieldAttributes.Private | FieldAttributes.Static);
         CreateInitChar();
         InitMethodsInformation();
         CreateActionPerfomer();
         GenerateMethod(MainSource, MainMethod);
         for (int i = 0; i < Names.Count; i++)
         {
             GenerateMethod(MethodsSource[Names[i]], MethodsList[Names[i]]);
         }
         MethodBuilder entryPoint = prog.DefineMethod("Entry", MethodAttributes.Public | MethodAttributes.Static);
         ILGenerator   entryIL    = entryPoint.GetILGenerator();
         entryIL.EmitCall(OpCodes.Call, initChar, Type.EmptyTypes);
         entryIL.EmitCall(OpCodes.Call, MainMethod, Type.EmptyTypes);
         entryIL.EmitCall(OpCodes.Call, typeof(Console).GetMethod("WriteLine", Type.EmptyTypes), Type.EmptyTypes);
         entryIL.EmitCall(OpCodes.Call, typeof(Console).GetMethod("ReadKey", Type.EmptyTypes), Type.EmptyTypes);
         entryIL.Emit(OpCodes.Pop);
         entryIL.Emit(OpCodes.Ret);
         prog.CreateType();
         MB.CreateGlobalFunctions();
         AB.SetEntryPoint(entryPoint);
         AB.Save(name.FullName);
         return(new Tuple <int, CompileErrors>(0, errors));
     }
     catch
     {
         return(new Tuple <int, CompileErrors>(2, null));
     }
 }
Пример #2
0
 public bool Compile()
 {
     char[,] main = OpenedProject.Sources[OpenedProject.MainMethod];
     var keys = OpenedProject.Sources.Keys.ToList();
     Dictionary<char, char[,]> otherMethods = new Dictionary<char, char[,]>();
     foreach (string key in keys)
         if (key.Length == 1)
             otherMethods.Add(key[0], OpenedProject.Sources[key]);
     Compiler comp = new Compiler(main, otherMethods);
     Tuple<int, CompileErrors> res = comp.GenerateCode(Path.GetDirectoryName(OpenedProject.ProjectPath) + @"\" + Path.GetFileNameWithoutExtension(OpenedProject.ProjectPath) + ".exe");
     CompilationErrors = res.Item2;
     ShowErrors();
     return res.Item1 == 0;
 }