Ejemplo n.º 1
0
        /// <summary>
        /// Compiles the specified Visua Studio document.
        /// </summary>
        /// <param name="itemPath">VS document item path</param>
        /// <param name="options">Assembler options to use</param>
        /// <param name="output">Assembler output</param>
        /// <returns>True, if compilation is successful; otherwise, false</returns>
        public async Task <AssemblerOutput> CompileDocument(string itemPath,
                                                            AssemblerOptions options)
        {
            var zxbOptions = PrepareZxbOptions(itemPath);

            MergeOptionsFromSource(zxbOptions);
            var output = new AssemblerOutput(new SourceFileItem(itemPath));
            var runner = new ZxbRunner(SpectNetPackage.Default.Options.ZxbPath);
            var result = await runner.RunAsync(zxbOptions);

            if (result.ExitCode != 0)
            {
                // --- Compile error - stop here
                output.Errors.Clear();
                output.Errors.AddRange(result.Errors);
                return(output);
            }

            // --- HACK: Take care that "ZXBASIC_HEAP_SIZE EQU" is added to the assembly file
            var asmContents      = File.ReadAllText(zxbOptions.OutputFilename);
            var hasHeapSizeLabel = Regex.Match(asmContents, "ZXBASIC_HEAP_SIZE\\s+EQU");

            if (!hasHeapSizeLabel.Success)
            {
                asmContents = Regex.Replace(asmContents, "ZXBASIC_USER_DATA_END\\s+EQU\\s+ZXBASIC_MEM_HEAP",
                                            "ZXBASIC_USER_DATA_END EQU ZXBASIC_USER_DATA");
                File.WriteAllText(zxbOptions.OutputFilename, asmContents);
            }

            // --- Second pass, compile the assembly file
            var compiler = new Z80Assembler();

            options.ProcExplicitLocalsOnly = true;
            if (_traceMessageHandler != null)
            {
                compiler.AssemblerMessageCreated += _traceMessageHandler;
            }
            compiler.AssemblerMessageCreated += OnAssemblerMessage;
            try
            {
                output           = compiler.CompileFile(zxbOptions.OutputFilename, options);
                output.ModelType = SpectrumModelType.Spectrum48;
            }
            finally
            {
                if (_traceMessageHandler != null)
                {
                    compiler.AssemblerMessageCreated -= _traceMessageHandler;
                }
                compiler.AssemblerMessageCreated -= OnAssemblerMessage;
            }
            return(output);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tests if the compiler is available.
        /// </summary>
        /// <returns>True, if the compiler is installed, and so available.</returns>
        public async Task <bool> IsAvailable()
        {
            var runner = new ZxbRunner(SpectNetPackage.Default.Options.ZxbPath, 10000);

            try
            {
                await runner.RunAsync(new ZxbOptions());
            }
            catch (Exception ex)
            {
                VsxDialogs.Show(string.Format(ZXB_NOT_FOUND_MESSAGE, ex.Message),
                                "Error when running ZXB", MessageBoxButton.OK, VsxMessageBoxIcon.Exclamation);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Compiles the specified Visua Studio document.
        /// </summary>
        /// <param name="itemPath">VS document item path</param>
        /// <param name="options">Assembler options to use</param>
        /// <returns>True, if compilation is successful; otherwise, false</returns>
        public async Task <AssemblerOutput> CompileDocument(string itemPath,
                                                            AssemblerOptions options)
        {
            var       addToProject = SpectNetPackage.Default.Options.StoreGeneratedZ80Files;
            var       zxbOptions   = PrepareZxbOptions(itemPath, addToProject);
            var       output       = new AssemblerOutput(new SourceFileItem(itemPath), options?.UseCaseSensitiveSymbols ?? false);
            var       runner       = new ZxbRunner(SpectNetPackage.Default.Options.ZxbPath);
            ZxbResult result;

            try
            {
                result = await runner.RunAsync(zxbOptions, true);
            }
            catch (Exception ex)
            {
                output.Errors.Clear();
                output.Errors.Add(new AssemblerErrorInfo("ZXB", "", 1, 0, ex.Message));
                return(output);
            }
            if (result.ExitCode != 0)
            {
                // --- Compile error - stop here
                output.Errors.Clear();
                output.Errors.AddRange(result.Errors);
                return(output);
            }

            // --- Add the generated file to the project
            if (addToProject)
            {
                var zxBasItem =
                    SpectNetPackage.Default.ActiveProject.ZxBasicProjectItems.FirstOrDefault(pi =>
                                                                                             pi.Filename == itemPath)?.DteProjectItem;
                if (SpectNetPackage.Default.Options.NestGeneratedZ80Files && zxBasItem != null)
                {
                    var newItem = zxBasItem.ProjectItems.AddFromFile(zxbOptions.OutputFilename);
                    newItem.Properties.Item("DependentUpon").Value = zxBasItem.Name;
                }
                else
                {
                    SpectNetPackage.Default.ActiveRoot.ProjectItems.AddFromFile(zxbOptions.OutputFilename);
                }
            }

            var asmContents = File.ReadAllText(zxbOptions.OutputFilename);

            ZxBasicNamespacePreprocessor preProc = new ZxBasicNamespacePreprocessor(asmContents);

            asmContents = "\t.zxbasic\r\n" + preProc.ProcessContent();
            var hasHeapSizeLabel = Regex.Match(asmContents, "ZXBASIC_HEAP_SIZE\\s+EQU");

            if (!hasHeapSizeLabel.Success)
            {
                // --- HACK: Take care that "ZXBASIC_HEAP_SIZE EQU" is added to the assembly file
                asmContents = Regex.Replace(asmContents, "ZXBASIC_USER_DATA_END\\s+EQU\\s+ZXBASIC_MEM_HEAP",
                                            "ZXBASIC_USER_DATA_END EQU ZXBASIC_USER_DATA");
            }
            File.WriteAllText(zxbOptions.OutputFilename, asmContents);

            // --- Second pass, compile the assembly file
            var compiler = new Z80Assembler();

            if (_traceMessageHandler != null)
            {
                compiler.AssemblerMessageCreated += _traceMessageHandler;
            }
            compiler.AssemblerMessageCreated += OnAssemblerMessage;
            try
            {
                output           = compiler.CompileFile(zxbOptions.OutputFilename, options);
                output.ModelType = SpectrumModelType.Spectrum48;
            }
            finally
            {
                if (_traceMessageHandler != null)
                {
                    compiler.AssemblerMessageCreated -= _traceMessageHandler;
                }
                compiler.AssemblerMessageCreated -= OnAssemblerMessage;
            }
            return(output);
        }