Пример #1
0
 public PurityCompiler(ModuleBuilder module, TypeBuilder dataClass, IMetadataContainer container,
     IRuntimeContainer runtimeContainer)
 {
     this.module = module;
     this.dataClass = dataClass;
     this.container = container;
     this.runtimeContainer = runtimeContainer;
     this.typeChecker = new TypeChecker(container);
     this.moduleImporter = new ModuleImporter(container, runtimeContainer);
 }
Пример #2
0
        public static void Main(string[] args)
        {
            int index = 0;
            int assemblyIndex = 0;

            var name = new AssemblyName(AssemblyName + assemblyIndex++);
            AppDomain domain = AppDomain.CurrentDomain;
            var assembly = domain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
            var module = assembly.DefineDynamicModule(AssemblyName);

            ModuleImporter moduleImporter = new ModuleImporter(container, runtimeContainer);

            Evaluator evaluator = new Evaluator(module, container, runtimeContainer);

            Console.TreatControlCAsInput = false;

            while (true)
            {
                try
                {
                    Console.Write(Prompt);

                    string line = Console.ReadLine();

                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        if (line[0] == CommandIntroduction)
                        {
                            switch (line[1])
                            {
                                case ClearCommand:
                                    // Clear console
                                    Console.Clear();
                                    break;
                                case ResetCommand:
                                    // Clear all data
                                    container.Clear();
                                    runtimeContainer.Clear();
                                    name = new AssemblyName(AssemblyName + assemblyIndex++);
                                    assembly = domain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
                                    module = assembly.DefineDynamicModule(AssemblyName);
                                    break;
                                case PrintDeclarationsCommand:
                                    // Print definitions
                                    PrintDeclarations();
                                    break;
                                case PrintTypeCommand:
                                    // Print type
                                    PrintType(line.Substring(2).Trim());
                                    break;
                                case EvaluateCommand:
                                    // Evaluate
                                    string result = evaluator.Evaluate(line.Substring(2).Trim());
                                    PrintResult(result);
                                    break;
                                default:
                                    PrintError("Unknown command.");
                                    break;
                            }
                        }
                        else
                        {
                            var dataClass = module.DefineType(Constants.DataClassName + index++,
                                TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Abstract);

                            var compiler = new PurityCompiler(module, dataClass, container, runtimeContainer);

                            var parseResult = ModuleParser.ParseProgramElement(line);

                            if (parseResult == null)
                            {
                                throw new CompilerException(ErrorMessages.UnableToParse);
                            }

                            try
                            {
                                compiler.Compile(parseResult.Output, module.Name);
                            }
                            catch (CompilerException ex)
                            {
                                PrintError(ex.Message);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    PrintError(ex.Message);
                }
            }
        }